EncodingTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. use \ParagonIE\ConstantTime\Base32;
  3. use \ParagonIE\ConstantTime\Base32Hex;
  4. use \ParagonIE\ConstantTime\Base64;
  5. use \ParagonIE\ConstantTime\Base64DotSlash;
  6. use \ParagonIE\ConstantTime\Base64DotSlashOrdered;
  7. use \ParagonIE\ConstantTime\Base64UrlSafe;
  8. use \ParagonIE\ConstantTime\Encoding;
  9. use \ParagonIE\ConstantTime\Hex;
  10. class EncodingTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function testBase32Encode()
  13. {
  14. $this->assertSame(
  15. Encoding::base32Encode("\x00"),
  16. 'aa======'
  17. );
  18. $this->assertSame(
  19. Encoding::base32Encode("\x00\x00"),
  20. 'aaaa===='
  21. );
  22. $this->assertSame(
  23. Encoding::base32Encode("\x00\x00\x00"),
  24. 'aaaaa==='
  25. );
  26. $this->assertSame(
  27. Encoding::base32Encode("\x00\x00\x00\x00"),
  28. 'aaaaaaa='
  29. );
  30. $this->assertSame(
  31. Encoding::base32Encode("\x00\x00\x00\x00\x00"),
  32. 'aaaaaaaa'
  33. );
  34. $this->assertSame(
  35. Encoding::base32Encode("\x00\x00\x0F\xFF\xFF"),
  36. 'aaaa7777'
  37. );
  38. $this->assertSame(
  39. Encoding::base32Encode("\xFF\xFF\xF0\x00\x00"),
  40. '7777aaaa'
  41. );
  42. $this->assertSame(
  43. Encoding::base32Encode("\xce\x73\x9c\xe7\x39"),
  44. 'zzzzzzzz'
  45. );
  46. $this->assertSame(
  47. Encoding::base32Encode("\xd6\xb5\xad\x6b\x5a"),
  48. '22222222'
  49. );
  50. $this->assertSame(
  51. Base32::encodeUpper("\x00"),
  52. 'AA======'
  53. );
  54. $this->assertSame(
  55. Base32::encodeUpper("\x00\x00"),
  56. 'AAAA===='
  57. );
  58. $this->assertSame(
  59. Base32::encodeUpper("\x00\x00\x00"),
  60. 'AAAAA==='
  61. );
  62. $this->assertSame(
  63. Base32::encodeUpper("\x00\x00\x00\x00"),
  64. 'AAAAAAA='
  65. );
  66. $this->assertSame(
  67. Base32::encodeUpper("\x00\x00\x00\x00\x00"),
  68. 'AAAAAAAA'
  69. );
  70. $this->assertSame(
  71. Base32::encodeUpper("\x00\x00\x0F\xFF\xFF"),
  72. 'AAAA7777'
  73. );
  74. $this->assertSame(
  75. Base32::encodeUpper("\xFF\xFF\xF0\x00\x00"),
  76. '7777AAAA'
  77. );
  78. $this->assertSame(
  79. Base32::encodeUpper("\xce\x73\x9c\xe7\x39"),
  80. 'ZZZZZZZZ'
  81. );
  82. $this->assertSame(
  83. Base32::encodeUpper("\xd6\xb5\xad\x6b\x5a"),
  84. '22222222'
  85. );
  86. }
  87. public function testBase32Hex()
  88. {
  89. $this->assertSame(
  90. Base32Hex::encode("\x00"),
  91. '00======'
  92. );
  93. $this->assertSame(
  94. Base32Hex::encode("\x00\x00"),
  95. '0000===='
  96. );
  97. $this->assertSame(
  98. Base32Hex::encode("\x00\x00\x00"),
  99. '00000==='
  100. );
  101. $this->assertSame(
  102. Base32Hex::encode("\x00\x00\x00\x00"),
  103. '0000000='
  104. );
  105. $this->assertSame(
  106. Base32Hex::encode("\x00\x00\x00\x00\x00"),
  107. '00000000'
  108. );
  109. $this->assertSame(
  110. Base32Hex::encode("\x00\x00\x0F\xFF\xFF"),
  111. '0000vvvv'
  112. );
  113. $this->assertSame(
  114. Base32Hex::encode("\xFF\xFF\xF0\x00\x00"),
  115. 'vvvv0000'
  116. );
  117. }
  118. /**
  119. * Based on test vectors from RFC 4648
  120. */
  121. public function testBase32Decode()
  122. {
  123. $this->assertSame(
  124. "\x00\x00\x00\x00\x00\x00",
  125. Encoding::base32Decode('aaaaaaaaaa======')
  126. );
  127. $this->assertSame(
  128. "\x00\x00\x00\x00\x00\x00\x00",
  129. Encoding::base32Decode('aaaaaaaaaaaa====')
  130. );
  131. $this->assertSame(
  132. "\x00\x00\x00\x00\x00\x00\x00\x00",
  133. Encoding::base32Decode('aaaaaaaaaaaaa===')
  134. );
  135. $this->assertSame(
  136. "\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  137. Encoding::base32Decode('aaaaaaaaaaaaaaa=')
  138. );
  139. $this->assertSame(
  140. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  141. Encoding::base32Decode('aaaaaaaaaaaaaaaa')
  142. );
  143. $this->assertSame(
  144. "\x00",
  145. Encoding::base32Decode('aa======')
  146. );
  147. $this->assertSame(
  148. "\x00\x00",
  149. Encoding::base32Decode('aaaa====')
  150. );
  151. $this->assertSame(
  152. "\x00\x00\x00",
  153. Encoding::base32Decode('aaaaa===')
  154. );
  155. $this->assertSame(
  156. "\x00\x00\x00\x00",
  157. Encoding::base32Decode('aaaaaaa=')
  158. );
  159. $this->assertSame(
  160. "\x00\x00\x00\x00\x00",
  161. Encoding::base32Decode('aaaaaaaa')
  162. );
  163. $this->assertSame(
  164. "\x00\x00\x0F\xFF\xFF",
  165. Encoding::base32Decode('aaaa7777')
  166. );
  167. $this->assertSame(
  168. "\xFF\xFF\xF0\x00\x00",
  169. Encoding::base32Decode('7777aaaa')
  170. );
  171. $this->assertSame(
  172. "\xce\x73\x9c\xe7\x39",
  173. Encoding::base32Decode('zzzzzzzz')
  174. );
  175. $this->assertSame(
  176. "\xd6\xb5\xad\x6b\x5a",
  177. Encoding::base32Decode('22222222')
  178. );
  179. $this->assertSame(
  180. 'foobar',
  181. Encoding::base32Decode('mzxw6ytboi======')
  182. );
  183. $rand = random_bytes(9);
  184. $enc = Encoding::base32Encode($rand);
  185. $this->assertSame(
  186. Encoding::base32Encode($rand),
  187. Encoding::base32Encode(Encoding::base32Decode($enc))
  188. );
  189. $this->assertSame(
  190. $rand,
  191. Encoding::base32Decode($enc)
  192. );
  193. }
  194. /**
  195. * @covers Encoding::hexDecode()
  196. * @covers Encoding::hexEncode()
  197. * @covers Encoding::base32Decode()
  198. * @covers Encoding::base32Encode()
  199. * @covers Encoding::base64Decode()
  200. * @covers Encoding::base64Encode()
  201. * @covers Encoding::base64DotSlashDecode()
  202. * @covers Encoding::base64DotSlashEncode()
  203. * @covers Encoding::base64DotSlashOrderedDecode()
  204. * @covers Encoding::base64DotSlashOrderedEncode()
  205. */
  206. public function testBasicEncoding()
  207. {
  208. // Re-run the test at least 3 times for each length
  209. for ($j = 0; $j < 3; ++$j) {
  210. for ($i = 1; $i < 84; ++$i) {
  211. $rand = random_bytes($i);
  212. $enc = Encoding::hexEncode($rand);
  213. $this->assertSame(
  214. \bin2hex($rand),
  215. $enc,
  216. "Hex Encoding - Length: " . $i
  217. );
  218. $this->assertSame(
  219. $rand,
  220. Encoding::hexDecode($enc),
  221. "Hex Encoding - Length: " . $i
  222. );
  223. // Uppercase variant:
  224. $enc = Hex::encodeUpper($rand);
  225. $this->assertSame(
  226. \strtoupper(\bin2hex($rand)),
  227. $enc,
  228. "Hex Encoding - Length: " . $i
  229. );
  230. $this->assertSame(
  231. $rand,
  232. Hex::decode($enc),
  233. "HexUpper Encoding - Length: " . $i
  234. );
  235. $enc = Encoding::base32Encode($rand);
  236. $this->assertSame(
  237. $rand,
  238. Encoding::base32Decode($enc),
  239. "Base32 Encoding - Length: " . $i
  240. );
  241. $enc = Encoding::base32EncodeUpper($rand);
  242. $this->assertSame(
  243. $rand,
  244. Encoding::base32DecodeUpper($enc),
  245. "Base32Upper Encoding - Length: " . $i
  246. );
  247. $enc = Encoding::base32HexEncode($rand);
  248. $this->assertSame(
  249. bin2hex($rand),
  250. bin2hex(Encoding::base32HexDecode($enc)),
  251. "Base32Hex Encoding - Length: " . $i
  252. );
  253. $enc = Encoding::base32HexEncodeUpper($rand);
  254. $this->assertSame(
  255. bin2hex($rand),
  256. bin2hex(Encoding::base32HexDecodeUpper($enc)),
  257. "Base32HexUpper Encoding - Length: " . $i
  258. );
  259. $enc = Encoding::base64Encode($rand);
  260. $this->assertSame(
  261. $rand,
  262. Encoding::base64Decode($enc),
  263. "Base64 Encoding - Length: " . $i
  264. );
  265. $enc = Encoding::base64EncodeDotSlash($rand);
  266. $this->assertSame(
  267. $rand,
  268. Encoding::base64DecodeDotSlash($enc),
  269. "Base64 DotSlash Encoding - Length: " . $i
  270. );
  271. $enc = Encoding::base64EncodeDotSlashOrdered($rand);
  272. $this->assertSame(
  273. $rand,
  274. Encoding::base64DecodeDotSlashOrdered($enc),
  275. "Base64 Ordered DotSlash Encoding - Length: " . $i
  276. );
  277. $enc = Base64UrlSafe::encode($rand);
  278. $this->assertSame(
  279. \strtr(\base64_encode($rand), '+/', '-_'),
  280. $enc
  281. );
  282. $this->assertSame(
  283. $rand,
  284. Base64UrlSafe::decode($enc)
  285. );
  286. }
  287. }
  288. }
  289. }