Fix some compiler errors that popped up after the CCSS merge.
[asterisk/asterisk.git] / tests / test_gosub.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * Tilghman Lesher <tlesher AT digium DOT 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 Gosub tests
22  *
23  * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
24  *
25  * \ingroup tests
26  */
27
28 /*** MODULEINFO
29         <depend>TEST_FRAMEWORK</depend>
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/pbx.h"
40 #include "asterisk/channel.h"
41
42 AST_TEST_DEFINE(test_gosub)
43 {
44         int res = AST_TEST_PASS, i;
45         struct ast_channel *chan;
46         struct ast_str *str;
47         struct ast_context *con;
48         struct testplan {
49                 const char *app;
50                 const char *args;
51                 const char *expected_value;
52         } testplan[] = {
53                 { "Gosub", "tests_test_gosub_virtual_context,s,1" },
54                 { NULL, "${EXTEN}", "s" },
55                 { "Gosub", "test,dne,1", (const char *) -1 }, /* This is the only invocation that should fail. */
56                 { NULL, "${EXTEN}", "s" },
57                 { "Gosub", "tests_test_gosub_virtual_context,s,1(5,5,5,5,5)" },
58                 { NULL, "$[0${ARG1} + 0${ARG5}]", "10" },
59                 { "Gosub", "tests_test_gosub_virtual_context,s,1(4,4,4,4)" },
60                 { NULL, "$[0${ARG1} + 0${ARG5}]", "4" },
61                 { NULL, "$[0${ARG1} + 0${ARG4}]", "8" },
62                 { "Gosub", "tests_test_gosub_virtual_context,s,1(3,3,3)" },
63                 { NULL, "$[0${ARG1} + 0${ARG4}]", "3" },
64                 { NULL, "$[0${ARG1} + 0${ARG3}]", "6" },
65                 { "Gosub", "tests_test_gosub_virtual_context,s,1(2,2)" },
66                 { NULL, "$[0${ARG1} + 0${ARG3}]", "2" },
67                 { NULL, "$[0${ARG1} + 0${ARG2}]", "4" },
68                 { "Gosub", "tests_test_gosub_virtual_context,s,1(1)" },
69                 { NULL, "$[0${ARG1} + 0${ARG2}]", "1" },
70                 { NULL, "$[0${ARG1} + 0${ARG1}]", "2" },
71                 { "Gosub", "tests_test_gosub_virtual_context,s,1" },
72                 { NULL, "$[0${ARG1} + 0${ARG1}]", "0" }, /* All arguments are correctly masked */
73                 { "Set", "LOCAL(foo)=5" },
74                 { NULL, "${foo}", "5" },                 /* LOCAL() set a variable correctly */
75                 { NULL, "${LOCAL_PEEK(0,ARG1)}", "" },   /* LOCAL_PEEK() arguments work correctly */
76                 { NULL, "${LOCAL_PEEK(4,ARG1)}", "4" },  /* LOCAL_PEEK() arguments work correctly */
77                 { NULL, "$[0${LOCAL_PEEK(3,ARG1)} + 0${LOCAL_PEEK(5,ARG1)}]", "8" },
78                 { "StackPop", "" },
79                 { NULL, "${foo}", "" },                  /* StackPop removed the variable set with LOCAL() */
80                 { "Return", "7" },
81                 { NULL, "${GOSUB_RETVAL}", "7" },              /* Return sets a return value correctly */
82                 { NULL, "$[0${GOSUB_RETVAL} + 0${ARG1}]", "9" }, /* Two frames less means ARG1 should have 2 */
83         };
84
85         switch (cmd) {
86         case TEST_INIT:
87                 info->name = "gosub application";
88                 info->category = "apps/app_gosub/";
89                 info->summary = "Verify functionality of gosub application";
90                 info->description =
91                         "Verify functionality of gosub application";
92                 return AST_TEST_NOT_RUN;
93         case TEST_EXECUTE:
94                 break;
95         }
96
97         if (!(chan = ast_dummy_channel_alloc())) {
98                 ast_test_status_update(test, "Unable to allocate dummy channel\n");
99                 return AST_TEST_FAIL;
100         }
101
102         if (!(str = ast_str_create(16))) {
103                 ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");
104                 ast_channel_release(chan);
105                 return AST_TEST_FAIL;
106         }
107
108         /* Create our test dialplan */
109         if (!(con = ast_context_find_or_create(NULL, NULL, "tests_test_gosub_virtual_context", "test_gosub"))) {
110                 ast_test_status_update(test, "Unable to create test dialplan context");
111                 ast_free(str);
112                 ast_channel_release(chan);
113                 return AST_TEST_FAIL;
114         }
115
116         ast_add_extension2(con, 1, "s", 1, NULL, NULL, "NoOp", ast_strdup(""), ast_free_ptr, "test_gosub");
117
118         for (i = 0; i < ARRAY_LEN(testplan); i++) {
119                 if (testplan[i].app == NULL) {
120                         /* Evaluation */
121                         ast_str_substitute_variables(&str, 0, chan, testplan[i].args);
122                         if (strcmp(ast_str_buffer(str), testplan[i].expected_value)) {
123                                 ast_test_status_update(test, "Evaluation of '%s' returned '%s' instead of the expected value '%s'\n",
124                                         testplan[i].args, ast_str_buffer(str), testplan[i].expected_value);
125                                 res = AST_TEST_FAIL;
126                         }
127                 } else {
128                         /* Run application */
129                         intptr_t exec_res;
130                         struct ast_app *app = pbx_findapp(testplan[i].app);
131                         if (!app) {
132                                 ast_test_status_update(test, "Could not find '%s' in application listing!\n", testplan[i].app);
133                                 res = AST_TEST_FAIL;
134                                 break;
135                         }
136
137                         if ((exec_res = pbx_exec(chan, app, testplan[i].args)) && ((const char *) exec_res != testplan[i].expected_value)) {
138                                 ast_test_status_update(test, "Application '%s' exited abnormally (with code %d)\n", testplan[i].app, (int) exec_res);
139                                 res = AST_TEST_FAIL;
140                                 break;
141                         }
142                 }
143         }
144
145         ast_free(str);
146         ast_channel_release(chan);
147         ast_context_remove_extension2(con, "s", 1, NULL, 0);
148         ast_context_destroy(con, "test_gosub");
149
150         return res;
151 }
152
153 static int unload_module(void)
154 {
155         AST_TEST_UNREGISTER(test_gosub);
156         return 0;
157 }
158
159 static int load_module(void)
160 {
161         AST_TEST_REGISTER(test_gosub);
162         return AST_MODULE_LOAD_SUCCESS;
163 }
164
165 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gosub Tests");