unittest.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. #include <config.h>
  37. #include <libtest/test.hpp>
  38. #if defined(LIBTEST_WITH_LIBMEMCACHED_SUPPORT) && LIBTEST_WITH_LIBMEMCACHED_SUPPORT
  39. #include <libmemcached-1.0/memcached.h>
  40. #endif
  41. #if defined(LIBTEST_WITH_LIBGEARMAN_SUPPORT) && LIBTEST_WITH_LIBGEARMAN_SUPPORT
  42. #include <libgearman/gearman.h>
  43. #endif
  44. #include <cstdlib>
  45. #include <unistd.h>
  46. using namespace libtest;
  47. static std::string testing_service;
  48. static test_return_t LIBTOOL_COMMAND_test(void *)
  49. {
  50. test_true(getenv("LIBTOOL_COMMAND"));
  51. return TEST_SUCCESS;
  52. }
  53. static test_return_t VALGRIND_COMMAND_test(void *)
  54. {
  55. test_true(getenv("VALGRIND_COMMAND"));
  56. return TEST_SUCCESS;
  57. }
  58. static test_return_t HELGRIND_COMMAND_test(void *)
  59. {
  60. test_true(getenv("HELGRIND_COMMAND"));
  61. return TEST_SUCCESS;
  62. }
  63. static test_return_t GDB_COMMAND_test(void *)
  64. {
  65. test_true(getenv("GDB_COMMAND"));
  66. return TEST_SUCCESS;
  67. }
  68. static test_return_t test_success_equals_one_test(void *)
  69. {
  70. test_skip(HAVE_LIBMEMCACHED, 1);
  71. #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
  72. test_zero(MEMCACHED_SUCCESS);
  73. #endif
  74. return TEST_SUCCESS;
  75. }
  76. static test_return_t test_success_test(void *)
  77. {
  78. return TEST_SUCCESS;
  79. }
  80. static test_return_t test_failure_test(void *)
  81. {
  82. return TEST_SKIPPED; // Only run this when debugging
  83. test_compare(1, 2);
  84. return TEST_SUCCESS;
  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. return TEST_SUCCESS;
  97. }
  98. static test_return_t local_not_test(void *)
  99. {
  100. return TEST_SKIPPED;
  101. std::string temp;
  102. const char *ptr;
  103. if ((ptr= getenv("LIBTEST_LOCAL")) == NULL)
  104. {
  105. temp.append(ptr);
  106. }
  107. // unsetenv() will cause issues with valgrind
  108. _compare(__FILE__, __LINE__, __func__, 0, unsetenv("LIBTEST_LOCAL"), true);
  109. test_compare(0, unsetenv("LIBTEST_LOCAL"));
  110. test_false(test_is_local());
  111. test_compare(0, setenv("LIBTEST_LOCAL", "1", 1));
  112. test_true(test_is_local());
  113. if (temp.empty())
  114. {
  115. test_compare(0, unsetenv("LIBTEST_LOCAL"));
  116. }
  117. else
  118. {
  119. char *old_string= strdup(temp.c_str());
  120. test_compare(0, setenv("LIBTEST_LOCAL", old_string, 1));
  121. }
  122. return TEST_SUCCESS;
  123. }
  124. static test_return_t var_exists_test(void *)
  125. {
  126. test_compare(0, access("var", R_OK | W_OK | X_OK));
  127. return TEST_SUCCESS;
  128. }
  129. static test_return_t var_tmp_exists_test(void *)
  130. {
  131. test_compare(0, access("var/tmp", R_OK | W_OK | X_OK));
  132. return TEST_SUCCESS;
  133. }
  134. static test_return_t var_run_exists_test(void *)
  135. {
  136. test_compare(0, access("var/run", R_OK | W_OK | X_OK));
  137. return TEST_SUCCESS;
  138. }
  139. static test_return_t var_log_exists_test(void *)
  140. {
  141. test_compare(0, access("var/log", R_OK | W_OK | X_OK));
  142. return TEST_SUCCESS;
  143. }
  144. static test_return_t var_drizzle_exists_test(void *)
  145. {
  146. test_compare(0, access("var/drizzle", R_OK | W_OK | X_OK));
  147. return TEST_SUCCESS;
  148. }
  149. static test_return_t var_tmp_test(void *)
  150. {
  151. FILE *file= fopen("var/tmp/junk", "w+");
  152. test_true(file);
  153. fclose(file);
  154. return TEST_SUCCESS;
  155. }
  156. static test_return_t var_run_test(void *)
  157. {
  158. FILE *file= fopen("var/run/junk", "w+");
  159. test_true(file);
  160. fclose(file);
  161. return TEST_SUCCESS;
  162. }
  163. static test_return_t var_log_test(void *)
  164. {
  165. FILE *file= fopen("var/log/junk", "w+");
  166. test_true(file);
  167. fclose(file);
  168. return TEST_SUCCESS;
  169. }
  170. static test_return_t var_drizzle_test(void *)
  171. {
  172. FILE *file= fopen("var/drizzle/junk", "w+");
  173. test_true(file);
  174. fclose(file);
  175. return TEST_SUCCESS;
  176. }
  177. static test_return_t var_tmp_rm_test(void *)
  178. {
  179. test_true(unlink("var/tmp/junk") == 0);
  180. return TEST_SUCCESS;
  181. }
  182. static test_return_t var_run_rm_test(void *)
  183. {
  184. test_true(unlink("var/run/junk") == 0);
  185. return TEST_SUCCESS;
  186. }
  187. static test_return_t var_log_rm_test(void *)
  188. {
  189. test_true(unlink("var/log/junk") == 0);
  190. return TEST_SUCCESS;
  191. }
  192. static test_return_t var_drizzle_rm_test(void *)
  193. {
  194. test_true(unlink("var/drizzle/junk") == 0);
  195. return TEST_SUCCESS;
  196. }
  197. static test_return_t _compare_test_return_t_test(void *)
  198. {
  199. test_compare(TEST_SUCCESS, TEST_SUCCESS);
  200. return TEST_SUCCESS;
  201. }
  202. static test_return_t _compare_memcached_return_t_test(void *)
  203. {
  204. test_skip(HAVE_LIBMEMCACHED, true);
  205. #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
  206. test_compare(MEMCACHED_SUCCESS, MEMCACHED_SUCCESS);
  207. #endif
  208. return TEST_SUCCESS;
  209. }
  210. static test_return_t _compare_gearman_return_t_test(void *)
  211. {
  212. test_skip(HAVE_LIBGEARMAN, true);
  213. #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
  214. test_compare(GEARMAN_SUCCESS, GEARMAN_SUCCESS);
  215. #endif
  216. return TEST_SUCCESS;
  217. }
  218. static test_return_t drizzled_cycle_test(void *object)
  219. {
  220. server_startup_st *servers= (server_startup_st*)object;
  221. test_true(servers and servers->validate());
  222. #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
  223. test_true(has_drizzled());
  224. #endif
  225. test_skip(true, has_drizzled());
  226. test_true(server_startup(*servers, "drizzled", get_free_port(), 0, NULL));
  227. return TEST_SUCCESS;
  228. }
  229. static test_return_t gearmand_cycle_test(void *object)
  230. {
  231. server_startup_st *servers= (server_startup_st*)object;
  232. test_true(servers and servers->validate());
  233. #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
  234. test_true(has_gearmand());
  235. #endif
  236. test_skip(true, has_gearmand());
  237. test_true(server_startup(*servers, "gearmand", get_free_port(), 0, NULL));
  238. return TEST_SUCCESS;
  239. }
  240. static test_return_t memcached_light_cycle_TEST(void *object)
  241. {
  242. server_startup_st *servers= (server_startup_st*)object;
  243. test_true(servers);
  244. test_skip(true, bool(HAVE_MEMCACHED_LIGHT_BINARY));
  245. test_true(server_startup(*servers, "memcached-light", get_free_port(), 0, NULL));
  246. return TEST_SUCCESS;
  247. }
  248. static test_return_t skip_shim(bool a, bool b)
  249. {
  250. test_skip(a, b);
  251. return TEST_SUCCESS;
  252. }
  253. static test_return_t test_skip_true_TEST(void *object)
  254. {
  255. test_compare(true, true);
  256. test_compare(false, false);
  257. test_compare(TEST_SUCCESS, skip_shim(true, true));
  258. test_compare(TEST_SUCCESS, skip_shim(false, false));
  259. return TEST_SUCCESS;
  260. }
  261. static test_return_t test_skip_false_TEST(void *object)
  262. {
  263. test_compare(TEST_SKIPPED, skip_shim(true, false));
  264. test_compare(TEST_SKIPPED, skip_shim(false, true));
  265. return TEST_SUCCESS;
  266. }
  267. static test_return_t server_startup_TEST(void *object)
  268. {
  269. server_startup_st *servers= (server_startup_st*)object;
  270. test_true(servers);
  271. test_true(servers->start_server(testing_service, get_free_port(), 0, NULL, true));
  272. test_true(servers->last());
  273. pid_t last_pid= servers->last()->pid();
  274. test_compare(servers->last()->pid(), last_pid);
  275. test_true(last_pid > 1);
  276. test_compare(kill(last_pid, 0), 0);
  277. test_true(servers->shutdown());
  278. #if 0
  279. test_compare(servers->last()->pid(), -1);
  280. test_compare(kill(last_pid, 0), -1);
  281. #endif
  282. return TEST_SUCCESS;
  283. }
  284. static test_return_t socket_server_startup_TEST(void *object)
  285. {
  286. server_startup_st *servers= (server_startup_st*)object;
  287. test_true(servers);
  288. test_true(servers->start_socket_server(testing_service, get_free_port(), 0, NULL, true));
  289. return TEST_SUCCESS;
  290. }
  291. static test_return_t memcached_sasl_test(void *object)
  292. {
  293. server_startup_st *servers= (server_startup_st*)object;
  294. test_true(servers);
  295. test_skip(false, bool(getenv("TESTS_ENVIRONMENT")));
  296. if (MEMCACHED_SASL_BINARY)
  297. {
  298. if (HAVE_LIBMEMCACHED)
  299. {
  300. test_true(has_memcached_sasl());
  301. test_true(server_startup(*servers, "memcached-sasl", get_free_port(), 0, NULL));
  302. return TEST_SUCCESS;
  303. }
  304. }
  305. return TEST_SKIPPED;
  306. }
  307. static test_return_t application_true_BINARY(void *)
  308. {
  309. Application true_app("true");
  310. test_compare(Application::SUCCESS, true_app.run());
  311. test_compare(Application::SUCCESS, true_app.wait());
  312. return TEST_SUCCESS;
  313. }
  314. static test_return_t application_gdb_true_BINARY2(void *)
  315. {
  316. test_skip(0, access("/usr/bin/gdb", X_OK ));
  317. Application true_app("true");
  318. true_app.use_gdb();
  319. test_compare(Application::SUCCESS, true_app.run());
  320. test_compare(Application::SUCCESS, true_app.wait());
  321. return TEST_SUCCESS;
  322. }
  323. static test_return_t application_gdb_true_BINARY(void *)
  324. {
  325. test_skip(0, access("/usr/bin/gdb", X_OK ));
  326. Application true_app("true");
  327. true_app.use_gdb();
  328. const char *args[]= { "--fubar", 0 };
  329. test_compare(Application::SUCCESS, true_app.run(args));
  330. test_compare(Application::SUCCESS, true_app.wait());
  331. return TEST_SUCCESS;
  332. }
  333. static test_return_t application_true_fubar_BINARY(void *)
  334. {
  335. Application true_app("true");
  336. const char *args[]= { "--fubar", 0 };
  337. test_compare(Application::SUCCESS, true_app.run(args));
  338. test_compare(Application::SUCCESS, true_app.wait());
  339. test_compare(0, true_app.stdout_result().size());
  340. return TEST_SUCCESS;
  341. }
  342. static test_return_t application_doesnotexist_BINARY(void *)
  343. {
  344. test_skip_valgrind();
  345. Application true_app("doesnotexist");
  346. true_app.will_fail();
  347. const char *args[]= { "--fubar", 0 };
  348. #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
  349. test_compare(Application::INVALID, true_app.run(args));
  350. #else
  351. test_compare(Application::SUCCESS, true_app.run(args));
  352. test_compare(Application::INVALID, true_app.wait(false));
  353. #endif
  354. test_compare(0, true_app.stdout_result().size());
  355. return TEST_SUCCESS;
  356. }
  357. static test_return_t application_true_fubar_eq_doh_BINARY(void *)
  358. {
  359. Application true_app("true");
  360. const char *args[]= { "--fubar=doh", 0 };
  361. test_compare(Application::SUCCESS, true_app.run(args));
  362. test_compare(Application::SUCCESS, true_app.wait());
  363. test_compare(0, true_app.stdout_result().size());
  364. return TEST_SUCCESS;
  365. }
  366. static test_return_t application_true_fubar_eq_doh_option_BINARY(void *)
  367. {
  368. Application true_app("true");
  369. true_app.add_option("--fubar=", "doh");
  370. test_compare(Application::SUCCESS, true_app.run());
  371. test_compare(Application::SUCCESS, true_app.wait());
  372. test_compare(0, true_app.stdout_result().size());
  373. return TEST_SUCCESS;
  374. }
  375. static test_return_t GET_TEST(void *)
  376. {
  377. libtest::http::GET get("http://foo.example.com/");
  378. test_compare(false, get.execute());
  379. return TEST_SUCCESS;
  380. }
  381. static test_return_t POST_TEST(void *)
  382. {
  383. libtest::vchar_t body;
  384. libtest::http::POST post("http://foo.example.com/", body);
  385. test_compare(false, post.execute());
  386. return TEST_SUCCESS;
  387. }
  388. static test_return_t TRACE_TEST(void *)
  389. {
  390. libtest::vchar_t body;
  391. libtest::http::TRACE trace("http://foo.example.com/", body);
  392. test_compare(false, trace.execute());
  393. return TEST_SUCCESS;
  394. }
  395. static test_return_t vchar_t_TEST(void *)
  396. {
  397. libtest::vchar_t response;
  398. libtest::make_vector(response, test_literal_param("fubar\n"));
  399. test_compare(response, response);
  400. return TEST_SUCCESS;
  401. }
  402. static test_return_t vchar_t_compare_neg_TEST(void *)
  403. {
  404. libtest::vchar_t response;
  405. libtest::vchar_t response2;
  406. libtest::make_vector(response, test_literal_param("fubar\n"));
  407. libtest::make_vector(response2, test_literal_param(__func__));
  408. test_true(response != response2);
  409. return TEST_SUCCESS;
  410. }
  411. static test_return_t application_echo_fubar_BINARY(void *)
  412. {
  413. Application true_app("echo");
  414. const char *args[]= { "fubar", 0 };
  415. test_compare(Application::SUCCESS, true_app.run(args));
  416. test_compare(Application::SUCCESS, true_app.wait());
  417. while (true_app.slurp() == false) {} ;
  418. libtest::vchar_t response;
  419. make_vector(response, test_literal_param("fubar\n"));
  420. test_compare(response, true_app.stdout_result());
  421. return TEST_SUCCESS;
  422. }
  423. static test_return_t application_echo_fubar_BINARY2(void *)
  424. {
  425. Application true_app("echo");
  426. true_app.add_option("fubar");
  427. test_compare(Application::SUCCESS, true_app.run());
  428. test_compare(Application::SUCCESS, true_app.wait(false));
  429. libtest::vchar_t response;
  430. make_vector(response, test_literal_param("fubar\n"));
  431. test_compare(response, true_app.stdout_result());
  432. return TEST_SUCCESS;
  433. }
  434. static test_return_t true_BINARY(void *)
  435. {
  436. const char *args[]= { 0 };
  437. test_compare(EXIT_SUCCESS, exec_cmdline("true", args));
  438. return TEST_SUCCESS;
  439. }
  440. static test_return_t true_fubar_BINARY(void *)
  441. {
  442. const char *args[]= { "--fubar", 0 };
  443. test_compare(EXIT_SUCCESS, exec_cmdline("true", args));
  444. return TEST_SUCCESS;
  445. }
  446. static test_return_t echo_fubar_BINARY(void *)
  447. {
  448. const char *args[]= { "fubar", 0 };
  449. test_compare(EXIT_SUCCESS, exec_cmdline("echo", args));
  450. return TEST_SUCCESS;
  451. }
  452. static test_return_t wait_BINARY(void *)
  453. {
  454. const char *args[]= { "--quiet", 0 };
  455. test_compare(EXIT_FAILURE, exec_cmdline("libtest/wait", args, true));
  456. return TEST_SUCCESS;
  457. }
  458. static test_return_t wait_help_BINARY(void *)
  459. {
  460. const char *args[]= { "--quiet", "--help", 0 };
  461. test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
  462. return TEST_SUCCESS;
  463. }
  464. static test_return_t wait_version_BINARY(void *)
  465. {
  466. const char *args[]= { "--quiet", "--version", 0 };
  467. test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
  468. return TEST_SUCCESS;
  469. }
  470. static test_return_t wait_services_BINARY(void *)
  471. {
  472. test_skip(0, access("/etc/services", R_OK ));
  473. const char *args[]= { "--quiet", "/etc/services", 0 };
  474. test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
  475. return TEST_SUCCESS;
  476. }
  477. static test_return_t wait_services_BINARY2(void *)
  478. {
  479. test_skip(0, access("/etc/services", R_OK ));
  480. const char *args[]= { "/etc/services", 0 };
  481. test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
  482. return TEST_SUCCESS;
  483. }
  484. static test_return_t wait_services_appliction_TEST(void *)
  485. {
  486. test_skip(0, access("/usr/bin/gdb", X_OK ));
  487. test_skip(0, access("/etc/services", R_OK ));
  488. libtest::Application wait_app("libtest/wait", true);
  489. wait_app.use_gdb();
  490. const char *args[]= { "/etc/services", 0 };
  491. test_compare(Application::SUCCESS, wait_app.run(args));
  492. test_compare(Application::SUCCESS, wait_app.wait());
  493. return TEST_SUCCESS;
  494. }
  495. static test_return_t gdb_wait_services_appliction_TEST(void *)
  496. {
  497. test_skip(true, false);
  498. #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
  499. test_skip(0, TARGET_OS_OSX);
  500. #endif
  501. test_skip(0, access("/usr/bin/gdb", X_OK ));
  502. test_skip(0, access("/etc/services", R_OK ));
  503. libtest::Application wait_app("libtest/wait", true);
  504. wait_app.use_gdb();
  505. const char *args[]= { "/etc/services", 0 };
  506. test_compare(Application::SUCCESS, wait_app.run(args));
  507. test_compare(Application::SUCCESS, wait_app.wait());
  508. return TEST_SUCCESS;
  509. }
  510. static test_return_t gdb_abort_services_appliction_TEST(void *)
  511. {
  512. test_skip(true, false);
  513. test_skip(0, access("/usr/bin/gdb", X_OK ));
  514. #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
  515. test_skip(0, TARGET_OS_OSX);
  516. #endif
  517. libtest::Application abort_app("libtest/abort", true);
  518. abort_app.use_gdb();
  519. test_compare(Application::SUCCESS, abort_app.run());
  520. test_compare(Application::SUCCESS, abort_app.wait());
  521. std::string gdb_filename= abort_app.gdb_filename();
  522. test_skip(0, access(gdb_filename.c_str(), R_OK ));
  523. const char *args[]= { "SIGABRT", gdb_filename.c_str(), 0 };
  524. test_compare(EXIT_SUCCESS, exec_cmdline("grep", args));
  525. // Sanity test
  526. args[0]= "THIS_WILL_NOT_BE_FOUND";
  527. test_compare(EXIT_FAILURE, exec_cmdline("grep", args));
  528. return TEST_SUCCESS;
  529. }
  530. static test_return_t get_free_port_TEST(void *)
  531. {
  532. in_port_t ret_port;
  533. test_true((ret_port= get_free_port()));
  534. test_true(get_free_port() != default_port());
  535. test_true(get_free_port() != get_free_port());
  536. return TEST_SUCCESS;
  537. }
  538. static uint32_t fatal_calls= 0;
  539. static test_return_t fatal_TEST(void *)
  540. {
  541. test_compare(fatal_calls++, fatal::disabled_counter());
  542. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Testing va_args based fatal(): %d", 10);
  543. return TEST_SUCCESS;
  544. }
  545. static test_return_t number_of_cpus_TEST(void *)
  546. {
  547. test_true(number_of_cpus() >= 1);
  548. return TEST_SUCCESS;
  549. }
  550. static test_return_t check_dns_TEST(void *)
  551. {
  552. test_warn(libtest::check_dns(), "Broken DNS server/no DNS server found");
  553. return TEST_SUCCESS;
  554. }
  555. static test_return_t Timer_TEST(void *)
  556. {
  557. int64_t minutes= random() % 50;
  558. minutes++;
  559. Timer check;
  560. check.reset();
  561. check.offset(minutes, 2, 200);
  562. test_compare(check.minutes(), minutes);
  563. return TEST_SUCCESS;
  564. }
  565. static test_return_t lookup_true_TEST(void *)
  566. {
  567. test_warn(libtest::lookup("exist.gearman.info"), "dns is not currently working");
  568. return TEST_SUCCESS;
  569. }
  570. static test_return_t lookup_false_TEST(void *)
  571. {
  572. if (libtest::lookup("does_not_exist.gearman.info"))
  573. {
  574. Error << "Broken DNS server detected";
  575. return TEST_SKIPPED;
  576. }
  577. return TEST_SUCCESS;
  578. }
  579. static test_return_t create_tmpfile_TEST(void *)
  580. {
  581. std::string tmp= create_tmpfile(__func__);
  582. test_compare(-1, access(tmp.c_str(), R_OK));
  583. test_compare(-1, access(tmp.c_str(), F_OK));
  584. Application touch_app("touch");
  585. const char *args[]= { tmp.c_str(), 0 };
  586. test_compare(Application::SUCCESS, touch_app.run(args));
  587. test_compare(Application::SUCCESS, touch_app.wait(false));
  588. test_compare(0, access(tmp.c_str(), R_OK));
  589. test_compare(0, unlink(tmp.c_str()));
  590. return TEST_SUCCESS;
  591. }
  592. static test_return_t fatal_message_TEST(void *)
  593. {
  594. test_compare(fatal_calls++, fatal::disabled_counter());
  595. fatal_message("Fatal test");
  596. return TEST_SUCCESS;
  597. }
  598. static test_return_t default_port_TEST(void *)
  599. {
  600. in_port_t ret_port= default_port();
  601. test_compare(ret_port, libtest::default_port());
  602. test_compare(ret_port, libtest::default_port());
  603. return TEST_SUCCESS;
  604. }
  605. static test_return_t check_for_gearman(void *)
  606. {
  607. test_skip(true, HAVE_LIBGEARMAN);
  608. test_skip(true, has_gearmand());
  609. return TEST_SUCCESS;
  610. }
  611. static test_return_t check_for_drizzle(void *)
  612. {
  613. test_skip(true, HAVE_LIBDRIZZLE);
  614. test_skip(true, has_drizzled());
  615. return TEST_SUCCESS;
  616. }
  617. test_st drizzled_tests[] ={
  618. {"drizzled startup-shutdown", 0, drizzled_cycle_test },
  619. {0, 0, 0}
  620. };
  621. test_st gearmand_tests[] ={
  622. #if 0
  623. {"pause", 0, pause_test },
  624. #endif
  625. {"gearmand startup-shutdown", 0, gearmand_cycle_test },
  626. {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test },
  627. {0, 0, 0}
  628. };
  629. static test_return_t check_for_libmemcached(void* object)
  630. {
  631. test_skip(true, HAVE_LIBMEMCACHED);
  632. test_skip(true, has_memcached());
  633. server_startup_st *servers= (server_startup_st*)object;
  634. test_true(servers);
  635. servers->clear();
  636. testing_service= "memcached";
  637. return TEST_SUCCESS;
  638. }
  639. test_st memcached_TESTS[] ={
  640. {"memcached startup-shutdown", 0, server_startup_TEST },
  641. {"memcached(socket file) startup-shutdown", 0, socket_server_startup_TEST },
  642. {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test },
  643. {0, 0, 0}
  644. };
  645. test_st test_skip_TESTS[] ={
  646. {"true, true", 0, test_skip_true_TEST },
  647. {"true, false", 0, test_skip_false_TEST },
  648. {0, 0, 0}
  649. };
  650. test_st environment_tests[] ={
  651. {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test },
  652. {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test },
  653. {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test },
  654. {"GDB_COMMAND", 0, GDB_COMMAND_test },
  655. {0, 0, 0}
  656. };
  657. test_st tests_log[] ={
  658. {"TEST_SUCCESS", false, test_success_test },
  659. {"TEST_FAILURE", false, test_failure_test },
  660. {"TEST_SUCCESS == 0", false, test_success_equals_one_test },
  661. {0, 0, 0}
  662. };
  663. test_st local_log[] ={
  664. {"test_is_local()", 0, local_test },
  665. {"test_is_local(NOT)", 0, local_not_test },
  666. {0, 0, 0}
  667. };
  668. test_st directories_tests[] ={
  669. {"var exists", 0, var_exists_test },
  670. {"var/tmp exists", 0, var_tmp_exists_test },
  671. {"var/run exists", 0, var_run_exists_test },
  672. {"var/log exists", 0, var_log_exists_test },
  673. {"var/drizzle exists", 0, var_drizzle_exists_test },
  674. {"var/tmp", 0, var_tmp_test },
  675. {"var/run", 0, var_run_test },
  676. {"var/log", 0, var_log_test },
  677. {"var/drizzle", 0, var_drizzle_test },
  678. {"var/tmp rm", 0, var_tmp_rm_test },
  679. {"var/run rm", 0, var_run_rm_test },
  680. {"var/log rm", 0, var_log_rm_test },
  681. {"var/drizzle rm", 0, var_drizzle_rm_test },
  682. {0, 0, 0}
  683. };
  684. test_st comparison_tests[] ={
  685. {"_compare(test_return_t)", 0, _compare_test_return_t_test },
  686. {0, 0, 0}
  687. };
  688. test_st cmdline_tests[] ={
  689. {"true", 0, true_BINARY },
  690. {"true --fubar", 0, true_fubar_BINARY },
  691. {"echo fubar", 0, echo_fubar_BINARY },
  692. {"wait --quiet", 0, wait_BINARY },
  693. {"wait --quiet --help", 0, wait_help_BINARY },
  694. {"wait --quiet --version", 0, wait_version_BINARY },
  695. {"wait --quiet /etc/services", 0, wait_services_BINARY },
  696. {"wait /etc/services", 0, wait_services_BINARY2 },
  697. {"wait /etc/services", 0, wait_services_appliction_TEST },
  698. {"gdb wait /etc/services", 0, gdb_wait_services_appliction_TEST },
  699. {"gdb abort", 0, gdb_abort_services_appliction_TEST },
  700. {0, 0, 0}
  701. };
  702. test_st get_free_port_TESTS[] ={
  703. {"get_free_port()", 0, get_free_port_TEST },
  704. {"default_port()", 0, default_port_TEST },
  705. {0, 0, 0}
  706. };
  707. test_st fatal_message_TESTS[] ={
  708. {"libtest::fatal", 0, fatal_TEST },
  709. {"fatal_message()", 0, fatal_message_TEST },
  710. {0, 0, 0}
  711. };
  712. test_st number_of_cpus_TESTS[] ={
  713. {"libtest::number_of_cpus()", 0, number_of_cpus_TEST },
  714. {0, 0, 0}
  715. };
  716. test_st create_tmpfile_TESTS[] ={
  717. {"libtest::create_tmpfile()", 0, create_tmpfile_TEST },
  718. {0, 0, 0}
  719. };
  720. test_st timer_TESTS[] ={
  721. {"libtest::Timer", 0, Timer_TEST },
  722. {0, 0, 0}
  723. };
  724. test_st dns_TESTS[] ={
  725. {"libtest::lookup(true)", 0, lookup_true_TEST },
  726. {"libtest::lookup(false)", 0, lookup_false_TEST },
  727. {"libtest::check_dns()", 0, check_dns_TEST },
  728. {0, 0, 0}
  729. };
  730. test_st application_tests[] ={
  731. {"vchar_t", 0, vchar_t_TEST },
  732. {"vchar_t compare()", 0, vchar_t_compare_neg_TEST },
  733. {"true", 0, application_true_BINARY },
  734. {"gbd true --fubar", 0, application_gdb_true_BINARY },
  735. {"gbd true", 0, application_gdb_true_BINARY2 },
  736. {"true --fubar", 0, application_true_fubar_BINARY },
  737. {"doesnotexist --fubar", 0, application_doesnotexist_BINARY },
  738. {"true --fubar=doh", 0, application_true_fubar_eq_doh_BINARY },
  739. {"true --fubar=doh add_option()", 0, application_true_fubar_eq_doh_option_BINARY },
  740. {"echo fubar", 0, application_echo_fubar_BINARY },
  741. {"echo fubar (as option)", 0, application_echo_fubar_BINARY2 },
  742. {0, 0, 0}
  743. };
  744. static test_return_t check_for_curl(void *)
  745. {
  746. test_skip(true, HAVE_LIBCURL);
  747. return TEST_SUCCESS;
  748. }
  749. static test_return_t disable_fatal_exception(void *)
  750. {
  751. fatal::disable();
  752. return TEST_SUCCESS;
  753. }
  754. static test_return_t enable_fatal_exception(void *)
  755. {
  756. fatal::disable();
  757. return TEST_SUCCESS;
  758. }
  759. test_st http_tests[] ={
  760. {"GET", 0, GET_TEST },
  761. {"POST", 0, POST_TEST },
  762. {"TRACE", 0, TRACE_TEST },
  763. {0, 0, 0}
  764. };
  765. collection_st collection[] ={
  766. {"environment", 0, 0, environment_tests},
  767. {"return values", 0, 0, tests_log},
  768. {"test_skip()", 0, 0, test_skip_TESTS },
  769. {"local", 0, 0, local_log},
  770. {"directories", 0, 0, directories_tests},
  771. {"comparison", 0, 0, comparison_tests},
  772. {"gearmand", check_for_gearman, 0, gearmand_tests},
  773. {"memcached", check_for_libmemcached, 0, memcached_TESTS },
  774. {"drizzled", check_for_drizzle, 0, drizzled_tests},
  775. {"cmdline", 0, 0, cmdline_tests},
  776. {"application", 0, 0, application_tests},
  777. {"http", check_for_curl, 0, http_tests},
  778. {"http", check_for_curl, 0, http_tests},
  779. {"get_free_port()", 0, 0, get_free_port_TESTS },
  780. {"fatal", disable_fatal_exception, enable_fatal_exception, fatal_message_TESTS },
  781. {"number_of_cpus()", 0, 0, number_of_cpus_TESTS },
  782. {"create_tmpfile()", 0, 0, create_tmpfile_TESTS },
  783. {"dns", 0, 0, dns_TESTS },
  784. {"libtest::Timer", 0, 0, timer_TESTS },
  785. {0, 0, 0, 0}
  786. };
  787. static void *world_create(server_startup_st& servers, test_return_t&)
  788. {
  789. return &servers;
  790. }
  791. void get_world(libtest::Framework *world)
  792. {
  793. world->collections(collection);
  794. world->create(world_create);
  795. }