test_event.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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: '%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. /* Make sure that the tech portion of the device string is case-insensitive */
  542. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  543. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "foo/bar",
  544. AST_EVENT_IE_END);
  545. if (sub_res != AST_EVENT_SUB_EXISTS) {
  546. ast_test_status_update(test, "Str FOO/bar subscription lacks proper case-sensitivity for device strings\n");
  547. res = AST_TEST_FAIL;
  548. }
  549. /* Make sure that the non-tech portion of the device string is case-sensitive
  550. * and fails to match appropriately */
  551. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  552. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/BAR",
  553. AST_EVENT_IE_END);
  554. if (sub_res == AST_EVENT_SUB_EXISTS) {
  555. ast_test_status_update(test, "Str FOO/bar subscription lacks proper case-sensitivity for device strings\n");
  556. res = AST_TEST_FAIL;
  557. }
  558. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  559. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "Money",
  560. AST_EVENT_IE_END);
  561. if (sub_res != AST_EVENT_SUB_NONE) {
  562. ast_test_status_update(test, "Str Money subscription should not exist! (%d)\n",
  563. sub_res);
  564. res = AST_TEST_FAIL;
  565. }
  566. /* Check RAW matching. */
  567. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  568. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  569. AST_EVENT_IE_END);
  570. if (sub_res != AST_EVENT_SUB_EXISTS) {
  571. ast_test_status_update(test, "Raw FOO/bar subscription did not exist\n");
  572. res = AST_TEST_FAIL;
  573. }
  574. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  575. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar") - 1,
  576. AST_EVENT_IE_END);
  577. if (sub_res != AST_EVENT_SUB_NONE) {
  578. ast_test_status_update(test, "Raw FOO/bar-1 subscription should not exist! (%d)\n",
  579. sub_res);
  580. res = AST_TEST_FAIL;
  581. }
  582. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  583. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "Monkeys", sizeof("Monkeys"),
  584. AST_EVENT_IE_END);
  585. if (sub_res != AST_EVENT_SUB_NONE) {
  586. ast_test_status_update(test, "Raw Monkeys subscription should not exist! (%d)\n",
  587. sub_res);
  588. res = AST_TEST_FAIL;
  589. }
  590. /* Check UINT matching. */
  591. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  592. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  593. AST_EVENT_IE_END);
  594. if (sub_res != AST_EVENT_SUB_EXISTS) {
  595. ast_test_status_update(test, "UINT=5 subscription did not exist\n");
  596. res = AST_TEST_FAIL;
  597. }
  598. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  599. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 1,
  600. AST_EVENT_IE_END);
  601. if (sub_res != AST_EVENT_SUB_NONE) {
  602. ast_test_status_update(test, "UINT=1 subscription should not exist! (%d)\n",
  603. sub_res);
  604. res = AST_TEST_FAIL;
  605. }
  606. /* Check BITFLAGS matching. */
  607. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  608. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_BITFLAGS, 2,
  609. AST_EVENT_IE_END);
  610. if (sub_res != AST_EVENT_SUB_EXISTS) {
  611. ast_test_status_update(test, "BITFLAGS=2 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_BITFLAGS, 8,
  616. AST_EVENT_IE_END);
  617. if (sub_res != AST_EVENT_SUB_NONE) {
  618. ast_test_status_update(test, "BITFLAGS=8 subscription should not exist! (%d)\n",
  619. sub_res);
  620. res = AST_TEST_FAIL;
  621. }
  622. /* Check EXISTS matching. */
  623. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  624. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  625. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, 100,
  626. AST_EVENT_IE_END);
  627. if (sub_res != AST_EVENT_SUB_EXISTS) {
  628. ast_test_status_update(test, "EXISTS subscription did not exist\n");
  629. res = AST_TEST_FAIL;
  630. }
  631. sub_res = ast_event_check_subscriber(AST_EVENT_CUSTOM,
  632. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  633. AST_EVENT_IE_END);
  634. if (sub_res != AST_EVENT_SUB_NONE) {
  635. ast_test_status_update(test, "EXISTS subscription should not exist! (%d)\n",
  636. sub_res);
  637. res = AST_TEST_FAIL;
  638. }
  639. ast_test_status_update(test, "Special event posting test\n");
  640. /*
  641. * Event to check if event is even posted.
  642. *
  643. * Matching subscriptions:
  644. * TEST_SUBS_CUSTOM_RAW
  645. */
  646. event = ast_event_new(AST_EVENT_CUSTOM,
  647. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "Mula",
  648. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  649. AST_EVENT_IE_END);
  650. if (!event) {
  651. ast_test_status_update(test, "Failed to create event\n");
  652. res = AST_TEST_FAIL;
  653. goto return_cleanup;
  654. }
  655. if (ast_event_queue(event)) {
  656. ast_event_destroy(event);
  657. event = NULL;
  658. ast_test_status_update(test, "Failed to queue event\n");
  659. res = AST_TEST_FAIL;
  660. goto return_cleanup;
  661. }
  662. ast_test_status_update(test, "Sleeping a few seconds to allow event propagation...\n");
  663. sleep(3);
  664. /*
  665. * Subscription TEST_SUBS_CUSTOM_ANY:
  666. * - allocate normally
  667. * - subscribe to all CUSTOM events
  668. */
  669. ast_test_status_update(test, "Adding TEST_SUBS_CUSTOM_ANY subscription\n");
  670. test_subs[TEST_SUBS_CUSTOM_ANY].sub = ast_event_subscribe(AST_EVENT_CUSTOM, event_sub_cb,
  671. test_subs_class_type_str(TEST_SUBS_CUSTOM_ANY), &test_subs[TEST_SUBS_CUSTOM_ANY].data,
  672. AST_EVENT_IE_END);
  673. if (!test_subs[TEST_SUBS_CUSTOM_ANY].sub) {
  674. ast_test_status_update(test, "Failed to create TEST_SUBS_CUSTOM_ANY subscription\n");
  675. res = AST_TEST_FAIL;
  676. goto return_cleanup;
  677. }
  678. if (strcmp(ast_event_subscriber_get_description(test_subs[TEST_SUBS_CUSTOM_ANY].sub),
  679. test_subs_class_type_str(TEST_SUBS_CUSTOM_ANY))) {
  680. ast_test_status_update(test,
  681. "Unexpected subscription description on TEST_SUBS_CUSTOM_ANY subscription\n");
  682. res = AST_TEST_FAIL;
  683. goto return_cleanup;
  684. }
  685. /*
  686. * Fire off some events and track what was received in the callback
  687. */
  688. ast_test_status_update(test, "Posting test events\n");
  689. /*
  690. * Event to check STR matching.
  691. *
  692. * Matching subscriptions:
  693. * TEST_SUBS_ALL_STR
  694. * TEST_SUBS_CUSTOM_STR
  695. * TEST_SUBS_CUSTOM_ANY
  696. */
  697. event = ast_event_new(AST_EVENT_CUSTOM,
  698. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar") - 1,
  699. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  700. AST_EVENT_IE_END);
  701. if (!event) {
  702. ast_test_status_update(test, "Failed to create event\n");
  703. res = AST_TEST_FAIL;
  704. goto return_cleanup;
  705. }
  706. if (ast_event_queue(event)) {
  707. ast_event_destroy(event);
  708. event = NULL;
  709. ast_test_status_update(test, "Failed to queue event\n");
  710. res = AST_TEST_FAIL;
  711. goto return_cleanup;
  712. }
  713. /*
  714. * Event to check RAW matching.
  715. *
  716. * Matching subscriptions:
  717. * TEST_SUBS_CUSTOM_RAW
  718. * TEST_SUBS_CUSTOM_ANY
  719. */
  720. event = ast_event_new(AST_EVENT_CUSTOM,
  721. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "Misery",
  722. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "FOO/bar", sizeof("FOO/bar"),
  723. AST_EVENT_IE_END);
  724. if (!event) {
  725. ast_test_status_update(test, "Failed to create event\n");
  726. res = AST_TEST_FAIL;
  727. goto return_cleanup;
  728. }
  729. if (ast_event_queue(event)) {
  730. ast_event_destroy(event);
  731. event = NULL;
  732. ast_test_status_update(test, "Failed to queue event\n");
  733. res = AST_TEST_FAIL;
  734. goto return_cleanup;
  735. }
  736. /*
  737. * Event to check UINT matching.
  738. *
  739. * Matching subscriptions:
  740. * TEST_SUBS_CUSTOM_UINT
  741. * TEST_SUBS_CUSTOM_BITFLAGS
  742. * TEST_SUBS_CUSTOM_ANY
  743. */
  744. event = ast_event_new(AST_EVENT_CUSTOM,
  745. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  746. AST_EVENT_IE_END);
  747. if (!event) {
  748. ast_test_status_update(test, "Failed to create event\n");
  749. res = AST_TEST_FAIL;
  750. goto return_cleanup;
  751. }
  752. if (ast_event_queue(event)) {
  753. ast_event_destroy(event);
  754. event = NULL;
  755. ast_test_status_update(test, "Failed to queue event\n");
  756. res = AST_TEST_FAIL;
  757. goto return_cleanup;
  758. }
  759. /*
  760. * Event to check BITFLAGS matching.
  761. *
  762. * Matching subscriptions:
  763. * TEST_SUBS_CUSTOM_BITFLAGS
  764. * TEST_SUBS_CUSTOM_ANY
  765. */
  766. event = ast_event_new(AST_EVENT_CUSTOM,
  767. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  768. AST_EVENT_IE_END);
  769. if (!event) {
  770. ast_test_status_update(test, "Failed to create event\n");
  771. res = AST_TEST_FAIL;
  772. goto return_cleanup;
  773. }
  774. if (ast_event_queue(event)) {
  775. ast_event_destroy(event);
  776. event = NULL;
  777. ast_test_status_update(test, "Failed to queue event\n");
  778. res = AST_TEST_FAIL;
  779. goto return_cleanup;
  780. }
  781. /*
  782. * Event to check EXISTS matching.
  783. *
  784. * Matching subscriptions:
  785. * TEST_SUBS_CUSTOM_EXISTS
  786. * TEST_SUBS_CUSTOM_BITFLAGS
  787. * TEST_SUBS_CUSTOM_ANY
  788. */
  789. event = ast_event_new(AST_EVENT_CUSTOM,
  790. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  791. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  792. AST_EVENT_IE_END);
  793. if (!event) {
  794. ast_test_status_update(test, "Failed to create event\n");
  795. res = AST_TEST_FAIL;
  796. goto return_cleanup;
  797. }
  798. if (ast_event_queue(event)) {
  799. ast_event_destroy(event);
  800. event = NULL;
  801. ast_test_status_update(test, "Failed to queue event\n");
  802. res = AST_TEST_FAIL;
  803. goto return_cleanup;
  804. }
  805. /*
  806. * Event to get dynamic subscription to have an event.
  807. *
  808. * Matching subscriptions:
  809. * TEST_SUBS_CUSTOM_DYNAMIC
  810. * TEST_SUBS_CUSTOM_BITFLAGS
  811. * TEST_SUBS_CUSTOM_EXISTS
  812. * TEST_SUBS_ALL_STR
  813. * TEST_SUBS_CUSTOM_STR
  814. * TEST_SUBS_CUSTOM_ANY
  815. */
  816. event = ast_event_new(AST_EVENT_CUSTOM,
  817. AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, 4,
  818. AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, 5,
  819. AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_RAW, "800 km", strlen("800 km"),
  820. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "FOO/bar",
  821. AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, 5,
  822. AST_EVENT_IE_END);
  823. if (!event) {
  824. ast_test_status_update(test, "Failed to create event\n");
  825. res = AST_TEST_FAIL;
  826. goto return_cleanup;
  827. }
  828. if (ast_event_queue(event)) {
  829. ast_event_destroy(event);
  830. event = NULL;
  831. ast_test_status_update(test, "Failed to queue event\n");
  832. res = AST_TEST_FAIL;
  833. goto return_cleanup;
  834. }
  835. event = NULL;
  836. /*
  837. * Check the results of the test.
  838. *
  839. * First of all, event distribution is asynchronous from the event producer,
  840. * so knowing when to continue from here and check results is an instance of
  841. * the halting problem. A few seconds really should be more than enough time.
  842. * If something was actually blocking event distribution that long, I would call
  843. * it a bug.
  844. *
  845. * See test_subs[] initialization for expected results.
  846. */
  847. ast_test_status_update(test, "Sleeping a few seconds to allow event propagation...\n");
  848. sleep(3);
  849. for (i = 0; i < ARRAY_LEN(test_subs); i++) {
  850. if (!test_subs[i].sub) {
  851. ast_test_status_update(test, "Missing a test subscription for %s\n",
  852. test_subs_class_type_str(i));
  853. res = AST_TEST_FAIL;
  854. }
  855. if (test_subs[i].data.count != test_subs[i].expected_count) {
  856. ast_test_status_update(test,
  857. "Unexpected callback count, got %u expected %u for %s\n",
  858. test_subs[i].data.count, test_subs[i].expected_count,
  859. test_subs_class_type_str(i));
  860. res = AST_TEST_FAIL;
  861. }
  862. }
  863. return_cleanup:
  864. for (i = 0; i < ARRAY_LEN(test_subs); i++) {
  865. if (test_subs[i].sub) {
  866. test_subs[i].sub = ast_event_unsubscribe(test_subs[i].sub);
  867. }
  868. }
  869. return res;
  870. }
  871. static int unload_module(void)
  872. {
  873. AST_TEST_UNREGISTER(event_new_test);
  874. AST_TEST_UNREGISTER(event_sub_test);
  875. return 0;
  876. }
  877. static int load_module(void)
  878. {
  879. AST_TEST_REGISTER(event_new_test);
  880. AST_TEST_REGISTER(event_sub_test);
  881. return AST_MODULE_LOAD_SUCCESS;
  882. }
  883. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_event API Tests");