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