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