test_guardfraction.c 14 KB

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