test_event.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@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 Tests for the ast_event API
  21. *
  22. * \author Russell Bryant <russell@digium.com>
  23. *
  24. * \ingroup tests
  25. *
  26. * \todo API Calls not yet touched by a test: XXX TODO
  27. * - ast_event_queue_and_cache()
  28. * - ast_event_get_cached()
  29. * - ast_event_report_subs()
  30. * - ast_event_dump_cache()
  31. * - ast_event_get_ie_type_name()
  32. * - ast_event_get_ie_pltype()
  33. * - ast_event_str_to_event_type()
  34. * - ast_event_str_to_ie_type()
  35. * - ast_event_iterator_init()
  36. * - ast_event_iterator_next()
  37. * - ast_event_iterator_get_ie_type()
  38. * - ast_event_iterator_get_ie_uint()
  39. * - ast_event_iterator_get_ie_bitflags()
  40. * - ast_event_iterator_get_ie_str()
  41. * - ast_event_iterator_get_ie_raw()
  42. */
  43. /*** MODULEINFO
  44. <depend>TEST_FRAMEWORK</depend>
  45. <support_level>extended</support_level>
  46. ***/
  47. #include "asterisk.h"
  48. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  49. #include "asterisk/module.h"
  50. #include "asterisk/utils.h"
  51. #include "asterisk/test.h"
  52. #include "asterisk/event.h"
  53. static int check_event(struct ast_event *event, struct ast_test *test,
  54. enum ast_event_type expected_type, const char *type_name,
  55. const char *str, uint32_t uint, uint32_t bitflags)
  56. {
  57. enum ast_event_type type;
  58. const void *foo;
  59. /* Check #1: Ensure event type is set properly. */
  60. type = ast_event_get_type(event);
  61. if (ast_event_get_type(event) != type) {
  62. ast_test_status_update(test, "Expected event type: '%d', got '%d'\n",
  63. expected_type, type);
  64. return -1;
  65. }
  66. /* Check #2: Check string representation of event type */
  67. if (strcmp(type_name, ast_event_get_type_name(event))) {
  68. ast_test_status_update(test, "Didn't get expected type name: '%s' != '%s'\n",
  69. type_name, ast_event_get_type_name(event));
  70. return -1;
  71. }
  72. /* Check #3: Check for automatically included EID */
  73. if (memcmp(&ast_eid_default, ast_event_get_ie_raw(event, AST_EVENT_IE_EID), sizeof(ast_eid_default))) {
  74. ast_test_status_update(test, "Failed to get EID\n");
  75. return -1;
  76. }
  77. /* Check #4: Check for the string IE */
  78. if (strcmp(str, ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX))) {
  79. ast_test_status_update(test, "Failed to get string IE.\n");
  80. return -1;
  81. }
  82. /* Check #5: Check for the uint IE */
  83. if (uint != ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS)) {
  84. ast_test_status_update(test, "Failed to get uint IE.\n");
  85. return -1;
  86. }
  87. /* Check #6: Check for the bitflags IE */
  88. if (bitflags != ast_event_get_ie_bitflags(event, AST_EVENT_IE_OLDMSGS)) {
  89. ast_test_status_update(test, "Failed to get bitflags IE.\n");
  90. return -1;
  91. }
  92. /* Check #7: Check if a check for a str IE that isn't there works */
  93. if ((foo = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE))) {
  94. ast_test_status_update(test, "DEVICE IE check returned non-NULL %p\n", foo);
  95. return -1;
  96. }
  97. /* Check #8: Check if a check for a uint IE that isn't there returns 0 */
  98. if (ast_event_get_ie_uint(event, AST_EVENT_IE_STATE)) {
  99. ast_test_status_update(test, "OLDMSGS IE should be 0\n");
  100. return -1;
  101. }
  102. ast_test_status_update(test, "Event looks good.\n");
  103. return 0;
  104. }
  105. /*!
  106. * \internal
  107. */
  108. AST_TEST_DEFINE(event_new_test)
  109. {
  110. enum ast_test_result_state res = AST_TEST_PASS;
  111. struct ast_event *event = NULL, *event2 = NULL;
  112. static const enum ast_event_type type = AST_EVENT_CUSTOM;
  113. static const char str[] = "SIP/alligatormittens";
  114. static const uint32_t uint = 0xb00bface;
  115. static const uint32_t bitflags = 0x12488421;
  116. switch (cmd) {
  117. case TEST_INIT:
  118. info->name = "ast_event_new_test";
  119. info->category = "/main/event/";
  120. info->summary = "Test event creation";
  121. info->description =
  122. "This test exercises the API calls that allow allocation "
  123. "of an ast_event.";
  124. return AST_TEST_NOT_RUN;
  125. case TEST_EXECUTE:
  126. break;
  127. }
  128. /*
  129. * Test 2 methods of event creation:
  130. *
  131. * 1) Dynamic via appending each IE individually.
  132. * 2) Statically, with all IEs in ast_event_new().
  133. */
  134. ast_test_status_update(test, "First, test dynamic event creation...\n");
  135. if (!(event = ast_event_new(type, AST_EVENT_IE_END))) {
  136. ast_test_status_update(test, "Failed to allocate ast_event object.\n");
  137. res = AST_TEST_FAIL;
  138. goto return_cleanup;
  139. }
  140. if (ast_event_append_ie_str(&event, AST_EVENT_IE_MAILBOX, str)) {
  141. ast_test_status_update(test, "Failed to append str IE\n");
  142. res = AST_TEST_FAIL;
  143. goto return_cleanup;
  144. }
  145. if (ast_event_append_ie_uint(&event, AST_EVENT_IE_NEWMSGS, uint)) {
  146. ast_test_status_update(test, "Failed to append uint IE\n");
  147. res = AST_TEST_FAIL;
  148. goto return_cleanup;
  149. }
  150. if (ast_event_append_ie_bitflags(&event, AST_EVENT_IE_OLDMSGS, bitflags)) {
  151. ast_test_status_update(test, "Failed to append bitflags IE\n");
  152. res = AST_TEST_FAIL;
  153. goto return_cleanup;
  154. }
  155. if (ast_event_append_eid(&event)) {
  156. ast_test_status_update(test, "Failed to append EID\n");
  157. res = AST_TEST_FAIL;
  158. goto return_cleanup;
  159. }
  160. if (check_event(event, test, type, "Custom", str, uint, bitflags)) {
  161. ast_test_status_update(test, "Dynamically generated event broken\n");
  162. res = AST_TEST_FAIL;
  163. goto return_cleanup;
  164. }
  165. event2 = ast_event_new(type,
  166. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, str,
  167. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, uint,
  168. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_BITFLAGS, bitflags,
  169. AST_EVENT_IE_END);
  170. if (!event2) {
  171. ast_test_status_update(test, "Failed to allocate ast_event object.\n");
  172. res = AST_TEST_FAIL;
  173. goto return_cleanup;
  174. }
  175. if (check_event(event2, test, type, "Custom", str, uint, bitflags)) {
  176. ast_test_status_update(test, "Statically generated event broken\n");
  177. res = AST_TEST_FAIL;
  178. goto return_cleanup;
  179. }
  180. if (ast_event_get_size(event) != ast_event_get_size(event2)) {
  181. ast_test_status_update(test, "Events expected to be identical have different size: %d != %d\n",
  182. (int) ast_event_get_size(event),
  183. (int) ast_event_get_size(event2));
  184. res = AST_TEST_FAIL;
  185. goto return_cleanup;
  186. }
  187. return_cleanup:
  188. if (event) {
  189. ast_event_destroy(event);
  190. event = NULL;
  191. }
  192. if (event2) {
  193. ast_event_destroy(event2);
  194. event2 = NULL;
  195. }
  196. return res;
  197. }
  198. struct event_sub_data {
  199. unsigned int count;
  200. };
  201. static void event_sub_cb(const struct ast_event *event, void *d)
  202. {
  203. struct event_sub_data *data = d;
  204. data->count++;
  205. }
  206. enum test_subs_class_type {
  207. TEST_SUBS_ALL_STR,
  208. TEST_SUBS_CUSTOM_STR,
  209. TEST_SUBS_CUSTOM_RAW,
  210. TEST_SUBS_CUSTOM_UINT,
  211. TEST_SUBS_CUSTOM_BITFLAGS,
  212. TEST_SUBS_CUSTOM_EXISTS,
  213. TEST_SUBS_CUSTOM_DYNAMIC,
  214. TEST_SUBS_CUSTOM_ANY,
  215. /* Must be last. */
  216. TEST_SUBS_TOTAL,
  217. };
  218. /*!
  219. * \internal
  220. * \brief Convert enum test_subs_class_type to string.
  221. *
  222. * \param val Enum value to convert to string.
  223. *
  224. * \return String equivalent of enum value.
  225. */
  226. static const char *test_subs_class_type_str(enum test_subs_class_type val)
  227. {
  228. switch (val) {
  229. case TEST_SUBS_ALL_STR:
  230. return "TEST_SUBS_ALL_STR";
  231. case TEST_SUBS_CUSTOM_STR:
  232. return "TEST_SUBS_CUSTOM_STR";
  233. case TEST_SUBS_CUSTOM_RAW:
  234. return "TEST_SUBS_CUSTOM_RAW";
  235. case TEST_SUBS_CUSTOM_UINT:
  236. return "TEST_SUBS_CUSTOM_UINT";
  237. case TEST_SUBS_CUSTOM_BITFLAGS:
  238. return "TEST_SUBS_CUSTOM_BITFLAGS";
  239. case TEST_SUBS_CUSTOM_EXISTS:
  240. return "TEST_SUBS_CUSTOM_EXISTS";
  241. case TEST_SUBS_CUSTOM_DYNAMIC:
  242. return "TEST_SUBS_CUSTOM_DYNAMIC";
  243. case TEST_SUBS_CUSTOM_ANY:
  244. return "TEST_SUBS_CUSTOM_ANY";
  245. case TEST_SUBS_TOTAL:
  246. break;
  247. }
  248. return "Unknown";
  249. }
  250. /*!
  251. * \internal
  252. * \brief Test event subscriptions
  253. *
  254. * - Query for existing Subscriptions:
  255. * - ast_event_check_subscriber()
  256. */
  257. AST_TEST_DEFINE(event_sub_test)
  258. {
  259. enum ast_test_result_state res = AST_TEST_PASS;
  260. struct ast_event *event;
  261. int i;
  262. enum ast_event_subscriber_res sub_res;
  263. struct {
  264. struct ast_event_sub *sub;
  265. struct event_sub_data data;
  266. const unsigned int expected_count;
  267. } test_subs[TEST_SUBS_TOTAL] = {
  268. [TEST_SUBS_ALL_STR] = {
  269. .expected_count = 2,
  270. },
  271. [TEST_SUBS_CUSTOM_STR] = {
  272. .expected_count = 2,
  273. },
  274. [TEST_SUBS_CUSTOM_RAW] = {
  275. .expected_count = 2,
  276. },
  277. [TEST_SUBS_CUSTOM_UINT] = {
  278. .expected_count = 1,
  279. },
  280. [TEST_SUBS_CUSTOM_BITFLAGS] = {
  281. .expected_count = 4,
  282. },
  283. [TEST_SUBS_CUSTOM_EXISTS] = {
  284. .expected_count = 2,
  285. },
  286. [TEST_SUBS_CUSTOM_DYNAMIC] = {
  287. .expected_count = 1,
  288. },
  289. [TEST_SUBS_CUSTOM_ANY] = {
  290. .expected_count = 6,
  291. },
  292. };
  293. switch (cmd) {
  294. case TEST_INIT:
  295. info->name = "ast_event_subscribe_test";
  296. info->category = "/main/event/";
  297. info->summary = "Test event subscriptions";
  298. info->description =
  299. "This test exercises the API calls that allow subscriptions "
  300. "to events.";
  301. return AST_TEST_NOT_RUN;
  302. case TEST_EXECUTE:
  303. break;
  304. }
  305. ast_test_status_update(test, "Check that NO CUSTOM subscribers exist\n");
  306. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  307. AST_EVENT_IE_END);
  308. if (sub_res != AST_EVENT_SUB_NONE) {
  309. ast_test_status_update(test, "CUSTOM subscriptions should not exist! (%d)\n",
  310. sub_res);
  311. res = AST_TEST_FAIL;
  312. }
  313. /*
  314. * Subscription TEST_SUBS_CUSTOM_STR:
  315. * - allocate normally
  316. * - subscribe to CUSTOM events with a DEVICE STR IE check
  317. */
  318. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_STR subscription\n");
  319. test_subs[TEST_SUBS_CUSTOM_STR].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  320. test_subs_class_type_str(TEST_SUBS_CUSTOM_STR), &test_subs[TEST_SUBS_CUSTOM_STR].data,
  321. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  322. AST_EVENT_IE_END);
  323. if (!test_subs[TEST_SUBS_CUSTOM_STR].sub) {
  324. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_STR subscription\n");
  325. res = AST_TEST_FAIL;
  326. goto return_cleanup;
  327. }
  328. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_STR].sub),
  329. test_subs_class_type_str(TEST_SUBS_CUSTOM_STR))) {
  330. ast_test_status_update(test,
  331. "Unexpected subscription description on TEST_SUBS_CUSTOM_STR subscription\n");
  332. res = AST_TEST_FAIL;
  333. goto return_cleanup;
  334. }
  335. ast_test_status_update(test, "Check that a CUSTOM subscriber exists\n");
  336. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  337. AST_EVENT_IE_END);
  338. if (sub_res != AST_EVENT_SUB_EXISTS) {
  339. ast_test_status_update(test, "A CUSTOM subscription should exist! (%d)\n",
  340. sub_res);
  341. res = AST_TEST_FAIL;
  342. }
  343. /*
  344. * Subscription TEST_SUBS_ALL_STR:
  345. * - allocate normally
  346. * - subscribe to ALL events with a DEVICE STR IE check
  347. */
  348. ast_test_status_update(test, "Adding TEST_SUBS_ALL_STR subscription\n");
  349. test_subs[TEST_SUBS_ALL_STR].sub = ast_event_subscribe(AST_EVENT_ALL, event_sub_cb,
  350. test_subs_class_type_str(TEST_SUBS_ALL_STR), &test_subs[TEST_SUBS_ALL_STR].data,
  351. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  352. AST_EVENT_IE_END);
  353. if (!test_subs[TEST_SUBS_ALL_STR].sub) {
  354. ast_test_status_update(test, "Failed to create TEST_SUBS_ALL_STR subscription\n");
  355. res = AST_TEST_FAIL;
  356. goto return_cleanup;
  357. }
  358. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_ALL_STR].sub),
  359. test_subs_class_type_str(TEST_SUBS_ALL_STR))) {
  360. ast_test_status_update(test,
  361. "Unexpected subscription description on TEST_SUBS_ALL_STR subscription\n");
  362. res = AST_TEST_FAIL;
  363. goto return_cleanup;
  364. }
  365. /*
  366. * Subscription TEST_SUBS_CUSTOM_RAW:
  367. * - allocate normally
  368. * - subscribe to CUSTOM events with a MAILBOX RAW IE check
  369. */
  370. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_RAW subscription\n");
  371. test_subs[TEST_SUBS_CUSTOM_RAW].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  372. test_subs_class_type_str(TEST_SUBS_CUSTOM_RAW), &test_subs[TEST_SUBS_CUSTOM_RAW].data,
  373. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  374. AST_EVENT_IE_END);
  375. if (!test_subs[TEST_SUBS_CUSTOM_RAW].sub) {
  376. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_RAW subscription\n");
  377. res = AST_TEST_FAIL;
  378. goto return_cleanup;
  379. }
  380. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_RAW].sub),
  381. test_subs_class_type_str(TEST_SUBS_CUSTOM_RAW))) {
  382. ast_test_status_update(test,
  383. "Unexpected subscription description on TEST_SUBS_CUSTOM_RAW subscription\n");
  384. res = AST_TEST_FAIL;
  385. goto return_cleanup;
  386. }
  387. /*
  388. * Subscription TEST_SUBS_CUSTOM_UINT:
  389. * - allocate normally
  390. * - subscribe to CUSTOM events with a NEWMSGS UINT IE check
  391. */
  392. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_UINT subscription\n");
  393. test_subs[TEST_SUBS_CUSTOM_UINT].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  394. test_subs_class_type_str(TEST_SUBS_CUSTOM_UINT), &test_subs[TEST_SUBS_CUSTOM_UINT].data,
  395. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  396. AST_EVENT_IE_END);
  397. if (!test_subs[TEST_SUBS_CUSTOM_UINT].sub) {
  398. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_UINT subscription\n");
  399. res = AST_TEST_FAIL;
  400. goto return_cleanup;
  401. }
  402. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_UINT].sub),
  403. test_subs_class_type_str(TEST_SUBS_CUSTOM_UINT))) {
  404. ast_test_status_update(test,
  405. "Unexpected subscription description on TEST_SUBS_CUSTOM_UINT subscription\n");
  406. res = AST_TEST_FAIL;
  407. goto return_cleanup;
  408. }
  409. /*
  410. * Subscription TEST_SUBS_CUSTOM_BITFLAGS:
  411. * - allocate normally
  412. * - subscribe to CUSTOM events with a NEWMSGS BITFLAGS IE check
  413. */
  414. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_BITFLAGS subscription\n");
  415. test_subs[TEST_SUBS_CUSTOM_BITFLAGS].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  416. test_subs_class_type_str(TEST_SUBS_CUSTOM_BITFLAGS), &test_subs[TEST_SUBS_CUSTOM_BITFLAGS].data,
  417. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_BITFLAGS, 0x06,
  418. AST_EVENT_IE_END);
  419. if (!test_subs[TEST_SUBS_CUSTOM_BITFLAGS].sub) {
  420. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_BITFLAGS subscription\n");
  421. res = AST_TEST_FAIL;
  422. goto return_cleanup;
  423. }
  424. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_BITFLAGS].sub),
  425. test_subs_class_type_str(TEST_SUBS_CUSTOM_BITFLAGS))) {
  426. ast_test_status_update(test,
  427. "Unexpected subscription description on TEST_SUBS_CUSTOM_BITFLAGS subscription\n");
  428. res = AST_TEST_FAIL;
  429. goto return_cleanup;
  430. }
  431. /*
  432. * Subscription TEST_SUBS_CUSTOM_EXISTS:
  433. * - allocate normally
  434. * - subscribe to CUSTOM events with a NEWMSGS UINT and OLDMSGS EXISTS IE check
  435. */
  436. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_EXISTS subscription\n");
  437. test_subs[TEST_SUBS_CUSTOM_EXISTS].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  438. test_subs_class_type_str(TEST_SUBS_CUSTOM_EXISTS), &test_subs[TEST_SUBS_CUSTOM_EXISTS].data,
  439. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  440. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
  441. AST_EVENT_IE_END);
  442. if (!test_subs[TEST_SUBS_CUSTOM_EXISTS].sub) {
  443. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_EXISTS subscription\n");
  444. res = AST_TEST_FAIL;
  445. goto return_cleanup;
  446. }
  447. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_EXISTS].sub),
  448. test_subs_class_type_str(TEST_SUBS_CUSTOM_EXISTS))) {
  449. ast_test_status_update(test,
  450. "Unexpected subscription description on TEST_SUBS_CUSTOM_EXISTS subscription\n");
  451. res = AST_TEST_FAIL;
  452. goto return_cleanup;
  453. }
  454. /* For the sake of exercising destruction before activation */
  455. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = ast_event_subscribe_new(AST_EVENT_CUSTOM,
  456. event_sub_cb, &test_subs[TEST_SUBS_CUSTOM_DYNAMIC].data);
  457. if (!test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub) {
  458. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  459. res = AST_TEST_FAIL;
  460. goto return_cleanup;
  461. }
  462. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  463. /*
  464. * Subscription TEST_SUBS_CUSTOM_DYNAMIC:
  465. * - allocate dynamically
  466. * - subscribe to all CUSTOM events
  467. * - add IE checks for all types
  468. */
  469. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  470. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = ast_event_subscribe_new(AST_EVENT_CUSTOM,
  471. event_sub_cb, &test_subs[TEST_SUBS_CUSTOM_DYNAMIC].data);
  472. if (!test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub) {
  473. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  474. res = AST_TEST_FAIL;
  475. goto return_cleanup;
  476. }
  477. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub),
  478. "")) {
  479. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  480. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  481. ast_test_status_update(test,
  482. "Unexpected subscription description on TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  483. res = AST_TEST_FAIL;
  484. goto return_cleanup;
  485. }
  486. if (ast_event_sub_append_ie_uint(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub, AST_EVENT_IE_NEWMSGS, 4)) {
  487. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  488. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  489. ast_test_status_update(test, "Failed to append UINT IE to TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  490. res = AST_TEST_FAIL;
  491. goto return_cleanup;
  492. }
  493. if (ast_event_sub_append_ie_bitflags(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub, AST_EVENT_IE_OLDMSGS, 1)) {
  494. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  495. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  496. ast_test_status_update(test, "Failed to append BITFLAGS IE to TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  497. res = AST_TEST_FAIL;
  498. goto return_cleanup;
  499. }
  500. if (ast_event_sub_append_ie_str(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub, AST_EVENT_IE_DEVICE, "FOO/bar")) {
  501. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  502. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  503. ast_test_status_update(test, "Failed to append STR IE to TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  504. res = AST_TEST_FAIL;
  505. goto return_cleanup;
  506. }
  507. if (ast_event_sub_append_ie_raw(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub, AST_EVENT_IE_MAILBOX, "800 km",
  508. strlen("800 km"))) {
  509. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  510. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  511. ast_test_status_update(test, "Failed to append RAW IE to TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  512. res = AST_TEST_FAIL;
  513. goto return_cleanup;
  514. }
  515. if (ast_event_sub_append_ie_exists(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub, AST_EVENT_IE_STATE)) {
  516. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  517. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  518. ast_test_status_update(test, "Failed to append EXISTS IE to TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  519. res = AST_TEST_FAIL;
  520. goto return_cleanup;
  521. }
  522. if (ast_event_sub_activate(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub)) {
  523. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  524. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  525. ast_test_status_update(test, "Failed to activate TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  526. res = AST_TEST_FAIL;
  527. goto return_cleanup;
  528. }
  529. /*
  530. * Exercise the API call to check for existing subscriptions.
  531. */
  532. ast_test_status_update(test, "Checking for subscribers to specific events\n");
  533. /* Check STR matching. */
  534. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  535. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  536. AST_EVENT_IE_END);
  537. if (sub_res != AST_EVENT_SUB_EXISTS) {
  538. ast_test_status_update(test, "Str FOO/bar subscription did not exist\n");
  539. res = AST_TEST_FAIL;
  540. }
  541. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  542. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "Money",
  543. AST_EVENT_IE_END);
  544. if (sub_res != AST_EVENT_SUB_NONE) {
  545. ast_test_status_update(test, "Str Money subscription should not exist! (%d)\n",
  546. sub_res);
  547. res = AST_TEST_FAIL;
  548. }
  549. /* Check RAW matching. */
  550. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  551. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  552. AST_EVENT_IE_END);
  553. if (sub_res != AST_EVENT_SUB_EXISTS) {
  554. ast_test_status_update(test, "Raw FOO/bar subscription did not exist\n");
  555. res = AST_TEST_FAIL;
  556. }
  557. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  558. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar") - 1,
  559. AST_EVENT_IE_END);
  560. if (sub_res != AST_EVENT_SUB_NONE) {
  561. ast_test_status_update(test, "Raw FOO/bar-1 subscription should not exist! (%d)\n",
  562. sub_res);
  563. res = AST_TEST_FAIL;
  564. }
  565. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  566. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "Monkeys", sizeof("Monkeys"),
  567. AST_EVENT_IE_END);
  568. if (sub_res != AST_EVENT_SUB_NONE) {
  569. ast_test_status_update(test, "Raw Monkeys subscription should not exist! (%d)\n",
  570. sub_res);
  571. res = AST_TEST_FAIL;
  572. }
  573. /* Check UINT matching. */
  574. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  575. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  576. AST_EVENT_IE_END);
  577. if (sub_res != AST_EVENT_SUB_EXISTS) {
  578. ast_test_status_update(test, "UINT=5 subscription did not exist\n");
  579. res = AST_TEST_FAIL;
  580. }
  581. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  582. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 1,
  583. AST_EVENT_IE_END);
  584. if (sub_res != AST_EVENT_SUB_NONE) {
  585. ast_test_status_update(test, "UINT=1 subscription should not exist! (%d)\n",
  586. sub_res);
  587. res = AST_TEST_FAIL;
  588. }
  589. /* Check BITFLAGS matching. */
  590. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  591. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_BITFLAGS, 2,
  592. AST_EVENT_IE_END);
  593. if (sub_res != AST_EVENT_SUB_EXISTS) {
  594. ast_test_status_update(test, "BITFLAGS=2 subscription did not exist\n");
  595. res = AST_TEST_FAIL;
  596. }
  597. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  598. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_BITFLAGS, 8,
  599. AST_EVENT_IE_END);
  600. if (sub_res != AST_EVENT_SUB_NONE) {
  601. ast_test_status_update(test, "BITFLAGS=8 subscription should not exist! (%d)\n",
  602. sub_res);
  603. res = AST_TEST_FAIL;
  604. }
  605. /* Check EXISTS matching. */
  606. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  607. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  608. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, 100,
  609. AST_EVENT_IE_END);
  610. if (sub_res != AST_EVENT_SUB_EXISTS) {
  611. ast_test_status_update(test, "EXISTS subscription did not exist\n");
  612. res = AST_TEST_FAIL;
  613. }
  614. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  615. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  616. AST_EVENT_IE_END);
  617. if (sub_res != AST_EVENT_SUB_NONE) {
  618. ast_test_status_update(test, "EXISTS subscription should not exist! (%d)\n",
  619. sub_res);
  620. res = AST_TEST_FAIL;
  621. }
  622. ast_test_status_update(test, "Special event posting test\n");
  623. /*
  624. * Event to check if event is even posted.
  625. *
  626. * Matching subscriptions:
  627. * TEST_SUBS_CUSTOM_RAW
  628. */
  629. event = ast_event_new(AST_EVENT_CUSTOM,
  630. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "Mula",
  631. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  632. AST_EVENT_IE_END);
  633. if (!event) {
  634. ast_test_status_update(test, "Failed to create event\n");
  635. res = AST_TEST_FAIL;
  636. goto return_cleanup;
  637. }
  638. if (ast_event_queue(event)) {
  639. ast_event_destroy(event);
  640. event = NULL;
  641. ast_test_status_update(test, "Failed to queue event\n");
  642. res = AST_TEST_FAIL;
  643. goto return_cleanup;
  644. }
  645. ast_test_status_update(test, "Sleeping a few seconds to allow event propagation...\n");
  646. sleep(3);
  647. /*
  648. * Subscription TEST_SUBS_CUSTOM_ANY:
  649. * - allocate normally
  650. * - subscribe to all CUSTOM events
  651. */
  652. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_ANY subscription\n");
  653. test_subs[TEST_SUBS_CUSTOM_ANY].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  654. test_subs_class_type_str(TEST_SUBS_CUSTOM_ANY), &test_subs[TEST_SUBS_CUSTOM_ANY].data,
  655. AST_EVENT_IE_END);
  656. if (!test_subs[TEST_SUBS_CUSTOM_ANY].sub) {
  657. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_ANY subscription\n");
  658. res = AST_TEST_FAIL;
  659. goto return_cleanup;
  660. }
  661. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_ANY].sub),
  662. test_subs_class_type_str(TEST_SUBS_CUSTOM_ANY))) {
  663. ast_test_status_update(test,
  664. "Unexpected subscription description on TEST_SUBS_CUSTOM_ANY subscription\n");
  665. res = AST_TEST_FAIL;
  666. goto return_cleanup;
  667. }
  668. /*
  669. * Fire off some events and track what was received in the callback
  670. */
  671. ast_test_status_update(test, "Posting test events\n");
  672. /*
  673. * Event to check STR matching.
  674. *
  675. * Matching subscriptions:
  676. * TEST_SUBS_ALL_STR
  677. * TEST_SUBS_CUSTOM_STR
  678. * TEST_SUBS_CUSTOM_ANY
  679. */
  680. event = ast_event_new(AST_EVENT_CUSTOM,
  681. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar") - 1,
  682. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  683. AST_EVENT_IE_END);
  684. if (!event) {
  685. ast_test_status_update(test, "Failed to create event\n");
  686. res = AST_TEST_FAIL;
  687. goto return_cleanup;
  688. }
  689. if (ast_event_queue(event)) {
  690. ast_event_destroy(event);
  691. event = NULL;
  692. ast_test_status_update(test, "Failed to queue event\n");
  693. res = AST_TEST_FAIL;
  694. goto return_cleanup;
  695. }
  696. /*
  697. * Event to check RAW matching.
  698. *
  699. * Matching subscriptions:
  700. * TEST_SUBS_CUSTOM_RAW
  701. * TEST_SUBS_CUSTOM_ANY
  702. */
  703. event = ast_event_new(AST_EVENT_CUSTOM,
  704. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "Misery",
  705. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  706. AST_EVENT_IE_END);
  707. if (!event) {
  708. ast_test_status_update(test, "Failed to create event\n");
  709. res = AST_TEST_FAIL;
  710. goto return_cleanup;
  711. }
  712. if (ast_event_queue(event)) {
  713. ast_event_destroy(event);
  714. event = NULL;
  715. ast_test_status_update(test, "Failed to queue event\n");
  716. res = AST_TEST_FAIL;
  717. goto return_cleanup;
  718. }
  719. /*
  720. * Event to check UINT matching.
  721. *
  722. * Matching subscriptions:
  723. * TEST_SUBS_CUSTOM_UINT
  724. * TEST_SUBS_CUSTOM_BITFLAGS
  725. * TEST_SUBS_CUSTOM_ANY
  726. */
  727. event = ast_event_new(AST_EVENT_CUSTOM,
  728. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  729. AST_EVENT_IE_END);
  730. if (!event) {
  731. ast_test_status_update(test, "Failed to create event\n");
  732. res = AST_TEST_FAIL;
  733. goto return_cleanup;
  734. }
  735. if (ast_event_queue(event)) {
  736. ast_event_destroy(event);
  737. event = NULL;
  738. ast_test_status_update(test, "Failed to queue event\n");
  739. res = AST_TEST_FAIL;
  740. goto return_cleanup;
  741. }
  742. /*
  743. * Event to check BITFLAGS matching.
  744. *
  745. * Matching subscriptions:
  746. * TEST_SUBS_CUSTOM_BITFLAGS
  747. * TEST_SUBS_CUSTOM_ANY
  748. */
  749. event = ast_event_new(AST_EVENT_CUSTOM,
  750. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  751. AST_EVENT_IE_END);
  752. if (!event) {
  753. ast_test_status_update(test, "Failed to create event\n");
  754. res = AST_TEST_FAIL;
  755. goto return_cleanup;
  756. }
  757. if (ast_event_queue(event)) {
  758. ast_event_destroy(event);
  759. event = NULL;
  760. ast_test_status_update(test, "Failed to queue event\n");
  761. res = AST_TEST_FAIL;
  762. goto return_cleanup;
  763. }
  764. /*
  765. * Event to check EXISTS matching.
  766. *
  767. * Matching subscriptions:
  768. * TEST_SUBS_CUSTOM_EXISTS
  769. * TEST_SUBS_CUSTOM_BITFLAGS
  770. * TEST_SUBS_CUSTOM_ANY
  771. */
  772. event = ast_event_new(AST_EVENT_CUSTOM,
  773. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  774. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  775. AST_EVENT_IE_END);
  776. if (!event) {
  777. ast_test_status_update(test, "Failed to create event\n");
  778. res = AST_TEST_FAIL;
  779. goto return_cleanup;
  780. }
  781. if (ast_event_queue(event)) {
  782. ast_event_destroy(event);
  783. event = NULL;
  784. ast_test_status_update(test, "Failed to queue event\n");
  785. res = AST_TEST_FAIL;
  786. goto return_cleanup;
  787. }
  788. /*
  789. * Event to get dynamic subscription to have an event.
  790. *
  791. * Matching subscriptions:
  792. * TEST_SUBS_CUSTOM_DYNAMIC
  793. * TEST_SUBS_CUSTOM_BITFLAGS
  794. * TEST_SUBS_CUSTOM_EXISTS
  795. * TEST_SUBS_ALL_STR
  796. * TEST_SUBS_CUSTOM_STR
  797. * TEST_SUBS_CUSTOM_ANY
  798. */
  799. event = ast_event_new(AST_EVENT_CUSTOM,
  800. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  801. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  802. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "800 km", strlen("800 km"),
  803. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  804. AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, 5,
  805. AST_EVENT_IE_END);
  806. if (!event) {
  807. ast_test_status_update(test, "Failed to create event\n");
  808. res = AST_TEST_FAIL;
  809. goto return_cleanup;
  810. }
  811. if (ast_event_queue(event)) {
  812. ast_event_destroy(event);
  813. event = NULL;
  814. ast_test_status_update(test, "Failed to queue event\n");
  815. res = AST_TEST_FAIL;
  816. goto return_cleanup;
  817. }
  818. event = NULL;
  819. /*
  820. * Check the results of the test.
  821. *
  822. * First of all, event distribution is asynchronous from the event producer,
  823. * so knowing when to continue from here and check results is an instance of
  824. * the halting problem. A few seconds really should be more than enough time.
  825. * If something was actually blocking event distribution that long, I would call
  826. * it a bug.
  827. *
  828. * See test_subs[] initialization for expected results.
  829. */
  830. ast_test_status_update(test, "Sleeping a few seconds to allow event propagation...\n");
  831. sleep(3);
  832. for (i = 0; i < ARRAY_LEN(test_subs); i++) {
  833. if (!test_subs[i].sub) {
  834. ast_test_status_update(test, "Missing a test subscription for %s\n",
  835. test_subs_class_type_str(i));
  836. res = AST_TEST_FAIL;
  837. }
  838. if (test_subs[i].data.count != test_subs[i].expected_count) {
  839. ast_test_status_update(test,
  840. "Unexpected callback count, got %u expected %u for %s\n",
  841. test_subs[i].data.count, test_subs[i].expected_count,
  842. test_subs_class_type_str(i));
  843. res = AST_TEST_FAIL;
  844. }
  845. }
  846. return_cleanup:
  847. for (i = 0; i < ARRAY_LEN(test_subs); i++) {
  848. if (test_subs[i].sub) {
  849. test_subs[i].sub = ast_event_unsubscribe(test_subs[i].sub);
  850. }
  851. }
  852. return res;
  853. }
  854. static int unload_module(void)
  855. {
  856. AST_TEST_UNREGISTER(event_new_test);
  857. AST_TEST_UNREGISTER(event_sub_test);
  858. return 0;
  859. }
  860. static int load_module(void)
  861. {
  862. AST_TEST_REGISTER(event_new_test);
  863. AST_TEST_REGISTER(event_sub_test);
  864. return AST_MODULE_LOAD_SUCCESS;
  865. }
  866. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_event API Tests");