Fix bamboo compile error by calculating an integer with the same size as a pointer.
[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
41 #if SIZEOF_INT == SIZEOF_CHAR_P
42 #define compat_int      int
43 #elif SIZEOF_LONG == SIZEOF_CHAR_P
44 #define compat_int      long
45 #elif SIZEOF_LONG_LONG == SIZEOF_CHAR_P
46 #define compat_int      long long
47 #else
48 #error "Cannot find an integer type the same size as a pointer"
49 #endif
50
51 AST_TEST_DEFINE(test_gosub)
52 {
53         int res = AST_TEST_PASS, i;
54         struct ast_channel *chan;
55         struct ast_str *str;
56         struct ast_context *con;
57         struct testplan {
58                 const char *app;
59                 const char *args;
60                 const char *expected_value;
61         } testplan[] = {
62                 { "Gosub", "tests_test_gosub_virtual_context,s,1" },
63                 { NULL, "${EXTEN}", "s" },
64                 { "Gosub", "test,dne,1", (const char *) -1 }, /* This is the only invocation that should fail. */
65                 { NULL, "${EXTEN}", "s" },
66                 { "Gosub", "tests_test_gosub_virtual_context,s,1(5,5,5,5,5)" },
67                 { NULL, "$[0${ARG1} + 0${ARG5}]", "10" },
68                 { "Gosub", "tests_test_gosub_virtual_context,s,1(4,4,4,4)" },
69                 { NULL, "$[0${ARG1} + 0${ARG5}]", "4" },
70                 { NULL, "$[0${ARG1} + 0${ARG4}]", "8" },
71                 { "Gosub", "tests_test_gosub_virtual_context,s,1(3,3,3)" },
72                 { NULL, "$[0${ARG1} + 0${ARG4}]", "3" },
73                 { NULL, "$[0${ARG1} + 0${ARG3}]", "6" },
74                 { "Gosub", "tests_test_gosub_virtual_context,s,1(2,2)" },
75                 { NULL, "$[0${ARG1} + 0${ARG3}]", "2" },
76                 { NULL, "$[0${ARG1} + 0${ARG2}]", "4" },
77                 { "Gosub", "tests_test_gosub_virtual_context,s,1(1)" },
78                 { NULL, "$[0${ARG1} + 0${ARG2}]", "1" },
79                 { NULL, "$[0${ARG1} + 0${ARG1}]", "2" },
80                 { "Gosub", "tests_test_gosub_virtual_context,s,1" },
81                 { NULL, "$[0${ARG1} + 0${ARG1}]", "0" }, /* All arguments are correctly masked */
82                 { "Set", "LOCAL(foo)=5" },
83                 { NULL, "${foo}", "5" },                 /* LOCAL() set a variable correctly */
84                 { NULL, "${LOCAL_PEEK(0,ARG1)}", "" },   /* LOCAL_PEEK() arguments work correctly */
85                 { NULL, "${LOCAL_PEEK(4,ARG1)}", "4" },  /* LOCAL_PEEK() arguments work correctly */
86                 { NULL, "$[0${LOCAL_PEEK(3,ARG1)} + 0${LOCAL_PEEK(5,ARG1)}]", "8" },
87                 { "StackPop", "" },
88                 { NULL, "${foo}", "" },                  /* StackPop removed the variable set with LOCAL() */
89                 { "Return", "7" },
90                 { NULL, "${GOSUB_RETVAL}", "7" },              /* Return sets a return value correctly */
91                 { NULL, "$[0${GOSUB_RETVAL} + 0${ARG1}]", "9" }, /* Two frames less means ARG1 should have 2 */
92         };
93
94         switch (cmd) {
95         case TEST_INIT:
96                 info->name = "gosub application";
97                 info->category = "apps/app_gosub/";
98                 info->summary = "Verify functionality of gosub application";
99                 info->description =
100                         "Verify functionality of gosub application";
101                 return AST_TEST_NOT_RUN;
102         case TEST_EXECUTE:
103                 break;
104         }
105
106         if (!(chan = ast_dummy_channel_alloc())) {
107                 ast_test_status_update(test, "Unable to allocate dummy channel\n");
108                 return AST_TEST_FAIL;
109         }
110
111         if (!(str = ast_str_create(16))) {
112                 ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");
113                 ast_channel_release(chan);
114                 return AST_TEST_FAIL;
115         }
116
117         /* Create our test dialplan */
118         if (!(con = ast_context_find_or_create(NULL, NULL, "tests_test_gosub_virtual_context", "test_gosub"))) {
119                 ast_test_status_update(test, "Unable to create test dialplan context");
120                 ast_free(str);
121                 ast_channel_release(chan);
122                 return AST_TEST_FAIL;
123         }
124
125         ast_add_extension2(con, 1, "s", 1, NULL, NULL, "NoOp", ast_strdup(""), ast_free_ptr, "test_gosub");
126
127         for (i = 0; i < ARRAY_LEN(testplan); i++) {
128                 if (testplan[i].app == NULL) {
129                         /* Evaluation */
130                         ast_str_substitute_variables(&str, 0, chan, testplan[i].args);
131                         if (strcmp(ast_str_buffer(str), testplan[i].expected_value)) {
132                                 ast_test_status_update(test, "Evaluation of '%s' returned '%s' instead of the expected value '%s'\n",
133                                         testplan[i].args, ast_str_buffer(str), testplan[i].expected_value);
134                                 res = AST_TEST_FAIL;
135                         }
136                 } else {
137                         /* Run application */
138                         compat_int exec_res;
139                         struct ast_app *app = pbx_findapp(testplan[i].app);
140                         if (!app) {
141                                 ast_test_status_update(test, "Could not find '%s' in application listing!\n", testplan[i].app);
142                                 res = AST_TEST_FAIL;
143                                 break;
144                         }
145
146                         if ((exec_res = pbx_exec(chan, app, testplan[i].args)) && ((const char *) exec_res != testplan[i].expected_value)) {
147                                 ast_test_status_update(test, "Application '%s' exited abnormally (with code %d)\n", testplan[i].app, exec_res);
148                                 res = AST_TEST_FAIL;
149                                 break;
150                         }
151                 }
152         }
153
154         ast_free(str);
155         ast_channel_release(chan);
156         ast_context_remove_extension2(con, "s", 1, NULL, 0);
157         ast_context_destroy(con, "test_gosub");
158
159         return res;
160 }
161
162 static int unload_module(void)
163 {
164         AST_TEST_UNREGISTER(test_gosub);
165         return 0;
166 }
167
168 static int load_module(void)
169 {
170         AST_TEST_REGISTER(test_gosub);
171         return AST_MODULE_LOAD_SUCCESS;
172 }
173
174 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gosub Tests");