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