| 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 |
#include "asterisk.h" |
| 37 |
|
| 38 |
#include "asterisk/utils.h" |
| 39 |
#include "asterisk/test.h" |
| 40 |
#include "asterisk/crypto.h" |
| 41 |
#include "asterisk/adsi.h" |
| 42 |
#include "asterisk/agi.h" |
| 43 |
#include "asterisk/channel.h" |
| 44 |
#include "asterisk/module.h" |
| 45 |
|
| 46 |
#include <sys/stat.h> |
| 47 |
|
| 48 |
AST_TEST_DEFINE(uri_encode_decode_test) |
| 49 |
{ |
| 50 |
int res = AST_TEST_PASS; |
| 51 |
const char *in = "abcdefghijklmnopurstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/"; |
| 52 |
char out[256] = { 0 }; |
| 53 |
char small[4] = { 0 }; |
| 54 |
const struct ast_flags none = {0}; |
| 55 |
int i = 0; |
| 56 |
|
| 57 |
static struct { |
| 58 |
const char *spec_str; |
| 59 |
struct ast_flags spec; |
| 60 |
|
| 61 |
char *buf; |
| 62 |
size_t buflen; |
| 63 |
|
| 64 |
const char *input; |
| 65 |
const char *output; |
| 66 |
const char *decoded_output; |
| 67 |
} tests[5]; |
| 68 |
|
| 69 |
#define INIT_ENCODE_TEST(s, buffer, in, out, dec_out) do { \ |
| 70 |
if (i < ARRAY_LEN(tests)) { \ |
| 71 |
tests[i].spec_str = #s; \ |
| 72 |
tests[i].spec = s; \ |
| 73 |
tests[i].buf = buffer; \ |
| 74 |
tests[i].buflen = sizeof(buffer); \ |
| 75 |
tests[i].input = in; \ |
| 76 |
tests[i].output = out; \ |
| 77 |
tests[i].decoded_output = dec_out; \ |
| 78 |
i++; \ |
| 79 |
} else { \ |
| 80 |
ast_test_status_update(test, "error: 'tests' array too small\n"); \ |
| 81 |
res = AST_TEST_FAIL; \ |
| 82 |
} \ |
| 83 |
} while (0) |
| 84 |
|
| 85 |
INIT_ENCODE_TEST(ast_uri_http, out, in, |
| 86 |
"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); |
| 87 |
INIT_ENCODE_TEST(ast_uri_http_legacy, out, in, |
| 88 |
"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); |
| 89 |
INIT_ENCODE_TEST(ast_uri_sip_user, out, in, |
| 90 |
"abcdefghijklmnopurstuvwxyz%20ABCDEFGHIJKLMNOPQRSTUVWXYZ%201234567890%20~%60!%40%23$%25%5E&*()_-+=%7B%5B%7D%5D%7C%5C%3A;%22'%3C,%3E.?/", in); |
| 91 |
INIT_ENCODE_TEST(none, small, in, "%61", "a"); |
| 92 |
INIT_ENCODE_TEST(ast_uri_http, small, in, "abc", "abc"); |
| 93 |
|
| 94 |
switch (cmd) { |
| 95 |
case TEST_INIT: |
| 96 |
info->name = "uri_encode_decode_test"; |
| 97 |
info->category = "/main/utils/"; |
| 98 |
info->summary = "encode and decode a hex escaped string"; |
| 99 |
info->description = "encode a string, verify encoded string matches what we expect. Decode the encoded string, verify decoded string matches the original string."; |
| 100 |
return AST_TEST_NOT_RUN; |
| 101 |
case TEST_EXECUTE: |
| 102 |
break; |
| 103 |
} |
| 104 |
|
| 105 |
for (i = 0; i < ARRAY_LEN(tests); i++) { |
| 106 |
ast_uri_encode(tests[i].input, tests[i].buf, tests[i].buflen, tests[i].spec); |
| 107 |
if (strcmp(tests[i].output, tests[i].buf)) { |
| 108 |
ast_test_status_update(test, "encoding with %s did not match expected output, FAIL\n", tests[i].spec_str); |
| 109 |
ast_test_status_update(test, "original: %s\n", tests[i].input); |
| 110 |
ast_test_status_update(test, "expected: %s\n", tests[i].output); |
| 111 |
ast_test_status_update(test, "result: %s\n", tests[i].buf); |
| 112 |
res = AST_TEST_FAIL; |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
ast_uri_decode(tests[i].buf, tests[i].spec); |
| 117 |
if (strcmp(tests[i].decoded_output, tests[i].buf)) { |
| 118 |
ast_test_status_update(test, "decoding with %s did not match the original input (or expected decoded output)\n", tests[i].spec_str); |
| 119 |
ast_test_status_update(test, "original: %s\n", tests[i].input); |
| 120 |
ast_test_status_update(test, "expected: %s\n", tests[i].decoded_output); |
| 121 |
ast_test_status_update(test, "decoded: %s\n", tests[i].buf); |
| 122 |
res = AST_TEST_FAIL; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return res; |
| 127 |
} |
| 128 |
|
| 129 |
AST_TEST_DEFINE(quoted_escape_test) |
| 130 |
{ |
| 131 |
int res = AST_TEST_PASS; |
| 132 |
const char *in = "a\"bcdefg\"hijkl\\mnopqrs tuv\twxyz"; |
| 133 |
int i; |
| 134 |
#define LONG_SIZE 256 |
| 135 |
#define SHORT_SIZE 4 |
| 136 |
|
| 137 |
static struct { |
| 138 |
char *buf; |
| 139 |
const size_t buflen; |
| 140 |
|
| 141 |
const char *output; |
| 142 |
} tests[] = { |
| 143 |
{NULL, LONG_SIZE, |
| 144 |
"a\\\"bcdefg\\\"hijkl\\\\mnopqrs tuv\twxyz"}, |
| 145 |
{NULL, SHORT_SIZE, |
| 146 |
"a\\\""}, |
| 147 |
}; |
| 148 |
|
| 149 |
tests[0].buf = ast_alloca(LONG_SIZE); |
| 150 |
tests[1].buf = ast_alloca(SHORT_SIZE); |
| 151 |
|
| 152 |
switch (cmd) { |
| 153 |
case TEST_INIT: |
| 154 |
info->name = "quoted_escape_test"; |
| 155 |
info->category = "/main/utils/"; |
| 156 |
info->summary = "escape a quoted string"; |
| 157 |
info->description = "Escape a string to be quoted and check the result."; |
| 158 |
return AST_TEST_NOT_RUN; |
| 159 |
case TEST_EXECUTE: |
| 160 |
break; |
| 161 |
} |
| 162 |
|
| 163 |
for (i = 0; i < ARRAY_LEN(tests); i++) { |
| 164 |
ast_escape_quoted(in, tests[i].buf, tests[i].buflen); |
| 165 |
if (strcmp(tests[i].output, tests[i].buf)) { |
| 166 |
ast_test_status_update(test, "ESCAPED DOES NOT MATCH EXPECTED, FAIL\n"); |
| 167 |
ast_test_status_update(test, "original: %s\n", in); |
| 168 |
ast_test_status_update(test, "expected: %s\n", tests[i].output); |
| 169 |
ast_test_status_update(test, "result: %s\n", tests[i].buf); |
| 170 |
res = AST_TEST_FAIL; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
#undef LONG_SIZE |
| 175 |
#undef SHORT_SIZE |
| 176 |
return res; |
| 177 |
} |
| 178 |
|
| 179 |
AST_TEST_DEFINE(md5_test) |
| 180 |
{ |
| 181 |
static const struct { |
| 182 |
const char *input; |
| 183 |
const char *expected_output; |
| 184 |
} tests[] = { |
| 185 |
{ "apples", "daeccf0ad3c1fc8c8015205c332f5b42" }, |
| 186 |
{ "bananas", "ec121ff80513ae58ed478d5c5787075b" }, |
| 187 |
{ "reallylongstringaboutgoatcheese", "0a2d9280d37e2e37545cfef6e7e4e890" }, |
| 188 |
}; |
| 189 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 190 |
int i; |
| 191 |
|
| 192 |
switch (cmd) { |
| 193 |
case TEST_INIT: |
| 194 |
info->name = "md5_test"; |
| 195 |
info->category = "/main/utils/"; |
| 196 |
info->summary = "MD5 test"; |
| 197 |
info->description = |
| 198 |
"This test exercises MD5 calculations." |
| 199 |
""; |
| 200 |
return AST_TEST_NOT_RUN; |
| 201 |
case TEST_EXECUTE: |
| 202 |
break; |
| 203 |
} |
| 204 |
|
| 205 |
ast_test_status_update(test, "Testing MD5 ...\n"); |
| 206 |
|
| 207 |
for (i = 0; i < ARRAY_LEN(tests); i++) { |
| 208 |
char md5_hash[33]; |
| 209 |
ast_md5_hash(md5_hash, tests[i].input); |
| 210 |
if (strcasecmp(md5_hash, tests[i].expected_output)) { |
| 211 |
ast_test_status_update(test, |
| 212 |
"input: '%s' hash: '%s' expected hash: '%s'\n", |
| 213 |
tests[i].input, md5_hash, tests[i].expected_output); |
| 214 |
res = AST_TEST_FAIL; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return res; |
| 219 |
} |
| 220 |
|
| 221 |
AST_TEST_DEFINE(sha1_test) |
| 222 |
{ |
| 223 |
static const struct { |
| 224 |
const char *input; |
| 225 |
const char *expected_output; |
| 226 |
} tests[] = { |
| 227 |
{ "giraffe", |
| 228 |
"fac8f1a31d2998734d6a5253e49876b8e6a08239" }, |
| 229 |
{ "platypus", |
| 230 |
"1dfb21b7a4d35e90d943e3a16107ccbfabd064d5" }, |
| 231 |
{ "ParastratiosphecomyiaStratiosphecomyioides", |
| 232 |
"58af4e8438676f2bd3c4d8df9e00ee7fe06945bb" }, |
| 233 |
}; |
| 234 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 235 |
int i; |
| 236 |
|
| 237 |
switch (cmd) { |
| 238 |
case TEST_INIT: |
| 239 |
info->name = "sha1_test"; |
| 240 |
info->category = "/main/utils/"; |
| 241 |
info->summary = "SHA1 test"; |
| 242 |
info->description = |
| 243 |
"This test exercises SHA1 calculations." |
| 244 |
""; |
| 245 |
return AST_TEST_NOT_RUN; |
| 246 |
case TEST_EXECUTE: |
| 247 |
break; |
| 248 |
} |
| 249 |
|
| 250 |
ast_test_status_update(test, "Testing SHA1 ...\n"); |
| 251 |
|
| 252 |
for (i = 0; i < ARRAY_LEN(tests); i++) { |
| 253 |
char sha1_hash[64]; |
| 254 |
ast_sha1_hash(sha1_hash, tests[i].input); |
| 255 |
if (strcasecmp(sha1_hash, tests[i].expected_output)) { |
| 256 |
ast_test_status_update(test, |
| 257 |
"input: '%s' hash: '%s' expected hash: '%s'\n", |
| 258 |
tests[i].input, sha1_hash, tests[i].expected_output); |
| 259 |
res = AST_TEST_FAIL; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return res; |
| 264 |
} |
| 265 |
|
| 266 |
AST_TEST_DEFINE(base64_test) |
| 267 |
{ |
| 268 |
static const struct { |
| 269 |
const char *input; |
| 270 |
const char *decoded; |
| 271 |
} tests[] = { |
| 272 |
{ "giraffe", |
| 273 |
"Z2lyYWZmZQ==" }, |
| 274 |
{ "platypus", |
| 275 |
"cGxhdHlwdXM=" }, |
| 276 |
{ "ParastratiosphecomyiaStratiosphecomyioides", |
| 277 |
"UGFyYXN0cmF0aW9zcGhlY29teWlhU3RyYXRpb3NwaGVjb215aW9pZGVz" }, |
| 278 |
}; |
| 279 |
int i; |
| 280 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 281 |
|
| 282 |
switch (cmd) { |
| 283 |
case TEST_INIT: |
| 284 |
info->name = "base64_test"; |
| 285 |
info->category = "/main/utils/"; |
| 286 |
info->summary = "base64 test"; |
| 287 |
info->description = "This test exercises the base64 conversions."; |
| 288 |
return AST_TEST_NOT_RUN; |
| 289 |
case TEST_EXECUTE: |
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
for (i = 0; i < ARRAY_LEN(tests); i++) { |
| 295 |
char tmp[64]; |
| 296 |
ast_base64encode(tmp, (unsigned char *)tests[i].input, strlen(tests[i].input), sizeof(tmp)); |
| 297 |
if (strcasecmp(tmp, tests[i].decoded)) { |
| 298 |
ast_test_status_update(test, |
| 299 |
"input: '%s' base64 output: '%s' expected base64 output: '%s'\n", |
| 300 |
tests[i].input, tmp, tests[i].decoded); |
| 301 |
res = AST_TEST_FAIL; |
| 302 |
} |
| 303 |
|
| 304 |
memset(tmp, 0, sizeof(tmp)); |
| 305 |
ast_base64decode((unsigned char *) tmp, tests[i].decoded, (sizeof(tmp) - 1)); |
| 306 |
if (strcasecmp(tmp, tests[i].input)) { |
| 307 |
ast_test_status_update(test, |
| 308 |
"base64 input: '%s' output: '%s' expected output: '%s'\n", |
| 309 |
tests[i].decoded, tmp, tests[i].input); |
| 310 |
res = AST_TEST_FAIL; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
return res; |
| 315 |
} |
| 316 |
|
| 317 |
AST_TEST_DEFINE(crypto_loaded_test) |
| 318 |
{ |
| 319 |
switch (cmd) { |
| 320 |
case TEST_INIT: |
| 321 |
info->name = "crypto_loaded_test"; |
| 322 |
info->category = "/res/crypto/"; |
| 323 |
info->summary = "Crypto loaded into memory"; |
| 324 |
info->description = "Verifies whether the crypto functions overrode the stubs"; |
| 325 |
return AST_TEST_NOT_RUN; |
| 326 |
case TEST_EXECUTE: |
| 327 |
break; |
| 328 |
} |
| 329 |
|
| 330 |
return ast_crypto_loaded() ? AST_TEST_PASS : AST_TEST_FAIL; |
| 331 |
} |
| 332 |
|
| 333 |
AST_TEST_DEFINE(adsi_loaded_test) |
| 334 |
{ |
| 335 |
struct ast_channel *c; |
| 336 |
int res; |
| 337 |
switch (cmd) { |
| 338 |
case TEST_INIT: |
| 339 |
info->name = "adsi_loaded_test"; |
| 340 |
info->category = "/res/adsi/"; |
| 341 |
info->summary = "ADSI loaded into memory"; |
| 342 |
info->description = "Verifies whether the adsi functions overrode the stubs"; |
| 343 |
return AST_TEST_NOT_RUN; |
| 344 |
case TEST_EXECUTE: |
| 345 |
break; |
| 346 |
} |
| 347 |
|
| 348 |
if (!ast_module_check("res_adsi.so")) { |
| 349 |
ast_test_status_update(test, "This test skipped because deprecated module res_adsi.so is not built by default.\n"); |
| 350 |
return AST_TEST_PASS; |
| 351 |
} |
| 352 |
|
| 353 |
if (!(c = ast_dummy_channel_alloc())) { |
| 354 |
return AST_TEST_FAIL; |
| 355 |
} |
| 356 |
ast_channel_adsicpe_set(c, AST_ADSI_AVAILABLE); |
| 357 |
res = ast_adsi_available(c) ? AST_TEST_PASS : AST_TEST_FAIL; |
| 358 |
c = ast_channel_unref(c); |
| 359 |
return res; |
| 360 |
} |
| 361 |
|
| 362 |
static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, const char * const argv[]) |
| 363 |
{ |
| 364 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 365 |
return RESULT_SUCCESS; |
| 366 |
} |
| 367 |
|
| 368 |
AST_TEST_DEFINE(agi_loaded_test) |
| 369 |
{ |
| 370 |
int res = AST_TEST_PASS; |
| 371 |
struct agi_command noop_command = |
| 372 |
{ { "testnoop", NULL }, handle_noop, NULL, NULL, 0 }; |
| 373 |
|
| 374 |
switch (cmd) { |
| 375 |
case TEST_INIT: |
| 376 |
info->name = "agi_loaded_test"; |
| 377 |
info->category = "/res/agi/"; |
| 378 |
info->summary = "AGI loaded into memory"; |
| 379 |
info->description = "Verifies whether the agi functions overrode the stubs"; |
| 380 |
return AST_TEST_NOT_RUN; |
| 381 |
case TEST_EXECUTE: |
| 382 |
break; |
| 383 |
} |
| 384 |
|
| 385 |
if (ast_agi_register(ast_module_info->self, &noop_command) == AST_OPTIONAL_API_UNAVAILABLE) { |
| 386 |
ast_test_status_update(test, "Unable to register testnoop command, because res_agi is not loaded.\n"); |
| 387 |
return AST_TEST_FAIL; |
| 388 |
} |
| 389 |
|
| 390 |
#ifndef HAVE_NULLSAFE_PRINTF |
| 391 |
|
| 392 |
if (noop_command.usage == NULL) { |
| 393 |
ast_test_status_update(test, "AGI testnoop usage was not updated properly.\n"); |
| 394 |
res = AST_TEST_FAIL; |
| 395 |
} |
| 396 |
if (noop_command.syntax == NULL) { |
| 397 |
ast_test_status_update(test, "AGI testnoop syntax was not updated properly.\n"); |
| 398 |
res = AST_TEST_FAIL; |
| 399 |
} |
| 400 |
#endif |
| 401 |
|
| 402 |
ast_agi_unregister(&noop_command); |
| 403 |
return res; |
| 404 |
} |
| 405 |
|
| 406 |
AST_TEST_DEFINE(safe_mkdir_test) |
| 407 |
{ |
| 408 |
char base_path[] = "/tmp/safe_mkdir.XXXXXX"; |
| 409 |
char path[80] = {}; |
| 410 |
int res; |
| 411 |
struct stat actual; |
| 412 |
|
| 413 |
switch (cmd) { |
| 414 |
case TEST_INIT: |
| 415 |
info->name = __func__; |
| 416 |
info->category = "/main/utils/"; |
| 417 |
info->summary = "Safe mkdir test"; |
| 418 |
info->description = |
| 419 |
"This test ensures that ast_safe_mkdir does what it is " |
| 420 |
"supposed to"; |
| 421 |
return AST_TEST_NOT_RUN; |
| 422 |
case TEST_EXECUTE: |
| 423 |
break; |
| 424 |
} |
| 425 |
|
| 426 |
if (mkdtemp(base_path) == NULL) { |
| 427 |
ast_test_status_update(test, "Failed to create tmpdir for test\n"); |
| 428 |
return AST_TEST_FAIL; |
| 429 |
} |
| 430 |
|
| 431 |
snprintf(path, sizeof(path), "%s/should_work", base_path); |
| 432 |
res = ast_safe_mkdir(base_path, path, 0777); |
| 433 |
ast_test_validate(test, 0 == res); |
| 434 |
res = stat(path, &actual); |
| 435 |
ast_test_validate(test, 0 == res); |
| 436 |
ast_test_validate(test, S_ISDIR(actual.st_mode)); |
| 437 |
|
| 438 |
snprintf(path, sizeof(path), "%s/should/also/work", base_path); |
| 439 |
res = ast_safe_mkdir(base_path, path, 0777); |
| 440 |
ast_test_validate(test, 0 == res); |
| 441 |
res = stat(path, &actual); |
| 442 |
ast_test_validate(test, 0 == res); |
| 443 |
ast_test_validate(test, S_ISDIR(actual.st_mode)); |
| 444 |
|
| 445 |
snprintf(path, sizeof(path), "%s/even/this/../should/work", base_path); |
| 446 |
res = ast_safe_mkdir(base_path, path, 0777); |
| 447 |
ast_test_validate(test, 0 == res); |
| 448 |
snprintf(path, sizeof(path), "%s/even/should/work", base_path); |
| 449 |
res = stat(path, &actual); |
| 450 |
ast_test_validate(test, 0 == res); |
| 451 |
ast_test_validate(test, S_ISDIR(actual.st_mode)); |
| 452 |
|
| 453 |
snprintf(path, sizeof(path), |
| 454 |
"%s/surprisingly/this/should//////////////////work", base_path); |
| 455 |
res = ast_safe_mkdir(base_path, path, 0777); |
| 456 |
ast_test_validate(test, 0 == res); |
| 457 |
snprintf(path, sizeof(path), |
| 458 |
"%s/surprisingly/this/should/work", base_path); |
| 459 |
res = stat(path, &actual); |
| 460 |
ast_test_validate(test, 0 == res); |
| 461 |
ast_test_validate(test, S_ISDIR(actual.st_mode)); |
| 462 |
|
| 463 |
snprintf(path, sizeof(path), "/should_not_work"); |
| 464 |
res = ast_safe_mkdir(base_path, path, 0777); |
| 465 |
ast_test_validate(test, 0 != res); |
| 466 |
ast_test_validate(test, EPERM == errno); |
| 467 |
res = stat(path, &actual); |
| 468 |
ast_test_validate(test, 0 != res); |
| 469 |
ast_test_validate(test, ENOENT == errno); |
| 470 |
|
| 471 |
snprintf(path, sizeof(path), "%s/../nor_should_this", base_path); |
| 472 |
res = ast_safe_mkdir(base_path, path, 0777); |
| 473 |
ast_test_validate(test, 0 != res); |
| 474 |
ast_test_validate(test, EPERM == errno); |
| 475 |
strncpy(path, "/tmp/nor_should_this", sizeof(path)); |
| 476 |
res = stat(path, &actual); |
| 477 |
ast_test_validate(test, 0 != res); |
| 478 |
ast_test_validate(test, ENOENT == errno); |
| 479 |
|
| 480 |
snprintf(path, sizeof(path), |
| 481 |
"%s/this/especially/should/not/../../../../../work", base_path); |
| 482 |
res = ast_safe_mkdir(base_path, path, 0777); |
| 483 |
ast_test_validate(test, 0 != res); |
| 484 |
ast_test_validate(test, EPERM == errno); |
| 485 |
strncpy(path, "/tmp/work", sizeof(path)); |
| 486 |
res = stat(path, &actual); |
| 487 |
ast_test_validate(test, 0 != res); |
| 488 |
ast_test_validate(test, ENOENT == errno); |
| 489 |
|
| 490 |
return AST_TEST_PASS; |
| 491 |
} |
| 492 |
|
| 493 |
AST_TEST_DEFINE(crypt_test) |
| 494 |
{ |
| 495 |
RAII_VAR(char *, password_crypted, NULL, ast_free); |
| 496 |
RAII_VAR(char *, blank_crypted, NULL, ast_free); |
| 497 |
const char *password = "Passw0rd"; |
| 498 |
const char *not_a_password = "not-a-password"; |
| 499 |
|
| 500 |
switch (cmd) { |
| 501 |
case TEST_INIT: |
| 502 |
info->name = "crypt_test"; |
| 503 |
info->category = "/main/utils/"; |
| 504 |
info->summary = "Test ast_crypt wrappers"; |
| 505 |
info->description = "Verifies that the ast_crypt wrappers work as expected."; |
| 506 |
return AST_TEST_NOT_RUN; |
| 507 |
case TEST_EXECUTE: |
| 508 |
break; |
| 509 |
} |
| 510 |
|
| 511 |
password_crypted = ast_crypt_encrypt(password); |
| 512 |
ast_test_validate(test, NULL != password_crypted); |
| 513 |
ast_test_validate(test, 0 != strcmp(password, password_crypted)); |
| 514 |
ast_test_validate(test, ast_crypt_validate(password, password_crypted)); |
| 515 |
ast_test_validate(test, |
| 516 |
!ast_crypt_validate(not_a_password, password_crypted)); |
| 517 |
|
| 518 |
blank_crypted = ast_crypt_encrypt(""); |
| 519 |
ast_test_validate(test, NULL != blank_crypted); |
| 520 |
ast_test_validate(test, 0 != strcmp(blank_crypted, "")); |
| 521 |
ast_test_validate(test, ast_crypt_validate("", blank_crypted)); |
| 522 |
ast_test_validate(test, |
| 523 |
!ast_crypt_validate(not_a_password, blank_crypted)); |
| 524 |
|
| 525 |
return AST_TEST_PASS; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
struct quote_set { |
| 530 |
char *input; |
| 531 |
char *output; |
| 532 |
}; |
| 533 |
|
| 534 |
AST_TEST_DEFINE(quote_mutation) |
| 535 |
{ |
| 536 |
char escaped[64]; |
| 537 |
static const struct quote_set escape_sets[] = { |
| 538 |
{"\"string\"", "\\\"string\\\""}, |
| 539 |
{"\"string", "\\\"string"}, |
| 540 |
{"string\"", "string\\\""}, |
| 541 |
{"string", "string"}, |
| 542 |
{"str\"ing", "str\\\"ing"}, |
| 543 |
{"\"", "\\\""}, |
| 544 |
{"\\\"", "\\\\\\\""}, |
| 545 |
}; |
| 546 |
int i; |
| 547 |
|
| 548 |
switch (cmd) { |
| 549 |
case TEST_INIT: |
| 550 |
info->name = "quote_mutation"; |
| 551 |
info->category = "/main/utils/"; |
| 552 |
info->summary = "Test mutation of quotes in strings"; |
| 553 |
info->description = |
| 554 |
"This tests escaping and unescaping of quotes in strings to " |
| 555 |
"verify that the original string is recovered."; |
| 556 |
return AST_TEST_NOT_RUN; |
| 557 |
case TEST_EXECUTE: |
| 558 |
break; |
| 559 |
} |
| 560 |
|
| 561 |
for (i = 0; i < ARRAY_LEN(escape_sets); i++) { |
| 562 |
ast_escape_quoted(escape_sets[i].input, escaped, sizeof(escaped)); |
| 563 |
|
| 564 |
if (strcmp(escaped, escape_sets[i].output)) { |
| 565 |
ast_test_status_update(test, |
| 566 |
"Expected escaped string '%s' instead of '%s'\n", |
| 567 |
escape_sets[i].output, escaped); |
| 568 |
return AST_TEST_FAIL; |
| 569 |
} |
| 570 |
|
| 571 |
ast_unescape_quoted(escaped); |
| 572 |
if (strcmp(escaped, escape_sets[i].input)) { |
| 573 |
ast_test_status_update(test, |
| 574 |
"Expected unescaped string '%s' instead of '%s'\n", |
| 575 |
escape_sets[i].input, escaped); |
| 576 |
return AST_TEST_FAIL; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
return AST_TEST_PASS; |
| 581 |
} |
| 582 |
|
| 583 |
AST_TEST_DEFINE(quote_unescaping) |
| 584 |
{ |
| 585 |
static const struct quote_set escape_sets[] = { |
| 586 |
{"\"string\"", "\"string\""}, |
| 587 |
{"\\\"string\"", "\"string\""}, |
| 588 |
{"\"string\\\"", "\"string\""}, |
| 589 |
{"str\\ing", "string"}, |
| 590 |
{"string\\", "string"}, |
| 591 |
{"\\string", "string"}, |
| 592 |
}; |
| 593 |
int i; |
| 594 |
|
| 595 |
switch (cmd) { |
| 596 |
case TEST_INIT: |
| 597 |
info->name = "quote_unescaping"; |
| 598 |
info->category = "/main/utils/"; |
| 599 |
info->summary = "Test unescaping of off-nominal strings"; |
| 600 |
info->description = |
| 601 |
"This tests unescaping of strings which contain a mix of " |
| 602 |
"escaped and unescaped sequences."; |
| 603 |
return AST_TEST_NOT_RUN; |
| 604 |
case TEST_EXECUTE: |
| 605 |
break; |
| 606 |
} |
| 607 |
|
| 608 |
for (i = 0; i < ARRAY_LEN(escape_sets); i++) { |
| 609 |
RAII_VAR(char *, escaped, ast_strdup(escape_sets[i].input), ast_free); |
| 610 |
|
| 611 |
ast_unescape_quoted(escaped); |
| 612 |
if (strcmp(escaped, escape_sets[i].output)) { |
| 613 |
ast_test_status_update(test, |
| 614 |
"Expected unescaped string '%s' instead of '%s'\n", |
| 615 |
escape_sets[i].output, escaped); |
| 616 |
return AST_TEST_FAIL; |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
return AST_TEST_PASS; |
| 621 |
} |
| 622 |
|
| 623 |
static int unload_module(void) |
| 624 |
{ |
| 625 |
AST_TEST_UNREGISTER(uri_encode_decode_test); |
| 626 |
AST_TEST_UNREGISTER(quoted_escape_test); |
| 627 |
AST_TEST_UNREGISTER(md5_test); |
| 628 |
AST_TEST_UNREGISTER(sha1_test); |
| 629 |
AST_TEST_UNREGISTER(base64_test); |
| 630 |
AST_TEST_UNREGISTER(crypto_loaded_test); |
| 631 |
AST_TEST_UNREGISTER(adsi_loaded_test); |
| 632 |
AST_TEST_UNREGISTER(agi_loaded_test); |
| 633 |
AST_TEST_UNREGISTER(safe_mkdir_test); |
| 634 |
AST_TEST_UNREGISTER(crypt_test); |
| 635 |
AST_TEST_UNREGISTER(quote_mutation); |
| 636 |
AST_TEST_UNREGISTER(quote_unescaping); |
| 637 |
return 0; |
| 638 |
} |
| 639 |
|
| 640 |
static int load_module(void) |
| 641 |
{ |
| 642 |
AST_TEST_REGISTER(uri_encode_decode_test); |
| 643 |
AST_TEST_REGISTER(quoted_escape_test); |
| 644 |
AST_TEST_REGISTER(md5_test); |
| 645 |
AST_TEST_REGISTER(sha1_test); |
| 646 |
AST_TEST_REGISTER(base64_test); |
| 647 |
AST_TEST_REGISTER(crypto_loaded_test); |
| 648 |
AST_TEST_REGISTER(adsi_loaded_test); |
| 649 |
AST_TEST_REGISTER(agi_loaded_test); |
| 650 |
AST_TEST_REGISTER(safe_mkdir_test); |
| 651 |
AST_TEST_REGISTER(crypt_test); |
| 652 |
AST_TEST_REGISTER(quote_mutation); |
| 653 |
AST_TEST_REGISTER(quote_unescaping); |
| 654 |
return AST_MODULE_LOAD_SUCCESS; |
| 655 |
} |
| 656 |
|
| 657 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Utils test module", |
| 658 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 659 |
.load = load_module, |
| 660 |
.unload = unload_module, |
| 661 |
.requires = "res_agi,res_crypto", |
| 662 |
); |