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