unittest.cc 24 KB

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