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