unittest.cc 27 KB

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