test_db.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011, Digium, Inc.
  5. *
  6. * Terry Wilson <twilson@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 AstDB Unit Tests
  21. *
  22. * \author Terry Wilson <twilson@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "")
  31. #include "asterisk/test.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/astdb.h"
  34. #include "asterisk/logger.h"
  35. enum {
  36. FAMILY = 0,
  37. KEY = 1,
  38. VALUE = 2,
  39. };
  40. /* Longest value we can support is 256 for family/key/ so, with
  41. * family = astdbtest and two slashes we are left with 244 bytes */
  42. static const char long_val[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  43. AST_TEST_DEFINE(put_get_del)
  44. {
  45. int res = AST_TEST_PASS;
  46. const char *inputs[][3] = {
  47. {"family", "key", "value"},
  48. {"astdbtest", "a", "b"},
  49. {"astdbtest", "a", "a"},
  50. {"astdbtest", "b", "a"},
  51. {"astdbtest", "b", "b"},
  52. {"astdbtest", "b", "!@#$%^&*()|+-<>?"},
  53. {"astdbtest", long_val, "b"},
  54. {"astdbtest", "b", long_val},
  55. {"astdbtest", "!@#$%^&*()|+-<>?", "b"},
  56. };
  57. size_t x;
  58. char buf[sizeof(long_val)] = { 0, };
  59. switch (cmd) {
  60. case TEST_INIT:
  61. info->name = "put_get_del";
  62. info->category = "/main/astdb/";
  63. info->summary = "ast_db_(put|get|del) unit test";
  64. info->description =
  65. "Ensures that the ast_db put, get, and del functions work";
  66. return AST_TEST_NOT_RUN;
  67. case TEST_EXECUTE:
  68. break;
  69. }
  70. for (x = 0; x < ARRAY_LEN(inputs); x++) {
  71. if (ast_db_put(inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE])) {
  72. ast_test_status_update(test, "Failed to put %s : %s : %s\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE]);
  73. res = AST_TEST_FAIL;
  74. }
  75. if (ast_db_get(inputs[x][FAMILY], inputs[x][KEY], buf, sizeof(buf))) {
  76. ast_test_status_update(test, "Failed to get %s : %s : %s\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE]);
  77. res = AST_TEST_FAIL;
  78. } else if (strcmp(buf, inputs[x][VALUE])) {
  79. ast_test_status_update(test, "Failed to match key '%s/%s' value '%s' to '%s'\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE], buf);
  80. res = AST_TEST_FAIL;
  81. }
  82. if (ast_db_del(inputs[x][FAMILY], inputs[x][KEY])) {
  83. ast_test_status_update(test, "Failed to del %s : %s\n", inputs[x][FAMILY], inputs[x][KEY]);
  84. res = AST_TEST_FAIL;
  85. }
  86. }
  87. return res;
  88. }
  89. AST_TEST_DEFINE(gettree_deltree)
  90. {
  91. int res = AST_TEST_PASS;
  92. const char *inputs[][3] = {
  93. #define BASE "astdbtest"
  94. #define SUB1 "one"
  95. #define SUB2 "two"
  96. #define FAM1 BASE "/" SUB1
  97. #define FAM2 BASE "/" SUB2
  98. {FAM1, "one", "blah"},
  99. {FAM1, "two", "bling"},
  100. {FAM1, "three", "blast"},
  101. {FAM2, "one", "blah"},
  102. {FAM2, "two", "bling"},
  103. {FAM2, "three", "blast"},
  104. };
  105. size_t x;
  106. struct ast_db_entry *dbes, *cur;
  107. int num_deleted;
  108. switch (cmd) {
  109. case TEST_INIT:
  110. info->name = "gettree_deltree";
  111. info->category = "/main/astdb/";
  112. info->summary = "ast_db_(gettree|deltree) unit test";
  113. info->description =
  114. "Ensures that the ast_db gettree and deltree functions work";
  115. return AST_TEST_NOT_RUN;
  116. case TEST_EXECUTE:
  117. break;
  118. }
  119. for (x = 0; x < ARRAY_LEN(inputs); x++) {
  120. if (ast_db_put(inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE])) {
  121. ast_test_status_update(test, "Failed to put %s : %s : %s\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE]);
  122. res = AST_TEST_FAIL;
  123. }
  124. }
  125. if (!(dbes = ast_db_gettree(BASE, NULL))) {
  126. ast_test_status_update(test, "Failed to ast_db_gettree family %s\n", BASE);
  127. res = AST_TEST_FAIL;
  128. }
  129. for (cur = dbes, x = 0; cur; cur = cur->next, x++) {
  130. int found = 0;
  131. size_t z;
  132. for (z = 0; z < ARRAY_LEN(inputs); z++) {
  133. char buf[256];
  134. snprintf(buf, sizeof(buf), "/%s/%s", inputs[z][FAMILY], inputs[z][KEY]);
  135. if (!strcmp(buf, cur->key) && !strcmp(inputs[z][VALUE], cur->data)) {
  136. found = 1;
  137. }
  138. }
  139. if (!found) {
  140. ast_test_status_update(test, "inputs array has no entry for %s == %s\n", cur->key, cur->data);
  141. res = AST_TEST_FAIL;
  142. }
  143. }
  144. if (x != ARRAY_LEN(inputs)) {
  145. ast_test_status_update(test, "ast_db_gettree returned %zu entries when we expected %zu\n", x, ARRAY_LEN(inputs));
  146. res = AST_TEST_FAIL;
  147. }
  148. ast_db_freetree(dbes);
  149. if (!(dbes = ast_db_gettree(BASE, SUB1))) {
  150. ast_test_status_update(test, "Failed to ast_db_gettree for %s/%s\n", BASE, SUB1);
  151. res = AST_TEST_FAIL;
  152. }
  153. for (cur = dbes, x = 0; cur; cur = cur->next, x++) {
  154. int found = 0;
  155. size_t z;
  156. for (z = 0; z < ARRAY_LEN(inputs); z++) {
  157. char buf[256];
  158. snprintf(buf, sizeof(buf), "/%s/%s", inputs[z][FAMILY], inputs[z][KEY]);
  159. if (!strcmp(buf, cur->key) && !strcmp(inputs[z][VALUE], cur->data)) {
  160. found = 1;
  161. }
  162. }
  163. if (!found) {
  164. ast_test_status_update(test, "inputs array has no entry for %s == %s\n", cur->key, cur->data);
  165. res = AST_TEST_FAIL;
  166. }
  167. }
  168. if (x != (ARRAY_LEN(inputs) / 2)) {
  169. ast_test_status_update(test, "ast_db_gettree returned %zu entries when we expected %zu\n", x, ARRAY_LEN(inputs) / 2);
  170. res = AST_TEST_FAIL;
  171. }
  172. ast_db_freetree(dbes);
  173. if ((num_deleted = ast_db_deltree(BASE, SUB2)) != ARRAY_LEN(inputs) / 2) {
  174. ast_test_status_update(test, "Failed to deltree %s/%s, expected %zu deletions and got %d\n", BASE, SUB2, ARRAY_LEN(inputs) / 2, num_deleted);
  175. res = AST_TEST_FAIL;
  176. }
  177. if ((num_deleted = ast_db_deltree(BASE, NULL)) != ARRAY_LEN(inputs) / 2) {
  178. ast_test_status_update(test, "Failed to deltree %s, expected %zu deletions and got %d\n", BASE, ARRAY_LEN(inputs) / 2, num_deleted);
  179. res = AST_TEST_FAIL;
  180. }
  181. return res;
  182. }
  183. AST_TEST_DEFINE(perftest)
  184. {
  185. int res = AST_TEST_PASS;
  186. size_t x;
  187. char buf[10];
  188. switch (cmd) {
  189. case TEST_INIT:
  190. info->name = "perftest";
  191. info->category = "/main/astdb/";
  192. info->summary = "astdb performance unit test";
  193. info->description =
  194. "Measure astdb performance";
  195. return AST_TEST_NOT_RUN;
  196. case TEST_EXECUTE:
  197. break;
  198. }
  199. for (x = 0; x < 100000; x++) {
  200. sprintf(buf, "%zu", x);
  201. ast_db_put("astdbtest", buf, buf);
  202. }
  203. ast_db_deltree("astdbtest", NULL);
  204. return res;
  205. }
  206. AST_TEST_DEFINE(put_get_long)
  207. {
  208. int res = AST_TEST_PASS;
  209. struct ast_str *s;
  210. int i, j;
  211. #define STR_FILL_32 "abcdefghijklmnopqrstuvwxyz123456"
  212. switch (cmd) {
  213. case TEST_INIT:
  214. info->name = "put_get_long";
  215. info->category = "/main/astdb/";
  216. info->summary = "ast_db_(put|get_allocated) unit test";
  217. info->description =
  218. "Ensures that the ast_db_put and ast_db_get_allocated functions work";
  219. return AST_TEST_NOT_RUN;
  220. case TEST_EXECUTE:
  221. break;
  222. }
  223. if (!(s = ast_str_create(4096))) {
  224. return AST_TEST_FAIL;
  225. }
  226. for (i = 1024; i <= 1024 * 1024 * 8; i *= 2) {
  227. char *out = NULL;
  228. ast_str_reset(s);
  229. for (j = 0; j < i; j += sizeof(STR_FILL_32) - 1) {
  230. ast_str_append(&s, 0, "%s", STR_FILL_32);
  231. }
  232. if (ast_db_put("astdbtest", "long", ast_str_buffer(s))) {
  233. ast_test_status_update(test, "Failed to put value of %zu bytes\n", ast_str_strlen(s));
  234. res = AST_TEST_FAIL;
  235. } else if (ast_db_get_allocated("astdbtest", "long", &out)) {
  236. ast_test_status_update(test, "Failed to get value of %zu bytes\n", ast_str_strlen(s));
  237. res = AST_TEST_FAIL;
  238. } else if (strcmp(ast_str_buffer(s), out)) {
  239. ast_test_status_update(test, "Failed to match value of %zu bytes\n", ast_str_strlen(s));
  240. res = AST_TEST_FAIL;
  241. } else if (ast_db_del("astdbtest", "long")) {
  242. ast_test_status_update(test, "Failed to delete astdbtest/long\n");
  243. res = AST_TEST_FAIL;
  244. }
  245. if (out) {
  246. ast_free(out);
  247. }
  248. }
  249. ast_free(s);
  250. return res;
  251. }
  252. static int unload_module(void)
  253. {
  254. AST_TEST_UNREGISTER(put_get_del);
  255. AST_TEST_UNREGISTER(gettree_deltree);
  256. AST_TEST_UNREGISTER(perftest);
  257. AST_TEST_UNREGISTER(put_get_long);
  258. return 0;
  259. }
  260. static int load_module(void)
  261. {
  262. AST_TEST_REGISTER(put_get_del);
  263. AST_TEST_REGISTER(gettree_deltree);
  264. AST_TEST_REGISTER(perftest);
  265. AST_TEST_REGISTER(put_get_long);
  266. return AST_MODULE_LOAD_SUCCESS;
  267. }
  268. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AstDB test module");