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