Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "test.h"
20 #include <pjlib.h>
21
22 #define THIS_FILE "test.c"
23
24 #ifdef _MSC_VER
25 # pragma warning(disable:4127)
26
27 /* Disable ioqueue stress test on MSVC2005, due to compile errors on
28 * structure field assignments.
29 */
30 # if _MSC_VER <= 1400
31 # undef INCLUDE_IOQUEUE_STRESS_TEST
32 # define INCLUDE_IOQUEUE_STRESS_TEST 0
33 # endif
34
35 #endif
36
37 pj_pool_factory *mem;
38
39 struct test_app_t test_app = {
40 {0}, /* .ut_app */
41 0, /* .param_echo_sock_type */
42 ECHO_SERVER_ADDRESS, /* .param_echo_server */
43 ECHO_SERVER_START_PORT, /* .param_echo_port */
44 PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_TIME |
45 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_INDENT,
46 /* .param_log_decor */
47 };
48
49 int null_func()
50 {
51 return 0;
52 }
53
54 static pj_bool_t test_included(const char *name, int argc, char *argv[])
55 {
56 if (argc <= 1)
57 return PJ_TRUE;
58
59 ++argv;
60 while (*argv) {
61 if (pj_ansi_strcmp(name, *argv)==0)
62 return PJ_TRUE;
63 ++argv;
64 }
65 return PJ_FALSE;
66 }
67
68 static pj_test_case *init_test_case( int (*test_func)(void), const char *obj_name,
69 unsigned flags, pj_test_case *tc,
70 char *log_buf, unsigned log_buf_size)
71 {
72 flags |= PJ_TEST_FUNC_NO_ARG;
73 pj_test_case_init(tc, obj_name, flags, (int (*)(void*))test_func, NULL,
74 log_buf, log_buf_size, NULL);
75 return tc;
76 }
77
78 static void list_tests(const pj_test_suite *suite, const char *title)
79 {
80 unsigned d = pj_log_get_decor();
81 const pj_test_case *tc;
82
83 pj_log_set_decor(d ^ PJ_LOG_HAS_NEWLINE);
84 PJ_LOG(3,(THIS_FILE, "%ld %s:", pj_list_size(&suite->tests), title));
85 pj_log_set_decor(0);
86 for (tc=suite->tests.next; tc!=&suite->tests; tc=tc->next) {
87 PJ_LOG(3,(THIS_FILE, " %s", tc->obj_name));
88 }
89 PJ_LOG(3,(THIS_FILE, "\n"));
90 pj_log_set_decor(d);
91 }
92
93 static pj_test_stat essential_tests(int argc, char *argv[])
94 {
95 pj_test_suite suite;
96 pj_test_runner runner;
97 pj_test_stat stat;
98 enum {
99 MAX_TESTS = 12,
100 LOG_BUF_SIZE = 256,
101 };
102 char log_bufs[MAX_TESTS][LOG_BUF_SIZE];
103 pj_test_case test_cases[MAX_TESTS];
104 int ntests = 0;
105
106 pj_bzero(&stat, sizeof(stat));
107 /* Not necessary but to prevent unused-var warning when no test */
108 pj_bzero(test_cases, sizeof(test_cases));
109 pj_bzero(log_bufs, sizeof(log_bufs));
110
111 if (test_app.ut_app.prm_config)
112 pj_dump_config();
113
114 /* Test the unit-testing framework first, outside unit-test!
115 * Only perform the test if user is not requesting specific test.
116 */
117 if (argc==1 && !test_app.ut_app.prm_list_test) {
118 PJ_LOG(3,(THIS_FILE, "Testing the unit-test framework (basic)"));
119 if (unittest_basic_test()) {
120 stat.nfailed = 1;
121 return stat;
122 }
123 }
124
125 /* Now that the basic unit-testing framework has been tested,
126 * perform essential tests using basic unit-testing framework.
127 */
128 pj_test_suite_init(&suite);
129
130 #define ADD_TEST(test_func, flags) \
131 if (ntests < MAX_TESTS) { \
132 const char *test_name = #test_func; \
133 if (test_included(test_name, argc, argv)) { \
134 pj_test_case *tc = init_test_case( &test_func, test_name, flags, \
135 &test_cases[ntests], \
136 log_bufs[ntests], \
137 LOG_BUF_SIZE); \
138 pj_test_suite_add_case( &suite, tc); \
139 ++ntests; \
140 } \
141 } else { \
142 PJ_LOG(1,(THIS_FILE, "Too many tests for adding %s", #test_func)); \
143 }
144
145 #if INCLUDE_ERRNO_TEST
146 ADD_TEST( errno_test, 0);
147 #endif
148
149 #if INCLUDE_EXCEPTION_TEST
150 ADD_TEST( exception_test, 0);
151 #endif
152
153 #if INCLUDE_OS_TEST
154 ADD_TEST( os_test, 0);
155 #endif
156
157 #if INCLUDE_LIST_TEST
158 ADD_TEST( list_test, 0);
159 #endif
160
161 #if INCLUDE_ATOMIC_SLIST_TEST
162 ADD_TEST( atomic_slist_test, 0);
163 #endif
164
165 #if INCLUDE_MT_ATOMIC_SLIST_TEST
166 ADD_TEST( atomic_slist_mt_test, PJ_TEST_EXCLUSIVE|PJ_TEST_FUNC_NO_ARG|PJ_TEST_LOG_NO_CACHE);
167 #endif
168
169 #if INCLUDE_POOL_TEST
170 ADD_TEST( pool_test, 0);
171 #endif
172
173 #if INCLUDE_STRING_TEST
174 ADD_TEST( string_test, 0);
175 #endif
176
177 #if INCLUDE_FIFOBUF_TEST
178 ADD_TEST( fifobuf_test, 0);
179 #endif
180
181 #if INCLUDE_MUTEX_TEST
182 ADD_TEST( mutex_test, 0);
183 #endif
184
185 #if INCLUDE_THREAD_TEST
186 ADD_TEST( thread_test, 0);
187 #endif
188
189 #undef ADD_TEST
190
191 if (test_app.ut_app.prm_list_test) {
192 list_tests(&suite, "essential tests");
193 return stat;
194 }
195
196 if (ntests > 0) {
197 pj_test_runner_param runner_prm;
198 pj_test_runner_param_default(&runner_prm);
199 runner_prm.stop_on_error = test_app.ut_app.prm_stop_on_error;
200
201 PJ_LOG(3,(THIS_FILE, "Performing %d essential tests", ntests));
202 pj_test_init_basic_runner(&runner, &runner_prm);
203 pj_test_run(&runner, &suite);
204 pj_test_display_log_messages(&suite,
205 test_app.ut_app.prm_logging_policy);
206 pj_test_get_stat(&suite, &stat);
207 pj_test_display_stat(&stat, "essential tests", THIS_FILE);
208
209 if (stat.nfailed)
210 return stat;
211 }
212
213 /* Now that the essential components have been tested, test the
214 * multithreaded unit-testing framework.
215 */
216 if (argc==1) {
217 PJ_LOG(3,(THIS_FILE, "Testing the unit-test test scheduling"));
218 if (unittest_parallel_test()) {
219 stat.nfailed = 1;
220 return stat;
221 }
222
223 PJ_LOG(3,(THIS_FILE, "Testing the unit-test framework (multithread)"));
224 if (unittest_test()) {
225 stat.nfailed = 1;
226 return stat;
227 }
228 }
229
230 return stat;
231 }
232
233 static int features_tests(int argc, char *argv[])
234 {
235 if (ut_app_init1(&test_app.ut_app, mem) != PJ_SUCCESS)
236 return 1;
237
238 #if INCLUDE_RAND_TEST
239 UT_ADD_TEST(&test_app.ut_app, rand_test, 0);
240 #endif
241
242 #if INCLUDE_POOL_PERF_TEST
243 UT_ADD_TEST(&test_app.ut_app, pool_perf_test, 0);
244 #endif
245
246 #if INCLUDE_RBTREE_TEST
247 UT_ADD_TEST(&test_app.ut_app, rbtree_test, 0);
248 #endif
249
250 #if INCLUDE_HASH_TEST
251 UT_ADD_TEST(&test_app.ut_app, hash_test, 0);
252 #endif
253
254 /* GH CI oftent fails with:
255
256 07:27:13.217 ...testing frequency accuracy (pls wait)
257 07:27:23.440 ....error: timestamp drifted by 3800 usec after 10020 msec
258 */
259 #if INCLUDE_TIMESTAMP_TEST
260 # if 1 //defined(PJ_WIN32) && PJ_WIN32!=0
261 UT_ADD_TEST(&test_app.ut_app, timestamp_test,
262 PJ_TEST_EXCLUSIVE | PJ_TEST_KEEP_LAST);
263 # else
264 UT_ADD_TEST(&test_app.ut_app, timestamp_test, 0);
265 # endif
266 #endif
267
268 #if INCLUDE_ATOMIC_TEST
269 UT_ADD_TEST(&test_app.ut_app, atomic_test, 0);
270 #endif
271
272 #if INCLUDE_TIMER_TEST
273 UT_ADD_TEST(&test_app.ut_app, timer_test, 0);
274 #endif
275
276 /* Very often sleep test failed on GitHub CI, with
277 the thread sleeping for much longer than tolerated. So
278 as a workaround, set it as exclusive.
279 */
280 #if INCLUDE_SLEEP_TEST
281 # if 1 //defined(PJ_WIN32) && PJ_WIN32!=0
282 UT_ADD_TEST(&test_app.ut_app, sleep_test,
283 PJ_TEST_EXCLUSIVE | PJ_TEST_KEEP_LAST);
284 # else
285 UT_ADD_TEST(&test_app.ut_app, sleep_test, 0);
286 # endif
287 #endif
288
289 #if INCLUDE_FILE_TEST
290 UT_ADD_TEST(&test_app.ut_app, file_test, 0);
291 #endif
292
293 #if INCLUDE_SOCK_TEST
294 UT_ADD_TEST(&test_app.ut_app, sock_test, 0);
295 #endif
296
297 #if INCLUDE_SOCK_PERF_TEST
298 UT_ADD_TEST(&test_app.ut_app, sock_perf_test, 0);
299 #endif
300
301 #if INCLUDE_SELECT_TEST
302 UT_ADD_TEST(&test_app.ut_app, select_test, 0);
303 #endif
304
305 #if INCLUDE_UDP_IOQUEUE_TEST
306 UT_ADD_TEST(&test_app.ut_app, udp_ioqueue_test, 0);
307 #endif
308
309 #if PJ_HAS_TCP && INCLUDE_TCP_IOQUEUE_TEST
310 UT_ADD_TEST(&test_app.ut_app, tcp_ioqueue_test, 0);
311 #endif
312
313 /* Consistently encountered retcode 520 on Windows virtual machine
314 with 8 vcpu and 16GB RAM when multithread unit test is used,
315 with the following logs:
316 17:50:57.761 .tcp (multithreads)
317 17:50:58.254 ...pj_ioqueue_send() error: Object is busy (PJ_EBUSY)
318 17:50:58.264 ..test failed (retcode=520)
319 17:50:58.264 .tcp (multithreads, sequenced, concur=0)
320 17:51:06.084 .tcp (multithreads, sequenced, concur=1)
321 17:51:06.484 ...pj_ioqueue_send() error: Object is busy (PJ_EBUSY)
322 17:51:06.486 ..test failed (retcode=520)
323
324 I suspect it's because the ioq stress test also uses a lot of threads
325 and couldn't keep up with processing the data.
326 Therefore we'll disable parallelism on Windows for this test. [blp]
327 */
328 #if INCLUDE_IOQUEUE_STRESS_TEST
329 # if defined(PJ_WIN32) && PJ_WIN32!=0
330 UT_ADD_TEST(&test_app.ut_app, ioqueue_stress_test,
331 PJ_TEST_EXCLUSIVE | PJ_TEST_KEEP_LAST);
332 # else
333 UT_ADD_TEST(&test_app.ut_app, ioqueue_stress_test, 0);
334 # endif
335 #endif
336
337 #if INCLUDE_IOQUEUE_UNREG_TEST
338 UT_ADD_TEST(&test_app.ut_app, udp_ioqueue_unreg_test, 0);
339 #endif
340
341 #if INCLUDE_IOQUEUE_PERF_TEST
342 UT_ADD_TEST(&test_app.ut_app, ioqueue_perf_test0, 0);
343 UT_ADD_TEST(&test_app.ut_app, ioqueue_perf_test1, 0);
344 #endif
345
346 #if INCLUDE_ACTIVESOCK_TEST
347 UT_ADD_TEST(&test_app.ut_app, activesock_test, 0);
348 #endif
349
350 #if INCLUDE_SSLSOCK_TEST
351 UT_ADD_TEST(&test_app.ut_app, ssl_sock_test, 0);
352 UT_ADD_TEST(&test_app.ut_app, ssl_sock_stress_test, 0);
353 #endif
354
355 #if INCLUDE_IOCP_UNREG_TEST
356 UT_ADD_TEST(&test_app.ut_app, iocp_unregister_test, 0);
357 #endif
358
359
360 #undef ADD_TEST
361
362 if (ut_run_tests(&test_app.ut_app, "features tests", argc, argv)) {
363 ut_app_destroy(&test_app.ut_app);
364 return 1;
365 }
366
367 ut_app_destroy(&test_app.ut_app);
368 return 0;
369 }
370
371 int test_inner(int argc, char *argv[])
372 {
373 pj_caching_pool caching_pool;
374 pj_test_stat stat;
375 const char *filename;
376 int line;
377 int rc = 0;
378
379 mem = &caching_pool.factory;
380
381 pj_log_set_level(3);
382 pj_log_set_decor(test_app.param_log_decor);
383 pj_bzero(&stat, sizeof(pj_test_stat));
384
385 #if 0
386 unsigned decor = pj_log_get_decor();
387 pj_log_set_decor(PJ_LOG_HAS_THREAD_ID | PJ_LOG_HAS_SENDER | decor);
388 pj_log_set_level(5);
389 #if defined(PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE) && (PJ_STRERROR_USE_WIN_GET_THREAD_LOCALE==1) && (WINVER >= 0x0500)
390 LCID locale = GetThreadLocale();
391 BOOL res = SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
392 PJ_UNUSED_ARG(res);
393 #endif
394 #endif
395
396
397 rc = pj_init();
398 if (rc != 0) {
399 app_perror("pj_init() error!!", rc);
400 return rc;
401 }
402
403 pj_caching_pool_init( &caching_pool, NULL, 0 );
404
405 if (test_app.ut_app.prm_ci_mode)
406 PJ_LOG(3,(THIS_FILE, "Using ci-mode"));
407
408 if (!test_app.param_skip_essentials) {
409 stat = essential_tests(argc, argv);
410 if (stat.nfailed) {
411 rc = 1;
412 goto on_return;
413 }
414 }
415
416 if (argc-1 > 0 && (int)stat.nruns==argc-1) {
417 /* cmdline specifies test(s) to run, and the number of runs
418 * matches that. That means all requested tests have been run.
419 */
420 } else {
421 rc = features_tests(argc, argv);
422 if (rc)
423 goto on_return;
424 }
425
426 #if INCLUDE_ECHO_SERVER
427 //echo_server();
428 //echo_srv_sync();
429 udp_echo_srv_ioqueue();
430
431 #elif INCLUDE_ECHO_CLIENT
432 if (test_app.param_echo_sock_type == 0)
433 test_app.param_echo_sock_type = pj_SOCK_DGRAM();
434
435 echo_client( test_app.param_echo_sock_type,
436 test_app.param_echo_server,
437 test_app.param_echo_port);
438 #endif
439
440 goto on_return;
441
442 on_return:
443
444 pj_caching_pool_destroy( &caching_pool );
445
446 PJ_LOG(3,(THIS_FILE, " "));
447
448 pj_thread_get_stack_info(pj_thread_this(), &filename, &line);
449 PJ_LOG(3,(THIS_FILE, "Stack max usage: %u, deepest: %s:%u",
450 pj_thread_get_stack_max_usage(pj_thread_this()),
451 filename, line));
452 if (rc == 0)
453 PJ_LOG(3,(THIS_FILE, "Looks like everything is okay!.."));
454 else
455 PJ_LOG(3,(THIS_FILE, "**Test completed with error(s)**"));
456
457 pj_shutdown();
458
459 return rc;
460 }
461
462 #include <pj/sock.h>
463
464 int test_main(int argc, char *argv[])
465 {
466 PJ_USE_EXCEPTION;
467
468 PJ_TRY {
469 return test_inner(argc, argv);
470 }
471 PJ_CATCH_ANY {
472 int id = PJ_GET_EXCEPTION();
473 PJ_LOG(3,(THIS_FILE, "FATAL: unhandled exception id %d (%s)",
474 id, pj_exception_id_name(id)));
475 }
476 PJ_END;
477
478 return -1;
479 }