[asterisk-commits] russell: trunk r245864 - in /trunk: channels/sip/ funcs/ include/asterisk/ ma...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Feb 9 17:32:18 CST 2010
Author: russell
Date: Tue Feb 9 17:32:14 2010
New Revision: 245864
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=245864
Log:
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/
Removed:
trunk/tests/test_md5.c
trunk/tests/test_sha1.c
Modified:
trunk/channels/sip/config_parser.c
trunk/channels/sip/reqresp_parser.c
trunk/funcs/func_math.c
trunk/include/asterisk/test.h
trunk/include/asterisk/utils.h
trunk/main/test.c
trunk/tests/test_ast_format_str_reduce.c
trunk/tests/test_heap.c
trunk/tests/test_sched.c
trunk/tests/test_skel.c
trunk/tests/test_substitution.c
trunk/tests/test_utils.c
Modified: trunk/channels/sip/config_parser.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/sip/config_parser.c?view=diff&rev=245864&r1=245863&r2=245864
==============================================================================
--- trunk/channels/sip/config_parser.c (original)
+++ trunk/channels/sip/config_parser.c Tue Feb 9 17:32:14 2010
@@ -263,7 +263,7 @@
reg->callid_valid != FALSE ||
reg->ocseq != INITIAL_CSEQ) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple config failed\n");
+ ast_test_status_update(test, "Test 1: simple config failed\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -290,7 +290,7 @@
reg->callid_valid != FALSE ||
reg->ocseq != INITIAL_CSEQ) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 2: add secret failed\n");
+ ast_test_status_update(test, "Test 2: add secret failed\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -317,7 +317,7 @@
reg->callid_valid != FALSE ||
reg->ocseq != INITIAL_CSEQ) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 3: add userdomain and authuser failed\n");
+ ast_test_status_update(test, "Test 3: add userdomain and authuser failed\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -344,7 +344,7 @@
reg->callid_valid != FALSE ||
reg->ocseq != INITIAL_CSEQ) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 4: add callback extension failed\n");
+ ast_test_status_update(test, "Test 4: add callback extension failed\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -371,7 +371,7 @@
reg->callid_valid != FALSE ||
reg->ocseq != INITIAL_CSEQ) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 5: add transport failed\n");
+ ast_test_status_update(test, "Test 5: add transport failed\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -398,7 +398,7 @@
reg->callid_valid != FALSE ||
reg->ocseq != INITIAL_CSEQ) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 6: change to tls transport and add expiry failed\n");
+ ast_test_status_update(test, "Test 6: change to tls transport and add expiry failed\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -425,7 +425,7 @@
reg->callid_valid != FALSE ||
reg->ocseq != INITIAL_CSEQ) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 7, change transport to tcp, add custom port, and add peer failed.\n");
+ ast_test_status_update(test, "Test 7, change transport to tcp, add custom port, and add peer failed.\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -452,7 +452,7 @@
reg->callid_valid != FALSE ||
reg->ocseq != INITIAL_CSEQ) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 8, remove transport failed.\n");
+ ast_test_status_update(test, "Test 8, remove transport failed.\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -462,8 +462,8 @@
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
goto alloc_fail;
} else if (!sip_parse_register_line(reg, default_expiry, reg9, 1)) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 9, missing domain, expected to fail but did not.\n");
+ ast_test_status_update(test,
+ "Test 9, missing domain, expected to fail but did not.\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -473,8 +473,8 @@
if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
goto alloc_fail;
} else if (!sip_parse_register_line(reg, default_expiry, reg10, 1)) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 10, missing user expected to fail but did not\n");
+ ast_test_status_update(test,
+ "Test 10, missing user expected to fail but did not\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -482,8 +482,8 @@
/* ---Test reg 11, no registry object, expected to fail--- */
if (!sip_parse_register_line(NULL, default_expiry, reg1, 1)) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 11, no registery object, expected to fail but did not.\n");
+ ast_test_status_update(test,
+ "Test 11, no registery object, expected to fail but did not.\n");
res = AST_TEST_FAIL;
}
@@ -492,7 +492,8 @@
goto alloc_fail;
} else if (!sip_parse_register_line(reg, default_expiry, NULL, 1)) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 11, NULL register line expected to fail but did not.\n");
+ ast_test_status_update(test,
+ "Test 11, NULL register line expected to fail but did not.\n");
res = AST_TEST_FAIL;
}
ast_string_field_free_memory(reg);
@@ -502,7 +503,7 @@
return res;
alloc_fail:
- ast_str_set(&args->ast_test_error_str, 0, "Out of memory. \n");
+ ast_test_status_update(test, "Out of memory. \n");
return res;
}
@@ -582,60 +583,54 @@
/* test 1, simple host */
sip_parse_host(host1, 1, &host, &port, &transport);
if (port != STANDARD_SIP_PORT ||
- ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
- transport != SIP_TRANSPORT_UDP) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple host failed.\n");
+ ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
+ transport != SIP_TRANSPORT_UDP) {
+ ast_test_status_update(test, "Test 1: simple host failed.\n");
res = AST_TEST_FAIL;
}
/* test 2, add tcp transport */
sip_parse_host(host2, 1, &host, &port, &transport);
if (port != STANDARD_SIP_PORT ||
- ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
- transport != SIP_TRANSPORT_TCP) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 2: tcp host failed.\n");
+ ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
+ transport != SIP_TRANSPORT_TCP) {
+ ast_test_status_update(test, "Test 2: tcp host failed.\n");
res = AST_TEST_FAIL;
}
/* test 3, add tls transport */
sip_parse_host(host3, 1, &host, &port, &transport);
if (port != STANDARD_TLS_PORT ||
- ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
- transport != SIP_TRANSPORT_TLS) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 3: tls host failed. \n");
+ ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
+ transport != SIP_TRANSPORT_TLS) {
+ ast_test_status_update(test, "Test 3: tls host failed. \n");
res = AST_TEST_FAIL;
}
/* test 4, add custom port with tls */
sip_parse_host(host4, 1, &host, &port, &transport);
- if (port != 1234 ||
- ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
- transport != SIP_TRANSPORT_TLS) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 4: tls host with custom port failed.\n");
+ if (port != 1234 || ast_strlen_zero(host) ||
+ strcmp(host, "10.10.10.10") ||
+ transport != SIP_TRANSPORT_TLS) {
+ ast_test_status_update(test, "Test 4: tls host with custom port failed.\n");
res = AST_TEST_FAIL;
}
/* test 5, simple host with custom port */
sip_parse_host(host5, 1, &host, &port, &transport);
- if (port != 1234 ||
- ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
- transport != SIP_TRANSPORT_UDP) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 5: simple host with custom port failed.\n");
- res = AST_TEST_FAIL;
- }
- return res;
+ if (port != 1234 || ast_strlen_zero(host) ||
+ strcmp(host, "10.10.10.10") ||
+ transport != SIP_TRANSPORT_UDP) {
+ ast_test_status_update(test, "Test 5: simple host with custom port failed.\n");
+ res = AST_TEST_FAIL;
+ }
/* test 6, expected failure with NULL input */
- if (sip_parse_host(NULL, 1, &host, &port, &transport)) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 6: expected error on NULL input did not occur.\n");
- res = AST_TEST_FAIL;
- }
+ if (!sip_parse_host(NULL, 1, &host, &port, &transport)) {
+ ast_test_status_update(test, "Test 6: expected error on NULL input did not occur.\n");
+ res = AST_TEST_FAIL;
+ }
+
return res;
}
Modified: trunk/channels/sip/reqresp_parser.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/sip/reqresp_parser.c?view=diff&rev=245864&r1=245863&r2=245864
==============================================================================
--- trunk/channels/sip/reqresp_parser.c (original)
+++ trunk/channels/sip/reqresp_parser.c Tue Feb 9 17:32:14 2010
@@ -139,66 +139,62 @@
/* Test 1, simple URI */
name = pass = domain = port = transport = NULL;
if (parse_uri(uri1, "sip:,sips:", &name, &pass, &domain, &port, &transport) ||
- strcmp(name, "name") ||
- !ast_strlen_zero(pass) ||
- strcmp(domain, "host") ||
- !ast_strlen_zero(port) ||
- !ast_strlen_zero(transport)) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple uri failed. \n");
+ strcmp(name, "name") ||
+ !ast_strlen_zero(pass) ||
+ strcmp(domain, "host") ||
+ !ast_strlen_zero(port) ||
+ !ast_strlen_zero(transport)) {
+ ast_test_status_update(test, "Test 1: simple uri failed. \n");
res = AST_TEST_FAIL;
}
/* Test 2, add tcp transport */
name = pass = domain = port = transport = NULL;
if (parse_uri(uri2, "sip:,sips:", &name, &pass, &domain, &port, &transport) ||
- strcmp(name, "name") ||
- !ast_strlen_zero(pass) ||
- strcmp(domain, "host") ||
- !ast_strlen_zero(port) ||
- strcmp(transport, "tcp")) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 2: uri with addtion of tcp transport failed. \n");
+ strcmp(name, "name") ||
+ !ast_strlen_zero(pass) ||
+ strcmp(domain, "host") ||
+ !ast_strlen_zero(port) ||
+ strcmp(transport, "tcp")) {
+ ast_test_status_update(test, "Test 2: uri with addtion of tcp transport failed. \n");
res = AST_TEST_FAIL;
}
/* Test 3, add secret */
name = pass = domain = port = transport = NULL;
if (parse_uri(uri3, "sip:,sips:", &name, &pass, &domain, &port, &transport) ||
- strcmp(name, "name") ||
- strcmp(pass, "secret") ||
- strcmp(domain, "host") ||
- !ast_strlen_zero(port) ||
- strcmp(transport, "tcp")) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 3: uri with addition of secret failed.\n");
+ strcmp(name, "name") ||
+ strcmp(pass, "secret") ||
+ strcmp(domain, "host") ||
+ !ast_strlen_zero(port) ||
+ strcmp(transport, "tcp")) {
+ ast_test_status_update(test, "Test 3: uri with addition of secret failed.\n");
res = AST_TEST_FAIL;
}
/* Test 4, add port and unparsed header field*/
name = pass = domain = port = transport = NULL;
if (parse_uri(uri4, "sip:,sips:", &name, &pass, &domain, &port, &transport) ||
- strcmp(name, "name") ||
- strcmp(pass, "secret") ||
- strcmp(domain, "host") ||
- strcmp(port, "port") ||
- strcmp(transport, "tcp")) {
-
- ast_str_append(&args->ast_test_error_str, 0, "Test 4: add port and unparsed header field failed.\n");
+ strcmp(name, "name") ||
+ strcmp(pass, "secret") ||
+ strcmp(domain, "host") ||
+ strcmp(port, "port") ||
+ strcmp(transport, "tcp")) {
+ ast_test_status_update(test, "Test 4: add port and unparsed header field failed.\n");
res = AST_TEST_FAIL;
}
/* Test 5, verify parse_uri does not crash when given a NULL uri */
name = pass = domain = port = transport = NULL;
if (!parse_uri(NULL, "sip:,sips:", &name, &pass, &domain, &port, &transport)) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 5: passing a NULL uri failed.\n");
+ ast_test_status_update(test, "Test 5: passing a NULL uri failed.\n");
res = AST_TEST_FAIL;
}
/* Test 6, verify parse_uri does not crash when given a NULL output parameters */
name = pass = domain = port = transport = NULL;
if (parse_uri(uri4, "sip:,sips:", NULL, NULL, NULL, NULL, NULL)) {
- ast_str_append(&args->ast_test_error_str, 0, "Test 6: passing NULL output parameters failed.\n");
+ ast_test_status_update(test, "Test 6: passing NULL output parameters failed.\n");
res = AST_TEST_FAIL;
}
@@ -339,46 +335,41 @@
/* quoted-text with backslash escaped quote */
after_dname = get_calleridname(in1, dname, sizeof(dname));
- ast_test_status_update(&args->status_update, "display-name1: %s\nafter: %s\n", dname, after_dname);
+ ast_test_status_update(test, "display-name1: %s\nafter: %s\n", dname, after_dname);
if (strcmp(dname, " quoted-text internal \" quote ")) {
- ast_test_status_update(&args->status_update, "display-name1 test failed\n");
- ast_str_append(&args->ast_test_error_str, 0, "quoted-text with internal backslash decode failed. \n");
+ ast_test_status_update(test, "display-name1 test failed\n");
res = AST_TEST_FAIL;
}
/* token text */
after_dname = get_calleridname(in2, dname, sizeof(dname));
- ast_test_status_update(&args->status_update, "display-name2: %s\nafter: %s\n", dname, after_dname);
+ ast_test_status_update(test, "display-name2: %s\nafter: %s\n", dname, after_dname);
if (strcmp(dname, "token text with no quotes")) {
- ast_test_status_update(&args->status_update, "display-name2 test failed\n");
- ast_str_append(&args->ast_test_error_str, 0, "token text with decode failed. \n");
+ ast_test_status_update(test, "display-name2 test failed\n");
res = AST_TEST_FAIL;
}
/* quoted-text buffer overflow */
after_dname = get_calleridname(overflow1, dname, sizeof(dname));
- ast_test_status_update(&args->status_update, "overflow display-name1: %s\nafter: %s\n", dname, after_dname);
+ ast_test_status_update(test, "overflow display-name1: %s\nafter: %s\n", dname, after_dname);
if (*dname != '\0' && after_dname != overflow1) {
- ast_test_status_update(&args->status_update, "overflow display-name1 test failed\n");
- ast_str_append(&args->ast_test_error_str, 0, "quoted-text buffer overflow check failed. \n");
+ ast_test_status_update(test, "overflow display-name1 test failed\n");
res = AST_TEST_FAIL;
}
/* quoted-text buffer with no terminating end quote */
after_dname = get_calleridname(noendquote, dname, sizeof(dname));
- ast_test_status_update(&args->status_update, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
+ ast_test_status_update(test, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
if (*dname != '\0' && after_dname != noendquote) {
- ast_test_status_update(&args->status_update, "no end quote for quoted-text display-name failed\n");
- ast_str_append(&args->ast_test_error_str, 0, "quoted-text buffer check no terminating end quote failed. \n");
+ ast_test_status_update(test, "no end quote for quoted-text display-name failed\n");
res = AST_TEST_FAIL;
}
/* addr-spec rather than display-name. */
after_dname = get_calleridname(addrspec, dname, sizeof(dname));
- ast_test_status_update(&args->status_update, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
+ ast_test_status_update(test, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname);
if (*dname != '\0' && after_dname != addrspec) {
- ast_test_status_update(&args->status_update, "detection of addr-spec failed\n");
- ast_str_append(&args->ast_test_error_str, 0, "detection of addr-spec failed. \n");
+ ast_test_status_update(test, "detection of addr-spec failed\n");
res = AST_TEST_FAIL;
}
Modified: trunk/funcs/func_math.c
URL: http://svnview.digium.com/svn/asterisk/trunk/funcs/func_math.c?view=diff&rev=245864&r1=245863&r2=245864
==============================================================================
--- trunk/funcs/func_math.c (original)
+++ trunk/funcs/func_math.c Tue Feb 9 17:32:14 2010
@@ -473,8 +473,7 @@
break;
}
- ast_test_status_update(&args->status_update, "Testing MATH() substitution ...\n");
- ast_str_reset(args->ast_test_error_str);
+ ast_test_status_update(test, "Testing MATH() substitution ...\n");
if (!(expr = ast_str_create(16)) || !(result = ast_str_create(16))) {
if (expr) {
@@ -489,14 +488,16 @@
ast_str_set(&expr, 0, "${MATH(170 AND 63,i)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "42") != 0) {
- ast_str_append(&args->ast_test_error_str, 0, "Expected result '42' not returned! ('%s')\n", ast_str_buffer(result));
+ ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n",
+ ast_str_buffer(result));
res = AST_TEST_FAIL;
}
ast_str_set(&expr, 0, "${MATH(170AND63,i)}");
ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
if (strcmp(ast_str_buffer(result), "42") != 0) {
- ast_str_append(&args->ast_test_error_str, 0, "Expected result '42' not returned! ('%s')\n", ast_str_buffer(result));
+ ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n",
+ ast_str_buffer(result));
res = AST_TEST_FAIL;
}
Modified: trunk/include/asterisk/test.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/test.h?view=diff&rev=245864&r1=245863&r2=245864
==============================================================================
--- trunk/include/asterisk/test.h (original)
+++ trunk/include/asterisk/test.h Tue Feb 9 17:32:14 2010
@@ -1,9 +1,10 @@
/*
* Asterisk -- An open source telephony toolkit.
*
- * Copyright (C) 2009, Digium, Inc.
+ * Copyright (C) 2009-2010, Digium, Inc.
*
* David Vossel <dvossel at digium.com>
+ * Russell Bryant <russell at digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
@@ -23,6 +24,7 @@
* For an overview on how to use the test API, see \ref AstUnitTestAPI
*
* \author David Vossel <dvossel at digium.com>
+ * \author Russell Bryant <russell at digium.com>
*/
#ifndef _AST_TEST_H_
@@ -46,7 +48,7 @@
Each defined test has three arguments avaliable to it's test code.
\param struct ast_test_info *info
\param enum ast_test_command cmd
- \param struct ast_test_args *args
+ \param struct ast_test *test
While these arguments are not visible they are passed to every test function
defined using the AST_TEST_DEFINE macro.
@@ -72,7 +74,7 @@
.
.
if (fail) { \\ the following is just some example logic
- ast_str_set(&args->ast_test_error_str, 0 , "an error occured because...");
+ ast_test_status_update(test, "an error occured because...");
res = AST_RESULT_FAIL;
} else {
res = AST_RESULT_PASS
@@ -81,10 +83,8 @@
}
\endcode
- Every callback function is passed an ast_test_args object which contains
- an ast_str allowing the function to provide an optional short description of
- what went wrong if the test failed. This is done by writing to
- args->ast_test_error_str.
+ Details of the test execution, especially failure details, should be provided
+ by using the ast_test_status_update() function.
\subsection RegisterTest Register a Test
@@ -117,13 +117,13 @@
/*! Macros used for defining and registering a test */
#ifdef TEST_FRAMEWORK
-#define AST_TEST_DEFINE(hdr) static enum ast_test_result_state hdr(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test_args *args)
+#define AST_TEST_DEFINE(hdr) static enum ast_test_result_state hdr(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test *test)
#define AST_TEST_REGISTER(cb) ast_test_register(cb)
#define AST_TEST_UNREGISTER(cb) ast_test_unregister(cb)
#else
-#define AST_TEST_DEFINE(hdr) static enum ast_test_result_state attribute_unused hdr(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test_args *args)
+#define AST_TEST_DEFINE(hdr) static enum ast_test_result_state attribute_unused hdr(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test *test)
#define AST_TEST_REGISTER(cb)
#define AST_TEST_UNREGISTER(cb)
#define ast_test_status_update(a,b,c...)
@@ -142,34 +142,23 @@
};
/*!
- * This struct is passed to ast_test_status_update() providing a place to push
- * the update to. In the future this structure may expand beyond simply being
- * a wrapper for cli args to including other status update options as well.
- */
-struct ast_test_status_args {
- /*! pointer to cli arg used for updating status */
- struct ast_cli_args *cli;
-};
-
-/*!
- * tools made available to the callback function during test execution
- */
-struct ast_test_args {
- struct ast_str *ast_test_error_str; /*! optional error str to describe error result */
- struct ast_test_status_args status_update;
-};
-
-/*!
- * Contains all the initilization information required to store a new test definition
+ * \brief An Asterisk unit test.
+ *
+ * This is an opaque type.
+ */
+struct ast_test;
+
+/*!
+ * \brief Contains all the initialization information required to store a new test definition
*/
struct ast_test_info {
- /*! name of test, unique to category */
+ /*! \brief name of test, unique to category */
const char *name;
- /*! test category */
+ /*! \brief test category */
const char *category;
- /*! optional short summary of test */
+ /*! \brief optional short summary of test */
const char *summary;
- /*! optional brief detailed description of test */
+ /*! \brief optional brief detailed description of test */
const char *description;
};
@@ -182,7 +171,8 @@
* \retval AST_TEST_PASS for pass
* \retval AST_TEST_FAIL for failure
*/
-typedef enum ast_test_result_state (ast_test_cb_t)(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test_args *args);
+typedef enum ast_test_result_state (ast_test_cb_t)(struct ast_test_info *info,
+ enum ast_test_command cmd, struct ast_test *test);
/*!
* \brief unregisters a test with the test framework
@@ -207,13 +197,19 @@
/*!
* \brief update test's status during testing.
*
- * \param ast_test_status_args defines everywhere the update should go.
+ * \param test currently executing test
*
* \retval 0 success
* \retval -1 failure
*/
-int ast_test_status_update(struct ast_test_status_args *args, const char *fmt, ...)
-__attribute__((format(printf, 2, 3)));
+int __ast_test_status_update(const char *file, const char *func, int line,
+ struct ast_test *test, const char *fmt, ...)
+ __attribute__((format(printf, 5, 6)));
+
+/*!
+ * \ref __ast_test_status_update()
+ */
+#define ast_test_status_update(t, f, ...) __ast_test_status_update(__FILE__, __PRETTY_FUNCTION__, __LINE__, (t), (f), ## __VA_ARGS__)
#endif /* TEST_FRAMEWORK */
#endif /* _AST_TEST_H */
Modified: trunk/include/asterisk/utils.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/utils.h?view=diff&rev=245864&r1=245863&r2=245864
==============================================================================
--- trunk/include/asterisk/utils.h (original)
+++ trunk/include/asterisk/utils.h Tue Feb 9 17:32:14 2010
@@ -758,8 +758,4 @@
*/
int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2);
-/*!
- * \brief Registers util api unit tests
- */
-void ast_utils_register_tests(void);
#endif /* _ASTERISK_UTILS_H */
Modified: trunk/main/test.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/test.c?view=diff&rev=245864&r1=245863&r2=245864
==============================================================================
--- trunk/main/test.c (original)
+++ trunk/main/test.c Tue Feb 9 17:32:14 2010
@@ -1,9 +1,10 @@
/*
* Asterisk -- An open source telephony toolkit.
*
- * Copyright (C) 2009, Digium, Inc.
+ * Copyright (C) 2009-2010, Digium, Inc.
*
* David Vossel <dvossel at digium.com>
+ * Russell Bryant <russell at 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 Test Framework
*
* \author David Vossel <dvossel at digium.com>
+ * \author Russell Bryant <russell at digium.com>
*/
#include "asterisk.h"
@@ -43,14 +45,24 @@
/*! This array corresponds to the values defined in the ast_test_state enum */
static const char * const test_result2str[] = {
[AST_TEST_NOT_RUN] = "NOT RUN",
- [AST_TEST_PASS] = "PASS",
- [AST_TEST_FAIL] = "FAIL",
+ [AST_TEST_PASS] = "PASS",
+ [AST_TEST_FAIL] = "FAIL",
};
/*! holds all the information pertaining to a single defined test */
struct ast_test {
struct ast_test_info info; /*!< holds test callback information */
- struct ast_test_args args; /*!< function callback arguments */
+ /*!
+ * \brief Test defined status output from last execution
+ */
+ struct ast_str *status_str;
+ /*!
+ * \brief CLI arguments, if tests being run from the CLI
+ *
+ * If this is set, status updates from the tests will be sent to the
+ * CLI in addition to being saved off in status_str.
+ */
+ struct ast_cli_args *cli;
enum ast_test_result_state state; /*!< current test state */
unsigned int time; /*!< time in ms test took */
ast_test_cb_t *cb; /*!< test callback function */
@@ -77,22 +89,17 @@
/*! List of registered test definitions */
static AST_LIST_HEAD_STATIC(tests, ast_test);
-/*! static function prototypes */
static struct ast_test *test_alloc(ast_test_cb_t *cb);
static struct ast_test *test_free(struct ast_test *test);
static int test_insert(struct ast_test *test);
static struct ast_test *test_remove(ast_test_cb_t *cb);
static int test_cat_cmp(const char *cat1, const char *cat2);
-int ast_test_status_update(struct ast_test_status_args *args, const char *fmt, ...)
+int __ast_test_status_update(const char *file, const char *func, int line,
+ struct ast_test *test, const char *fmt, ...)
{
struct ast_str *buf = NULL;
va_list ap;
-
- /* it is not an error if no cli args exist. */
- if (!args->cli) {
- return 0;
- }
if (!(buf = ast_str_create(128))) {
return -1;
@@ -102,9 +109,16 @@
ast_str_set_va(&buf, 0, fmt, ap);
va_end(ap);
- ast_cli(args->cli->fd, "%s", ast_str_buffer(buf));
+ if (test->cli) {
+ ast_cli(test->cli->fd, "[%s:%s:%d]: %s",
+ file, func, line, ast_str_buffer(buf));
+ }
+
+ ast_str_append(&test->status_str, 0, "[%s:%s:%d]: %s",
+ file, func, line, ast_str_buffer(buf));
ast_free(buf);
+
return 0;
}
@@ -112,18 +126,15 @@
{
struct ast_test *test;
- /* verify data.*/
if (!cb) {
ast_log(LOG_WARNING, "Attempted to register test without all required information\n");
return -1;
}
- /* create test object */
if (!(test = test_alloc(cb))) {
return -1;
}
- /* insert into list */
if (test_insert(test)) {
test_free(test);
return -1;
@@ -136,12 +147,10 @@
{
struct ast_test *test;
- /* find test and remove */
if (!(test = test_remove(cb))) {
return -1; /* not found */
}
- /* free test object */
test_free(test);
return 0;
@@ -158,16 +167,11 @@
{
struct timeval begin;
- /* clear any previous error results before starting */
- ast_str_reset(test->args.ast_test_error_str);
- /* get start time */
+ ast_str_reset(test->status_str);
+
begin = ast_tvnow();
- /* the callback gets the pointer to the pointer of the error buf */
- test->state = test->cb(&test->info, TEST_EXECUTE, &test->args);
- /* record the total time the test took */
+ test->state = test->cb(&test->info, TEST_EXECUTE, test);
test->time = ast_tvdiff_ms(ast_tvnow(), begin);
- /* clear any status update args that may have been set */
- memset(&test->args.status_update, 0, sizeof(struct ast_test_status_args));
}
static void test_xml_entry(struct ast_test *test, FILE *f)
@@ -183,7 +187,7 @@
if (test->state == AST_TEST_FAIL) {
fprintf(f, "\t\t<failure>%s</failure>\n",
- S_OR(ast_str_buffer(test->args.ast_test_error_str), "NA"));
+ S_OR(ast_str_buffer(test->status_str), "NA"));
fprintf(f, "\t</testcase>\n");
}
@@ -200,11 +204,11 @@
fprintf(f, "Summary: %s\n", test->info.summary);
fprintf(f, "Description: %s\n", test->info.description);
fprintf(f, "Result: %s\n", test_result2str[test->state]);
- if (test->state == AST_TEST_FAIL) {
- fprintf(f, "Error Description: %s\n", S_OR(ast_str_buffer(test->args.ast_test_error_str), "NA"));
- }
if (test->state != AST_TEST_NOT_RUN) {
fprintf(f, "Time: %d\n", test->time);
+ }
+ if (test->state == AST_TEST_FAIL) {
+ fprintf(f, "Error Description: %s\n\n", S_OR(ast_str_buffer(test->status_str), "NA"));
}
}
@@ -266,10 +270,12 @@
}
/* set the test status update argument. it is ok if cli is NULL */
- test->args.status_update.cli = cli;
+ test->cli = cli;
/* execute the test and save results */
test_execute(test);
+
+ test->cli = NULL;
/* update execution specific counts here */
last_results.last_time += test->time;
@@ -285,12 +291,11 @@
(test->state == AST_TEST_FAIL) ? COLOR_RED : COLOR_GREEN,
0,
sizeof(result_buf));
- ast_cli(cli->fd, "END %s - %s Time: %dms Result: %s %s\n",
+ ast_cli(cli->fd, "END %s - %s Time: %dms Result: %s\n",
test->info.category,
test->info.name,
test->time,
- result_buf,
- ast_str_buffer(test->args.ast_test_error_str));
+ result_buf);
}
}
@@ -432,46 +437,20 @@
* \internal
* \brief adds test to container sorted first by category then by name
*
- * \return 0 on success, -1 on failure
+ * \retval 0 success
+ * \retval -1 failure
*/
static int test_insert(struct ast_test *test)
{
- struct ast_test *cur = NULL;
- int res = 0;
- int i = 0;
- int inserted = 0;
-
/* This is a slow operation that may need to be optimized in the future
* as the test framework expands. At the moment we are doing string
* comparisons on every item within the list to insert in sorted order. */
+
AST_LIST_LOCK(&tests);
- AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, cur, entry) {
- if ((i = strcmp(test->info.category, cur->info.category)) < 0) {
- AST_LIST_INSERT_BEFORE_CURRENT(test, entry);
- inserted = 1;
- break;
- } else if (!i) { /* same category, now insert by name within that category*/
- if ((i = strcmp(test->info.name, cur->info.name)) < 0) {
- AST_LIST_INSERT_BEFORE_CURRENT(test, entry);
- inserted = 1;
- break;
- } else if (!i) {
- /* Error, duplicate found */
- res = -1;
- break;
- }
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
-
- if (!inserted && !res) {
- AST_LIST_INSERT_TAIL(&tests, test, entry);
- inserted = 1;
- }
-
+ AST_LIST_INSERT_SORTALPHA(&tests, test, entry, info.category);
AST_LIST_UNLOCK(&tests);
- return res;
+ return 0;
}
/*!
@@ -501,7 +480,8 @@
* \brief compares two test categories to determine if cat1 resides in cat2
* \internal
*
- * \return 0 if true
+ * \retval 0 true
+ * \retval non-zero false
*/
static int test_cat_cmp(const char *cat1, const char *cat2)
@@ -524,8 +504,8 @@
}
/*!
- * \brief frees a ast_test object and all it's data members
* \internal
+ * \brief free an ast_test object and all it's data members
*/
static struct ast_test *test_free(struct ast_test *test)
{
@@ -533,7 +513,7 @@
return NULL;
}
- ast_free(test->args.ast_test_error_str);
+ ast_free(test->status_str);
ast_free(test);
return NULL;
@@ -541,7 +521,7 @@
/*!
* \internal
- * \brief allocates an ast_test object.
+ * \brief allocate an ast_test object.
*/
static struct ast_test *test_alloc(ast_test_cb_t *cb)
{
@@ -553,14 +533,32 @@
test->cb = cb;
- test->cb(&test->info, TEST_INIT, &test->args);
-
- if (ast_strlen_zero(test->info.name) ||
- ast_strlen_zero(test->info.category) ||
- ast_strlen_zero(test->info.summary) ||
- ast_strlen_zero(test->info.description) ||
- !(test->args.ast_test_error_str = ast_str_create(128))) {
-
+ test->cb(&test->info, TEST_INIT, test);
+
+ if (ast_strlen_zero(test->info.name)) {
+ ast_log(LOG_WARNING, "Test has no name, test registration refused.\n");
+ return test_free(test);
+ }
+
+ if (ast_strlen_zero(test->info.category)) {
+ ast_log(LOG_WARNING, "Test %s has no category, test registration refused.\n",
+ test->info.name);
+ return test_free(test);
+ }
+
+ if (ast_strlen_zero(test->info.summary)) {
+ ast_log(LOG_WARNING, "Test %s/%s has no summary, test registration refused.\n",
+ test->info.category, test->info.name);
+ return test_free(test);
+ }
+
+ if (ast_strlen_zero(test->info.description)) {
+ ast_log(LOG_WARNING, "Test %s/%s has no description, test registration refused.\n",
+ test->info.category, test->info.name);
+ return test_free(test);
+ }
+
+ if (!(test->status_str = ast_str_create(128))) {
return test_free(test);
}
@@ -570,7 +568,7 @@
/* CLI commands */
static char *test_cli_show_registered(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
-#define FORMAT "%-30.30s %-25.25s %-40.40s %-13.13s\n"
+#define FORMAT "%-25.25s %-30.30s %-40.40s %-13.13s\n"
static const char * const option1[] = { "all", "category", NULL };
static const char * const option2[] = { "name", NULL };
struct ast_test *test = NULL;
@@ -601,19 +599,22 @@
((a->argc == 7) && strcmp(a->argv[5], "name"))) {
return CLI_SHOWUSAGE;
}
- ast_cli(a->fd, FORMAT, "Name", "Category", "Summary", "Test Result");
+ ast_cli(a->fd, FORMAT, "Category", "Name", "Summary", "Test Result");
+ ast_cli(a->fd, FORMAT, "--------", "----", "-------", "-----------");
AST_LIST_LOCK(&tests);
AST_LIST_TRAVERSE(&tests, test, entry) {
if ((a->argc == 4) ||
((a->argc == 5) && !test_cat_cmp(test->info.category, a->argv[4])) ||
((a->argc == 7) && !strcmp(test->info.category, a->argv[4]) && !strcmp(test->info.name, a->argv[6]))) {
- ast_cli(a->fd, FORMAT, test->info.name, test->info.category, test->info.summary, test_result2str[test->state]);
- count ++;
+ ast_cli(a->fd, FORMAT, test->info.category, test->info.name,
+ test->info.summary, test_result2str[test->state]);
+ count++;
}
}
AST_LIST_UNLOCK(&tests);
- ast_cli(a->fd, "%d Registered Tests Matched\n", count);
+ ast_cli(a->fd, FORMAT, "--------", "----", "-------", "-----------");
+ ast_cli(a->fd, "\n%d Registered Tests Matched\n", count);
default:
return NULL;
}
@@ -625,6 +626,7 @@
{
static const char * const option1[] = { "all", "category", NULL };
static const char * const option2[] = { "name", NULL };
+
switch (cmd) {
case CLI_INIT:
e->command = "test execute";
@@ -681,7 +683,7 @@
static char *test_cli_show_results(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
-#define FORMAT_RES_ALL "%s%s %-30.30s %-25.25s %-40.40s\n"
+#define FORMAT_RES_ALL "%s%s %-30.30s %-25.25s\n"
static const char * const option1[] = { "all", "failed", "passed", NULL };
char result_buf[32] = { 0 };
struct ast_test *test = NULL;
@@ -718,7 +720,7 @@
return CLI_SHOWUSAGE;
}
- ast_cli(a->fd, FORMAT_RES_ALL, "Result", "", "Name", "Category", "Error Description");
+ ast_cli(a->fd, FORMAT_RES_ALL, "Result", "", "Name", "Category");
AST_LIST_LOCK(&tests);
AST_LIST_TRAVERSE(&tests, test, entry) {
if (test->state == AST_TEST_NOT_RUN) {
@@ -735,8 +737,7 @@
result_buf,
" ",
test->info.name,
- test->info.category,
- (test->state == AST_TEST_FAIL) ? S_OR(ast_str_buffer(test->args.ast_test_error_str), "Not Avaliable") : "");
+ test->info.category);
}
}
AST_LIST_UNLOCK(&tests);
Modified: trunk/tests/test_ast_format_str_reduce.c
URL: http://svnview.digium.com/svn/asterisk/trunk/tests/test_ast_format_str_reduce.c?view=diff&rev=245864&r1=245863&r2=245864
==============================================================================
--- trunk/tests/test_ast_format_str_reduce.c (original)
+++ trunk/tests/test_ast_format_str_reduce.c Tue Feb 9 17:32:14 2010
@@ -94,12 +94,14 @@
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 @@
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;
}
}
Modified: trunk/tests/test_heap.c
URL: http://svnview.digium.com/svn/asterisk/trunk/tests/test_heap.c?view=diff&rev=245864&r1=245863&r2=245864
==============================================================================
[... 667 lines stripped ...]
More information about the asterisk-commits
mailing list