53fb605fd658cca034db7b6f5f7be3563855f275
[m6w6/libmemcached] / libtest / unittest.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
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.
11 *
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.
16 *
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
20 */
21
22
23 #include <config.h>
24
25 #include <libtest/test.hpp>
26
27 #if defined(LIBTEST_WITH_LIBMEMCACHED_SUPPORT) && LIBTEST_WITH_LIBMEMCACHED_SUPPORT
28 #include <libmemcached-1.0/memcached.h>
29 #endif
30
31 #if defined(LIBTEST_WITH_LIBGEARMAN_SUPPORT) && LIBTEST_WITH_LIBGEARMAN_SUPPORT
32 #include <libgearman/gearman.h>
33 #endif
34
35 #include <cstdlib>
36 #include <unistd.h>
37
38 using namespace libtest;
39
40 static test_return_t LIBTOOL_COMMAND_test(void *)
41 {
42 test_true(getenv("LIBTOOL_COMMAND"));
43 return TEST_SUCCESS;
44 }
45
46 static test_return_t VALGRIND_COMMAND_test(void *)
47 {
48 test_true(getenv("VALGRIND_COMMAND"));
49 return TEST_SUCCESS;
50 }
51
52 static test_return_t HELGRIND_COMMAND_test(void *)
53 {
54 test_true(getenv("HELGRIND_COMMAND"));
55 return TEST_SUCCESS;
56 }
57
58 static test_return_t GDB_COMMAND_test(void *)
59 {
60 test_true(getenv("GDB_COMMAND"));
61 return TEST_SUCCESS;
62 }
63
64 static test_return_t test_success_equals_one_test(void *)
65 {
66 test_skip(HAVE_LIBMEMCACHED, true);
67 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
68 test_zero(MEMCACHED_SUCCESS);
69 #endif
70 return TEST_SUCCESS;
71 }
72
73 static test_return_t test_success_test(void *)
74 {
75 return TEST_SUCCESS;
76 }
77
78 static test_return_t test_failure_test(void *)
79 {
80 return TEST_SKIPPED; // Only run this when debugging
81
82 test_compare(1, 2);
83 return TEST_SUCCESS;
84 }
85
86 static test_return_t local_test(void *)
87 {
88 if (getenv("LIBTEST_LOCAL"))
89 {
90 test_true(test_is_local());
91 }
92 else
93 {
94 test_false(test_is_local());
95 }
96
97 return TEST_SUCCESS;
98 }
99
100 static test_return_t local_not_test(void *)
101 {
102 return TEST_SKIPPED;
103
104 std::string temp;
105
106 const char *ptr;
107 if ((ptr= getenv("LIBTEST_LOCAL")) == NULL)
108 {
109 temp.append(ptr);
110 }
111
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());
116
117 test_compare(0, setenv("LIBTEST_LOCAL", "1", 1));
118 test_true(test_is_local());
119
120 if (temp.empty())
121 {
122 test_compare(0, unsetenv("LIBTEST_LOCAL"));
123 }
124 else
125 {
126 char *old_string= strdup(temp.c_str());
127 test_compare(0, setenv("LIBTEST_LOCAL", old_string, 1));
128 }
129
130 return TEST_SUCCESS;
131 }
132
133 static test_return_t var_exists_test(void *)
134 {
135 test_compare(0, access("var", R_OK | W_OK | X_OK));
136 return TEST_SUCCESS;
137 }
138
139 static test_return_t var_tmp_exists_test(void *)
140 {
141 test_compare(0, access("var/tmp", R_OK | W_OK | X_OK));
142 return TEST_SUCCESS;
143 }
144
145 static test_return_t var_run_exists_test(void *)
146 {
147 test_compare(0, access("var/run", R_OK | W_OK | X_OK));
148 return TEST_SUCCESS;
149 }
150
151 static test_return_t var_log_exists_test(void *)
152 {
153 test_compare(0, access("var/log", R_OK | W_OK | X_OK));
154 return TEST_SUCCESS;
155 }
156
157 static test_return_t var_tmp_test(void *)
158 {
159 FILE *file= fopen("var/tmp/junk", "w+");
160 char buffer[1024];
161 const char *dir= getcwd(buffer, sizeof(buffer));
162 test_true_got(file, dir);
163 fclose(file);
164 return TEST_SUCCESS;
165 }
166
167 static test_return_t var_run_test(void *)
168 {
169 FILE *file= fopen("var/run/junk", "w+");
170 test_true(file);
171 fclose(file);
172 return TEST_SUCCESS;
173 }
174
175 static test_return_t var_log_test(void *)
176 {
177 FILE *file= fopen("var/log/junk", "w+");
178 test_true(file);
179 fclose(file);
180 return TEST_SUCCESS;
181 }
182
183 static test_return_t var_tmp_rm_test(void *)
184 {
185 test_true(unlink("var/tmp/junk") == 0);
186 return TEST_SUCCESS;
187 }
188
189 static test_return_t var_run_rm_test(void *)
190 {
191 test_true(unlink("var/run/junk") == 0);
192 return TEST_SUCCESS;
193 }
194
195 static test_return_t var_log_rm_test(void *)
196 {
197 test_true(unlink("var/log/junk") == 0);
198 return TEST_SUCCESS;
199 }
200
201 static test_return_t _compare_test_return_t_test(void *)
202 {
203 test_compare(TEST_SUCCESS, TEST_SUCCESS);
204
205 return TEST_SUCCESS;
206 }
207
208 static test_return_t _compare_memcached_return_t_test(void *)
209 {
210 test_skip(HAVE_LIBMEMCACHED, true);
211 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
212 test_compare(MEMCACHED_SUCCESS, MEMCACHED_SUCCESS);
213 #endif
214
215 return TEST_SUCCESS;
216 }
217
218 static test_return_t _compare_gearman_return_t_test(void *)
219 {
220 test_skip(HAVE_LIBGEARMAN, true);
221 #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
222 test_compare(GEARMAN_SUCCESS, GEARMAN_SUCCESS);
223 #endif
224
225 return TEST_SUCCESS;
226 }
227
228 static test_return_t gearmand_cycle_test(void *object)
229 {
230 server_startup_st *servers= (server_startup_st*)object;
231 test_true(servers);
232
233 #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
234 test_true(has_gearmand_binary());
235 #else
236 test_skip(true, has_gearmand_binary());
237 #endif
238
239 test_true(server_startup(*servers, "gearmand", get_free_port(), 0, NULL));
240
241 return TEST_SUCCESS;
242 }
243
244 static test_return_t memcached_cycle_test(void *object)
245 {
246 server_startup_st *servers= (server_startup_st*)object;
247 test_true(servers);
248
249 if (MEMCACHED_BINARY and HAVE_LIBMEMCACHED)
250 {
251 test_true(has_memcached_binary());
252 test_true(server_startup(*servers, "memcached", get_free_port(), 0, NULL));
253
254 return TEST_SUCCESS;
255 }
256
257 return TEST_SKIPPED;
258 }
259
260 static test_return_t memcached_socket_cycle_test(void *object)
261 {
262 server_startup_st *servers= (server_startup_st*)object;
263 test_true(servers);
264
265 if (MEMCACHED_BINARY)
266 {
267 if (HAVE_LIBMEMCACHED)
268 {
269 test_true(has_memcached_binary());
270 test_true(servers->start_socket_server("memcached", get_free_port(), 0, NULL));
271
272 return TEST_SUCCESS;
273 }
274 }
275
276 return TEST_SKIPPED;
277 }
278
279 static test_return_t memcached_sasl_test(void *object)
280 {
281 server_startup_st *servers= (server_startup_st*)object;
282 test_true(servers);
283
284 if (getenv("TESTS_ENVIRONMENT"))
285 {
286 return TEST_SKIPPED;
287 }
288
289 if (MEMCACHED_SASL_BINARY)
290 {
291 if (HAVE_LIBMEMCACHED)
292 {
293 test_true(has_memcached_sasl_binary());
294 test_true(server_startup(*servers, "memcached-sasl", get_free_port(), 0, NULL));
295
296 return TEST_SUCCESS;
297 }
298 }
299
300 return TEST_SKIPPED;
301 }
302
303 static test_return_t application_true_BINARY(void *)
304 {
305 Application true_app("true");
306
307 test_compare(Application::SUCCESS, true_app.run());
308 test_compare(Application::SUCCESS, true_app.wait());
309
310 return TEST_SUCCESS;
311 }
312
313 static test_return_t application_true_fubar_BINARY(void *)
314 {
315 Application true_app("true");
316
317 const char *args[]= { "--fubar", 0 };
318 test_compare(Application::SUCCESS, true_app.run(args));
319 test_compare(Application::SUCCESS, true_app.wait());
320 test_compare(0, true_app.stdout_result().size());
321
322 return TEST_SUCCESS;
323 }
324
325 static test_return_t application_true_fubar_eq_doh_BINARY(void *)
326 {
327 Application true_app("true");
328
329 const char *args[]= { "--fubar=doh", 0 };
330 test_compare(Application::SUCCESS, true_app.run(args));
331 test_compare(Application::SUCCESS, true_app.wait());
332 test_compare(0, true_app.stdout_result().size());
333
334 return TEST_SUCCESS;
335 }
336
337 static test_return_t application_true_fubar_eq_doh_option_BINARY(void *)
338 {
339 Application true_app("true");
340
341 true_app.add_option("--fubar=", "doh");
342
343 test_compare(Application::SUCCESS, true_app.run());
344 test_compare(Application::SUCCESS, true_app.wait());
345 test_compare(0, true_app.stdout_result().size());
346
347 return TEST_SUCCESS;
348 }
349
350
351 static test_return_t GET_TEST(void *)
352 {
353 libtest::http::GET get("http://foo.example.com/");
354
355 test_compare(false, get.execute());
356
357 return TEST_SUCCESS;
358 }
359
360 static test_return_t POST_TEST(void *)
361 {
362 libtest::vchar_t body;
363 libtest::http::POST post("http://foo.example.com/", body);
364
365 test_compare(false, post.execute());
366
367 return TEST_SUCCESS;
368 }
369
370 static test_return_t TRACE_TEST(void *)
371 {
372 libtest::vchar_t body;
373 libtest::http::TRACE trace("http://foo.example.com/", body);
374
375 test_compare(false, trace.execute());
376
377 return TEST_SUCCESS;
378 }
379
380
381 static test_return_t vchar_t_TEST(void *)
382 {
383 libtest::vchar_t response;
384 libtest::make_vector(response, test_literal_param("fubar\n"));
385 test_compare(response, response);
386
387 return TEST_SUCCESS;
388 }
389
390 static test_return_t application_echo_fubar_BINARY(void *)
391 {
392 Application true_app("echo");
393
394 const char *args[]= { "fubar", 0 };
395 test_compare(Application::SUCCESS, true_app.run(args));
396 test_compare(Application::SUCCESS, true_app.wait());
397
398 libtest::vchar_t response;
399 make_vector(response, test_literal_param("fubar\n"));
400 test_compare(response, true_app.stdout_result());
401
402 return TEST_SUCCESS;
403 }
404
405 static test_return_t application_echo_fubar_BINARY2(void *)
406 {
407 Application true_app("echo");
408
409 true_app.add_option("fubar");
410
411 test_compare(Application::SUCCESS, true_app.run());
412 test_compare(Application::SUCCESS, true_app.wait());
413 libtest::vchar_t response;
414 make_vector(response, test_literal_param("fubar\n"));
415 test_compare(response, true_app.stdout_result());
416
417 return TEST_SUCCESS;
418 }
419
420 static test_return_t true_BINARY(void *)
421 {
422 const char *args[]= { 0 };
423 test_compare(EXIT_SUCCESS, exec_cmdline("true", args));
424
425 return TEST_SUCCESS;
426 }
427
428 static test_return_t true_fubar_BINARY(void *)
429 {
430 const char *args[]= { "--fubar", 0 };
431 test_compare(EXIT_SUCCESS, exec_cmdline("true", args));
432
433 return TEST_SUCCESS;
434 }
435
436 static test_return_t echo_fubar_BINARY(void *)
437 {
438 const char *args[]= { "fubar", 0 };
439 test_compare(EXIT_SUCCESS, exec_cmdline("echo", args));
440
441 return TEST_SUCCESS;
442 }
443
444 static test_return_t wait_BINARY(void *)
445 {
446 const char *args[]= { "--quiet", 0 };
447
448 test_compare(EXIT_FAILURE, exec_cmdline("libtest/wait", args, true));
449
450 return TEST_SUCCESS;
451 }
452
453 static test_return_t wait_help_BINARY(void *)
454 {
455 const char *args[]= { "--quiet", "--help", 0 };
456
457 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
458
459 return TEST_SUCCESS;
460 }
461
462 static test_return_t wait_version_BINARY(void *)
463 {
464 const char *args[]= { "--quiet", "--version", 0 };
465
466 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
467
468 return TEST_SUCCESS;
469 }
470
471 static test_return_t wait_services_BINARY(void *)
472 {
473 test_skip(0, access("/etc/services", R_OK ));
474
475 const char *args[]= { "--quiet", "/etc/services", 0 };
476
477 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
478
479 return TEST_SUCCESS;
480 }
481
482 static test_return_t wait_services_BINARY2(void *)
483 {
484 test_skip(0, access("/etc/services", R_OK ));
485
486 const char *args[]= { "/etc/services", 0 };
487
488 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
489
490 return TEST_SUCCESS;
491 }
492
493 static test_return_t application_wait_services_BINARY2(void *)
494 {
495 test_skip(0, access("/etc/services", R_OK ));
496
497 libtest::Application("libtest/wait", true);
498 const char *args[]= { "/etc/services", 0 };
499
500 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
501
502 return TEST_SUCCESS;
503 }
504
505 static test_return_t get_free_port_TEST(void *)
506 {
507 in_port_t ret_port;
508 test_true_hint((ret_port= get_free_port()), ret_port);
509 test_true(get_free_port() != default_port());
510 test_true(get_free_port() != get_free_port());
511
512 return TEST_SUCCESS;
513 }
514
515 static uint32_t fatal_calls= 0;
516
517 static test_return_t fatal_TEST(void *)
518 {
519 test_compare(fatal_calls++, fatal::disabled_counter());
520 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Testing va_args based fatal(): %d", 10);
521
522 return TEST_SUCCESS;
523 }
524
525 static test_return_t fatal_message_TEST(void *)
526 {
527 test_compare(fatal_calls++, fatal::disabled_counter());
528 throw fatal_message("Fatal test");
529
530 return TEST_SUCCESS;
531 }
532
533 static test_return_t default_port_TEST(void *)
534 {
535 in_port_t ret_port= default_port();
536 test_compare(ret_port, libtest::default_port());
537 test_compare(ret_port, libtest::default_port());
538
539 return TEST_SUCCESS;
540 }
541
542 static test_return_t check_for_gearman(void *)
543 {
544 test_skip(true, HAVE_LIBGEARMAN);
545 test_skip(true, has_gearmand_binary());
546 return TEST_SUCCESS;
547 }
548
549
550 test_st gearmand_tests[] ={
551 #if 0
552 {"pause", 0, pause_test },
553 #endif
554 {"gearmand startup-shutdown", 0, gearmand_cycle_test },
555 {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test },
556 {0, 0, 0}
557 };
558
559 static test_return_t check_for_libmemcached(void *)
560 {
561 test_skip(true, HAVE_LIBMEMCACHED);
562 test_skip(true, has_memcached_binary());
563 return TEST_SUCCESS;
564 }
565
566 test_st memcached_tests[] ={
567 {"memcached startup-shutdown", 0, memcached_cycle_test },
568 {"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test },
569 {"memcached_sasl() startup-shutdown", 0, memcached_sasl_test },
570 {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test },
571 {0, 0, 0}
572 };
573
574 test_st environment_tests[] ={
575 {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test },
576 {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test },
577 {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test },
578 {"GDB_COMMAND", 0, GDB_COMMAND_test },
579 {0, 0, 0}
580 };
581
582 test_st tests_log[] ={
583 {"TEST_SUCCESS", false, test_success_test },
584 {"TEST_FAILURE", false, test_failure_test },
585 {"TEST_SUCCESS == 0", false, test_success_equals_one_test },
586 {0, 0, 0}
587 };
588
589 test_st local_log[] ={
590 {"test_is_local()", 0, local_test },
591 {"test_is_local(NOT)", 0, local_not_test },
592 {0, 0, 0}
593 };
594
595 test_st directories_tests[] ={
596 {"var exists", 0, var_exists_test },
597 {"var/tmp exists", 0, var_tmp_exists_test },
598 {"var/run exists", 0, var_run_exists_test },
599 {"var/log exists", 0, var_log_exists_test },
600 {"var/tmp", 0, var_tmp_test },
601 {"var/run", 0, var_run_test },
602 {"var/log", 0, var_log_test },
603 {"var/tmp rm", 0, var_tmp_rm_test },
604 {"var/run rm", 0, var_run_rm_test },
605 {"var/log rm", 0, var_log_rm_test },
606 {0, 0, 0}
607 };
608
609 test_st comparison_tests[] ={
610 {"_compare(test_return_t)", 0, _compare_test_return_t_test },
611 {0, 0, 0}
612 };
613
614 test_st cmdline_tests[] ={
615 {"true", 0, true_BINARY },
616 {"true --fubar", 0, true_fubar_BINARY },
617 {"echo fubar", 0, echo_fubar_BINARY },
618 {"wait --quiet", 0, wait_BINARY },
619 {"wait --quiet --help", 0, wait_help_BINARY },
620 {"wait --quiet --version", 0, wait_version_BINARY },
621 {"wait --quiet /etc/services", 0, wait_services_BINARY },
622 {"wait /etc/services", 0, wait_services_BINARY2 },
623 {0, 0, 0}
624 };
625
626 test_st get_free_port_TESTS[] ={
627 {"get_free_port()", 0, get_free_port_TEST },
628 {"default_port()", 0, default_port_TEST },
629 {0, 0, 0}
630 };
631
632 test_st fatal_message_TESTS[] ={
633 {"libtest::fatal", 0, fatal_TEST },
634 {"fatal_message()", 0, fatal_message_TEST },
635 {0, 0, 0}
636 };
637
638 test_st application_tests[] ={
639 {"vchar_t", 0, vchar_t_TEST },
640 {"true", 0, application_true_BINARY },
641 {"true --fubar", 0, application_true_fubar_BINARY },
642 {"true --fubar=doh", 0, application_true_fubar_eq_doh_BINARY },
643 {"true --fubar=doh add_option()", 0, application_true_fubar_eq_doh_option_BINARY },
644 {"echo fubar", 0, application_echo_fubar_BINARY },
645 {"echo fubar (as option)", 0, application_echo_fubar_BINARY2 },
646 {0, 0, 0}
647 };
648
649 static test_return_t check_for_curl(void *)
650 {
651 test_skip(true, HAVE_LIBCURL);
652 return TEST_SUCCESS;
653 }
654
655 static test_return_t disable_fatal_exception(void *)
656 {
657 fatal::disable();
658 return TEST_SUCCESS;
659 }
660
661 static test_return_t enable_fatal_exception(void *)
662 {
663 fatal::disable();
664 return TEST_SUCCESS;
665 }
666
667 test_st http_tests[] ={
668 {"GET", 0, GET_TEST },
669 {"POST", 0, POST_TEST },
670 {"TRACE", 0, TRACE_TEST },
671 {0, 0, 0}
672 };
673
674 collection_st collection[] ={
675 {"environment", 0, 0, environment_tests},
676 {"return values", 0, 0, tests_log},
677 {"local", 0, 0, local_log},
678 {"directories", 0, 0, directories_tests},
679 {"comparison", 0, 0, comparison_tests},
680 {"gearmand", check_for_gearman, 0, gearmand_tests},
681 {"memcached", check_for_libmemcached, 0, memcached_tests},
682 {"cmdline", 0, 0, cmdline_tests},
683 {"application", 0, 0, application_tests},
684 {"http", check_for_curl, 0, http_tests},
685 {"get_free_port()", 0, 0, get_free_port_TESTS },
686 {"fatal", disable_fatal_exception, enable_fatal_exception, fatal_message_TESTS },
687 {0, 0, 0, 0}
688 };
689
690 static void *world_create(server_startup_st& servers, test_return_t&)
691 {
692 return &servers;
693 }
694
695 void get_world(Framework *world)
696 {
697 world->collections= collection;
698 world->_create= world_create;
699 }