2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2010, Digium, Inc.
6 * David Vossel <dvossel@digium.com>
7 * Russell Bryant <russell@digium.com>
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.
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.
22 * \brief Unit Tests for utils API
24 * \author David Vossel <dvossel@digium.com>
25 * \author Russell Bryant <russell@digium.com>
29 <depend>TEST_FRAMEWORK</depend>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
36 #include "asterisk/utils.h"
37 #include "asterisk/test.h"
38 #include "asterisk/module.h"
40 AST_TEST_DEFINE(uri_encode_decode_test)
42 int res = AST_TEST_PASS;
43 const char *in = "abcdefghijklmnopurstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
44 const char *expected1 = "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";
45 const char *expected2 = "abcdefghijklmnopurstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ~`!@#$%25^&*()_-+={[}]|\\:;\"'<,>.?/";
46 char out[256] = { 0 };
50 info->name = "uri_encode_decode_test";
51 info->category = "main/utils/";
52 info->summary = "encode and decode a hex escaped string";
53 info->description = "encode a string, verify encoded string matches what we expect. Decode the encoded string, verify decoded string matches the original string.";
54 return AST_TEST_NOT_RUN;
59 ast_test_status_update(test, "Input before executing ast_uri_encode:\n%s\n", in);
60 ast_test_status_update(test, "Output expected for ast_uri_encode with enabling do_special_char: %s\n", expected1);
61 ast_test_status_update(test, "Output expected for ast_uri_encode with out enabling do_special_char: %s\n\n", expected2);
63 /* Test with do_special_char enabled */
64 ast_uri_encode(in, out, sizeof(out), 1);
65 ast_test_status_update(test, "Output after enabling do_special_char:\n%s\n", out);
66 if (strcmp(expected1, out)) {
67 ast_test_status_update(test, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n");
71 /* Verify uri decode matches original */
73 if (strcmp(in, out)) {
74 ast_test_status_update(test, "Decoded string did not match original input\n");
77 ast_test_status_update(test, "Decoded string matched original input\n");
80 /* Test with do_special_char disabled */
82 ast_uri_encode(in, out, sizeof(out), 0);
83 ast_test_status_update(test, "Output after disabling do_special_char: %s\n", out);
84 if (strcmp(expected2, out)) {
85 ast_test_status_update(test, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n");
89 /* Verify uri decode matches original */
91 if (strcmp(in, out)) {
92 ast_test_status_update(test, "Decoded string did not match original input\n");
95 ast_test_status_update(test, "Decoded string matched original input\n");
101 AST_TEST_DEFINE(md5_test)
103 static const struct {
105 const char *expected_output;
107 { "apples", "daeccf0ad3c1fc8c8015205c332f5b42" },
108 { "bananas", "ec121ff80513ae58ed478d5c5787075b" },
109 { "reallylongstringaboutgoatcheese", "0a2d9280d37e2e37545cfef6e7e4e890" },
111 enum ast_test_result_state res = AST_TEST_PASS;
116 info->name = "md5_test";
117 info->category = "main/utils/";
118 info->summary = "MD5 test";
120 "This test exercises MD5 calculations."
122 return AST_TEST_NOT_RUN;
127 ast_test_status_update(test, "Testing MD5 ...\n");
129 for (i = 0; i < ARRAY_LEN(tests); i++) {
131 ast_md5_hash(md5_hash, tests[i].input);
132 if (strcasecmp(md5_hash, tests[i].expected_output)) {
133 ast_test_status_update(test,
134 "input: '%s' hash: '%s' expected hash: '%s'\n",
135 tests[i].input, md5_hash, tests[i].expected_output);
143 AST_TEST_DEFINE(sha1_test)
145 static const struct {
147 const char *expected_output;
150 "fac8f1a31d2998734d6a5253e49876b8e6a08239" },
152 "1dfb21b7a4d35e90d943e3a16107ccbfabd064d5" },
153 { "ParastratiosphecomyiaStratiosphecomyioides",
154 "58af4e8438676f2bd3c4d8df9e00ee7fe06945bb" },
156 enum ast_test_result_state res = AST_TEST_PASS;
161 info->name = "sha1_test";
162 info->category = "main/utils/";
163 info->summary = "SHA1 test";
165 "This test exercises SHA1 calculations."
167 return AST_TEST_NOT_RUN;
172 ast_test_status_update(test, "Testing SHA1 ...\n");
174 for (i = 0; i < ARRAY_LEN(tests); i++) {
176 ast_sha1_hash(sha1_hash, tests[i].input);
177 if (strcasecmp(sha1_hash, tests[i].expected_output)) {
178 ast_test_status_update(test,
179 "input: '%s' hash: '%s' expected hash: '%s'\n",
180 tests[i].input, sha1_hash, tests[i].expected_output);
188 AST_TEST_DEFINE(base64_test)
190 static const struct {
198 { "ParastratiosphecomyiaStratiosphecomyioides",
199 "UGFyYXN0cmF0aW9zcGhlY29teWlhU3RyYXRpb3NwaGVjb215aW9pZGVz" },
202 enum ast_test_result_state res = AST_TEST_PASS;
206 info->name = "base64_test";
207 info->category = "main/utils/";
208 info->summary = "base64 test";
209 info->description = "This test exercises the base64 conversions.";
210 return AST_TEST_NOT_RUN;
216 for (i = 0; i < ARRAY_LEN(tests); i++) {
218 ast_base64encode(tmp, (unsigned char *)tests[i].input, strlen(tests[i].input), sizeof(tmp));
219 if (strcasecmp(tmp, tests[i].decoded)) {
220 ast_test_status_update(test,
221 "input: '%s' base64 output: '%s' expected base64 output: '%s'\n",
222 tests[i].input, tmp, tests[i].decoded);
226 memset(tmp, 0, sizeof(tmp));
227 ast_base64decode((unsigned char *) tmp, tests[i].decoded, (sizeof(tmp) - 1));
228 if (strcasecmp(tmp, tests[i].input)) {
229 ast_test_status_update(test,
230 "base64 input: '%s' output: '%s' expected output: '%s'\n",
231 tests[i].decoded, tmp, tests[i].input);
240 static int unload_module(void)
242 AST_TEST_UNREGISTER(uri_encode_decode_test);
243 AST_TEST_UNREGISTER(md5_test);
244 AST_TEST_UNREGISTER(sha1_test);
245 AST_TEST_UNREGISTER(base64_test);
249 static int load_module(void)
251 AST_TEST_REGISTER(uri_encode_decode_test);
252 AST_TEST_REGISTER(md5_test);
253 AST_TEST_REGISTER(sha1_test);
254 AST_TEST_REGISTER(base64_test);
255 return AST_MODULE_LOAD_SUCCESS;
258 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Utils test module");