test_guardfraction.c 14 KB

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