test_util_format.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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,
  35. "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8g"
  36. "ISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BB"
  37. "QkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFi"
  38. "Y2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKD"
  39. "hIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOk"
  40. "paanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TF"
  41. "xsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm"
  42. "5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==");
  43. res = base64_encode(dst, 1000, src, 256, BASE64_ENCODE_MULTILINE);
  44. tt_int_op(res, OP_EQ, 350);
  45. tt_str_op(dst, OP_EQ,
  46. "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8g"
  47. "ISIjJCUmJygpKissLS4v\n"
  48. "MDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9Q"
  49. "UVJTVFVWV1hZWltcXV5f\n"
  50. "YGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+A"
  51. "gYKDhIWGh4iJiouMjY6P\n"
  52. "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+w"
  53. "sbKztLW2t7i5uru8vb6/\n"
  54. "wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g"
  55. "4eLj5OXm5+jp6uvs7e7v\n"
  56. "8PHy8/T19vf4+fr7/P3+/w==\n");
  57. res = base64_encode(dst, 1000, src+1, 255, BASE64_ENCODE_MULTILINE);
  58. tt_int_op(res, OP_EQ, 346);
  59. for (i = 0;i<50;i++) {
  60. src[i] = 0;
  61. }
  62. src[50] = 255;
  63. src[51] = 255;
  64. src[52] = 255;
  65. src[53] = 255;
  66. res = base64_encode(dst, 1000, src, 54, BASE64_ENCODE_MULTILINE);
  67. tt_int_op(res, OP_EQ, 74);
  68. res = base64_encode(dst, 1000, src+1, 53, BASE64_ENCODE_MULTILINE);
  69. tt_int_op(res, OP_EQ, 74);
  70. res = base64_encode(dst, 1000, src+2, 52, BASE64_ENCODE_MULTILINE);
  71. tt_int_op(res, OP_EQ, 74);
  72. res = base64_encode(dst, 1000, src+3, 51, BASE64_ENCODE_MULTILINE);
  73. tt_int_op(res, OP_EQ, 70);
  74. res = base64_encode(dst, 1000, src+4, 50, BASE64_ENCODE_MULTILINE);
  75. tt_int_op(res, OP_EQ, 70);
  76. res = base64_encode(dst, 1000, src+5, 49, BASE64_ENCODE_MULTILINE);
  77. tt_int_op(res, OP_EQ, 70);
  78. res = base64_encode(dst, 1000, src+6, 48, BASE64_ENCODE_MULTILINE);
  79. tt_int_op(res, OP_EQ, 65);
  80. res = base64_encode(dst, 1000, src+7, 47, BASE64_ENCODE_MULTILINE);
  81. tt_int_op(res, OP_EQ, 65);
  82. res = base64_encode(dst, 1000, src+8, 46, BASE64_ENCODE_MULTILINE);
  83. tt_int_op(res, OP_EQ, 65);
  84. done:
  85. tor_free(src);
  86. tor_free(dst);
  87. }
  88. static void
  89. test_util_format_base64_decode_nopad(void *ignored)
  90. {
  91. (void)ignored;
  92. int res;
  93. int i;
  94. char *src;
  95. uint8_t *dst, *real_dst;
  96. uint8_t expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
  97. char real_src[] = "ZXhhbXBsZQ";
  98. src = tor_malloc_zero(256);
  99. dst = tor_malloc_zero(1000);
  100. real_dst = tor_malloc_zero(10);
  101. for (i=0;i<256;i++) {
  102. src[i] = (char)i;
  103. }
  104. res = base64_decode_nopad(dst, 1, src, SIZE_T_CEILING);
  105. tt_int_op(res, OP_EQ, -1);
  106. res = base64_decode_nopad(dst, 1, src, 5);
  107. tt_int_op(res, OP_EQ, -1);
  108. res = base64_decode_nopad(real_dst, 10, real_src, 10);
  109. tt_int_op(res, OP_EQ, 7);
  110. tt_mem_op(real_dst, OP_EQ, expected, 7);
  111. done:
  112. tor_free(src);
  113. tor_free(dst);
  114. tor_free(real_dst);
  115. }
  116. static void
  117. test_util_format_base64_decode(void *ignored)
  118. {
  119. (void)ignored;
  120. int res;
  121. int i;
  122. char *src;
  123. char *dst, *real_dst;
  124. uint8_t expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
  125. char real_src[] = "ZXhhbXBsZQ==";
  126. src = tor_malloc_zero(256);
  127. dst = tor_malloc_zero(1000);
  128. real_dst = tor_malloc_zero(10);
  129. for (i=0;i<256;i++) {
  130. src[i] = (char)i;
  131. }
  132. res = base64_decode(dst, 1, src, SIZE_T_CEILING);
  133. tt_int_op(res, OP_EQ, -1);
  134. res = base64_decode(dst, SIZE_T_CEILING+1, src, 10);
  135. tt_int_op(res, OP_EQ, -1);
  136. res = base64_decode(real_dst, 10, real_src, 10);
  137. tt_int_op(res, OP_EQ, 7);
  138. tt_mem_op(real_dst, OP_EQ, expected, 7);
  139. done:
  140. tor_free(src);
  141. tor_free(dst);
  142. tor_free(real_dst);
  143. }
  144. static void
  145. test_util_format_base16_decode(void *ignored)
  146. {
  147. (void)ignored;
  148. int res;
  149. int i;
  150. char *src;
  151. char *dst, *real_dst;
  152. char expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
  153. char real_src[] = "6578616D706C65";
  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 = base16_decode(dst, 3, src, 3);
  161. tt_int_op(res, OP_EQ, -1);
  162. res = base16_decode(dst, 1, src, 10);
  163. tt_int_op(res, OP_EQ, -1);
  164. res = base16_decode(dst, SIZE_T_CEILING+2, src, 10);
  165. tt_int_op(res, OP_EQ, -1);
  166. res = base16_decode(real_dst, 10, real_src, 14);
  167. tt_int_op(res, OP_EQ, 0);
  168. tt_mem_op(real_dst, OP_EQ, expected, 7);
  169. done:
  170. tor_free(src);
  171. tor_free(dst);
  172. tor_free(real_dst);
  173. }
  174. struct testcase_t util_format_tests[] = {
  175. { "base64_encode", test_util_format_base64_encode, 0, NULL, NULL },
  176. { "base64_decode_nopad", test_util_format_base64_decode_nopad,
  177. 0, NULL, NULL },
  178. { "base64_decode", test_util_format_base64_decode, 0, NULL, NULL },
  179. { "base16_decode", test_util_format_base16_decode, 0, NULL, NULL },
  180. END_OF_TESTCASES
  181. };