test_message.c 26 KB

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