test_stringfields.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Mark Michelson
  5. *
  6. * Mark Michelson <mmmichelson@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. /*! \file
  19. *
  20. * \brief String fields test
  21. *
  22. * \author\verbatim Mark Michelson <mmichelson@digium.com> \endverbatim
  23. *
  24. * Test module for string fields API
  25. * \ingroup tests
  26. * \todo need to test ast_calloc_with_stringfields
  27. */
  28. /*** MODULEINFO
  29. <depend>TEST_FRAMEWORK</depend>
  30. <support_level>extended</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/stringfields.h"
  35. #include "asterisk/cli.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/test.h"
  38. AST_TEST_DEFINE(string_field_test)
  39. {
  40. const char *address_holder;
  41. struct ast_string_field_pool *field_pool1;
  42. struct ast_string_field_pool *field_pool2;
  43. struct ast_string_field_pool *field_pool3;
  44. static const char LONG_STRING[] = "A professional panoramic photograph of the majestic elephant bathing itself and its young by the shores of the raging Mississippi River";
  45. struct {
  46. AST_DECLARE_STRING_FIELDS (
  47. AST_STRING_FIELD(string1);
  48. AST_STRING_FIELD(string2);
  49. );
  50. } test_struct;
  51. struct {
  52. AST_DECLARE_STRING_FIELDS (
  53. AST_STRING_FIELD(string1);
  54. AST_STRING_FIELD(string2);
  55. AST_STRING_FIELD(string3);
  56. );
  57. } test_struct2;
  58. switch (cmd) {
  59. case TEST_INIT:
  60. info->name = "string_field_test";
  61. info->category = "/main/utils/";
  62. info->summary = "Test stringfield operations";
  63. info->description =
  64. "This tests the stringfield API";
  65. return AST_TEST_NOT_RUN;
  66. case TEST_EXECUTE:
  67. break;
  68. }
  69. ast_test_status_update(test, "First things first. Let's see if we can actually allocate string fields\n");
  70. if (ast_string_field_init(&test_struct, 32)) {
  71. ast_test_status_update(test, "Failure to initialize string fields. They are totally messed up\n");
  72. return AST_TEST_FAIL;
  73. } else {
  74. ast_test_status_update(test, "All right! Successfully allocated! Now let's get down to business\n");
  75. }
  76. ast_test_status_update(test,"We're going to set some string fields and perform some checks\n");
  77. ast_string_field_set(&test_struct, string1, "elephant");
  78. ast_string_field_set(&test_struct, string2, "hippopotamus");
  79. ast_test_status_update(test, "First we're going to make sure that the strings are actually set to what we expect\n");
  80. if (strcmp(test_struct.string1, "elephant")) {
  81. ast_test_status_update(test, "We were expecting test_struct.string1 to have 'elephant' but it has %s\n", test_struct.string1);
  82. goto error;
  83. } else {
  84. ast_test_status_update(test, "test_struct.string1 appears to be all clear. It has '%s' and that's what we expect\n", test_struct.string1);
  85. }
  86. if (strcmp(test_struct.string2, "hippopotamus")) {
  87. ast_test_status_update(test, "We were expecting test_struct.string2 to have 'hippopotamus' but it has %s\n", test_struct.string2);
  88. goto error;
  89. } else {
  90. ast_test_status_update(test, "test_struct.string2 appears to be all clear. It has '%s' and that's what we expect\n", test_struct.string2);
  91. }
  92. ast_test_status_update(test, "Now let's make sure that our recorded capacities for these strings is what we expect\n");
  93. if (AST_STRING_FIELD_ALLOCATION(test_struct.string1) != strlen("elephant") + 1) {
  94. ast_test_status_update(test, "string1 has allocation area of %hu but we expect %lu\n",
  95. AST_STRING_FIELD_ALLOCATION(test_struct.string1), (unsigned long) strlen("elephant") + 1);
  96. goto error;
  97. } else {
  98. ast_test_status_update(test, "string1 has the allocation area we expect: %hu\n", AST_STRING_FIELD_ALLOCATION(test_struct.string1));
  99. }
  100. if (AST_STRING_FIELD_ALLOCATION(test_struct.string2) != strlen("hippopotamus") + 1) {
  101. ast_test_status_update(test, "string2 has allocation area of %hu but we expect %lu\n",
  102. AST_STRING_FIELD_ALLOCATION(test_struct.string2), (unsigned long) strlen("hippopotamus") + 1);
  103. goto error;
  104. } else {
  105. ast_test_status_update(test, "string2 has the allocation area we expect: %hu\n", AST_STRING_FIELD_ALLOCATION(test_struct.string2));
  106. }
  107. ast_test_status_update(test, "Now we're going to shrink string1 and see if it's in the same place in memory\n");
  108. address_holder = test_struct.string1;
  109. ast_string_field_set(&test_struct, string1, "rhino");
  110. if (strcmp(test_struct.string1, "rhino")) {
  111. ast_test_status_update(test, "string1 has the wrong value in it. We want 'rhino' but it has '%s'\n", test_struct.string1);
  112. goto error;
  113. } else {
  114. ast_test_status_update(test, "string1 successfully was changed to '%s'\n", test_struct.string1);
  115. }
  116. if (address_holder != test_struct.string1) {
  117. ast_test_status_update(test, "We shrunk string1, but it moved?!\n");
  118. goto error;
  119. } else {
  120. ast_test_status_update(test, "Shrinking string1 allowed it to stay in the same place in memory\n");
  121. }
  122. if (AST_STRING_FIELD_ALLOCATION(test_struct.string1) != strlen("elephant") + 1) {
  123. ast_test_status_update(test, "The allocation amount changed when we shrunk the string...\n");
  124. goto error;
  125. } else {
  126. ast_test_status_update(test, "Shrinking string1 did not change its allocation area (This is a good thing)\n");
  127. }
  128. ast_test_status_update(test, "Next, let's increase it a little but not all the way to its original size\n");
  129. address_holder = test_struct.string1;
  130. ast_string_field_set(&test_struct, string1, "mammoth");
  131. if (strcmp(test_struct.string1, "mammoth")) {
  132. ast_test_status_update(test, "string1 has the wrong value in it. We want 'mammoth' but it has '%s'\n", test_struct.string1);
  133. goto error;
  134. } else {
  135. ast_test_status_update(test, "string1 successfully was changed to '%s'\n", test_struct.string1);
  136. }
  137. if (address_holder != test_struct.string1) {
  138. ast_test_status_update(test, "We expanded string1, but it moved?!\n");
  139. goto error;
  140. } else {
  141. ast_test_status_update(test, "Expanding string1 allowed it to stay in the same place in memory\n");
  142. }
  143. if (AST_STRING_FIELD_ALLOCATION(test_struct.string1) != strlen("elephant") + 1) {
  144. ast_test_status_update(test, "The allocation amount changed when we expanded the string...\n");
  145. goto error;
  146. } else {
  147. ast_test_status_update(test, "Expanding string1 did not change its allocation area (This is a good thing)\n");
  148. }
  149. ast_test_status_update(test, "Cool, now let's bring it back to its original size and see what happens\n");
  150. ast_string_field_set(&test_struct, string1, "elephant");
  151. if (strcmp(test_struct.string1, "elephant")) {
  152. ast_test_status_update(test, "string1 has the wrong value in it. We want 'elephant' but it has '%s'\n", test_struct.string1);
  153. goto error;
  154. } else {
  155. ast_test_status_update(test, "string1 successfully changed to '%s'\n", test_struct.string1);
  156. }
  157. if (address_holder != test_struct.string1) {
  158. ast_test_status_update(test, "We restored string1 to its original size, but it moved?!\n");
  159. goto error;
  160. } else {
  161. ast_test_status_update(test, "Restoring string1 did not cause it to move (This is a good thing)\n");
  162. }
  163. if (AST_STRING_FIELD_ALLOCATION(test_struct.string1) != strlen("elephant") + 1) {
  164. ast_test_status_update(test, "The allocation amount changed when we re-expanded the string...\n");
  165. goto error;
  166. } else {
  167. ast_test_status_update(test, "The allocation amount for string1 is still holding steady\n");
  168. }
  169. ast_test_status_update(test, "All right, now we're going to expand string 2. It should stay in place since it was the last string allocated in this pool\n");
  170. address_holder = test_struct.string2;
  171. ast_string_field_set(&test_struct, string2, "hippopotamus face");
  172. if (strcmp(test_struct.string2, "hippopotamus face")) {
  173. ast_test_status_update(test, "string2 has the wrong value. We want 'hippopotamus face' but it has '%s'\n", test_struct.string2);
  174. goto error;
  175. } else {
  176. ast_test_status_update(test, "string2 successfully changed to '%s'\n", test_struct.string2);
  177. }
  178. if (AST_STRING_FIELD_ALLOCATION(test_struct.string2) != strlen("hippopotamus face") + 1) {
  179. ast_test_status_update(test, "The allocation amount is incorrect for string2. We expect %lu but it has %hu\n",
  180. (unsigned long) strlen("hippopotamus face"), AST_STRING_FIELD_ALLOCATION(test_struct.string2) + 1);
  181. goto error;
  182. } else {
  183. ast_test_status_update(test, "The allocation amount successfully increased for string2 when it grew\n");
  184. }
  185. if (test_struct.string2 != address_holder) {
  186. ast_test_status_update(test, "string2 has moved, but it should not have since it had room to grow\n");
  187. goto error;
  188. } else {
  189. ast_test_status_update(test, "string2 stayed in place when it grew. Good job!\n");
  190. }
  191. ast_test_status_update(test, "Now we're going to set string1 to a very long string so that a new string field pool must be allocated\n");
  192. address_holder = test_struct.string1;
  193. ast_string_field_set(&test_struct, string1, LONG_STRING);
  194. if (strcmp(test_struct.string1, LONG_STRING)) {
  195. ast_test_status_update(test, "We were expecting string1 to be '%s'\nbut it was actually '%s'\n", LONG_STRING, test_struct.string1);
  196. goto error;
  197. } else {
  198. ast_test_status_update(test, "string1 successfully changed to '%s'\n", test_struct.string1);
  199. }
  200. if (address_holder == test_struct.string1) {
  201. ast_test_status_update(test, "Uh oh, string1 didn't move when we set it to a long value\n");
  202. goto error;
  203. } else {
  204. ast_test_status_update(test, "Good. Setting string1 to a long value caused it to change addresses\n");
  205. }
  206. if (AST_STRING_FIELD_ALLOCATION(test_struct.string1) != strlen(LONG_STRING) + 1) {
  207. ast_test_status_update(test, "The string field allocation for string1 indicates a length of %hu instead of the expected %lu\n",
  208. AST_STRING_FIELD_ALLOCATION(test_struct.string1), (unsigned long) strlen(LONG_STRING) + 1);
  209. goto error;
  210. } else {
  211. ast_test_status_update(test, "The stored allocation size of string1 is what we expect\n");
  212. }
  213. ast_string_field_init(&test_struct2, 32);
  214. ast_test_status_update(test, "Now using a totally separate area of memory we're going to test a basic pool freeing scenario\n");
  215. ast_string_field_set(&test_struct2, string1, "first");
  216. ast_string_field_set(&test_struct2, string2, "second");
  217. ast_string_field_set(&test_struct2, string3, "third");
  218. /* This string is 208 characters long, which will surely exceed the initial pool size */
  219. ast_string_field_set(&test_struct2, string1, "Expanded first string to create new pool-----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
  220. /* Pool size at this point is 976, so 1000 chars should do it */
  221. ast_string_field_set(&test_struct2, string2, "Expanded second string to create new pool----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
  222. field_pool3 = test_struct2.__field_mgr_pool;
  223. field_pool2 = test_struct2.__field_mgr_pool->prev;
  224. field_pool1 = test_struct2.__field_mgr_pool->prev->prev;
  225. if(field_pool3->prev != field_pool2 || field_pool2->prev != field_pool1) {
  226. ast_test_status_update(test, "Pools are not linked properly!\n");
  227. goto error;
  228. } else {
  229. ast_test_status_update(test, "Three different pools are linked as expected.\n");
  230. }
  231. ast_string_field_set(&test_struct2, string1, NULL);
  232. if (test_struct2.string1 != __ast_string_field_empty || field_pool3->prev != field_pool1) {
  233. ast_test_status_update(test, "Things did not work out when removing the middle pool!\n");
  234. goto error;
  235. } else {
  236. ast_test_status_update(test, "After removing a pool the remaining two are linked as expected.\n");
  237. }
  238. ast_string_field_free_memory(&test_struct2);
  239. ast_string_field_free_memory(&test_struct);
  240. return AST_TEST_PASS;
  241. error:
  242. ast_string_field_free_memory(&test_struct);
  243. ast_string_field_free_memory(&test_struct2);
  244. return AST_TEST_FAIL;
  245. }
  246. static int unload_module(void)
  247. {
  248. AST_TEST_UNREGISTER(string_field_test);
  249. return 0;
  250. }
  251. static int load_module(void)
  252. {
  253. AST_TEST_REGISTER(string_field_test);
  254. return AST_MODULE_LOAD_SUCCESS;
  255. }
  256. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "String Fields Test");