diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_ast_format_str_reduce.c | 10 | ||||
-rw-r--r-- | tests/test_heap.c | 26 | ||||
-rw-r--r-- | tests/test_md5.c | 97 | ||||
-rw-r--r-- | tests/test_sched.c | 28 | ||||
-rw-r--r-- | tests/test_sha1.c | 100 | ||||
-rw-r--r-- | tests/test_skel.c | 4 | ||||
-rw-r--r-- | tests/test_substitution.c | 116 | ||||
-rw-r--r-- | tests/test_utils.c | 124 |
8 files changed, 200 insertions, 305 deletions
diff --git a/tests/test_ast_format_str_reduce.c b/tests/test_ast_format_str_reduce.c index 0c24ab2d1..8dd1f500f 100644 --- a/tests/test_ast_format_str_reduce.c +++ b/tests/test_ast_format_str_reduce.c @@ -94,12 +94,14 @@ AST_TEST_DEFINE(ast_format_str_reduce_test_1) for (i = 0; test_strings[i][0]; i++) { c = ast_strdupa(test_strings[i][0]); if (!(c = ast_format_str_reduce(c))) { - ast_str_set(&args->ast_test_error_str, 0, "Error running ast_format_str_reduce() on string '%s'\n", test_strings[i][0]); + ast_test_status_update(test, "Error running ast_format_str_reduce() on string '%s'\n", + test_strings[i][0]); return AST_TEST_FAIL; } if (strcmp(test_strings[i][1], c)) { - ast_str_set(&args->ast_test_error_str, 0, "Format string '%s' reduced to '%s'. Expected '%s'\n", test_strings[i][0], c, test_strings[i][1]); + ast_test_status_update(test, "Format string '%s' reduced to '%s'. Expected '%s'\n", + test_strings[i][0], c, test_strings[i][1]); return AST_TEST_FAIL; } } @@ -107,7 +109,9 @@ AST_TEST_DEFINE(ast_format_str_reduce_test_1) for (i = 0; fail_strings[i]; i++) { c = ast_strdupa(fail_strings[i]); if ((c = ast_format_str_reduce(c))) { - ast_str_set(&args->ast_test_error_str, 0, "ast_format_str_reduce() succeded on string '%s' with result '%s', but we expected it to fail\n", fail_strings[i], c); + ast_test_status_update(test, "ast_format_str_reduce() succeded on string '%s' " + "with result '%s', but we expected it to fail\n", + fail_strings[i], c); return AST_TEST_FAIL; } } diff --git a/tests/test_heap.c b/tests/test_heap.c index 420bd4f7b..a8dd13e0a 100644 --- a/tests/test_heap.c +++ b/tests/test_heap.c @@ -80,8 +80,6 @@ AST_TEST_DEFINE(heap_test_1) return AST_TEST_FAIL; } - ast_test_status_update(&args->status_update, "pushing nodes\n"); - ast_heap_push(h, &nodes[0]); ast_heap_push(h, &nodes[1]); @@ -90,22 +88,25 @@ AST_TEST_DEFINE(heap_test_1) obj = ast_heap_pop(h); if (obj->val != 3) { + ast_test_status_update(test, "expected 3, got %ld\n", obj->val); return AST_TEST_FAIL; } - ast_test_status_update(&args->status_update, "popping nodes\n"); obj = ast_heap_pop(h); if (obj->val != 2) { + ast_test_status_update(test, "expected 2, got %ld\n", obj->val); return AST_TEST_FAIL; } obj = ast_heap_pop(h); if (obj->val != 1) { + ast_test_status_update(test, "expected 1, got %ld\n", obj->val); return AST_TEST_FAIL; } obj = ast_heap_pop(h); if (obj) { + ast_test_status_update(test, "got unexpected object\n"); return AST_TEST_FAIL; } @@ -117,10 +118,10 @@ AST_TEST_DEFINE(heap_test_1) AST_TEST_DEFINE(heap_test_2) { struct ast_heap *h = NULL; - static const unsigned int one_million = 1000000; + static const unsigned int total = 100000; struct node *nodes = NULL; struct node *node; - unsigned int i = one_million; + unsigned int i = total; long last = LONG_MAX; long cur; enum ast_test_result_state res = AST_TEST_PASS; @@ -130,13 +131,18 @@ AST_TEST_DEFINE(heap_test_2) info->name = "heap_test_2"; info->category = "main/heap/"; info->summary = "load test"; - info->description = "Push a million random elements on to a heap,verify that the heap has been properly constructed, and then ensure that the elements are come back off in the proper order"; + info->description = + "Push one hundred thousand random elements on to a heap, " + "verify that the heap has been properly constructed, " + "and then ensure that the elements are come back off " + "in the proper order." + "\n"; return AST_TEST_NOT_RUN; case TEST_EXECUTE: break; } - if (!(nodes = ast_malloc(one_million * sizeof(*node)))) { + if (!(nodes = ast_malloc(total * sizeof(*node)))) { res = AST_TEST_FAIL; goto return_cleanup; } @@ -160,7 +166,7 @@ AST_TEST_DEFINE(heap_test_2) while ((node = ast_heap_pop(h))) { cur = node->val; if (cur > last) { - ast_str_set(&args->ast_test_error_str, 0, "i: %u, cur: %ld, last: %ld\n", i, cur, last); + ast_test_status_update(test, "i: %u, cur: %ld, last: %ld\n", i, cur, last); res = AST_TEST_FAIL; goto return_cleanup; } @@ -168,8 +174,8 @@ AST_TEST_DEFINE(heap_test_2) i++; } - if (i != one_million) { - ast_str_set(&args->ast_test_error_str, 0, "Stopped popping off after only getting %u nodes\n", i); + if (i != total) { + ast_test_status_update(test, "Stopped popping off after only getting %u nodes\n", i); res = AST_TEST_FAIL; goto return_cleanup; } diff --git a/tests/test_md5.c b/tests/test_md5.c deleted file mode 100644 index f8b884913..000000000 --- a/tests/test_md5.c +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Asterisk -- An open source telephony toolkit. - * - * Copyright (C) 2010, Digium, Inc. - * - * 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 MD5 test - * - * \author Russell Bryant <russell@digium.com> - * - * \ingroup tests - */ - -/*** MODULEINFO - <depend>TEST_FRAMEWORK</depend> - ***/ - -#include "asterisk.h" - -ASTERISK_FILE_VERSION(__FILE__, "$Revision$") - -#include "asterisk/utils.h" -#include "asterisk/module.h" -#include "asterisk/test.h" - -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/"; - info->summary = "MD5 test"; - info->description = - "This test exercises MD5 calculations.\n" - ""; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - ast_test_status_update(&args->status_update, "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(&args->status_update, - "input: '%s' hash: '%s' expected hash: '%s'\n", - tests[i].input, md5_hash, tests[i].expected_output); - ast_str_append(&args->ast_test_error_str, 0, - "input: '%s' hash: '%s' expected hash: '%s'\n", - tests[i].input, md5_hash, tests[i].expected_output); - res = AST_TEST_FAIL; - } - } - - return res; -} - -static int unload_module(void) -{ - AST_TEST_UNREGISTER(md5_test); - return 0; -} - -static int load_module(void) -{ - AST_TEST_REGISTER(md5_test); - return AST_MODULE_LOAD_SUCCESS; -} - -AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "MD5 Test"); diff --git a/tests/test_sched.c b/tests/test_sched.c index b9e23d81e..6cb9f61cb 100644 --- a/tests/test_sched.c +++ b/tests/test_sched.c @@ -64,7 +64,7 @@ AST_TEST_DEFINE(sched_test_order) } if (!(con = sched_context_create())) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "Test failed - could not create scheduler context\n"); return AST_TEST_FAIL; } @@ -73,79 +73,79 @@ AST_TEST_DEFINE(sched_test_order) * of ast_sched_wait() looks appropriate at each step along the way. */ if ((wait = ast_sched_wait(con)) != -1) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned -1, returned '%d'\n", wait); goto return_cleanup; } if ((id1 = ast_sched_add(con, 100000, sched_cb, NULL)) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to add scheduler entry\n"); + ast_test_status_update(test, "Failed to add scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) > 100000) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned <= 100000, returned '%d'\n", wait); goto return_cleanup; } if ((id2 = ast_sched_add(con, 10000, sched_cb, NULL)) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to add scheduler entry\n"); + ast_test_status_update(test, "Failed to add scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) > 10000) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned <= 10000, returned '%d'\n", wait); goto return_cleanup; } if ((id3 = ast_sched_add(con, 1000, sched_cb, NULL)) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to add scheduler entry\n"); + ast_test_status_update(test, "Failed to add scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) > 1000) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned <= 1000, returned '%d'\n", wait); goto return_cleanup; } if (ast_sched_del(con, id3) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to remove scheduler entry\n"); + ast_test_status_update(test, "Failed to remove scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) <= 1000) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned > 1000, returned '%d'\n", wait); goto return_cleanup; } if (ast_sched_del(con, id2) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to remove scheduler entry\n"); + ast_test_status_update(test, "Failed to remove scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) <= 10000) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned > 10000, returned '%d'\n", wait); goto return_cleanup; } if (ast_sched_del(con, id1) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to remove scheduler entry\n"); + ast_test_status_update(test, "Failed to remove scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) != -1) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned -1, returned '%d'\n", wait); goto return_cleanup; diff --git a/tests/test_sha1.c b/tests/test_sha1.c deleted file mode 100644 index 62d62959e..000000000 --- a/tests/test_sha1.c +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Asterisk -- An open source telephony toolkit. - * - * Copyright (C) 2010, Digium, Inc. - * - * 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 SHA1 test - * - * \author Russell Bryant <russell@digium.com> - * - * \ingroup tests - */ - -/*** MODULEINFO - <depend>TEST_FRAMEWORK</depend> - ***/ - -#include "asterisk.h" - -ASTERISK_FILE_VERSION(__FILE__, "$Revision$") - -#include "asterisk/utils.h" -#include "asterisk/module.h" -#include "asterisk/test.h" - -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/"; - info->summary = "SHA1 test"; - info->description = - "This test exercises SHA1 calculations.\n" - ""; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - ast_test_status_update(&args->status_update, "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(&args->status_update, - "input: '%s' hash: '%s' expected hash: '%s'\n", - tests[i].input, sha1_hash, tests[i].expected_output); - ast_str_append(&args->ast_test_error_str, 0, - "input: '%s' hash: '%s' expected hash: '%s'\n", - tests[i].input, sha1_hash, tests[i].expected_output); - res = AST_TEST_FAIL; - } - } - - return res; -} - -static int unload_module(void) -{ - AST_TEST_UNREGISTER(sha1_test); - return 0; -} - -static int load_module(void) -{ - AST_TEST_REGISTER(sha1_test); - return AST_MODULE_LOAD_SUCCESS; -} - -AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SHA1 Test"); diff --git a/tests/test_skel.c b/tests/test_skel.c index 10ae097b3..c1ce5e844 100644 --- a/tests/test_skel.c +++ b/tests/test_skel.c @@ -55,10 +55,10 @@ AST_TEST_DEFINE(sample_test) break; } - ast_test_status_update(&args->status_update, "Executing sample test.\n"); + ast_test_status_update(test, "Executing sample test...\n"); if (!(ptr = ast_malloc(8))) { - ast_str_set(&args->ast_test_error_str, 0, "ast_malloc() failed\n"); + ast_test_status_update(test, "ast_malloc() failed\n"); return AST_TEST_FAIL; } diff --git a/tests/test_substitution.c b/tests/test_substitution.c index 6fdcfa1ac..b1ffdf64b 100644 --- a/tests/test_substitution.c +++ b/tests/test_substitution.c @@ -44,23 +44,20 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/threadstorage.h" #include "asterisk/test.h" -static enum ast_test_result_state test_chan_integer(void *status, struct ast_str **err, +static enum ast_test_result_state test_chan_integer(struct ast_test *test, struct ast_channel *c, int *ifield, const char *expression) { int i, okay = 1, value1 = -1, value2 = -1; char workspace[4096]; struct ast_str *str = ast_str_create(16); - ast_test_status_update(status, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); + ast_test_status_update(test, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); for (i = 0; i < 256; i++) { *ifield = i; ast_str_substitute_variables(&str, 0, c, expression); pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) { - ast_test_status_update(status, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i); - ast_str_set(err, 0, "%s: '%s' != '%s' and/or %d != %d != %d\n", - __PRETTY_FUNCTION__, ast_str_buffer(str), - workspace, value1, value2, i); + ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i); okay = 0; } } @@ -70,7 +67,7 @@ static enum ast_test_result_state test_chan_integer(void *status, struct ast_str return okay ? AST_TEST_PASS : AST_TEST_FAIL; } -static enum ast_test_result_state test_chan_string(void *status, struct ast_str **err, +static enum ast_test_result_state test_chan_string(struct ast_test *test, struct ast_channel *c, char *cfield, size_t cfieldsize, const char *expression) { @@ -83,11 +80,10 @@ static enum ast_test_result_state test_chan_string(void *status, struct ast_str ast_copy_string(cfield, values[i], cfieldsize); ast_str_substitute_variables(&str, 0, c, expression); pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); - ast_test_status_update(status, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); + ast_test_status_update(test, "Testing '%s' . . . . . %s\n", + expression, okay ? "passed" : "FAILED"); if (strcmp(cfield, ast_str_buffer(str)) != 0 || strcmp(cfield, workspace) != 0) { - ast_test_status_update(status, "%s != %s != %s\n", cfield, ast_str_buffer(str), workspace); - ast_str_set(err, 0, "%s: '%s' != '%s' != '%s'\n", - __PRETTY_FUNCTION__, cfield, ast_str_buffer(str), workspace); + ast_test_status_update(test, "%s != %s != %s\n", cfield, ast_str_buffer(str), workspace); okay = 0; } } @@ -97,7 +93,7 @@ static enum ast_test_result_state test_chan_string(void *status, struct ast_str return okay ? AST_TEST_PASS : AST_TEST_FAIL; } -static enum ast_test_result_state test_chan_variable(void *status, struct ast_str **err, +static enum ast_test_result_state test_chan_variable(struct ast_test *test, struct ast_channel *c, const char *varname) { const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" }; @@ -111,11 +107,11 @@ static enum ast_test_result_state test_chan_variable(void *status, struct ast_st pbx_builtin_setvar_helper(c, varname, values[i]); ast_str_substitute_variables(&str, 0, c, ast_str_buffer(var)); pbx_substitute_variables_helper(c, ast_str_buffer(var), workspace, sizeof(workspace)); - ast_test_status_update(status, "Testing '%s' . . . . . %s\n", ast_str_buffer(var), okay ? "passed" : "FAILED"); + ast_test_status_update(test, "Testing '%s' . . . . . %s\n", + ast_str_buffer(var), okay ? "passed" : "FAILED"); if (strcmp(values[i], ast_str_buffer(str)) != 0 || strcmp(values[i], workspace) != 0) { - ast_test_status_update(status, "%s != %s != %s\n", values[i], ast_str_buffer(str), workspace); - ast_str_set(err, 0, "%s: Variable: '%s' ... '%s' != '%s' != '%s'\n", varname, - __PRETTY_FUNCTION__, values[i], ast_str_buffer(str), workspace); + ast_test_status_update(test, "%s != %s != %s\n", + values[i], ast_str_buffer(str), workspace); okay = 0; } } @@ -126,7 +122,7 @@ static enum ast_test_result_state test_chan_variable(void *status, struct ast_st return okay ? AST_TEST_PASS : AST_TEST_FAIL; } -static enum ast_test_result_state test_chan_function(void *status, struct ast_str **err, +static enum ast_test_result_state test_chan_function(struct ast_test *test, struct ast_channel *c, const char *expression) { int okay = 1; @@ -135,12 +131,11 @@ static enum ast_test_result_state test_chan_function(void *status, struct ast_st ast_str_substitute_variables(&str, 0, c, expression); pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); - ast_test_status_update(status, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); + ast_test_status_update(test, "Testing '%s' . . . . . %s\n", + expression, okay ? "passed" : "FAILED"); if (strcmp(workspace, ast_str_buffer(str)) != 0) { - ast_test_status_update(status, "test_chan_function, expr: '%s' ... %s != %s\n", + ast_test_status_update(test, "test_chan_function, expr: '%s' ... %s != %s\n", expression, ast_str_buffer(str), workspace); - ast_str_set(err, 0, "%s: expr: '%s' ... '%s' != '%s'\n", - __PRETTY_FUNCTION__, expression, ast_str_buffer(str), workspace); okay = 0; } @@ -149,7 +144,7 @@ static enum ast_test_result_state test_chan_function(void *status, struct ast_st return okay ? AST_TEST_PASS : AST_TEST_FAIL; } -static enum ast_test_result_state test_2way_function(void *status, struct ast_str **err, +static enum ast_test_result_state test_2way_function(struct ast_test *test, struct ast_channel *c, const char *encode1, const char *encode2, const char *decode1, const char *decode2) { @@ -163,14 +158,13 @@ static enum ast_test_result_state test_2way_function(void *status, struct ast_st okay = !strcmp(ast_str_buffer(str), "foobarbaz"); - ast_test_status_update(status, "Testing '%s%s' and '%s%s' . . . . . %s\n", + ast_test_status_update(test, "Testing '%s%s' and '%s%s' . . . . . %s\n", encode1, encode2, decode1, decode2, okay ? "passed" : "FAILED"); if (!okay) { - ast_test_status_update(status, " '%s' != 'foobarbaz'\n", ast_str_buffer(str)); - ast_str_set(err, 0, "%s: '%s' != 'foobarbaz'\n", - __PRETTY_FUNCTION__, ast_str_buffer(str)); + ast_test_status_update(test, " '%s' != 'foobarbaz'\n", + ast_str_buffer(str)); } ast_free(str); @@ -178,7 +172,7 @@ static enum ast_test_result_state test_2way_function(void *status, struct ast_st return okay ? AST_TEST_PASS : AST_TEST_FAIL; } -static enum ast_test_result_state test_expected_result(void *status, struct ast_str **err, +static enum ast_test_result_state test_expected_result(struct ast_test *test, struct ast_channel *c, const char *expression, const char *result) { struct ast_str *str = ast_str_create(16); @@ -187,14 +181,12 @@ static enum ast_test_result_state test_expected_result(void *status, struct ast_ ast_str_substitute_variables(&str, 0, c, expression); okay = !strcmp(ast_str_buffer(str), result); - ast_test_status_update(status, "Testing '%s' ('%s') == '%s' . . . . . %s\n", + ast_test_status_update(test, "Testing '%s' ('%s') == '%s' . . . . . %s\n", ast_str_buffer(str), expression, result, okay ? "passed" : "FAILED"); if (!okay) { - ast_test_status_update(status, "test_expected_result: '%s' != '%s'\n", - ast_str_buffer(str), result); - ast_str_set(err, 0, "%s: '%s' != '%s'\n", __PRETTY_FUNCTION__, + ast_test_status_update(test, "test_expected_result: '%s' != '%s'\n", ast_str_buffer(str), result); } @@ -222,43 +214,43 @@ AST_TEST_DEFINE(test_substitution) break; } - ast_test_status_update(&args->status_update, "Testing variable substitution ...\n"); + ast_test_status_update(test, "Testing variable substitution ...\n"); c = ast_channel_alloc(0, 0, "", "", "", "", "", "", 0, "Test/substitution"); #define TEST(t) if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; } - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_pres, "${CALLINGPRES}")); - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_ani2, "${CALLINGANI2}")); - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_ton, "${CALLINGTON}")); - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_tns, "${CALLINGTNS}")); - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->hangupcause, "${HANGUPCAUSE}")); - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->priority, "${PRIORITY}")); - TEST(test_chan_string(&args->status_update, &args->ast_test_error_str, c, c->context, sizeof(c->context), "${CONTEXT}")); - TEST(test_chan_string(&args->status_update, &args->ast_test_error_str, c, c->exten, sizeof(c->exten), "${EXTEN}")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CHANNEL(language)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CHANNEL(musicclass)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CHANNEL(parkinglot)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CALLERID(name)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CURLOPT(proxyuserpwd)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CDR(foo)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "ENV(foo)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "GLOBAL(foo)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "GROUP()")); - TEST(test_2way_function(&args->status_update, &args->ast_test_error_str, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}")); - TEST(test_2way_function(&args->status_update, &args->ast_test_error_str, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}")); + TEST(test_chan_integer(test, c, &c->cid.cid_pres, "${CALLINGPRES}")); + TEST(test_chan_integer(test, c, &c->cid.cid_ani2, "${CALLINGANI2}")); + TEST(test_chan_integer(test, c, &c->cid.cid_ton, "${CALLINGTON}")); + TEST(test_chan_integer(test, c, &c->cid.cid_tns, "${CALLINGTNS}")); + TEST(test_chan_integer(test, c, &c->hangupcause, "${HANGUPCAUSE}")); + TEST(test_chan_integer(test, c, &c->priority, "${PRIORITY}")); + TEST(test_chan_string(test, c, c->context, sizeof(c->context), "${CONTEXT}")); + TEST(test_chan_string(test, c, c->exten, sizeof(c->exten), "${EXTEN}")); + TEST(test_chan_variable(test, c, "CHANNEL(language)")); + TEST(test_chan_variable(test, c, "CHANNEL(musicclass)")); + TEST(test_chan_variable(test, c, "CHANNEL(parkinglot)")); + TEST(test_chan_variable(test, c, "CALLERID(name)")); + TEST(test_chan_variable(test, c, "CURLOPT(proxyuserpwd)")); + TEST(test_chan_variable(test, c, "CDR(foo)")); + TEST(test_chan_variable(test, c, "ENV(foo)")); + TEST(test_chan_variable(test, c, "GLOBAL(foo)")); + TEST(test_chan_variable(test, c, "GROUP()")); + TEST(test_2way_function(test, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}")); + TEST(test_2way_function(test, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}")); pbx_builtin_setvar_helper(c, "foo", "123"); pbx_builtin_setvar_helper(c, "bar", "foo"); pbx_builtin_setvar_helper(c, "baz", "fo"); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "${foo}${foo}", "123123")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${foo}A${foo}A", "A123A123A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${bar}}A", "A123A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o}A", "A123A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:1}A", "A23A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:1:1}A", "A2A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:1:-1}A", "A2A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:-1:1}A", "A3A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:-2:1}A", "A2A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:-2:-1}A", "A2A")); + TEST(test_expected_result(test, c, "${foo}${foo}", "123123")); + TEST(test_expected_result(test, c, "A${foo}A${foo}A", "A123A123A")); + TEST(test_expected_result(test, c, "A${${bar}}A", "A123A")); + TEST(test_expected_result(test, c, "A${${baz}o}A", "A123A")); + TEST(test_expected_result(test, c, "A${${baz}o:1}A", "A23A")); + TEST(test_expected_result(test, c, "A${${baz}o:1:1}A", "A2A")); + TEST(test_expected_result(test, c, "A${${baz}o:1:-1}A", "A2A")); + TEST(test_expected_result(test, c, "A${${baz}o:-1:1}A", "A3A")); + TEST(test_expected_result(test, c, "A${${baz}o:-2:1}A", "A2A")); + TEST(test_expected_result(test, c, "A${${baz}o:-2:-1}A", "A2A")); #undef TEST /* For testing dialplan functions */ @@ -276,7 +268,7 @@ AST_TEST_DEFINE(test_substitution) if (acf->read && acf->read2) { char expression[80]; snprintf(expression, sizeof(expression), "${%s(foo)}", cmd); - if (AST_TEST_FAIL == test_chan_function(&args->status_update, &args->ast_test_error_str,c, expression)) { + if (AST_TEST_FAIL == test_chan_function(test, c, expression)) { res = AST_TEST_FAIL; } } diff --git a/tests/test_utils.c b/tests/test_utils.c index 92db72a08..b79da926f 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -4,6 +4,7 @@ * 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 @@ -16,11 +17,12 @@ * at the top of the source tree. */ -/*! \file - * +/*! + * \file * \brief Unit Tests for utils API * * \author David Vossel <dvossel@digium.com> + * \author Russell Bryant <russell@digium.com> */ /*** MODULEINFO @@ -54,60 +56,148 @@ AST_TEST_DEFINE(uri_encode_decode_test) break; } - ast_test_status_update(&args->status_update, "Input before executing ast_uri_encode:\n%s\n", in); - ast_test_status_update(&args->status_update, "Output expected for ast_uri_encode with enabling do_special_char:\n%s\n", expected1); - ast_test_status_update(&args->status_update, "Output expected for ast_uri_encode with out enabling do_special_char:\n%s\n\n", expected2); + 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(&args->status_update, "Output after enabling do_special_char:\n%s\n", out); + ast_test_status_update(test, "Output after enabling do_special_char:\n%s\n", out); if (strcmp(expected1, out)) { - ast_test_status_update(&args->status_update, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n"); - ast_str_append(&args->ast_test_error_str, 0, "enable do_special_char test encode failed: \n"); + 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(&args->status_update, "Decoded string did not match original input\n\n"); - ast_str_append(&args->ast_test_error_str, 0, "enable do_special_char test decode failed: \n"); + ast_test_status_update(test, "Decoded string did not match original input\n"); res = AST_TEST_FAIL; } else { - ast_test_status_update(&args->status_update, "Decoded string matched original input\n\n"); + 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(&args->status_update, "Output after disabling do_special_char:\n%s\n", out); + ast_test_status_update(test, "Output after disabling do_special_char: %s\n", out); if (strcmp(expected2, out)) { - ast_test_status_update(&args->status_update, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n"); - ast_str_append(&args->ast_test_error_str, 0, "no do_special_char test encode failed: \n"); + 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(&args->status_update, "Decoded string did not match original input\n\n"); - ast_str_append(&args->ast_test_error_str, 0, "no do_special_char test decode failed\n"); + ast_test_status_update(test, "Decoded string did not match original input\n"); res = AST_TEST_FAIL; } else { - ast_test_status_update(&args->status_update, "Decoded string matched original input\n\n"); + ast_test_status_update(test, "Decoded string matched original input\n"); + } + + 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; } static int unload_module(void) { AST_TEST_UNREGISTER(uri_encode_decode_test); + AST_TEST_UNREGISTER(md5_test); + AST_TEST_UNREGISTER(sha1_test); return 0; } static int load_module(void) { AST_TEST_REGISTER(uri_encode_decode_test); + AST_TEST_REGISTER(md5_test); + AST_TEST_REGISTER(sha1_test); return AST_MODULE_LOAD_SUCCESS; } |