test_util_format.c 5.5 KB

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