test_guardfraction.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* Copyright (c) 2014-2016, 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. int log_level = 0;
  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, ==, 0);
  244. tt_assert(rs_good.has_guardfraction);
  245. tt_int_op(rs_good.guardfraction_percentage, ==, 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. log_level = 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, ==, 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(log_level);
  260. log_level = 0;
  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, ==, -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, ==, -1);
  278. tt_assert(!rs_bad2.has_guardfraction);
  279. }
  280. done:
  281. if (log_level)
  282. teardown_capture_of_logs(log_level);
  283. }
  284. /** Make sure that we use GuardFraction information when we should,
  285. * according to the torrc option and consensus parameter. */
  286. static void
  287. test_should_apply_guardfraction(void *arg)
  288. {
  289. networkstatus_t vote_enabled, vote_disabled, vote_missing;
  290. or_options_t *options = get_options_mutable();
  291. (void) arg;
  292. { /* Fill the votes for later */
  293. /* This one suggests enabled GuardFraction. */
  294. memset(&vote_enabled, 0, sizeof(vote_enabled));
  295. vote_enabled.net_params = smartlist_new();
  296. smartlist_split_string(vote_enabled.net_params,
  297. "UseGuardFraction=1", NULL, 0, 0);
  298. /* This one suggests disabled GuardFraction. */
  299. memset(&vote_disabled, 0, sizeof(vote_disabled));
  300. vote_disabled.net_params = smartlist_new();
  301. smartlist_split_string(vote_disabled.net_params,
  302. "UseGuardFraction=0", NULL, 0, 0);
  303. /* This one doesn't have GuardFraction at all. */
  304. memset(&vote_missing, 0, sizeof(vote_missing));
  305. vote_missing.net_params = smartlist_new();
  306. smartlist_split_string(vote_missing.net_params,
  307. "leon=trout", NULL, 0, 0);
  308. }
  309. /* If torrc option is set to yes, we should always use
  310. * guardfraction.*/
  311. options->UseGuardFraction = 1;
  312. tt_int_op(should_apply_guardfraction(&vote_disabled), ==, 1);
  313. /* If torrc option is set to no, we should never use
  314. * guardfraction.*/
  315. options->UseGuardFraction = 0;
  316. tt_int_op(should_apply_guardfraction(&vote_enabled), ==, 0);
  317. /* Now let's test torrc option set to auto. */
  318. options->UseGuardFraction = -1;
  319. /* If torrc option is set to auto, and consensus parameter is set to
  320. * yes, we should use guardfraction. */
  321. tt_int_op(should_apply_guardfraction(&vote_enabled), ==, 1);
  322. /* If torrc option is set to auto, and consensus parameter is set to
  323. * no, we should use guardfraction. */
  324. tt_int_op(should_apply_guardfraction(&vote_disabled), ==, 0);
  325. /* If torrc option is set to auto, and consensus parameter is not
  326. * set, we should fallback to "no". */
  327. tt_int_op(should_apply_guardfraction(&vote_missing), ==, 0);
  328. done:
  329. SMARTLIST_FOREACH(vote_enabled.net_params, char *, cp, tor_free(cp));
  330. SMARTLIST_FOREACH(vote_disabled.net_params, char *, cp, tor_free(cp));
  331. SMARTLIST_FOREACH(vote_missing.net_params, char *, cp, tor_free(cp));
  332. smartlist_free(vote_enabled.net_params);
  333. smartlist_free(vote_disabled.net_params);
  334. smartlist_free(vote_missing.net_params);
  335. }
  336. struct testcase_t guardfraction_tests[] = {
  337. { "parse_guardfraction_file_bad", test_parse_guardfraction_file_bad,
  338. TT_FORK, NULL, NULL },
  339. { "parse_guardfraction_file_good", test_parse_guardfraction_file_good,
  340. TT_FORK, NULL, NULL },
  341. { "parse_guardfraction_consensus", test_parse_guardfraction_consensus,
  342. TT_FORK, NULL, NULL },
  343. { "get_guardfraction_bandwidth", test_get_guardfraction_bandwidth,
  344. TT_FORK, NULL, NULL },
  345. { "should_apply_guardfraction", test_should_apply_guardfraction,
  346. TT_FORK, NULL, NULL },
  347. END_OF_TESTCASES
  348. };