test_buffer.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * OpenVPN -- An application to securely tunnel IP networks
  3. * over a single UDP port, with support for SSL/TLS-based
  4. * session authentication and key exchange,
  5. * packet encryption, packet authentication, and
  6. * packet compression.
  7. *
  8. * Copyright (C) 2016-2018 Fox Crypto B.V. <openvpn@fox-it.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #elif defined(_MSC_VER)
  26. #include "config-msvc.h"
  27. #endif
  28. #include "syshead.h"
  29. #include <setjmp.h>
  30. #include <cmocka.h>
  31. #include "buffer.h"
  32. static void
  33. test_buffer_strprefix(void **state)
  34. {
  35. assert_true(strprefix("123456", "123456"));
  36. assert_true(strprefix("123456", "123"));
  37. assert_true(strprefix("123456", ""));
  38. assert_false(strprefix("123456", "456"));
  39. assert_false(strprefix("12", "123"));
  40. }
  41. #define testsep ","
  42. #define testnosep ""
  43. #define teststr1 "one"
  44. #define teststr2 "two"
  45. #define teststr3 "three"
  46. #define teststr4 "four"
  47. #define assert_buf_equals_str(buf, str) \
  48. assert_int_equal(BLEN(buf), strlen(str)); \
  49. assert_memory_equal(BPTR(buf), str, BLEN(buf));
  50. struct test_buffer_list_aggregate_ctx {
  51. struct buffer_list *empty;
  52. struct buffer_list *one_two_three;
  53. struct buffer_list *zero_length_strings;
  54. struct buffer_list *empty_buffers;
  55. };
  56. static int test_buffer_list_setup(void **state)
  57. {
  58. struct test_buffer_list_aggregate_ctx *ctx = calloc(1, sizeof(*ctx));
  59. ctx->empty = buffer_list_new(0);
  60. ctx->one_two_three = buffer_list_new(3);
  61. buffer_list_push(ctx->one_two_three, teststr1);
  62. buffer_list_push(ctx->one_two_three, teststr2);
  63. buffer_list_push(ctx->one_two_three, teststr3);
  64. ctx->zero_length_strings = buffer_list_new(2);
  65. buffer_list_push(ctx->zero_length_strings, "");
  66. buffer_list_push(ctx->zero_length_strings, "");
  67. ctx->empty_buffers = buffer_list_new(2);
  68. uint8_t data = 0;
  69. buffer_list_push_data(ctx->empty_buffers, &data, 0);
  70. buffer_list_push_data(ctx->empty_buffers, &data, 0);
  71. *state = ctx;
  72. return 0;
  73. }
  74. static int test_buffer_list_teardown(void **state)
  75. {
  76. struct test_buffer_list_aggregate_ctx *ctx = *state;
  77. buffer_list_free(ctx->empty);
  78. buffer_list_free(ctx->one_two_three);
  79. buffer_list_free(ctx->zero_length_strings);
  80. buffer_list_free(ctx->empty_buffers);
  81. free(ctx);
  82. return 0;
  83. }
  84. static void
  85. test_buffer_list_full(void **state)
  86. {
  87. struct test_buffer_list_aggregate_ctx *ctx = *state;
  88. /* list full */
  89. assert_int_equal(ctx->one_two_three->size, 3);
  90. buffer_list_push(ctx->one_two_three, teststr4);
  91. assert_int_equal(ctx->one_two_three->size, 3);
  92. }
  93. static void
  94. test_buffer_list_aggregate_separator_empty(void **state)
  95. {
  96. struct test_buffer_list_aggregate_ctx *ctx = *state;
  97. /* aggregating an empty buffer list results in an empty buffer list */
  98. buffer_list_aggregate_separator(ctx->empty, 3, testsep);
  99. assert_null(ctx->empty->head);
  100. }
  101. static void
  102. test_buffer_list_aggregate_separator_noop(void **state)
  103. {
  104. struct test_buffer_list_aggregate_ctx *ctx = *state;
  105. /* With a max length of 2, no aggregation should take place */
  106. buffer_list_aggregate_separator(ctx->one_two_three, 2, testsep);
  107. assert_int_equal(ctx->one_two_three->size, 3);
  108. struct buffer *buf = buffer_list_peek(ctx->one_two_three);
  109. assert_buf_equals_str(buf, teststr1);
  110. }
  111. static void
  112. test_buffer_list_aggregate_separator_two(void **state)
  113. {
  114. struct test_buffer_list_aggregate_ctx *ctx = *state;
  115. const char *expected = teststr1 testsep teststr2 testsep;
  116. /* Aggregate the first two elements
  117. * (add 1 to max_len to test if "three" is not sneaked in too)
  118. */
  119. buffer_list_aggregate_separator(ctx->one_two_three, strlen(expected) + 1,
  120. testsep);
  121. assert_int_equal(ctx->one_two_three->size, 2);
  122. struct buffer *buf = buffer_list_peek(ctx->one_two_three);
  123. assert_buf_equals_str(buf, expected);
  124. }
  125. static void
  126. test_buffer_list_aggregate_separator_all(void **state)
  127. {
  128. struct test_buffer_list_aggregate_ctx *ctx = *state;
  129. /* Aggregate all */
  130. buffer_list_aggregate_separator(ctx->one_two_three, 1<<16, testsep);
  131. assert_int_equal(ctx->one_two_three->size, 1);
  132. struct buffer *buf = buffer_list_peek(ctx->one_two_three);
  133. assert_buf_equals_str(buf,
  134. teststr1 testsep teststr2 testsep teststr3 testsep);
  135. }
  136. static void
  137. test_buffer_list_aggregate_separator_nosep(void **state)
  138. {
  139. struct test_buffer_list_aggregate_ctx *ctx = *state;
  140. /* Aggregate all */
  141. buffer_list_aggregate_separator(ctx->one_two_three, 1<<16, testnosep);
  142. assert_int_equal(ctx->one_two_three->size, 1);
  143. struct buffer *buf = buffer_list_peek(ctx->one_two_three);
  144. assert_buf_equals_str(buf, teststr1 teststr2 teststr3);
  145. }
  146. static void
  147. test_buffer_list_aggregate_separator_zerolen(void **state)
  148. {
  149. struct test_buffer_list_aggregate_ctx *ctx = *state;
  150. struct buffer_list *bl_zerolen = ctx->zero_length_strings;
  151. /* Aggregate all */
  152. buffer_list_aggregate_separator(bl_zerolen, 1<<16, testnosep);
  153. assert_int_equal(bl_zerolen->size, 1);
  154. struct buffer *buf = buffer_list_peek(bl_zerolen);
  155. assert_buf_equals_str(buf, "");
  156. }
  157. static void
  158. test_buffer_list_aggregate_separator_emptybuffers(void **state)
  159. {
  160. struct test_buffer_list_aggregate_ctx *ctx = *state;
  161. struct buffer_list *bl_emptybuffers = ctx->empty_buffers;
  162. /* Aggregate all */
  163. buffer_list_aggregate_separator(bl_emptybuffers, 1<<16, testnosep);
  164. assert_int_equal(bl_emptybuffers->size, 1);
  165. struct buffer *buf = buffer_list_peek(bl_emptybuffers);
  166. assert_int_equal(BLEN(buf), 0);
  167. }
  168. int
  169. main(void)
  170. {
  171. const struct CMUnitTest tests[] = {
  172. cmocka_unit_test(test_buffer_strprefix),
  173. cmocka_unit_test_setup_teardown(test_buffer_list_full,
  174. test_buffer_list_setup,
  175. test_buffer_list_teardown),
  176. cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_empty,
  177. test_buffer_list_setup,
  178. test_buffer_list_teardown),
  179. cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_noop,
  180. test_buffer_list_setup,
  181. test_buffer_list_teardown),
  182. cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_two,
  183. test_buffer_list_setup,
  184. test_buffer_list_teardown),
  185. cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_all,
  186. test_buffer_list_setup,
  187. test_buffer_list_teardown),
  188. cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_nosep,
  189. test_buffer_list_setup,
  190. test_buffer_list_teardown),
  191. cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_zerolen,
  192. test_buffer_list_setup,
  193. test_buffer_list_teardown),
  194. cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_emptybuffers,
  195. test_buffer_list_setup,
  196. test_buffer_list_teardown),
  197. };
  198. return cmocka_run_group_tests_name("buffer", tests, NULL, NULL);
  199. }