Files
asterisk/tests/test_skel.c

141 lines
3.9 KiB
C
Raw Normal View History

/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) <Year>, <Your Name Here>
*
* <Your Name Here> <<Your Email Here>>
*
* 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 Skeleton Test
*
* \author\verbatim <Your Name Here> <<Your Email Here>> \endverbatim
*
* This is a skeleton for development of an Asterisk test module
* \ingroup tests
*/
/*** MODULEINFO
<depend>TEST_FRAMEWORK</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
#include "asterisk/utils.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
AST_TEST_DEFINE(sample_test)
{
/* Retrieve the command line arguments used to invoke the test */
struct ast_cli_args *cli_args = ast_test_get_cli_args(test);
/* Set default values for the options */
int test_option = 999;
char test_option2[128] = { 0 };
void *ptr = NULL;
void *ptr2 = NULL;
int i;
enum ast_test_result_state rc = AST_TEST_PASS;
switch (cmd) {
case TEST_INIT:
info->name = "sample_test";
info->category = "/main/sample/";
info->summary = "sample unit test";
info->description =
"This demonstrates what is required to implement "
"a unit test. You can pass in test-option and "
"test-option2 as command line arguments to this "
"test. test-option is an integer and test-option2 "
"is a string.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/*
* This is an example of how to get command line arguments
* from the test framework. The arguments are "test-option"
* (expected to be an integer) and "test-option2" (expected
* to be a string).
*
* NOTES:
*
* cli_args will contain all of the command line arguments
* including "test execute", etc. so the location of the options
* will vary depending on how the test was invoked.
* For instance, this test could be run by either of the following:
*
* test execute category /main/sample/ options test-option=444
* test execute category /main/sample/ name sample_test options test-option=444
*
* You therefore need to test each of the items in the argv array
* to find the ones you are looking for.
*
* No special processing is done on string arguments so if your
* option value is a string, you must deal with the possibility
* of embedded spaces yourself.
*/
for (i = 0; i < cli_args->argc; i++) {
ast_test_status_update(test, "Test argument: %d: %s\n", i, cli_args->argv[i]);
if (ast_begins_with(cli_args->argv[i], "test-option=")) {
sscanf(cli_args->argv[i], "test-option=%d", &test_option);
}
if (ast_begins_with(cli_args->argv[i], "test-option2=")) {
sscanf(cli_args->argv[i], "test-option2=%s", test_option2);
}
}
ast_test_status_update(test, "Executing sample test with test-option=%d and test-option2=%s\n",
test_option, test_option2);
if (!(ptr = ast_malloc(8))) {
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
2010-02-09 23:32:14 +00:00
ast_test_status_update(test, "ast_malloc() failed\n");
return AST_TEST_FAIL;
}
ptr2 = ast_malloc(8);
/*
* This is an example of how to use the ast_test_validate_cleanup_custom
* macro to check a condition and cleanup if it fails.
* If ptr2 is NULL, rc will be set to AST_TEST_FAIL, the specified
* message will be printed, and the test will jump to the "done"
* label to perform cleanup.
*/
ast_test_validate_cleanup_custom(test, ptr2, rc, done, "ptr2 is NULL\n");
done:
ast_free(ptr);
ast_free(ptr2);
return rc;
}
static int unload_module(void)
{
AST_TEST_UNREGISTER(sample_test);
return 0;
}
static int load_module(void)
{
AST_TEST_REGISTER(sample_test);
return AST_MODULE_LOAD_SUCCESS;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Skeleton (sample) Test");