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