test_oos.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* Copyright (c) 2016-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* Unit tests for OOS handler */
  4. #define CONNECTION_PRIVATE
  5. #include "or/or.h"
  6. #include "or/config.h"
  7. #include "or/connection.h"
  8. #include "or/connection_or.h"
  9. #include "or/main.h"
  10. #include "test/test.h"
  11. #include "or/dir_connection_st.h"
  12. #include "or/or_connection_st.h"
  13. static or_options_t mock_options;
  14. static void
  15. reset_options_mock(void)
  16. {
  17. memset(&mock_options, 0, sizeof(or_options_t));
  18. }
  19. static const or_options_t *
  20. mock_get_options(void)
  21. {
  22. return &mock_options;
  23. }
  24. static int moribund_calls = 0;
  25. static int moribund_conns = 0;
  26. static int
  27. mock_connection_count_moribund(void)
  28. {
  29. ++moribund_calls;
  30. return moribund_conns;
  31. }
  32. /*
  33. * For unit test purposes it's sufficient to tell that
  34. * kill_conn_list_for_oos() was called with an approximately
  35. * sane argument; it's just the thing we returned from the
  36. * mock for pick_oos_victims().
  37. */
  38. static int kill_conn_list_calls = 0;
  39. static int kill_conn_list_killed = 0;
  40. static void
  41. kill_conn_list_mock(smartlist_t *conns)
  42. {
  43. ++kill_conn_list_calls;
  44. tt_ptr_op(conns, OP_NE, NULL);
  45. kill_conn_list_killed += smartlist_len(conns);
  46. done:
  47. return;
  48. }
  49. static int pick_oos_mock_calls = 0;
  50. static int pick_oos_mock_fail = 0;
  51. static int pick_oos_mock_last_n = 0;
  52. static smartlist_t *
  53. pick_oos_victims_mock(int n)
  54. {
  55. smartlist_t *l = NULL;
  56. int i;
  57. ++pick_oos_mock_calls;
  58. tt_int_op(n, OP_GT, 0);
  59. if (!pick_oos_mock_fail) {
  60. /*
  61. * connection_check_oos() just passes the list onto
  62. * kill_conn_list_for_oos(); we don't need to simulate
  63. * its content for this mock, just its existence, but
  64. * we do need to check the parameter.
  65. */
  66. l = smartlist_new();
  67. for (i = 0; i < n; ++i) smartlist_add(l, NULL);
  68. } else {
  69. l = NULL;
  70. }
  71. pick_oos_mock_last_n = n;
  72. done:
  73. return l;
  74. }
  75. /** Unit test for the logic in connection_check_oos(), which is concerned
  76. * with comparing thresholds and connection counts to decide if an OOS has
  77. * occurred and if so, how many connections to try to kill, and then using
  78. * pick_oos_victims() and kill_conn_list_for_oos() to carry out its grim
  79. * duty.
  80. */
  81. static void
  82. test_oos_connection_check_oos(void *arg)
  83. {
  84. (void)arg;
  85. /* Set up mocks */
  86. reset_options_mock();
  87. /* OOS handling is only sensitive to these fields */
  88. mock_options.ConnLimit = 32;
  89. mock_options.ConnLimit_ = 64;
  90. mock_options.ConnLimit_high_thresh = 60;
  91. mock_options.ConnLimit_low_thresh = 50;
  92. MOCK(get_options, mock_get_options);
  93. moribund_calls = 0;
  94. moribund_conns = 0;
  95. MOCK(connection_count_moribund, mock_connection_count_moribund);
  96. kill_conn_list_calls = 0;
  97. kill_conn_list_killed = 0;
  98. MOCK(kill_conn_list_for_oos, kill_conn_list_mock);
  99. pick_oos_mock_calls = 0;
  100. pick_oos_mock_fail = 0;
  101. MOCK(pick_oos_victims, pick_oos_victims_mock);
  102. /* No OOS case */
  103. connection_check_oos(50, 0);
  104. tt_int_op(moribund_calls, OP_EQ, 0);
  105. tt_int_op(pick_oos_mock_calls, OP_EQ, 0);
  106. tt_int_op(kill_conn_list_calls, OP_EQ, 0);
  107. /* OOS from socket count, nothing moribund */
  108. connection_check_oos(62, 0);
  109. tt_int_op(moribund_calls, OP_EQ, 1);
  110. tt_int_op(pick_oos_mock_calls, OP_EQ, 1);
  111. /* 12 == 62 - ConnLimit_low_thresh */
  112. tt_int_op(pick_oos_mock_last_n, OP_EQ, 12);
  113. tt_int_op(kill_conn_list_calls, OP_EQ, 1);
  114. tt_int_op(kill_conn_list_killed, OP_EQ, 12);
  115. /* OOS from socket count, some are moribund */
  116. kill_conn_list_killed = 0;
  117. moribund_conns = 5;
  118. connection_check_oos(62, 0);
  119. tt_int_op(moribund_calls, OP_EQ, 2);
  120. tt_int_op(pick_oos_mock_calls, OP_EQ, 2);
  121. /* 7 == 62 - ConnLimit_low_thresh - moribund_conns */
  122. tt_int_op(pick_oos_mock_last_n, OP_EQ, 7);
  123. tt_int_op(kill_conn_list_calls, OP_EQ, 2);
  124. tt_int_op(kill_conn_list_killed, OP_EQ, 7);
  125. /* OOS from socket count, but pick fails */
  126. kill_conn_list_killed = 0;
  127. moribund_conns = 0;
  128. pick_oos_mock_fail = 1;
  129. connection_check_oos(62, 0);
  130. tt_int_op(moribund_calls, OP_EQ, 3);
  131. tt_int_op(pick_oos_mock_calls, OP_EQ, 3);
  132. tt_int_op(kill_conn_list_calls, OP_EQ, 2);
  133. tt_int_op(kill_conn_list_killed, OP_EQ, 0);
  134. pick_oos_mock_fail = 0;
  135. /*
  136. * OOS from socket count with so many moribund conns
  137. * we have none to kill.
  138. */
  139. kill_conn_list_killed = 0;
  140. moribund_conns = 15;
  141. connection_check_oos(62, 0);
  142. tt_int_op(moribund_calls, OP_EQ, 4);
  143. tt_int_op(pick_oos_mock_calls, OP_EQ, 3);
  144. tt_int_op(kill_conn_list_calls, OP_EQ, 2);
  145. /*
  146. * OOS from socket exhaustion; OOS handler will try to
  147. * kill 1/10 (5) of the connections.
  148. */
  149. kill_conn_list_killed = 0;
  150. moribund_conns = 0;
  151. connection_check_oos(50, 1);
  152. tt_int_op(moribund_calls, OP_EQ, 5);
  153. tt_int_op(pick_oos_mock_calls, OP_EQ, 4);
  154. tt_int_op(kill_conn_list_calls, OP_EQ, 3);
  155. tt_int_op(kill_conn_list_killed, OP_EQ, 5);
  156. /* OOS from socket exhaustion with moribund conns */
  157. kill_conn_list_killed = 0;
  158. moribund_conns = 2;
  159. connection_check_oos(50, 1);
  160. tt_int_op(moribund_calls, OP_EQ, 6);
  161. tt_int_op(pick_oos_mock_calls, OP_EQ, 5);
  162. tt_int_op(kill_conn_list_calls, OP_EQ, 4);
  163. tt_int_op(kill_conn_list_killed, OP_EQ, 3);
  164. /* OOS from socket exhaustion with many moribund conns */
  165. kill_conn_list_killed = 0;
  166. moribund_conns = 7;
  167. connection_check_oos(50, 1);
  168. tt_int_op(moribund_calls, OP_EQ, 7);
  169. tt_int_op(pick_oos_mock_calls, OP_EQ, 5);
  170. tt_int_op(kill_conn_list_calls, OP_EQ, 4);
  171. /* OOS with both socket exhaustion and above-threshold */
  172. kill_conn_list_killed = 0;
  173. moribund_conns = 0;
  174. connection_check_oos(62, 1);
  175. tt_int_op(moribund_calls, OP_EQ, 8);
  176. tt_int_op(pick_oos_mock_calls, OP_EQ, 6);
  177. tt_int_op(kill_conn_list_calls, OP_EQ, 5);
  178. tt_int_op(kill_conn_list_killed, OP_EQ, 12);
  179. /*
  180. * OOS with both socket exhaustion and above-threshold with some
  181. * moribund conns
  182. */
  183. kill_conn_list_killed = 0;
  184. moribund_conns = 5;
  185. connection_check_oos(62, 1);
  186. tt_int_op(moribund_calls, OP_EQ, 9);
  187. tt_int_op(pick_oos_mock_calls, OP_EQ, 7);
  188. tt_int_op(kill_conn_list_calls, OP_EQ, 6);
  189. tt_int_op(kill_conn_list_killed, OP_EQ, 7);
  190. /*
  191. * OOS with both socket exhaustion and above-threshold with many
  192. * moribund conns
  193. */
  194. kill_conn_list_killed = 0;
  195. moribund_conns = 15;
  196. connection_check_oos(62, 1);
  197. tt_int_op(moribund_calls, OP_EQ, 10);
  198. tt_int_op(pick_oos_mock_calls, OP_EQ, 7);
  199. tt_int_op(kill_conn_list_calls, OP_EQ, 6);
  200. done:
  201. UNMOCK(pick_oos_victims);
  202. UNMOCK(kill_conn_list_for_oos);
  203. UNMOCK(connection_count_moribund);
  204. UNMOCK(get_options);
  205. return;
  206. }
  207. static int cfe_calls = 0;
  208. static void
  209. close_for_error_mock(or_connection_t *orconn, int flush)
  210. {
  211. (void)flush;
  212. tt_ptr_op(orconn, OP_NE, NULL);
  213. ++cfe_calls;
  214. done:
  215. return;
  216. }
  217. static int mark_calls = 0;
  218. static void
  219. mark_for_close_oos_mock(connection_t *conn,
  220. int line, const char *file)
  221. {
  222. (void)line;
  223. (void)file;
  224. tt_ptr_op(conn, OP_NE, NULL);
  225. ++mark_calls;
  226. done:
  227. return;
  228. }
  229. static void
  230. test_oos_kill_conn_list(void *arg)
  231. {
  232. connection_t *c1, *c2;
  233. or_connection_t *or_c1 = NULL;
  234. dir_connection_t *dir_c2 = NULL;
  235. smartlist_t *l = NULL;
  236. (void)arg;
  237. /* Set up mocks */
  238. mark_calls = 0;
  239. MOCK(connection_mark_for_close_internal_, mark_for_close_oos_mock);
  240. cfe_calls = 0;
  241. MOCK(connection_or_close_for_error, close_for_error_mock);
  242. /* Make fake conns */
  243. or_c1 = tor_malloc_zero(sizeof(*or_c1));
  244. or_c1->base_.magic = OR_CONNECTION_MAGIC;
  245. or_c1->base_.type = CONN_TYPE_OR;
  246. c1 = TO_CONN(or_c1);
  247. dir_c2 = tor_malloc_zero(sizeof(*dir_c2));
  248. dir_c2->base_.magic = DIR_CONNECTION_MAGIC;
  249. dir_c2->base_.type = CONN_TYPE_DIR;
  250. dir_c2->base_.state = DIR_CONN_STATE_MIN_;
  251. dir_c2->base_.purpose = DIR_PURPOSE_MIN_;
  252. c2 = TO_CONN(dir_c2);
  253. tt_ptr_op(c1, OP_NE, NULL);
  254. tt_ptr_op(c2, OP_NE, NULL);
  255. /* Make list */
  256. l = smartlist_new();
  257. smartlist_add(l, c1);
  258. smartlist_add(l, c2);
  259. /* Run kill_conn_list_for_oos() */
  260. kill_conn_list_for_oos(l);
  261. /* Check call counters */
  262. tt_int_op(mark_calls, OP_EQ, 1);
  263. tt_int_op(cfe_calls, OP_EQ, 1);
  264. done:
  265. UNMOCK(connection_or_close_for_error);
  266. UNMOCK(connection_mark_for_close_internal_);
  267. if (l) smartlist_free(l);
  268. tor_free(or_c1);
  269. tor_free(dir_c2);
  270. return;
  271. }
  272. static smartlist_t *conns_for_mock = NULL;
  273. static smartlist_t *
  274. get_conns_mock(void)
  275. {
  276. return conns_for_mock;
  277. }
  278. /*
  279. * For this mock, we pretend all conns have either zero or one circuits,
  280. * depending on if this appears on the list of things to say have a circuit.
  281. */
  282. static smartlist_t *conns_with_circs = NULL;
  283. static int
  284. get_num_circuits_mock(or_connection_t *conn)
  285. {
  286. int circs = 0;
  287. tt_ptr_op(conn, OP_NE, NULL);
  288. if (conns_with_circs &&
  289. smartlist_contains(conns_with_circs, TO_CONN(conn))) {
  290. circs = 1;
  291. }
  292. done:
  293. return circs;
  294. }
  295. static void
  296. test_oos_pick_oos_victims(void *arg)
  297. {
  298. (void)arg;
  299. or_connection_t *ortmp;
  300. dir_connection_t *dirtmp;
  301. smartlist_t *picked;
  302. /* Set up mocks */
  303. conns_for_mock = smartlist_new();
  304. MOCK(get_connection_array, get_conns_mock);
  305. conns_with_circs = smartlist_new();
  306. MOCK(connection_or_get_num_circuits, get_num_circuits_mock);
  307. /* Make some fake connections */
  308. ortmp = tor_malloc_zero(sizeof(*ortmp));
  309. ortmp->base_.magic = OR_CONNECTION_MAGIC;
  310. ortmp->base_.type = CONN_TYPE_OR;
  311. smartlist_add(conns_for_mock, TO_CONN(ortmp));
  312. /* We'll pretend this one has a circuit too */
  313. smartlist_add(conns_with_circs, TO_CONN(ortmp));
  314. /* Next one */
  315. ortmp = tor_malloc_zero(sizeof(*ortmp));
  316. ortmp->base_.magic = OR_CONNECTION_MAGIC;
  317. ortmp->base_.type = CONN_TYPE_OR;
  318. smartlist_add(conns_for_mock, TO_CONN(ortmp));
  319. /* Next one is moribund */
  320. ortmp = tor_malloc_zero(sizeof(*ortmp));
  321. ortmp->base_.magic = OR_CONNECTION_MAGIC;
  322. ortmp->base_.type = CONN_TYPE_OR;
  323. ortmp->base_.marked_for_close = 1;
  324. smartlist_add(conns_for_mock, TO_CONN(ortmp));
  325. /* Last one isn't an orconn */
  326. dirtmp = tor_malloc_zero(sizeof(*dirtmp));
  327. dirtmp->base_.magic = DIR_CONNECTION_MAGIC;
  328. dirtmp->base_.type = CONN_TYPE_DIR;
  329. smartlist_add(conns_for_mock, TO_CONN(dirtmp));
  330. /* Try picking one */
  331. picked = pick_oos_victims(1);
  332. /* It should be the one with circuits */
  333. tt_ptr_op(picked, OP_NE, NULL);
  334. tt_int_op(smartlist_len(picked), OP_EQ, 1);
  335. tt_assert(smartlist_contains(picked, smartlist_get(conns_for_mock, 0)));
  336. smartlist_free(picked);
  337. /* Try picking none */
  338. picked = pick_oos_victims(0);
  339. /* We should get an empty list */
  340. tt_ptr_op(picked, OP_NE, NULL);
  341. tt_int_op(smartlist_len(picked), OP_EQ, 0);
  342. smartlist_free(picked);
  343. /* Try picking two */
  344. picked = pick_oos_victims(2);
  345. /* We should get both active orconns */
  346. tt_ptr_op(picked, OP_NE, NULL);
  347. tt_int_op(smartlist_len(picked), OP_EQ, 2);
  348. tt_assert(smartlist_contains(picked, smartlist_get(conns_for_mock, 0)));
  349. tt_assert(smartlist_contains(picked, smartlist_get(conns_for_mock, 1)));
  350. smartlist_free(picked);
  351. /* Try picking three - only two are eligible */
  352. picked = pick_oos_victims(3);
  353. tt_int_op(smartlist_len(picked), OP_EQ, 2);
  354. tt_assert(smartlist_contains(picked, smartlist_get(conns_for_mock, 0)));
  355. tt_assert(smartlist_contains(picked, smartlist_get(conns_for_mock, 1)));
  356. smartlist_free(picked);
  357. done:
  358. /* Free leftover stuff */
  359. if (conns_with_circs) {
  360. smartlist_free(conns_with_circs);
  361. conns_with_circs = NULL;
  362. }
  363. UNMOCK(connection_or_get_num_circuits);
  364. if (conns_for_mock) {
  365. SMARTLIST_FOREACH(conns_for_mock, connection_t *, c, tor_free(c));
  366. smartlist_free(conns_for_mock);
  367. conns_for_mock = NULL;
  368. }
  369. UNMOCK(get_connection_array);
  370. return;
  371. }
  372. struct testcase_t oos_tests[] = {
  373. { "connection_check_oos", test_oos_connection_check_oos,
  374. TT_FORK, NULL, NULL },
  375. { "kill_conn_list", test_oos_kill_conn_list, TT_FORK, NULL, NULL },
  376. { "pick_oos_victims", test_oos_pick_oos_victims, TT_FORK, NULL, NULL },
  377. END_OF_TESTCASES
  378. };