test_db.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "")
  30. #include "asterisk/test.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/astdb.h"
  33. #include "asterisk/logger.h"
  34. enum {
  35. FAMILY = 0,
  36. KEY = 1,
  37. VALUE = 2,
  38. };
  39. /* Longest value we can support is 256 for family/key/ so, with
  40. * family = astdbtest and two slashes we are left with 244 bytes */
  41. static const char long_val[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  42. AST_TEST_DEFINE(put_get_del)
  43. {
  44. int res = AST_TEST_PASS;
  45. const char *inputs[][3] = {
  46. {"family", "key", "value"},
  47. {"astdbtest", "a", "b"},
  48. {"astdbtest", "a", "a"},
  49. {"astdbtest", "b", "a"},
  50. {"astdbtest", "b", "b"},
  51. {"astdbtest", "b", "!@#$%^&*()|+-<>?"},
  52. {"astdbtest", long_val, "b"},
  53. {"astdbtest", "b", long_val},
  54. {"astdbtest", "!@#$%^&*()|+-<>?", "b"},
  55. };
  56. size_t x;
  57. char buf[sizeof(long_val)] = { 0, };
  58. switch (cmd) {
  59. case TEST_INIT:
  60. info->name = "put_get_del";
  61. info->category = "/main/astdb/";
  62. info->summary = "ast_db_(put|get|del) unit test";
  63. info->description =
  64. "Ensures that the ast_db put, get, and del functions work";
  65. return AST_TEST_NOT_RUN;
  66. case TEST_EXECUTE:
  67. break;
  68. }
  69. for (x = 0; x < ARRAY_LEN(inputs); x++) {
  70. if (ast_db_put(inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE])) {
  71. ast_test_status_update(test, "Failed to put %s : %s : %s\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE]);
  72. res = AST_TEST_FAIL;
  73. }
  74. if (ast_db_get(inputs[x][FAMILY], inputs[x][KEY], buf, sizeof(buf))) {
  75. ast_test_status_update(test, "Failed to get %s : %s : %s\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE]);
  76. res = AST_TEST_FAIL;
  77. } else if (strcmp(buf, inputs[x][VALUE])) {
  78. 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);
  79. res = AST_TEST_FAIL;
  80. }
  81. if (ast_db_del(inputs[x][FAMILY], inputs[x][KEY])) {
  82. ast_test_status_update(test, "Failed to del %s : %s\n", inputs[x][FAMILY], inputs[x][KEY]);
  83. res = AST_TEST_FAIL;
  84. }
  85. }
  86. return res;
  87. }
  88. AST_TEST_DEFINE(gettree_deltree)
  89. {
  90. int res = AST_TEST_PASS;
  91. const char *inputs[][3] = {
  92. #define BASE "astdbtest"
  93. #define SUB1 "one"
  94. #define SUB2 "two"
  95. #define FAM1 BASE "/" SUB1
  96. #define FAM2 BASE "/" SUB2
  97. {FAM1, "one", "blah"},
  98. {FAM1, "two", "bling"},
  99. {FAM1, "three", "blast"},
  100. {FAM2, "one", "blah"},
  101. {FAM2, "two", "bling"},
  102. {FAM2, "three", "blast"},
  103. };
  104. size_t x;
  105. struct ast_db_entry *dbes, *cur;
  106. int num_deleted;
  107. switch (cmd) {
  108. case TEST_INIT:
  109. info->name = "gettree_deltree";
  110. info->category = "/main/astdb/";
  111. info->summary = "ast_db_(gettree|deltree) unit test";
  112. info->description =
  113. "Ensures that the ast_db gettree and deltree functions work";
  114. return AST_TEST_NOT_RUN;
  115. case TEST_EXECUTE:
  116. break;
  117. }
  118. for (x = 0; x < ARRAY_LEN(inputs); x++) {
  119. if (ast_db_put(inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE])) {
  120. ast_test_status_update(test, "Failed to put %s : %s : %s\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE]);
  121. res = AST_TEST_FAIL;
  122. }
  123. }
  124. if (!(dbes = ast_db_gettree(BASE, NULL))) {
  125. ast_test_status_update(test, "Failed to ast_db_gettree family %s\n", BASE);
  126. res = AST_TEST_FAIL;
  127. }
  128. for (cur = dbes, x = 0; cur; cur = cur->next, x++) {
  129. int found = 0;
  130. size_t z;
  131. for (z = 0; z < ARRAY_LEN(inputs); z++) {
  132. char buf[256];
  133. snprintf(buf, sizeof(buf), "/%s/%s", inputs[z][FAMILY], inputs[z][KEY]);
  134. if (!strcmp(buf, cur->key) && !strcmp(inputs[z][VALUE], cur->data)) {
  135. found = 1;
  136. }
  137. }
  138. if (!found) {
  139. ast_test_status_update(test, "inputs array has no entry for %s == %s\n", cur->key, cur->data);
  140. res = AST_TEST_FAIL;
  141. }
  142. }
  143. if (x != ARRAY_LEN(inputs)) {
  144. ast_test_status_update(test, "ast_db_gettree returned %zu entries when we expected %zu\n", x, ARRAY_LEN(inputs));
  145. res = AST_TEST_FAIL;
  146. }
  147. ast_db_freetree(dbes);
  148. if (!(dbes = ast_db_gettree(BASE, SUB1))) {
  149. ast_test_status_update(test, "Failed to ast_db_gettree for %s/%s\n", BASE, SUB1);
  150. res = AST_TEST_FAIL;
  151. }
  152. for (cur = dbes, x = 0; cur; cur = cur->next, x++) {
  153. int found = 0;
  154. size_t z;
  155. for (z = 0; z < ARRAY_LEN(inputs); z++) {
  156. char buf[256];
  157. snprintf(buf, sizeof(buf), "/%s/%s", inputs[z][FAMILY], inputs[z][KEY]);
  158. if (!strcmp(buf, cur->key) && !strcmp(inputs[z][VALUE], cur->data)) {
  159. found = 1;
  160. }
  161. }
  162. if (!found) {
  163. ast_test_status_update(test, "inputs array has no entry for %s == %s\n", cur->key, cur->data);
  164. res = AST_TEST_FAIL;
  165. }
  166. }
  167. if (x != (ARRAY_LEN(inputs) / 2)) {
  168. ast_test_status_update(test, "ast_db_gettree returned %zu entries when we expected %zu\n", x, ARRAY_LEN(inputs) / 2);
  169. res = AST_TEST_FAIL;
  170. }
  171. ast_db_freetree(dbes);
  172. if ((num_deleted = ast_db_deltree(BASE, SUB2)) != ARRAY_LEN(inputs) / 2) {
  173. 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);
  174. res = AST_TEST_FAIL;
  175. }
  176. if ((num_deleted = ast_db_deltree(BASE, NULL)) != ARRAY_LEN(inputs) / 2) {
  177. ast_test_status_update(test, "Failed to deltree %s, expected %zu deletions and got %d\n", BASE, ARRAY_LEN(inputs) / 2, num_deleted);
  178. res = AST_TEST_FAIL;
  179. }
  180. return res;
  181. }
  182. AST_TEST_DEFINE(perftest)
  183. {
  184. int res = AST_TEST_PASS;
  185. size_t x;
  186. char buf[10];
  187. switch (cmd) {
  188. case TEST_INIT:
  189. info->name = "perftest";
  190. info->category = "/main/astdb/";
  191. info->summary = "astdb performance unit test";
  192. info->description =
  193. "Measure astdb performance";
  194. return AST_TEST_NOT_RUN;
  195. case TEST_EXECUTE:
  196. break;
  197. }
  198. for (x = 0; x < 100000; x++) {
  199. sprintf(buf, "%zu", x);
  200. ast_db_put("astdbtest", buf, buf);
  201. }
  202. ast_db_deltree("astdbtest", NULL);
  203. return res;
  204. }
  205. static int unload_module(void)
  206. {
  207. AST_TEST_UNREGISTER(put_get_del);
  208. AST_TEST_UNREGISTER(gettree_deltree);
  209. AST_TEST_UNREGISTER(perftest);
  210. return 0;
  211. }
  212. static int load_module(void)
  213. {
  214. AST_TEST_REGISTER(put_get_del);
  215. AST_TEST_REGISTER(gettree_deltree);
  216. AST_TEST_REGISTER(perftest);
  217. return AST_MODULE_LOAD_SUCCESS;
  218. }
  219. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AstDB test module");