Update hardening flags.
[m6w6/libmemcached] / libtest / unittest.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <config.h>
38
39 #include <libtest/test.hpp>
40
41 #if defined(LIBTEST_WITH_LIBMEMCACHED_SUPPORT) && LIBTEST_WITH_LIBMEMCACHED_SUPPORT
42 #include <libmemcached-1.0/memcached.h>
43 #endif
44
45 #if defined(LIBTEST_WITH_LIBGEARMAN_SUPPORT) && LIBTEST_WITH_LIBGEARMAN_SUPPORT
46 #include <libgearman/gearman.h>
47 #endif
48
49 #include <cstdlib>
50 #include <unistd.h>
51
52 using namespace libtest;
53
54 static std::string testing_service;
55
56 static test_return_t LIBTOOL_COMMAND_test(void *)
57 {
58 test_true(getenv("LIBTOOL_COMMAND"));
59 return TEST_SUCCESS;
60 }
61
62 static test_return_t VALGRIND_COMMAND_test(void *)
63 {
64 test_true(getenv("VALGRIND_COMMAND"));
65 return TEST_SUCCESS;
66 }
67
68 static test_return_t HELGRIND_COMMAND_test(void *)
69 {
70 test_true(getenv("HELGRIND_COMMAND"));
71 return TEST_SUCCESS;
72 }
73
74 static test_return_t GDB_COMMAND_test(void *)
75 {
76 test_true(getenv("GDB_COMMAND"));
77 return TEST_SUCCESS;
78 }
79
80 static test_return_t test_success_equals_one_test(void *)
81 {
82 test_skip(HAVE_LIBMEMCACHED, 1);
83 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
84 test_zero(MEMCACHED_SUCCESS);
85 #endif
86 return TEST_SUCCESS;
87 }
88
89 static test_return_t test_success_test(void *)
90 {
91 return TEST_SUCCESS;
92 }
93
94 static test_return_t test_failure_test(void *)
95 {
96 return TEST_SKIPPED; // Only run this when debugging
97
98 test_compare(1, 2);
99 return TEST_SUCCESS;
100 }
101
102 static test_return_t local_test(void *)
103 {
104 if (getenv("LIBTEST_LOCAL"))
105 {
106 test_true(test_is_local());
107 }
108 else
109 {
110 test_false(test_is_local());
111 }
112
113 return TEST_SUCCESS;
114 }
115
116 static test_return_t local_not_test(void *)
117 {
118 return TEST_SKIPPED;
119
120 std::string temp;
121
122 const char *ptr;
123 if ((ptr= getenv("LIBTEST_LOCAL")) == NULL)
124 {
125 temp.append(ptr);
126 }
127
128 // unsetenv() will cause issues with valgrind
129 _compare(__FILE__, __LINE__, __func__, 0, unsetenv("LIBTEST_LOCAL"), true);
130 test_compare(0, unsetenv("LIBTEST_LOCAL"));
131 test_false(test_is_local());
132
133 test_compare(0, setenv("LIBTEST_LOCAL", "1", 1));
134 test_true(test_is_local());
135
136 if (temp.empty())
137 {
138 test_compare(0, unsetenv("LIBTEST_LOCAL"));
139 }
140 else
141 {
142 char *old_string= strdup(temp.c_str());
143 test_compare(0, setenv("LIBTEST_LOCAL", old_string, 1));
144 }
145
146 return TEST_SUCCESS;
147 }
148
149 static test_return_t var_exists_test(void *)
150 {
151 test_compare(0, access("var", R_OK | W_OK | X_OK));
152 return TEST_SUCCESS;
153 }
154
155 static test_return_t var_tmp_exists_test(void *)
156 {
157 test_compare(0, access("var/tmp", R_OK | W_OK | X_OK));
158 return TEST_SUCCESS;
159 }
160
161 static test_return_t var_run_exists_test(void *)
162 {
163 test_compare(0, access("var/run", R_OK | W_OK | X_OK));
164 return TEST_SUCCESS;
165 }
166
167 static test_return_t var_log_exists_test(void *)
168 {
169 test_compare(0, access("var/log", R_OK | W_OK | X_OK));
170 return TEST_SUCCESS;
171 }
172
173 static test_return_t var_drizzle_exists_test(void *)
174 {
175 test_compare(0, access("var/drizzle", R_OK | W_OK | X_OK));
176 return TEST_SUCCESS;
177 }
178
179 static test_return_t var_tmp_test(void *)
180 {
181 FILE *file= fopen("var/tmp/junk", "w+");
182 test_true(file);
183 fclose(file);
184 return TEST_SUCCESS;
185 }
186
187 static test_return_t var_run_test(void *)
188 {
189 FILE *file= fopen("var/run/junk", "w+");
190 test_true(file);
191 fclose(file);
192 return TEST_SUCCESS;
193 }
194
195 static test_return_t var_log_test(void *)
196 {
197 FILE *file= fopen("var/log/junk", "w+");
198 test_true(file);
199 fclose(file);
200 return TEST_SUCCESS;
201 }
202
203 static test_return_t var_drizzle_test(void *)
204 {
205 FILE *file= fopen("var/drizzle/junk", "w+");
206 test_true(file);
207 fclose(file);
208 return TEST_SUCCESS;
209 }
210
211 static test_return_t var_tmp_rm_test(void *)
212 {
213 test_true(unlink("var/tmp/junk") == 0);
214 return TEST_SUCCESS;
215 }
216
217 static test_return_t var_run_rm_test(void *)
218 {
219 test_true(unlink("var/run/junk") == 0);
220 return TEST_SUCCESS;
221 }
222
223 static test_return_t var_log_rm_test(void *)
224 {
225 test_true(unlink("var/log/junk") == 0);
226 return TEST_SUCCESS;
227 }
228
229 static test_return_t var_drizzle_rm_test(void *)
230 {
231 test_true(unlink("var/drizzle/junk") == 0);
232 return TEST_SUCCESS;
233 }
234
235 static test_return_t _compare_test_return_t_test(void *)
236 {
237 test_compare(TEST_SUCCESS, TEST_SUCCESS);
238
239 return TEST_SUCCESS;
240 }
241
242 static test_return_t _compare_memcached_return_t_test(void *)
243 {
244 test_skip(HAVE_LIBMEMCACHED, true);
245 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
246 test_compare(MEMCACHED_SUCCESS, MEMCACHED_SUCCESS);
247 #endif
248
249 return TEST_SUCCESS;
250 }
251
252 static test_return_t _compare_gearman_return_t_test(void *)
253 {
254 test_skip(HAVE_LIBGEARMAN, true);
255 #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
256 test_compare(GEARMAN_SUCCESS, GEARMAN_SUCCESS);
257 #endif
258
259 return TEST_SUCCESS;
260 }
261
262 static test_return_t drizzled_cycle_test(void *object)
263 {
264 server_startup_st *servers= (server_startup_st*)object;
265 test_true(servers and servers->validate());
266
267 #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
268 test_true(has_drizzled());
269 #endif
270
271 test_skip(true, has_drizzled());
272
273 test_skip(true, server_startup(*servers, "drizzled", get_free_port(), 0, NULL));
274
275 return TEST_SUCCESS;
276 }
277
278 static test_return_t gearmand_cycle_test(void *object)
279 {
280 server_startup_st *servers= (server_startup_st*)object;
281 test_true(servers and servers->validate());
282
283 #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
284 test_true(has_gearmand());
285 #endif
286
287 test_skip(true, has_gearmand());
288
289 test_skip(true, server_startup(*servers, "gearmand", get_free_port(), 0, NULL));
290
291 return TEST_SUCCESS;
292 }
293
294 #if 0
295 static test_return_t memcached_light_cycle_TEST(void *object)
296 {
297 server_startup_st *servers= (server_startup_st*)object;
298 test_true(servers);
299
300 test_skip(true, bool(HAVE_MEMCACHED_LIGHT_BINARY));
301
302 test_true(server_startup(*servers, "memcached-light", get_free_port(), 0, NULL));
303
304 return TEST_SUCCESS;
305 }
306 #endif
307
308 static test_return_t skip_shim(bool a, bool b)
309 {
310 test_skip(a, b);
311 return TEST_SUCCESS;
312 }
313
314 static test_return_t test_skip_true_TEST(void*)
315 {
316 test_compare(true, true);
317 test_compare(false, false);
318 test_compare(TEST_SUCCESS, skip_shim(true, true));
319 test_compare(TEST_SUCCESS, skip_shim(false, false));
320
321 return TEST_SUCCESS;
322 }
323
324 static test_return_t test_skip_false_TEST(void*)
325 {
326 test_compare(TEST_SKIPPED, skip_shim(true, false));
327 test_compare(TEST_SKIPPED, skip_shim(false, true));
328 return TEST_SUCCESS;
329 }
330
331 static test_return_t server_startup_fail_TEST(void *object)
332 {
333 server_startup_st *servers= (server_startup_st*)object;
334 test_true(servers);
335
336 test_compare(servers->start_server(testing_service, LIBTEST_FAIL_PORT, 0, NULL, true), false);
337
338 return TEST_SUCCESS;
339 }
340
341 static test_return_t server_startup_TEST(void *object)
342 {
343 server_startup_st *servers= (server_startup_st*)object;
344 test_true(servers);
345
346 test_true(servers->start_server(testing_service, get_free_port(), 0, NULL, true));
347
348 test_true(servers->last());
349 pid_t last_pid= servers->last()->pid();
350
351 test_compare(servers->last()->pid(), last_pid);
352 test_true(last_pid > 1);
353 test_compare(kill(last_pid, 0), 0);
354
355 test_true(servers->shutdown());
356 #if 0
357 test_compare(servers->last()->pid(), -1);
358 test_compare(kill(last_pid, 0), -1);
359 #endif
360
361 return TEST_SUCCESS;
362 }
363
364 static test_return_t socket_server_startup_TEST(void *object)
365 {
366 server_startup_st *servers= (server_startup_st*)object;
367 test_true(servers);
368
369 test_true(servers->start_socket_server(testing_service, get_free_port(), 0, NULL, true));
370
371 return TEST_SUCCESS;
372 }
373
374 #if 0
375 static test_return_t memcached_sasl_test(void *object)
376 {
377 server_startup_st *servers= (server_startup_st*)object;
378 test_true(servers);
379
380 test_skip(false, bool(getenv("TESTS_ENVIRONMENT")));
381
382 if (MEMCACHED_SASL_BINARY)
383 {
384 if (HAVE_LIBMEMCACHED)
385 {
386 test_true(has_memcached_sasl());
387 test_true(server_startup(*servers, "memcached-sasl", get_free_port(), 0, NULL));
388
389 return TEST_SUCCESS;
390 }
391 }
392
393 return TEST_SKIPPED;
394 }
395 #endif
396
397 static test_return_t application_true_BINARY(void *)
398 {
399 Application true_app("true");
400
401 test_compare(Application::SUCCESS, true_app.run());
402 test_compare(Application::SUCCESS, true_app.wait());
403
404 return TEST_SUCCESS;
405 }
406
407 static test_return_t application_gdb_true_BINARY2(void *)
408 {
409 test_skip(0, access("/usr/bin/gdb", X_OK ));
410 Application true_app("true");
411 true_app.use_gdb();
412
413 test_compare(Application::SUCCESS, true_app.run());
414 test_compare(Application::SUCCESS, true_app.wait());
415
416 return TEST_SUCCESS;
417 }
418
419 static test_return_t application_gdb_true_BINARY(void *)
420 {
421 test_skip(0, access("/usr/bin/gdb", X_OK ));
422 Application true_app("true");
423 true_app.use_gdb();
424
425 const char *args[]= { "--fubar", 0 };
426 test_compare(Application::SUCCESS, true_app.run(args));
427 test_compare(Application::SUCCESS, true_app.wait());
428
429 return TEST_SUCCESS;
430 }
431
432 static test_return_t application_true_fubar_BINARY(void *)
433 {
434 Application true_app("true");
435
436 const char *args[]= { "--fubar", 0 };
437 test_compare(Application::SUCCESS, true_app.run(args));
438 test_compare(Application::SUCCESS, true_app.wait());
439 test_zero(true_app.stdout_result().size());
440
441 return TEST_SUCCESS;
442 }
443
444 static test_return_t application_doesnotexist_BINARY(void *)
445 {
446
447 test_skip_valgrind();
448 Application true_app("doesnotexist");
449 true_app.will_fail();
450
451 const char *args[]= { "--fubar", 0 };
452 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
453 test_compare(Application::INVALID, true_app.run(args));
454 #else
455 test_compare(Application::SUCCESS, true_app.run(args));
456 test_compare(Application::INVALID, true_app.wait(false));
457 #endif
458
459 test_zero(true_app.stdout_result().size());
460
461 return TEST_SUCCESS;
462 }
463
464 static test_return_t application_true_fubar_eq_doh_BINARY(void *)
465 {
466 Application true_app("true");
467
468 const char *args[]= { "--fubar=doh", 0 };
469 test_compare(Application::SUCCESS, true_app.run(args));
470 test_compare(Application::SUCCESS, true_app.wait());
471 test_zero(true_app.stdout_result().size());
472
473 return TEST_SUCCESS;
474 }
475
476 static test_return_t application_true_fubar_eq_doh_option_BINARY(void *)
477 {
478 Application true_app("true");
479
480 true_app.add_option("--fubar=", "doh");
481
482 test_compare(Application::SUCCESS, true_app.run());
483 test_compare(Application::SUCCESS, true_app.wait());
484 test_zero(true_app.stdout_result().size());
485
486 return TEST_SUCCESS;
487 }
488
489
490 static test_return_t GET_TEST(void *)
491 {
492 libtest::http::GET get("http://foo.example.com/");
493
494 test_compare(false, get.execute());
495
496 return TEST_SUCCESS;
497 }
498
499 static test_return_t POST_TEST(void *)
500 {
501 libtest::vchar_t body;
502 libtest::http::POST post("http://foo.example.com/", body);
503
504 test_compare(false, post.execute());
505
506 return TEST_SUCCESS;
507 }
508
509 static test_return_t TRACE_TEST(void *)
510 {
511 libtest::vchar_t body;
512 libtest::http::TRACE trace("http://foo.example.com/", body);
513
514 test_compare(false, trace.execute());
515
516 return TEST_SUCCESS;
517 }
518
519
520 static test_return_t vchar_t_TEST(void *)
521 {
522 libtest::vchar_t response;
523 libtest::make_vector(response, test_literal_param("fubar\n"));
524 test_compare(response, response);
525
526 return TEST_SUCCESS;
527 }
528
529 static test_return_t vchar_t_compare_neg_TEST(void *)
530 {
531 libtest::vchar_t response;
532 libtest::vchar_t response2;
533 libtest::make_vector(response, test_literal_param("fubar\n"));
534 libtest::make_vector(response2, test_literal_param(__func__));
535 test_true(response != response2);
536
537 return TEST_SUCCESS;
538 }
539
540 static test_return_t application_echo_fubar_BINARY(void *)
541 {
542 Application true_app("echo");
543
544 const char *args[]= { "fubar", 0 };
545 test_compare(Application::SUCCESS, true_app.run(args));
546 test_compare(Application::SUCCESS, true_app.wait());
547
548 while (true_app.slurp() == false) {} ;
549
550 libtest::vchar_t response;
551 make_vector(response, test_literal_param("fubar\n"));
552 test_compare(response, true_app.stdout_result());
553
554 return TEST_SUCCESS;
555 }
556
557 static test_return_t application_echo_fubar_BINARY2(void *)
558 {
559 Application true_app("echo");
560
561 true_app.add_option("fubar");
562
563 test_compare(Application::SUCCESS, true_app.run());
564 test_compare(Application::SUCCESS, true_app.join());
565
566 libtest::vchar_t response;
567 make_vector(response, test_literal_param("fubar\n"));
568 test_compare(response, true_app.stdout_result());
569
570 return TEST_SUCCESS;
571 }
572
573 static test_return_t true_BINARY(void *)
574 {
575 const char *args[]= { 0 };
576 test_compare(EXIT_SUCCESS, exec_cmdline("true", args));
577
578 return TEST_SUCCESS;
579 }
580
581 static test_return_t true_fubar_BINARY(void *)
582 {
583 const char *args[]= { "--fubar", 0 };
584 test_compare(EXIT_SUCCESS, exec_cmdline("true", args));
585
586 return TEST_SUCCESS;
587 }
588
589 static test_return_t echo_fubar_BINARY(void *)
590 {
591 const char *args[]= { "fubar", 0 };
592 test_compare(EXIT_SUCCESS, exec_cmdline("echo", args));
593
594 return TEST_SUCCESS;
595 }
596
597 static test_return_t wait_BINARY(void *)
598 {
599 const char *args[]= { "--quiet", 0 };
600
601 test_compare(EXIT_FAILURE, exec_cmdline("libtest/wait", args, true));
602
603 return TEST_SUCCESS;
604 }
605
606 static test_return_t wait_help_BINARY(void *)
607 {
608 const char *args[]= { "--quiet", "--help", 0 };
609
610 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
611
612 return TEST_SUCCESS;
613 }
614
615 static test_return_t wait_version_BINARY(void *)
616 {
617 const char *args[]= { "--quiet", "--version", 0 };
618
619 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
620
621 return TEST_SUCCESS;
622 }
623
624 static test_return_t wait_services_BINARY(void *)
625 {
626 test_skip(0, access("/etc/services", R_OK ));
627
628 const char *args[]= { "--quiet", "/etc/services", 0 };
629
630 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
631
632 return TEST_SUCCESS;
633 }
634
635 static test_return_t wait_services_BINARY2(void *)
636 {
637 test_skip(0, access("/etc/services", R_OK ));
638
639 const char *args[]= { "/etc/services", 0 };
640
641 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
642
643 return TEST_SUCCESS;
644 }
645
646 static test_return_t wait_services_appliction_TEST(void *)
647 {
648 test_skip(0, access("/usr/bin/gdb", X_OK ));
649 test_skip(0, access("/etc/services", R_OK ));
650
651 libtest::Application wait_app("libtest/wait", true);
652 wait_app.use_gdb();
653
654 const char *args[]= { "/etc/services", 0 };
655 test_compare(Application::SUCCESS, wait_app.run(args));
656 test_compare(Application::SUCCESS, wait_app.wait());
657
658 return TEST_SUCCESS;
659 }
660
661 static test_return_t gdb_wait_services_appliction_TEST(void *)
662 {
663 test_skip(true, false);
664 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
665 test_skip(0, TARGET_OS_OSX);
666 #endif
667
668 test_skip(0, access("/usr/bin/gdb", X_OK ));
669 test_skip(0, access("/etc/services", R_OK ));
670
671 libtest::Application wait_app("libtest/wait", true);
672 wait_app.use_gdb();
673
674 const char *args[]= { "/etc/services", 0 };
675 test_compare(Application::SUCCESS, wait_app.run(args));
676 test_compare(Application::SUCCESS, wait_app.wait());
677
678 return TEST_SUCCESS;
679 }
680
681 static test_return_t gdb_abort_services_appliction_TEST(void *)
682 {
683 test_skip(true, false);
684 test_skip(0, access("/usr/bin/gdb", X_OK ));
685
686 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
687 test_skip(0, TARGET_OS_OSX);
688 #endif
689
690 libtest::Application abort_app("libtest/abort", true);
691 abort_app.use_gdb();
692
693 test_compare(Application::SUCCESS, abort_app.run());
694 test_compare(Application::SUCCESS, abort_app.wait());
695
696 std::string gdb_filename= abort_app.gdb_filename();
697 test_skip(0, access(gdb_filename.c_str(), R_OK ));
698 const char *args[]= { "SIGABRT", gdb_filename.c_str(), 0 };
699 test_compare(EXIT_SUCCESS, exec_cmdline("grep", args));
700
701 // Sanity test
702 args[0]= "THIS_WILL_NOT_BE_FOUND";
703 test_compare(EXIT_FAILURE, exec_cmdline("grep", args));
704
705 return TEST_SUCCESS;
706 }
707
708 static test_return_t get_free_port_TEST(void *)
709 {
710 in_port_t ret_port;
711 test_true((ret_port= get_free_port()));
712 test_true(get_free_port() != default_port());
713 test_true(get_free_port() != get_free_port());
714
715 return TEST_SUCCESS;
716 }
717
718 static uint32_t fatal_calls= 0;
719
720 static test_return_t fatal_TEST(void *)
721 {
722 test_compare(fatal_calls++, fatal::disabled_counter());
723 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Testing va_args based fatal(): %d", 10);
724
725 return TEST_SUCCESS;
726 }
727
728 static test_return_t number_of_cpus_TEST(void *)
729 {
730 test_true(number_of_cpus() >= 1);
731
732 return TEST_SUCCESS;
733 }
734
735 static test_return_t check_dns_TEST(void *)
736 {
737 test_warn(libtest::check_dns(), "Broken DNS server/no DNS server found");
738
739 return TEST_SUCCESS;
740 }
741
742 static test_return_t Timer_TEST(void *)
743 {
744 int64_t minutes= random() % 50;
745 minutes++;
746
747 Timer check;
748
749 check.reset();
750 check.offset(minutes, 2, 200);
751
752 test_compare(check.minutes(), minutes);
753
754 return TEST_SUCCESS;
755 }
756
757 static test_return_t lookup_true_TEST(void *)
758 {
759 test_warn(libtest::lookup("exist.gearman.info"), "dns is not currently working");
760 return TEST_SUCCESS;
761 }
762
763 static test_return_t lookup_false_TEST(void *)
764 {
765 if (libtest::lookup("does_not_exist.gearman.info"))
766 {
767 Error << "Broken DNS server detected";
768 return TEST_SKIPPED;
769 }
770
771 return TEST_SUCCESS;
772 }
773
774 static test_return_t create_tmpfile_TEST(void *)
775 {
776 std::string tmp= create_tmpfile(__func__);
777 test_compare(-1, access(tmp.c_str(), R_OK));
778 test_compare(-1, access(tmp.c_str(), F_OK));
779
780 Application touch_app("touch");
781 const char *args[]= { tmp.c_str(), 0 };
782 test_compare(Application::SUCCESS, touch_app.run(args));
783 test_compare(Application::SUCCESS, touch_app.join());
784
785 test_compare(0, access(tmp.c_str(), R_OK));
786 test_compare(0, unlink(tmp.c_str()));
787
788 return TEST_SUCCESS;
789 }
790
791 static test_return_t fatal_message_TEST(void *)
792 {
793 test_compare(fatal_calls++, fatal::disabled_counter());
794 fatal_message("Fatal test");
795
796 return TEST_SUCCESS;
797 }
798
799 static test_return_t default_port_TEST(void *)
800 {
801 in_port_t ret_port= default_port();
802 test_compare(ret_port, libtest::default_port());
803 test_compare(ret_port, libtest::default_port());
804
805 return TEST_SUCCESS;
806 }
807
808 static test_return_t check_for_gearman(void *)
809 {
810 test_skip(true, HAVE_LIBGEARMAN);
811 test_skip(true, has_gearmand());
812
813 testing_service= "gearmand";
814
815 return TEST_SUCCESS;
816 }
817
818 static test_return_t check_for_drizzle(void *)
819 {
820 test_skip(true, HAVE_LIBDRIZZLE);
821 test_skip(true, has_drizzled());
822
823 testing_service= "drizzled";
824
825 return TEST_SUCCESS;
826 }
827
828
829 test_st drizzled_tests[] ={
830 {"drizzled startup-shutdown", 0, drizzled_cycle_test },
831 {0, 0, 0}
832 };
833
834 test_st gearmand_tests[] ={
835 #if 0
836 {"pause", 0, pause_test },
837 #endif
838 {"gearmand startup-shutdown", 0, gearmand_cycle_test },
839 {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test },
840 {"server_startup(fail)", 0, server_startup_fail_TEST },
841 {0, 0, 0}
842 };
843
844 static test_return_t check_for_libmemcached(void* object)
845 {
846 test_skip(true, HAVE_LIBMEMCACHED);
847 test_skip(true, has_memcached());
848
849 server_startup_st *servers= (server_startup_st*)object;
850 test_true(servers);
851 servers->clear();
852
853 testing_service= "memcached";
854
855 return TEST_SUCCESS;
856 }
857
858 test_st memcached_TESTS[] ={
859 {"memcached startup-shutdown", 0, server_startup_TEST },
860 {"memcached(socket file) startup-shutdown", 0, socket_server_startup_TEST },
861 {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test },
862 {"server_startup(fail)", 0, server_startup_fail_TEST },
863 {0, 0, 0}
864 };
865
866 test_st test_skip_TESTS[] ={
867 {"true, true", 0, test_skip_true_TEST },
868 {"true, false", 0, test_skip_false_TEST },
869 {0, 0, 0}
870 };
871
872 test_st environment_tests[] ={
873 {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test },
874 {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test },
875 {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test },
876 {"GDB_COMMAND", 0, GDB_COMMAND_test },
877 {0, 0, 0}
878 };
879
880 test_st tests_log[] ={
881 {"TEST_SUCCESS", false, test_success_test },
882 {"TEST_FAILURE", false, test_failure_test },
883 {"TEST_SUCCESS == 0", false, test_success_equals_one_test },
884 {0, 0, 0}
885 };
886
887 test_st local_log[] ={
888 {"test_is_local()", 0, local_test },
889 {"test_is_local(NOT)", 0, local_not_test },
890 {0, 0, 0}
891 };
892
893 test_st directories_tests[] ={
894 {"var exists", 0, var_exists_test },
895 {"var/tmp exists", 0, var_tmp_exists_test },
896 {"var/run exists", 0, var_run_exists_test },
897 {"var/log exists", 0, var_log_exists_test },
898 {"var/drizzle exists", 0, var_drizzle_exists_test },
899 {"var/tmp", 0, var_tmp_test },
900 {"var/run", 0, var_run_test },
901 {"var/log", 0, var_log_test },
902 {"var/drizzle", 0, var_drizzle_test },
903 {"var/tmp rm", 0, var_tmp_rm_test },
904 {"var/run rm", 0, var_run_rm_test },
905 {"var/log rm", 0, var_log_rm_test },
906 {"var/drizzle rm", 0, var_drizzle_rm_test },
907 {0, 0, 0}
908 };
909
910 test_st comparison_tests[] ={
911 {"_compare(test_return_t)", 0, _compare_test_return_t_test },
912 {0, 0, 0}
913 };
914
915 test_st cmdline_tests[] ={
916 {"true", 0, true_BINARY },
917 {"true --fubar", 0, true_fubar_BINARY },
918 {"echo fubar", 0, echo_fubar_BINARY },
919 {"wait --quiet", 0, wait_BINARY },
920 {"wait --quiet --help", 0, wait_help_BINARY },
921 {"wait --quiet --version", 0, wait_version_BINARY },
922 {"wait --quiet /etc/services", 0, wait_services_BINARY },
923 {"wait /etc/services", 0, wait_services_BINARY2 },
924 {"wait /etc/services", 0, wait_services_appliction_TEST },
925 {"gdb wait /etc/services", 0, gdb_wait_services_appliction_TEST },
926 {"gdb abort", 0, gdb_abort_services_appliction_TEST },
927 {0, 0, 0}
928 };
929
930 test_st get_free_port_TESTS[] ={
931 {"get_free_port()", 0, get_free_port_TEST },
932 {"default_port()", 0, default_port_TEST },
933 {0, 0, 0}
934 };
935
936 test_st fatal_message_TESTS[] ={
937 {"libtest::fatal", 0, fatal_TEST },
938 {"fatal_message()", 0, fatal_message_TEST },
939 {0, 0, 0}
940 };
941
942 test_st number_of_cpus_TESTS[] ={
943 {"libtest::number_of_cpus()", 0, number_of_cpus_TEST },
944 {0, 0, 0}
945 };
946
947 test_st create_tmpfile_TESTS[] ={
948 {"libtest::create_tmpfile()", 0, create_tmpfile_TEST },
949 {0, 0, 0}
950 };
951
952 test_st timer_TESTS[] ={
953 {"libtest::Timer", 0, Timer_TEST },
954 {0, 0, 0}
955 };
956
957 test_st dns_TESTS[] ={
958 {"libtest::lookup(true)", 0, lookup_true_TEST },
959 {"libtest::lookup(false)", 0, lookup_false_TEST },
960 {"libtest::check_dns()", 0, check_dns_TEST },
961 {0, 0, 0}
962 };
963
964 test_st application_tests[] ={
965 {"vchar_t", 0, vchar_t_TEST },
966 {"vchar_t compare()", 0, vchar_t_compare_neg_TEST },
967 {"true", 0, application_true_BINARY },
968 {"gbd true --fubar", 0, application_gdb_true_BINARY },
969 {"gbd true", 0, application_gdb_true_BINARY2 },
970 {"true --fubar", 0, application_true_fubar_BINARY },
971 {"doesnotexist --fubar", 0, application_doesnotexist_BINARY },
972 {"true --fubar=doh", 0, application_true_fubar_eq_doh_BINARY },
973 {"true --fubar=doh add_option()", 0, application_true_fubar_eq_doh_option_BINARY },
974 {"echo fubar", 0, application_echo_fubar_BINARY },
975 {"echo fubar (as option)", 0, application_echo_fubar_BINARY2 },
976 {0, 0, 0}
977 };
978
979 static test_return_t check_for_curl(void *)
980 {
981 test_skip(true, HAVE_LIBCURL);
982 return TEST_SUCCESS;
983 }
984
985 static test_return_t disable_fatal_exception(void *)
986 {
987 fatal::disable();
988 return TEST_SUCCESS;
989 }
990
991 static test_return_t enable_fatal_exception(void *)
992 {
993 fatal::disable();
994 return TEST_SUCCESS;
995 }
996
997 test_st http_tests[] ={
998 {"GET", 0, GET_TEST },
999 {"POST", 0, POST_TEST },
1000 {"TRACE", 0, TRACE_TEST },
1001 {0, 0, 0}
1002 };
1003
1004 collection_st collection[] ={
1005 {"environment", 0, 0, environment_tests},
1006 {"return values", 0, 0, tests_log},
1007 {"test_skip()", 0, 0, test_skip_TESTS },
1008 {"local", 0, 0, local_log},
1009 {"directories", 0, 0, directories_tests},
1010 {"comparison", 0, 0, comparison_tests},
1011 {"gearmand", check_for_gearman, 0, gearmand_tests},
1012 {"memcached", check_for_libmemcached, 0, memcached_TESTS },
1013 {"drizzled", check_for_drizzle, 0, drizzled_tests},
1014 {"cmdline", 0, 0, cmdline_tests},
1015 {"application", 0, 0, application_tests},
1016 {"http", check_for_curl, 0, http_tests},
1017 {"http", check_for_curl, 0, http_tests},
1018 {"get_free_port()", 0, 0, get_free_port_TESTS },
1019 {"fatal", disable_fatal_exception, enable_fatal_exception, fatal_message_TESTS },
1020 {"number_of_cpus()", 0, 0, number_of_cpus_TESTS },
1021 {"create_tmpfile()", 0, 0, create_tmpfile_TESTS },
1022 {"dns", 0, 0, dns_TESTS },
1023 {"libtest::Timer", 0, 0, timer_TESTS },
1024 {0, 0, 0, 0}
1025 };
1026
1027 static void *world_create(server_startup_st& servers, test_return_t&)
1028 {
1029 return &servers;
1030 }
1031
1032 void get_world(libtest::Framework *world)
1033 {
1034 world->collections(collection);
1035 world->create(world_create);
1036 }