test_strings.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief Dynamic string tests
  21. *
  22. * \author Mark Michelson <mmichelson@digium.com>
  23. *
  24. * This module will run some dyanmic string tests.
  25. *
  26. * \ingroup tests
  27. */
  28. /*** MODULEINFO
  29. <depend>TEST_FRAMEWORK</depend>
  30. <support_level>extended</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include "asterisk/test.h"
  35. #include "asterisk/utils.h"
  36. #include "asterisk/strings.h"
  37. #include "asterisk/module.h"
  38. AST_TEST_DEFINE(str_test)
  39. {
  40. struct ast_str *stack_str;
  41. struct ast_str *heap_str;
  42. const char short_string1[] = "apple";
  43. const char short_string2[] = "banana";
  44. char short_string_cat[30];
  45. const char long_string1[] = "applebananapeachmangocherrypeargrapeplumlimetangerinepomegranategravel";
  46. const char long_string2[] = "passionuglinectarinepineapplekiwilemonpaintthinner";
  47. char long_string_cat[200];
  48. char string_limit_cat[11];
  49. const int string_limit = 5;
  50. int current_size;
  51. enum ast_test_result_state res = AST_TEST_PASS;
  52. switch (cmd) {
  53. case TEST_INIT:
  54. info->name = "str_test";
  55. info->category = "/main/strings/";
  56. info->summary = "Test dynamic string operations";
  57. info->description = "Test setting and appending stack and heap-allocated strings";
  58. return AST_TEST_NOT_RUN;
  59. case TEST_EXECUTE:
  60. break;
  61. }
  62. snprintf(short_string_cat, sizeof(short_string_cat), "%s%s", short_string1, short_string2);
  63. snprintf(long_string_cat, sizeof(long_string_cat), "%s%s", long_string1, long_string2);
  64. snprintf(string_limit_cat, string_limit, "%s", long_string1);
  65. strncat(string_limit_cat, long_string2, string_limit);
  66. if (!(stack_str = ast_str_alloca(15))) {
  67. ast_test_status_update(test, "Failed to allocate an ast_str on the stack\n");
  68. return AST_TEST_FAIL;
  69. }
  70. if (!(heap_str = ast_str_create(15))) {
  71. ast_test_status_update(test, "Failed to allocate an ast_str on the heap\n");
  72. }
  73. /* Stack string tests:
  74. * Part 1: Basic tests
  75. * a. set a small string
  76. * b. append a small string
  77. * c. clear a string
  78. * Part 2: Advanced tests
  79. * a. Set a string that is larger than our allocation
  80. * b. Append a string that is larger than our allocation
  81. */
  82. /* Part 1a */
  83. if (ast_str_set(&stack_str, 0, "%s", short_string1) < 0) {
  84. ast_test_status_update(test, "Error setting stack string\n");
  85. res = AST_TEST_FAIL;
  86. goto cleanup;
  87. }
  88. if (strcmp(ast_str_buffer(stack_str), short_string1)) {
  89. ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s' but"
  90. "instead got %s\n", short_string1, ast_str_buffer(stack_str));
  91. res = AST_TEST_FAIL;
  92. goto cleanup;
  93. }
  94. /* Part 1b */
  95. if (ast_str_append(&stack_str, 0, "%s", short_string2) < 0) {
  96. ast_test_status_update(test, "Error appending to stack string\n");
  97. res = AST_TEST_FAIL;
  98. goto cleanup;
  99. }
  100. if (strcmp(ast_str_buffer(stack_str), short_string_cat)) {
  101. ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s'"
  102. "but instead got %s\n", short_string_cat, ast_str_buffer(stack_str));
  103. res = AST_TEST_FAIL;
  104. goto cleanup;
  105. }
  106. /* Part 1c */
  107. ast_str_reset(stack_str);
  108. if (ast_str_strlen(stack_str) != 0) {
  109. ast_test_status_update(test, "ast_str_reset resulted in non-zero length for stack_str\n");
  110. res = AST_TEST_FAIL;
  111. goto cleanup;
  112. }
  113. /* Part 2a */
  114. if (ast_str_set(&stack_str, -1, "%s", long_string1) < 0) {
  115. ast_test_status_update(test, "Error setting stack string with long input\n");
  116. res = AST_TEST_FAIL;
  117. goto cleanup;
  118. }
  119. if (strncmp(ast_str_buffer(stack_str), long_string1, ast_str_strlen(stack_str))) {
  120. ast_test_status_update(test, "Stack string not set to what is expected.\n");
  121. res = AST_TEST_FAIL;
  122. goto cleanup;
  123. }
  124. /* Part 2b */
  125. if (ast_str_append(&stack_str, -1, "%s", long_string2) < 0) {
  126. ast_test_status_update(test, "Error appending long string to full stack string buffer\n");
  127. res = AST_TEST_FAIL;
  128. goto cleanup;
  129. }
  130. if (strncmp(ast_str_buffer(stack_str), long_string_cat, ast_str_strlen(stack_str))) {
  131. ast_test_status_update(test, "Stack string not set to what is expected.\n");
  132. res = AST_TEST_FAIL;
  133. goto cleanup;
  134. }
  135. /* Heap string tests
  136. *
  137. * All stack string tests from part 1.
  138. * All stack string tests 2a and 2b.
  139. * Tests 2a and 2b from stack string tests, passing 0 as max_len
  140. * instead of -1. This allows for the buffer to grow.
  141. */
  142. /* Part 1a */
  143. if (ast_str_set(&heap_str, 0, "%s", short_string1) < 0) {
  144. ast_test_status_update(test, "Error setting heap string\n");
  145. res = AST_TEST_FAIL;
  146. goto cleanup;
  147. }
  148. if (strcmp(ast_str_buffer(heap_str), short_string1)) {
  149. ast_test_status_update(test, "ast_str_set failed for heap string. Expected '%s' but"
  150. "instead got %s\n", short_string1, ast_str_buffer(heap_str));
  151. res = AST_TEST_FAIL;
  152. goto cleanup;
  153. }
  154. /* Part 1b */
  155. if (ast_str_append(&heap_str, 0, "%s", short_string2) < 0) {
  156. ast_test_status_update(test, "Error appending to heap string\n");
  157. res = AST_TEST_FAIL;
  158. goto cleanup;
  159. }
  160. if (strcmp(ast_str_buffer(heap_str), short_string_cat)) {
  161. ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s'"
  162. "but instead got %s\n", short_string_cat, ast_str_buffer(stack_str));
  163. res = AST_TEST_FAIL;
  164. goto cleanup;
  165. }
  166. /* Part 1c */
  167. ast_str_reset(heap_str);
  168. if (ast_str_strlen(heap_str) != 0) {
  169. ast_test_status_update(test, "ast_str_reset resulted in non-zero length for stack_str\n");
  170. res = AST_TEST_FAIL;
  171. goto cleanup;
  172. }
  173. /* Part 2a with -1 arg */
  174. current_size = ast_str_size(heap_str);
  175. if (ast_str_set(&heap_str, -1, "%s", long_string1) < 0) {
  176. ast_test_status_update(test, "Error setting heap string with long input\n");
  177. res = AST_TEST_FAIL;
  178. goto cleanup;
  179. }
  180. if (current_size != ast_str_size(heap_str)) {
  181. ast_test_status_update(test, "Heap string changed size during ast_str_set when it was"
  182. "instructed not to. Was %d and now is %d\n", current_size, (int) ast_str_size(heap_str));
  183. res = AST_TEST_FAIL;
  184. goto cleanup;
  185. }
  186. if (strncmp(ast_str_buffer(heap_str), long_string1, ast_str_strlen(heap_str))) {
  187. ast_test_status_update(test, "Heap string not set to what is expected.\n");
  188. res = AST_TEST_FAIL;
  189. goto cleanup;
  190. }
  191. /* Part 2b with -1 arg */
  192. current_size = ast_str_size(heap_str);
  193. if (ast_str_append(&heap_str, -1, "%s", long_string2) < 0) {
  194. ast_test_status_update(test, "Error appending long string to full heap string buffer\n");
  195. res = AST_TEST_FAIL;
  196. goto cleanup;
  197. }
  198. if (current_size != ast_str_size(heap_str)) {
  199. ast_test_status_update(test, "Heap string changed size during ast_str_append when it was"
  200. "instructed not to. Was %d and now is %d\n", current_size, (int) ast_str_size(heap_str));
  201. res = AST_TEST_FAIL;
  202. goto cleanup;
  203. }
  204. if (strncmp(ast_str_buffer(heap_str), long_string_cat, ast_str_strlen(heap_str))) {
  205. ast_test_status_update(test, "Heap string not set to what is expected.\n");
  206. res = AST_TEST_FAIL;
  207. goto cleanup;
  208. }
  209. /* reset string before continuing */
  210. ast_str_reset(heap_str);
  211. /* Part 2a with 0 arg */
  212. if (ast_str_set(&heap_str, 0, "%s", long_string1) < 0) {
  213. ast_test_status_update(test, "Error setting heap string with long input\n");
  214. res = AST_TEST_FAIL;
  215. goto cleanup;
  216. }
  217. if (strcmp(ast_str_buffer(heap_str), long_string1)) {
  218. ast_test_status_update(test, "Heap string does not contain what was expected. Expected %s"
  219. "but have %s instead\n", long_string1, ast_str_buffer(heap_str));
  220. res = AST_TEST_FAIL;
  221. goto cleanup;
  222. }
  223. /* Part 2b with 0 arg */
  224. if (ast_str_append(&heap_str, 0, "%s", long_string2) < 0) {
  225. ast_test_status_update(test, "Error setting heap string with long input\n");
  226. res = AST_TEST_FAIL;
  227. goto cleanup;
  228. }
  229. if (strcmp(ast_str_buffer(heap_str), long_string_cat)) {
  230. ast_test_status_update(test, "Heap string does not contain what was expected. Expected %s"
  231. "but have %s instead\n", long_string_cat, ast_str_buffer(heap_str));
  232. res = AST_TEST_FAIL;
  233. goto cleanup;
  234. }
  235. cleanup:
  236. ast_free(heap_str);
  237. return res;
  238. }
  239. static int unload_module(void)
  240. {
  241. AST_TEST_UNREGISTER(str_test);
  242. return 0;
  243. }
  244. static int load_module(void)
  245. {
  246. AST_TEST_REGISTER(str_test);
  247. return AST_MODULE_LOAD_SUCCESS;
  248. }
  249. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dynamic string test module");