unittest.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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 "libtest/yatlcon.h"
  37. #include <libtest/yatl.h>
  38. #if defined(HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H) && HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H
  39. # include <libmemcached-1.0/types/return.h>
  40. #endif
  41. #if defined(HAVE_LIBGEARMAN_1_0_RETURN_H) && HAVE_LIBGEARMAN_1_0_RETURN_H
  42. # include <libgearman-1.0/return.h>
  43. #endif
  44. #include <cstdlib>
  45. #include <unistd.h>
  46. #if defined(__linux__)
  47. # define TRUE_CMD "/bin/true"
  48. #else
  49. # define TRUE_CMD "/usr/bin/true"
  50. #endif
  51. using namespace libtest;
  52. static std::string testing_service;
  53. // Used to track setups where we see if failure is happening
  54. static uint32_t fatal_calls= 0;
  55. static test_return_t getenv_TEST(void *)
  56. {
  57. #if 0
  58. for (char **ptr= environ; *ptr; ptr++)
  59. {
  60. Error << *ptr;
  61. }
  62. #endif
  63. return TEST_SUCCESS;
  64. }
  65. static test_return_t LIBTOOL_COMMAND_test(void *)
  66. {
  67. ASSERT_TRUE(getenv("LIBTOOL_COMMAND"));
  68. return TEST_SUCCESS;
  69. }
  70. static test_return_t VALGRIND_COMMAND_test(void *)
  71. {
  72. ASSERT_TRUE(getenv("VALGRIND_COMMAND"));
  73. return TEST_SUCCESS;
  74. }
  75. static test_return_t VALGRIND_CHECK_TEST(void *)
  76. {
  77. SKIP_IF(bool(getenv("VALGRIND_COMMAND")) == false);
  78. SKIP_IF(bool(getenv("LOG_COMPILER")) == false);
  79. if (getenv("LOG_COMPILER") && strstr(getenv("LOG_COMPILER"), "valgrind"))
  80. {
  81. ASSERT_TRUE(valgrind_is_caller());
  82. }
  83. return TEST_SUCCESS;
  84. }
  85. static test_return_t HELGRIND_COMMAND_test(void *)
  86. {
  87. ASSERT_TRUE(getenv("HELGRIND_COMMAND"));
  88. return TEST_SUCCESS;
  89. }
  90. static test_return_t GDB_COMMAND_test(void *)
  91. {
  92. ASSERT_TRUE(getenv("GDB_COMMAND"));
  93. return TEST_SUCCESS;
  94. }
  95. static test_return_t test_success_equals_one_test(void *)
  96. {
  97. test_skip(HAVE_LIBMEMCACHED, 1);
  98. #if defined(HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H) && HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H
  99. test_zero(MEMCACHED_SUCCESS);
  100. #endif
  101. return TEST_SUCCESS;
  102. }
  103. static test_return_t test_success_test(void *)
  104. {
  105. return TEST_SUCCESS;
  106. }
  107. #pragma GCC diagnostic push
  108. #pragma GCC diagnostic ignored "-Wunreachable-code"
  109. static test_return_t test_throw_success_TEST(void *)
  110. {
  111. try {
  112. _SUCCESS;
  113. }
  114. catch (const libtest::__success&)
  115. {
  116. return TEST_SUCCESS;
  117. }
  118. catch (...)
  119. {
  120. return TEST_FAILURE;
  121. }
  122. return TEST_FAILURE;
  123. }
  124. #pragma GCC diagnostic pop
  125. static test_return_t test_throw_skip_macro_TEST(void *)
  126. {
  127. try {
  128. SKIP_IF(true);
  129. }
  130. catch (const libtest::__skipped&)
  131. {
  132. return TEST_SUCCESS;
  133. }
  134. catch (...)
  135. {
  136. FAIL("SLIP_IF() failed to throw libtest::_skipped");
  137. }
  138. FAIL("SLIP_IF() failed to throw");
  139. return TEST_FAILURE;
  140. }
  141. static test_return_t test_throw_skip_unless_macro_TEST(void *)
  142. {
  143. try {
  144. SKIP_UNLESS(false);
  145. }
  146. catch (const libtest::__skipped&)
  147. {
  148. return TEST_SUCCESS;
  149. }
  150. catch (...)
  151. {
  152. FAIL("SLIP_UNLESS() failed to throw libtest::_skipped");
  153. }
  154. FAIL("SLIP_UNLESS() failed to throw");
  155. return TEST_FAILURE;
  156. }
  157. static test_return_t test_throw_skip_TEST(void *)
  158. {
  159. try {
  160. throw libtest::__skipped(LIBYATL_DEFAULT_PARAM, "basic test");
  161. }
  162. catch (const libtest::__skipped&)
  163. {
  164. return TEST_SUCCESS;
  165. }
  166. catch (...)
  167. {
  168. FAIL("SLIP_IF() failed to throw libtest::_skipped");
  169. }
  170. FAIL("SLIP_IF() failed to throw");
  171. return TEST_FAILURE;
  172. }
  173. static test_return_t test_throw_fail_TEST(void *)
  174. {
  175. try {
  176. FAIL("test message!");
  177. }
  178. catch (const libtest::__failure& e)
  179. {
  180. std::string compare_message("test message!");
  181. test_zero(compare_message.compare(e.what()));
  182. return TEST_SUCCESS;
  183. }
  184. catch (...)
  185. {
  186. return TEST_FAILURE;
  187. }
  188. return TEST_FAILURE;
  189. }
  190. #pragma GCC diagnostic push
  191. #pragma GCC diagnostic ignored "-Wstack-protector"
  192. #ifdef __clang__
  193. # pragma GCC diagnostic ignored "-Wformat-security"
  194. #endif
  195. static test_return_t ASSERT_FALSE__TEST(void *)
  196. {
  197. try {
  198. ASSERT_FALSE(true);
  199. }
  200. catch (const libtest::__failure& e)
  201. {
  202. ASSERT_STREQ(e.what(), "Assertion '!true'");
  203. return TEST_SUCCESS;
  204. }
  205. catch (...)
  206. {
  207. return TEST_FAILURE;
  208. }
  209. return TEST_FAILURE;
  210. }
  211. #pragma GCC diagnostic pop
  212. static test_return_t ASSERT_NOT_NULL_FAIL_TEST(void *)
  213. {
  214. const char *valid_ptr= NULL;
  215. try {
  216. ASSERT_NOT_NULL(valid_ptr);
  217. }
  218. catch (const libtest::__failure& e)
  219. {
  220. return TEST_SUCCESS;
  221. }
  222. catch (...)
  223. {
  224. return TEST_FAILURE;
  225. }
  226. return TEST_FAILURE;
  227. }
  228. static test_return_t ASSERT_NEQ_FAIL_TEST(void *)
  229. {
  230. try {
  231. ASSERT_NEQ(1,1);
  232. }
  233. catch (const libtest::__failure& e)
  234. {
  235. ASSERT_STREQ(e.what(), "Assertion '1' == '1'");
  236. return TEST_SUCCESS;
  237. }
  238. catch (...)
  239. {
  240. return TEST_FAILURE;
  241. }
  242. return TEST_FAILURE;
  243. }
  244. static test_return_t ASSERT_NEQ_TEST(void *)
  245. {
  246. ASSERT_NEQ(1,0);
  247. return TEST_SUCCESS;
  248. }
  249. static test_return_t ASSERT_FALSE_TEST(void *)
  250. {
  251. try {
  252. FAIL(__func__);
  253. }
  254. catch (const libtest::__failure& e)
  255. {
  256. ASSERT_STREQ(e.what(), __func__);
  257. return TEST_SUCCESS;
  258. }
  259. catch (...)
  260. {
  261. return TEST_FAILURE;
  262. }
  263. return TEST_FAILURE;
  264. }
  265. static test_return_t test_failure_test(void *)
  266. {
  267. try {
  268. ASSERT_EQ(1, 2);
  269. }
  270. catch (...)
  271. {
  272. return TEST_SUCCESS;
  273. }
  274. return TEST_FAILURE;
  275. }
  276. static test_return_t local_test(void *)
  277. {
  278. if (getenv("LIBTEST_LOCAL"))
  279. {
  280. ASSERT_TRUE(test_is_local());
  281. }
  282. else
  283. {
  284. test_false(test_is_local());
  285. }
  286. return TEST_SUCCESS;
  287. }
  288. static test_return_t local_not_test(void *)
  289. {
  290. return TEST_SKIPPED;
  291. #if 0
  292. std::string temp;
  293. const char *ptr;
  294. if ((ptr= getenv("LIBTEST_LOCAL")) == NULL)
  295. {
  296. temp.append(ptr);
  297. }
  298. // unsetenv() will cause issues with valgrind
  299. _compare(__FILE__, __LINE__, __func__, 0, unsetenv("LIBTEST_LOCAL"), true);
  300. ASSERT_EQ(0, unsetenv("LIBTEST_LOCAL"));
  301. test_false(test_is_local());
  302. ASSERT_EQ(0, setenv("LIBTEST_LOCAL", "1", 1));
  303. ASSERT_TRUE(test_is_local());
  304. if (temp.empty())
  305. {
  306. ASSERT_EQ(0, unsetenv("LIBTEST_LOCAL"));
  307. }
  308. else
  309. {
  310. char *old_string= strdup(temp.c_str());
  311. ASSERT_EQ(0, setenv("LIBTEST_LOCAL", old_string, 1));
  312. }
  313. return TEST_SUCCESS;
  314. #endif
  315. }
  316. static test_return_t var_exists_test(void *)
  317. {
  318. ASSERT_EQ(0, access("var", R_OK | W_OK | X_OK));
  319. return TEST_SUCCESS;
  320. }
  321. static test_return_t var_tmp_exists_test(void *)
  322. {
  323. ASSERT_EQ(0, access("var/tmp", R_OK | W_OK | X_OK));
  324. return TEST_SUCCESS;
  325. }
  326. static test_return_t var_run_exists_test(void *)
  327. {
  328. ASSERT_EQ(0, access("var/run", R_OK | W_OK | X_OK));
  329. return TEST_SUCCESS;
  330. }
  331. static test_return_t var_log_exists_test(void *)
  332. {
  333. ASSERT_EQ(0, access("var/log", R_OK | W_OK | X_OK));
  334. return TEST_SUCCESS;
  335. }
  336. static test_return_t var_drizzle_exists_test(void *)
  337. {
  338. ASSERT_EQ(0, access("var/drizzle", R_OK | W_OK | X_OK));
  339. return TEST_SUCCESS;
  340. }
  341. static test_return_t var_tmp_test(void *)
  342. {
  343. FILE *file= fopen("var/tmp/junk", "w+");
  344. ASSERT_TRUE(file);
  345. fclose(file);
  346. return TEST_SUCCESS;
  347. }
  348. static test_return_t var_run_test(void *)
  349. {
  350. FILE *file= fopen("var/run/junk", "w+");
  351. ASSERT_TRUE(file);
  352. fclose(file);
  353. return TEST_SUCCESS;
  354. }
  355. static test_return_t var_log_test(void *)
  356. {
  357. FILE *file= fopen("var/log/junk", "w+");
  358. ASSERT_TRUE(file);
  359. fclose(file);
  360. return TEST_SUCCESS;
  361. }
  362. static test_return_t var_drizzle_test(void *)
  363. {
  364. FILE *file= fopen("var/drizzle/junk", "w+");
  365. ASSERT_TRUE(file);
  366. fclose(file);
  367. return TEST_SUCCESS;
  368. }
  369. static test_return_t var_tmp_rm_test(void *)
  370. {
  371. ASSERT_TRUE(unlink("var/tmp/junk") == 0);
  372. return TEST_SUCCESS;
  373. }
  374. static test_return_t var_run_rm_test(void *)
  375. {
  376. ASSERT_TRUE(unlink("var/run/junk") == 0);
  377. return TEST_SUCCESS;
  378. }
  379. static test_return_t var_log_rm_test(void *)
  380. {
  381. ASSERT_TRUE(unlink("var/log/junk") == 0);
  382. return TEST_SUCCESS;
  383. }
  384. static test_return_t var_drizzle_rm_test(void *)
  385. {
  386. ASSERT_TRUE(unlink("var/drizzle/junk") == 0);
  387. return TEST_SUCCESS;
  388. }
  389. static test_return_t _compare_test_return_t_test(void *)
  390. {
  391. ASSERT_EQ(TEST_SUCCESS, TEST_SUCCESS);
  392. return TEST_SUCCESS;
  393. }
  394. static test_return_t _compare_memcached_return_t_test(void *)
  395. {
  396. test_skip(HAVE_LIBMEMCACHED, true);
  397. #if defined(HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H) && HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H
  398. ASSERT_EQ(MEMCACHED_SUCCESS, MEMCACHED_SUCCESS);
  399. #endif
  400. return TEST_SUCCESS;
  401. }
  402. static test_return_t _compare_gearman_return_t_test(void *)
  403. {
  404. test_skip(HAVE_LIBGEARMAN, true);
  405. #if defined(HAVE_LIBGEARMAN_1_0_RETURN_H) && HAVE_LIBGEARMAN_1_0_RETURN_H
  406. ASSERT_EQ(GEARMAN_SUCCESS, GEARMAN_SUCCESS);
  407. #endif
  408. return TEST_SUCCESS;
  409. }
  410. static test_return_t drizzled_cycle_test(void *object)
  411. {
  412. server_startup_st *servers= (server_startup_st*)object;
  413. ASSERT_TRUE(servers and servers->validate());
  414. #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
  415. ASSERT_TRUE(has_drizzled());
  416. #endif
  417. test_skip(true, has_drizzled());
  418. test_skip(true, server_startup(*servers, "drizzled", get_free_port(), NULL));
  419. return TEST_SUCCESS;
  420. }
  421. static test_return_t gearmand_cycle_test(void *object)
  422. {
  423. server_startup_st *servers= (server_startup_st*)object;
  424. ASSERT_TRUE(servers and servers->validate());
  425. test_skip(true, has_gearmand());
  426. test_skip(true, server_startup(*servers, "gearmand", get_free_port(), NULL));
  427. servers->clear();
  428. return TEST_SUCCESS;
  429. }
  430. static test_return_t skip_shim(bool a, bool b)
  431. {
  432. test_skip(a, b);
  433. return TEST_SUCCESS;
  434. }
  435. static test_return_t test_skip_true_TEST(void*)
  436. {
  437. ASSERT_EQ(true, true);
  438. ASSERT_EQ(false, false);
  439. ASSERT_EQ(TEST_SUCCESS, skip_shim(true, true));
  440. ASSERT_EQ(TEST_SUCCESS, skip_shim(false, false));
  441. return TEST_SUCCESS;
  442. }
  443. static test_return_t test_skip_false_TEST(void*)
  444. {
  445. ASSERT_EQ(TEST_SKIPPED, skip_shim(true, false));
  446. ASSERT_EQ(TEST_SKIPPED, skip_shim(false, true));
  447. return TEST_SUCCESS;
  448. }
  449. static test_return_t server_startup_fail_TEST(void *object)
  450. {
  451. server_startup_st *servers= (server_startup_st*)object;
  452. ASSERT_TRUE(servers);
  453. fatal::disable();
  454. ASSERT_EQ(servers->start_server(testing_service, LIBTEST_FAIL_PORT, NULL), true);
  455. fatal::enable();
  456. return TEST_SUCCESS;
  457. }
  458. static test_return_t server_startup_TEST(void *object)
  459. {
  460. server_startup_st *servers= (server_startup_st*)object;
  461. ASSERT_TRUE(servers);
  462. ASSERT_EQ(servers->start_server(testing_service, get_free_port(), NULL), true);
  463. ASSERT_TRUE(servers->last());
  464. pid_t last_pid= servers->last()->pid();
  465. ASSERT_EQ(servers->last()->pid(), last_pid);
  466. ASSERT_TRUE(last_pid > 1);
  467. ASSERT_EQ(kill(last_pid, 0), 0);
  468. ASSERT_TRUE(servers->shutdown());
  469. #if 0
  470. ASSERT_EQ(servers->last()->pid(), -1);
  471. ASSERT_EQ(kill(last_pid, 0), -1);
  472. #endif
  473. return TEST_SUCCESS;
  474. }
  475. static test_return_t socket_server_startup_TEST(void *object)
  476. {
  477. server_startup_st *servers= (server_startup_st*)object;
  478. ASSERT_TRUE(servers);
  479. ASSERT_TRUE(servers->start_socket_server(testing_service, get_free_port(), NULL));
  480. return TEST_SUCCESS;
  481. }
  482. #if 0
  483. static test_return_t memcached_sasl_test(void *object)
  484. {
  485. server_startup_st *servers= (server_startup_st*)object;
  486. ASSERT_TRUE(servers);
  487. test_skip(false, bool(getenv("LOG_COMPILER")));
  488. if (MEMCACHED_SASL_BINARY)
  489. {
  490. if (HAVE_LIBMEMCACHED)
  491. {
  492. ASSERT_TRUE(has_memcached_sasl());
  493. ASSERT_TRUE(server_startup(*servers, "memcached-sasl", get_free_port(), NULL));
  494. return TEST_SUCCESS;
  495. }
  496. }
  497. return TEST_SKIPPED;
  498. }
  499. #endif
  500. static test_return_t application_true_BINARY(void *)
  501. {
  502. test_skip(0, access(TRUE_CMD, X_OK ));
  503. Application true_app(TRUE_CMD);
  504. ASSERT_EQ(Application::SUCCESS, true_app.run());
  505. ASSERT_EQ(Application::SUCCESS, true_app.join());
  506. return TEST_SUCCESS;
  507. }
  508. static test_return_t application_gdb_true_BINARY2(void *)
  509. {
  510. test_skip(0, access("/usr/bin/gdb", X_OK ));
  511. test_skip(0, access(TRUE_CMD, X_OK ));
  512. Application true_app(TRUE_CMD);
  513. true_app.use_gdb(true);
  514. ASSERT_EQ(Application::SUCCESS, true_app.run());
  515. ASSERT_EQ(Application::SUCCESS, true_app.join());
  516. return TEST_SUCCESS;
  517. }
  518. static test_return_t application_gdb_true_BINARY(void *)
  519. {
  520. test_skip(0, access("/usr/bin/gdb", X_OK ));
  521. test_skip(0, access(TRUE_CMD, X_OK ));
  522. Application true_app(TRUE_CMD);
  523. true_app.use_gdb(true);
  524. const char *args[]= { "--fubar", 0 };
  525. ASSERT_EQ(Application::SUCCESS, true_app.run(args));
  526. ASSERT_EQ(Application::SUCCESS, true_app.join());
  527. return TEST_SUCCESS;
  528. }
  529. static test_return_t application_true_fubar_BINARY(void *)
  530. {
  531. test_skip(0, access(TRUE_CMD, X_OK ));
  532. Application true_app(TRUE_CMD);
  533. const char *args[]= { "--fubar", 0 };
  534. ASSERT_EQ(Application::SUCCESS, true_app.run(args));
  535. ASSERT_EQ(Application::SUCCESS, true_app.join());
  536. test_zero(true_app.stdout_result().size());
  537. return TEST_SUCCESS;
  538. }
  539. static test_return_t application_doesnotexist_BINARY(void *)
  540. {
  541. test_skip_valgrind();
  542. Application true_app("doesnotexist");
  543. true_app.will_fail();
  544. const char *args[]= { "--fubar", 0 };
  545. #if ((defined(__APPLE__) && __APPLE__) || (defined(__FreeBSD__) && __FreeBSD__))
  546. ASSERT_EQ(Application::INVALID_POSIX_SPAWN, true_app.run(args));
  547. #else
  548. /* true_app.run(args) returns INVALID_POSIX_SPAWN here on Ubuntu 18.04, but
  549. it returns SUCCESS on older Linux distributions such as Ubuntu 14.04 and
  550. 16.04, where one needs to follow up with true_app.join() to get
  551. INVALID_POSIX_SPAWN. */
  552. Application::error_t return_value = true_app.run(args);
  553. if (return_value == Application::INVALID_POSIX_SPAWN)
  554. {
  555. ASSERT_EQ(Application::INVALID_POSIX_SPAWN, return_value);
  556. }
  557. else
  558. {
  559. ASSERT_EQ(Application::SUCCESS, return_value);
  560. ASSERT_EQ(Application::INVALID_POSIX_SPAWN, true_app.join());
  561. }
  562. #endif
  563. test_zero(true_app.stdout_result().size());
  564. return TEST_SUCCESS;
  565. }
  566. static test_return_t GET_TEST(void *)
  567. {
  568. libtest::http::GET get("http://foo.example.com/");
  569. ASSERT_EQ(false, get.execute());
  570. return TEST_SUCCESS;
  571. }
  572. static test_return_t POST_TEST(void *)
  573. {
  574. libtest::vchar_t body;
  575. libtest::http::POST post("http://foo.example.com/", body);
  576. ASSERT_EQ(false, post.execute());
  577. return TEST_SUCCESS;
  578. }
  579. static test_return_t TRACE_TEST(void *)
  580. {
  581. libtest::vchar_t body;
  582. libtest::http::TRACE trace("http://foo.example.com/", body);
  583. ASSERT_EQ(false, trace.execute());
  584. return TEST_SUCCESS;
  585. }
  586. static test_return_t vchar_t_TEST(void *)
  587. {
  588. libtest::vchar_t response;
  589. libtest::make_vector(response, test_literal_param("fubar\n"));
  590. ASSERT_EQ(response, response);
  591. return TEST_SUCCESS;
  592. }
  593. static test_return_t vchar_t_make_append_TEST(void *)
  594. {
  595. libtest::vchar_t hostname;
  596. libtest::vchar::make(hostname, 23);
  597. libtest::vchar::append(hostname, ".com");
  598. ASSERT_EQ(28, hostname.size());
  599. ASSERT_EQ(0, hostname[27]);
  600. return TEST_SUCCESS;
  601. }
  602. static test_return_t vchar_t_compare_neg_TEST(void *)
  603. {
  604. libtest::vchar_t response;
  605. libtest::vchar_t response2;
  606. libtest::make_vector(response, test_literal_param("fubar\n"));
  607. libtest::make_vector(response2, test_literal_param(__func__));
  608. ASSERT_TRUE(response != response2);
  609. return TEST_SUCCESS;
  610. }
  611. static test_return_t application_echo_fubar_BINARY(void *)
  612. {
  613. if (0)
  614. {
  615. test_skip(0, access("/bin/echo", X_OK ));
  616. Application true_app("/bin/echo");
  617. const char *args[]= { "fubar", 0 };
  618. ASSERT_EQ(Application::SUCCESS, true_app.run(args));
  619. while (true_app.slurp() == false) {} ;
  620. libtest::vchar_t response;
  621. make_vector(response, test_literal_param("fubar\n"));
  622. ASSERT_EQ(response, true_app.stdout_result());
  623. }
  624. return TEST_SUCCESS;
  625. }
  626. static test_return_t application_echo_fubar_BINARY2(void *)
  627. {
  628. if (0)
  629. {
  630. test_skip(0, access("/bin/echo", X_OK ));
  631. Application true_app("/bin/echo");
  632. true_app.add_option("fubar");
  633. ASSERT_EQ(Application::SUCCESS, true_app.run());
  634. ASSERT_EQ(Application::SUCCESS, true_app.join());
  635. libtest::vchar_t response;
  636. make_vector(response, test_literal_param("fubar\n"));
  637. ASSERT_EQ(response, true_app.stdout_result());
  638. }
  639. return TEST_SUCCESS;
  640. }
  641. static test_return_t echo_fubar_BINARY(void *)
  642. {
  643. const char *args[]= { "fubar", 0 };
  644. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("/bin/echo", args));
  645. return TEST_SUCCESS;
  646. }
  647. static test_return_t core_count_BINARY(void *)
  648. {
  649. const char *args[]= { 0 };
  650. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("libtest/core-count", args, true));
  651. return TEST_SUCCESS;
  652. }
  653. static test_return_t wait_BINARY(void *)
  654. {
  655. const char *args[]= { "--quiet", 0 };
  656. ASSERT_EQ(EXIT_FAILURE, exec_cmdline("libtest/wait", args, true));
  657. return TEST_SUCCESS;
  658. }
  659. static test_return_t wait_help_BINARY(void *)
  660. {
  661. const char *args[]= { "--quiet", "--help", 0 };
  662. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
  663. return TEST_SUCCESS;
  664. }
  665. static test_return_t wait_version_BINARY(void *)
  666. {
  667. const char *args[]= { "--quiet", "--version", 0 };
  668. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
  669. return TEST_SUCCESS;
  670. }
  671. static test_return_t wait_services_BINARY(void *)
  672. {
  673. test_skip(0, access("/etc/services", R_OK ));
  674. const char *args[]= { "--quiet", "/etc/services", 0 };
  675. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
  676. return TEST_SUCCESS;
  677. }
  678. static test_return_t wait_services_BINARY2(void *)
  679. {
  680. test_skip(0, access("/etc/services", R_OK ));
  681. const char *args[]= { "/etc/services", 0 };
  682. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
  683. return TEST_SUCCESS;
  684. }
  685. static test_return_t wait_services_appliction_TEST(void *)
  686. {
  687. test_skip(0, access("/etc/services", R_OK ));
  688. test_skip(0, access("/usr/bin/gdb", X_OK ));
  689. test_skip(0, access("libtest/wait", X_OK ));
  690. libtest::Application wait_app("libtest/wait", true);
  691. wait_app.use_gdb(true);
  692. const char *args[]= { "/etc/services", 0 };
  693. ASSERT_EQ(Application::SUCCESS, wait_app.run(args));
  694. ASSERT_EQ(Application::SUCCESS, wait_app.join());
  695. return TEST_SUCCESS;
  696. }
  697. static test_return_t gdb_wait_services_appliction_TEST(void *)
  698. {
  699. test_skip(true, false);
  700. #if defined(__APPLE__) && __APPLE__
  701. test_skip(0, __APPLE__);
  702. #endif
  703. test_skip(0, access("/etc/services", R_OK ));
  704. test_skip(0, access("/usr/bin/gdb", X_OK ));
  705. test_skip(0, access("libtest/wait", X_OK ));
  706. libtest::Application wait_app("libtest/wait", true);
  707. wait_app.use_gdb(true);
  708. const char *args[]= { "/etc/services", 0 };
  709. ASSERT_EQ(Application::SUCCESS, wait_app.run(args));
  710. ASSERT_EQ(Application::SUCCESS, wait_app.join());
  711. return TEST_SUCCESS;
  712. }
  713. static test_return_t gdb_abort_services_appliction_TEST(void *)
  714. {
  715. test_skip(0, access("/usr/bin/gdb", X_OK ));
  716. test_skip(0, access("libtest/abort", X_OK ));
  717. test_skip(true, false);
  718. #if defined(__APPLE__) && __APPLE__
  719. test_skip(0, __APPLE__);
  720. #endif
  721. libtest::Application abort_app("libtest/abort", true);
  722. abort_app.use_gdb(true);
  723. ASSERT_EQ(Application::SUCCESS, abort_app.run());
  724. ASSERT_EQ(Application::SUCCESS, abort_app.join());
  725. std::string gdb_filename= abort_app.gdb_filename();
  726. test_skip(0, access(gdb_filename.c_str(), R_OK ));
  727. const char *args[]= { "SIGABRT", gdb_filename.c_str(), 0 };
  728. ASSERT_EQ(EXIT_SUCCESS, exec_cmdline("grep", args));
  729. // Sanity test
  730. args[0]= "THIS_WILL_NOT_BE_FOUND";
  731. ASSERT_EQ(EXIT_FAILURE, exec_cmdline("grep", args));
  732. return TEST_SUCCESS;
  733. }
  734. static test_return_t get_free_port_TEST(void *)
  735. {
  736. in_port_t ret_port;
  737. ASSERT_TRUE((ret_port= get_free_port()));
  738. ASSERT_TRUE(get_free_port() != default_port());
  739. ASSERT_TRUE(get_free_port() != get_free_port());
  740. return TEST_SUCCESS;
  741. }
  742. static test_return_t fatal_TEST(void *)
  743. {
  744. ASSERT_EQ(fatal_calls++, fatal::disabled_counter());
  745. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Testing va_args based fatal(): %d", 10);
  746. }
  747. static test_return_t number_of_cpus_TEST(void *)
  748. {
  749. ASSERT_TRUE(number_of_cpus() >= 1);
  750. return TEST_SUCCESS;
  751. }
  752. static test_return_t check_dns_TEST(void *)
  753. {
  754. test_warn(libtest::check_dns(), "Broken DNS server/no DNS server found");
  755. return TEST_SUCCESS;
  756. }
  757. static test_return_t Timer_TEST(void *)
  758. {
  759. int64_t minutes= random() % 50;
  760. minutes++;
  761. Timer check;
  762. check.reset();
  763. check.offset(minutes, 2, 200);
  764. ASSERT_EQ(check.minutes(), minutes);
  765. return TEST_SUCCESS;
  766. }
  767. static test_return_t lookup_true_TEST(void *)
  768. {
  769. test_warn(libtest::lookup("exist.gearman.info"), "dns is not currently working");
  770. return TEST_SUCCESS;
  771. }
  772. static test_return_t lookup_false_TEST(void *)
  773. {
  774. SKIP_IF_(libtest::lookup("does_not_exist.gearman.info"),
  775. "Broken DNS server detected");
  776. return TEST_SUCCESS;
  777. }
  778. static test_return_t create_tmpfile_TEST(void *)
  779. {
  780. test_skip(0, access("/usr/bin/touch", X_OK ));
  781. std::string tmp= create_tmpfile(__func__);
  782. ASSERT_EQ(-1, access(tmp.c_str(), R_OK));
  783. ASSERT_EQ(-1, access(tmp.c_str(), F_OK));
  784. Application touch_app("/usr/bin/touch");
  785. const char *args[]= { tmp.c_str(), 0 };
  786. ASSERT_EQ(Application::SUCCESS, touch_app.run(args));
  787. ASSERT_EQ(Application::SUCCESS, touch_app.join());
  788. ASSERT_EQ(0, access(tmp.c_str(), R_OK));
  789. ASSERT_EQ(0, unlink(tmp.c_str()));
  790. return TEST_SUCCESS;
  791. }
  792. static test_return_t fatal_message_TEST(void *)
  793. {
  794. ASSERT_EQ(fatal_calls++, fatal::disabled_counter());
  795. FATAL("Fatal test");
  796. return TEST_SUCCESS;
  797. }
  798. static test_return_t default_port_TEST(void *)
  799. {
  800. in_port_t ret_port= default_port();
  801. ASSERT_EQ(ret_port, libtest::default_port());
  802. ASSERT_EQ(ret_port, libtest::default_port());
  803. return TEST_SUCCESS;
  804. }
  805. static test_return_t check_for_VALGRIND(void *)
  806. {
  807. test_skip_valgrind();
  808. return TEST_SUCCESS;
  809. }
  810. #pragma GCC diagnostic push
  811. #pragma GCC diagnostic ignored "-Wunreachable-code"
  812. static test_return_t check_for_gearman(void *)
  813. {
  814. test_skip(true, HAVE_LIBGEARMAN);
  815. test_skip(true, has_gearmand());
  816. #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
  817. if (GEARMAND_BINARY)
  818. {
  819. if (strcmp(GEARMAND_BINARY, "./gearmand/gearmand"))
  820. {
  821. test_zero(access(GEARMAND_BINARY, X_OK ));
  822. }
  823. }
  824. else
  825. {
  826. return TEST_SKIPPED;
  827. }
  828. #endif
  829. testing_service= "gearmand";
  830. return TEST_SUCCESS;
  831. }
  832. #pragma GCC diagnostic pop
  833. static test_return_t check_for_drizzle(void *)
  834. {
  835. #if defined(HAVE_WOLFSSL) && HAVE_WOLFSSL
  836. SKIP_IF(HAVE_WOLFSSL);
  837. #endif
  838. test_skip(true, has_drizzled());
  839. testing_service= "drizzled";
  840. return TEST_SUCCESS;
  841. }
  842. test_st drizzled_tests[] ={
  843. {"drizzled startup-shutdown", 0, drizzled_cycle_test },
  844. {0, 0, 0}
  845. };
  846. test_st gearmand_tests[] ={
  847. #if 0
  848. {"pause", 0, pause_test },
  849. #endif
  850. {"gearmand startup-shutdown", 0, gearmand_cycle_test },
  851. {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test },
  852. {"server_startup(fail)", 0, server_startup_fail_TEST },
  853. {0, 0, 0}
  854. };
  855. static test_return_t clear_servers(void* object)
  856. {
  857. server_startup_st *servers= (server_startup_st*)object;
  858. ASSERT_TRUE(servers);
  859. servers->clear();
  860. testing_service.clear();
  861. return TEST_SUCCESS;
  862. }
  863. static test_return_t check_for_memcached(void* object)
  864. {
  865. #if defined(HAVE_WOLFSSL) && HAVE_WOLFSSL
  866. SKIP_IF(HAVE_WOLFSSL);
  867. #endif
  868. test_skip(true, has_memcached());
  869. server_startup_st *servers= (server_startup_st*)object;
  870. ASSERT_TRUE(servers);
  871. servers->clear();
  872. testing_service= "memcached";
  873. return TEST_SUCCESS;
  874. }
  875. test_st memcached_TESTS[] ={
  876. {"memcached startup-shutdown", 0, server_startup_TEST },
  877. {"memcached(socket file) startup-shutdown", 0, socket_server_startup_TEST },
  878. {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test },
  879. {"server_startup(fail)", 0, server_startup_fail_TEST },
  880. {0, 0, 0}
  881. };
  882. test_st test_skip_TESTS[] ={
  883. {"true, true", 0, test_skip_true_TEST },
  884. {"true, false", 0, test_skip_false_TEST },
  885. {0, 0, 0}
  886. };
  887. test_st environment_tests[] ={
  888. {"getenv()", 0, getenv_TEST },
  889. {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test },
  890. {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test },
  891. {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test },
  892. {"GDB_COMMAND", 0, GDB_COMMAND_test },
  893. {"valgrind_is_caller()", 0, VALGRIND_CHECK_TEST },
  894. {0, 0, 0}
  895. };
  896. test_st tests_log[] ={
  897. {"TEST_SUCCESS", false, test_success_test },
  898. {"TEST_FAILURE", false, test_failure_test },
  899. {"TEST_SUCCESS == 0", false, test_success_equals_one_test },
  900. {"SUCCESS", false, test_throw_success_TEST },
  901. {"libtest::__skipped", false, test_throw_skip_TEST },
  902. {"SKIP_IF", false, test_throw_skip_macro_TEST },
  903. {"SKIP_UNLESS", false, test_throw_skip_unless_macro_TEST },
  904. {"FAIL", false, test_throw_fail_TEST },
  905. {"ASSERT_FALSE_", false, ASSERT_FALSE__TEST },
  906. {"ASSERT_FALSE", false, ASSERT_FALSE_TEST },
  907. {"ASSERT_NEQ", false, ASSERT_NEQ_TEST },
  908. {"ASSERT_NEQ FAIL", false, ASSERT_NEQ_FAIL_TEST },
  909. {"ASSERT_NOT_NULL FAIL", false, ASSERT_NOT_NULL_FAIL_TEST },
  910. {0, 0, 0}
  911. };
  912. test_st local_log[] ={
  913. {"test_is_local()", 0, local_test },
  914. {"test_is_local(NOT)", 0, local_not_test },
  915. {0, 0, 0}
  916. };
  917. test_st directories_tests[] ={
  918. {"var exists", 0, var_exists_test },
  919. {"var/tmp exists", 0, var_tmp_exists_test },
  920. {"var/run exists", 0, var_run_exists_test },
  921. {"var/log exists", 0, var_log_exists_test },
  922. {"var/drizzle exists", 0, var_drizzle_exists_test },
  923. {"var/tmp", 0, var_tmp_test },
  924. {"var/run", 0, var_run_test },
  925. {"var/log", 0, var_log_test },
  926. {"var/drizzle", 0, var_drizzle_test },
  927. {"var/tmp rm", 0, var_tmp_rm_test },
  928. {"var/run rm", 0, var_run_rm_test },
  929. {"var/log rm", 0, var_log_rm_test },
  930. {"var/drizzle rm", 0, var_drizzle_rm_test },
  931. {0, 0, 0}
  932. };
  933. test_st comparison_tests[] ={
  934. {"_compare(test_return_t)", 0, _compare_test_return_t_test },
  935. {0, 0, 0}
  936. };
  937. test_st cmdline_tests[] ={
  938. {"echo fubar", 0, echo_fubar_BINARY },
  939. {"core-count", 0, core_count_BINARY },
  940. {"wait --quiet", 0, wait_BINARY },
  941. {"wait --quiet --help", 0, wait_help_BINARY },
  942. {"wait --quiet --version", 0, wait_version_BINARY },
  943. {"wait --quiet /etc/services", 0, wait_services_BINARY },
  944. {"wait /etc/services", 0, wait_services_BINARY2 },
  945. {"wait /etc/services", 0, wait_services_appliction_TEST },
  946. {"gdb wait /etc/services", 0, gdb_wait_services_appliction_TEST },
  947. {"gdb abort", 0, gdb_abort_services_appliction_TEST },
  948. {0, 0, 0}
  949. };
  950. test_st get_free_port_TESTS[] ={
  951. {"get_free_port()", 0, get_free_port_TEST },
  952. {"default_port()", 0, default_port_TEST },
  953. {0, 0, 0}
  954. };
  955. test_st fatal_message_TESTS[] ={
  956. {"libtest::fatal", 0, fatal_TEST },
  957. {"fatal_message()", 0, fatal_message_TEST },
  958. {0, 0, 0}
  959. };
  960. test_st number_of_cpus_TESTS[] ={
  961. {"libtest::number_of_cpus()", 0, number_of_cpus_TEST },
  962. {0, 0, 0}
  963. };
  964. test_st create_tmpfile_TESTS[] ={
  965. {"libtest::create_tmpfile()", 0, create_tmpfile_TEST },
  966. {0, 0, 0}
  967. };
  968. test_st timer_TESTS[] ={
  969. {"libtest::Timer", 0, Timer_TEST },
  970. {0, 0, 0}
  971. };
  972. test_st dns_TESTS[] ={
  973. {"libtest::lookup(true)", 0, lookup_true_TEST },
  974. {"libtest::lookup(false)", 0, lookup_false_TEST },
  975. {"libtest::check_dns()", 0, check_dns_TEST },
  976. {0, 0, 0}
  977. };
  978. test_st application_tests[] ={
  979. {"vchar_t", 0, vchar_t_TEST },
  980. {"vchar_t make() append()", 0, vchar_t_make_append_TEST },
  981. {"vchar_t compare()", 0, vchar_t_compare_neg_TEST },
  982. {"true", 0, application_true_BINARY },
  983. {"gbd true --fubar", 0, application_gdb_true_BINARY },
  984. {"gbd true", 0, application_gdb_true_BINARY2 },
  985. {"true --fubar", 0, application_true_fubar_BINARY },
  986. {"doesnotexist --fubar", 0, application_doesnotexist_BINARY },
  987. {"echo fubar", 0, application_echo_fubar_BINARY },
  988. {"echo fubar (as option)", 0, application_echo_fubar_BINARY2 },
  989. {0, 0, 0}
  990. };
  991. static test_return_t check_for_curl(void *)
  992. {
  993. test_skip_valgrind();
  994. test_skip(true, HAVE_LIBCURL);
  995. return TEST_SUCCESS;
  996. }
  997. static test_return_t disable_fatal_exception(void *)
  998. {
  999. fatal_calls= 0;
  1000. fatal::disable();
  1001. return TEST_SUCCESS;
  1002. }
  1003. static test_return_t enable_fatal_exception(void *)
  1004. {
  1005. fatal::enable();
  1006. return TEST_SUCCESS;
  1007. }
  1008. test_st http_tests[] ={
  1009. {"GET", 0, GET_TEST },
  1010. {"POST", 0, POST_TEST },
  1011. {"TRACE", 0, TRACE_TEST },
  1012. {0, 0, 0}
  1013. };
  1014. collection_st collection[] ={
  1015. {"environment", 0, 0, environment_tests},
  1016. {"return values", 0, 0, tests_log},
  1017. {"test_skip()", 0, 0, test_skip_TESTS },
  1018. {"local", 0, 0, local_log},
  1019. {"directories", 0, 0, directories_tests},
  1020. {"comparison", 0, 0, comparison_tests},
  1021. {"gearmand", check_for_gearman, clear_servers, gearmand_tests},
  1022. {"memcached", check_for_memcached, clear_servers, memcached_TESTS },
  1023. {"drizzled", check_for_drizzle, clear_servers, drizzled_tests},
  1024. {"cmdline", 0, 0, cmdline_tests},
  1025. {"application", 0, 0, application_tests},
  1026. {"http", check_for_curl, 0, http_tests},
  1027. {"http", check_for_curl, 0, http_tests},
  1028. {"get_free_port()", 0, 0, get_free_port_TESTS },
  1029. {"fatal", disable_fatal_exception, enable_fatal_exception, fatal_message_TESTS },
  1030. {"number_of_cpus()", 0, 0, number_of_cpus_TESTS },
  1031. {"create_tmpfile()", 0, 0, create_tmpfile_TESTS },
  1032. {"dns", check_for_VALGRIND, 0, dns_TESTS },
  1033. {"libtest::Timer", 0, 0, timer_TESTS },
  1034. {0, 0, 0, 0}
  1035. };
  1036. static void *world_create(server_startup_st& servers, test_return_t&)
  1037. {
  1038. return &servers;
  1039. }
  1040. void get_world(libtest::Framework *world)
  1041. {
  1042. world->collections(collection);
  1043. world->create(world_create);
  1044. }