Debian Codegraph @

tests/test_skel.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) <Year>, <Your Name Here>
5 *
6 * <Your Name Here> <<Your Email Here>>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*!
20 * \file
21 * \brief Skeleton Test
22 *
23 * \author\verbatim <Your Name Here> <<Your Email Here>> \endverbatim
24 *
25 * This is a skeleton for development of an Asterisk test module
26 * \ingroup tests
27 */
28
29 /*** MODULEINFO
30 <depend>TEST_FRAMEWORK</depend>
31 <support_level>core</support_level>
32 ***/
33
34 #include "asterisk.h"
35
36 #include "asterisk/utils.h"
37 #include "asterisk/module.h"
38 #include "asterisk/test.h"
39
40 AST_TEST_DEFINE(sample_test)
41 {
42 /* Retrieve the command line arguments used to invoke the test */
43 struct ast_cli_args *cli_args = ast_test_get_cli_args(test);
44 /* Set default values for the options */
45 int test_option = 999;
46 char test_option2[128] = { 0 };
47 void *ptr = NULL;
48 void *ptr2 = NULL;
49 int i;
50 enum ast_test_result_state rc = AST_TEST_PASS;
51
52 switch (cmd) {
53 case TEST_INIT:
54 info->name = "sample_test";
55 info->category = "/main/sample/";
56 info->summary = "sample unit test";
57 info->description =
58 "This demonstrates what is required to implement "
59 "a unit test. You can pass in test-option and "
60 "test-option2 as command line arguments to this "
61 "test. test-option is an integer and test-option2 "
62 "is a string.";
63 return AST_TEST_NOT_RUN;
64 case TEST_EXECUTE:
65 break;
66 }
67
68 /*
69 * This is an example of how to get command line arguments
70 * from the test framework. The arguments are "test-option"
71 * (expected to be an integer) and "test-option2" (expected
72 * to be a string).
73 *
74 * NOTES:
75 *
76 * cli_args will contain all of the command line arguments
77 * including "test execute", etc. so the location of the options
78 * will vary depending on how the test was invoked.
79 * For instance, this test could be run by either of the following:
80 *
81 * test execute category /main/sample/ options test-option=444
82 * test execute category /main/sample/ name sample_test options test-option=444
83 *
84 * You therefore need to test each of the items in the argv array
85 * to find the ones you are looking for.
86 *
87 * No special processing is done on string arguments so if your
88 * option value is a string, you must deal with the possibility
89 * of embedded spaces yourself.
90 */
91
92 for (i = 0; i < cli_args->argc; i++) {
93 ast_test_status_update(test, "Test argument: %d: %s\n", i, cli_args->argv[i]);
94 if (ast_begins_with(cli_args->argv[i], "test-option=")) {
95 sscanf(cli_args->argv[i], "test-option=%d", &test_option);
96 }
97 if (ast_begins_with(cli_args->argv[i], "test-option2=")) {
98 sscanf(cli_args->argv[i], "test-option2=%s", test_option2);
99 }
100 }
101
102 ast_test_status_update(test, "Executing sample test with test-option=%d and test-option2=%s\n",
103 test_option, test_option2);
104
105 if (!(ptr = ast_malloc(8))) {
106 ast_test_status_update(test, "ast_malloc() failed\n");
107 return AST_TEST_FAIL;
108 }
109
110 ptr2 = ast_malloc(8);
111 /*
112 * This is an example of how to use the ast_test_validate_cleanup_custom
113 * macro to check a condition and cleanup if it fails.
114 * If ptr2 is NULL, rc will be set to AST_TEST_FAIL, the specified
115 * message will be printed, and the test will jump to the "done"
116 * label to perform cleanup.
117 */
118 ast_test_validate_cleanup_custom(test, ptr2, rc, done, "ptr2 is NULL\n");
119
120 done:
121
122 ast_free(ptr);
123 ast_free(ptr2);
124
125 return rc;
126 }
127
128 static int unload_module(void)
129 {
130 AST_TEST_UNREGISTER(sample_test);
131 return 0;
132 }
133
134 static int load_module(void)
135 {
136 AST_TEST_REGISTER(sample_test);
137 return AST_MODULE_LOAD_SUCCESS;
138 }
139
140 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Skeleton (sample) Test");