test_guardfraction.c 14 KB

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