test_oos.c 12 KB

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