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