unittest.cc 27 KB

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