test_sorcery_realtime.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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 Sorcery Unit Tests
  21. *
  22. * \author Joshua Colp <jcolp@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/sorcery.h"
  34. #include "asterisk/astdb.h"
  35. #include "asterisk/logger.h"
  36. /*! \brief Configuration structure which contains all stored objects */
  37. static struct ast_config *realtime_objects;
  38. static struct ast_variable *realtime_sorcery(const char *database, const char *table, const struct ast_variable *fields)
  39. {
  40. char *object_id = NULL;
  41. while ((object_id = ast_category_browse(realtime_objects, object_id))) {
  42. if (!ast_variable_lists_match(ast_category_root(realtime_objects, object_id), fields, 0)) {
  43. continue;
  44. }
  45. return ast_variables_dup(ast_category_root(realtime_objects, object_id));
  46. }
  47. return NULL;
  48. }
  49. static struct ast_config *realtime_sorcery_multi(const char *database, const char *table, const struct ast_variable *fields)
  50. {
  51. struct ast_config *objects;
  52. char *object_id = NULL;
  53. if (!(objects = ast_config_new())) {
  54. return NULL;
  55. }
  56. while ((object_id = ast_category_browse(realtime_objects, object_id))) {
  57. struct ast_category *object;
  58. const struct ast_variable *object_fields = ast_category_root(realtime_objects, object_id);
  59. if (!ast_variable_lists_match(object_fields, fields, 0)) {
  60. continue;
  61. }
  62. if (!(object = ast_category_new("", "", 0))) {
  63. ast_config_destroy(objects);
  64. return NULL;
  65. }
  66. ast_variable_append(object, ast_variables_dup(ast_category_root(realtime_objects, object_id)));
  67. ast_category_append(objects, object);
  68. }
  69. return objects;
  70. }
  71. static int realtime_sorcery_update(const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields)
  72. {
  73. struct ast_category *object, *found;
  74. if (!(found = ast_category_get(realtime_objects, entity, NULL))) {
  75. return 0;
  76. } else if (!(object = ast_category_new(entity, "", 0))) {
  77. return -1;
  78. }
  79. ast_category_delete(realtime_objects, found);
  80. ast_variable_append(object, ast_variables_dup((struct ast_variable*)fields));
  81. ast_variable_append(object, ast_variable_new(keyfield, entity, ""));
  82. ast_category_append(realtime_objects, object);
  83. return 1;
  84. }
  85. static int realtime_sorcery_store(const char *database, const char *table, const struct ast_variable *fields)
  86. {
  87. /* The key field is explicit within res_sorcery_realtime */
  88. const struct ast_variable *keyfield = ast_variable_find_variable_in_list(fields, "id");
  89. struct ast_category *object;
  90. if (!keyfield || ast_category_exist(realtime_objects, keyfield->value, NULL) || !(object = ast_category_new(keyfield->value, "", 0))) {
  91. return -1;
  92. }
  93. ast_variable_append(object, ast_variables_dup((struct ast_variable*)fields));
  94. ast_category_append(realtime_objects, object);
  95. return 1;
  96. }
  97. static int realtime_sorcery_destroy(const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields)
  98. {
  99. struct ast_category *found;
  100. if (!(found = ast_category_get(realtime_objects, entity, NULL))) {
  101. return 0;
  102. }
  103. ast_category_delete(realtime_objects, found);
  104. return 1;
  105. }
  106. struct ast_config_engine sorcery_config_engine = {
  107. .name = "sorcery_realtime_test",
  108. .realtime_func = realtime_sorcery,
  109. .realtime_multi_func = realtime_sorcery_multi,
  110. .update_func = realtime_sorcery_update,
  111. .store_func = realtime_sorcery_store,
  112. .destroy_func = realtime_sorcery_destroy,
  113. };
  114. /*! \brief Dummy sorcery object */
  115. struct test_sorcery_object {
  116. SORCERY_OBJECT(details);
  117. unsigned int bob;
  118. unsigned int joe;
  119. };
  120. /*! \brief Internal function to allocate a test object */
  121. static void *test_sorcery_object_alloc(const char *id)
  122. {
  123. return ast_sorcery_generic_alloc(sizeof(struct test_sorcery_object), NULL);
  124. }
  125. static struct ast_sorcery *alloc_and_initialize_sorcery(char *table)
  126. {
  127. struct ast_sorcery *sorcery;
  128. if (!(sorcery = ast_sorcery_open())) {
  129. return NULL;
  130. }
  131. if ((ast_sorcery_apply_default(sorcery, "test", "realtime", table) != AST_SORCERY_APPLY_SUCCESS) ||
  132. ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL) ||
  133. !(realtime_objects = ast_config_new())) {
  134. ast_sorcery_unref(sorcery);
  135. return NULL;
  136. }
  137. ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
  138. ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
  139. return sorcery;
  140. }
  141. static void deinitialize_sorcery(struct ast_sorcery *sorcery)
  142. {
  143. ast_config_destroy(realtime_objects);
  144. realtime_objects = NULL;
  145. ast_sorcery_unref(sorcery);
  146. }
  147. AST_TEST_DEFINE(object_create)
  148. {
  149. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  150. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  151. switch (cmd) {
  152. case TEST_INIT:
  153. info->name = "object_create";
  154. info->category = "/res/sorcery_realtime/";
  155. info->summary = "sorcery realtime object creation unit test";
  156. info->description =
  157. "Test object creation in sorcery using realtime wizard";
  158. return AST_TEST_NOT_RUN;
  159. case TEST_EXECUTE:
  160. break;
  161. }
  162. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  163. ast_test_status_update(test, "Failed to open sorcery structure\n");
  164. return AST_TEST_FAIL;
  165. }
  166. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  167. ast_test_status_update(test, "Failed to allocate a known object type\n");
  168. return AST_TEST_FAIL;
  169. }
  170. if (ast_sorcery_create(sorcery, obj)) {
  171. ast_test_status_update(test, "Failed to create object using realtime wizard\n");
  172. return AST_TEST_FAIL;
  173. }
  174. return AST_TEST_PASS;
  175. }
  176. AST_TEST_DEFINE(object_retrieve_id)
  177. {
  178. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  179. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  180. switch (cmd) {
  181. case TEST_INIT:
  182. info->name = "object_retrieve_id";
  183. info->category = "/res/sorcery_realtime/";
  184. info->summary = "sorcery object retrieval using id unit test";
  185. info->description =
  186. "Test object retrieval using id in sorcery with realtime wizard";
  187. return AST_TEST_NOT_RUN;
  188. case TEST_EXECUTE:
  189. break;
  190. }
  191. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  192. ast_test_status_update(test, "Failed to open sorcery structure\n");
  193. return AST_TEST_FAIL;
  194. }
  195. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  196. ast_test_status_update(test, "Failed to allocate a known object type\n");
  197. return AST_TEST_FAIL;
  198. }
  199. if (ast_sorcery_create(sorcery, obj)) {
  200. ast_test_status_update(test, "Failed to create object using astdb wizard\n");
  201. return AST_TEST_FAIL;
  202. }
  203. ao2_cleanup(obj);
  204. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
  205. ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
  206. return AST_TEST_FAIL;
  207. }
  208. if (ast_sorcery_create(sorcery, obj)) {
  209. ast_test_status_update(test, "Failed to create second object using astdb wizard\n");
  210. return AST_TEST_FAIL;
  211. }
  212. ao2_cleanup(obj);
  213. if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  214. ast_test_status_update(test, "Failed to retrieve properly created object using id of 'blah'\n");
  215. return AST_TEST_FAIL;
  216. } else if (strcmp(ast_sorcery_object_get_id(obj), "blah")) {
  217. ast_test_status_update(test, "Retrieved object does not have correct id\n");
  218. return AST_TEST_FAIL;
  219. }
  220. return AST_TEST_PASS;
  221. }
  222. AST_TEST_DEFINE(object_retrieve_field)
  223. {
  224. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  225. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  226. RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "42", ""), ast_variables_destroy);
  227. switch (cmd) {
  228. case TEST_INIT:
  229. info->name = "object_retrieve_field";
  230. info->category = "/res/sorcery_realtime/";
  231. info->summary = "sorcery object retrieval using a specific field unit test";
  232. info->description =
  233. "Test object retrieval using a specific field in sorcery with realtime wizard";
  234. return AST_TEST_NOT_RUN;
  235. case TEST_EXECUTE:
  236. break;
  237. }
  238. if (!fields) {
  239. ast_test_status_update(test, "Failed to create fields for object retrieval attempt\n");
  240. return AST_TEST_FAIL;
  241. }
  242. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  243. ast_test_status_update(test, "Failed to open sorcery structure\n");
  244. return AST_TEST_FAIL;
  245. }
  246. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  247. ast_test_status_update(test, "Failed to allocate a known object type\n");
  248. return AST_TEST_FAIL;
  249. }
  250. obj->joe = 42;
  251. if (ast_sorcery_create(sorcery, obj)) {
  252. ast_test_status_update(test, "Failed to create object using realtime wizard\n");
  253. return AST_TEST_FAIL;
  254. }
  255. ao2_cleanup(obj);
  256. if (!(obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) {
  257. ast_test_status_update(test, "Failed to retrieve properly created object using 'joe' field\n");
  258. return AST_TEST_FAIL;
  259. }
  260. ao2_cleanup(obj);
  261. ast_variables_destroy(fields);
  262. if (!(fields = ast_variable_new("joe", "49", ""))) {
  263. ast_test_status_update(test, "Failed to create fields for object retrieval attempt\n");
  264. return AST_TEST_FAIL;
  265. }
  266. if ((obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) {
  267. ast_test_status_update(test, "Retrieved an object using a field with an in-correct value... that should not happen\n");
  268. return AST_TEST_FAIL;
  269. }
  270. return AST_TEST_PASS;
  271. }
  272. AST_TEST_DEFINE(object_retrieve_multiple_all)
  273. {
  274. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  275. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  276. RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
  277. switch (cmd) {
  278. case TEST_INIT:
  279. info->name = "object_retrieve_multiple_all";
  280. info->category = "/res/sorcery_realtime/";
  281. info->summary = "sorcery multiple object retrieval unit test";
  282. info->description =
  283. "Test multiple object retrieval in sorcery using realtime wizard";
  284. return AST_TEST_NOT_RUN;
  285. case TEST_EXECUTE:
  286. break;
  287. }
  288. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  289. ast_test_status_update(test, "Failed to open sorcery structure\n");
  290. return AST_TEST_FAIL;
  291. }
  292. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  293. ast_test_status_update(test, "Failed to allocate a known object type\n");
  294. return AST_TEST_FAIL;
  295. }
  296. if (ast_sorcery_create(sorcery, obj)) {
  297. ast_test_status_update(test, "Failed to create object using realtime wizard\n");
  298. return AST_TEST_FAIL;
  299. }
  300. ao2_cleanup(obj);
  301. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
  302. ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
  303. return AST_TEST_FAIL;
  304. }
  305. if (ast_sorcery_create(sorcery, obj)) {
  306. ast_test_status_update(test, "Failed to create second object using realtime wizard\n");
  307. return AST_TEST_FAIL;
  308. }
  309. if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL))) {
  310. ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
  311. return AST_TEST_FAIL;
  312. } else if (ao2_container_count(objects) != 2) {
  313. ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
  314. return AST_TEST_FAIL;
  315. }
  316. return AST_TEST_PASS;
  317. }
  318. AST_TEST_DEFINE(object_retrieve_multiple_all_nofetch)
  319. {
  320. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  321. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  322. RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
  323. switch (cmd) {
  324. case TEST_INIT:
  325. info->name = "object_retrieve_multiple_all_nofetch";
  326. info->category = "/res/sorcery_realtime/";
  327. info->summary = "sorcery multiple object retrieval unit test";
  328. info->description =
  329. "Test multiple object retrieval in sorcery using realtime wizard";
  330. return AST_TEST_NOT_RUN;
  331. case TEST_EXECUTE:
  332. break;
  333. }
  334. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test,allow_unqualified_fetch=no"))) {
  335. ast_test_status_update(test, "Failed to open sorcery structure\n");
  336. return AST_TEST_FAIL;
  337. }
  338. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  339. ast_test_status_update(test, "Failed to allocate a known object type\n");
  340. return AST_TEST_FAIL;
  341. }
  342. if (ast_sorcery_create(sorcery, obj)) {
  343. ast_test_status_update(test, "Failed to create object using realtime wizard\n");
  344. return AST_TEST_FAIL;
  345. }
  346. ao2_cleanup(obj);
  347. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
  348. ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
  349. return AST_TEST_FAIL;
  350. }
  351. if (ast_sorcery_create(sorcery, obj)) {
  352. ast_test_status_update(test, "Failed to create second object using realtime wizard\n");
  353. return AST_TEST_FAIL;
  354. }
  355. if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL))) {
  356. ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
  357. return AST_TEST_FAIL;
  358. } else if (ao2_container_count(objects) != 0) {
  359. ast_test_status_update(test, "Received a container with objects in it when there should be none\n");
  360. return AST_TEST_FAIL;
  361. }
  362. return AST_TEST_PASS;
  363. }
  364. AST_TEST_DEFINE(object_retrieve_multiple_field)
  365. {
  366. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  367. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  368. RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
  369. RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "6", ""), ast_variables_destroy);
  370. switch (cmd) {
  371. case TEST_INIT:
  372. info->name = "object_retrieve_multiple_field";
  373. info->category = "/res/sorcery_realtime/";
  374. info->summary = "sorcery multiple object retrieval unit test";
  375. info->description =
  376. "Test multiple object retrieval in sorcery using fields using realtime wizard";
  377. return AST_TEST_NOT_RUN;
  378. case TEST_EXECUTE:
  379. break;
  380. }
  381. if (!fields) {
  382. ast_test_status_update(test, "Failed to create fields for multiple retrieve\n");
  383. return AST_TEST_FAIL;
  384. }
  385. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  386. ast_test_status_update(test, "Failed to open sorcery structure\n");
  387. return AST_TEST_FAIL;
  388. }
  389. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  390. ast_test_status_update(test, "Failed to allocate a known object type\n");
  391. return AST_TEST_FAIL;
  392. }
  393. obj->joe = 6;
  394. if (ast_sorcery_create(sorcery, obj)) {
  395. ast_test_status_update(test, "Failed to create object using realtime wizard\n");
  396. return AST_TEST_FAIL;
  397. }
  398. if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
  399. ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
  400. return AST_TEST_FAIL;
  401. } else if (ao2_container_count(objects) != 1) {
  402. ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
  403. return AST_TEST_FAIL;
  404. }
  405. ao2_cleanup(objects);
  406. ast_variables_destroy(fields);
  407. if (!(fields = ast_variable_new("joe", "7", ""))) {
  408. ast_test_status_update(test, "Failed to create fields for multiple retrieval\n");
  409. return AST_TEST_FAIL;
  410. } else if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
  411. ast_test_status_update(test, "Failed to retrieve an empty container when retrieving multiple\n");
  412. return AST_TEST_FAIL;
  413. } else if (ao2_container_count(objects)) {
  414. ast_test_status_update(test, "Received a container with objects when there should be none in it\n");
  415. return AST_TEST_FAIL;
  416. }
  417. return AST_TEST_PASS;
  418. }
  419. AST_TEST_DEFINE(object_retrieve_regex)
  420. {
  421. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  422. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  423. RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
  424. switch (cmd) {
  425. case TEST_INIT:
  426. info->name = "object_retrieve_regex";
  427. info->category = "/res/sorcery_realtime/";
  428. info->summary = "sorcery multiple object retrieval using regex unit test";
  429. info->description =
  430. "Test multiple object retrieval in sorcery using regular expression for matching using realtime wizard";
  431. return AST_TEST_NOT_RUN;
  432. case TEST_EXECUTE:
  433. break;
  434. }
  435. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  436. ast_test_status_update(test, "Failed to open sorcery structure\n");
  437. return AST_TEST_FAIL;
  438. }
  439. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-98joe"))) {
  440. ast_test_status_update(test, "Failed to allocate a known object type\n");
  441. return AST_TEST_FAIL;
  442. }
  443. if (ast_sorcery_create(sorcery, obj)) {
  444. ast_test_status_update(test, "Failed to create object using realtime wizard\n");
  445. return AST_TEST_FAIL;
  446. }
  447. ao2_cleanup(obj);
  448. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-93joe"))) {
  449. ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
  450. return AST_TEST_FAIL;
  451. }
  452. if (ast_sorcery_create(sorcery, obj)) {
  453. ast_test_status_update(test, "Failed to create second object using astdb wizard\n");
  454. return AST_TEST_FAIL;
  455. }
  456. ao2_cleanup(obj);
  457. if (!(obj = ast_sorcery_alloc(sorcery, "test", "neener-93joe"))) {
  458. ast_test_status_update(test, "Failed to allocate third instance of a known object type\n");
  459. return AST_TEST_FAIL;
  460. }
  461. if (ast_sorcery_create(sorcery, obj)) {
  462. ast_test_status_update(test, "Failed to create third object using astdb wizard\n");
  463. return AST_TEST_FAIL;
  464. }
  465. if (!(objects = ast_sorcery_retrieve_by_regex(sorcery, "test", "blah-"))) {
  466. ast_test_status_update(test, "Failed to retrieve a container of objects\n");
  467. return AST_TEST_FAIL;
  468. } else if (ao2_container_count(objects) != 2) {
  469. ast_test_status_update(test, "Received a container with incorrect number of objects in it: %d instead of 2\n", ao2_container_count(objects));
  470. return AST_TEST_FAIL;
  471. }
  472. return AST_TEST_PASS;
  473. }
  474. AST_TEST_DEFINE(object_retrieve_regex_nofetch)
  475. {
  476. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  477. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  478. RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
  479. switch (cmd) {
  480. case TEST_INIT:
  481. info->name = "object_retrieve_regex_nofetch";
  482. info->category = "/res/sorcery_realtime/";
  483. info->summary = "sorcery multiple object retrieval using regex unit test";
  484. info->description =
  485. "Test multiple object retrieval in sorcery using regular expression for matching using realtime wizard";
  486. return AST_TEST_NOT_RUN;
  487. case TEST_EXECUTE:
  488. break;
  489. }
  490. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test,allow_unqualified_fetch=no"))) {
  491. ast_test_status_update(test, "Failed to open sorcery structure\n");
  492. return AST_TEST_FAIL;
  493. }
  494. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-98joe"))) {
  495. ast_test_status_update(test, "Failed to allocate a known object type\n");
  496. return AST_TEST_FAIL;
  497. }
  498. if (ast_sorcery_create(sorcery, obj)) {
  499. ast_test_status_update(test, "Failed to create object using realtime wizard\n");
  500. return AST_TEST_FAIL;
  501. }
  502. ao2_cleanup(obj);
  503. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-93joe"))) {
  504. ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
  505. return AST_TEST_FAIL;
  506. }
  507. if (ast_sorcery_create(sorcery, obj)) {
  508. ast_test_status_update(test, "Failed to create second object using astdb wizard\n");
  509. return AST_TEST_FAIL;
  510. }
  511. ao2_cleanup(obj);
  512. if (!(obj = ast_sorcery_alloc(sorcery, "test", "neener-93joe"))) {
  513. ast_test_status_update(test, "Failed to allocate third instance of a known object type\n");
  514. return AST_TEST_FAIL;
  515. }
  516. if (ast_sorcery_create(sorcery, obj)) {
  517. ast_test_status_update(test, "Failed to create third object using astdb wizard\n");
  518. return AST_TEST_FAIL;
  519. }
  520. if (!(objects = ast_sorcery_retrieve_by_regex(sorcery, "test", ""))) {
  521. ast_test_status_update(test, "Failed to retrieve a container of objects\n");
  522. return AST_TEST_FAIL;
  523. } else if (ao2_container_count(objects) != 0) {
  524. ast_test_status_update(test, "Received a container with incorrect number of objects in it: %d instead of 0\n", ao2_container_count(objects));
  525. return AST_TEST_FAIL;
  526. }
  527. return AST_TEST_PASS;
  528. }
  529. AST_TEST_DEFINE(object_update)
  530. {
  531. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  532. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  533. RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
  534. switch (cmd) {
  535. case TEST_INIT:
  536. info->name = "object_update";
  537. info->category = "/res/sorcery_realtime/";
  538. info->summary = "sorcery object update unit test";
  539. info->description =
  540. "Test object updating in sorcery using realtime wizard";
  541. return AST_TEST_NOT_RUN;
  542. case TEST_EXECUTE:
  543. break;
  544. }
  545. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  546. ast_test_status_update(test, "Failed to open sorcery structure\n");
  547. return AST_TEST_FAIL;
  548. }
  549. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  550. ast_test_status_update(test, "Failed to allocate a known object type\n");
  551. return AST_TEST_FAIL;
  552. }
  553. if (ast_sorcery_create(sorcery, obj)) {
  554. ast_test_status_update(test, "Failed to create object using realtime wizard\n");
  555. return AST_TEST_FAIL;
  556. }
  557. if (!(obj2 = ast_sorcery_copy(sorcery, obj))) {
  558. ast_test_status_update(test, "Failed to allocate a known object type for updating\n");
  559. return AST_TEST_FAIL;
  560. }
  561. ao2_cleanup(obj);
  562. obj2->bob = 1000;
  563. obj2->joe = 2000;
  564. if (ast_sorcery_update(sorcery, obj2)) {
  565. ast_test_status_update(test, "Failed to update sorcery with new object\n");
  566. return AST_TEST_FAIL;
  567. }
  568. if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  569. ast_test_status_update(test, "Failed to retrieve properly updated object\n");
  570. return AST_TEST_FAIL;
  571. } else if ((obj->bob != obj2->bob) || (obj->joe != obj2->joe)) {
  572. ast_test_status_update(test, "Object retrieved is not the updated object\n");
  573. return AST_TEST_FAIL;
  574. }
  575. return AST_TEST_PASS;
  576. }
  577. AST_TEST_DEFINE(object_delete)
  578. {
  579. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  580. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  581. switch (cmd) {
  582. case TEST_INIT:
  583. info->name = "object_delete";
  584. info->category = "/res/sorcery_realtime/";
  585. info->summary = "sorcery object deletion unit test";
  586. info->description =
  587. "Test object deletion in sorcery using realtime wizard";
  588. return AST_TEST_NOT_RUN;
  589. case TEST_EXECUTE:
  590. break;
  591. }
  592. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  593. ast_test_status_update(test, "Failed to open sorcery structure\n");
  594. return AST_TEST_FAIL;
  595. }
  596. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  597. ast_test_status_update(test, "Failed to allocate a known object type\n");
  598. return AST_TEST_FAIL;
  599. }
  600. if (ast_sorcery_create(sorcery, obj)) {
  601. ast_test_status_update(test, "Failed to create object using realtime wizard\n");
  602. return AST_TEST_FAIL;
  603. }
  604. if (ast_sorcery_delete(sorcery, obj)) {
  605. ast_test_status_update(test, "Failed to delete object using realtime wizard\n");
  606. return AST_TEST_FAIL;
  607. }
  608. ao2_cleanup(obj);
  609. if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  610. ast_test_status_update(test, "Retrieved deleted object that should not be there\n");
  611. return AST_TEST_FAIL;
  612. }
  613. return AST_TEST_PASS;
  614. }
  615. AST_TEST_DEFINE(object_delete_uncreated)
  616. {
  617. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  618. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  619. switch (cmd) {
  620. case TEST_INIT:
  621. info->name = "object_delete_uncreated";
  622. info->category = "/res/sorcery_realtime/";
  623. info->summary = "sorcery object deletion unit test";
  624. info->description =
  625. "Test object deletion of an uncreated object in sorcery using realtime wizard";
  626. return AST_TEST_NOT_RUN;
  627. case TEST_EXECUTE:
  628. break;
  629. }
  630. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  631. ast_test_status_update(test, "Failed to open sorcery structure\n");
  632. return AST_TEST_FAIL;
  633. }
  634. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  635. ast_test_status_update(test, "Failed to allocate a known object type\n");
  636. return AST_TEST_FAIL;
  637. }
  638. if (!ast_sorcery_delete(sorcery, obj)) {
  639. ast_test_status_update(test, "Successfully deleted an object which was never created\n");
  640. return AST_TEST_FAIL;
  641. }
  642. return AST_TEST_PASS;
  643. }
  644. AST_TEST_DEFINE(object_allocate_on_retrieval)
  645. {
  646. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  647. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  648. struct ast_category *cat;
  649. switch (cmd) {
  650. case TEST_INIT:
  651. info->name = "object_allocate_on_retrieval";
  652. info->category = "/res/sorcery_realtime/";
  653. info->summary = "sorcery object allocation upon retrieval unit test";
  654. info->description =
  655. "This test creates data in a realtime backend, not through sorcery. Sorcery is then\n"
  656. "instructed to retrieve an object with the id of the object that was created in the\n"
  657. "realtime backend. Sorcery should be able to allocate the object appropriately";
  658. return AST_TEST_NOT_RUN;
  659. case TEST_EXECUTE:
  660. break;
  661. }
  662. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  663. ast_test_status_update(test, "Failed to open sorcery structure\n");
  664. return AST_TEST_FAIL;
  665. }
  666. cat = ast_category_new("blah", "", 0);
  667. ast_variable_append(cat, ast_variable_new("id", "blah", ""));
  668. ast_variable_append(cat, ast_variable_new("bob", "42", ""));
  669. ast_variable_append(cat, ast_variable_new("joe", "93", ""));
  670. ast_category_append(realtime_objects, cat);
  671. if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  672. ast_test_status_update(test, "Failed to allocate object 'blah' base on realtime data\n");
  673. return AST_TEST_FAIL;
  674. }
  675. if (obj->bob != 42) {
  676. ast_test_status_update(test, "Object's 'bob' field does not have expected value: %u != 42\n",
  677. obj->bob);
  678. return AST_TEST_FAIL;
  679. } else if (obj->joe != 93) {
  680. ast_test_status_update(test, "Object's 'joe' field does not have expected value: %u != 93\n",
  681. obj->joe);
  682. return AST_TEST_FAIL;
  683. }
  684. return AST_TEST_PASS;
  685. }
  686. AST_TEST_DEFINE(object_filter)
  687. {
  688. RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
  689. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  690. struct ast_category *cat;
  691. switch (cmd) {
  692. case TEST_INIT:
  693. info->name = "object_filter";
  694. info->category = "/res/sorcery_realtime/";
  695. info->summary = "sorcery object field filter unit test";
  696. info->description =
  697. "This test creates data in a realtime backend, not through sorcery. In addition to\n"
  698. "the object fields that have been registered with sorcery, there is data in the\n"
  699. "realtime backend that is unknown to sorcery. When sorcery attempts to retrieve\n"
  700. "the object from the realtime backend, the data unknown to sorcery should be\n"
  701. "filtered out of the returned objectset, and the object should be successfully\n"
  702. "allocated by sorcery";
  703. return AST_TEST_NOT_RUN;
  704. case TEST_EXECUTE:
  705. break;
  706. }
  707. if (!(sorcery = alloc_and_initialize_sorcery("sorcery_realtime_test"))) {
  708. ast_test_status_update(test, "Failed to open sorcery structure\n");
  709. return AST_TEST_FAIL;
  710. }
  711. cat = ast_category_new("blah", "", 0);
  712. ast_variable_append(cat, ast_variable_new("id", "blah", ""));
  713. ast_variable_append(cat, ast_variable_new("bob", "42", ""));
  714. ast_variable_append(cat, ast_variable_new("joe", "93", ""));
  715. ast_variable_append(cat, ast_variable_new("fred", "50", ""));
  716. ast_category_append(realtime_objects, cat);
  717. if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  718. ast_test_status_update(test, "Failed to retrieve properly created object using id of 'blah'\n");
  719. return AST_TEST_FAIL;
  720. }
  721. if (obj->bob != 42) {
  722. ast_test_status_update(test, "Object's 'bob' field does not have expected value: %u != 42\n",
  723. obj->bob);
  724. return AST_TEST_FAIL;
  725. } else if (obj->joe != 93) {
  726. ast_test_status_update(test, "Object's 'joe' field does not have expected value: %u != 93\n",
  727. obj->joe);
  728. return AST_TEST_FAIL;
  729. }
  730. return AST_TEST_PASS;
  731. }
  732. static int unload_module(void)
  733. {
  734. ast_config_engine_deregister(&sorcery_config_engine);
  735. AST_TEST_UNREGISTER(object_create);
  736. AST_TEST_UNREGISTER(object_retrieve_id);
  737. AST_TEST_UNREGISTER(object_retrieve_field);
  738. AST_TEST_UNREGISTER(object_retrieve_multiple_all);
  739. AST_TEST_UNREGISTER(object_retrieve_multiple_all_nofetch);
  740. AST_TEST_UNREGISTER(object_retrieve_multiple_field);
  741. AST_TEST_UNREGISTER(object_retrieve_regex);
  742. AST_TEST_UNREGISTER(object_retrieve_regex_nofetch);
  743. AST_TEST_UNREGISTER(object_update);
  744. AST_TEST_UNREGISTER(object_delete);
  745. AST_TEST_UNREGISTER(object_delete_uncreated);
  746. AST_TEST_UNREGISTER(object_allocate_on_retrieval);
  747. AST_TEST_UNREGISTER(object_filter);
  748. return 0;
  749. }
  750. static int load_module(void)
  751. {
  752. ast_config_engine_register(&sorcery_config_engine);
  753. ast_realtime_append_mapping("sorcery_realtime_test", "sorcery_realtime_test", "test", "test", 1);
  754. AST_TEST_REGISTER(object_create);
  755. AST_TEST_REGISTER(object_retrieve_id);
  756. AST_TEST_REGISTER(object_retrieve_field);
  757. AST_TEST_REGISTER(object_retrieve_multiple_all);
  758. AST_TEST_REGISTER(object_retrieve_multiple_all_nofetch);
  759. AST_TEST_REGISTER(object_retrieve_multiple_field);
  760. AST_TEST_REGISTER(object_retrieve_regex);
  761. AST_TEST_REGISTER(object_retrieve_regex_nofetch);
  762. AST_TEST_REGISTER(object_update);
  763. AST_TEST_REGISTER(object_delete);
  764. AST_TEST_REGISTER(object_delete_uncreated);
  765. AST_TEST_REGISTER(object_allocate_on_retrieval);
  766. AST_TEST_REGISTER(object_filter);
  767. return AST_MODULE_LOAD_SUCCESS;
  768. }
  769. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Sorcery Realtime Wizard test module");