base64 unit test
[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  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
35
36 #include "asterisk/utils.h"
37 #include "asterisk/test.h"
38 #include "asterisk/module.h"
39
40 AST_TEST_DEFINE(uri_encode_decode_test)
41 {
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 };
47
48         switch (cmd) {
49         case TEST_INIT:
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;
55         case TEST_EXECUTE:
56                 break;
57         }
58
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);
62
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");
68                 res = AST_TEST_FAIL;
69         }
70
71         /* Verify uri decode matches original */
72         ast_uri_decode(out);
73         if (strcmp(in, out)) {
74                 ast_test_status_update(test, "Decoded string did not match original input\n");
75                 res = AST_TEST_FAIL;
76         } else {
77                 ast_test_status_update(test, "Decoded string matched original input\n");
78         }
79
80         /* Test with do_special_char disabled */
81         out[0] = '\0';
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");
86                 res = AST_TEST_FAIL;
87         }
88
89         /* Verify uri decode matches original */
90         ast_uri_decode(out);
91         if (strcmp(in, out)) {
92                 ast_test_status_update(test, "Decoded string did not match original input\n");
93                 res = AST_TEST_FAIL;
94         } else {
95                 ast_test_status_update(test, "Decoded string matched original input\n");
96         }
97
98         return res;
99 }
100
101 AST_TEST_DEFINE(md5_test)
102 {
103         static const struct {
104                 const char *input;
105                 const char *expected_output;
106         } tests[] = {
107                 { "apples",                          "daeccf0ad3c1fc8c8015205c332f5b42" },
108                 { "bananas",                         "ec121ff80513ae58ed478d5c5787075b" },
109                 { "reallylongstringaboutgoatcheese", "0a2d9280d37e2e37545cfef6e7e4e890" },
110         };
111         enum ast_test_result_state res = AST_TEST_PASS;
112         int i;
113
114         switch (cmd) {
115         case TEST_INIT:
116                 info->name = "md5_test";
117                 info->category = "main/utils/";
118                 info->summary = "MD5 test";
119                 info->description =
120                         "This test exercises MD5 calculations."
121                         "";
122                 return AST_TEST_NOT_RUN;
123         case TEST_EXECUTE:
124                 break;
125         }
126
127         ast_test_status_update(test, "Testing MD5 ...\n");
128
129         for (i = 0; i < ARRAY_LEN(tests); i++) {
130                 char md5_hash[32];
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);
136                         res = AST_TEST_FAIL;
137                 }
138         }
139
140         return res;
141 }
142
143 AST_TEST_DEFINE(sha1_test)
144 {
145         static const struct {
146                 const char *input;
147                 const char *expected_output;
148         } tests[] = {
149                 { "giraffe",
150                         "fac8f1a31d2998734d6a5253e49876b8e6a08239" },
151                 { "platypus",
152                         "1dfb21b7a4d35e90d943e3a16107ccbfabd064d5" },
153                 { "ParastratiosphecomyiaStratiosphecomyioides",
154                         "58af4e8438676f2bd3c4d8df9e00ee7fe06945bb" },
155         };
156         enum ast_test_result_state res = AST_TEST_PASS;
157         int i;
158
159         switch (cmd) {
160         case TEST_INIT:
161                 info->name = "sha1_test";
162                 info->category = "main/utils/";
163                 info->summary = "SHA1 test";
164                 info->description =
165                         "This test exercises SHA1 calculations."
166                         "";
167                 return AST_TEST_NOT_RUN;
168         case TEST_EXECUTE:
169                 break;
170         }
171
172         ast_test_status_update(test, "Testing SHA1 ...\n");
173
174         for (i = 0; i < ARRAY_LEN(tests); i++) {
175                 char sha1_hash[64];
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);
181                         res = AST_TEST_FAIL;
182                 }
183         }
184
185         return res;
186 }
187
188 AST_TEST_DEFINE(base64_test)
189 {
190         static const struct {
191                 const char *input;
192                 const char *decoded;
193         } tests[] = {
194                 { "giraffe",
195                         "Z2lyYWZmZQ==" },
196                 { "platypus",
197                         "cGxhdHlwdXM=" },
198                 { "ParastratiosphecomyiaStratiosphecomyioides",
199                         "UGFyYXN0cmF0aW9zcGhlY29teWlhU3RyYXRpb3NwaGVjb215aW9pZGVz" },
200         };
201         int i;
202         enum ast_test_result_state res = AST_TEST_PASS;
203
204         switch (cmd) {
205         case TEST_INIT:
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;
211         case TEST_EXECUTE:
212                 break;
213         }
214
215
216         for (i = 0; i < ARRAY_LEN(tests); i++) {
217                 char tmp[64];
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);
223                         res = AST_TEST_FAIL;
224                 }
225
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);
232                         res = AST_TEST_FAIL;
233                 }
234         }
235
236         return res;
237 }
238
239
240 static int unload_module(void)
241 {
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);
246         return 0;
247 }
248
249 static int load_module(void)
250 {
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;
256 }
257
258 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Utils test module");