test_util_format.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Copyright (c) 2010-2015, 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_base64_encode(void *ignored)
  11. {
  12. (void)ignored;
  13. int res;
  14. int i;
  15. char *src;
  16. char *dst;
  17. src = tor_malloc_zero(256);
  18. dst = tor_malloc_zero(1000);
  19. for (i=0;i<256;i++) {
  20. src[i] = (char)i;
  21. }
  22. res = base64_encode(NULL, 1, src, 1, 0);
  23. tt_int_op(res, OP_EQ, -1);
  24. res = base64_encode(dst, 1, NULL, 1, 0);
  25. tt_int_op(res, OP_EQ, -1);
  26. res = base64_encode(dst, 1, src, 10, 0);
  27. tt_int_op(res, OP_EQ, -1);
  28. res = base64_encode(dst, SSIZE_MAX-1, src, 1, 0);
  29. tt_int_op(res, OP_EQ, -1);
  30. res = base64_encode(dst, SSIZE_MAX-1, src, 10, 0);
  31. tt_int_op(res, OP_EQ, -1);
  32. res = base64_encode(dst, 1000, src, 256, 0);
  33. tt_int_op(res, OP_EQ, 344);
  34. tt_str_op(dst, OP_EQ, "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh"
  35. "8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH"
  36. "SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3"
  37. "BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeY"
  38. "mZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wM"
  39. "HCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp"
  40. "6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==");
  41. res = base64_encode(dst, 1000, src, 256, BASE64_ENCODE_MULTILINE);
  42. tt_int_op(res, OP_EQ, 350);
  43. tt_str_op(dst, OP_EQ,
  44. "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"
  45. "MDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5f\n"
  46. "YGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P\n"
  47. "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/\n"
  48. "wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v\n"
  49. "8PHy8/T19vf4+fr7/P3+/w==\n");
  50. res = base64_encode(dst, 1000, src+1, 255, BASE64_ENCODE_MULTILINE);
  51. tt_int_op(res, OP_EQ, 346);
  52. for (i = 0;i<50;i++) {
  53. src[i] = 0;
  54. }
  55. src[50] = 255;
  56. src[51] = 255;
  57. src[52] = 255;
  58. src[53] = 255;
  59. res = base64_encode(dst, 1000, src, 54, BASE64_ENCODE_MULTILINE);
  60. tt_int_op(res, OP_EQ, 74);
  61. res = base64_encode(dst, 1000, src+1, 53, BASE64_ENCODE_MULTILINE);
  62. tt_int_op(res, OP_EQ, 74);
  63. res = base64_encode(dst, 1000, src+2, 52, BASE64_ENCODE_MULTILINE);
  64. tt_int_op(res, OP_EQ, 74);
  65. res = base64_encode(dst, 1000, src+3, 51, BASE64_ENCODE_MULTILINE);
  66. tt_int_op(res, OP_EQ, 70);
  67. res = base64_encode(dst, 1000, src+4, 50, BASE64_ENCODE_MULTILINE);
  68. tt_int_op(res, OP_EQ, 70);
  69. res = base64_encode(dst, 1000, src+5, 49, BASE64_ENCODE_MULTILINE);
  70. tt_int_op(res, OP_EQ, 70);
  71. res = base64_encode(dst, 1000, src+6, 48, BASE64_ENCODE_MULTILINE);
  72. tt_int_op(res, OP_EQ, 65);
  73. res = base64_encode(dst, 1000, src+7, 47, BASE64_ENCODE_MULTILINE);
  74. tt_int_op(res, OP_EQ, 65);
  75. res = base64_encode(dst, 1000, src+8, 46, BASE64_ENCODE_MULTILINE);
  76. tt_int_op(res, OP_EQ, 65);
  77. done:
  78. tor_free(src);
  79. tor_free(dst);
  80. }
  81. static void
  82. test_util_format_base64_decode_nopad(void *ignored)
  83. {
  84. (void)ignored;
  85. int res;
  86. int i;
  87. char *src;
  88. uint8_t *dst, *real_dst;
  89. uint8_t expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
  90. char real_src[] = "ZXhhbXBsZQ";
  91. src = tor_malloc_zero(256);
  92. dst = tor_malloc_zero(1000);
  93. real_dst = tor_malloc_zero(10);
  94. for (i=0;i<256;i++) {
  95. src[i] = (char)i;
  96. }
  97. res = base64_decode_nopad(dst, 1, src, SIZE_T_CEILING);
  98. tt_int_op(res, OP_EQ, -1);
  99. res = base64_decode_nopad(dst, 1, src, 5);
  100. tt_int_op(res, OP_EQ, -1);
  101. const char *s = "SGVsbG8gd29ybGQ";
  102. res = base64_decode_nopad(dst, 1000, s, strlen(s));
  103. tt_int_op(res, OP_EQ, 11);
  104. tt_mem_op(dst, OP_EQ, "Hello world", 11);
  105. s = "T3BhIG11bmRv";
  106. res = base64_decode_nopad(dst, 9, s, strlen(s));
  107. tt_int_op(res, OP_EQ, 9);
  108. tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
  109. res = base64_decode_nopad(real_dst, 10, real_src, 10);
  110. tt_int_op(res, OP_EQ, 7);
  111. tt_mem_op(real_dst, OP_EQ, expected, 7);
  112. done:
  113. tor_free(src);
  114. tor_free(dst);
  115. tor_free(real_dst);
  116. }
  117. static void
  118. test_util_format_base64_decode(void *ignored)
  119. {
  120. (void)ignored;
  121. int res;
  122. int i;
  123. char *src;
  124. char *dst, *real_dst;
  125. uint8_t expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
  126. char real_src[] = "ZXhhbXBsZQ==";
  127. src = tor_malloc_zero(256);
  128. dst = tor_malloc_zero(1000);
  129. real_dst = tor_malloc_zero(10);
  130. for (i=0;i<256;i++) {
  131. src[i] = (char)i;
  132. }
  133. res = base64_decode(dst, 1, src, SIZE_T_CEILING);
  134. tt_int_op(res, OP_EQ, -1);
  135. res = base64_decode(dst, SIZE_T_CEILING+1, src, 10);
  136. tt_int_op(res, OP_EQ, -1);
  137. const char *s = "T3BhIG11bmRv";
  138. res = base64_decode(dst, 9, s, strlen(s));
  139. tt_int_op(res, OP_EQ, 9);
  140. tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
  141. memset(dst, 0, 1000);
  142. res = base64_decode(dst, 100, s, strlen(s));
  143. tt_int_op(res, OP_EQ, 9);
  144. tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
  145. s = "SGVsbG8gd29ybGQ=";
  146. res = base64_decode(dst, 100, s, strlen(s));
  147. tt_int_op(res, OP_EQ, 11);
  148. tt_mem_op(dst, OP_EQ, "Hello world", 11);
  149. res = base64_decode(real_dst, 10, real_src, 10);
  150. tt_int_op(res, OP_EQ, 7);
  151. tt_mem_op(real_dst, OP_EQ, expected, 7);
  152. done:
  153. tor_free(src);
  154. tor_free(dst);
  155. tor_free(real_dst);
  156. }
  157. static void
  158. test_util_format_base16_decode(void *ignored)
  159. {
  160. (void)ignored;
  161. int res;
  162. int i;
  163. char *src;
  164. char *dst, *real_dst;
  165. char expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
  166. char real_src[] = "6578616D706C65";
  167. src = tor_malloc_zero(256);
  168. dst = tor_malloc_zero(1000);
  169. real_dst = tor_malloc_zero(10);
  170. for (i=0;i<256;i++) {
  171. src[i] = (char)i;
  172. }
  173. res = base16_decode(dst, 3, src, 3);
  174. tt_int_op(res, OP_EQ, -1);
  175. res = base16_decode(dst, 1, src, 10);
  176. tt_int_op(res, OP_EQ, -1);
  177. res = base16_decode(dst, SIZE_T_CEILING+2, src, 10);
  178. tt_int_op(res, OP_EQ, -1);
  179. res = base16_decode(dst, 1000, "", 0);
  180. tt_int_op(res, OP_EQ, 0);
  181. res = base16_decode(dst, 1000, "aabc", 4);
  182. tt_int_op(res, OP_EQ, 0);
  183. tt_mem_op(dst, OP_EQ, "\xaa\xbc", 2);
  184. res = base16_decode(dst, 1000, "aabcd", 6);
  185. tt_int_op(res, OP_EQ, -1);
  186. res = base16_decode(dst, 1000, "axxx", 4);
  187. tt_int_op(res, OP_EQ, -1);
  188. res = base16_decode(real_dst, 10, real_src, 14);
  189. tt_int_op(res, OP_EQ, 0);
  190. tt_mem_op(real_dst, OP_EQ, expected, 7);
  191. done:
  192. tor_free(src);
  193. tor_free(dst);
  194. tor_free(real_dst);
  195. }
  196. struct testcase_t util_format_tests[] = {
  197. { "base64_encode", test_util_format_base64_encode, 0, NULL, NULL },
  198. { "base64_decode_nopad", test_util_format_base64_decode_nopad, 0,
  199. NULL, NULL },
  200. { "base64_decode", test_util_format_base64_decode, 0, NULL, NULL },
  201. { "base16_decode", test_util_format_base16_decode, 0, NULL, NULL },
  202. END_OF_TESTCASES
  203. };