Base32Test.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. use \ParagonIE\ConstantTime\Base32;
  3. class Base32Test extends PHPUnit\Framework\TestCase
  4. {
  5. /**
  6. * @covers Base32::encode()
  7. * @covers Base32::decode()
  8. * @covers Base32::encodeUpper()
  9. * @covers Base32::decodeUpper()
  10. */
  11. public function testRandom()
  12. {
  13. for ($i = 1; $i < 32; ++$i) {
  14. for ($j = 0; $j < 50; ++$j) {
  15. $random = \random_bytes($i);
  16. $enc = Base32::encode($random);
  17. $this->assertSame(
  18. $random,
  19. Base32::decode($enc)
  20. );
  21. $unpadded = \rtrim($enc, '=');
  22. $this->assertSame(
  23. $unpadded,
  24. Base32::encodeUnpadded($random)
  25. );
  26. $this->assertSame(
  27. $random,
  28. Base32::decode($unpadded)
  29. );
  30. $enc = Base32::encodeUpper($random);
  31. $this->assertSame(
  32. $random,
  33. Base32::decodeUpper($enc)
  34. );
  35. $unpadded = \rtrim($enc, '=');
  36. $this->assertSame(
  37. $unpadded,
  38. Base32::encodeUpperUnpadded($random)
  39. );
  40. $this->assertSame(
  41. $random,
  42. Base32::decodeUpper($unpadded)
  43. );
  44. }
  45. }
  46. }
  47. }