1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <libtest/test.hpp>
27 #if defined(LIBTEST_WITH_LIBMEMCACHED_SUPPORT) && LIBTEST_WITH_LIBMEMCACHED_SUPPORT
28 #include <libmemcached-1.0/memcached.h>
31 #if defined(LIBTEST_WITH_LIBGEARMAN_SUPPORT) && LIBTEST_WITH_LIBGEARMAN_SUPPORT
32 #include <libgearman/gearman.h>
38 using namespace libtest
;
40 static test_return_t
LIBTOOL_COMMAND_test(void *)
42 test_true(getenv("LIBTOOL_COMMAND"));
46 static test_return_t
VALGRIND_COMMAND_test(void *)
48 test_true(getenv("VALGRIND_COMMAND"));
52 static test_return_t
HELGRIND_COMMAND_test(void *)
54 test_true(getenv("HELGRIND_COMMAND"));
58 static test_return_t
GDB_COMMAND_test(void *)
60 test_true(getenv("GDB_COMMAND"));
64 static test_return_t
test_success_equals_one_test(void *)
66 test_skip(HAVE_LIBMEMCACHED
, true);
67 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
68 test_zero(MEMCACHED_SUCCESS
);
73 static test_return_t
test_success_test(void *)
78 static test_return_t
test_failure_test(void *)
80 return TEST_SKIPPED
; // Only run this when debugging
86 static test_return_t
local_test(void *)
88 if (getenv("LIBTEST_LOCAL"))
90 test_true(test_is_local());
94 test_false(test_is_local());
100 static test_return_t
local_not_test(void *)
107 if ((ptr
= getenv("LIBTEST_LOCAL")) == NULL
)
112 // unsetenv() will cause issues with valgrind
113 _compare(__FILE__
, __LINE__
, __func__
, 0, unsetenv("LIBTEST_LOCAL"));
114 test_compare(0, unsetenv("LIBTEST_LOCAL"));
115 test_false(test_is_local());
117 test_compare(0, setenv("LIBTEST_LOCAL", "1", 1));
118 test_true(test_is_local());
122 test_compare(0, unsetenv("LIBTEST_LOCAL"));
126 char *old_string
= strdup(temp
.c_str());
127 test_compare(0, setenv("LIBTEST_LOCAL", old_string
, 1));
133 static test_return_t
var_exists_test(void *)
135 test_compare(0, access("var", R_OK
| W_OK
| X_OK
));
139 static test_return_t
var_tmp_exists_test(void *)
141 test_compare(0, access("var/tmp", R_OK
| W_OK
| X_OK
));
145 static test_return_t
var_run_exists_test(void *)
147 test_compare(0, access("var/run", R_OK
| W_OK
| X_OK
));
151 static test_return_t
var_log_exists_test(void *)
153 test_compare(0, access("var/log", R_OK
| W_OK
| X_OK
));
157 static test_return_t
var_tmp_test(void *)
159 FILE *file
= fopen("var/tmp/junk", "w+");
161 const char *dir
= getcwd(buffer
, sizeof(buffer
));
162 test_true_got(file
, dir
);
167 static test_return_t
var_run_test(void *)
169 FILE *file
= fopen("var/run/junk", "w+");
175 static test_return_t
var_log_test(void *)
177 FILE *file
= fopen("var/log/junk", "w+");
183 static test_return_t
var_tmp_rm_test(void *)
185 test_true(unlink("var/tmp/junk") == 0);
189 static test_return_t
var_run_rm_test(void *)
191 test_true(unlink("var/run/junk") == 0);
195 static test_return_t
var_log_rm_test(void *)
197 test_true(unlink("var/log/junk") == 0);
201 static test_return_t
_compare_test_return_t_test(void *)
203 test_compare(TEST_SUCCESS
, TEST_SUCCESS
);
208 static test_return_t
_compare_memcached_return_t_test(void *)
210 test_skip(HAVE_LIBMEMCACHED
, true);
211 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
212 test_compare(MEMCACHED_SUCCESS
, MEMCACHED_SUCCESS
);
218 static test_return_t
_compare_gearman_return_t_test(void *)
220 test_skip(HAVE_LIBGEARMAN
, true);
221 #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
222 test_compare(GEARMAN_SUCCESS
, GEARMAN_SUCCESS
);
228 static test_return_t
gearmand_cycle_test(void *object
)
230 server_startup_st
*servers
= (server_startup_st
*)object
;
233 #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
234 test_true(has_gearmand_binary());
236 test_skip(true, has_gearmand_binary());
239 test_true(server_startup(*servers
, "gearmand", get_free_port(), 0, NULL
));
244 static test_return_t
memcached_light_cycle_TEST(void *object
)
246 server_startup_st
*servers
= (server_startup_st
*)object
;
249 test_skip(true, bool(HAVE_MEMCACHED_LIGHT_BINARY
));
251 test_true(server_startup(*servers
, "memcached-light", get_free_port(), 0, NULL
));
256 static test_return_t
memcached_cycle_test(void *object
)
258 server_startup_st
*servers
= (server_startup_st
*)object
;
261 if (MEMCACHED_BINARY
and HAVE_LIBMEMCACHED
)
263 test_true(has_memcached_binary());
264 test_true(server_startup(*servers
, "memcached", get_free_port(), 0, NULL
));
272 static test_return_t
memcached_socket_cycle_test(void *object
)
274 server_startup_st
*servers
= (server_startup_st
*)object
;
277 if (MEMCACHED_BINARY
)
279 if (HAVE_LIBMEMCACHED
)
281 test_true(has_memcached_binary());
282 test_true(servers
->start_socket_server("memcached", get_free_port(), 0, NULL
));
291 static test_return_t
memcached_sasl_test(void *object
)
293 server_startup_st
*servers
= (server_startup_st
*)object
;
296 if (getenv("TESTS_ENVIRONMENT"))
301 if (MEMCACHED_SASL_BINARY
)
303 if (HAVE_LIBMEMCACHED
)
305 test_true(has_memcached_sasl_binary());
306 test_true(server_startup(*servers
, "memcached-sasl", get_free_port(), 0, NULL
));
315 static test_return_t
application_true_BINARY(void *)
317 Application
true_app("true");
319 test_compare(Application::SUCCESS
, true_app
.run());
320 test_compare(Application::SUCCESS
, true_app
.wait());
325 static test_return_t
application_gdb_true_BINARY2(void *)
327 test_skip(0, access("/usr/bin/gdb", X_OK
));
328 Application
true_app("true");
331 test_compare(Application::SUCCESS
, true_app
.run());
332 test_compare(Application::SUCCESS
, true_app
.wait());
337 static test_return_t
application_gdb_true_BINARY(void *)
339 test_skip(0, access("/usr/bin/gdb", X_OK
));
340 Application
true_app("true");
343 const char *args
[]= { "--fubar", 0 };
344 test_compare(Application::SUCCESS
, true_app
.run(args
));
345 test_compare(Application::SUCCESS
, true_app
.wait());
350 static test_return_t
application_true_fubar_BINARY(void *)
352 Application
true_app("true");
354 const char *args
[]= { "--fubar", 0 };
355 test_compare(Application::SUCCESS
, true_app
.run(args
));
356 test_compare(Application::SUCCESS
, true_app
.wait());
357 test_compare(0, true_app
.stdout_result().size());
362 static test_return_t
application_doesnotexist_BINARY(void *)
364 Application
true_app("doesnotexist");
366 const char *args
[]= { "--fubar", 0 };
367 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
368 test_compare(Application::INVALID
, true_app
.run(args
));
370 test_compare(Application::SUCCESS
, true_app
.run(args
));
372 // Behavior is different if we are running under valgrind
373 if (getenv("TESTS_ENVIRONMENT") and strstr(getenv("TESTS_ENVIRONMENT"), "valgrind"))
375 test_compare(Application::FAILURE
, true_app
.wait());
379 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
380 test_compare(Application::FAILURE
, true_app
.wait());
382 if (getenv("TESTS_ENVIRONMENT") and strstr(getenv("TESTS_ENVIRONMENT"), "valgrind"))
384 test_compare(Application::FAILURE
, true_app
.wait());
388 test_compare(Application::INVALID
, true_app
.wait());
392 test_compare(0, true_app
.stdout_result().size());
397 static test_return_t
application_true_fubar_eq_doh_BINARY(void *)
399 Application
true_app("true");
401 const char *args
[]= { "--fubar=doh", 0 };
402 test_compare(Application::SUCCESS
, true_app
.run(args
));
403 test_compare(Application::SUCCESS
, true_app
.wait());
404 test_compare(0, true_app
.stdout_result().size());
409 static test_return_t
application_true_fubar_eq_doh_option_BINARY(void *)
411 Application
true_app("true");
413 true_app
.add_option("--fubar=", "doh");
415 test_compare(Application::SUCCESS
, true_app
.run());
416 test_compare(Application::SUCCESS
, true_app
.wait());
417 test_compare(0, true_app
.stdout_result().size());
423 static test_return_t
GET_TEST(void *)
425 libtest::http::GET
get("http://foo.example.com/");
427 test_compare(false, get
.execute());
432 static test_return_t
POST_TEST(void *)
434 libtest::vchar_t body
;
435 libtest::http::POST
post("http://foo.example.com/", body
);
437 test_compare(false, post
.execute());
442 static test_return_t
TRACE_TEST(void *)
444 libtest::vchar_t body
;
445 libtest::http::TRACE
trace("http://foo.example.com/", body
);
447 test_compare(false, trace
.execute());
453 static test_return_t
vchar_t_TEST(void *)
455 libtest::vchar_t response
;
456 libtest::make_vector(response
, test_literal_param("fubar\n"));
457 test_compare(response
, response
);
462 static test_return_t
application_echo_fubar_BINARY(void *)
464 Application
true_app("echo");
466 const char *args
[]= { "fubar", 0 };
467 test_compare(Application::SUCCESS
, true_app
.run(args
));
468 test_compare(Application::SUCCESS
, true_app
.wait());
470 libtest::vchar_t response
;
471 make_vector(response
, test_literal_param("fubar\n"));
472 test_compare(response
, true_app
.stdout_result());
477 static test_return_t
application_echo_fubar_BINARY2(void *)
479 Application
true_app("echo");
481 true_app
.add_option("fubar");
483 test_compare(Application::SUCCESS
, true_app
.run());
484 test_compare(Application::SUCCESS
, true_app
.wait());
485 libtest::vchar_t response
;
486 make_vector(response
, test_literal_param("fubar\n"));
487 test_compare(response
, true_app
.stdout_result());
492 static test_return_t
true_BINARY(void *)
494 const char *args
[]= { 0 };
495 test_compare(EXIT_SUCCESS
, exec_cmdline("true", args
));
500 static test_return_t
true_fubar_BINARY(void *)
502 const char *args
[]= { "--fubar", 0 };
503 test_compare(EXIT_SUCCESS
, exec_cmdline("true", args
));
508 static test_return_t
echo_fubar_BINARY(void *)
510 const char *args
[]= { "fubar", 0 };
511 test_compare(EXIT_SUCCESS
, exec_cmdline("echo", args
));
516 static test_return_t
wait_BINARY(void *)
518 const char *args
[]= { "--quiet", 0 };
520 test_compare(EXIT_FAILURE
, exec_cmdline("libtest/wait", args
, true));
525 static test_return_t
wait_help_BINARY(void *)
527 const char *args
[]= { "--quiet", "--help", 0 };
529 test_compare(EXIT_SUCCESS
, exec_cmdline("libtest/wait", args
, true));
534 static test_return_t
wait_version_BINARY(void *)
536 const char *args
[]= { "--quiet", "--version", 0 };
538 test_compare(EXIT_SUCCESS
, exec_cmdline("libtest/wait", args
, true));
543 static test_return_t
wait_services_BINARY(void *)
545 test_skip(0, access("/etc/services", R_OK
));
547 const char *args
[]= { "--quiet", "/etc/services", 0 };
549 test_compare(EXIT_SUCCESS
, exec_cmdline("libtest/wait", args
, true));
554 static test_return_t
wait_services_BINARY2(void *)
556 test_skip(0, access("/etc/services", R_OK
));
558 const char *args
[]= { "/etc/services", 0 };
560 test_compare(EXIT_SUCCESS
, exec_cmdline("libtest/wait", args
, true));
565 static test_return_t
wait_services_appliction_TEST(void *)
567 test_skip(0, access("/usr/bin/gdb", X_OK
));
568 test_skip(0, access("/etc/services", R_OK
));
570 libtest::Application
wait_app("libtest/wait", true);
573 const char *args
[]= { "/etc/services", 0 };
574 test_compare(Application::SUCCESS
, wait_app
.run(args
));
575 test_compare(Application::SUCCESS
, wait_app
.wait());
580 static test_return_t
gdb_wait_services_appliction_TEST(void *)
582 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
583 test_skip(0, TARGET_OS_OSX
);
586 test_skip(0, access("/usr/bin/gdb", X_OK
));
587 test_skip(0, access("/etc/services", R_OK
));
589 libtest::Application
wait_app("libtest/wait", true);
592 const char *args
[]= { "/etc/services", 0 };
593 test_compare(Application::SUCCESS
, wait_app
.run(args
));
594 test_compare(Application::SUCCESS
, wait_app
.wait());
599 static test_return_t
gdb_abort_services_appliction_TEST(void *)
601 test_skip(0, access("/usr/bin/gdb", X_OK
));
603 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
604 test_skip(0, TARGET_OS_OSX
);
607 libtest::Application
abort_app("libtest/abort", true);
610 test_compare(Application::SUCCESS
, abort_app
.run());
611 test_compare(Application::SUCCESS
, abort_app
.wait());
613 std::string gdb_filename
= abort_app
.gdb_filename();
614 const char *args
[]= { "SIGABRT", gdb_filename
.c_str(), 0 };
615 test_compare(EXIT_SUCCESS
, exec_cmdline("grep", args
));
618 args
[0]= "THIS_WILL_NOT_BE_FOUND";
619 test_compare(EXIT_FAILURE
, exec_cmdline("grep", args
));
624 static test_return_t
get_free_port_TEST(void *)
627 test_true_hint((ret_port
= get_free_port()), ret_port
);
628 test_true(get_free_port() != default_port());
629 test_true(get_free_port() != get_free_port());
634 static uint32_t fatal_calls
= 0;
636 static test_return_t
fatal_TEST(void *)
638 test_compare(fatal_calls
++, fatal::disabled_counter());
639 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Testing va_args based fatal(): %d", 10);
644 static test_return_t
number_of_cpus_TEST(void *)
646 test_true(number_of_cpus() >= 1);
651 static test_return_t
create_tmpfile_TEST(void *)
653 std::string tmp
= create_tmpfile(__func__
);
655 Application
touch_app("touch");
656 const char *args
[]= { tmp
.c_str(), 0 };
657 test_compare(Application::SUCCESS
, touch_app
.run(args
));
658 test_compare(Application::SUCCESS
, touch_app
.wait());
660 test_compare_hint(0, access(tmp
.c_str(), R_OK
), strerror(errno
));
661 test_compare_hint(0, unlink(tmp
.c_str()), strerror(errno
));
666 static test_return_t
fatal_message_TEST(void *)
668 test_compare(fatal_calls
++, fatal::disabled_counter());
669 throw fatal_message("Fatal test");
674 static test_return_t
default_port_TEST(void *)
676 in_port_t ret_port
= default_port();
677 test_compare(ret_port
, libtest::default_port());
678 test_compare(ret_port
, libtest::default_port());
683 static test_return_t
check_for_gearman(void *)
685 test_skip(true, HAVE_LIBGEARMAN
);
686 test_skip(true, has_gearmand_binary());
691 test_st gearmand_tests
[] ={
693 {"pause", 0, pause_test
},
695 {"gearmand startup-shutdown", 0, gearmand_cycle_test
},
696 {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test
},
700 static test_return_t
check_for_libmemcached(void *)
702 test_skip(true, HAVE_LIBMEMCACHED
);
703 test_skip(true, has_memcached_binary());
707 test_st memcached_tests
[] ={
708 {"memcached startup-shutdown", 0, memcached_cycle_test
},
709 {"memcached-light startup-shutdown", 0, memcached_light_cycle_TEST
},
710 {"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test
},
711 {"memcached_sasl() startup-shutdown", 0, memcached_sasl_test
},
712 {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test
},
716 test_st environment_tests
[] ={
717 {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test
},
718 {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test
},
719 {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test
},
720 {"GDB_COMMAND", 0, GDB_COMMAND_test
},
724 test_st tests_log
[] ={
725 {"TEST_SUCCESS", false, test_success_test
},
726 {"TEST_FAILURE", false, test_failure_test
},
727 {"TEST_SUCCESS == 0", false, test_success_equals_one_test
},
731 test_st local_log
[] ={
732 {"test_is_local()", 0, local_test
},
733 {"test_is_local(NOT)", 0, local_not_test
},
737 test_st directories_tests
[] ={
738 {"var exists", 0, var_exists_test
},
739 {"var/tmp exists", 0, var_tmp_exists_test
},
740 {"var/run exists", 0, var_run_exists_test
},
741 {"var/log exists", 0, var_log_exists_test
},
742 {"var/tmp", 0, var_tmp_test
},
743 {"var/run", 0, var_run_test
},
744 {"var/log", 0, var_log_test
},
745 {"var/tmp rm", 0, var_tmp_rm_test
},
746 {"var/run rm", 0, var_run_rm_test
},
747 {"var/log rm", 0, var_log_rm_test
},
751 test_st comparison_tests
[] ={
752 {"_compare(test_return_t)", 0, _compare_test_return_t_test
},
756 test_st cmdline_tests
[] ={
757 {"true", 0, true_BINARY
},
758 {"true --fubar", 0, true_fubar_BINARY
},
759 {"echo fubar", 0, echo_fubar_BINARY
},
760 {"wait --quiet", 0, wait_BINARY
},
761 {"wait --quiet --help", 0, wait_help_BINARY
},
762 {"wait --quiet --version", 0, wait_version_BINARY
},
763 {"wait --quiet /etc/services", 0, wait_services_BINARY
},
764 {"wait /etc/services", 0, wait_services_BINARY2
},
765 {"wait /etc/services", 0, wait_services_appliction_TEST
},
766 {"gdb wait /etc/services", 0, gdb_wait_services_appliction_TEST
},
767 {"gdb abort", 0, gdb_abort_services_appliction_TEST
},
771 test_st get_free_port_TESTS
[] ={
772 {"get_free_port()", 0, get_free_port_TEST
},
773 {"default_port()", 0, default_port_TEST
},
777 test_st fatal_message_TESTS
[] ={
778 {"libtest::fatal", 0, fatal_TEST
},
779 {"fatal_message()", 0, fatal_message_TEST
},
783 test_st number_of_cpus_TESTS
[] ={
784 {"libtest::number_of_cpus()", 0, number_of_cpus_TEST
},
788 test_st create_tmpfile_TESTS
[] ={
789 {"libtest::create_tmpfile()", 0, create_tmpfile_TEST
},
793 test_st application_tests
[] ={
794 {"vchar_t", 0, vchar_t_TEST
},
795 {"true", 0, application_true_BINARY
},
796 {"gbd true --fubar", 0, application_gdb_true_BINARY
},
797 {"gbd true", 0, application_gdb_true_BINARY2
},
798 {"true --fubar", 0, application_true_fubar_BINARY
},
799 {"doesnotexist --fubar", 0, application_doesnotexist_BINARY
},
800 {"true --fubar=doh", 0, application_true_fubar_eq_doh_BINARY
},
801 {"true --fubar=doh add_option()", 0, application_true_fubar_eq_doh_option_BINARY
},
802 {"echo fubar", 0, application_echo_fubar_BINARY
},
803 {"echo fubar (as option)", 0, application_echo_fubar_BINARY2
},
807 static test_return_t
check_for_curl(void *)
809 test_skip(true, HAVE_LIBCURL
);
813 static test_return_t
disable_fatal_exception(void *)
819 static test_return_t
enable_fatal_exception(void *)
825 test_st http_tests
[] ={
826 {"GET", 0, GET_TEST
},
827 {"POST", 0, POST_TEST
},
828 {"TRACE", 0, TRACE_TEST
},
832 collection_st collection
[] ={
833 {"environment", 0, 0, environment_tests
},
834 {"return values", 0, 0, tests_log
},
835 {"local", 0, 0, local_log
},
836 {"directories", 0, 0, directories_tests
},
837 {"comparison", 0, 0, comparison_tests
},
838 {"gearmand", check_for_gearman
, 0, gearmand_tests
},
839 {"memcached", check_for_libmemcached
, 0, memcached_tests
},
840 {"cmdline", 0, 0, cmdline_tests
},
841 {"application", 0, 0, application_tests
},
842 {"http", check_for_curl
, 0, http_tests
},
843 {"get_free_port()", 0, 0, get_free_port_TESTS
},
844 {"fatal", disable_fatal_exception
, enable_fatal_exception
, fatal_message_TESTS
},
845 {"number_of_cpus()", 0, 0, number_of_cpus_TESTS
},
846 {"create_tmpfile()", 0, 0, create_tmpfile_TESTS
},
850 static void *world_create(server_startup_st
& servers
, test_return_t
&)
855 void get_world(Framework
*world
)
857 world
->collections
= collection
;
858 world
->_create
= world_create
;