hostile.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "gear_config.h"
  38. #include <libtest/test.hpp>
  39. using namespace libtest;
  40. #include <libgearman-1.0/gearman.h>
  41. #include <libhostile/hostile.h>
  42. #include "libgearman/client.hpp"
  43. using namespace org::gearmand;
  44. #include "tests/start_worker.h"
  45. #include "tests/workers/v2/echo_or_react.h"
  46. #include <cerrno>
  47. #include <cstdio>
  48. #include <cstdlib>
  49. #include <cstring>
  50. #include <unistd.h>
  51. #include <sys/time.h>
  52. #define WORKER_FUNCTION_NAME "foo"
  53. #ifndef SERVER_TARGET
  54. # define SERVER_TARGET "hostile-gearmand"
  55. #endif
  56. #pragma GCC diagnostic push
  57. #pragma GCC diagnostic ignored "-Wunreachable-code"
  58. static bool has_hostile()
  59. {
  60. #if defined(HAVE_LIBHOSTILE)
  61. if (HAVE_LIBHOSTILE)
  62. {
  63. return true;
  64. }
  65. #endif
  66. return false;
  67. }
  68. #pragma GCC diagnostic pop
  69. static in_port_t hostile_server_port= 0;
  70. static in_port_t& current_server_= hostile_server_port;
  71. static void reset_server()
  72. {
  73. current_server_= hostile_server_port;
  74. }
  75. static in_port_t current_server()
  76. {
  77. return current_server_;
  78. }
  79. static void set_server(in_port_t& arg)
  80. {
  81. current_server_= arg;
  82. }
  83. struct client_thread_context_st
  84. {
  85. size_t count;
  86. size_t payload_size;
  87. client_thread_context_st() :
  88. count(0),
  89. payload_size(0)
  90. { }
  91. void increment()
  92. {
  93. count++;
  94. }
  95. };
  96. extern "C" {
  97. static __attribute__((noreturn)) void *client_thread(void *object)
  98. {
  99. client_thread_context_st *success= (client_thread_context_st *)object;
  100. fatal_assert(success);
  101. fatal_assert(success->count == 0);
  102. {
  103. libgearman::Client client(current_server());
  104. libtest::vchar_t payload;
  105. payload.resize(success->payload_size);
  106. gearman_client_set_timeout(&client, 1000);
  107. for (size_t x= 0; x < 100; x++)
  108. {
  109. int oldstate;
  110. pthread_setcanceltype(PTHREAD_CANCEL_DISABLE, &oldstate);
  111. gearman_return_t rc;
  112. void *value= gearman_client_do(&client, WORKER_FUNCTION_NAME,
  113. NULL,
  114. payload.size() ? &payload[0] : NULL,
  115. payload.size() ? random() % payload.size() : 0,
  116. NULL, &rc);
  117. fatal_assert(gearman_client_has_tasks(&client) == false);
  118. if (gearman_success(rc))
  119. {
  120. success->increment();
  121. }
  122. if (value)
  123. {
  124. free(value);
  125. }
  126. pthread_setcanceltype(oldstate, NULL);
  127. }
  128. fatal_assert(gearman_client_has_tasks(&client) == false);
  129. }
  130. pthread_exit(0);
  131. }
  132. }
  133. namespace {
  134. #if defined(HAVE_PTHREAD_TIMEDJOIN_NP) && HAVE_PTHREAD_TIMEDJOIN_NP
  135. bool fill_timespec(struct timespec& ts)
  136. {
  137. #if defined(HAVE_LIBRT) && HAVE_LIBRT
  138. if (HAVE_LIBRT) // This won't be called on OSX, etc,...
  139. {
  140. if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
  141. {
  142. Error << "clock_gettime(CLOCK_REALTIME) " << strerror(errno);
  143. return false;
  144. }
  145. }
  146. #else
  147. {
  148. struct timeval tv;
  149. if (gettimeofday(&tv, NULL) == -1)
  150. {
  151. Error << "gettimeofday() " << strerror(errno);
  152. return false;
  153. }
  154. TIMEVAL_TO_TIMESPEC(&tv, &ts);
  155. }
  156. #endif
  157. return true;
  158. }
  159. #endif
  160. } // namespace
  161. #pragma GCC diagnostic push
  162. #pragma GCC diagnostic ignored "-Wunreachable-code"
  163. static bool join_thread(pthread_t& thread_arg)
  164. {
  165. int error;
  166. #if defined(HAVE_PTHREAD_TIMEDJOIN_NP) && HAVE_PTHREAD_TIMEDJOIN_NP
  167. if (HAVE_PTHREAD_TIMEDJOIN_NP)
  168. {
  169. int limit= 2;
  170. while (--limit)
  171. {
  172. struct timespec ts;
  173. if (fill_timespec(ts))
  174. {
  175. ts.tv_sec+= 30;
  176. switch ((error= pthread_timedjoin_np(thread_arg, NULL, &ts)))
  177. {
  178. case ETIMEDOUT:
  179. continue;
  180. case 0:
  181. return true;
  182. case ESRCH:
  183. return false;
  184. default:
  185. Error << "pthread_timedjoin_np() " << strerror(error);
  186. return false;
  187. }
  188. }
  189. }
  190. if ((error= pthread_cancel(thread_arg)) != 0)
  191. {
  192. Error << "pthread_cancel() " << strerror(error);
  193. return false;
  194. }
  195. return true;
  196. }
  197. #endif
  198. if ((error= pthread_join(thread_arg, NULL)) != 0)
  199. {
  200. Error << "pthread_join() " << strerror(error);
  201. return false;
  202. }
  203. return true;
  204. }
  205. #pragma GCC diagnostic pop
  206. static test_return_t send_random_port_data_TEST(void* )
  207. {
  208. set_alarm(1200, 0);
  209. SimpleClient client("localhost", current_server());
  210. for (size_t x= 0; x < 200; ++x)
  211. {
  212. libtest::vchar_t message_;
  213. libtest::vchar_t response_;
  214. libtest::vchar::make(message_, size_t(random() % 1024));
  215. client.send_data(message_, response_);
  216. }
  217. return TEST_SUCCESS;
  218. }
  219. static test_return_t worker_ramp_exec(const size_t payload_size)
  220. {
  221. set_alarm(1200, 0);
  222. std::vector<pthread_t> children;
  223. children.resize(number_of_cpus());
  224. std::vector<client_thread_context_st> success;
  225. success.resize(children.size());
  226. for (size_t x= 0; x < children.size(); ++x)
  227. {
  228. success[x].payload_size= payload_size;
  229. pthread_create(&children[x], NULL, client_thread, &success[x]);
  230. }
  231. for (size_t x= 0; x < children.size(); ++x)
  232. {
  233. pthread_t& thread= children[x];
  234. bool join_success= false;
  235. int limit= 3;
  236. while (join_success == false and --limit)
  237. {
  238. join_success= join_thread(thread);
  239. }
  240. if (join_success == false)
  241. {
  242. pthread_cancel(thread);
  243. Error << "Something went very wrong, it is likely threads were not cleaned up";
  244. }
  245. }
  246. return TEST_SUCCESS;
  247. }
  248. static test_return_t worker_ramp_TEST(void *)
  249. {
  250. return worker_ramp_exec(0);
  251. }
  252. static test_return_t worker_ramp_1K_TEST(void *)
  253. {
  254. return worker_ramp_exec(1024);
  255. }
  256. static test_return_t worker_ramp_10K_TEST(void *)
  257. {
  258. return worker_ramp_exec(1024*10);
  259. }
  260. static test_return_t skip_SETUP(void*)
  261. {
  262. SKIP_IF(true);
  263. return TEST_SUCCESS;
  264. }
  265. static test_return_t worker_ramp_SETUP(void *object)
  266. {
  267. worker_handles_st *handles= (worker_handles_st*)object;
  268. gearman_function_t echo_react_fn= gearman_function_create(echo_or_react_worker_v2);
  269. for (uint32_t x= 0; x < 10; x++)
  270. {
  271. worker_handle_st *worker;
  272. if ((worker= test_worker_start(current_server(), NULL, WORKER_FUNCTION_NAME, echo_react_fn, NULL, gearman_worker_options_t())) == NULL)
  273. {
  274. return TEST_FAILURE;
  275. }
  276. handles->push(worker);
  277. }
  278. return TEST_SUCCESS;
  279. }
  280. static test_return_t worker_ramp_TEARDOWN(void* object)
  281. {
  282. worker_handles_st *handles= (worker_handles_st*)object;
  283. handles->reset();
  284. reset_server();
  285. return TEST_SUCCESS;
  286. }
  287. static test_return_t hostile_gearmand_SETUP(void* object)
  288. {
  289. test_skip(true, has_hostile());
  290. test_skip_valgrind();
  291. test_skip(true, libtest::is_massive());
  292. // Programmer error
  293. assert(hostile_server_port);
  294. set_server(hostile_server_port);
  295. worker_ramp_SETUP(object);
  296. return TEST_SUCCESS;
  297. }
  298. static test_return_t recv_SETUP(void* object)
  299. {
  300. test_skip_valgrind();
  301. test_skip(true, libtest::is_massive());
  302. worker_ramp_SETUP(object);
  303. set_recv_close(true, 20, 20);
  304. return TEST_SUCCESS;
  305. }
  306. static test_return_t getaddrinfo_SETUP(void* object)
  307. {
  308. test_skip_valgrind();
  309. test_skip(true, libtest::is_massive());
  310. worker_ramp_SETUP(object);
  311. set_getaddrinfo_error(true, 20, 20);
  312. return TEST_SUCCESS;
  313. }
  314. static test_return_t recv_corrupt_SETUP(void* object)
  315. {
  316. return TEST_SKIPPED;
  317. test_skip_valgrind();
  318. test_skip(true, libtest::is_massive());
  319. worker_ramp_SETUP(object);
  320. set_recv_corrupt(true, 20, 20);
  321. return TEST_SUCCESS;
  322. }
  323. static test_return_t getaddrinfo_TEARDOWN(void* object)
  324. {
  325. set_getaddrinfo_error(true, 0, 0);
  326. worker_handles_st *handles= (worker_handles_st*)object;
  327. handles->kill_all();
  328. reset_server();
  329. return TEST_SUCCESS;
  330. }
  331. static test_return_t recv_TEARDOWN(void* object)
  332. {
  333. set_recv_close(true, 0, 0);
  334. worker_handles_st *handles= (worker_handles_st*)object;
  335. handles->kill_all();
  336. reset_server();
  337. return TEST_SUCCESS;
  338. }
  339. static test_return_t send_SETUP(void* object)
  340. {
  341. test_skip_valgrind();
  342. test_skip(true, libtest::is_massive());
  343. worker_ramp_SETUP(object);
  344. set_send_close(true, 20, 20);
  345. return TEST_SUCCESS;
  346. }
  347. static test_return_t accept_SETUP(void* object)
  348. {
  349. test_skip_valgrind();
  350. test_skip(true, libtest::is_massive());
  351. worker_ramp_SETUP(object);
  352. set_accept_close(true, 20, 20);
  353. return TEST_SUCCESS;
  354. }
  355. static test_return_t connect_SETUP(void* object)
  356. {
  357. test_skip_valgrind();
  358. test_skip(true, libtest::is_massive());
  359. worker_ramp_SETUP(object);
  360. set_connect_close(true, 20, 20);
  361. return TEST_SUCCESS;
  362. }
  363. static test_return_t poll_HOSTILE_POLL_CLOSED_SETUP(void* object)
  364. {
  365. test_skip_valgrind();
  366. test_skip(true, libtest::is_massive());
  367. worker_ramp_SETUP(object);
  368. set_poll_close(true, 4, 0, HOSTILE_POLL_CLOSED);
  369. return TEST_SUCCESS;
  370. }
  371. static test_return_t poll_HOSTILE_POLL_SHUT_WR_SETUP(void* object)
  372. {
  373. test_skip_valgrind();
  374. test_skip(true, libtest::is_massive());
  375. worker_ramp_SETUP(object);
  376. set_poll_close(true, 4, 0, HOSTILE_POLL_SHUT_WR);
  377. return TEST_SUCCESS;
  378. }
  379. static test_return_t poll_HOSTILE_POLL_SHUT_RD_SETUP(void* object)
  380. {
  381. test_skip_valgrind();
  382. test_skip(true, libtest::is_massive());
  383. worker_ramp_SETUP(object);
  384. set_poll_close(true, 4, 0, HOSTILE_POLL_SHUT_RD);
  385. return TEST_SUCCESS;
  386. }
  387. static test_return_t poll_TEARDOWN(void* object)
  388. {
  389. set_poll_close(false, 0, 0, HOSTILE_POLL_CLOSED);
  390. worker_handles_st *handles= (worker_handles_st*)object;
  391. handles->kill_all();
  392. reset_server();
  393. return TEST_SUCCESS;
  394. }
  395. static test_return_t send_TEARDOWN(void* object)
  396. {
  397. set_send_close(true, 0, 0);
  398. worker_handles_st *handles= (worker_handles_st*)object;
  399. handles->kill_all();
  400. reset_server();
  401. return TEST_SUCCESS;
  402. }
  403. static test_return_t accept_TEARDOWN(void* object)
  404. {
  405. set_accept_close(true, 0, 0);
  406. worker_handles_st *handles= (worker_handles_st*)object;
  407. handles->kill_all();
  408. reset_server();
  409. return TEST_SUCCESS;
  410. }
  411. static test_return_t connect_TEARDOWN(void* object)
  412. {
  413. set_connect_close(true, 0, 0);
  414. worker_handles_st *handles= (worker_handles_st*)object;
  415. handles->kill_all();
  416. reset_server();
  417. return TEST_SUCCESS;
  418. }
  419. /*********************** World functions **************************************/
  420. static void *world_create(server_startup_st& servers, test_return_t&)
  421. {
  422. SKIP_IF(valgrind_is_caller());
  423. SKIP_UNLESS(has_hostile());
  424. hostile_server_port= libtest::get_free_port();
  425. ASSERT_TRUE(server_startup(servers, SERVER_TARGET, hostile_server_port, NULL));
  426. return new worker_handles_st;
  427. }
  428. static bool world_destroy(void *object)
  429. {
  430. worker_handles_st *handles= (worker_handles_st *)object;
  431. delete handles;
  432. return TEST_SUCCESS;
  433. }
  434. test_st dos_TESTS[] ={
  435. {"send random port data", 0, send_random_port_data_TEST },
  436. {0, 0, 0}
  437. };
  438. test_st worker_TESTS[] ={
  439. {"first pass", 0, worker_ramp_TEST },
  440. {"second pass", 0, worker_ramp_TEST },
  441. {"first pass 1K jobs", 0, worker_ramp_1K_TEST },
  442. {"first pass 10K jobs", 0, worker_ramp_10K_TEST },
  443. {0, 0, 0}
  444. };
  445. collection_st collection[] ={
  446. {"dos", skip_SETUP, 0, dos_TESTS },
  447. {"plain", worker_ramp_SETUP, worker_ramp_TEARDOWN, worker_TESTS },
  448. {"plain against hostile server", hostile_gearmand_SETUP, worker_ramp_TEARDOWN, worker_TESTS },
  449. {"hostile recv()", recv_SETUP, recv_TEARDOWN, worker_TESTS },
  450. {"hostile recv() corrupt", recv_corrupt_SETUP, recv_TEARDOWN, worker_TESTS },
  451. {"hostile send()", send_SETUP, send_TEARDOWN, worker_TESTS },
  452. {"hostile accept()", accept_SETUP, accept_TEARDOWN, worker_TESTS },
  453. {"hostile connect()", connect_SETUP, connect_TEARDOWN, worker_TESTS },
  454. {"hostile poll(CLOSED)", poll_HOSTILE_POLL_CLOSED_SETUP, poll_TEARDOWN, worker_TESTS },
  455. {"hostile poll(SHUT_RD)", poll_HOSTILE_POLL_SHUT_RD_SETUP, poll_TEARDOWN, worker_TESTS },
  456. {"hostile poll(SHUT_WR)", poll_HOSTILE_POLL_SHUT_WR_SETUP, poll_TEARDOWN, worker_TESTS },
  457. {"hostile getaddrinfo()", getaddrinfo_SETUP, getaddrinfo_TEARDOWN, worker_TESTS },
  458. {0, 0, 0, 0}
  459. };
  460. void get_world(libtest::Framework *world)
  461. {
  462. world->collections(collection);
  463. world->create(world_create);
  464. world->destroy(world_destroy);
  465. }