Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-186
[awesomized/libmemcached] / libtest / unittest.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "libtest/yatlcon.h"
38
39 #include <libtest/yatl.h>
40
41 #if defined(HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H) && HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H
42 # include <libmemcached-1.0/types/return.h>
43 #endif
44
45 #if defined(HAVE_LIBGEARMAN_1_0_RETURN_H) && HAVE_LIBGEARMAN_1_0_RETURN_H
46 # include <libgearman-1.0/return.h>
47 #endif
48
49 #include <cstdlib>
50 #include <unistd.h>
51
52 using namespace libtest;
53
54 static std::string testing_service;
55
56 // Used to track setups where we see if failure is happening
57 static uint32_t fatal_calls= 0;
58
59 static test_return_t getenv_TEST(void *)
60 {
61 #if 0
62 for (char **ptr= environ; *ptr; ptr++)
63 {
64 Error << *ptr;
65 }
66 #endif
67
68 return TEST_SUCCESS;
69 }
70
71 static test_return_t LIBTOOL_COMMAND_test(void *)
72 {
73 test_true(getenv("LIBTOOL_COMMAND"));
74 return TEST_SUCCESS;
75 }
76
77 static test_return_t VALGRIND_COMMAND_test(void *)
78 {
79 test_true(getenv("VALGRIND_COMMAND"));
80 return TEST_SUCCESS;
81 }
82
83 static test_return_t HELGRIND_COMMAND_test(void *)
84 {
85 test_true(getenv("HELGRIND_COMMAND"));
86 return TEST_SUCCESS;
87 }
88
89 static test_return_t GDB_COMMAND_test(void *)
90 {
91 test_true(getenv("GDB_COMMAND"));
92 return TEST_SUCCESS;
93 }
94
95 static test_return_t test_success_equals_one_test(void *)
96 {
97 test_skip(HAVE_LIBMEMCACHED, 1);
98 #if defined(HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H) && HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H
99 test_zero(MEMCACHED_SUCCESS);
100 #endif
101 return TEST_SUCCESS;
102 }
103
104 static test_return_t test_success_test(void *)
105 {
106 return TEST_SUCCESS;
107 }
108
109 static test_return_t test_throw_success_TEST(void *)
110 {
111 try {
112 _SUCCESS;
113 }
114 catch (libtest::__success)
115 {
116 return TEST_SUCCESS;
117 }
118 catch (...)
119 {
120 return TEST_FAILURE;
121 }
122
123 return TEST_FAILURE;
124 }
125
126 static test_return_t test_throw_skip_macro_TEST(void *)
127 {
128 try {
129 SKIP_IF(true);
130 }
131 catch (libtest::__skipped e)
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 (libtest::__skipped e)
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_FALSE_TEST(void *)
203 {
204 try {
205 FAIL(__func__);
206 }
207 catch (const libtest::__failure& e)
208 {
209 ASSERT_STREQ(e.what(), __func__);
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 test_failure_test(void *)
221 {
222 return TEST_SKIPPED; // Only run this when debugging
223
224 ASSERT_EQ(1, 2);
225 return TEST_SUCCESS;
226 }
227
228 static test_return_t local_test(void *)
229 {
230 if (getenv("LIBTEST_LOCAL"))
231 {
232 test_true(test_is_local());
233 }
234 else
235 {
236 test_false(test_is_local());
237 }
238
239 return TEST_SUCCESS;
240 }
241
242 static test_return_t local_not_test(void *)
243 {
244 return TEST_SKIPPED;
245
246 std::string temp;
247
248 const char *ptr;
249 if ((ptr= getenv("LIBTEST_LOCAL")) == NULL)
250 {
251 temp.append(ptr);
252 }
253
254 // unsetenv() will cause issues with valgrind
255 _compare(__FILE__, __LINE__, __func__, 0, unsetenv("LIBTEST_LOCAL"), true);
256 ASSERT_EQ(0, unsetenv("LIBTEST_LOCAL"));
257 test_false(test_is_local());
258
259 ASSERT_EQ(0, setenv("LIBTEST_LOCAL", "1", 1));
260 test_true(test_is_local());
261
262 if (temp.empty())
263 {
264 ASSERT_EQ(0, unsetenv("LIBTEST_LOCAL"));
265 }
266 else
267 {
268 char *old_string= strdup(temp.c_str());
269 ASSERT_EQ(0, setenv("LIBTEST_LOCAL", old_string, 1));
270 }
271
272 return TEST_SUCCESS;
273 }
274
275 static test_return_t var_exists_test(void *)
276 {
277 ASSERT_EQ(0, access("var", R_OK | W_OK | X_OK));
278 return TEST_SUCCESS;
279 }
280
281 static test_return_t var_tmp_exists_test(void *)
282 {
283 ASSERT_EQ(0, access("var/tmp", R_OK | W_OK | X_OK));
284 return TEST_SUCCESS;
285 }
286
287 static test_return_t var_run_exists_test(void *)
288 {
289 ASSERT_EQ(0, access("var/run", R_OK | W_OK | X_OK));
290 return TEST_SUCCESS;
291 }
292
293 static test_return_t var_log_exists_test(void *)
294 {
295 ASSERT_EQ(0, access("var/log", R_OK | W_OK | X_OK));
296 return TEST_SUCCESS;
297 }
298
299 static test_return_t var_drizzle_exists_test(void *)
300 {
301 ASSERT_EQ(0, access("var/drizzle", R_OK | W_OK | X_OK));
302 return TEST_SUCCESS;
303 }
304
305 static test_return_t var_tmp_test(void *)
306 {
307 FILE *file= fopen("var/tmp/junk", "w+");
308 test_true(file);
309 fclose(file);
310 return TEST_SUCCESS;
311 }
312
313 static test_return_t var_run_test(void *)
314 {
315 FILE *file= fopen("var/run/junk", "w+");
316 test_true(file);
317 fclose(file);
318 return TEST_SUCCESS;
319 }
320
321 static test_return_t var_log_test(void *)
322 {
323 FILE *file= fopen("var/log/junk", "w+");
324 test_true(file);
325 fclose(file);
326 return TEST_SUCCESS;
327 }
328
329 static test_return_t var_drizzle_test(void *)
330 {
331 FILE *file= fopen("var/drizzle/junk", "w+");
332 test_true(file);
333 fclose(file);
334 return TEST_SUCCESS;
335 }
336
337 static test_return_t var_tmp_rm_test(void *)
338 {
339 test_true(unlink("var/tmp/junk") == 0);
340 return TEST_SUCCESS;
341 }
342
343 static test_return_t var_run_rm_test(void *)
344 {
345 test_true(unlink("var/run/junk") == 0);
346 return TEST_SUCCESS;
347 }
348
349 static test_return_t var_log_rm_test(void *)
350 {
351 test_true(unlink("var/log/junk") == 0);
352 return TEST_SUCCESS;
353 }
354
355 static test_return_t var_drizzle_rm_test(void *)
356 {
357 test_true(unlink("var/drizzle/junk") == 0);
358 return TEST_SUCCESS;
359 }
360
361 static test_return_t _compare_test_return_t_test(void *)
362 {
363 ASSERT_EQ(TEST_SUCCESS, TEST_SUCCESS);
364
365 return TEST_SUCCESS;
366 }
367
368 static test_return_t _compare_memcached_return_t_test(void *)
369 {
370 test_skip(HAVE_LIBMEMCACHED, true);
371 #if defined(HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H) && HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H
372 ASSERT_EQ(MEMCACHED_SUCCESS, MEMCACHED_SUCCESS);
373 #endif
374
375 return TEST_SUCCESS;
376 }
377
378 static test_return_t _compare_gearman_return_t_test(void *)
379 {
380 test_skip(HAVE_LIBGEARMAN, true);
381 #if defined(HAVE_LIBGEARMAN_1_0_RETURN_H) && HAVE_LIBGEARMAN_1_0_RETURN_H
382 ASSERT_EQ(GEARMAN_SUCCESS, GEARMAN_SUCCESS);
383 #endif
384
385 return TEST_SUCCESS;
386 }
387
388 static test_return_t drizzled_cycle_test(void *object)
389 {
390 server_startup_st *servers= (server_startup_st*)object;
391 test_true(servers and servers->validate());
392
393 #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
394 test_true(has_drizzled());
395 #endif
396
397 test_skip(true, has_drizzled());
398
399 test_skip(true, server_startup(*servers, "drizzled", get_free_port(), 0, NULL, false));
400
401 return TEST_SUCCESS;
402 }
403
404 static test_return_t gearmand_cycle_test(void *object)
405 {
406 server_startup_st *servers= (server_startup_st*)object;
407 test_true(servers and servers->validate());
408
409 test_skip(true, has_gearmand());
410 test_skip(true, server_startup(*servers, "gearmand", get_free_port(), 0, NULL, false));
411 servers->clear();
412
413 return TEST_SUCCESS;
414 }
415
416 static test_return_t skip_shim(bool a, bool b)
417 {
418 test_skip(a, b);
419 return TEST_SUCCESS;
420 }
421
422 static test_return_t test_skip_true_TEST(void*)
423 {
424 ASSERT_EQ(true, true);
425 ASSERT_EQ(false, false);
426 ASSERT_EQ(TEST_SUCCESS, skip_shim(true, true));
427 ASSERT_EQ(TEST_SUCCESS, skip_shim(false, false));
428
429 return TEST_SUCCESS;
430 }
431
432 static test_return_t test_skip_false_TEST(void*)
433 {
434 ASSERT_EQ(TEST_SKIPPED, skip_shim(true, false));
435 ASSERT_EQ(TEST_SKIPPED, skip_shim(false, true));
436 return TEST_SUCCESS;
437 }
438
439 static test_return_t server_startup_fail_TEST(void *object)
440 {
441 server_startup_st *servers= (server_startup_st*)object;
442 test_true(servers);
443
444 fatal::disable();
445 ASSERT_EQ(servers->start_server(testing_service, LIBTEST_FAIL_PORT, 0, NULL, false), true);
446 fatal::enable();
447
448 return TEST_SUCCESS;
449 }
450
451 static test_return_t server_startup_TEST(void *object)
452 {
453 server_startup_st *servers= (server_startup_st*)object;
454 test_true(servers);
455
456 ASSERT_EQ(servers->start_server(testing_service, get_free_port(), 0, NULL, false), true);
457
458 test_true(servers->last());
459 pid_t last_pid= servers->last()->pid();
460
461 ASSERT_EQ(servers->last()->pid(), last_pid);
462 test_true(last_pid > 1);
463 ASSERT_EQ(kill(last_pid, 0), 0);
464
465 test_true(servers->shutdown());
466 #if 0
467 ASSERT_EQ(servers->last()->pid(), -1);
468 ASSERT_EQ(kill(last_pid, 0), -1);
469 #endif
470
471 return TEST_SUCCESS;
472 }
473
474 static test_return_t socket_server_startup_TEST(void *object)
475 {
476 server_startup_st *servers= (server_startup_st*)object;
477 test_true(servers);
478
479 test_true(servers->start_socket_server(testing_service, get_free_port(), 0, NULL, false));
480
481 return TEST_SUCCESS;
482 }
483
484 #if 0
485 static test_return_t memcached_sasl_test(void *object)
486 {
487 server_startup_st *servers= (server_startup_st*)object;
488 test_true(servers);
489
490 test_skip(false, bool(getenv("TESTS_ENVIRONMENT")));
491
492 if (MEMCACHED_SASL_BINARY)
493 {
494 if (HAVE_LIBMEMCACHED)
495 {
496 test_true(has_memcached_sasl());
497 test_true(server_startup(*servers, "memcached-sasl", get_free_port(), 0, NULL, false));
498
499 return TEST_SUCCESS;
500 }
501 }
502
503 return TEST_SKIPPED;
504 }
505 #endif
506
507 static test_return_t application_true_BINARY(void *)
508 {
509 test_skip(0, access("/usr/bin/true", X_OK ));
510 Application true_app("/usr/bin/true");
511
512 ASSERT_EQ(Application::SUCCESS, true_app.run());
513 ASSERT_EQ(Application::SUCCESS, true_app.join());
514
515 return TEST_SUCCESS;
516 }
517
518 static test_return_t application_gdb_true_BINARY2(void *)
519 {
520 test_skip(0, access("/usr/bin/gdb", X_OK ));
521 test_skip(0, access("/usr/bin/true", X_OK ));
522
523 Application true_app("/usr/bin/true");
524 true_app.use_gdb(true);
525
526 ASSERT_EQ(Application::SUCCESS, true_app.run());
527 ASSERT_EQ(Application::SUCCESS, true_app.join());
528
529 return TEST_SUCCESS;
530 }
531
532 static test_return_t application_gdb_true_BINARY(void *)
533 {
534 test_skip(0, access("/usr/bin/gdb", X_OK ));
535 test_skip(0, access("/usr/bin/true", X_OK ));
536
537 Application true_app("/usr/bin/true");
538 true_app.use_gdb(true);
539
540 const char *args[]= { "--fubar", 0 };
541 ASSERT_EQ(Application::SUCCESS, true_app.run(args));
542 ASSERT_EQ(Application::SUCCESS, true_app.join());
543
544 return TEST_SUCCESS;
545 }
546
547 static test_return_t application_true_fubar_BINARY(void *)
548 {
549 test_skip(0, access("/usr/bin/true", X_OK ));
550 Application true_app("/usr/bin/true");
551
552 const char *args[]= { "--fubar", 0 };
553 ASSERT_EQ(Application::SUCCESS, true_app.run(args));
554 ASSERT_EQ(Application::SUCCESS, true_app.join());
555 test_zero(true_app.stdout_result().size());
556
557 return TEST_SUCCESS;
558 }
559
560 static test_return_t application_doesnotexist_BINARY(void *)
561 {
562
563 test_skip_valgrind();
564 Application true_app("doesnotexist");
565 true_app.will_fail();
566
567 const char *args[]= { "--fubar", 0 };
568 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
569 ASSERT_EQ(Application::INVALID_POSIX_SPAWN, true_app.run(args));
570 #elif defined(TARGET_OS_FREEBSD) && TARGET_OS_FREEBSD
571 ASSERT_EQ(Application::INVALID_POSIX_SPAWN, true_app.run(args));
572 #else
573 ASSERT_EQ(Application::SUCCESS, true_app.run(args));
574 ASSERT_EQ(Application::INVALID_POSIX_SPAWN, true_app.join());
575 #endif
576
577 test_zero(true_app.stdout_result().size());
578
579 return TEST_SUCCESS;
580 }
581
582 static test_return_t GET_TEST(void *)
583 {
584 libtest::http::GET get("http://foo.example.com/");
585
586 ASSERT_EQ(false, get.execute());
587
588 return TEST_SUCCESS;
589 }
590
591 static test_return_t POST_TEST(void *)
592 {
593 libtest::vchar_t body;
594 libtest::http::POST post("http://foo.example.com/", body);
595
596 ASSERT_EQ(false, post.execute());
597
598 return TEST_SUCCESS;
599 }
600
601 static test_return_t TRACE_TEST(void *)
602 {
603 libtest::vchar_t body;
604 libtest::http::TRACE trace("http://foo.example.com/", body);
605
606 ASSERT_EQ(false, trace.execute());
607
608 return TEST_SUCCESS;
609 }
610
611
612 static test_return_t vchar_t_TEST(void *)
613 {
614 libtest::vchar_t response;
615 libtest::make_vector(response, test_literal_param("fubar\n"));
616 ASSERT_EQ(response, response);
617
618 return TEST_SUCCESS;
619 }
620
621 static test_return_t vchar_t_compare_neg_TEST(void *)
622 {
623 libtest::vchar_t response;
624 libtest::vchar_t response2;
625 libtest::make_vector(response, test_literal_param("fubar\n"));
626 libtest::make_vector(response2, test_literal_param(__func__));
627 test_true(response != response2);
628
629 return TEST_SUCCESS;
630 }
631
632 static test_return_t application_echo_fubar_BINARY(void *)
633 {
634 if (0)
635 {
636 test_skip(0, access("/bin/echo", X_OK ));
637 Application true_app("/bin/echo");
638
639 const char *args[]= { "fubar", 0 };
640 ASSERT_EQ(Application::SUCCESS, true_app.run(args));
641
642 while (true_app.slurp() == false) {} ;
643
644 libtest::vchar_t response;
645 make_vector(response, test_literal_param("fubar\n"));
646 ASSERT_EQ(response, true_app.stdout_result());
647 }
648
649 return TEST_SUCCESS;
650 }
651
652 static test_return_t application_echo_fubar_BINARY2(void *)
653 {
654 if (0)
655 {
656 test_skip(0, access("/bin/echo", X_OK ));
657 Application true_app("/bin/echo");
658
659 true_app.add_option("fubar");
660
661 ASSERT_EQ(Application::SUCCESS, true_app.run());
662 ASSERT_EQ(Application::SUCCESS, true_app.join());
663
664 libtest::vchar_t response;
665 make_vector(response, test_literal_param("fubar\n"));
666 ASSERT_EQ(response, true_app.stdout_result());
667 }
668
669 return TEST_SUCCESS;
670 }
671
672 static test_return_t echo_fubar_BINARY(void *)
673 {
674 const char *args[]= { "fubar", 0 };
675 ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("/bin/echo", args));
676
677 return TEST_SUCCESS;
678 }
679
680 static test_return_t core_count_BINARY(void *)
681 {
682 const char *args[]= { 0 };
683
684 ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("libtest/core-count", args, true));
685
686 return TEST_SUCCESS;
687 }
688
689 static test_return_t wait_BINARY(void *)
690 {
691 const char *args[]= { "--quiet", 0 };
692
693 ASSERT_EQ(EXIT_FAILURE, exec_cmdline("libtest/wait", args, true));
694
695 return TEST_SUCCESS;
696 }
697
698 static test_return_t wait_help_BINARY(void *)
699 {
700 const char *args[]= { "--quiet", "--help", 0 };
701
702 ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
703
704 return TEST_SUCCESS;
705 }
706
707 static test_return_t wait_version_BINARY(void *)
708 {
709 const char *args[]= { "--quiet", "--version", 0 };
710
711 ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
712
713 return TEST_SUCCESS;
714 }
715
716 static test_return_t wait_services_BINARY(void *)
717 {
718 test_skip(0, access("/etc/services", R_OK ));
719
720 const char *args[]= { "--quiet", "/etc/services", 0 };
721
722 ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
723
724 return TEST_SUCCESS;
725 }
726
727 static test_return_t wait_services_BINARY2(void *)
728 {
729 test_skip(0, access("/etc/services", R_OK ));
730
731 const char *args[]= { "/etc/services", 0 };
732
733 ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
734
735 return TEST_SUCCESS;
736 }
737
738 static test_return_t wait_services_appliction_TEST(void *)
739 {
740 test_skip(0, access("/etc/services", R_OK ));
741 test_skip(0, access("/usr/bin/gdb", X_OK ));
742 test_skip(0, access("libtest/wait", X_OK ));
743
744 libtest::Application wait_app("libtest/wait", true);
745 wait_app.use_gdb(true);
746
747 const char *args[]= { "/etc/services", 0 };
748 ASSERT_EQ(Application::SUCCESS, wait_app.run(args));
749 ASSERT_EQ(Application::SUCCESS, wait_app.join());
750
751 return TEST_SUCCESS;
752 }
753
754 static test_return_t gdb_wait_services_appliction_TEST(void *)
755 {
756 test_skip(true, false);
757 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
758 test_skip(0, TARGET_OS_OSX);
759 #endif
760
761 test_skip(0, access("/etc/services", R_OK ));
762 test_skip(0, access("/usr/bin/gdb", X_OK ));
763 test_skip(0, access("libtest/wait", X_OK ));
764
765 libtest::Application wait_app("libtest/wait", true);
766 wait_app.use_gdb(true);
767
768 const char *args[]= { "/etc/services", 0 };
769 ASSERT_EQ(Application::SUCCESS, wait_app.run(args));
770 ASSERT_EQ(Application::SUCCESS, wait_app.join());
771
772 return TEST_SUCCESS;
773 }
774
775 static test_return_t gdb_abort_services_appliction_TEST(void *)
776 {
777 test_skip(0, access("/usr/bin/gdb", X_OK ));
778 test_skip(0, access("libtest/abort", X_OK ));
779 test_skip(true, false);
780
781 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
782 test_skip(0, TARGET_OS_OSX);
783 #endif
784
785 libtest::Application abort_app("libtest/abort", true);
786 abort_app.use_gdb(true);
787
788 ASSERT_EQ(Application::SUCCESS, abort_app.run());
789 ASSERT_EQ(Application::SUCCESS, abort_app.join());
790
791 std::string gdb_filename= abort_app.gdb_filename();
792 test_skip(0, access(gdb_filename.c_str(), R_OK ));
793 const char *args[]= { "SIGABRT", gdb_filename.c_str(), 0 };
794 ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("grep", args));
795
796 // Sanity test
797 args[0]= "THIS_WILL_NOT_BE_FOUND";
798 ASSERT_EQ(EXIT_FAILURE, exec_cmdline("grep", args));
799
800 return TEST_SUCCESS;
801 }
802
803 static test_return_t get_free_port_TEST(void *)
804 {
805 in_port_t ret_port;
806 test_true((ret_port= get_free_port()));
807 test_true(get_free_port() != default_port());
808 test_true(get_free_port() != get_free_port());
809
810 return TEST_SUCCESS;
811 }
812
813 static test_return_t fatal_TEST(void *)
814 {
815 ASSERT_EQ(fatal_calls++, fatal::disabled_counter());
816 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Testing va_args based fatal(): %d", 10);
817
818 return TEST_SUCCESS;
819 }
820
821 static test_return_t number_of_cpus_TEST(void *)
822 {
823 test_true(number_of_cpus() >= 1);
824
825 return TEST_SUCCESS;
826 }
827
828 static test_return_t check_dns_TEST(void *)
829 {
830 test_warn(libtest::check_dns(), "Broken DNS server/no DNS server found");
831
832 return TEST_SUCCESS;
833 }
834
835 static test_return_t Timer_TEST(void *)
836 {
837 int64_t minutes= random() % 50;
838 minutes++;
839
840 Timer check;
841
842 check.reset();
843 check.offset(minutes, 2, 200);
844
845 ASSERT_EQ(check.minutes(), minutes);
846
847 return TEST_SUCCESS;
848 }
849
850 static test_return_t lookup_true_TEST(void *)
851 {
852 test_warn(libtest::lookup("exist.gearman.info"), "dns is not currently working");
853 return TEST_SUCCESS;
854 }
855
856 static test_return_t lookup_false_TEST(void *)
857 {
858 if (libtest::lookup("does_not_exist.gearman.info"))
859 {
860 Error << "Broken DNS server detected";
861 return TEST_SKIPPED;
862 }
863
864 return TEST_SUCCESS;
865 }
866
867 static test_return_t create_tmpfile_TEST(void *)
868 {
869 test_skip(0, access("/usr/bin/touch", X_OK ));
870 std::string tmp= create_tmpfile(__func__);
871 ASSERT_EQ(-1, access(tmp.c_str(), R_OK));
872 ASSERT_EQ(-1, access(tmp.c_str(), F_OK));
873
874 Application touch_app("/usr/bin/touch");
875 const char *args[]= { tmp.c_str(), 0 };
876 ASSERT_EQ(Application::SUCCESS, touch_app.run(args));
877 ASSERT_EQ(Application::SUCCESS, touch_app.join());
878
879 ASSERT_EQ(0, access(tmp.c_str(), R_OK));
880 ASSERT_EQ(0, unlink(tmp.c_str()));
881
882 return TEST_SUCCESS;
883 }
884
885 static test_return_t fatal_message_TEST(void *)
886 {
887 ASSERT_EQ(fatal_calls++, fatal::disabled_counter());
888 fatal_message("Fatal test");
889
890 return TEST_SUCCESS;
891 }
892
893 static test_return_t default_port_TEST(void *)
894 {
895 in_port_t ret_port= default_port();
896 ASSERT_EQ(ret_port, libtest::default_port());
897 ASSERT_EQ(ret_port, libtest::default_port());
898
899 return TEST_SUCCESS;
900 }
901
902 static test_return_t check_for_gearman(void *)
903 {
904 test_skip(true, HAVE_LIBGEARMAN);
905 test_skip(true, has_gearmand());
906 #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
907 if (GEARMAND_BINARY)
908 {
909 if (strcmp(GEARMAND_BINARY, "./gearmand/gearmand"))
910 {
911 test_zero(access(GEARMAND_BINARY, X_OK ));
912 }
913 }
914 else
915 {
916 return TEST_SKIPPED;
917 }
918 #endif
919
920 testing_service= "gearmand";
921
922 return TEST_SUCCESS;
923 }
924
925 static test_return_t check_for_drizzle(void *)
926 {
927 test_skip(true, HAVE_LIBDRIZZLE);
928 test_skip(true, has_drizzled());
929
930 testing_service= "drizzled";
931
932 return TEST_SUCCESS;
933 }
934
935
936 test_st drizzled_tests[] ={
937 {"drizzled startup-shutdown", 0, drizzled_cycle_test },
938 {0, 0, 0}
939 };
940
941 test_st gearmand_tests[] ={
942 #if 0
943 {"pause", 0, pause_test },
944 #endif
945 {"gearmand startup-shutdown", 0, gearmand_cycle_test },
946 {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test },
947 {"server_startup(fail)", 0, server_startup_fail_TEST },
948 {0, 0, 0}
949 };
950
951 static test_return_t clear_servers(void* object)
952 {
953 server_startup_st *servers= (server_startup_st*)object;
954 test_true(servers);
955 servers->clear();
956
957 testing_service.clear();
958
959 return TEST_SUCCESS;
960 }
961
962 static test_return_t check_for_memcached(void* object)
963 {
964 test_skip(true, has_memcached());
965
966 server_startup_st *servers= (server_startup_st*)object;
967 test_true(servers);
968 servers->clear();
969
970 testing_service= "memcached";
971
972 return TEST_SUCCESS;
973 }
974
975 test_st memcached_TESTS[] ={
976 {"memcached startup-shutdown", 0, server_startup_TEST },
977 {"memcached(socket file) startup-shutdown", 0, socket_server_startup_TEST },
978 {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test },
979 {"server_startup(fail)", 0, server_startup_fail_TEST },
980 {0, 0, 0}
981 };
982
983 test_st test_skip_TESTS[] ={
984 {"true, true", 0, test_skip_true_TEST },
985 {"true, false", 0, test_skip_false_TEST },
986 {0, 0, 0}
987 };
988
989 test_st environment_tests[] ={
990 {"getenv()", 0, getenv_TEST },
991 {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test },
992 {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test },
993 {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test },
994 {"GDB_COMMAND", 0, GDB_COMMAND_test },
995 {0, 0, 0}
996 };
997
998 test_st tests_log[] ={
999 {"TEST_SUCCESS", false, test_success_test },
1000 {"TEST_FAILURE", false, test_failure_test },
1001 {"TEST_SUCCESS == 0", false, test_success_equals_one_test },
1002 {"SUCCESS", false, test_throw_success_TEST },
1003 {"libtest::__skipped", false, test_throw_skip_TEST },
1004 {"SKIP_IF", false, test_throw_skip_macro_TEST },
1005 {"FAIL", false, test_throw_fail_TEST },
1006 {"ASSERT_FALSE_", false, ASSERT_FALSE__TEST },
1007 {"ASSERT_FALSE", false, ASSERT_FALSE_TEST },
1008 {0, 0, 0}
1009 };
1010
1011 test_st local_log[] ={
1012 {"test_is_local()", 0, local_test },
1013 {"test_is_local(NOT)", 0, local_not_test },
1014 {0, 0, 0}
1015 };
1016
1017 test_st directories_tests[] ={
1018 {"var exists", 0, var_exists_test },
1019 {"var/tmp exists", 0, var_tmp_exists_test },
1020 {"var/run exists", 0, var_run_exists_test },
1021 {"var/log exists", 0, var_log_exists_test },
1022 {"var/drizzle exists", 0, var_drizzle_exists_test },
1023 {"var/tmp", 0, var_tmp_test },
1024 {"var/run", 0, var_run_test },
1025 {"var/log", 0, var_log_test },
1026 {"var/drizzle", 0, var_drizzle_test },
1027 {"var/tmp rm", 0, var_tmp_rm_test },
1028 {"var/run rm", 0, var_run_rm_test },
1029 {"var/log rm", 0, var_log_rm_test },
1030 {"var/drizzle rm", 0, var_drizzle_rm_test },
1031 {0, 0, 0}
1032 };
1033
1034 test_st comparison_tests[] ={
1035 {"_compare(test_return_t)", 0, _compare_test_return_t_test },
1036 {0, 0, 0}
1037 };
1038
1039 test_st cmdline_tests[] ={
1040 {"echo fubar", 0, echo_fubar_BINARY },
1041 {"core-count", 0, core_count_BINARY },
1042 {"wait --quiet", 0, wait_BINARY },
1043 {"wait --quiet --help", 0, wait_help_BINARY },
1044 {"wait --quiet --version", 0, wait_version_BINARY },
1045 {"wait --quiet /etc/services", 0, wait_services_BINARY },
1046 {"wait /etc/services", 0, wait_services_BINARY2 },
1047 {"wait /etc/services", 0, wait_services_appliction_TEST },
1048 {"gdb wait /etc/services", 0, gdb_wait_services_appliction_TEST },
1049 {"gdb abort", 0, gdb_abort_services_appliction_TEST },
1050 {0, 0, 0}
1051 };
1052
1053 test_st get_free_port_TESTS[] ={
1054 {"get_free_port()", 0, get_free_port_TEST },
1055 {"default_port()", 0, default_port_TEST },
1056 {0, 0, 0}
1057 };
1058
1059 test_st fatal_message_TESTS[] ={
1060 {"libtest::fatal", 0, fatal_TEST },
1061 {"fatal_message()", 0, fatal_message_TEST },
1062 {0, 0, 0}
1063 };
1064
1065 test_st number_of_cpus_TESTS[] ={
1066 {"libtest::number_of_cpus()", 0, number_of_cpus_TEST },
1067 {0, 0, 0}
1068 };
1069
1070 test_st create_tmpfile_TESTS[] ={
1071 {"libtest::create_tmpfile()", 0, create_tmpfile_TEST },
1072 {0, 0, 0}
1073 };
1074
1075 test_st timer_TESTS[] ={
1076 {"libtest::Timer", 0, Timer_TEST },
1077 {0, 0, 0}
1078 };
1079
1080 test_st dns_TESTS[] ={
1081 {"libtest::lookup(true)", 0, lookup_true_TEST },
1082 {"libtest::lookup(false)", 0, lookup_false_TEST },
1083 {"libtest::check_dns()", 0, check_dns_TEST },
1084 {0, 0, 0}
1085 };
1086
1087 test_st application_tests[] ={
1088 {"vchar_t", 0, vchar_t_TEST },
1089 {"vchar_t compare()", 0, vchar_t_compare_neg_TEST },
1090 {"true", 0, application_true_BINARY },
1091 {"gbd true --fubar", 0, application_gdb_true_BINARY },
1092 {"gbd true", 0, application_gdb_true_BINARY2 },
1093 {"true --fubar", 0, application_true_fubar_BINARY },
1094 {"doesnotexist --fubar", 0, application_doesnotexist_BINARY },
1095 {"echo fubar", 0, application_echo_fubar_BINARY },
1096 {"echo fubar (as option)", 0, application_echo_fubar_BINARY2 },
1097 {0, 0, 0}
1098 };
1099
1100 static test_return_t check_for_curl(void *)
1101 {
1102 test_skip(true, HAVE_LIBCURL);
1103 return TEST_SUCCESS;
1104 }
1105
1106 static test_return_t disable_fatal_exception(void *)
1107 {
1108 fatal_calls= 0;
1109 fatal::disable();
1110 return TEST_SUCCESS;
1111 }
1112
1113 static test_return_t enable_fatal_exception(void *)
1114 {
1115 fatal::enable();
1116 return TEST_SUCCESS;
1117 }
1118
1119 test_st http_tests[] ={
1120 {"GET", 0, GET_TEST },
1121 {"POST", 0, POST_TEST },
1122 {"TRACE", 0, TRACE_TEST },
1123 {0, 0, 0}
1124 };
1125
1126 collection_st collection[] ={
1127 {"environment", 0, 0, environment_tests},
1128 {"return values", 0, 0, tests_log},
1129 {"test_skip()", 0, 0, test_skip_TESTS },
1130 {"local", 0, 0, local_log},
1131 {"directories", 0, 0, directories_tests},
1132 {"comparison", 0, 0, comparison_tests},
1133 {"gearmand", check_for_gearman, clear_servers, gearmand_tests},
1134 {"memcached", check_for_memcached, clear_servers, memcached_TESTS },
1135 {"drizzled", check_for_drizzle, clear_servers, drizzled_tests},
1136 {"cmdline", 0, 0, cmdline_tests},
1137 {"application", 0, 0, application_tests},
1138 {"http", check_for_curl, 0, http_tests},
1139 {"http", check_for_curl, 0, http_tests},
1140 {"get_free_port()", 0, 0, get_free_port_TESTS },
1141 {"fatal", disable_fatal_exception, enable_fatal_exception, fatal_message_TESTS },
1142 {"number_of_cpus()", 0, 0, number_of_cpus_TESTS },
1143 {"create_tmpfile()", 0, 0, create_tmpfile_TESTS },
1144 {"dns", 0, 0, dns_TESTS },
1145 {"libtest::Timer", 0, 0, timer_TESTS },
1146 {0, 0, 0, 0}
1147 };
1148
1149 static void *world_create(server_startup_st& servers, test_return_t&)
1150 {
1151 return &servers;
1152 }
1153
1154 void get_world(libtest::Framework *world)
1155 {
1156 world->collections(collection);
1157 world->create(world_create);
1158 }