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