xref: /php-src/ext/hash/tests/hash_equals.phpt (revision c5fe1a19)
1--TEST--
2Hash: hash_equals() test
3--FILE--
4<?php
5
6function trycatch_dump(...$tests) {
7    foreach ($tests as $test) {
8        try {
9            var_dump($test());
10        }
11        catch (\Error $e) {
12            echo '[' . get_class($e) . '] ' . $e->getMessage() . "\n";
13        }
14    }
15}
16
17trycatch_dump(
18    fn() => hash_equals("same", "same"),
19    fn() => hash_equals("not1same", "not2same"),
20    fn() => hash_equals("short", "longer"),
21    fn() => hash_equals("longer", "short"),
22    fn() => hash_equals("", "notempty"),
23    fn() => hash_equals("notempty", ""),
24    fn() => hash_equals("", ""),
25    fn() => hash_equals(123, "NaN"),
26    fn() => hash_equals("NaN", 123),
27    fn() => hash_equals(123, 123),
28    fn() => hash_equals(null, ""),
29    fn() => hash_equals(null, 123),
30    fn() => hash_equals(null, null),
31);
32
33?>
34--EXPECT--
35bool(true)
36bool(false)
37bool(false)
38bool(false)
39bool(false)
40bool(false)
41bool(true)
42[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, int given
43[TypeError] hash_equals(): Argument #2 ($user_string) must be of type string, int given
44[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, int given
45[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, null given
46[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, null given
47[TypeError] hash_equals(): Argument #1 ($known_string) must be of type string, null given
48