unittest.cc 29 KB

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