summaryrefslogtreecommitdiff
path: root/tests/test_message.c
blob: 1d03d74a939e4cf1347d0830676a45a8abbb2e89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2014, Digium, Inc.
 *
 * Matt Jordan <mjordan@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Test module for out-of-call text message module
 *
 * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
 *
 * \ingroup tests
 */

/*** MODULEINFO
	<depend>TEST_FRAMEWORK</depend>
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include <regex.h>

#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/message.h"
#include "asterisk/pbx.h"
#include "asterisk/manager.h"
#include "asterisk/vector.h"

#define TEST_CATEGORY "/main/message/"

#define TEST_CONTEXT "__TEST_MESSAGE_CONTEXT__"
#define TEST_EXTENSION "test_message_extension"

/*! \brief The number of user events we should get in a dialplan test */
#define DEFAULT_EXPECTED_EVENTS 4

/*! \brief The current number of received user events */
static int received_user_events;

/*! \brief The number of user events we expect for this test */
static int expected_user_events;

/*! \brief Predicate for the \ref test_message_handler receiving a message */
static int handler_received_message;

/*! \brief Condition wait variable for all dialplan user events being received */
static ast_cond_t user_event_cond;

/*! \brief Mutex for \c user_event_cond */
AST_MUTEX_DEFINE_STATIC(user_event_lock);

/*! \brief Condition wait variable for \ref test_msg_handler receiving message */
static ast_cond_t handler_cond;

/*! \brief Mutex for \c handler_cond */
AST_MUTEX_DEFINE_STATIC(handler_lock);

/*! \brief The expected user event fields */
AST_VECTOR(var_vector, struct ast_variable *) expected_user_event_fields;

/*! \brief If a user event fails, the bad headers that didn't match */
AST_VECTOR(, struct ast_variable *) bad_headers;

static int test_msg_send(const struct ast_msg *msg, const char *to, const char *from);

static struct ast_msg_tech test_msg_tech = {
	.name = "testmsg",
	.msg_send = test_msg_send,
};

static int test_msg_handle_msg_cb(struct ast_msg *msg);
static int test_msg_has_destination_cb(const struct ast_msg *msg);

/*! \brief Our test message handler */
static struct ast_msg_handler test_msg_handler = {
	.name = "testmsg",
	.handle_msg = test_msg_handle_msg_cb,
	.has_destination = test_msg_has_destination_cb,
};

static int user_event_hook_cb(int category, const char *event, char *body);

/*! \brief AMI event hook that verifies whether or not we've gotten our user events */
static struct manager_custom_hook user_event_hook = {
	.file = AST_MODULE,
	.helper = user_event_hook_cb,
};

/*!
 * \brief Verifies a user event header/value pair
 *
 * \param user_event which user event to check
 * \param header The header to verify
 * \param value The value read from the event
 *
 * \retval -1 on error or evaluation failure
 * \retval 0 if match not needed or success
 */
static int verify_user_event_fields(int user_event, const char *header, const char *value)
{
	struct ast_variable *current;
	struct ast_variable *expected;
	regex_t regexbuf;
	int error;

	if (user_event >= AST_VECTOR_SIZE(&expected_user_event_fields)) {
		return -1;
	}

	expected = AST_VECTOR_GET(&expected_user_event_fields, user_event);
	if (!expected) {
		return -1;
	}

	for (current = expected; current; current = current->next) {
		struct ast_variable *bad_header;

		if (strcmp(current->name, header)) {
			continue;
		}

		error = regcomp(&regexbuf, current->value, REG_EXTENDED | REG_NOSUB);
		if (error) {
			char error_buf[128];
			regerror(error, &regexbuf, error_buf, sizeof(error_buf));
			ast_log(LOG_ERROR, "Failed to compile regex '%s' for header check '%s': %s\n",
				current->value, current->name, error_buf);
			return -1;
		}

		if (!regexec(&regexbuf, value, 0, NULL, 0)) {
			regfree(&regexbuf);
			return 0;
		}

		bad_header = ast_variable_new(header, value, __FILE__);
		if (bad_header) {
			struct ast_variable *bad_headers_head = NULL;

			if (user_event < AST_VECTOR_SIZE(&bad_headers)) {
				bad_headers_head = AST_VECTOR_GET(&bad_headers, user_event);
			}
			ast_variable_list_append(&bad_headers_head, bad_header);
			AST_VECTOR_REPLACE(&bad_headers, user_event, bad_headers_head);
		}
		regfree(&regexbuf);
		return -1;
	}

	return 0;
}

static int message_received;

static int test_msg_send(const struct ast_msg *msg, const char *to, const char *from)
{
	message_received = 1;

	return 0;
}

static int test_msg_handle_msg_cb(struct ast_msg *msg)
{
	ast_mutex_lock(&handler_lock);
	handler_received_message = 1;
	ast_cond_signal(&handler_cond);
	ast_mutex_unlock(&handler_lock);

	return 0;
}

static int test_msg_has_destination_cb(const struct ast_msg *msg)
{
	/* We only care about one destination: foo! */
	if (ast_strlen_zero(ast_msg_get_to(msg))) {
		return 0;
	}
	return (!strcmp(ast_msg_get_to(msg), "foo") ? 1 : 0);
}

static int user_event_hook_cb(int category, const char *event, char *body)
{
	char *parse;
	char *kvp;

	if (strcmp(event, "UserEvent")) {
		return -1;
	}

	parse = ast_strdupa(body);
	while ((kvp = strsep(&parse, "\r\n"))) {
		char *key, *value;

		kvp = ast_trim_blanks(kvp);
		if (ast_strlen_zero(kvp)) {
			continue;
		}
		key = strsep(&kvp, ":");
		value = ast_skip_blanks(kvp);
		verify_user_event_fields(received_user_events, key, value);
	}

	received_user_events++;

	ast_mutex_lock(&user_event_lock);
	if (received_user_events == expected_user_events) {
		ast_cond_signal(&user_event_cond);
	}
	ast_mutex_unlock(&user_event_lock);

	return 0;
}

/*! \brief Wait for the \ref test_msg_handler to receive the message */
static int handler_wait_for_message(struct ast_test *test)
{
	int error = 0;
	struct timeval wait = ast_tvadd(ast_tvnow(), ast_tv(5 /* seconds */, 0));
	struct timespec wait_time = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000 };

	ast_mutex_lock(&handler_lock);
	while (!handler_received_message) {
		error = ast_cond_timedwait(&handler_cond, &handler_lock, &wait_time);
		if (error == ETIMEDOUT) {
			ast_test_status_update(test, "Test timed out while waiting for handler to get message\n");
			ast_test_set_result(test, AST_TEST_FAIL);
			break;
		}
	}
	ast_mutex_unlock(&handler_lock);

	return (error != ETIMEDOUT);
}

/*! \brief Wait for the expected number of user events to be received */
static int user_event_wait_for_events(struct ast_test *test, int expected_events)
{
	int error;
	struct timeval wait = ast_tvadd(ast_tvnow(), ast_tv(5 /* seconds */, 0));
	struct timespec wait_time = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000 };

	expected_user_events = expected_events;

	ast_mutex_lock(&user_event_lock);
	while (received_user_events != expected_user_events) {
		error = ast_cond_timedwait(&user_event_cond, &user_event_lock, &wait_time);
		if (error == ETIMEDOUT) {
			ast_test_status_update(test, "Test timed out while waiting for %d expected user events\n", expected_events);
			ast_test_set_result(test, AST_TEST_FAIL);
			break;
		}
	}
	ast_mutex_unlock(&user_event_lock);

	ast_test_status_update(test, "Received %d of %d user events\n", received_user_events, expected_events);
	return !(received_user_events == expected_events);
}

static int verify_bad_headers(struct ast_test *test)
{
	int res = 0;
	int i;

	for (i = 0; i < AST_VECTOR_SIZE(&bad_headers); i++) {
		struct ast_variable *headers;
		struct ast_variable *current;

		headers = AST_VECTOR_GET(&bad_headers, i);
		if (!headers) {
			continue;
		}

		res = -1;
		for (current = headers; current; current = current->next) {
			ast_test_status_update(test, "Expected UserEvent %d: Failed to match %s: %s\n",
				i, current->name, current->value);
			ast_test_set_result(test, AST_TEST_FAIL);
		}
	}

	return res;
}

AST_TEST_DEFINE(test_message_msg_tech_registration)
{
	int reg_result;

	switch (cmd) {
	case TEST_INIT:
		info->name = __func__;
		info->category = TEST_CATEGORY;
		info->summary = "Test register/unregister of a message tech";
		info->description =
			"Test that:\n"
			"\tA message technology can be registered once only\n"
			"\tA registered message technology can be unregistered once only";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	reg_result = ast_msg_tech_register(&test_msg_tech);
	ast_test_validate(test, reg_result == 0);

	reg_result = ast_msg_tech_register(&test_msg_tech);
	ast_test_validate(test, reg_result == -1);

	reg_result = ast_msg_tech_unregister(&test_msg_tech);
	ast_test_validate(test, reg_result == 0);

	reg_result = ast_msg_tech_unregister(&test_msg_tech);
	ast_test_validate(test, reg_result == -1);

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(test_message_msg_handler_registration)
{
	int reg_result;

	switch (cmd) {
	case TEST_INIT:
		info->name = __func__;
		info->category = TEST_CATEGORY;
		info->summary = "Test register/unregister of a message handler";
		info->description =
			"Test that:\n"
			"\tA message handler can be registered once only\n"
			"\tA registered message handler can be unregistered once only";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	reg_result = ast_msg_handler_register(&test_msg_handler);
	ast_test_validate(test, reg_result == 0);

	reg_result = ast_msg_handler_register(&test_msg_handler);
	ast_test_validate(test, reg_result == -1);

	reg_result = ast_msg_handler_unregister(&test_msg_handler);
	ast_test_validate(test, reg_result == 0);

	reg_result = ast_msg_handler_unregister(&test_msg_handler);
	ast_test_validate(test, reg_result == -1);

	return AST_TEST_PASS;
}

static void ast_msg_safe_destroy(void *obj)
{
	struct ast_msg *msg = obj;

	if (msg) {
		ast_msg_destroy(msg);
	}
}

AST_TEST_DEFINE(test_message_manipulation)
{
	RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
	RAII_VAR(struct ast_msg_var_iterator *, it_vars, NULL, ast_msg_var_iterator_destroy);
	int result;
	const char *actual;
	const char *out_name;
	const char *out_value;

	switch (cmd) {
	case TEST_INIT:
		info->name = __func__;
		info->category = TEST_CATEGORY;
		info->summary = "Test manipulating properties of a message";
		info->description =
			"This test covers the following:\n"
			"\tSetting/getting the body\n"
			"\tSetting/getting inbound/outbound variables\n"
			"\tIterating over variables";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	msg = ast_msg_alloc();
	ast_test_validate(test, msg != NULL);

	/* Test setting/getting to */
	result = ast_msg_set_to(msg, "testmsg:%s", "foo");
	ast_test_validate(test, result == 0);
	actual = ast_msg_get_to(msg);
	ast_test_validate(test, !strcmp(actual, "testmsg:foo"));

	/* Test setting/getting from */
	result = ast_msg_set_from(msg, "testmsg:%s", "bar");
	ast_test_validate(test, result == 0);
	actual = ast_msg_get_from(msg);
	ast_test_validate(test, !strcmp(actual, "testmsg:bar"));

	/* Test setting/getting body */
	result = ast_msg_set_body(msg, "BodyTest: %s", "foo");
	ast_test_validate(test, result == 0);
	actual = ast_msg_get_body(msg);
	ast_test_validate(test, !strcmp(actual, "BodyTest: foo"));

	/* Test setting/getting technology */
	result = ast_msg_set_tech(msg, "%s", "my_tech");
	ast_test_validate(test, result == 0);
	actual = ast_msg_get_tech(msg);
	ast_test_validate(test, !strcmp(actual, "my_tech"));

	/* Test setting/getting endpoint */
	result = ast_msg_set_endpoint(msg, "%s", "terminus");
	ast_test_validate(test, result == 0);
	actual = ast_msg_get_endpoint(msg);
	ast_test_validate(test, !strcmp(actual, "terminus"));

	/* Test setting/getting non-outbound variable */
	result = ast_msg_set_var(msg, "foo", "bar");
	ast_test_validate(test, result == 0);
	actual = ast_msg_get_var(msg, "foo");
	ast_test_validate(test, !strcmp(actual, "bar"));

	/* Test updating existing variable */
	result = ast_msg_set_var(msg, "foo", "new_bar");
	ast_test_validate(test, result == 0);
	actual = ast_msg_get_var(msg, "foo");
	ast_test_validate(test, !strcmp(actual, "new_bar"));

	/* Verify a non-outbound variable is not iterable */
	it_vars = ast_msg_var_iterator_init(msg);
	ast_test_validate(test, it_vars != NULL);
	ast_test_validate(test, ast_msg_var_iterator_next(msg, it_vars, &out_name, &out_value) == 0);
	ast_msg_var_iterator_destroy(it_vars);

	/* Test updating an existing variable as an outbound variable */
	result = ast_msg_set_var_outbound(msg, "foo", "outbound_bar");
	ast_test_validate(test, result == 0);
	it_vars = ast_msg_var_iterator_init(msg);
	ast_test_validate(test, it_vars != NULL);
	result = ast_msg_var_iterator_next(msg, it_vars, &out_name, &out_value);
	ast_test_validate(test, result == 1);
	ast_test_validate(test, !strcmp(out_name, "foo"));
	ast_test_validate(test, !strcmp(out_value, "outbound_bar"));
	ast_msg_var_unref_current(it_vars);
	result = ast_msg_var_iterator_next(msg, it_vars, &out_name, &out_value);
	ast_test_validate(test, result == 0);

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(test_message_queue_dialplan_nominal)
{
	RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
	struct ast_variable *expected;
	struct ast_variable *expected_response = NULL;

	switch (cmd) {
	case TEST_INIT:
		info->name = __func__;
		info->category = TEST_CATEGORY;
		info->summary = "Test enqueueing messages to the dialplan";
		info->description =
			"Test that a message enqueued for the dialplan is\n"
			"passed to that particular extension";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	msg = ast_msg_alloc();
	ast_test_validate(test, msg != NULL);

	expected = ast_variable_new("Verify","^To$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	expected = ast_variable_new("Value","^foo$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	AST_VECTOR_REPLACE(&expected_user_event_fields, 0, expected_response);

	expected_response = NULL;
	expected = ast_variable_new("Verify", "^From$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	expected = ast_variable_new("Value","^bar$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	AST_VECTOR_REPLACE(&expected_user_event_fields, 1, expected_response);

	expected_response = NULL;
	expected = ast_variable_new("Verify", "^Body$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	expected = ast_variable_new("Value", "^a body$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	AST_VECTOR_REPLACE(&expected_user_event_fields, 2, expected_response);

	expected_response = NULL;
	expected = ast_variable_new("Verify", "^Custom$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	expected = ast_variable_new("Value", "^field$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	AST_VECTOR_REPLACE(&expected_user_event_fields, 3, expected_response);

	ast_msg_set_to(msg, "foo");
	ast_msg_set_from(msg, "bar");
	ast_msg_set_body(msg, "a body");
	ast_msg_set_var_outbound(msg, "custom_data", "field");

	ast_msg_set_context(msg, TEST_CONTEXT);
	ast_msg_set_exten(msg, TEST_EXTENSION);

	ast_msg_queue(msg);
	msg = NULL;

	if (user_event_wait_for_events(test, DEFAULT_EXPECTED_EVENTS)) {
		ast_test_status_update(test, "Failed to received %d expected user events\n", DEFAULT_EXPECTED_EVENTS);
		return AST_TEST_FAIL;
	}

	if (verify_bad_headers(test)) {
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(test_message_queue_handler_nominal)
{
	RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
	int result;

	switch (cmd) {
	case TEST_INIT:
		info->name = __func__;
		info->category = TEST_CATEGORY;
		info->summary = "Test enqueueing messages to a handler";
		info->description =
			"Test that a message enqueued can be handled by a\n"
			"non-dialplan handler";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	msg = ast_msg_alloc();
	ast_test_validate(test, msg != NULL);

	result = ast_msg_handler_register(&test_msg_handler);
	ast_test_validate(test, result == 0);

	ast_msg_set_to(msg, "foo");
	ast_msg_set_from(msg, "bar");
	ast_msg_set_body(msg, "a body");

	ast_msg_queue(msg);
	msg = NULL;

	/* This will automatically fail the test if we don't get the message */
	handler_wait_for_message(test);

	result = ast_msg_handler_unregister(&test_msg_handler);
	ast_test_validate(test, result == 0);

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(test_message_queue_both_nominal)
{
	RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
	struct ast_variable *expected;
	struct ast_variable *expected_response = NULL;
	int result;

	switch (cmd) {
	case TEST_INIT:
		info->name = __func__;
		info->category = TEST_CATEGORY;
		info->summary = "Test enqueueing messages to a dialplan and custom handler";
		info->description =
			"Test that a message enqueued is passed to all\n"
			"handlers that can process it, dialplan as well as\n"
			"a custom handler";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	msg = ast_msg_alloc();
	ast_test_validate(test, msg != NULL);

	result = ast_msg_handler_register(&test_msg_handler);
	ast_test_validate(test, result == 0);

	expected = ast_variable_new("Verify","^To$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	expected = ast_variable_new("Value","^foo$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	AST_VECTOR_REPLACE(&expected_user_event_fields, 0, expected_response);

	expected_response = NULL;
	expected = ast_variable_new("Verify", "^From$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	expected = ast_variable_new("Value","^bar$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	AST_VECTOR_REPLACE(&expected_user_event_fields, 1, expected_response);

	expected_response = NULL;
	expected = ast_variable_new("Verify", "^Body$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	expected = ast_variable_new("Value", "^a body$", __FILE__);
	ast_variable_list_append(&expected_response, expected);
	AST_VECTOR_REPLACE(&expected_user_event_fields, 2, expected_response);

	ast_msg_set_to(msg, "foo");
	ast_msg_set_from(msg, "bar");
	ast_msg_set_body(msg, "a body");

	ast_msg_set_context(msg, TEST_CONTEXT);
	ast_msg_set_exten(msg, TEST_EXTENSION);

	ast_msg_queue(msg);
	msg = NULL;

	if (user_event_wait_for_events(test, DEFAULT_EXPECTED_EVENTS)) {
		ast_test_status_update(test, "Failed to received %d expected user events\n", DEFAULT_EXPECTED_EVENTS);
		ast_test_set_result(test, AST_TEST_FAIL);
	}

	/* This will automatically fail the test if we don't get the message */
	handler_wait_for_message(test);

	result = ast_msg_handler_unregister(&test_msg_handler);
	ast_test_validate(test, result == 0);

	if (verify_bad_headers(test)) {
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(test_message_has_destination_dialplan)
{
	RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);

	switch (cmd) {
	case TEST_INIT:
		info->name = __func__;
		info->category = TEST_CATEGORY;
		info->summary = "Test checking for a dialplan destination";
		info->description =
			"Test that a message's destination is verified via the\n"
			"dialplan";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	msg = ast_msg_alloc();
	ast_test_validate(test, msg != NULL);

	ast_msg_set_context(msg, TEST_CONTEXT);
	ast_msg_set_exten(msg, TEST_EXTENSION);
	ast_test_validate(test, ast_msg_has_destination(msg) == 1);

	ast_msg_set_context(msg, "__I_SHOULD_NOT_EXIST_PLZ__");
	ast_test_validate(test, ast_msg_has_destination(msg) == 0);

	ast_msg_set_context(msg, TEST_CONTEXT);
	ast_msg_set_exten(msg, "__I_SHOULD_NOT_EXIST_PLZ__");
	ast_test_validate(test, ast_msg_has_destination(msg) == 0);

	ast_msg_set_exten(msg, NULL);
	ast_test_validate(test, ast_msg_has_destination(msg) == 0);

	ast_msg_set_context(msg, NULL);
	ast_msg_set_exten(msg, TEST_EXTENSION);
	ast_test_validate(test, ast_msg_has_destination(msg) == 0);

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(test_message_has_destination_handler)
{
	RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);
	int result;

	switch (cmd) {
	case TEST_INIT:
		info->name = __func__;
		info->category = TEST_CATEGORY;
		info->summary = "Test checking for a handler destination";
		info->description =
			"Test that a message's destination is verified via a\n"
			"handler";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	result = ast_msg_handler_register(&test_msg_handler);
	ast_test_validate(test, result == 0);

	msg = ast_msg_alloc();
	ast_test_validate(test, msg != NULL);

	ast_msg_set_to(msg, "foo");
	ast_msg_set_context(msg, TEST_CONTEXT);
	ast_msg_set_exten(msg, NULL);
	ast_test_validate(test, ast_msg_has_destination(msg) == 1);

	ast_msg_set_context(msg, NULL);
	ast_test_validate(test, ast_msg_has_destination(msg) == 1);

	ast_msg_set_to(msg, "__I_SHOULD_NOT_EXIST_PLZ__");
	ast_test_validate(test, ast_msg_has_destination(msg) == 0);

	result = ast_msg_handler_unregister(&test_msg_handler);
	ast_test_validate(test, result == 0);

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(test_message_msg_send)
{
	RAII_VAR(struct ast_msg *, msg, NULL, ast_msg_safe_destroy);

	switch (cmd) {
	case TEST_INIT:
		info->name = __func__;
		info->category = TEST_CATEGORY;
		info->summary = "Test message routing";
		info->description =
			"Test that a message can be routed if it has\n"
			"a valid handler";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	ast_test_validate(test, ast_msg_tech_register(&test_msg_tech) == 0);
	ast_test_validate(test, ast_msg_handler_register(&test_msg_handler) == 0);

	msg = ast_msg_alloc();
	ast_test_validate(test, msg != NULL);

	ast_msg_set_to(msg, "foo");
	ast_msg_set_context(msg, TEST_CONTEXT);
	ast_msg_set_exten(msg, NULL);
	ast_test_validate(test, ast_msg_has_destination(msg) == 1);

	if (!ast_msg_send(msg, "testmsg:foo", "blah")) {
		msg = NULL;
	} else {
		ast_test_status_update(test, "Failed to send message\n");
		ast_test_set_result(test, AST_TEST_FAIL);
	}

	ast_test_validate(test, ast_msg_handler_unregister(&test_msg_handler) == 0);
	ast_test_validate(test, ast_msg_tech_unregister(&test_msg_tech) == 0);

	return AST_TEST_PASS;
}

static int test_init_cb(struct ast_test_info *info, struct ast_test *test)
{
	received_user_events = 0;
	handler_received_message = 0;
	message_received = 0;

	AST_VECTOR_INIT(&expected_user_event_fields, DEFAULT_EXPECTED_EVENTS);
	AST_VECTOR_INIT(&bad_headers, DEFAULT_EXPECTED_EVENTS);

	return 0;
}

#define FREE_VARIABLE_VECTOR(vector) do { \
	int i; \
	for (i = 0; i < AST_VECTOR_SIZE(&(vector)); i++) { \
		struct ast_variable *headers; \
		headers = AST_VECTOR_GET(&(vector), i); \
		if (!headers) { \
			continue; \
		} \
		ast_variables_destroy(headers); \
	} \
	AST_VECTOR_FREE(&(vector)); \
	} while (0)


static int test_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
{
	FREE_VARIABLE_VECTOR(expected_user_event_fields);
	FREE_VARIABLE_VECTOR(bad_headers);

	return 0;
}

static int unload_module(void)
{
	AST_TEST_UNREGISTER(test_message_msg_tech_registration);
	AST_TEST_UNREGISTER(test_message_msg_handler_registration);
	AST_TEST_UNREGISTER(test_message_manipulation);
	AST_TEST_UNREGISTER(test_message_queue_dialplan_nominal);
	AST_TEST_UNREGISTER(test_message_queue_handler_nominal);
	AST_TEST_UNREGISTER(test_message_queue_both_nominal);
	AST_TEST_UNREGISTER(test_message_has_destination_dialplan);
	AST_TEST_UNREGISTER(test_message_has_destination_handler);
	AST_TEST_UNREGISTER(test_message_msg_send);

	ast_context_destroy(NULL, AST_MODULE);

	ast_manager_unregister_hook(&user_event_hook);

	return 0;
}

static int create_test_dialplan(void)
{
	int res = 0;

	if (!ast_context_find_or_create(NULL, NULL, TEST_CONTEXT, AST_MODULE)) {
		return -1;
	}

	res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 1, NULL, NULL,
	                         "UserEvent", "TestMessageUnitTest,Verify:To,Value:${MESSAGE(to)}",
	                         NULL, AST_MODULE);
	res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 2, NULL, NULL,
	                         "UserEvent", "TestMessageUnitTest,Verify:From,Value:${MESSAGE(from)}",
	                         NULL, AST_MODULE);
	res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 3, NULL, NULL,
	                         "UserEvent", "TestMessageUnitTest,Verify:Body,Value:${MESSAGE(body)}",
	                         NULL, AST_MODULE);
	res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 4, NULL, NULL,
	                         "UserEvent", "TestMessageUnitTest,Verify:Custom,Value:${MESSAGE_DATA(custom_data)}",
	                         NULL, AST_MODULE);
	res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 5, NULL, NULL,
	                         "Set", "MESSAGE_DATA(custom_data)=${MESSAGE_DATA(custom_data)}",
	                         NULL, AST_MODULE);
	res |= ast_add_extension(TEST_CONTEXT, 0, TEST_EXTENSION, 6, NULL, NULL,
	                         "MessageSend", "testmsg:${MESSAGE(from)},testmsg:${MESSAGE(to)}",
	                         NULL, AST_MODULE);

	ast_manager_register_hook(&user_event_hook);

	return res;
}

static int load_module(void)
{
	AST_TEST_REGISTER(test_message_msg_tech_registration);
	AST_TEST_REGISTER(test_message_msg_handler_registration);
	AST_TEST_REGISTER(test_message_manipulation);
	AST_TEST_REGISTER(test_message_queue_dialplan_nominal);
	AST_TEST_REGISTER(test_message_queue_handler_nominal);
	AST_TEST_REGISTER(test_message_queue_both_nominal);
	AST_TEST_REGISTER(test_message_has_destination_dialplan);
	AST_TEST_REGISTER(test_message_has_destination_handler);
	AST_TEST_REGISTER(test_message_msg_send);

	create_test_dialplan();

	ast_test_register_init(TEST_CATEGORY, test_init_cb);
	ast_test_register_cleanup(TEST_CATEGORY, test_cleanup_cb);

	return AST_MODULE_LOAD_SUCCESS;
}


AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Out-of-call text message support");