test_message.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Matt Jordan <mjordan@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. /*! \file
  19. *
  20. * \brief Test module for out-of-call text message module
  21. *
  22. * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
  23. *
  24. * \ingroup tests
  25. */
  26. /*** MODULEINFO
  27. <depend>TEST_FRAMEWORK</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include <regex.h>
  33. #include "asterisk/module.h"
  34. #include "asterisk/test.h"
  35. #include "asterisk/message.h"
  36. #include "asterisk/pbx.h"
  37. #include "asterisk/manager.h"
  38. #include "asterisk/vector.h"
  39. #define TEST_CATEGORY "/main/message/"
  40. #define TEST_CONTEXT "__TEST_MESSAGE_CONTEXT__"
  41. #define TEST_EXTENSION "test_message_extension"
  42. /*! \brief The number of user events we should get in a dialplan test */
  43. #define DEFAULT_EXPECTED_EVENTS 4
  44. /*! \brief The current number of received user events */
  45. static int received_user_events;
  46. /*! \brief The number of user events we expect for this test */
  47. static int expected_user_events;
  48. /*! \brief Predicate for the \ref test_message_handler receiving a message */
  49. static int handler_received_message;
  50. /*! \brief Condition wait variable for all dialplan user events being received */
  51. static ast_cond_t user_event_cond;
  52. /*! \brief Mutex for \c user_event_cond */
  53. AST_MUTEX_DEFINE_STATIC(user_event_lock);
  54. /*! \brief Condition wait variable for \ref test_msg_handler receiving message */
  55. static ast_cond_t handler_cond;
  56. /*! \brief Mutex for \c handler_cond */
  57. AST_MUTEX_DEFINE_STATIC(handler_lock);
  58. /*! \brief The expected user event fields */
  59. AST_VECTOR(var_vector, struct ast_variable *) expected_user_event_fields;
  60. /*! \brief If a user event fails, the bad headers that didn't match */
  61. AST_VECTOR(, struct ast_variable *) bad_headers;
  62. static int test_msg_send(const struct ast_msg *msg, const char *to, const char *from);
  63. static struct ast_msg_tech test_msg_tech = {
  64. .name = "testmsg",
  65. .msg_send = test_msg_send,
  66. };
  67. static int test_msg_handle_msg_cb(struct ast_msg *msg);
  68. static int test_msg_has_destination_cb(const struct ast_msg *msg);
  69. /*! \brief Our test message handler */
  70. static struct ast_msg_handler test_msg_handler = {
  71. .name = "testmsg",
  72. .handle_msg = test_msg_handle_msg_cb,
  73. .has_destination = test_msg_has_destination_cb,
  74. };
  75. static int user_event_hook_cb(int category, const char *event, char *body);
  76. /*! \brief AMI event hook that verifies whether or not we've gotten our user events */
  77. static struct manager_custom_hook user_event_hook = {
  78. .file = AST_MODULE,
  79. .helper = user_event_hook_cb,
  80. };
  81. /*!
  82. * \brief Verifies a user event header/value pair
  83. *
  84. * \param user_event which user event to check
  85. * \param header The header to verify
  86. * \param value The value read from the event
  87. *
  88. * \retval -1 on error or evaluation failure
  89. * \retval 0 if match not needed or success
  90. */
  91. static int verify_user_event_fields(int user_event, const char *header, const char *value)
  92. {
  93. struct ast_variable *current;
  94. struct ast_variable *expected;
  95. regex_t regexbuf;
  96. int error;
  97. if (user_event >= AST_VECTOR_SIZE(&expected_user_event_fields)) {
  98. return -1;
  99. }
  100. expected = AST_VECTOR_GET(&expected_user_event_fields, user_event);
  101. if (!expected) {
  102. return -1;
  103. }
  104. for (current = expected; current; current = current->next) {
  105. struct ast_variable *bad_header;
  106. if (strcmp(current->name, header)) {
  107. continue;
  108. }
  109. error = regcomp(&regexbuf, current->value, REG_EXTENDED | REG_NOSUB);
  110. if (error) {
  111. char error_buf[128];
  112. regerror(error, &regexbuf, error_buf, sizeof(error_buf));
  113. ast_log(LOG_ERROR, "Failed to compile regex '%s' for header check '%s': %s\n",
  114. current->value, current->name, error_buf);
  115. return -1;
  116. }
  117. if (!regexec(&regexbuf, value, 0, NULL, 0)) {
  118. regfree(&regexbuf);
  119. return 0;
  120. }
  121. bad_header = ast_variable_new(header, value, __FILE__);
  122. if (bad_header) {
  123. struct ast_variable *bad_headers_head = NULL;
  124. if (user_event < AST_VECTOR_SIZE(&bad_headers)) {
  125. bad_headers_head = AST_VECTOR_GET(&bad_headers, user_event);
  126. }
  127. ast_variable_list_append(&bad_headers_head, bad_header);
  128. AST_VECTOR_REPLACE(&bad_headers, user_event, bad_headers_head);
  129. }
  130. regfree(&regexbuf);
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. static int message_received;
  136. static int test_msg_send(const struct ast_msg *msg, const char *to, const char *from)
  137. {
  138. message_received = 1;
  139. return 0;
  140. }
  141. static int test_msg_handle_msg_cb(struct ast_msg *msg)
  142. {
  143. ast_mutex_lock(&handler_lock);
  144. handler_received_message = 1;
  145. ast_cond_signal(&handler_cond);
  146. ast_mutex_unlock(&handler_lock);
  147. return 0;
  148. }
  149. static int test_msg_has_destination_cb(const struct ast_msg *msg)
  150. {
  151. /* We only care about one destination: foo! */
  152. if (ast_strlen_zero(ast_msg_get_to(msg))) {
  153. return 0;
  154. }
  155. return (!strcmp(ast_msg_get_to(msg), "foo") ? 1 : 0);
  156. }
  157. static int user_event_hook_cb(int category, const char *event, char *body)
  158. {
  159. char *parse;
  160. char *kvp;
  161. if (strcmp(event, "UserEvent")) {
  162. return -1;
  163. }
  164. parse = ast_strdupa(body);
  165. while ((kvp = strsep(&parse, "\r\n"))) {
  166. char *key, *value;
  167. kvp = ast_trim_blanks(kvp);
  168. if (ast_strlen_zero(kvp)) {
  169. continue;
  170. }
  171. key = strsep(&kvp, ":");
  172. value = ast_skip_blanks(kvp);
  173. verify_user_event_fields(received_user_events, key, value);
  174. }
  175. received_user_events++;
  176. ast_mutex_lock(&user_event_lock);
  177. if (received_user_events == expected_user_events) {
  178. ast_cond_signal(&user_event_cond);
  179. }
  180. ast_mutex_unlock(&user_event_lock);
  181. return 0;
  182. }
  183. /*! \brief Wait for the \ref test_msg_handler to receive the message */
  184. static int handler_wait_for_message(struct ast_test *test)
  185. {
  186. int error = 0;
  187. struct timeval wait = ast_tvadd(ast_tvnow(), ast_tv(5 /* seconds */, 0));
  188. struct timespec wait_time = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000 };
  189. ast_mutex_lock(&handler_lock);
  190. while (!handler_received_message) {
  191. error = ast_cond_timedwait(&handler_cond, &handler_lock, &wait_time);
  192. if (error == ETIMEDOUT) {
  193. ast_test_status_update(test, "Test timed out while waiting for handler to get message\n");
  194. ast_test_set_result(test, AST_TEST_FAIL);
  195. break;
  196. }
  197. }
  198. ast_mutex_unlock(&handler_lock);
  199. return (error != ETIMEDOUT);
  200. }
  201. /*! \brief Wait for the expected number of user events to be received */
  202. static int user_event_wait_for_events(struct ast_test *test, int expected_events)
  203. {
  204. int error;
  205. struct timeval wait = ast_tvadd(ast_tvnow(), ast_tv(5 /* seconds */, 0));
  206. struct timespec wait_time = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000 };
  207. expected_user_events = expected_events;
  208. ast_mutex_lock(&user_event_lock);
  209. while (received_user_events != expected_user_events) {
  210. error = ast_cond_timedwait(&user_event_cond, &user_event_lock, &wait_time);
  211. if (error == ETIMEDOUT) {
  212. ast_test_status_update(test, "Test timed out while waiting for %d expected user events\n", expected_events);
  213. ast_test_set_result(test, AST_TEST_FAIL);
  214. break;
  215. }
  216. }
  217. ast_mutex_unlock(&user_event_lock);
  218. ast_test_status_update(test, "Received %d of %d user events\n", received_user_events, expected_events);
  219. return !(received_user_events == expected_events);
  220. }
  221. static int verify_bad_headers(struct ast_test *test)
  222. {
  223. int res = 0;
  224. int i;
  225. for (i = 0; i < AST_VECTOR_SIZE(&bad_headers); i++) {
  226. struct ast_variable *headers;
  227. struct ast_variable *current;
  228. headers = AST_VECTOR_GET(&bad_headers, i);
  229. if (!headers) {
  230. continue;
  231. }
  232. res = -1;
  233. for (current = headers; current; current = current->next) {
  234. ast_test_status_update(test, "Expected UserEvent %d: Failed to match %s: %s\n",
  235. i, current->name, current->value);
  236. ast_test_set_result(test, AST_TEST_FAIL);
  237. }
  238. }
  239. return res;
  240. }
  241. AST_TEST_DEFINE(test_message_msg_tech_registration)
  242. {
  243. int reg_result;
  244. switch (cmd) {
  245. case TEST_INIT:
  246. info->name = __func__;
  247. info->category = TEST_CATEGORY;
  248. info->summary = "Test register/unregister of a message tech";
  249. info->description =
  250. "Test that:\n"
  251. "\tA message technology can be registered once only\n"
  252. "\tA registered message technology can be unregistered once only";
  253. return AST_TEST_NOT_RUN;
  254. case TEST_EXECUTE:
  255. break;
  256. }
  257. reg_result = ast_msg_tech_register(&test_msg_tech);
  258. ast_test_validate(test, reg_result == 0);
  259. reg_result = ast_msg_tech_register(&test_msg_tech);
  260. ast_test_validate(test, reg_result == -1);
  261. reg_result = ast_msg_tech_unregister(&test_msg_tech);
  262. ast_test_validate(test, reg_result == 0);
  263. reg_result = ast_msg_tech_unregister(&test_msg_tech);
  264. ast_test_validate(test, reg_result == -1);
  265. return AST_TEST_PASS;
  266. }
  267. AST_TEST_DEFINE(test_message_msg_handler_registration)
  268. {
  269. int reg_result;
  270. switch (cmd) {
  271. case TEST_INIT:
  272. info->name = __func__;
  273. info->category = TEST_CATEGORY;
  274. info->summary = "Test register/unregister of a message handler";
  275. info->description =
  276. "Test that:\n"
  277. "\tA message handler can be registered once only\n"
  278. "\tA registered message handler can be unregistered once only";
  279. return AST_TEST_NOT_RUN;
  280. case TEST_EXECUTE:
  281. break;
  282. }
  283. reg_result = ast_msg_handler_register(&test_msg_handler);
  284. ast_test_validate(test, reg_result == 0);
  285. reg_result = ast_msg_handler_register(&test_msg_handler);
  286. ast_test_validate(test, reg_result == -1);
  287. reg_result = ast_msg_handler_unregister(&test_msg_handler);
  288. ast_test_validate(test, reg_result == 0);
  289. reg_result = ast_msg_handler_unregister(&test_msg_handler);
  290. ast_test_validate(test, reg_result == -1);
  291. return AST_TEST_PASS;
  292. }
  293. static void ast_msg_safe_destroy(void *obj)
  294. {
  295. struct ast_msg *msg = obj;
  296. if (msg) {
  297. ast_msg_destroy(msg);
  298. }
  299. }
  300. AST_TEST_DEFINE(test_message_manipulation)
  301. {
  302. RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
  303. RAII_VAR(struct ast_msg_var_iterator *, it_vars, NULL, ast_msg_var_iterator_destroy);
  304. int result;
  305. const char *actual;
  306. const char *out_name;
  307. const char *out_value;
  308. switch (cmd) {
  309. case TEST_INIT:
  310. info->name = __func__;
  311. info->category = TEST_CATEGORY;
  312. info->summary = "Test manipulating properties of a message";
  313. info->description =
  314. "This test covers the following:\n"
  315. "\tSetting/getting the body\n"
  316. "\tSetting/getting inbound/outbound variables\n"
  317. "\tIterating over variables";
  318. return AST_TEST_NOT_RUN;
  319. case TEST_EXECUTE:
  320. break;
  321. }
  322. msg = ast_msg_alloc();
  323. ast_test_validate(test, msg != NULL);
  324. /* Test setting/getting to */
  325. result = ast_msg_set_to(msg, "testmsg:%s", "foo");
  326. ast_test_validate(test, result == 0);
  327. actual = ast_msg_get_to(msg);
  328. ast_test_validate(test, !strcmp(actual, "testmsg:foo"));
  329. /* Test setting/getting from */
  330. result = ast_msg_set_from(msg, "testmsg:%s", "bar");
  331. ast_test_validate(test, result == 0);
  332. actual = ast_msg_get_from(msg);
  333. ast_test_validate(test, !strcmp(actual, "testmsg:bar"));
  334. /* Test setting/getting body */
  335. result = ast_msg_set_body(msg, "BodyTest: %s", "foo");
  336. ast_test_validate(test, result == 0);
  337. actual = ast_msg_get_body(msg);
  338. ast_test_validate(test, !strcmp(actual, "BodyTest: foo"));
  339. /* Test setting/getting technology */
  340. result = ast_msg_set_tech(msg, "%s", "my_tech");
  341. ast_test_validate(test, result == 0);
  342. actual = ast_msg_get_tech(msg);
  343. ast_test_validate(test, !strcmp(actual, "my_tech"));
  344. /* Test setting/getting endpoint */
  345. result = ast_msg_set_endpoint(msg, "%s", "terminus");
  346. ast_test_validate(test, result == 0);
  347. actual = ast_msg_get_endpoint(msg);
  348. ast_test_validate(test, !strcmp(actual, "terminus"));
  349. /* Test setting/getting non-outbound variable */
  350. result = ast_msg_set_var(msg, "foo", "bar");
  351. ast_test_validate(test, result == 0);
  352. actual = ast_msg_get_var(msg, "foo");
  353. ast_test_validate(test, !strcmp(actual, "bar"));
  354. /* Test updating existing variable */
  355. result = ast_msg_set_var(msg, "foo", "new_bar");
  356. ast_test_validate(test, result == 0);
  357. actual = ast_msg_get_var(msg, "foo");
  358. ast_test_validate(test, !strcmp(actual, "new_bar"));
  359. /* Verify a non-outbound variable is not iterable */
  360. it_vars = ast_msg_var_iterator_init(msg);
  361. ast_test_validate(test, it_vars != NULL);
  362. ast_test_validate(test, ast_msg_var_iterator_next(msg, it_vars, &out_name, &out_value) == 0);
  363. ast_msg_var_iterator_destroy(it_vars);
  364. /* Test updating an existing variable as an outbound variable */
  365. result = ast_msg_set_var_outbound(msg, "foo", "outbound_bar");
  366. ast_test_validate(test, result == 0);
  367. it_vars = ast_msg_var_iterator_init(msg);
  368. ast_test_validate(test, it_vars != NULL);
  369. result = ast_msg_var_iterator_next(msg, it_vars, &out_name, &out_value);
  370. ast_test_validate(test, result == 1);
  371. ast_test_validate(test, !strcmp(out_name, "foo"));
  372. ast_test_validate(test, !strcmp(out_value, "outbound_bar"));
  373. ast_msg_var_unref_current(it_vars);
  374. result = ast_msg_var_iterator_next(msg, it_vars, &out_name, &out_value);
  375. ast_test_validate(test, result == 0);
  376. return AST_TEST_PASS;
  377. }
  378. AST_TEST_DEFINE(test_message_queue_dialplan_nominal)
  379. {
  380. RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
  381. struct ast_variable *expected;
  382. struct ast_variable *expected_response = NULL;
  383. switch (cmd) {
  384. case TEST_INIT:
  385. info->name = __func__;
  386. info->category = TEST_CATEGORY;
  387. info->summary = "Test enqueueing messages to the dialplan";
  388. info->description =
  389. "Test that a message enqueued for the dialplan is\n"
  390. "passed to that particular extension";
  391. return AST_TEST_NOT_RUN;
  392. case TEST_EXECUTE:
  393. break;
  394. }
  395. msg = ast_msg_alloc();
  396. ast_test_validate(test, msg != NULL);
  397. expected = ast_variable_new("Verify","^To$", __FILE__);
  398. ast_variable_list_append(&expected_response, expected);
  399. expected = ast_variable_new("Value","^foo$", __FILE__);
  400. ast_variable_list_append(&expected_response, expected);
  401. AST_VECTOR_REPLACE(&expected_user_event_fields, 0, expected_response);
  402. expected_response = NULL;
  403. expected = ast_variable_new("Verify", "^From$", __FILE__);
  404. ast_variable_list_append(&expected_response, expected);
  405. expected = ast_variable_new("Value","^bar$", __FILE__);
  406. ast_variable_list_append(&expected_response, expected);
  407. AST_VECTOR_REPLACE(&expected_user_event_fields, 1, expected_response);
  408. expected_response = NULL;
  409. expected = ast_variable_new("Verify", "^Body$", __FILE__);
  410. ast_variable_list_append(&expected_response, expected);
  411. expected = ast_variable_new("Value", "^a body$", __FILE__);
  412. ast_variable_list_append(&expected_response, expected);
  413. AST_VECTOR_REPLACE(&expected_user_event_fields, 2, expected_response);
  414. expected_response = NULL;
  415. expected = ast_variable_new("Verify", "^Custom$", __FILE__);
  416. ast_variable_list_append(&expected_response, expected);
  417. expected = ast_variable_new("Value", "^field$", __FILE__);
  418. ast_variable_list_append(&expected_response, expected);
  419. AST_VECTOR_REPLACE(&expected_user_event_fields, 3, expected_response);
  420. ast_msg_set_to(msg, "foo");
  421. ast_msg_set_from(msg, "bar");
  422. ast_msg_set_body(msg, "a body");
  423. ast_msg_set_var_outbound(msg, "custom_data", "field");
  424. ast_msg_set_context(msg, TEST_CONTEXT);
  425. ast_msg_set_exten(msg, TEST_EXTENSION);
  426. ast_msg_queue(msg);
  427. msg = NULL;
  428. if (user_event_wait_for_events(test, DEFAULT_EXPECTED_EVENTS)) {
  429. ast_test_status_update(test, "Failed to received %d expected user events\n", DEFAULT_EXPECTED_EVENTS);
  430. return AST_TEST_FAIL;
  431. }
  432. if (verify_bad_headers(test)) {
  433. return AST_TEST_FAIL;
  434. }
  435. return AST_TEST_PASS;
  436. }
  437. AST_TEST_DEFINE(test_message_queue_handler_nominal)
  438. {
  439. RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
  440. int result;
  441. switch (cmd) {
  442. case TEST_INIT:
  443. info->name = __func__;
  444. info->category = TEST_CATEGORY;
  445. info->summary = "Test enqueueing messages to a handler";
  446. info->description =
  447. "Test that a message enqueued can be handled by a\n"
  448. "non-dialplan handler";
  449. return AST_TEST_NOT_RUN;
  450. case TEST_EXECUTE:
  451. break;
  452. }
  453. msg = ast_msg_alloc();
  454. ast_test_validate(test, msg != NULL);
  455. result = ast_msg_handler_register(&test_msg_handler);
  456. ast_test_validate(test, result == 0);
  457. ast_msg_set_to(msg, "foo");
  458. ast_msg_set_from(msg, "bar");
  459. ast_msg_set_body(msg, "a body");
  460. ast_msg_queue(msg);
  461. msg = NULL;
  462. /* This will automatically fail the test if we don't get the message */
  463. handler_wait_for_message(test);
  464. result = ast_msg_handler_unregister(&test_msg_handler);
  465. ast_test_validate(test, result == 0);
  466. return AST_TEST_PASS;
  467. }
  468. AST_TEST_DEFINE(test_message_queue_both_nominal)
  469. {
  470. RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
  471. struct ast_variable *expected;
  472. struct ast_variable *expected_response = NULL;
  473. int result;
  474. switch (cmd) {
  475. case TEST_INIT:
  476. info->name = __func__;
  477. info->category = TEST_CATEGORY;
  478. info->summary = "Test enqueueing messages to a dialplan and custom handler";
  479. info->description =
  480. "Test that a message enqueued is passed to all\n"
  481. "handlers that can process it, dialplan as well as\n"
  482. "a custom handler";
  483. return AST_TEST_NOT_RUN;
  484. case TEST_EXECUTE:
  485. break;
  486. }
  487. msg = ast_msg_alloc();
  488. ast_test_validate(test, msg != NULL);
  489. result = ast_msg_handler_register(&test_msg_handler);
  490. ast_test_validate(test, result == 0);
  491. expected = ast_variable_new("Verify","^To$", __FILE__);
  492. ast_variable_list_append(&expected_response, expected);
  493. expected = ast_variable_new("Value","^foo$", __FILE__);
  494. ast_variable_list_append(&expected_response, expected);
  495. AST_VECTOR_REPLACE(&expected_user_event_fields, 0, expected_response);
  496. expected_response = NULL;
  497. expected = ast_variable_new("Verify", "^From$", __FILE__);
  498. ast_variable_list_append(&expected_response, expected);
  499. expected = ast_variable_new("Value","^bar$", __FILE__);
  500. ast_variable_list_append(&expected_response, expected);
  501. AST_VECTOR_REPLACE(&expected_user_event_fields, 1, expected_response);
  502. expected_response = NULL;
  503. expected = ast_variable_new("Verify", "^Body$", __FILE__);
  504. ast_variable_list_append(&expected_response, expected);
  505. expected = ast_variable_new("Value", "^a body$", __FILE__);
  506. ast_variable_list_append(&expected_response, expected);
  507. AST_VECTOR_REPLACE(&expected_user_event_fields, 2, expected_response);
  508. ast_msg_set_to(msg, "foo");
  509. ast_msg_set_from(msg, "bar");
  510. ast_msg_set_body(msg, "a body");
  511. ast_msg_set_context(msg, TEST_CONTEXT);
  512. ast_msg_set_exten(msg, TEST_EXTENSION);
  513. ast_msg_queue(msg);
  514. msg = NULL;
  515. if (user_event_wait_for_events(test, DEFAULT_EXPECTED_EVENTS)) {
  516. ast_test_status_update(test, "Failed to received %d expected user events\n", DEFAULT_EXPECTED_EVENTS);
  517. ast_test_set_result(test, AST_TEST_FAIL);
  518. }
  519. /* This will automatically fail the test if we don't get the message */
  520. handler_wait_for_message(test);
  521. result = ast_msg_handler_unregister(&test_msg_handler);
  522. ast_test_validate(test, result == 0);
  523. if (verify_bad_headers(test)) {
  524. return AST_TEST_FAIL;
  525. }
  526. return AST_TEST_PASS;
  527. }
  528. AST_TEST_DEFINE(test_message_has_destination_dialplan)
  529. {
  530. RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
  531. switch (cmd) {
  532. case TEST_INIT:
  533. info->name = __func__;
  534. info->category = TEST_CATEGORY;
  535. info->summary = "Test checking for a dialplan destination";
  536. info->description =
  537. "Test that a message's destination is verified via the\n"
  538. "dialplan";
  539. return AST_TEST_NOT_RUN;
  540. case TEST_EXECUTE:
  541. break;
  542. }
  543. msg = ast_msg_alloc();
  544. ast_test_validate(test, msg != NULL);
  545. ast_msg_set_context(msg, TEST_CONTEXT);
  546. ast_msg_set_exten(msg, TEST_EXTENSION);
  547. ast_test_validate(test, ast_msg_has_destination(msg) == 1);
  548. ast_msg_set_context(msg, "__I_SHOULD_NOT_EXIST_PLZ__");
  549. ast_test_validate(test, ast_msg_has_destination(msg) == 0);
  550. ast_msg_set_context(msg, TEST_CONTEXT);
  551. ast_msg_set_exten(msg, "__I_SHOULD_NOT_EXIST_PLZ__");
  552. ast_test_validate(test, ast_msg_has_destination(msg) == 0);
  553. ast_msg_set_exten(msg, NULL);
  554. ast_test_validate(test, ast_msg_has_destination(msg) == 0);
  555. ast_msg_set_context(msg, NULL);
  556. ast_msg_set_exten(msg, TEST_EXTENSION);
  557. ast_test_validate(test, ast_msg_has_destination(msg) == 0);
  558. return AST_TEST_PASS;
  559. }
  560. AST_TEST_DEFINE(test_message_has_destination_handler)
  561. {
  562. RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
  563. int result;
  564. switch (cmd) {
  565. case TEST_INIT:
  566. info->name = __func__;
  567. info->category = TEST_CATEGORY;
  568. info->summary = "Test checking for a handler destination";
  569. info->description =
  570. "Test that a message's destination is verified via a\n"
  571. "handler";
  572. return AST_TEST_NOT_RUN;
  573. case TEST_EXECUTE:
  574. break;
  575. }
  576. result = ast_msg_handler_register(&test_msg_handler);
  577. ast_test_validate(test, result == 0);
  578. msg = ast_msg_alloc();
  579. ast_test_validate(test, msg != NULL);
  580. ast_msg_set_to(msg, "foo");
  581. ast_msg_set_context(msg, TEST_CONTEXT);
  582. ast_msg_set_exten(msg, NULL);
  583. ast_test_validate(test, ast_msg_has_destination(msg) == 1);
  584. ast_msg_set_context(msg, NULL);
  585. ast_test_validate(test, ast_msg_has_destination(msg) == 1);
  586. ast_msg_set_to(msg, "__I_SHOULD_NOT_EXIST_PLZ__");
  587. ast_test_validate(test, ast_msg_has_destination(msg) == 0);
  588. result = ast_msg_handler_unregister(&test_msg_handler);
  589. ast_test_validate(test, result == 0);
  590. return AST_TEST_PASS;
  591. }
  592. AST_TEST_DEFINE(test_message_msg_send)
  593. {
  594. RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
  595. switch (cmd) {
  596. case TEST_INIT:
  597. info->name = __func__;
  598. info->category = TEST_CATEGORY;
  599. info->summary = "Test message routing";
  600. info->description =
  601. "Test that a message can be routed if it has\n"
  602. "a valid handler";
  603. return AST_TEST_NOT_RUN;
  604. case TEST_EXECUTE:
  605. break;
  606. }
  607. ast_test_validate(test, ast_msg_tech_register(&test_msg_tech) == 0);
  608. ast_test_validate(test, ast_msg_handler_register(&test_msg_handler) == 0);
  609. msg = ast_msg_alloc();
  610. ast_test_validate(test, msg != NULL);
  611. ast_msg_set_to(msg, "foo");
  612. ast_msg_set_context(msg, TEST_CONTEXT);
  613. ast_msg_set_exten(msg, NULL);
  614. ast_test_validate(test, ast_msg_has_destination(msg) == 1);
  615. if (!ast_msg_send(msg, "testmsg:foo", "blah")) {
  616. msg = NULL;
  617. } else {
  618. ast_test_status_update(test, "Failed to send message\n");
  619. ast_test_set_result(test, AST_TEST_FAIL);
  620. }
  621. ast_test_validate(test, ast_msg_handler_unregister(&test_msg_handler) == 0);
  622. ast_test_validate(test, ast_msg_tech_unregister(&test_msg_tech) == 0);
  623. return AST_TEST_PASS;
  624. }
  625. static int test_init_cb(struct ast_test_info *info, struct ast_test *test)
  626. {
  627. received_user_events = 0;
  628. handler_received_message = 0;
  629. message_received = 0;
  630. AST_VECTOR_INIT(&expected_user_event_fields, DEFAULT_EXPECTED_EVENTS);
  631. AST_VECTOR_INIT(&bad_headers, DEFAULT_EXPECTED_EVENTS);
  632. return 0;
  633. }
  634. #define FREE_VARIABLE_VECTOR(vector) do { \
  635. int i; \
  636. for (i = 0; i < AST_VECTOR_SIZE(&(vector)); i++) { \
  637. struct ast_variable *headers; \
  638. headers = AST_VECTOR_GET(&(vector), i); \
  639. if (!headers) { \
  640. continue; \
  641. } \
  642. ast_variables_destroy(headers); \
  643. } \
  644. AST_VECTOR_FREE(&(vector)); \
  645. } while (0)
  646. static int test_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
  647. {
  648. FREE_VARIABLE_VECTOR(expected_user_event_fields);
  649. FREE_VARIABLE_VECTOR(bad_headers);
  650. return 0;
  651. }
  652. static int unload_module(void)
  653. {
  654. AST_TEST_UNREGISTER(test_message_msg_tech_registration);
  655. AST_TEST_UNREGISTER(test_message_msg_handler_registration);
  656. AST_TEST_UNREGISTER(test_message_manipulation);
  657. AST_TEST_UNREGISTER(test_message_queue_dialplan_nominal);
  658. AST_TEST_UNREGISTER(test_message_queue_handler_nominal);
  659. AST_TEST_UNREGISTER(test_message_queue_both_nominal);
  660. AST_TEST_UNREGISTER(test_message_has_destination_dialplan);
  661. AST_TEST_UNREGISTER(test_message_has_destination_handler);
  662. AST_TEST_UNREGISTER(test_message_msg_send);
  663. ast_context_destroy(NULL, AST_MODULE);
  664. ast_manager_unregister_hook(&user_event_hook);
  665. return 0;
  666. }
  667. static int create_test_dialplan(void)
  668. {
  669. int res = 0;
  670. if (!ast_context_find_or_create(NULL, NULL, TEST_CONTEXT, AST_MODULE)) {
  671. return -1;
  672. }
  673. res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 1, NULL, NULL,
  674. "UserEvent", "TestMessageUnitTest,Verify:To,Value:${MESSAGE(to)}",
  675. NULL, AST_MODULE);
  676. res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 2, NULL, NULL,
  677. "UserEvent", "TestMessageUnitTest,Verify:From,Value:${MESSAGE(from)}",
  678. NULL, AST_MODULE);
  679. res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 3, NULL, NULL,
  680. "UserEvent", "TestMessageUnitTest,Verify:Body,Value:${MESSAGE(body)}",
  681. NULL, AST_MODULE);
  682. res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 4, NULL, NULL,
  683. "UserEvent", "TestMessageUnitTest,Verify:Custom,Value:${MESSAGE_DATA(custom_data)}",
  684. NULL, AST_MODULE);
  685. res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 5, NULL, NULL,
  686. "Set", "MESSAGE_DATA(custom_data)=${MESSAGE_DATA(custom_data)}",
  687. NULL, AST_MODULE);
  688. res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 6, NULL, NULL,
  689. "MessageSend", "testmsg:${MESSAGE(from)},testmsg:${MESSAGE(to)}",
  690. NULL, AST_MODULE);
  691. ast_manager_register_hook(&user_event_hook);
  692. return res;
  693. }
  694. static int load_module(void)
  695. {
  696. AST_TEST_REGISTER(test_message_msg_tech_registration);
  697. AST_TEST_REGISTER(test_message_msg_handler_registration);
  698. AST_TEST_REGISTER(test_message_manipulation);
  699. AST_TEST_REGISTER(test_message_queue_dialplan_nominal);
  700. AST_TEST_REGISTER(test_message_queue_handler_nominal);
  701. AST_TEST_REGISTER(test_message_queue_both_nominal);
  702. AST_TEST_REGISTER(test_message_has_destination_dialplan);
  703. AST_TEST_REGISTER(test_message_has_destination_handler);
  704. AST_TEST_REGISTER(test_message_msg_send);
  705. create_test_dialplan();
  706. ast_test_register_init(TEST_CATEGORY, test_init_cb);
  707. ast_test_register_cleanup(TEST_CATEGORY, test_cleanup_cb);
  708. return AST_MODULE_LOAD_SUCCESS;
  709. }
  710. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Out-of-call text message support");