hash_equals.phpt 1.4 KB

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