test_util_format.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /* XXXX Needs to test the success case */
  95. res = base64_decode_nopad(dst, 1, src, SIZE_T_CEILING);
  96. tt_int_op(res, OP_EQ, -1);
  97. res = base64_decode_nopad(dst, 1, src, 5);
  98. tt_int_op(res, OP_EQ, -1);
  99. done:
  100. tor_free(src);
  101. tor_free(dst);
  102. }
  103. static void
  104. test_util_format_base64_decode(void *ignored)
  105. {
  106. (void)ignored;
  107. int res;
  108. int i;
  109. char *src;
  110. char *dst;
  111. src = tor_malloc_zero(256);
  112. dst = tor_malloc_zero(1000);
  113. for (i=0;i<256;i++) {
  114. src[i] = (char)i;
  115. }
  116. /* XXXX Needs to test the success case */
  117. res = base64_decode(dst, 1, src, SIZE_T_CEILING);
  118. tt_int_op(res, OP_EQ, -1);
  119. res = base64_decode(dst, SIZE_T_CEILING+1, src, 10);
  120. tt_int_op(res, OP_EQ, -1);
  121. done:
  122. tor_free(src);
  123. tor_free(dst);
  124. }
  125. static void
  126. test_util_format_base16_decode(void *ignored)
  127. {
  128. (void)ignored;
  129. int res;
  130. int i;
  131. char *src;
  132. char *dst;
  133. src = tor_malloc_zero(256);
  134. dst = tor_malloc_zero(1000);
  135. for (i=0;i<256;i++) {
  136. src[i] = (char)i;
  137. }
  138. /* XXXX Needs to test the success case */
  139. res = base16_decode(dst, 3, src, 3);
  140. tt_int_op(res, OP_EQ, -1);
  141. res = base16_decode(dst, 1, src, 10);
  142. tt_int_op(res, OP_EQ, -1);
  143. res = base16_decode(dst, SIZE_T_CEILING+2, src, 10);
  144. tt_int_op(res, OP_EQ, -1);
  145. done:
  146. tor_free(src);
  147. tor_free(dst);
  148. }
  149. struct testcase_t util_format_tests[] = {
  150. { "base64_encode", test_util_format_base64_encode, 0, NULL, NULL },
  151. { "base64_decode_nopad", test_util_format_base64_decode_nopad, 0,
  152. NULL, NULL },
  153. { "base64_decode", test_util_format_base64_decode, 0, NULL, NULL },
  154. { "base16_decode", test_util_format_base16_decode, 0, NULL, NULL },
  155. END_OF_TESTCASES
  156. };