test_util_format.c 11 KB

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