test_guardfraction.c 14 KB

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