xref: /PHP-8.2/ext/hash/tests/hash_hkdf_error.phpt (revision 9d9bcc2b)
1--TEST--
2Hash: hash_hkdf() function: error conditions
3--FILE--
4<?php
5
6error_reporting(E_ALL);
7
8function trycatch_dump(...$tests) {
9    foreach ($tests as $test) {
10        try {
11            var_dump($test());
12        }
13        catch (\Error $e) {
14            echo '[' . get_class($e) . '] ' . $e->getMessage() . "\n";
15        }
16    }
17}
18
19$ikm = 'input key material';
20
21echo "*** Testing hash_hkdf(): error conditions ***\n";
22
23echo "\n-- Testing hash_hkdf() function with invalid hash algorithm --\n";
24trycatch_dump(
25    fn() => hash_hkdf('foo', $ikm)
26);
27
28echo "\n-- Testing hash_hkdf() function with non-cryptographic hash algorithm --\n";
29trycatch_dump(
30    fn() => hash_hkdf('adler32', $ikm),
31    fn() => hash_hkdf('crc32', $ikm),
32    fn() => hash_hkdf('crc32b', $ikm),
33    fn() => hash_hkdf('fnv132', $ikm),
34    fn() => hash_hkdf('fnv1a32', $ikm),
35    fn() => hash_hkdf('fnv164', $ikm),
36    fn() => hash_hkdf('fnv1a64', $ikm),
37    fn() => hash_hkdf('joaat', $ikm)
38);
39
40echo "\n-- Testing hash_hkdf() function with invalid parameters --\n";
41trycatch_dump(
42    fn() => hash_hkdf('sha1', ''),
43    fn() => hash_hkdf('sha1', $ikm, -1),
44    fn() => hash_hkdf('sha1', $ikm, 20 * 255 + 1) // Length can't be more than 255 times the hash digest size
45)
46?>
47--EXPECT--
48*** Testing hash_hkdf(): error conditions ***
49
50-- Testing hash_hkdf() function with invalid hash algorithm --
51[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
52
53-- Testing hash_hkdf() function with non-cryptographic hash algorithm --
54[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
55[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
56[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
57[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
58[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
59[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
60[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
61[ValueError] hash_hkdf(): Argument #1 ($algo) must be a valid cryptographic hashing algorithm
62
63-- Testing hash_hkdf() function with invalid parameters --
64[ValueError] hash_hkdf(): Argument #2 ($key) cannot be empty
65[ValueError] hash_hkdf(): Argument #3 ($length) must be greater than or equal to 0
66[ValueError] hash_hkdf(): Argument #3 ($length) must be less than or equal to 5100
67