unittest.cc 16 KB

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