test_util_format.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* Copyright (c) 2010-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include "core/or/or.h"
  5. #include "test/test.h"
  6. #include "lib/crypt_ops/crypto_rand.h"
  7. #define UTIL_FORMAT_PRIVATE
  8. #include "lib/encoding/binascii.h"
  9. #define NS_MODULE util_format
  10. static void
  11. test_util_format_unaligned_accessors(void *ignored)
  12. {
  13. (void)ignored;
  14. char buf[9] = "onionsoup"; // 6f6e696f6e736f7570
  15. tt_u64_op(get_uint64(buf+1), OP_EQ,
  16. tor_htonll(UINT64_C(0x6e696f6e736f7570)));
  17. tt_uint_op(get_uint32(buf+1), OP_EQ, htonl(0x6e696f6e));
  18. tt_uint_op(get_uint16(buf+1), OP_EQ, htons(0x6e69));
  19. tt_uint_op(get_uint8(buf+1), OP_EQ, 0x6e);
  20. set_uint8(buf+7, 0x61);
  21. tt_mem_op(buf, OP_EQ, "onionsoap", 9);
  22. set_uint16(buf+6, htons(0x746f));
  23. tt_mem_op(buf, OP_EQ, "onionstop", 9);
  24. set_uint32(buf+1, htonl(0x78696465));
  25. tt_mem_op(buf, OP_EQ, "oxidestop", 9);
  26. set_uint64(buf+1, tor_htonll(UINT64_C(0x6266757363617465)));
  27. tt_mem_op(buf, OP_EQ, "obfuscate", 9);
  28. done:
  29. ;
  30. }
  31. static void
  32. test_util_format_base64_encode(void *ignored)
  33. {
  34. (void)ignored;
  35. int res;
  36. int i;
  37. char *src;
  38. char *dst;
  39. src = tor_malloc_zero(256);
  40. dst = tor_malloc_zero(1000);
  41. for (i=0;i<256;i++) {
  42. src[i] = (char)i;
  43. }
  44. res = base64_encode(NULL, 1, src, 1, 0);
  45. tt_int_op(res, OP_EQ, -1);
  46. res = base64_encode(dst, 1, NULL, 1, 0);
  47. tt_int_op(res, OP_EQ, -1);
  48. res = base64_encode(dst, 1, src, 10, 0);
  49. tt_int_op(res, OP_EQ, -1);
  50. res = base64_encode(dst, SSIZE_MAX-1, src, 1, 0);
  51. tt_int_op(res, OP_EQ, -1);
  52. res = base64_encode(dst, SSIZE_MAX-1, src, 10, 0);
  53. tt_int_op(res, OP_EQ, -1);
  54. res = base64_encode(dst, 1000, src, 256, 0);
  55. tt_int_op(res, OP_EQ, 344);
  56. tt_str_op(dst, OP_EQ, "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh"
  57. "8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH"
  58. "SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3"
  59. "BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeY"
  60. "mZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wM"
  61. "HCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp"
  62. "6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==");
  63. res = base64_encode(dst, 1000, src, 256, BASE64_ENCODE_MULTILINE);
  64. tt_int_op(res, OP_EQ, 350);
  65. tt_str_op(dst, OP_EQ,
  66. "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"
  67. "MDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5f\n"
  68. "YGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P\n"
  69. "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/\n"
  70. "wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v\n"
  71. "8PHy8/T19vf4+fr7/P3+/w==\n");
  72. res = base64_encode(dst, 1000, src+1, 255, BASE64_ENCODE_MULTILINE);
  73. tt_int_op(res, OP_EQ, 346);
  74. for (i = 0;i<50;i++) {
  75. src[i] = 0;
  76. }
  77. src[50] = (char)255;
  78. src[51] = (char)255;
  79. src[52] = (char)255;
  80. src[53] = (char)255;
  81. res = base64_encode(dst, 1000, src, 54, BASE64_ENCODE_MULTILINE);
  82. tt_int_op(res, OP_EQ, 74);
  83. res = base64_encode(dst, 1000, src+1, 53, BASE64_ENCODE_MULTILINE);
  84. tt_int_op(res, OP_EQ, 74);
  85. res = base64_encode(dst, 1000, src+2, 52, BASE64_ENCODE_MULTILINE);
  86. tt_int_op(res, OP_EQ, 74);
  87. res = base64_encode(dst, 1000, src+3, 51, BASE64_ENCODE_MULTILINE);
  88. tt_int_op(res, OP_EQ, 70);
  89. res = base64_encode(dst, 1000, src+4, 50, BASE64_ENCODE_MULTILINE);
  90. tt_int_op(res, OP_EQ, 70);
  91. res = base64_encode(dst, 1000, src+5, 49, BASE64_ENCODE_MULTILINE);
  92. tt_int_op(res, OP_EQ, 70);
  93. res = base64_encode(dst, 1000, src+6, 48, BASE64_ENCODE_MULTILINE);
  94. tt_int_op(res, OP_EQ, 65);
  95. res = base64_encode(dst, 1000, src+7, 47, BASE64_ENCODE_MULTILINE);
  96. tt_int_op(res, OP_EQ, 65);
  97. res = base64_encode(dst, 1000, src+8, 46, BASE64_ENCODE_MULTILINE);
  98. tt_int_op(res, OP_EQ, 65);
  99. done:
  100. tor_free(src);
  101. tor_free(dst);
  102. }
  103. static void
  104. test_util_format_base64_decode_oddsize(void *ignored)
  105. {
  106. (void)ignored;
  107. int res;
  108. int i;
  109. char *src;
  110. char *dst, real_dst[7];
  111. char expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
  112. char real_src[] = "ZXhhbXBsZQ";
  113. char expected40[] = "testing40characteroddsizebase64encoding!";
  114. char src40[] = "dGVzdGluZzQwY2hhcmFjdGVyb2Rkc2l6ZWJhc2U2NGVuY29kaW5nIQ";
  115. char pad40[] = "dGVzdGluZzQwY2hhcmFjdGVyb2Rkc2l6ZWJhc2U2NGVuY29kaW5nIQ==";
  116. src = tor_malloc_zero(256);
  117. dst = tor_malloc_zero(1000);
  118. for (i=0;i<256;i++) {
  119. src[i] = (char)i;
  120. }
  121. res = base64_decode(dst, 1, src, 5);
  122. tt_int_op(res, OP_EQ, -1);
  123. const char *s = "SGVsbG8gd29ybGQ";
  124. res = base64_decode(dst, 1000, s, strlen(s));
  125. tt_int_op(res, OP_EQ, 11);
  126. tt_mem_op(dst, OP_EQ, "Hello world", 11);
  127. s = "T3BhIG11bmRv";
  128. res = base64_decode(dst, 9, s, strlen(s));
  129. tt_int_op(res, OP_EQ, 9);
  130. tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
  131. res = base64_decode(real_dst, sizeof(real_dst), real_src, 10);
  132. tt_int_op(res, OP_EQ, 7);
  133. tt_mem_op(real_dst, OP_EQ, expected, 7);
  134. res = base64_decode(dst, 40, src40, strlen(src40));
  135. tt_int_op(res, OP_EQ, 40);
  136. tt_mem_op(dst, OP_EQ, expected40, 40);
  137. res = base64_decode(dst, 40, pad40, strlen(pad40));
  138. tt_int_op(res, OP_EQ, 40);
  139. tt_mem_op(dst, OP_EQ, expected40, 40);
  140. done:
  141. tor_free(src);
  142. tor_free(dst);
  143. }
  144. static void
  145. test_util_format_base64_decode(void *ignored)
  146. {
  147. (void)ignored;
  148. int res;
  149. int i;
  150. char *src;
  151. char *dst, *real_dst;
  152. uint8_t expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
  153. char real_src[] = "ZXhhbXBsZQ==";
  154. src = tor_malloc_zero(256);
  155. dst = tor_malloc_zero(1000);
  156. real_dst = tor_malloc_zero(10);
  157. for (i=0;i<256;i++) {
  158. src[i] = (char)i;
  159. }
  160. res = base64_decode(dst, 1, src, 100);
  161. tt_int_op(res, OP_EQ, -1);
  162. res = base64_decode(dst, 1, real_src, 10);
  163. tt_int_op(res, OP_EQ, -1);
  164. const char *s = "T3BhIG11bmRv";
  165. res = base64_decode(dst, 9, s, strlen(s));
  166. tt_int_op(res, OP_EQ, 9);
  167. tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
  168. memset(dst, 0, 1000);
  169. res = base64_decode(dst, 100, s, strlen(s));
  170. tt_int_op(res, OP_EQ, 9);
  171. tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
  172. s = "SGVsbG8gd29ybGQ=";
  173. res = base64_decode(dst, 100, s, strlen(s));
  174. tt_int_op(res, OP_EQ, 11);
  175. tt_mem_op(dst, OP_EQ, "Hello world", 11);
  176. res = base64_decode(real_dst, 10, real_src, 10);
  177. tt_int_op(res, OP_EQ, 7);
  178. tt_mem_op(real_dst, OP_EQ, expected, 7);
  179. done:
  180. tor_free(src);
  181. tor_free(dst);
  182. tor_free(real_dst);
  183. }
  184. static void
  185. test_util_format_base16_decode(void *ignored)
  186. {
  187. (void)ignored;
  188. int res;
  189. int i;
  190. char *src;
  191. char *dst, *real_dst;
  192. char expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
  193. char real_src[] = "6578616D706C65";
  194. src = tor_malloc_zero(256);
  195. dst = tor_malloc_zero(1000);
  196. real_dst = tor_malloc_zero(10);
  197. for (i=0;i<256;i++) {
  198. src[i] = (char)i;
  199. }
  200. res = base16_decode(dst, 3, src, 3);
  201. tt_int_op(res, OP_EQ, -1);
  202. res = base16_decode(dst, 1, src, 10);
  203. tt_int_op(res, OP_EQ, -1);
  204. res = base16_decode(dst, ((size_t)INT_MAX)+1, src, 10);
  205. tt_int_op(res, OP_EQ, -1);
  206. res = base16_decode(dst, 1000, "", 0);
  207. tt_int_op(res, OP_EQ, 0);
  208. res = base16_decode(dst, 1000, "aabc", 4);
  209. tt_int_op(res, OP_EQ, 2);
  210. tt_mem_op(dst, OP_EQ, "\xaa\xbc", 2);
  211. res = base16_decode(dst, 1000, "aabcd", 6);
  212. tt_int_op(res, OP_EQ, -1);
  213. res = base16_decode(dst, 1000, "axxx", 4);
  214. tt_int_op(res, OP_EQ, -1);
  215. res = base16_decode(real_dst, 10, real_src, 14);
  216. tt_int_op(res, OP_EQ, 7);
  217. tt_mem_op(real_dst, OP_EQ, expected, 7);
  218. done:
  219. tor_free(src);
  220. tor_free(dst);
  221. tor_free(real_dst);
  222. }
  223. static void
  224. test_util_format_base32_encode(void *arg)
  225. {
  226. (void) arg;
  227. size_t real_dstlen = 32;
  228. char *dst = tor_malloc_zero(real_dstlen);
  229. /* Basic use case that doesn't require a source length correction. */
  230. {
  231. /* Length of 10 bytes. */
  232. const char *src = "blahbleh12";
  233. size_t srclen = strlen(src);
  234. /* Expected result encoded base32. This was created using python as
  235. * such (and same goes for all test case.):
  236. *
  237. * b = bytes("blahbleh12", 'utf-8')
  238. * base64.b32encode(b)
  239. * (result in lower case)
  240. */
  241. const char *expected = "mjwgc2dcnrswqmjs";
  242. base32_encode(dst, base32_encoded_size(srclen), src, srclen);
  243. tt_mem_op(expected, OP_EQ, dst, strlen(expected));
  244. /* Encode but to a larger size destination. */
  245. memset(dst, 0, real_dstlen);
  246. base32_encode(dst, real_dstlen, src, srclen);
  247. tt_mem_op(expected, OP_EQ, dst, strlen(expected));
  248. }
  249. /* Non multiple of 5 for the source buffer length. */
  250. {
  251. /* Length of 8 bytes. */
  252. const char *expected = "mjwgc2dcnrswq";
  253. const char *src = "blahbleh";
  254. size_t srclen = strlen(src);
  255. memset(dst, 0, real_dstlen);
  256. base32_encode(dst, base32_encoded_size(srclen), src, srclen);
  257. tt_mem_op(expected, OP_EQ, dst, strlen(expected));
  258. }
  259. done:
  260. tor_free(dst);
  261. }
  262. static void
  263. test_util_format_base32_decode(void *arg)
  264. {
  265. (void) arg;
  266. int ret;
  267. size_t real_dstlen = 32;
  268. char *dst = tor_malloc_zero(real_dstlen);
  269. /* Basic use case. */
  270. {
  271. /* Length of 10 bytes. */
  272. const char *expected = "blahbleh12";
  273. /* Expected result encoded base32. */
  274. const char *src = "mjwgc2dcnrswqmjs";
  275. ret = base32_decode(dst, strlen(expected), src, strlen(src));
  276. tt_int_op(ret, OP_EQ, 0);
  277. tt_str_op(expected, OP_EQ, dst);
  278. }
  279. /* Non multiple of 5 for the source buffer length. */
  280. {
  281. /* Length of 8 bytes. */
  282. const char *expected = "blahbleh";
  283. const char *src = "mjwgc2dcnrswq";
  284. ret = base32_decode(dst, strlen(expected), src, strlen(src));
  285. tt_int_op(ret, OP_EQ, 0);
  286. tt_mem_op(expected, OP_EQ, dst, strlen(expected));
  287. }
  288. /* Invalid values. */
  289. {
  290. /* Invalid character '#'. */
  291. ret = base32_decode(dst, real_dstlen, "#abcde", 6);
  292. tt_int_op(ret, OP_EQ, -1);
  293. /* Make sure the destination buffer has been zeroed even on error. */
  294. tt_int_op(tor_mem_is_zero(dst, real_dstlen), OP_EQ, 1);
  295. }
  296. done:
  297. tor_free(dst);
  298. }
  299. static void
  300. test_util_format_encoded_size(void *arg)
  301. {
  302. (void)arg;
  303. uint8_t inbuf[256];
  304. char outbuf[1024];
  305. unsigned i;
  306. crypto_rand((char *)inbuf, sizeof(inbuf));
  307. for (i = 0; i <= sizeof(inbuf); ++i) {
  308. /* XXXX (Once the return values are consistent, check them too.) */
  309. base32_encode(outbuf, sizeof(outbuf), (char *)inbuf, i);
  310. /* The "+ 1" below is an API inconsistency. */
  311. tt_int_op(strlen(outbuf) + 1, OP_EQ, base32_encoded_size(i));
  312. base64_encode(outbuf, sizeof(outbuf), (char *)inbuf, i, 0);
  313. tt_int_op(strlen(outbuf), OP_EQ, base64_encode_size(i, 0));
  314. base64_encode(outbuf, sizeof(outbuf), (char *)inbuf, i,
  315. BASE64_ENCODE_MULTILINE);
  316. tt_int_op(strlen(outbuf), OP_EQ,
  317. base64_encode_size(i, BASE64_ENCODE_MULTILINE));
  318. }
  319. done:
  320. ;
  321. }
  322. struct testcase_t util_format_tests[] = {
  323. { "unaligned_accessors", test_util_format_unaligned_accessors, 0,
  324. NULL, NULL },
  325. { "base64_encode", test_util_format_base64_encode, 0, NULL, NULL },
  326. { "base64_decode_oddsize", test_util_format_base64_decode_oddsize, 0,
  327. NULL, NULL },
  328. { "base64_decode", test_util_format_base64_decode, 0, NULL, NULL },
  329. { "base16_decode", test_util_format_base16_decode, 0, NULL, NULL },
  330. { "base32_encode", test_util_format_base32_encode, 0,
  331. NULL, NULL },
  332. { "base32_decode", test_util_format_base32_decode, 0,
  333. NULL, NULL },
  334. { "encoded_size", test_util_format_encoded_size, 0, NULL, NULL },
  335. END_OF_TESTCASES
  336. };