SDP: Make process possible multiple fmtp attributes per rtpmap.
[asterisk/asterisk.git] / tests / test_logger.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * Kevin P. Fleming <kpfleming@digium.com>
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 /*! \file
20  *
21  * \brief Test module for the logging subsystem
22  *
23  * \author\verbatim Kevin P. Fleming <kpfleming@digium.com> \endverbatim
24  *
25  * \ingroup tests
26  */
27
28 /*** MODULEINFO
29         <depend>TEST_FRAMEWORK</depend>
30         <support_level>core</support_level>
31  ***/
32
33 #include "asterisk.h"
34
35 #include "asterisk/file.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/module.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/app.h"
41 #include "asterisk/cli.h"
42
43 struct test {
44         const char *name;
45         unsigned int x_success;
46         unsigned int x_failure;
47         unsigned int u_success;
48         unsigned int u_failure;
49 };
50
51 static void output_tests(struct test *tests, size_t num_tests, int fd)
52 {
53         unsigned int x;
54
55         for (x = 0; x < num_tests; x++) {
56                 ast_cli(fd, "Test %u: %s\n", x + 1, tests[x].name);
57                 ast_cli(fd, "\tExpected Successes: %u\n", tests[x].x_success);
58                 ast_cli(fd, "\tExpected Failures: %u\n", tests[x].x_failure);
59                 ast_cli(fd, "\tUnexpected Successes: %u\n", tests[x].u_success);
60                 ast_cli(fd, "\tUnexpected Failures: %u\n", tests[x].u_failure);
61                 ast_cli(fd, "Test %u Result: %s\n", x + 1, (tests[x].u_success + tests[x].u_failure) ? "FAIL" : "PASS");
62         }
63 }
64
65 static char *handle_cli_dynamic_level_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
66 {
67         unsigned int level;
68         unsigned int x;
69         unsigned int test;
70         struct test tests[] = {
71                 { .name = "Simple register/message/unregister",
72                 },
73                 { .name = "Register multiple levels",
74                 },
75         };
76
77         switch (cmd) {
78         case CLI_INIT:
79                 e->command = "logger test dynamic";
80                 e->usage = ""
81                         "Usage: logger test dynamic\n"
82                         "";
83                 return NULL;
84         case CLI_GENERATE:
85                 return NULL;
86         }
87
88         for (test = 0; test < ARRAY_LEN(tests); test++) {
89                 ast_cli(a->fd, "Test %u: %s.\n", test + 1, tests[test].name);
90                 switch (test) {
91                 case 0:
92                         if ((level = ast_logger_register_level("test")) != -1) {
93                                 ast_cli(a->fd, "Test: got level %u\n", level);
94                                 ast_log_dynamic_level(level, "Logger Dynamic Test: Test 1\n");
95                                 ast_logger_unregister_level("test");
96                                 tests[test].x_success++;
97                         } else {
98                                 ast_cli(a->fd, "Test: Failed, could not register level 'test'.\n");
99                                 tests[test].u_failure++;
100                         }
101                         break;
102                 case 1:
103                 {
104                         char level_name[18][8];
105
106                         for (x = 0; x < ARRAY_LEN(level_name); x++) {
107                                 sprintf(level_name[x], "level%02u", x);
108                                 if ((level = ast_logger_register_level(level_name[x])) == -1) {
109                                         if (x < 16) {
110                                                 tests[test].u_failure++;
111                                         } else {
112                                                 tests[test].x_failure++;
113                                         }
114                                         level_name[x][0] = '\0';
115                                 } else {
116                                         ast_cli(a->fd, "Test: registered '%s', got level %u\n", level_name[x], level);
117                                         if (x < 16) {
118                                                 tests[test].x_success++;
119                                         } else {
120                                                 tests[test].u_success++;
121                                         }
122                                 }
123                         }
124
125                         for (x = 0; x < ARRAY_LEN(level_name); x++) {
126                                 if (!ast_strlen_zero(level_name[x])) {
127                                         ast_logger_unregister_level(level_name[x]);
128                                 }
129                         }
130                 }
131                 }
132         }
133
134         output_tests(tests, ARRAY_LEN(tests), a->fd);
135
136         return CLI_SUCCESS;
137 }
138
139 static char *handle_cli_performance_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
140 {
141         unsigned int level;
142         unsigned int test;
143         struct test tests[] = {
144                 { .name = "Log 10,000 messages",
145                 },
146         };
147
148         switch (cmd) {
149         case CLI_INIT:
150                 e->command = "logger test performance";
151                 e->usage = ""
152                         "Usage: logger test performance\n"
153                         "";
154                 return NULL;
155         case CLI_GENERATE:
156                 return NULL;
157         }
158
159         for (test = 0; test < ARRAY_LEN(tests); test++) {
160                 ast_cli(a->fd, "Test %u: %s.\n", test + 1, tests[test].name);
161                 switch (test) {
162                 case 0:
163                         if ((level = ast_logger_register_level("perftest")) != -1) {
164                                 unsigned int x;
165                                 struct timeval start, end;
166                                 int elapsed;
167
168                                 ast_cli(a->fd, "Test: got level %u\n", level);
169                                 start = ast_tvnow();
170                                 for (x = 0; x < 10000; x++) {
171                                         ast_log_dynamic_level(level, "Performance test log message\n");
172                                 }
173                                 end = ast_tvnow();
174                                 elapsed = ast_tvdiff_ms(end, start);
175                                 ast_cli(a->fd, "Test: 10,000 messages in %f seconds.\n", (float) elapsed / 1000);
176                                 ast_logger_unregister_level("perftest");
177                                 tests[test].x_success++;
178                         } else {
179                                 ast_cli(a->fd, "Test: Failed, could not register level 'perftest'.\n");
180                                 tests[test].u_failure++;
181                         }
182                         break;
183                 }
184         }
185
186         output_tests(tests, ARRAY_LEN(tests), a->fd);
187
188         return CLI_SUCCESS;
189 }
190
191 static struct ast_cli_entry cli_logger[] = {
192         AST_CLI_DEFINE(handle_cli_dynamic_level_test, "Test the dynamic logger level implementation"),
193         AST_CLI_DEFINE(handle_cli_performance_test, "Test the logger performance"),
194 };
195
196 static int unload_module(void)
197 {
198         ast_cli_unregister_multiple(cli_logger, ARRAY_LEN(cli_logger));
199         return 0;
200 }
201
202 static int load_module(void)
203 {
204         ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
205         return AST_MODULE_LOAD_SUCCESS;
206 }
207
208 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logger Test Module");