Merged revisions 257544 via svnmerge from
[asterisk/asterisk.git] / tests / test_app.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * Jeff Peeler <jpeeler@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 /*!
20  * \file
21  * \brief App unit test
22  *
23  * \author Jeff Peeler <jpeeler@digium.com>
24  *
25  */
26
27 /*** MODULEINFO
28         <depend>TEST_FRAMEWORK</depend>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/utils.h"
36 #include "asterisk/module.h"
37 #include "asterisk/test.h"
38 #include "asterisk/app.h"
39 #include "asterisk/channel.h"
40
41 #define BASE_GROUP "a group"
42
43 AST_TEST_DEFINE(options_parsing)
44 {
45         enum test_option_flags {
46                 OPT_SIMPLE,
47                 OPT_WITHQUOTES,
48                 OPT_WITHBACKSLASH,
49         };
50         enum test_option_args {
51                 OPT_ARG_SIMPLE,
52                 OPT_ARG_WITHQUOTES,
53                 OPT_ARG_WITHBACKSLASH,
54                 OPT_ARG_ARRAY_SIZE,
55         };
56         AST_APP_OPTIONS(test_options, {
57                 AST_APP_OPTION_ARG('a', OPT_SIMPLE,        OPT_ARG_SIMPLE),
58                 AST_APP_OPTION_ARG('b', OPT_WITHQUOTES,    OPT_ARG_WITHQUOTES),
59                 AST_APP_OPTION_ARG('c', OPT_WITHBACKSLASH, OPT_ARG_WITHBACKSLASH),
60         });
61         struct ast_flags opts = { 0, };
62         struct ast_flags64 opts64 = { 0, };
63         char *opt_args[OPT_ARG_ARRAY_SIZE];
64         struct {
65                 const char *string;
66                 const char *parse[3];
67         } options[] = {
68                 { "a(simple)b(\"quoted\")c(back\\slash)", { "simple", "quoted", "backslash", }, },
69                 { "b(\"((())))\")a(simple)c(back\\)slash)", { "simple", "((())))", "back)slash", }, },
70                 { "b(\"((\\\"\\)\\(\")a(simple)c(back\\\"\\)\\(\\\"slash)", { "simple", "((\"\\)\\(", "back\")(\"slash", }, },
71         };
72         int i, j, res = AST_TEST_PASS;
73         char buffer[256];
74
75         switch (cmd) {
76         case TEST_INIT:
77                 info->name = "options_parsing";
78                 info->category = "main/app/";
79                 info->summary = "App options unit test";
80                 info->description =
81                         "This tests the options parsing code to ensure that it behaves as expected";
82                 return AST_TEST_NOT_RUN;
83         case TEST_EXECUTE:
84                 break;
85         }
86
87         for (i = 0; i < ARRAY_LEN(options); i++) {
88                 ast_copy_string(buffer, options[i].string, sizeof(buffer));
89
90                 if (ast_app_parse_options(test_options, &opts, opt_args, buffer)) {
91                         ast_test_status_update(test, "ast_app_parse_options() of '%s' failed\n", options[i].string);
92                         res = AST_TEST_FAIL;
93                 } else {
94                         /* Check arguments for success */
95                         for (j = 0; j < 3; j++) {
96                                 if (strcmp(opt_args[j], options[i].parse[j])) {
97                                         ast_test_status_update(test, "Parse of option %c from '%s' produced '%s', "
98                                                 "but it should have produced '%s'\n",
99                                                 'a' + j, options[i].string, opt_args[j], options[i].parse[j]);
100                                         res = AST_TEST_FAIL;
101                                 }
102                         }
103                 }
104
105                 ast_copy_string(buffer, options[i].string, sizeof(buffer));
106                 if (ast_app_parse_options64(test_options, &opts64, opt_args, buffer)) {
107                         ast_test_status_update(test, "ast_app_parse_options64() of '%s' failed\n", options[i].string);
108                         res = AST_TEST_FAIL;
109                 } else {
110                         /* Check arguments for success */
111                         for (j = 0; j < 3; j++) {
112                                 if (strcmp(opt_args[j], options[i].parse[j])) {
113                                         ast_test_status_update(test, "Parse of option %c from '%s' produced '%s', "
114                                                 "but it should have produced '%s'\n",
115                                                 'a' + j, options[i].string, opt_args[j], options[i].parse[j]);
116                                         res = AST_TEST_FAIL;
117                                 }
118                         }
119                 }
120         }
121
122         return res;
123 }
124
125 AST_TEST_DEFINE(app_group)
126 {
127         struct ast_channel *test_channel1 = NULL;
128         struct ast_channel *test_channel2 = NULL;
129         struct ast_channel *test_channel3 = NULL;
130         struct ast_channel *test_channel4 = NULL;
131
132         static const char group1_full[] = BASE_GROUP "groupgroup";
133         static const char group2_full[] = BASE_GROUP "Groupgroup";
134         static const char regex1[] = "gr"; /* matches everything */
135         static const char regex2[] = "(group){2}$"; /* matches only group1_full */
136         static const char regex3[] = "[:ascii:]"; /* matches everything */
137         static const char regex4[] = "^(NOMATCH)"; /* matches nothing */
138         static const char category1_full[] = BASE_GROUP "@a_category"; /* categories shouldn't have spaces */
139         static const char category2_full[] = BASE_GROUP "@another!Category";
140         static const char regex5[] = "(gory)$"; /* matches both categories */
141         static const char regex6[] = "[A-Z]+"; /* matches only category2_full */
142         static const char regex7[] = "[["; /* not valid syntax, yes an expected warning will be displayed */
143         static enum ast_test_result_state res = AST_TEST_PASS;
144         static const struct group_test_params {
145                 const char *groupmatch;
146                 const char *category;
147                 int expected;
148         } subtests[] = {
149                 { regex1, "", 4 },
150                 { regex2, "", 1 },
151                 { regex3, "", 4 },
152                 { regex4, "", 0 },
153                 { BASE_GROUP, regex5, 2 },
154                 { BASE_GROUP, regex6, 1 },
155                 /* this test is expected to generate a warning message from the invalid regex */
156                 { BASE_GROUP, regex7, 0 }
157         };
158         int i;
159         int returned_count;
160
161         switch (cmd) {
162         case TEST_INIT:
163                 info->name = "app_group";
164                 info->category = "main/app/";
165                 info->summary = "App group unit test";
166                 info->description =
167                         "This tests various app group functionality";
168                 return AST_TEST_NOT_RUN;
169         case TEST_EXECUTE:
170                 break;
171         }
172
173         ast_test_status_update(test, "Creating test channels with the following groups:\n"
174                 "'%s', '%s', '%s', '%s'\n", group1_full, group2_full, category1_full, category2_full);
175
176         if (!(test_channel1 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
177         NULL, NULL, 0, 0, "TestChannel1"))) {
178                 goto exit_group_test;
179         }
180         if (!(test_channel2 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
181         NULL, NULL, 0, 0, "TestChannel2"))) {
182                 goto exit_group_test;
183         }
184         if (!(test_channel3 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
185         NULL, NULL, 0, 0, "TestChannel3"))) {
186                 goto exit_group_test;
187         }
188         if (!(test_channel4 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
189         NULL, NULL, 0, 0, "TestChannel4"))) {
190                 goto exit_group_test;
191         }
192
193         ast_app_group_set_channel(test_channel1, group1_full);
194         ast_app_group_set_channel(test_channel2, group2_full);
195         ast_app_group_set_channel(test_channel3, category1_full);
196         ast_app_group_set_channel(test_channel4, category2_full);
197
198         for (i = 0; i < ARRAY_LEN(subtests); i++) {
199                 ast_assert(subtests[i].groupmatch != NULL || subtests[i].category != NULL);
200                 returned_count = ast_app_group_match_get_count(subtests[i].groupmatch, subtests[i].category);
201
202                 if (subtests[i].expected != returned_count) {
203                         ast_test_status_update(test, "(Subtest %d) Expected %d matches but found %d when examining group:'%s' category:'%s'\n",
204                                 i + 1, subtests[i].expected, returned_count, subtests[i].groupmatch, subtests[i].category);
205                         res = AST_TEST_FAIL;
206                         goto exit_group_test;
207                 } else {
208                         ast_test_status_update(test, "(Subtest %d) Found %d matches as expected when examining group:'%s' category:'%s'\n",
209                                 i + 1, subtests[i].expected, subtests[i].groupmatch, subtests[i].category);
210                 }
211         }
212
213 exit_group_test:
214         if (test_channel1) {
215                 ast_hangup(test_channel1);
216         }
217         if (test_channel2) {
218                 ast_hangup(test_channel2);
219         }
220         if (test_channel3) {
221                 ast_hangup(test_channel3);
222         }
223         if (test_channel4) {
224                 ast_hangup(test_channel4);
225         }
226         return res;
227 }
228
229 static int unload_module(void)
230 {
231         AST_TEST_UNREGISTER(app_group);
232         AST_TEST_UNREGISTER(options_parsing);
233         return 0;
234 }
235
236 static int load_module(void)
237 {
238         AST_TEST_REGISTER(app_group);
239         AST_TEST_REGISTER(options_parsing);
240         return AST_MODULE_LOAD_SUCCESS;
241 }
242
243 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "App unit tests");