ece63a4c191835164252aac7b6e2f61a8665edec
[asterisk/asterisk.git] / tests / test_utils.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * David Vossel <dvossel@digium.com>
7  * Russell Bryant <russell@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*!
21  * \file
22  * \brief Unit Tests for utils API
23  *
24  * \author David Vossel <dvossel@digium.com>
25  * \author Russell Bryant <russell@digium.com>
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/test.h"
39 #include "asterisk/crypto.h"
40 #include "asterisk/adsi.h"
41 #include "asterisk/agi.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/module.h"
44
45 AST_TEST_DEFINE(uri_encode_decode_test)
46 {
47         int res = AST_TEST_PASS;
48         const char *in = "abcdefghijklmnopurstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
49         char out[256] = { 0 };
50         char small[4] = { 0 };
51         const struct ast_flags none = {0};
52         int i = 0;
53
54         static struct {
55                 const char *spec_str;
56                 struct ast_flags spec;
57
58                 char *buf;
59                 size_t buflen;
60
61                 const char *input;
62                 const char *output;
63                 const char *decoded_output;
64         } tests[5];
65
66 #define INIT_ENCODE_TEST(s, buffer, in, out, dec_out) do { \
67         if (i < ARRAY_LEN(tests)) { \
68                 tests[i].spec_str = #s; \
69                 tests[i].spec = s; \
70                 tests[i].buf = buffer; \
71                 tests[i].buflen = sizeof(buffer); \
72                 tests[i].input = in; \
73                 tests[i].output = out; \
74                 tests[i].decoded_output = dec_out; \
75                 i++; \
76         } else { \
77                         ast_test_status_update(test, "error: 'tests' array too small\n"); \
78                         res = AST_TEST_FAIL; \
79         } \
80         } while (0)
81
82         INIT_ENCODE_TEST(ast_uri_http, out, in,
83                 "abcdefghijklmnopurstuvwxyz%20ABCDEFGHIJKLMNOPQRSTUVWXYZ%201234567890%20~%60!%40%23%24%25%5E%26*()_-%2B%3D%7B%5B%7D%5D%7C%5C%3A%3B%22'%3C%2C%3E.%3F%2F", in);
84         INIT_ENCODE_TEST(ast_uri_http_legacy, out, in,
85                 "abcdefghijklmnopurstuvwxyz+ABCDEFGHIJKLMNOPQRSTUVWXYZ+1234567890+~%60!%40%23%24%25%5E%26*()_-%2B%3D%7B%5B%7D%5D%7C%5C%3A%3B%22'%3C%2C%3E.%3F%2F", in);
86         INIT_ENCODE_TEST(ast_uri_sip_user, out, in,
87                 "abcdefghijklmnopurstuvwxyz%20ABCDEFGHIJKLMNOPQRSTUVWXYZ%201234567890%20~%60!%40%23$%25%5E&*()_-+=%7B%5B%7D%5D%7C%5C%3A;%22'%3C,%3E.?/", in);
88         INIT_ENCODE_TEST(none, small, in, "%61", "a");
89         INIT_ENCODE_TEST(ast_uri_http, small, in, "abc", "abc");
90
91         switch (cmd) {
92         case TEST_INIT:
93                 info->name = "uri_encode_decode_test";
94                 info->category = "/main/utils/";
95                 info->summary = "encode and decode a hex escaped string";
96                 info->description = "encode a string, verify encoded string matches what we expect.  Decode the encoded string, verify decoded string matches the original string.";
97                 return AST_TEST_NOT_RUN;
98         case TEST_EXECUTE:
99                 break;
100         }
101
102         for (i = 0; i < ARRAY_LEN(tests); i++) {
103                 ast_uri_encode(tests[i].input, tests[i].buf, tests[i].buflen, tests[i].spec);
104                 if (strcmp(tests[i].output, tests[i].buf)) {
105                         ast_test_status_update(test, "encoding with %s did not match expected output, FAIL\n", tests[i].spec_str);
106                         ast_test_status_update(test, "original: %s\n", tests[i].input);
107                         ast_test_status_update(test, "expected: %s\n", tests[i].output);
108                         ast_test_status_update(test, "result: %s\n", tests[i].buf);
109                         res = AST_TEST_FAIL;
110                         continue;
111                 }
112
113                 ast_uri_decode(tests[i].buf, tests[i].spec);
114                 if (strcmp(tests[i].decoded_output, tests[i].buf)) {
115                         ast_test_status_update(test, "decoding with %s did not match the original input (or expected decoded output)\n", tests[i].spec_str);
116                         ast_test_status_update(test, "original: %s\n", tests[i].input);
117                         ast_test_status_update(test, "expected: %s\n", tests[i].decoded_output);
118                         ast_test_status_update(test, "decoded: %s\n", tests[i].buf);
119                         res = AST_TEST_FAIL;
120                 }
121         }
122
123         return res;
124 }
125
126 AST_TEST_DEFINE(quoted_escape_test)
127 {
128         int res = AST_TEST_PASS;
129         const char *in = "a\"bcdefg\"hijkl\\mnopqrs tuv\twxyz";
130         char out[256] = { 0 };
131         char small[4] = { 0 };
132         int i;
133
134         static struct {
135                 char *buf;
136                 const size_t buflen;
137
138                 const char *output;
139         } tests[] = {
140                 {0, sizeof(out),
141                         "a\\\"bcdefg\\\"hijkl\\\\mnopqrs tuv\twxyz"},
142                 {0, sizeof(small),
143                         "a\\\""},
144         };
145
146         tests[0].buf = out;
147         tests[1].buf = small;
148
149         switch (cmd) {
150         case TEST_INIT:
151                 info->name = "quoted_escape_test";
152                 info->category = "/main/utils/";
153                 info->summary = "escape a quoted string";
154                 info->description = "Escape a string to be quoted and check the result.";
155                 return AST_TEST_NOT_RUN;
156         case TEST_EXECUTE:
157                 break;
158         }
159
160         for (i = 0; i < ARRAY_LEN(tests); i++) {
161                 ast_escape_quoted(in, tests[i].buf, tests[i].buflen);
162                 if (strcmp(tests[i].output, tests[i].buf)) {
163                         ast_test_status_update(test, "ESCAPED DOES NOT MATCH EXPECTED, FAIL\n");
164                         ast_test_status_update(test, "original: %s\n", in);
165                         ast_test_status_update(test, "expected: %s\n", tests[i].output);
166                         ast_test_status_update(test, "result: %s\n", tests[i].buf);
167                         res = AST_TEST_FAIL;
168                 }
169         }
170
171         return res;
172 }
173
174 AST_TEST_DEFINE(md5_test)
175 {
176         static const struct {
177                 const char *input;
178                 const char *expected_output;
179         } tests[] = {
180                 { "apples",                          "daeccf0ad3c1fc8c8015205c332f5b42" },
181                 { "bananas",                         "ec121ff80513ae58ed478d5c5787075b" },
182                 { "reallylongstringaboutgoatcheese", "0a2d9280d37e2e37545cfef6e7e4e890" },
183         };
184         enum ast_test_result_state res = AST_TEST_PASS;
185         int i;
186
187         switch (cmd) {
188         case TEST_INIT:
189                 info->name = "md5_test";
190                 info->category = "/main/utils/";
191                 info->summary = "MD5 test";
192                 info->description =
193                         "This test exercises MD5 calculations."
194                         "";
195                 return AST_TEST_NOT_RUN;
196         case TEST_EXECUTE:
197                 break;
198         }
199
200         ast_test_status_update(test, "Testing MD5 ...\n");
201
202         for (i = 0; i < ARRAY_LEN(tests); i++) {
203                 char md5_hash[32];
204                 ast_md5_hash(md5_hash, tests[i].input);
205                 if (strcasecmp(md5_hash, tests[i].expected_output)) {
206                         ast_test_status_update(test,
207                                         "input: '%s'  hash: '%s'  expected hash: '%s'\n",
208                                         tests[i].input, md5_hash, tests[i].expected_output);
209                         res = AST_TEST_FAIL;
210                 }
211         }
212
213         return res;
214 }
215
216 AST_TEST_DEFINE(sha1_test)
217 {
218         static const struct {
219                 const char *input;
220                 const char *expected_output;
221         } tests[] = {
222                 { "giraffe",
223                         "fac8f1a31d2998734d6a5253e49876b8e6a08239" },
224                 { "platypus",
225                         "1dfb21b7a4d35e90d943e3a16107ccbfabd064d5" },
226                 { "ParastratiosphecomyiaStratiosphecomyioides",
227                         "58af4e8438676f2bd3c4d8df9e00ee7fe06945bb" },
228         };
229         enum ast_test_result_state res = AST_TEST_PASS;
230         int i;
231
232         switch (cmd) {
233         case TEST_INIT:
234                 info->name = "sha1_test";
235                 info->category = "/main/utils/";
236                 info->summary = "SHA1 test";
237                 info->description =
238                         "This test exercises SHA1 calculations."
239                         "";
240                 return AST_TEST_NOT_RUN;
241         case TEST_EXECUTE:
242                 break;
243         }
244
245         ast_test_status_update(test, "Testing SHA1 ...\n");
246
247         for (i = 0; i < ARRAY_LEN(tests); i++) {
248                 char sha1_hash[64];
249                 ast_sha1_hash(sha1_hash, tests[i].input);
250                 if (strcasecmp(sha1_hash, tests[i].expected_output)) {
251                         ast_test_status_update(test,
252                                         "input: '%s'  hash: '%s'  expected hash: '%s'\n",
253                                         tests[i].input, sha1_hash, tests[i].expected_output);
254                         res = AST_TEST_FAIL;
255                 }
256         }
257
258         return res;
259 }
260
261 AST_TEST_DEFINE(base64_test)
262 {
263         static const struct {
264                 const char *input;
265                 const char *decoded;
266         } tests[] = {
267                 { "giraffe",
268                         "Z2lyYWZmZQ==" },
269                 { "platypus",
270                         "cGxhdHlwdXM=" },
271                 { "ParastratiosphecomyiaStratiosphecomyioides",
272                         "UGFyYXN0cmF0aW9zcGhlY29teWlhU3RyYXRpb3NwaGVjb215aW9pZGVz" },
273         };
274         int i;
275         enum ast_test_result_state res = AST_TEST_PASS;
276
277         switch (cmd) {
278         case TEST_INIT:
279                 info->name = "base64_test";
280                 info->category = "/main/utils/";
281                 info->summary = "base64 test";
282                 info->description = "This test exercises the base64 conversions.";
283                 return AST_TEST_NOT_RUN;
284         case TEST_EXECUTE:
285                 break;
286         }
287
288
289         for (i = 0; i < ARRAY_LEN(tests); i++) {
290                 char tmp[64];
291                 ast_base64encode(tmp, (unsigned char *)tests[i].input, strlen(tests[i].input), sizeof(tmp));
292                 if (strcasecmp(tmp, tests[i].decoded)) {
293                         ast_test_status_update(test,
294                                         "input: '%s'  base64 output: '%s'  expected base64 output: '%s'\n",
295                                         tests[i].input, tmp, tests[i].decoded);
296                         res = AST_TEST_FAIL;
297                 }
298
299                 memset(tmp, 0, sizeof(tmp));
300                 ast_base64decode((unsigned char *) tmp, tests[i].decoded, (sizeof(tmp) - 1));
301                 if (strcasecmp(tmp, tests[i].input)) {
302                         ast_test_status_update(test,
303                                         "base64 input: '%s'  output: '%s'  expected output: '%s'\n",
304                                         tests[i].decoded, tmp, tests[i].input);
305                         res = AST_TEST_FAIL;
306                 }
307         }
308
309         return res;
310 }
311
312 AST_TEST_DEFINE(crypto_loaded_test)
313 {
314         switch (cmd) {
315         case TEST_INIT:
316                 info->name = "crypto_loaded_test";
317                 info->category = "/res/crypto/";
318                 info->summary = "Crypto loaded into memory";
319                 info->description = "Verifies whether the crypto functions overrode the stubs";
320                 return AST_TEST_NOT_RUN;
321         case TEST_EXECUTE:
322                 break;
323         }
324
325 #if 0 /* Not defined on Solaris */
326         ast_test_status_update(test,
327                                "address of __stub__ast_crypto_loaded is %p\n",
328                                __stub__ast_crypto_loaded);
329 #ifndef HAVE_ATTRIBUTE_weak_import
330         ast_test_status_update(test,
331                                "address of __ref__ast_crypto_loaded is %p\n",
332                                __ref__ast_crypto_loaded);
333 #endif
334         ast_test_status_update(test,
335                                "pointer to ast_crypto_loaded is %p\n",
336                                ast_crypto_loaded);
337 #endif
338
339         return ast_crypto_loaded() ? AST_TEST_PASS : AST_TEST_FAIL;
340 }
341
342 AST_TEST_DEFINE(adsi_loaded_test)
343 {
344         struct ast_channel *c;
345         int res;
346         switch (cmd) {
347         case TEST_INIT:
348                 info->name = "adsi_loaded_test";
349                 info->category = "/res/adsi/";
350                 info->summary = "ADSI loaded into memory";
351                 info->description = "Verifies whether the adsi functions overrode the stubs";
352                 return AST_TEST_NOT_RUN;
353         case TEST_EXECUTE:
354                 break;
355         }
356
357         if (!(c = ast_dummy_channel_alloc())) {
358                 return AST_TEST_FAIL;
359         }
360         ast_channel_adsicpe_set(c, AST_ADSI_AVAILABLE);
361         res = ast_adsi_available(c) ? AST_TEST_PASS : AST_TEST_FAIL;
362         c = ast_channel_unref(c);
363         return res;
364 }
365
366 static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, const char * const argv[])
367 {
368         ast_agi_send(agi->fd, chan, "200 result=0\n");
369         return RESULT_SUCCESS;
370 }
371
372 AST_TEST_DEFINE(agi_loaded_test)
373 {
374         int res = AST_TEST_PASS;
375         struct agi_command noop_command =
376                 { { "testnoop", NULL }, handle_noop, NULL, NULL, 0 };
377
378         switch (cmd) {
379         case TEST_INIT:
380                 info->name = "agi_loaded_test";
381                 info->category = "/res/agi/";
382                 info->summary = "AGI loaded into memory";
383                 info->description = "Verifies whether the agi functions overrode the stubs";
384                 return AST_TEST_NOT_RUN;
385         case TEST_EXECUTE:
386                 break;
387         }
388
389 #if 0
390         ast_test_status_update(test,
391                                "address of __stub__ast_agi_register is %p\n",
392                                __stub__ast_agi_register);
393 #ifndef HAVE_ATTRIBUTE_weak_import
394         ast_test_status_update(test,
395                                "address of __ref__ast_agi_register is %p\n",
396                                __ref__ast_agi_register);
397 #endif
398         ast_test_status_update(test,
399                                "pointer to ast_agi_register is %p\n",
400                                ast_agi_register);
401 #endif
402
403         if (ast_agi_register(ast_module_info->self, &noop_command) == AST_OPTIONAL_API_UNAVAILABLE) {
404                 ast_test_status_update(test, "Unable to register testnoop command, because res_agi is not loaded.\n");
405                 return AST_TEST_FAIL;
406         }
407
408 #ifndef HAVE_NULLSAFE_PRINTF
409         /* Test for condition without actually crashing Asterisk */
410         if (noop_command.usage == NULL) {
411                 ast_test_status_update(test, "AGI testnoop usage was not updated properly.\n");
412                 res = AST_TEST_FAIL;
413         }
414         if (noop_command.syntax == NULL) {
415                 ast_test_status_update(test, "AGI testnoop syntax was not updated properly.\n");
416                 res = AST_TEST_FAIL;
417         }
418 #endif
419
420         ast_agi_unregister(ast_module_info->self, &noop_command);
421         return res;
422 }
423
424 static int unload_module(void)
425 {
426         AST_TEST_UNREGISTER(uri_encode_decode_test);
427         AST_TEST_UNREGISTER(quoted_escape_test);
428         AST_TEST_UNREGISTER(md5_test);
429         AST_TEST_UNREGISTER(sha1_test);
430         AST_TEST_UNREGISTER(base64_test);
431         AST_TEST_UNREGISTER(crypto_loaded_test);
432         AST_TEST_UNREGISTER(adsi_loaded_test);
433         AST_TEST_UNREGISTER(agi_loaded_test);
434         return 0;
435 }
436
437 static int load_module(void)
438 {
439         AST_TEST_REGISTER(uri_encode_decode_test);
440         AST_TEST_REGISTER(quoted_escape_test);
441         AST_TEST_REGISTER(md5_test);
442         AST_TEST_REGISTER(sha1_test);
443         AST_TEST_REGISTER(base64_test);
444         AST_TEST_REGISTER(crypto_loaded_test);
445         AST_TEST_REGISTER(adsi_loaded_test);
446         AST_TEST_REGISTER(agi_loaded_test);
447         return AST_MODULE_LOAD_SUCCESS;
448 }
449
450 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Utils test module");