Update libtest/associated tests.
[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 const char *argv[1]= { "cycle_gearmand" };
240 test_true(server_startup(*servers, "gearmand", 9999, 1, argv));
241
242 return TEST_SUCCESS;
243 }
244
245 static test_return_t memcached_cycle_test(void *object)
246 {
247 server_startup_st *servers= (server_startup_st*)object;
248 test_true(servers);
249
250 if (MEMCACHED_BINARY and HAVE_LIBMEMCACHED)
251 {
252 test_true(has_memcached_binary());
253 const char *argv[1]= { "cycle_memcached" };
254 test_true(server_startup(*servers, "memcached", 9998, 1, argv));
255
256 return TEST_SUCCESS;
257 }
258
259 return TEST_SKIPPED;
260 }
261
262 static test_return_t memcached_socket_cycle_test(void *object)
263 {
264 server_startup_st *servers= (server_startup_st*)object;
265 test_true(servers);
266
267 if (MEMCACHED_BINARY)
268 {
269 if (HAVE_LIBMEMCACHED)
270 {
271 test_true(has_memcached_binary());
272 const char *argv[1]= { "cycle_memcached" };
273 test_true(servers->start_socket_server("memcached", 9997, 1, argv));
274
275 return TEST_SUCCESS;
276 }
277 }
278
279 return TEST_SKIPPED;
280 }
281
282 static test_return_t memcached_sasl_test(void *object)
283 {
284 server_startup_st *servers= (server_startup_st*)object;
285 test_true(servers);
286
287 if (getenv("TESTS_ENVIRONMENT"))
288 {
289 return TEST_SKIPPED;
290 }
291
292 if (MEMCACHED_SASL_BINARY)
293 {
294 if (HAVE_LIBMEMCACHED)
295 {
296 test_true(has_memcached_sasl_binary());
297 const char *argv[1]= { "cycle_memcached_sasl" };
298 test_true(server_startup(*servers, "memcached-sasl", 9996, 1, argv));
299
300 return TEST_SUCCESS;
301 }
302 }
303
304 return TEST_SKIPPED;
305 }
306
307 static test_return_t application_true_BINARY(void *)
308 {
309 Application true_app("true");
310
311 test_compare(Application::SUCCESS, true_app.run());
312 test_compare(Application::SUCCESS, true_app.wait());
313
314 return TEST_SUCCESS;
315 }
316
317 static test_return_t application_true_fubar_BINARY(void *)
318 {
319 Application true_app("true");
320
321 const char *args[]= { "--fubar", 0 };
322 test_compare(Application::SUCCESS, true_app.run(args));
323 test_compare(Application::SUCCESS, true_app.wait());
324 test_compare(0, true_app.stdout_result().size());
325
326 return TEST_SUCCESS;
327 }
328
329 static test_return_t application_true_fubar_eq_doh_BINARY(void *)
330 {
331 Application true_app("true");
332
333 const char *args[]= { "--fubar=doh", 0 };
334 test_compare(Application::SUCCESS, true_app.run(args));
335 test_compare(Application::SUCCESS, true_app.wait());
336 test_compare(0, true_app.stdout_result().size());
337
338 return TEST_SUCCESS;
339 }
340
341 static test_return_t application_true_fubar_eq_doh_option_BINARY(void *)
342 {
343 Application true_app("true");
344
345 true_app.add_option("--fubar=", "doh");
346
347 test_compare(Application::SUCCESS, true_app.run());
348 test_compare(Application::SUCCESS, true_app.wait());
349 test_compare(0, true_app.stdout_result().size());
350
351 return TEST_SUCCESS;
352 }
353
354
355 static test_return_t GET_TEST(void *)
356 {
357 libtest::http::GET get("http://foo.example.com/");
358
359 test_compare(false, get.execute());
360
361 return TEST_SUCCESS;
362 }
363
364 static test_return_t POST_TEST(void *)
365 {
366 libtest::vchar_t body;
367 libtest::http::POST post("http://foo.example.com/", body);
368
369 test_compare(false, post.execute());
370
371 return TEST_SUCCESS;
372 }
373
374 static test_return_t TRACE_TEST(void *)
375 {
376 libtest::vchar_t body;
377 libtest::http::TRACE trace("http://foo.example.com/", body);
378
379 test_compare(false, trace.execute());
380
381 return TEST_SUCCESS;
382 }
383
384
385 static test_return_t vchar_t_TEST(void *)
386 {
387 libtest::vchar_t response;
388 libtest::make_vector(response, test_literal_param("fubar\n"));
389 test_compare(response, response);
390
391 return TEST_SUCCESS;
392 }
393
394 static test_return_t application_echo_fubar_BINARY(void *)
395 {
396 Application true_app("echo");
397
398 const char *args[]= { "fubar", 0 };
399 test_compare(Application::SUCCESS, true_app.run(args));
400 test_compare(Application::SUCCESS, true_app.wait());
401
402 libtest::vchar_t response;
403 make_vector(response, test_literal_param("fubar\n"));
404 test_compare(response, true_app.stdout_result());
405
406 return TEST_SUCCESS;
407 }
408
409 static test_return_t application_echo_fubar_BINARY2(void *)
410 {
411 Application true_app("echo");
412
413 true_app.add_option("fubar");
414
415 test_compare(Application::SUCCESS, true_app.run());
416 test_compare(Application::SUCCESS, true_app.wait());
417 libtest::vchar_t response;
418 make_vector(response, test_literal_param("fubar\n"));
419 test_compare(response, true_app.stdout_result());
420
421 return TEST_SUCCESS;
422 }
423
424 static test_return_t true_BINARY(void *)
425 {
426 const char *args[]= { 0 };
427 test_compare(EXIT_SUCCESS, exec_cmdline("true", args));
428
429 return TEST_SUCCESS;
430 }
431
432 static test_return_t true_fubar_BINARY(void *)
433 {
434 const char *args[]= { "--fubar", 0 };
435 test_compare(EXIT_SUCCESS, exec_cmdline("true", args));
436
437 return TEST_SUCCESS;
438 }
439
440 static test_return_t echo_fubar_BINARY(void *)
441 {
442 const char *args[]= { "fubar", 0 };
443 test_compare(EXIT_SUCCESS, exec_cmdline("echo", args));
444
445 return TEST_SUCCESS;
446 }
447
448 static test_return_t wait_BINARY(void *)
449 {
450 const char *args[]= { "--quiet", 0 };
451
452 test_compare(EXIT_FAILURE, exec_cmdline("libtest/wait", args, true));
453
454 return TEST_SUCCESS;
455 }
456
457 static test_return_t wait_help_BINARY(void *)
458 {
459 const char *args[]= { "--quiet", "--help", 0 };
460
461 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
462
463 return TEST_SUCCESS;
464 }
465
466 static test_return_t wait_version_BINARY(void *)
467 {
468 const char *args[]= { "--quiet", "--version", 0 };
469
470 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
471
472 return TEST_SUCCESS;
473 }
474
475 static test_return_t wait_services_BINARY(void *)
476 {
477 test_skip(0, access("/etc/services", R_OK ));
478
479 const char *args[]= { "--quiet", "/etc/services", 0 };
480
481 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
482
483 return TEST_SUCCESS;
484 }
485
486 static test_return_t wait_services_BINARY2(void *)
487 {
488 test_skip(0, access("/etc/services", R_OK ));
489
490 const char *args[]= { "/etc/services", 0 };
491
492 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
493
494 return TEST_SUCCESS;
495 }
496
497 static test_return_t application_wait_services_BINARY2(void *)
498 {
499 test_skip(0, access("/etc/services", R_OK ));
500
501 libtest::Application("libtest/wait", true);
502 const char *args[]= { "/etc/services", 0 };
503
504 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
505
506 return TEST_SUCCESS;
507 }
508
509 static test_return_t check_for_gearman(void *)
510 {
511 test_skip(true, HAVE_LIBGEARMAN);
512 test_skip(true, has_gearmand_binary());
513 return TEST_SUCCESS;
514 }
515
516
517 test_st gearmand_tests[] ={
518 #if 0
519 {"pause", 0, pause_test },
520 #endif
521 {"gearmand startup-shutdown", 0, gearmand_cycle_test },
522 {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test },
523 {0, 0, 0}
524 };
525
526 static test_return_t check_for_libmemcached(void *)
527 {
528 test_skip(true, HAVE_LIBMEMCACHED);
529 test_skip(true, has_memcached_binary());
530 return TEST_SUCCESS;
531 }
532
533 test_st memcached_tests[] ={
534 {"memcached startup-shutdown", 0, memcached_cycle_test },
535 {"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test },
536 {"memcached_sasl() startup-shutdown", 0, memcached_sasl_test },
537 {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test },
538 {0, 0, 0}
539 };
540
541 test_st environment_tests[] ={
542 {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test },
543 {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test },
544 {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test },
545 {"GDB_COMMAND", 0, GDB_COMMAND_test },
546 {0, 0, 0}
547 };
548
549 test_st tests_log[] ={
550 {"TEST_SUCCESS", false, test_success_test },
551 {"TEST_FAILURE", false, test_failure_test },
552 {"TEST_SUCCESS == 0", false, test_success_equals_one_test },
553 {0, 0, 0}
554 };
555
556 test_st local_log[] ={
557 {"test_is_local()", 0, local_test },
558 {"test_is_local(NOT)", 0, local_not_test },
559 {0, 0, 0}
560 };
561
562 test_st directories_tests[] ={
563 {"var exists", 0, var_exists_test },
564 {"var/tmp exists", 0, var_tmp_exists_test },
565 {"var/run exists", 0, var_run_exists_test },
566 {"var/log exists", 0, var_log_exists_test },
567 {"var/tmp", 0, var_tmp_test },
568 {"var/run", 0, var_run_test },
569 {"var/log", 0, var_log_test },
570 {"var/tmp rm", 0, var_tmp_rm_test },
571 {"var/run rm", 0, var_run_rm_test },
572 {"var/log rm", 0, var_log_rm_test },
573 {0, 0, 0}
574 };
575
576 test_st comparison_tests[] ={
577 {"_compare(test_return_t)", 0, _compare_test_return_t_test },
578 {0, 0, 0}
579 };
580
581 test_st cmdline_tests[] ={
582 {"true", 0, true_BINARY },
583 {"true --fubar", 0, true_fubar_BINARY },
584 {"echo fubar", 0, echo_fubar_BINARY },
585 {"wait --quiet", 0, wait_BINARY },
586 {"wait --quiet --help", 0, wait_help_BINARY },
587 {"wait --quiet --version", 0, wait_version_BINARY },
588 {"wait --quiet /etc/services", 0, wait_services_BINARY },
589 {"wait /etc/services", 0, wait_services_BINARY2 },
590 {0, 0, 0}
591 };
592
593 test_st application_tests[] ={
594 {"vchar_t", 0, vchar_t_TEST },
595 {"true", 0, application_true_BINARY },
596 {"true --fubar", 0, application_true_fubar_BINARY },
597 {"true --fubar=doh", 0, application_true_fubar_eq_doh_BINARY },
598 {"true --fubar=doh add_option()", 0, application_true_fubar_eq_doh_option_BINARY },
599 {"echo fubar", 0, application_echo_fubar_BINARY },
600 {"echo fubar (as option)", 0, application_echo_fubar_BINARY2 },
601 {0, 0, 0}
602 };
603
604 static test_return_t check_for_curl(void *)
605 {
606 test_skip(true, HAVE_LIBCURL);
607 return TEST_SUCCESS;
608 }
609
610 test_st http_tests[] ={
611 {"GET", 0, GET_TEST },
612 {"POST", 0, POST_TEST },
613 {"TRACE", 0, TRACE_TEST },
614 {0, 0, 0}
615 };
616
617 collection_st collection[] ={
618 {"environment", 0, 0, environment_tests},
619 {"return values", 0, 0, tests_log},
620 {"local", 0, 0, local_log},
621 {"directories", 0, 0, directories_tests},
622 {"comparison", 0, 0, comparison_tests},
623 {"gearmand", check_for_gearman, 0, gearmand_tests},
624 {"memcached", check_for_libmemcached, 0, memcached_tests},
625 {"cmdline", 0, 0, cmdline_tests},
626 {"application", 0, 0, application_tests},
627 {"http", check_for_curl, 0, http_tests},
628 {0, 0, 0, 0}
629 };
630
631 static void *world_create(server_startup_st& servers, test_return_t&)
632 {
633 return &servers;
634 }
635
636 void get_world(Framework *world)
637 {
638 world->collections= collection;
639 world->_create= world_create;
640 }