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