test_util_format.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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;
  89. src = tor_malloc_zero(256);
  90. dst = tor_malloc_zero(1000);
  91. for (i=0;i<256;i++) {
  92. src[i] = (char)i;
  93. }
  94. res = base64_decode_nopad(dst, 1, src, SIZE_T_CEILING);
  95. tt_int_op(res, OP_EQ, -1);
  96. res = base64_decode_nopad(dst, 1, src, 5);
  97. tt_int_op(res, OP_EQ, -1);
  98. const char *s = "SGVsbG8gd29ybGQ";
  99. res = base64_decode_nopad(dst, 1000, s, strlen(s));
  100. tt_int_op(res, OP_EQ, 11);
  101. tt_mem_op(dst, OP_EQ, "Hello world", 11);
  102. s = "T3BhIG11bmRv";
  103. res = base64_decode_nopad(dst, 9, s, strlen(s));
  104. tt_int_op(res, OP_EQ, 9);
  105. tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
  106. done:
  107. tor_free(src);
  108. tor_free(dst);
  109. }
  110. static void
  111. test_util_format_base64_decode(void *ignored)
  112. {
  113. (void)ignored;
  114. int res;
  115. int i;
  116. char *src;
  117. char *dst;
  118. src = tor_malloc_zero(256);
  119. dst = tor_malloc_zero(1000);
  120. for (i=0;i<256;i++) {
  121. src[i] = (char)i;
  122. }
  123. res = base64_decode(dst, 1, src, SIZE_T_CEILING);
  124. tt_int_op(res, OP_EQ, -1);
  125. res = base64_decode(dst, SIZE_T_CEILING+1, src, 10);
  126. tt_int_op(res, OP_EQ, -1);
  127. const char *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. memset(dst, 0, 1000);
  132. res = base64_decode(dst, 100, s, strlen(s));
  133. tt_int_op(res, OP_EQ, 9);
  134. tt_mem_op(dst, OP_EQ, "Opa mundo", 9);
  135. s = "SGVsbG8gd29ybGQ=";
  136. res = base64_decode(dst, 100, s, strlen(s));
  137. tt_int_op(res, OP_EQ, 11);
  138. tt_mem_op(dst, OP_EQ, "Hello world", 11);
  139. done:
  140. tor_free(src);
  141. tor_free(dst);
  142. }
  143. static void
  144. test_util_format_base16_decode(void *ignored)
  145. {
  146. (void)ignored;
  147. int res;
  148. int i;
  149. char *src;
  150. char *dst;
  151. src = tor_malloc_zero(256);
  152. dst = tor_malloc_zero(1000);
  153. for (i=0;i<256;i++) {
  154. src[i] = (char)i;
  155. }
  156. res = base16_decode(dst, 3, src, 3);
  157. tt_int_op(res, OP_EQ, -1);
  158. res = base16_decode(dst, 1, src, 10);
  159. tt_int_op(res, OP_EQ, -1);
  160. res = base16_decode(dst, SIZE_T_CEILING+2, src, 10);
  161. tt_int_op(res, OP_EQ, -1);
  162. res = base16_decode(dst, 1000, "", 0);
  163. tt_int_op(res, OP_EQ, 0);
  164. res = base16_decode(dst, 1000, "aabc", 4);
  165. tt_int_op(res, OP_EQ, 0);
  166. tt_mem_op(dst, OP_EQ, "\xaa\xbc", 2);
  167. res = base16_decode(dst, 1000, "aabcd", 6);
  168. tt_int_op(res, OP_EQ, -1);
  169. res = base16_decode(dst, 1000, "axxx", 4);
  170. tt_int_op(res, OP_EQ, -1);
  171. done:
  172. tor_free(src);
  173. tor_free(dst);
  174. }
  175. struct testcase_t util_format_tests[] = {
  176. { "base64_encode", test_util_format_base64_encode, 0, NULL, NULL },
  177. { "base64_decode_nopad", test_util_format_base64_decode_nopad, 0,
  178. NULL, NULL },
  179. { "base64_decode", test_util_format_base64_decode, 0, NULL, NULL },
  180. { "base16_decode", test_util_format_base16_decode, 0, NULL, NULL },
  181. END_OF_TESTCASES
  182. };