Base64UrlSafeTest.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. use \ParagonIE\ConstantTime\Base64UrlSafe;
  3. class Base64UrlSafeTest extends PHPUnit_Framework_TestCase
  4. {
  5. /**
  6. * @covers Base64UrlSafe::encode()
  7. * @covers Base64UrlSafe::decode()
  8. */
  9. public function testRandom()
  10. {
  11. for ($i = 1; $i < 32; ++$i) {
  12. for ($j = 0; $j < 50; ++$j) {
  13. $random = \random_bytes($i);
  14. $enc = Base64UrlSafe::encode($random);
  15. $this->assertSame(
  16. $random,
  17. Base64UrlSafe::decode($enc)
  18. );
  19. $this->assertSame(
  20. \strtr(\base64_encode($random), '+/', '-_'),
  21. $enc
  22. );
  23. $unpadded = \rtrim($enc, '=');
  24. $this->assertSame(
  25. $unpadded,
  26. Base64UrlSafe::encodeUnpadded($random)
  27. );
  28. $this->assertSame(
  29. $random,
  30. Base64UrlSafe::decode($unpadded)
  31. );
  32. }
  33. }
  34. }
  35. }