test_util_format.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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;
  82. src = tor_malloc_zero(256);
  83. dst = tor_malloc_zero(1000);
  84. for(i=0;i<256;i++) {
  85. src[i] = (char)i;
  86. }
  87. res = base64_decode_nopad(dst, 1, src, SIZE_T_CEILING);
  88. tt_int_op(res, OP_EQ, -1);
  89. res = base64_decode_nopad(dst, 1, src, 5);
  90. tt_int_op(res, OP_EQ, -1);
  91. done:
  92. tor_free(src);
  93. tor_free(dst);
  94. }
  95. static void
  96. test_util_format_base64_decode(void *ignored)
  97. {
  98. (void)ignored;
  99. int res;
  100. int i;
  101. char *src;
  102. char *dst;
  103. src = tor_malloc_zero(256);
  104. dst = tor_malloc_zero(1000);
  105. for(i=0;i<256;i++) {
  106. src[i] = (char)i;
  107. }
  108. res = base64_decode(dst, 1, src, SIZE_T_CEILING);
  109. tt_int_op(res, OP_EQ, -1);
  110. res = base64_decode(dst, SIZE_T_CEILING+1, src, 10);
  111. tt_int_op(res, OP_EQ, -1);
  112. done:
  113. tor_free(src);
  114. tor_free(dst);
  115. }
  116. static void
  117. test_util_format_base16_decode(void *ignored)
  118. {
  119. (void)ignored;
  120. int res;
  121. int i;
  122. char *src;
  123. char *dst;
  124. src = tor_malloc_zero(256);
  125. dst = tor_malloc_zero(1000);
  126. for(i=0;i<256;i++) {
  127. src[i] = (char)i;
  128. }
  129. res = base16_decode(dst, 3, src, 3);
  130. tt_int_op(res, OP_EQ, -1);
  131. res = base16_decode(dst, 1, src, 10);
  132. tt_int_op(res, OP_EQ, -1);
  133. res = base16_decode(dst, SIZE_T_CEILING+2, src, 10);
  134. tt_int_op(res, OP_EQ, -1);
  135. done:
  136. tor_free(src);
  137. tor_free(dst);
  138. }
  139. struct testcase_t util_format_tests[] = {
  140. { "base64_encode", test_util_format_base64_encode, 0, NULL, NULL },
  141. { "base64_decode_nopad", test_util_format_base64_decode_nopad, 0, NULL, NULL },
  142. { "base64_decode", test_util_format_base64_decode, 0, NULL, NULL },
  143. { "base16_decode", test_util_format_base16_decode, 0, NULL, NULL },
  144. END_OF_TESTCASES
  145. };