test_util_format.c 10 KB

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