test_event.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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>core</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: '%u', got '%u'\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 (check_event(event, test, type, "Custom", str, uint, bitflags)) {
  156. ast_test_status_update(test, "Dynamically generated event broken\n");
  157. res = AST_TEST_FAIL;
  158. goto return_cleanup;
  159. }
  160. event2 = ast_event_new(type,
  161. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, str,
  162. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, uint,
  163. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_BITFLAGS, bitflags,
  164. AST_EVENT_IE_END);
  165. if (!event2) {
  166. ast_test_status_update(test, "Failed to allocate ast_event object.\n");
  167. res = AST_TEST_FAIL;
  168. goto return_cleanup;
  169. }
  170. if (check_event(event2, test, type, "Custom", str, uint, bitflags)) {
  171. ast_test_status_update(test, "Statically generated event broken\n");
  172. res = AST_TEST_FAIL;
  173. goto return_cleanup;
  174. }
  175. if (ast_event_get_size(event) != ast_event_get_size(event2)) {
  176. ast_test_status_update(test, "Events expected to be identical have different size: %d != %d\n",
  177. (int) ast_event_get_size(event),
  178. (int) ast_event_get_size(event2));
  179. res = AST_TEST_FAIL;
  180. goto return_cleanup;
  181. }
  182. return_cleanup:
  183. if (event) {
  184. ast_event_destroy(event);
  185. event = NULL;
  186. }
  187. if (event2) {
  188. ast_event_destroy(event2);
  189. event2 = NULL;
  190. }
  191. return res;
  192. }
  193. struct event_sub_data {
  194. unsigned int count;
  195. };
  196. static void event_sub_cb(const struct ast_event *event, void *d)
  197. {
  198. struct event_sub_data *data = d;
  199. data->count++;
  200. }
  201. enum test_subs_class_type {
  202. TEST_SUBS_ALL_STR,
  203. TEST_SUBS_CUSTOM_STR,
  204. TEST_SUBS_CUSTOM_RAW,
  205. TEST_SUBS_CUSTOM_UINT,
  206. TEST_SUBS_CUSTOM_BITFLAGS,
  207. TEST_SUBS_CUSTOM_EXISTS,
  208. TEST_SUBS_CUSTOM_DYNAMIC,
  209. TEST_SUBS_CUSTOM_ANY,
  210. /* Must be last. */
  211. TEST_SUBS_TOTAL,
  212. };
  213. /*!
  214. * \internal
  215. * \brief Convert enum test_subs_class_type to string.
  216. *
  217. * \param val Enum value to convert to string.
  218. *
  219. * \return String equivalent of enum value.
  220. */
  221. static const char *test_subs_class_type_str(enum test_subs_class_type val)
  222. {
  223. switch (val) {
  224. case TEST_SUBS_ALL_STR:
  225. return "TEST_SUBS_ALL_STR";
  226. case TEST_SUBS_CUSTOM_STR:
  227. return "TEST_SUBS_CUSTOM_STR";
  228. case TEST_SUBS_CUSTOM_RAW:
  229. return "TEST_SUBS_CUSTOM_RAW";
  230. case TEST_SUBS_CUSTOM_UINT:
  231. return "TEST_SUBS_CUSTOM_UINT";
  232. case TEST_SUBS_CUSTOM_BITFLAGS:
  233. return "TEST_SUBS_CUSTOM_BITFLAGS";
  234. case TEST_SUBS_CUSTOM_EXISTS:
  235. return "TEST_SUBS_CUSTOM_EXISTS";
  236. case TEST_SUBS_CUSTOM_DYNAMIC:
  237. return "TEST_SUBS_CUSTOM_DYNAMIC";
  238. case TEST_SUBS_CUSTOM_ANY:
  239. return "TEST_SUBS_CUSTOM_ANY";
  240. case TEST_SUBS_TOTAL:
  241. break;
  242. }
  243. return "Unknown";
  244. }
  245. /*!
  246. * \internal
  247. * \brief Test event subscriptions
  248. *
  249. * - Query for existing Subscriptions:
  250. * - ast_event_check_subscriber()
  251. */
  252. AST_TEST_DEFINE(event_sub_test)
  253. {
  254. enum ast_test_result_state res = AST_TEST_PASS;
  255. struct ast_event *event;
  256. int i;
  257. enum ast_event_subscriber_res sub_res;
  258. struct {
  259. struct ast_event_sub *sub;
  260. struct event_sub_data data;
  261. const unsigned int expected_count;
  262. } test_subs[TEST_SUBS_TOTAL] = {
  263. [TEST_SUBS_ALL_STR] = {
  264. .expected_count = 2,
  265. },
  266. [TEST_SUBS_CUSTOM_STR] = {
  267. .expected_count = 2,
  268. },
  269. [TEST_SUBS_CUSTOM_RAW] = {
  270. .expected_count = 2,
  271. },
  272. [TEST_SUBS_CUSTOM_UINT] = {
  273. .expected_count = 1,
  274. },
  275. [TEST_SUBS_CUSTOM_BITFLAGS] = {
  276. .expected_count = 4,
  277. },
  278. [TEST_SUBS_CUSTOM_EXISTS] = {
  279. .expected_count = 2,
  280. },
  281. [TEST_SUBS_CUSTOM_DYNAMIC] = {
  282. .expected_count = 1,
  283. },
  284. [TEST_SUBS_CUSTOM_ANY] = {
  285. .expected_count = 6,
  286. },
  287. };
  288. switch (cmd) {
  289. case TEST_INIT:
  290. info->name = "ast_event_subscribe_test";
  291. info->category = "/main/event/";
  292. info->summary = "Test event subscriptions";
  293. info->description =
  294. "This test exercises the API calls that allow subscriptions "
  295. "to events.";
  296. return AST_TEST_NOT_RUN;
  297. case TEST_EXECUTE:
  298. break;
  299. }
  300. ast_test_status_update(test, "Check that NO CUSTOM subscribers exist\n");
  301. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  302. AST_EVENT_IE_END);
  303. if (sub_res != AST_EVENT_SUB_NONE) {
  304. ast_test_status_update(test, "CUSTOM subscriptions should not exist! (%u)\n",
  305. sub_res);
  306. res = AST_TEST_FAIL;
  307. }
  308. /*
  309. * Subscription TEST_SUBS_CUSTOM_STR:
  310. * - allocate normally
  311. * - subscribe to CUSTOM events with a DEVICE STR IE check
  312. */
  313. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_STR subscription\n");
  314. test_subs[TEST_SUBS_CUSTOM_STR].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  315. test_subs_class_type_str(TEST_SUBS_CUSTOM_STR), &test_subs[TEST_SUBS_CUSTOM_STR].data,
  316. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  317. AST_EVENT_IE_END);
  318. if (!test_subs[TEST_SUBS_CUSTOM_STR].sub) {
  319. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_STR subscription\n");
  320. res = AST_TEST_FAIL;
  321. goto return_cleanup;
  322. }
  323. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_STR].sub),
  324. test_subs_class_type_str(TEST_SUBS_CUSTOM_STR))) {
  325. ast_test_status_update(test,
  326. "Unexpected subscription description on TEST_SUBS_CUSTOM_STR subscription\n");
  327. res = AST_TEST_FAIL;
  328. goto return_cleanup;
  329. }
  330. ast_test_status_update(test, "Check that a CUSTOM subscriber exists\n");
  331. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  332. AST_EVENT_IE_END);
  333. if (sub_res != AST_EVENT_SUB_EXISTS) {
  334. ast_test_status_update(test, "A CUSTOM subscription should exist! (%u)\n",
  335. sub_res);
  336. res = AST_TEST_FAIL;
  337. }
  338. /*
  339. * Subscription TEST_SUBS_ALL_STR:
  340. * - allocate normally
  341. * - subscribe to ALL events with a DEVICE STR IE check
  342. */
  343. ast_test_status_update(test, "Adding TEST_SUBS_ALL_STR subscription\n");
  344. test_subs[TEST_SUBS_ALL_STR].sub = ast_event_subscribe(AST_EVENT_ALL, event_sub_cb,
  345. test_subs_class_type_str(TEST_SUBS_ALL_STR), &test_subs[TEST_SUBS_ALL_STR].data,
  346. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  347. AST_EVENT_IE_END);
  348. if (!test_subs[TEST_SUBS_ALL_STR].sub) {
  349. ast_test_status_update(test, "Failed to create TEST_SUBS_ALL_STR subscription\n");
  350. res = AST_TEST_FAIL;
  351. goto return_cleanup;
  352. }
  353. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_ALL_STR].sub),
  354. test_subs_class_type_str(TEST_SUBS_ALL_STR))) {
  355. ast_test_status_update(test,
  356. "Unexpected subscription description on TEST_SUBS_ALL_STR subscription\n");
  357. res = AST_TEST_FAIL;
  358. goto return_cleanup;
  359. }
  360. /*
  361. * Subscription TEST_SUBS_CUSTOM_RAW:
  362. * - allocate normally
  363. * - subscribe to CUSTOM events with a MAILBOX RAW IE check
  364. */
  365. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_RAW subscription\n");
  366. test_subs[TEST_SUBS_CUSTOM_RAW].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  367. test_subs_class_type_str(TEST_SUBS_CUSTOM_RAW), &test_subs[TEST_SUBS_CUSTOM_RAW].data,
  368. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  369. AST_EVENT_IE_END);
  370. if (!test_subs[TEST_SUBS_CUSTOM_RAW].sub) {
  371. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_RAW subscription\n");
  372. res = AST_TEST_FAIL;
  373. goto return_cleanup;
  374. }
  375. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_RAW].sub),
  376. test_subs_class_type_str(TEST_SUBS_CUSTOM_RAW))) {
  377. ast_test_status_update(test,
  378. "Unexpected subscription description on TEST_SUBS_CUSTOM_RAW subscription\n");
  379. res = AST_TEST_FAIL;
  380. goto return_cleanup;
  381. }
  382. /*
  383. * Subscription TEST_SUBS_CUSTOM_UINT:
  384. * - allocate normally
  385. * - subscribe to CUSTOM events with a NEWMSGS UINT IE check
  386. */
  387. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_UINT subscription\n");
  388. test_subs[TEST_SUBS_CUSTOM_UINT].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  389. test_subs_class_type_str(TEST_SUBS_CUSTOM_UINT), &test_subs[TEST_SUBS_CUSTOM_UINT].data,
  390. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  391. AST_EVENT_IE_END);
  392. if (!test_subs[TEST_SUBS_CUSTOM_UINT].sub) {
  393. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_UINT subscription\n");
  394. res = AST_TEST_FAIL;
  395. goto return_cleanup;
  396. }
  397. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_UINT].sub),
  398. test_subs_class_type_str(TEST_SUBS_CUSTOM_UINT))) {
  399. ast_test_status_update(test,
  400. "Unexpected subscription description on TEST_SUBS_CUSTOM_UINT subscription\n");
  401. res = AST_TEST_FAIL;
  402. goto return_cleanup;
  403. }
  404. /*
  405. * Subscription TEST_SUBS_CUSTOM_BITFLAGS:
  406. * - allocate normally
  407. * - subscribe to CUSTOM events with a NEWMSGS BITFLAGS IE check
  408. */
  409. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_BITFLAGS subscription\n");
  410. test_subs[TEST_SUBS_CUSTOM_BITFLAGS].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  411. test_subs_class_type_str(TEST_SUBS_CUSTOM_BITFLAGS), &test_subs[TEST_SUBS_CUSTOM_BITFLAGS].data,
  412. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_BITFLAGS, 0x06,
  413. AST_EVENT_IE_END);
  414. if (!test_subs[TEST_SUBS_CUSTOM_BITFLAGS].sub) {
  415. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_BITFLAGS subscription\n");
  416. res = AST_TEST_FAIL;
  417. goto return_cleanup;
  418. }
  419. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_BITFLAGS].sub),
  420. test_subs_class_type_str(TEST_SUBS_CUSTOM_BITFLAGS))) {
  421. ast_test_status_update(test,
  422. "Unexpected subscription description on TEST_SUBS_CUSTOM_BITFLAGS subscription\n");
  423. res = AST_TEST_FAIL;
  424. goto return_cleanup;
  425. }
  426. /*
  427. * Subscription TEST_SUBS_CUSTOM_EXISTS:
  428. * - allocate normally
  429. * - subscribe to CUSTOM events with a NEWMSGS UINT and OLDMSGS EXISTS IE check
  430. */
  431. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_EXISTS subscription\n");
  432. test_subs[TEST_SUBS_CUSTOM_EXISTS].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  433. test_subs_class_type_str(TEST_SUBS_CUSTOM_EXISTS), &test_subs[TEST_SUBS_CUSTOM_EXISTS].data,
  434. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  435. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
  436. AST_EVENT_IE_END);
  437. if (!test_subs[TEST_SUBS_CUSTOM_EXISTS].sub) {
  438. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_EXISTS subscription\n");
  439. res = AST_TEST_FAIL;
  440. goto return_cleanup;
  441. }
  442. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_EXISTS].sub),
  443. test_subs_class_type_str(TEST_SUBS_CUSTOM_EXISTS))) {
  444. ast_test_status_update(test,
  445. "Unexpected subscription description on TEST_SUBS_CUSTOM_EXISTS subscription\n");
  446. res = AST_TEST_FAIL;
  447. goto return_cleanup;
  448. }
  449. /* For the sake of exercising destruction before activation */
  450. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = ast_event_subscribe_new(AST_EVENT_CUSTOM,
  451. event_sub_cb, &test_subs[TEST_SUBS_CUSTOM_DYNAMIC].data);
  452. if (!test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub) {
  453. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  454. res = AST_TEST_FAIL;
  455. goto return_cleanup;
  456. }
  457. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  458. /*
  459. * Subscription TEST_SUBS_CUSTOM_DYNAMIC:
  460. * - allocate dynamically
  461. * - subscribe to all CUSTOM events
  462. * - add IE checks for all types
  463. */
  464. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  465. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = ast_event_subscribe_new(AST_EVENT_CUSTOM,
  466. event_sub_cb, &test_subs[TEST_SUBS_CUSTOM_DYNAMIC].data);
  467. if (!test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub) {
  468. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  469. res = AST_TEST_FAIL;
  470. goto return_cleanup;
  471. }
  472. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub),
  473. "")) {
  474. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  475. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  476. ast_test_status_update(test,
  477. "Unexpected subscription description on TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  478. res = AST_TEST_FAIL;
  479. goto return_cleanup;
  480. }
  481. if (ast_event_sub_append_ie_uint(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub, AST_EVENT_IE_NEWMSGS, 4)) {
  482. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  483. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  484. ast_test_status_update(test, "Failed to append UINT IE to TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  485. res = AST_TEST_FAIL;
  486. goto return_cleanup;
  487. }
  488. if (ast_event_sub_append_ie_bitflags(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub, AST_EVENT_IE_OLDMSGS, 1)) {
  489. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  490. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  491. ast_test_status_update(test, "Failed to append BITFLAGS IE to TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  492. res = AST_TEST_FAIL;
  493. goto return_cleanup;
  494. }
  495. if (ast_event_sub_append_ie_str(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub, AST_EVENT_IE_DEVICE, "FOO/bar")) {
  496. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  497. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  498. ast_test_status_update(test, "Failed to append STR IE to TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  499. res = AST_TEST_FAIL;
  500. goto return_cleanup;
  501. }
  502. if (ast_event_sub_append_ie_raw(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub, AST_EVENT_IE_MAILBOX, "800 km",
  503. strlen("800 km"))) {
  504. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  505. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  506. ast_test_status_update(test, "Failed to append RAW IE to TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  507. res = AST_TEST_FAIL;
  508. goto return_cleanup;
  509. }
  510. if (ast_event_sub_append_ie_exists(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub, AST_EVENT_IE_STATE)) {
  511. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  512. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  513. ast_test_status_update(test, "Failed to append EXISTS IE to TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  514. res = AST_TEST_FAIL;
  515. goto return_cleanup;
  516. }
  517. if (ast_event_sub_activate(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub)) {
  518. ast_event_sub_destroy(test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub);
  519. test_subs[TEST_SUBS_CUSTOM_DYNAMIC].sub = NULL;
  520. ast_test_status_update(test, "Failed to activate TEST_SUBS_CUSTOM_DYNAMIC subscription\n");
  521. res = AST_TEST_FAIL;
  522. goto return_cleanup;
  523. }
  524. /*
  525. * Exercise the API call to check for existing subscriptions.
  526. */
  527. ast_test_status_update(test, "Checking for subscribers to specific events\n");
  528. /* Check STR matching. */
  529. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  530. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  531. AST_EVENT_IE_END);
  532. if (sub_res != AST_EVENT_SUB_EXISTS) {
  533. ast_test_status_update(test, "Str FOO/bar subscription did not exist\n");
  534. res = AST_TEST_FAIL;
  535. }
  536. /* Make sure that the tech portion of the device string is case-insensitive */
  537. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  538. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "foo/bar",
  539. AST_EVENT_IE_END);
  540. if (sub_res != AST_EVENT_SUB_EXISTS) {
  541. ast_test_status_update(test, "Str FOO/bar subscription lacks proper case-sensitivity for device strings\n");
  542. res = AST_TEST_FAIL;
  543. }
  544. /* Make sure that the non-tech portion of the device string is case-sensitive
  545. * and fails to match appropriately */
  546. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  547. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/BAR",
  548. AST_EVENT_IE_END);
  549. if (sub_res == AST_EVENT_SUB_EXISTS) {
  550. ast_test_status_update(test, "Str FOO/bar subscription lacks proper case-sensitivity for device strings\n");
  551. res = AST_TEST_FAIL;
  552. }
  553. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  554. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "Money",
  555. AST_EVENT_IE_END);
  556. if (sub_res != AST_EVENT_SUB_NONE) {
  557. ast_test_status_update(test, "Str Money subscription should not exist! (%u)\n",
  558. sub_res);
  559. res = AST_TEST_FAIL;
  560. }
  561. /* Check RAW matching. */
  562. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  563. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  564. AST_EVENT_IE_END);
  565. if (sub_res != AST_EVENT_SUB_EXISTS) {
  566. ast_test_status_update(test, "Raw FOO/bar subscription did not exist\n");
  567. res = AST_TEST_FAIL;
  568. }
  569. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  570. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar") - 1,
  571. AST_EVENT_IE_END);
  572. if (sub_res != AST_EVENT_SUB_NONE) {
  573. ast_test_status_update(test, "Raw FOO/bar-1 subscription should not exist! (%u)\n",
  574. sub_res);
  575. res = AST_TEST_FAIL;
  576. }
  577. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  578. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "Monkeys", sizeof("Monkeys"),
  579. AST_EVENT_IE_END);
  580. if (sub_res != AST_EVENT_SUB_NONE) {
  581. ast_test_status_update(test, "Raw Monkeys subscription should not exist! (%u)\n",
  582. sub_res);
  583. res = AST_TEST_FAIL;
  584. }
  585. /* Check UINT matching. */
  586. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  587. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  588. AST_EVENT_IE_END);
  589. if (sub_res != AST_EVENT_SUB_EXISTS) {
  590. ast_test_status_update(test, "UINT=5 subscription did not exist\n");
  591. res = AST_TEST_FAIL;
  592. }
  593. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  594. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 1,
  595. AST_EVENT_IE_END);
  596. if (sub_res != AST_EVENT_SUB_NONE) {
  597. ast_test_status_update(test, "UINT=1 subscription should not exist! (%u)\n",
  598. sub_res);
  599. res = AST_TEST_FAIL;
  600. }
  601. /* Check BITFLAGS matching. */
  602. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  603. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_BITFLAGS, 2,
  604. AST_EVENT_IE_END);
  605. if (sub_res != AST_EVENT_SUB_EXISTS) {
  606. ast_test_status_update(test, "BITFLAGS=2 subscription did not exist\n");
  607. res = AST_TEST_FAIL;
  608. }
  609. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  610. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_BITFLAGS, 8,
  611. AST_EVENT_IE_END);
  612. if (sub_res != AST_EVENT_SUB_NONE) {
  613. ast_test_status_update(test, "BITFLAGS=8 subscription should not exist! (%u)\n",
  614. sub_res);
  615. res = AST_TEST_FAIL;
  616. }
  617. /* Check EXISTS matching. */
  618. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  619. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  620. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, 100,
  621. AST_EVENT_IE_END);
  622. if (sub_res != AST_EVENT_SUB_EXISTS) {
  623. ast_test_status_update(test, "EXISTS subscription did not exist\n");
  624. res = AST_TEST_FAIL;
  625. }
  626. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  627. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  628. AST_EVENT_IE_END);
  629. if (sub_res != AST_EVENT_SUB_NONE) {
  630. ast_test_status_update(test, "EXISTS subscription should not exist! (%u)\n",
  631. sub_res);
  632. res = AST_TEST_FAIL;
  633. }
  634. ast_test_status_update(test, "Special event posting test\n");
  635. /*
  636. * Event to check if event is even posted.
  637. *
  638. * Matching subscriptions:
  639. * TEST_SUBS_CUSTOM_RAW
  640. */
  641. event = ast_event_new(AST_EVENT_CUSTOM,
  642. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "Mula",
  643. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  644. AST_EVENT_IE_END);
  645. if (!event) {
  646. ast_test_status_update(test, "Failed to create event\n");
  647. res = AST_TEST_FAIL;
  648. goto return_cleanup;
  649. }
  650. if (ast_event_queue(event)) {
  651. ast_event_destroy(event);
  652. event = NULL;
  653. ast_test_status_update(test, "Failed to queue event\n");
  654. res = AST_TEST_FAIL;
  655. goto return_cleanup;
  656. }
  657. ast_test_status_update(test, "Sleeping a few seconds to allow event propagation...\n");
  658. sleep(3);
  659. /*
  660. * Subscription TEST_SUBS_CUSTOM_ANY:
  661. * - allocate normally
  662. * - subscribe to all CUSTOM events
  663. */
  664. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_ANY subscription\n");
  665. test_subs[TEST_SUBS_CUSTOM_ANY].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  666. test_subs_class_type_str(TEST_SUBS_CUSTOM_ANY), &test_subs[TEST_SUBS_CUSTOM_ANY].data,
  667. AST_EVENT_IE_END);
  668. if (!test_subs[TEST_SUBS_CUSTOM_ANY].sub) {
  669. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_ANY subscription\n");
  670. res = AST_TEST_FAIL;
  671. goto return_cleanup;
  672. }
  673. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_ANY].sub),
  674. test_subs_class_type_str(TEST_SUBS_CUSTOM_ANY))) {
  675. ast_test_status_update(test,
  676. "Unexpected subscription description on TEST_SUBS_CUSTOM_ANY subscription\n");
  677. res = AST_TEST_FAIL;
  678. goto return_cleanup;
  679. }
  680. /*
  681. * Fire off some events and track what was received in the callback
  682. */
  683. ast_test_status_update(test, "Posting test events\n");
  684. /*
  685. * Event to check STR matching.
  686. *
  687. * Matching subscriptions:
  688. * TEST_SUBS_ALL_STR
  689. * TEST_SUBS_CUSTOM_STR
  690. * TEST_SUBS_CUSTOM_ANY
  691. */
  692. event = ast_event_new(AST_EVENT_CUSTOM,
  693. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar") - 1,
  694. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  695. AST_EVENT_IE_END);
  696. if (!event) {
  697. ast_test_status_update(test, "Failed to create event\n");
  698. res = AST_TEST_FAIL;
  699. goto return_cleanup;
  700. }
  701. if (ast_event_queue(event)) {
  702. ast_event_destroy(event);
  703. event = NULL;
  704. ast_test_status_update(test, "Failed to queue event\n");
  705. res = AST_TEST_FAIL;
  706. goto return_cleanup;
  707. }
  708. /*
  709. * Event to check RAW matching.
  710. *
  711. * Matching subscriptions:
  712. * TEST_SUBS_CUSTOM_RAW
  713. * TEST_SUBS_CUSTOM_ANY
  714. */
  715. event = ast_event_new(AST_EVENT_CUSTOM,
  716. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "Misery",
  717. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  718. AST_EVENT_IE_END);
  719. if (!event) {
  720. ast_test_status_update(test, "Failed to create event\n");
  721. res = AST_TEST_FAIL;
  722. goto return_cleanup;
  723. }
  724. if (ast_event_queue(event)) {
  725. ast_event_destroy(event);
  726. event = NULL;
  727. ast_test_status_update(test, "Failed to queue event\n");
  728. res = AST_TEST_FAIL;
  729. goto return_cleanup;
  730. }
  731. /*
  732. * Event to check UINT matching.
  733. *
  734. * Matching subscriptions:
  735. * TEST_SUBS_CUSTOM_UINT
  736. * TEST_SUBS_CUSTOM_BITFLAGS
  737. * TEST_SUBS_CUSTOM_ANY
  738. */
  739. event = ast_event_new(AST_EVENT_CUSTOM,
  740. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  741. AST_EVENT_IE_END);
  742. if (!event) {
  743. ast_test_status_update(test, "Failed to create event\n");
  744. res = AST_TEST_FAIL;
  745. goto return_cleanup;
  746. }
  747. if (ast_event_queue(event)) {
  748. ast_event_destroy(event);
  749. event = NULL;
  750. ast_test_status_update(test, "Failed to queue event\n");
  751. res = AST_TEST_FAIL;
  752. goto return_cleanup;
  753. }
  754. /*
  755. * Event to check BITFLAGS matching.
  756. *
  757. * Matching subscriptions:
  758. * TEST_SUBS_CUSTOM_BITFLAGS
  759. * TEST_SUBS_CUSTOM_ANY
  760. */
  761. event = ast_event_new(AST_EVENT_CUSTOM,
  762. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  763. AST_EVENT_IE_END);
  764. if (!event) {
  765. ast_test_status_update(test, "Failed to create event\n");
  766. res = AST_TEST_FAIL;
  767. goto return_cleanup;
  768. }
  769. if (ast_event_queue(event)) {
  770. ast_event_destroy(event);
  771. event = NULL;
  772. ast_test_status_update(test, "Failed to queue event\n");
  773. res = AST_TEST_FAIL;
  774. goto return_cleanup;
  775. }
  776. /*
  777. * Event to check EXISTS matching.
  778. *
  779. * Matching subscriptions:
  780. * TEST_SUBS_CUSTOM_EXISTS
  781. * TEST_SUBS_CUSTOM_BITFLAGS
  782. * TEST_SUBS_CUSTOM_ANY
  783. */
  784. event = ast_event_new(AST_EVENT_CUSTOM,
  785. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  786. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  787. AST_EVENT_IE_END);
  788. if (!event) {
  789. ast_test_status_update(test, "Failed to create event\n");
  790. res = AST_TEST_FAIL;
  791. goto return_cleanup;
  792. }
  793. if (ast_event_queue(event)) {
  794. ast_event_destroy(event);
  795. event = NULL;
  796. ast_test_status_update(test, "Failed to queue event\n");
  797. res = AST_TEST_FAIL;
  798. goto return_cleanup;
  799. }
  800. /*
  801. * Event to get dynamic subscription to have an event.
  802. *
  803. * Matching subscriptions:
  804. * TEST_SUBS_CUSTOM_DYNAMIC
  805. * TEST_SUBS_CUSTOM_BITFLAGS
  806. * TEST_SUBS_CUSTOM_EXISTS
  807. * TEST_SUBS_ALL_STR
  808. * TEST_SUBS_CUSTOM_STR
  809. * TEST_SUBS_CUSTOM_ANY
  810. */
  811. event = ast_event_new(AST_EVENT_CUSTOM,
  812. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  813. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  814. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "800 km", strlen("800 km"),
  815. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  816. AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, 5,
  817. AST_EVENT_IE_END);
  818. if (!event) {
  819. ast_test_status_update(test, "Failed to create event\n");
  820. res = AST_TEST_FAIL;
  821. goto return_cleanup;
  822. }
  823. if (ast_event_queue(event)) {
  824. ast_event_destroy(event);
  825. event = NULL;
  826. ast_test_status_update(test, "Failed to queue event\n");
  827. res = AST_TEST_FAIL;
  828. goto return_cleanup;
  829. }
  830. event = NULL;
  831. /*
  832. * Check the results of the test.
  833. *
  834. * First of all, event distribution is asynchronous from the event producer,
  835. * so knowing when to continue from here and check results is an instance of
  836. * the halting problem. A few seconds really should be more than enough time.
  837. * If something was actually blocking event distribution that long, I would call
  838. * it a bug.
  839. *
  840. * See test_subs[] initialization for expected results.
  841. */
  842. ast_test_status_update(test, "Sleeping a few seconds to allow event propagation...\n");
  843. sleep(3);
  844. for (i = 0; i < ARRAY_LEN(test_subs); i++) {
  845. if (!test_subs[i].sub) {
  846. ast_test_status_update(test, "Missing a test subscription for %s\n",
  847. test_subs_class_type_str(i));
  848. res = AST_TEST_FAIL;
  849. }
  850. if (test_subs[i].data.count != test_subs[i].expected_count) {
  851. ast_test_status_update(test,
  852. "Unexpected callback count, got %u expected %u for %s\n",
  853. test_subs[i].data.count, test_subs[i].expected_count,
  854. test_subs_class_type_str(i));
  855. res = AST_TEST_FAIL;
  856. }
  857. }
  858. return_cleanup:
  859. for (i = 0; i < ARRAY_LEN(test_subs); i++) {
  860. if (test_subs[i].sub) {
  861. test_subs[i].sub = ast_event_unsubscribe(test_subs[i].sub);
  862. }
  863. }
  864. return res;
  865. }
  866. static int unload_module(void)
  867. {
  868. AST_TEST_UNREGISTER(event_new_test);
  869. AST_TEST_UNREGISTER(event_sub_test);
  870. return 0;
  871. }
  872. static int load_module(void)
  873. {
  874. AST_TEST_REGISTER(event_new_test);
  875. AST_TEST_REGISTER(event_sub_test);
  876. return AST_MODULE_LOAD_SUCCESS;
  877. }
  878. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_event API Tests");