test_guardfraction.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* Copyright (c) 2014-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define GUARDFRACTION_PRIVATE
  4. #define NETWORKSTATUS_PRIVATE
  5. #define NS_PARSE_PRIVATE
  6. #include "orconfig.h"
  7. #include "core/or/or.h"
  8. #include "app/config/config.h"
  9. #include "feature/dirauth/guardfraction.h"
  10. #include "feature/client/entrynodes.h"
  11. #include "feature/dirparse/ns_parse.h"
  12. #include "feature/nodelist/networkstatus.h"
  13. #include "feature/nodelist/networkstatus_st.h"
  14. #include "feature/dirauth/vote_microdesc_hash_st.h"
  15. #include "feature/nodelist/vote_routerstatus_st.h"
  16. #include "test/test.h"
  17. #include "test/test_helpers.h"
  18. #include "test/log_test_helpers.h"
  19. /** Generate a vote_routerstatus_t for a router with identity digest
  20. * <b>digest_in_hex</b>. */
  21. static vote_routerstatus_t *
  22. gen_vote_routerstatus_for_tests(const char *digest_in_hex, int is_guard)
  23. {
  24. int retval;
  25. vote_routerstatus_t *vrs = NULL;
  26. routerstatus_t *rs;
  27. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  28. rs = &vrs->status;
  29. { /* Useful information for tests */
  30. char digest_tmp[DIGEST_LEN];
  31. /* Guard or not? */
  32. rs->is_possible_guard = is_guard;
  33. /* Fill in the fpr */
  34. tt_int_op(strlen(digest_in_hex), OP_EQ, HEX_DIGEST_LEN);
  35. retval = base16_decode(digest_tmp, sizeof(digest_tmp),
  36. digest_in_hex, HEX_DIGEST_LEN);
  37. tt_int_op(retval, OP_EQ, sizeof(digest_tmp));
  38. memcpy(rs->identity_digest, digest_tmp, DIGEST_LEN);
  39. }
  40. { /* Misc info (maybe not used in tests) */
  41. vrs->version = tor_strdup("0.1.2.14");
  42. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  43. memset(rs->descriptor_digest, 78, DIGEST_LEN);
  44. rs->addr = 0x99008801;
  45. rs->or_port = 443;
  46. rs->dir_port = 8000;
  47. /* all flags but running cleared */
  48. rs->is_flagged_running = 1;
  49. vrs->has_measured_bw = 1;
  50. rs->has_bandwidth = 1;
  51. }
  52. return vrs;
  53. done:
  54. vote_routerstatus_free(vrs);
  55. return NULL;
  56. }
  57. /** Make sure our parsers reject corrupted guardfraction files. */
  58. static void
  59. test_parse_guardfraction_file_bad(void *arg)
  60. {
  61. int retval;
  62. char *guardfraction_bad = NULL;
  63. const char *yesterday_date_str = get_yesterday_date_str();
  64. (void) arg;
  65. /* Start parsing all those corrupted guardfraction files! */
  66. /* Guardfraction file version is not a number! */
  67. tor_asprintf(&guardfraction_bad,
  68. "guardfraction-file-version nan\n"
  69. "written-at %s\n"
  70. "n-inputs 420 3\n"
  71. "guard-seen D0EDB47BEAD32D26D0A837F7D5357EC3AD3B8777 100 420\n"
  72. "guard-seen 07B5547026DF3E229806E135CFA8552D56AFBABC 5 420\n",
  73. yesterday_date_str);
  74. retval = dirserv_read_guardfraction_file_from_str(guardfraction_bad, NULL);
  75. tt_int_op(retval, OP_EQ, -1);
  76. tor_free(guardfraction_bad);
  77. /* This one does not have a date! Parsing should fail. */
  78. tor_asprintf(&guardfraction_bad,
  79. "guardfraction-file-version 1\n"
  80. "written-at not_date\n"
  81. "n-inputs 420 3\n"
  82. "guard-seen D0EDB47BEAD32D26D0A837F7D5357EC3AD3B8777 100 420\n"
  83. "guard-seen 07B5547026DF3E229806E135CFA8552D56AFBABC 5 420\n");
  84. retval = dirserv_read_guardfraction_file_from_str(guardfraction_bad, NULL);
  85. tt_int_op(retval, OP_EQ, -1);
  86. tor_free(guardfraction_bad);
  87. /* This one has an incomplete n-inputs line, but parsing should
  88. still continue. */
  89. tor_asprintf(&guardfraction_bad,
  90. "guardfraction-file-version 1\n"
  91. "written-at %s\n"
  92. "n-inputs biggie\n"
  93. "guard-seen D0EDB47BEAD32D26D0A837F7D5357EC3AD3B8777 100 420\n"
  94. "guard-seen 07B5547026DF3E229806E135CFA8552D56AFBABC 5 420\n",
  95. yesterday_date_str);
  96. retval = dirserv_read_guardfraction_file_from_str(guardfraction_bad, NULL);
  97. tt_int_op(retval, OP_EQ, 2);
  98. tor_free(guardfraction_bad);
  99. /* This one does not have a fingerprint in the guard line! */
  100. tor_asprintf(&guardfraction_bad,
  101. "guardfraction-file-version 1\n"
  102. "written-at %s\n"
  103. "n-inputs 420 3\n"
  104. "guard-seen not_a_fingerprint 100 420\n",
  105. yesterday_date_str);
  106. retval = dirserv_read_guardfraction_file_from_str(guardfraction_bad, NULL);
  107. tt_int_op(retval, OP_EQ, 0);
  108. tor_free(guardfraction_bad);
  109. /* This one does not even have an integer guardfraction value. */
  110. tor_asprintf(&guardfraction_bad,
  111. "guardfraction-file-version 1\n"
  112. "written-at %s\n"
  113. "n-inputs 420 3\n"
  114. "guard-seen D0EDB47BEAD32D26D0A837F7D5357EC3AD3B8777 NaN 420\n"
  115. "guard-seen 07B5547026DF3E229806E135CFA8552D56AFBABC 5 420\n",
  116. yesterday_date_str);
  117. retval = dirserv_read_guardfraction_file_from_str(guardfraction_bad, NULL);
  118. tt_int_op(retval, OP_EQ, 1);
  119. tor_free(guardfraction_bad);
  120. /* This one is not a percentage (not in [0, 100]) */
  121. tor_asprintf(&guardfraction_bad,
  122. "guardfraction-file-version 1\n"
  123. "written-at %s\n"
  124. "n-inputs 420 3\n"
  125. "guard-seen D0EDB47BEAD32D26D0A837F7D5357EC3AD3B8777 666 420\n"
  126. "guard-seen 07B5547026DF3E229806E135CFA8552D56AFBABC 5 420\n",
  127. yesterday_date_str);
  128. retval = dirserv_read_guardfraction_file_from_str(guardfraction_bad, NULL);
  129. tt_int_op(retval, OP_EQ, 1);
  130. tor_free(guardfraction_bad);
  131. /* This one is not a percentage either (not in [0, 100]) */
  132. tor_asprintf(&guardfraction_bad,
  133. "guardfraction-file-version 1\n"
  134. "written-at %s\n"
  135. "n-inputs 420 3\n"
  136. "guard-seen D0EDB47BEAD32D26D0A837F7D5357EC3AD3B8777 -3 420\n",
  137. yesterday_date_str);
  138. retval = dirserv_read_guardfraction_file_from_str(guardfraction_bad, NULL);
  139. tt_int_op(retval, OP_EQ, 0);
  140. done:
  141. tor_free(guardfraction_bad);
  142. }
  143. /** Make sure that our test guardfraction file gets parsed properly, and
  144. * its information are applied properly to our routerstatuses. */
  145. static void
  146. test_parse_guardfraction_file_good(void *arg)
  147. {
  148. int retval;
  149. vote_routerstatus_t *vrs_guard = NULL;
  150. vote_routerstatus_t *vrs_dummy = NULL;
  151. char *guardfraction_good = NULL;
  152. const char *yesterday_date_str = get_yesterday_date_str();
  153. smartlist_t *routerstatuses = smartlist_new();
  154. /* Some test values that we need to validate later */
  155. const char fpr_guard[] = "D0EDB47BEAD32D26D0A837F7D5357EC3AD3B8777";
  156. const char fpr_unlisted[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  157. const int guardfraction_value = 42;
  158. (void) arg;
  159. {
  160. /* Populate the smartlist with some fake routerstatuses, so that
  161. after parsing the guardfraction file we can check that their
  162. elements got filled properly. */
  163. /* This one is a guard */
  164. vrs_guard = gen_vote_routerstatus_for_tests(fpr_guard, 1);
  165. tt_assert(vrs_guard);
  166. smartlist_add(routerstatuses, vrs_guard);
  167. /* This one is a guard but it's not in the guardfraction file */
  168. vrs_dummy = gen_vote_routerstatus_for_tests(fpr_unlisted, 1);
  169. tt_assert(vrs_dummy);
  170. smartlist_add(routerstatuses, vrs_dummy);
  171. }
  172. tor_asprintf(&guardfraction_good,
  173. "guardfraction-file-version 1\n"
  174. "written-at %s\n"
  175. "n-inputs 420 3\n"
  176. "guard-seen %s %d 420\n",
  177. yesterday_date_str,
  178. fpr_guard, guardfraction_value);
  179. /* Read the guardfraction file */
  180. retval = dirserv_read_guardfraction_file_from_str(guardfraction_good,
  181. routerstatuses);
  182. tt_int_op(retval, OP_EQ, 1);
  183. { /* Test that routerstatus fields got filled properly */
  184. /* The guardfraction fields of the guard should be filled. */
  185. tt_assert(vrs_guard->status.has_guardfraction);
  186. tt_int_op(vrs_guard->status.guardfraction_percentage,
  187. OP_EQ,
  188. guardfraction_value);
  189. /* The guard that was not in the guardfraction file should not have
  190. been touched either. */
  191. tt_assert(!vrs_dummy->status.has_guardfraction);
  192. }
  193. done:
  194. vote_routerstatus_free(vrs_guard);
  195. vote_routerstatus_free(vrs_dummy);
  196. smartlist_free(routerstatuses);
  197. tor_free(guardfraction_good);
  198. }
  199. /** Make sure that the guardfraction bandwidths get calculated properly. */
  200. static void
  201. test_get_guardfraction_bandwidth(void *arg)
  202. {
  203. guardfraction_bandwidth_t gf_bw;
  204. const int orig_bw = 1000;
  205. (void) arg;
  206. /* A guard with bandwidth 1000 and GuardFraction 0.25, should have
  207. bandwidth 250 as a guard and bandwidth 750 as a non-guard. */
  208. guard_get_guardfraction_bandwidth(&gf_bw,
  209. orig_bw, 25);
  210. tt_int_op(gf_bw.guard_bw, OP_EQ, 250);
  211. tt_int_op(gf_bw.non_guard_bw, OP_EQ, 750);
  212. /* Also check the 'guard_bw + non_guard_bw == original_bw'
  213. * invariant. */
  214. tt_int_op(gf_bw.non_guard_bw + gf_bw.guard_bw, OP_EQ, orig_bw);
  215. done:
  216. ;
  217. }
  218. /** Parse the GuardFraction element of the consensus, and make sure it
  219. * gets parsed correctly. */
  220. static void
  221. test_parse_guardfraction_consensus(void *arg)
  222. {
  223. int retval;
  224. or_options_t *options = get_options_mutable();
  225. const char *guardfraction_str_good = "GuardFraction=66";
  226. routerstatus_t rs_good;
  227. routerstatus_t rs_no_guard;
  228. const char *guardfraction_str_bad1 = "GuardFraction="; /* no value */
  229. routerstatus_t rs_bad1;
  230. const char *guardfraction_str_bad2 = "GuardFraction=166"; /* no percentage */
  231. routerstatus_t rs_bad2;
  232. (void) arg;
  233. /* GuardFraction use is currently disabled by default. So we need to
  234. manually enable it. */
  235. options->UseGuardFraction = 1;
  236. { /* Properly formatted GuardFraction. Check that it gets applied
  237. correctly. */
  238. memset(&rs_good, 0, sizeof(routerstatus_t));
  239. rs_good.is_possible_guard = 1;
  240. retval = routerstatus_parse_guardfraction(guardfraction_str_good,
  241. NULL, NULL,
  242. &rs_good);
  243. tt_int_op(retval, OP_EQ, 0);
  244. tt_assert(rs_good.has_guardfraction);
  245. tt_int_op(rs_good.guardfraction_percentage, OP_EQ, 66);
  246. }
  247. { /* Properly formatted GuardFraction but router is not a
  248. guard. GuardFraction should not get applied. */
  249. memset(&rs_no_guard, 0, sizeof(routerstatus_t));
  250. tt_assert(!rs_no_guard.is_possible_guard);
  251. setup_full_capture_of_logs(LOG_WARN);
  252. retval = routerstatus_parse_guardfraction(guardfraction_str_good,
  253. NULL, NULL,
  254. &rs_no_guard);
  255. tt_int_op(retval, OP_EQ, 0);
  256. tt_assert(!rs_no_guard.has_guardfraction);
  257. expect_single_log_msg_containing("Got GuardFraction for non-guard . "
  258. "This is not supposed to happen.");
  259. teardown_capture_of_logs();
  260. }
  261. { /* Bad GuardFraction. Function should fail and not apply. */
  262. memset(&rs_bad1, 0, sizeof(routerstatus_t));
  263. rs_bad1.is_possible_guard = 1;
  264. retval = routerstatus_parse_guardfraction(guardfraction_str_bad1,
  265. NULL, NULL,
  266. &rs_bad1);
  267. tt_int_op(retval, OP_EQ, -1);
  268. tt_assert(!rs_bad1.has_guardfraction);
  269. }
  270. { /* Bad GuardFraction. Function should fail and not apply. */
  271. memset(&rs_bad2, 0, sizeof(routerstatus_t));
  272. rs_bad2.is_possible_guard = 1;
  273. retval = routerstatus_parse_guardfraction(guardfraction_str_bad2,
  274. NULL, NULL,
  275. &rs_bad2);
  276. tt_int_op(retval, OP_EQ, -1);
  277. tt_assert(!rs_bad2.has_guardfraction);
  278. }
  279. done:
  280. teardown_capture_of_logs();
  281. }
  282. /** Make sure that we use GuardFraction information when we should,
  283. * according to the torrc option and consensus parameter. */
  284. static void
  285. test_should_apply_guardfraction(void *arg)
  286. {
  287. networkstatus_t vote_enabled, vote_disabled, vote_missing;
  288. or_options_t *options = get_options_mutable();
  289. (void) arg;
  290. { /* Fill the votes for later */
  291. /* This one suggests enabled GuardFraction. */
  292. memset(&vote_enabled, 0, sizeof(vote_enabled));
  293. vote_enabled.net_params = smartlist_new();
  294. smartlist_split_string(vote_enabled.net_params,
  295. "UseGuardFraction=1", NULL, 0, 0);
  296. /* This one suggests disabled GuardFraction. */
  297. memset(&vote_disabled, 0, sizeof(vote_disabled));
  298. vote_disabled.net_params = smartlist_new();
  299. smartlist_split_string(vote_disabled.net_params,
  300. "UseGuardFraction=0", NULL, 0, 0);
  301. /* This one doesn't have GuardFraction at all. */
  302. memset(&vote_missing, 0, sizeof(vote_missing));
  303. vote_missing.net_params = smartlist_new();
  304. smartlist_split_string(vote_missing.net_params,
  305. "leon=trout", NULL, 0, 0);
  306. }
  307. /* If torrc option is set to yes, we should always use
  308. * guardfraction.*/
  309. options->UseGuardFraction = 1;
  310. tt_int_op(should_apply_guardfraction(&vote_disabled), OP_EQ, 1);
  311. /* If torrc option is set to no, we should never use
  312. * guardfraction.*/
  313. options->UseGuardFraction = 0;
  314. tt_int_op(should_apply_guardfraction(&vote_enabled), OP_EQ, 0);
  315. /* Now let's test torrc option set to auto. */
  316. options->UseGuardFraction = -1;
  317. /* If torrc option is set to auto, and consensus parameter is set to
  318. * yes, we should use guardfraction. */
  319. tt_int_op(should_apply_guardfraction(&vote_enabled), OP_EQ, 1);
  320. /* If torrc option is set to auto, and consensus parameter is set to
  321. * no, we should use guardfraction. */
  322. tt_int_op(should_apply_guardfraction(&vote_disabled), OP_EQ, 0);
  323. /* If torrc option is set to auto, and consensus parameter is not
  324. * set, we should fallback to "no". */
  325. tt_int_op(should_apply_guardfraction(&vote_missing), OP_EQ, 0);
  326. done:
  327. SMARTLIST_FOREACH(vote_enabled.net_params, char *, cp, tor_free(cp));
  328. SMARTLIST_FOREACH(vote_disabled.net_params, char *, cp, tor_free(cp));
  329. SMARTLIST_FOREACH(vote_missing.net_params, char *, cp, tor_free(cp));
  330. smartlist_free(vote_enabled.net_params);
  331. smartlist_free(vote_disabled.net_params);
  332. smartlist_free(vote_missing.net_params);
  333. }
  334. struct testcase_t guardfraction_tests[] = {
  335. { "parse_guardfraction_file_bad", test_parse_guardfraction_file_bad,
  336. TT_FORK, NULL, NULL },
  337. { "parse_guardfraction_file_good", test_parse_guardfraction_file_good,
  338. TT_FORK, NULL, NULL },
  339. { "parse_guardfraction_consensus", test_parse_guardfraction_consensus,
  340. TT_FORK, NULL, NULL },
  341. { "get_guardfraction_bandwidth", test_get_guardfraction_bandwidth,
  342. TT_FORK, NULL, NULL },
  343. { "should_apply_guardfraction", test_should_apply_guardfraction,
  344. TT_FORK, NULL, NULL },
  345. END_OF_TESTCASES
  346. };