summaryrefslogtreecommitdiff
path: root/tests/test_utils.c
diff options
context:
space:
mode:
authorRussell Bryant <russell@russellbryant.com>2010-02-09 23:32:14 +0000
committerRussell Bryant <russell@russellbryant.com>2010-02-09 23:32:14 +0000
commitbbed34f76455df0250862ba4c4c324452ad6c0bb (patch)
treed10f5c6801b7a0d86ccd2dd6b5b3986e93b82fb5 /tests/test_utils.c
parentdd48c7eb407db16ac60d013319b893f021253af9 (diff)
Various updates to the unit test API.
1) It occurred to me that the difference in usage between the error ast_str and the ast_test_update_status() usage has turned out to be a bit ambiguous in practice. In a lot of cases, the same message was being sent to both. In other cases, it was only sent to one or the other. My opinion now is that in every case, I think it makes sense to do both; we should output it to the CLI as well as save it off for logging purposes. This change results in most of the changes in this diff, since it required changes to all existing unit tests. It also allowed for some simplifications of unit test API implementation code. 2) Update ast_test_status_update() to include the file, function, and line number for the code providing the update. 3) There are some formatting tweaks here and there. Hopefully they aren't too distracting for code review purposes. Reviewboard's diff viewer seems to do a pretty good job of pointing out when something is a whitespace change. 4) I moved the md5_test and sha1_test into the test_utils module. It seemed like a better approach since these tests are so tiny. 5) I changed the number of nodes used in heap_test_2 from 1 million to 100 thousand. The only reason for this was to reduce the time it took for this test to run. 6) Remove an unused function prototype that was at the bottom of utils.h. 7) Simplify test_insert() using the LIST_INSERT_SORTALPHA() macro. The one minor difference in behavior is that it no longer checks for a test registered with the same name. 8) Expand the code in test_alloc() to provide specific error messages for each failure case, to clearly inform developers if they forget to set the name, summary, description, etc. 9) Tweak the output of the "test show registered" CLI command. I swapped the name and category to have the category first. It seemed more natural since that is the sort key. 10) Don't output the status ast_str in the "test show results" CLI command. This is going to tend to be pretty verbose, so just leave that for the detailed test logs (test generate results). Review: https://reviewboard.asterisk.org/r/493/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@245864 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'tests/test_utils.c')
-rw-r--r--tests/test_utils.c124
1 files changed, 107 insertions, 17 deletions
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;
}