test_shared_random.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define SHARED_RANDOM_PRIVATE
  4. #define SHARED_RANDOM_STATE_PRIVATE
  5. #define CONFIG_PRIVATE
  6. #define DIRVOTE_PRIVATE
  7. #include "or.h"
  8. #include "test.h"
  9. #include "config.h"
  10. #include "dirauth/dirvote.h"
  11. #include "dirauth/shared_random.h"
  12. #include "dirauth/shared_random_state.h"
  13. #include "routerkeys.h"
  14. #include "routerlist.h"
  15. #include "router.h"
  16. #include "routerparse.h"
  17. #include "shared_random_common.h"
  18. #include "networkstatus.h"
  19. #include "log_test_helpers.h"
  20. #include "voting_schedule.h"
  21. static authority_cert_t *mock_cert;
  22. static authority_cert_t *
  23. get_my_v3_authority_cert_m(void)
  24. {
  25. tor_assert(mock_cert);
  26. return mock_cert;
  27. }
  28. static dir_server_t ds;
  29. static dir_server_t *
  30. trusteddirserver_get_by_v3_auth_digest_m(const char *digest)
  31. {
  32. (void) digest;
  33. /* The shared random code only need to know if a valid pointer to a dir
  34. * server object has been found so this is safe because it won't use the
  35. * pointer at all never. */
  36. return &ds;
  37. }
  38. /* Setup a minimal dirauth environment by initializing the SR state and
  39. * making sure the options are set to be an authority directory. */
  40. static void
  41. init_authority_state(void)
  42. {
  43. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  44. or_options_t *options = get_options_mutable();
  45. mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  46. tt_assert(mock_cert);
  47. options->AuthoritativeDir = 1;
  48. tt_int_op(load_ed_keys(options, time(NULL)), OP_GE, 0);
  49. sr_state_init(0, 0);
  50. /* It's possible a commit has been generated in our state depending on
  51. * the phase we are currently in which uses "now" as the starting
  52. * timestamp. Delete it before we do any testing below. */
  53. sr_state_delete_commits();
  54. done:
  55. UNMOCK(get_my_v3_authority_cert);
  56. }
  57. static void
  58. test_get_sr_protocol_phase(void *arg)
  59. {
  60. time_t the_time;
  61. sr_phase_t phase;
  62. int retval;
  63. (void) arg;
  64. /* Initialize SR state */
  65. init_authority_state();
  66. {
  67. retval = parse_rfc1123_time("Wed, 20 Apr 2015 23:59:00 UTC", &the_time);
  68. tt_int_op(retval, OP_EQ, 0);
  69. phase = get_sr_protocol_phase(the_time);
  70. tt_int_op(phase, OP_EQ, SR_PHASE_REVEAL);
  71. }
  72. {
  73. retval = parse_rfc1123_time("Wed, 20 Apr 2015 00:00:00 UTC", &the_time);
  74. tt_int_op(retval, OP_EQ, 0);
  75. phase = get_sr_protocol_phase(the_time);
  76. tt_int_op(phase, OP_EQ, SR_PHASE_COMMIT);
  77. }
  78. {
  79. retval = parse_rfc1123_time("Wed, 20 Apr 2015 00:00:01 UTC", &the_time);
  80. tt_int_op(retval, OP_EQ, 0);
  81. phase = get_sr_protocol_phase(the_time);
  82. tt_int_op(phase, OP_EQ, SR_PHASE_COMMIT);
  83. }
  84. {
  85. retval = parse_rfc1123_time("Wed, 20 Apr 2015 11:59:00 UTC", &the_time);
  86. tt_int_op(retval, OP_EQ, 0);
  87. phase = get_sr_protocol_phase(the_time);
  88. tt_int_op(phase, OP_EQ, SR_PHASE_COMMIT);
  89. }
  90. {
  91. retval = parse_rfc1123_time("Wed, 20 Apr 2015 12:00:00 UTC", &the_time);
  92. tt_int_op(retval, OP_EQ, 0);
  93. phase = get_sr_protocol_phase(the_time);
  94. tt_int_op(phase, OP_EQ, SR_PHASE_REVEAL);
  95. }
  96. {
  97. retval = parse_rfc1123_time("Wed, 20 Apr 2015 12:00:01 UTC", &the_time);
  98. tt_int_op(retval, OP_EQ, 0);
  99. phase = get_sr_protocol_phase(the_time);
  100. tt_int_op(phase, OP_EQ, SR_PHASE_REVEAL);
  101. }
  102. {
  103. retval = parse_rfc1123_time("Wed, 20 Apr 2015 13:00:00 UTC", &the_time);
  104. tt_int_op(retval, OP_EQ, 0);
  105. phase = get_sr_protocol_phase(the_time);
  106. tt_int_op(phase, OP_EQ, SR_PHASE_REVEAL);
  107. }
  108. done:
  109. ;
  110. }
  111. static networkstatus_t mock_consensus;
  112. /* Mock function to immediately return our local 'mock_consensus'. */
  113. static networkstatus_t *
  114. mock_networkstatus_get_live_consensus(time_t now)
  115. {
  116. (void) now;
  117. return &mock_consensus;
  118. }
  119. static void
  120. test_get_state_valid_until_time(void *arg)
  121. {
  122. time_t current_time;
  123. time_t valid_until_time;
  124. char tbuf[ISO_TIME_LEN + 1];
  125. int retval;
  126. (void) arg;
  127. MOCK(networkstatus_get_live_consensus,
  128. mock_networkstatus_get_live_consensus);
  129. retval = parse_rfc1123_time("Mon, 20 Apr 2015 01:00:00 UTC",
  130. &mock_consensus.fresh_until);
  131. tt_int_op(retval, OP_EQ, 0);
  132. retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
  133. &mock_consensus.valid_after);
  134. tt_int_op(retval, OP_EQ, 0);
  135. {
  136. /* Get the valid until time if called at 00:00:01 */
  137. retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:01 UTC",
  138. &current_time);
  139. tt_int_op(retval, OP_EQ, 0);
  140. voting_schedule_recalculate_timing(get_options(), current_time);
  141. valid_until_time = get_state_valid_until_time(current_time);
  142. /* Compare it with the correct result */
  143. format_iso_time(tbuf, valid_until_time);
  144. tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
  145. }
  146. {
  147. retval = parse_rfc1123_time("Mon, 20 Apr 2015 19:22:00 UTC",
  148. &current_time);
  149. tt_int_op(retval, OP_EQ, 0);
  150. voting_schedule_recalculate_timing(get_options(), current_time);
  151. valid_until_time = get_state_valid_until_time(current_time);
  152. format_iso_time(tbuf, valid_until_time);
  153. tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
  154. }
  155. {
  156. retval = parse_rfc1123_time("Mon, 20 Apr 2015 23:59:00 UTC",
  157. &current_time);
  158. tt_int_op(retval, OP_EQ, 0);
  159. voting_schedule_recalculate_timing(get_options(), current_time);
  160. valid_until_time = get_state_valid_until_time(current_time);
  161. format_iso_time(tbuf, valid_until_time);
  162. tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
  163. }
  164. {
  165. retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
  166. &current_time);
  167. tt_int_op(retval, OP_EQ, 0);
  168. voting_schedule_recalculate_timing(get_options(), current_time);
  169. valid_until_time = get_state_valid_until_time(current_time);
  170. format_iso_time(tbuf, valid_until_time);
  171. tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
  172. }
  173. done:
  174. UNMOCK(networkstatus_get_live_consensus);
  175. }
  176. /** Test the function that calculates the start time of the current SRV
  177. * protocol run. */
  178. static void
  179. test_get_start_time_of_current_run(void *arg)
  180. {
  181. int retval;
  182. char tbuf[ISO_TIME_LEN + 1];
  183. time_t current_time, run_start_time;
  184. (void) arg;
  185. MOCK(networkstatus_get_live_consensus,
  186. mock_networkstatus_get_live_consensus);
  187. retval = parse_rfc1123_time("Mon, 20 Apr 2015 01:00:00 UTC",
  188. &mock_consensus.fresh_until);
  189. tt_int_op(retval, OP_EQ, 0);
  190. retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
  191. &mock_consensus.valid_after);
  192. tt_int_op(retval, OP_EQ, 0);
  193. {
  194. /* Get start time if called at 00:00:01 */
  195. retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:01 UTC",
  196. &current_time);
  197. tt_int_op(retval, OP_EQ, 0);
  198. voting_schedule_recalculate_timing(get_options(), current_time);
  199. run_start_time =
  200. sr_state_get_start_time_of_current_protocol_run(current_time);
  201. /* Compare it with the correct result */
  202. format_iso_time(tbuf, run_start_time);
  203. tt_str_op("2015-04-20 00:00:00", OP_EQ, tbuf);
  204. }
  205. {
  206. retval = parse_rfc1123_time("Mon, 20 Apr 2015 23:59:59 UTC",
  207. &current_time);
  208. tt_int_op(retval, OP_EQ, 0);
  209. voting_schedule_recalculate_timing(get_options(), current_time);
  210. run_start_time =
  211. sr_state_get_start_time_of_current_protocol_run(current_time);
  212. /* Compare it with the correct result */
  213. format_iso_time(tbuf, run_start_time);
  214. tt_str_op("2015-04-20 00:00:00", OP_EQ, tbuf);
  215. }
  216. {
  217. retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
  218. &current_time);
  219. tt_int_op(retval, OP_EQ, 0);
  220. voting_schedule_recalculate_timing(get_options(), current_time);
  221. run_start_time =
  222. sr_state_get_start_time_of_current_protocol_run(current_time);
  223. /* Compare it with the correct result */
  224. format_iso_time(tbuf, run_start_time);
  225. tt_str_op("2015-04-20 00:00:00", OP_EQ, tbuf);
  226. }
  227. /* Next test is testing it without a consensus to use the testing voting
  228. * interval . */
  229. UNMOCK(networkstatus_get_live_consensus);
  230. /* Now let's alter the voting schedule and check the correctness of the
  231. * function. Voting interval of 10 seconds, means that an SRV protocol run
  232. * takes 10 seconds * 24 rounds = 4 mins */
  233. {
  234. or_options_t *options = get_options_mutable();
  235. options->V3AuthVotingInterval = 10;
  236. options->TestingV3AuthInitialVotingInterval = 10;
  237. retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:15:32 UTC",
  238. &current_time);
  239. tt_int_op(retval, OP_EQ, 0);
  240. voting_schedule_recalculate_timing(get_options(), current_time);
  241. run_start_time =
  242. sr_state_get_start_time_of_current_protocol_run(current_time);
  243. /* Compare it with the correct result */
  244. format_iso_time(tbuf, run_start_time);
  245. tt_str_op("2015-04-20 00:12:00", OP_EQ, tbuf);
  246. }
  247. done:
  248. ;
  249. }
  250. /** Do some rudimentary consistency checks between the functions that
  251. * understand the shared random protocol schedule */
  252. static void
  253. test_get_start_time_functions(void *arg)
  254. {
  255. (void) arg;
  256. int retval;
  257. MOCK(networkstatus_get_live_consensus,
  258. mock_networkstatus_get_live_consensus);
  259. retval = parse_rfc1123_time("Mon, 20 Apr 2015 01:00:00 UTC",
  260. &mock_consensus.fresh_until);
  261. tt_int_op(retval, OP_EQ, 0);
  262. retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
  263. &mock_consensus.valid_after);
  264. tt_int_op(retval, OP_EQ, 0);
  265. time_t now = mock_consensus.valid_after;
  266. voting_schedule_recalculate_timing(get_options(), now);
  267. time_t start_time_of_protocol_run =
  268. sr_state_get_start_time_of_current_protocol_run(now);
  269. tt_assert(start_time_of_protocol_run);
  270. /* Check that the round start time of the beginning of the run, is itself */
  271. tt_int_op(get_start_time_of_current_round(), OP_EQ,
  272. start_time_of_protocol_run);
  273. done:
  274. UNMOCK(networkstatus_get_live_consensus);
  275. }
  276. static void
  277. test_get_sr_protocol_duration(void *arg)
  278. {
  279. (void) arg;
  280. /* Check that by default an SR phase is 12 hours */
  281. tt_int_op(sr_state_get_phase_duration(), OP_EQ, 12*60*60);
  282. tt_int_op(sr_state_get_protocol_run_duration(), OP_EQ, 24*60*60);
  283. /* Now alter the voting interval and check that the SR phase is 2 mins long
  284. * if voting happens every 10 seconds (10*12 seconds = 2 mins) */
  285. or_options_t *options = get_options_mutable();
  286. options->V3AuthVotingInterval = 10;
  287. tt_int_op(sr_state_get_phase_duration(), OP_EQ, 2*60);
  288. tt_int_op(sr_state_get_protocol_run_duration(), OP_EQ, 4*60);
  289. done: ;
  290. }
  291. /* In this test we are going to generate a sr_commit_t object and validate
  292. * it. We first generate our values, and then we parse them as if they were
  293. * received from the network. After we parse both the commit and the reveal,
  294. * we verify that they indeed match. */
  295. static void
  296. test_sr_commit(void *arg)
  297. {
  298. authority_cert_t *auth_cert = NULL;
  299. time_t now = time(NULL);
  300. sr_commit_t *our_commit = NULL;
  301. smartlist_t *args = smartlist_new();
  302. sr_commit_t *parsed_commit = NULL;
  303. (void) arg;
  304. { /* Setup a minimal dirauth environment for this test */
  305. or_options_t *options = get_options_mutable();
  306. auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  307. tt_assert(auth_cert);
  308. options->AuthoritativeDir = 1;
  309. tt_int_op(load_ed_keys(options, time(NULL)), OP_GE, 0);
  310. }
  311. /* Generate our commit object and validate it has the appropriate field
  312. * that we can then use to build a representation that we'll find in a
  313. * vote coming from the network. */
  314. {
  315. sr_commit_t test_commit;
  316. our_commit = sr_generate_our_commit(now, auth_cert);
  317. tt_assert(our_commit);
  318. /* Default and only supported algorithm for now. */
  319. tt_assert(our_commit->alg == DIGEST_SHA3_256);
  320. /* We should have a reveal value. */
  321. tt_assert(commit_has_reveal_value(our_commit));
  322. /* We should have a random value. */
  323. tt_assert(!tor_mem_is_zero((char *) our_commit->random_number,
  324. sizeof(our_commit->random_number)));
  325. /* Commit and reveal timestamp should be the same. */
  326. tt_u64_op(our_commit->commit_ts, OP_EQ, our_commit->reveal_ts);
  327. /* We should have a hashed reveal. */
  328. tt_assert(!tor_mem_is_zero(our_commit->hashed_reveal,
  329. sizeof(our_commit->hashed_reveal)));
  330. /* Do we have a valid encoded commit and reveal. Note the following only
  331. * tests if the generated values are correct. Their could be a bug in
  332. * the decode function but we test them separately. */
  333. tt_int_op(0, OP_EQ, reveal_decode(our_commit->encoded_reveal,
  334. &test_commit));
  335. tt_int_op(0, OP_EQ, commit_decode(our_commit->encoded_commit,
  336. &test_commit));
  337. tt_int_op(0, OP_EQ, verify_commit_and_reveal(our_commit));
  338. }
  339. /* Let's make sure our verify commit and reveal function works. We'll
  340. * make it fail a bit with known failure case. */
  341. {
  342. /* Copy our commit so we don't alter it for the rest of testing. */
  343. sr_commit_t test_commit;
  344. memcpy(&test_commit, our_commit, sizeof(test_commit));
  345. /* Timestamp MUST match. */
  346. test_commit.commit_ts = test_commit.reveal_ts - 42;
  347. setup_full_capture_of_logs(LOG_WARN);
  348. tt_int_op(-1, OP_EQ, verify_commit_and_reveal(&test_commit));
  349. expect_log_msg_containing("doesn't match reveal timestamp");
  350. teardown_capture_of_logs();
  351. memcpy(&test_commit, our_commit, sizeof(test_commit));
  352. tt_int_op(0, OP_EQ, verify_commit_and_reveal(&test_commit));
  353. /* Hashed reveal must match the H(encoded_reveal). */
  354. memset(test_commit.hashed_reveal, 'X',
  355. sizeof(test_commit.hashed_reveal));
  356. setup_full_capture_of_logs(LOG_WARN);
  357. tt_int_op(-1, OP_EQ, verify_commit_and_reveal(&test_commit));
  358. expect_single_log_msg_containing("doesn't match the commit value");
  359. teardown_capture_of_logs();
  360. memcpy(&test_commit, our_commit, sizeof(test_commit));
  361. tt_int_op(0, OP_EQ, verify_commit_and_reveal(&test_commit));
  362. }
  363. /* We'll build a list of values from our commit that our parsing function
  364. * takes from a vote line and see if we can parse it correctly. */
  365. {
  366. smartlist_add_strdup(args, "1");
  367. smartlist_add_strdup(args,
  368. crypto_digest_algorithm_get_name(our_commit->alg));
  369. smartlist_add_strdup(args, sr_commit_get_rsa_fpr(our_commit));
  370. smartlist_add_strdup(args, our_commit->encoded_commit);
  371. smartlist_add_strdup(args, our_commit->encoded_reveal);
  372. parsed_commit = sr_parse_commit(args);
  373. tt_assert(parsed_commit);
  374. /* That parsed commit should be _EXACTLY_ like our original commit (we
  375. * have to explicitly set the valid flag though). */
  376. parsed_commit->valid = 1;
  377. tt_mem_op(parsed_commit, OP_EQ, our_commit, sizeof(*parsed_commit));
  378. /* Cleanup */
  379. }
  380. done:
  381. teardown_capture_of_logs();
  382. SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
  383. smartlist_free(args);
  384. sr_commit_free(our_commit);
  385. sr_commit_free(parsed_commit);
  386. authority_cert_free(auth_cert);
  387. }
  388. /* Test the encoding and decoding function for commit and reveal values. */
  389. static void
  390. test_encoding(void *arg)
  391. {
  392. (void) arg;
  393. int ret;
  394. /* Random number is 32 bytes. */
  395. char raw_rand[32];
  396. time_t ts = 1454333590;
  397. char hashed_rand[DIGEST256_LEN], hashed_reveal[DIGEST256_LEN];
  398. sr_commit_t parsed_commit;
  399. /* Those values were generated by sr_commit_calc_ref.py where the random
  400. * value is 32 'A' and timestamp is the one in ts. */
  401. static const char *encoded_reveal =
  402. "AAAAAFavXpZJxbwTupvaJCTeIUCQmOPxAMblc7ChL5H2nZKuGchdaA==";
  403. static const char *encoded_commit =
  404. "AAAAAFavXpbkBMzMQG7aNoaGLFNpm2Wkk1ozXhuWWqL//GynltxVAg==";
  405. /* Set up our raw random bytes array. */
  406. memset(raw_rand, 'A', sizeof(raw_rand));
  407. /* Hash random number because we don't expose bytes of the RNG. */
  408. ret = crypto_digest256(hashed_rand, raw_rand,
  409. sizeof(raw_rand), SR_DIGEST_ALG);
  410. tt_int_op(0, OP_EQ, ret);
  411. /* Hash reveal value. */
  412. tt_int_op(SR_REVEAL_BASE64_LEN, OP_EQ, strlen(encoded_reveal));
  413. ret = crypto_digest256(hashed_reveal, encoded_reveal,
  414. strlen(encoded_reveal), SR_DIGEST_ALG);
  415. tt_int_op(0, OP_EQ, ret);
  416. tt_int_op(SR_COMMIT_BASE64_LEN, OP_EQ, strlen(encoded_commit));
  417. /* Test our commit/reveal decode functions. */
  418. {
  419. /* Test the reveal encoded value. */
  420. tt_int_op(0, OP_EQ, reveal_decode(encoded_reveal, &parsed_commit));
  421. tt_u64_op(ts, OP_EQ, parsed_commit.reveal_ts);
  422. tt_mem_op(hashed_rand, OP_EQ, parsed_commit.random_number,
  423. sizeof(hashed_rand));
  424. /* Test the commit encoded value. */
  425. memset(&parsed_commit, 0, sizeof(parsed_commit));
  426. tt_int_op(0, OP_EQ, commit_decode(encoded_commit, &parsed_commit));
  427. tt_u64_op(ts, OP_EQ, parsed_commit.commit_ts);
  428. tt_mem_op(encoded_commit, OP_EQ, parsed_commit.encoded_commit,
  429. sizeof(parsed_commit.encoded_commit));
  430. tt_mem_op(hashed_reveal, OP_EQ, parsed_commit.hashed_reveal,
  431. sizeof(hashed_reveal));
  432. }
  433. /* Test our commit/reveal encode functions. */
  434. {
  435. /* Test the reveal encode. */
  436. char encoded[SR_REVEAL_BASE64_LEN + 1];
  437. parsed_commit.reveal_ts = ts;
  438. memcpy(parsed_commit.random_number, hashed_rand,
  439. sizeof(parsed_commit.random_number));
  440. ret = reveal_encode(&parsed_commit, encoded, sizeof(encoded));
  441. tt_int_op(SR_REVEAL_BASE64_LEN, OP_EQ, ret);
  442. tt_mem_op(encoded_reveal, OP_EQ, encoded, strlen(encoded_reveal));
  443. }
  444. {
  445. /* Test the commit encode. */
  446. char encoded[SR_COMMIT_BASE64_LEN + 1];
  447. parsed_commit.commit_ts = ts;
  448. memcpy(parsed_commit.hashed_reveal, hashed_reveal,
  449. sizeof(parsed_commit.hashed_reveal));
  450. ret = commit_encode(&parsed_commit, encoded, sizeof(encoded));
  451. tt_int_op(SR_COMMIT_BASE64_LEN, OP_EQ, ret);
  452. tt_mem_op(encoded_commit, OP_EQ, encoded, strlen(encoded_commit));
  453. }
  454. done:
  455. ;
  456. }
  457. /** Setup some SRVs in our SR state. If <b>also_current</b> is set, then set
  458. * both current and previous SRVs.
  459. * Helper of test_vote() and test_sr_compute_srv(). */
  460. static void
  461. test_sr_setup_srv(int also_current)
  462. {
  463. sr_srv_t *srv = tor_malloc_zero(sizeof(sr_srv_t));
  464. srv->num_reveals = 42;
  465. memcpy(srv->value,
  466. "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
  467. sizeof(srv->value));
  468. sr_state_set_previous_srv(srv);
  469. if (also_current) {
  470. srv = tor_malloc_zero(sizeof(sr_srv_t));
  471. srv->num_reveals = 128;
  472. memcpy(srv->value,
  473. "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN",
  474. sizeof(srv->value));
  475. sr_state_set_current_srv(srv);
  476. }
  477. }
  478. /* Test anything that has to do with SR protocol and vote. */
  479. static void
  480. test_vote(void *arg)
  481. {
  482. int ret;
  483. time_t now = time(NULL);
  484. sr_commit_t *our_commit = NULL;
  485. (void) arg;
  486. MOCK(trusteddirserver_get_by_v3_auth_digest,
  487. trusteddirserver_get_by_v3_auth_digest_m);
  488. { /* Setup a minimal dirauth environment for this test */
  489. init_authority_state();
  490. /* Set ourself in reveal phase so we can parse the reveal value in the
  491. * vote as well. */
  492. set_sr_phase(SR_PHASE_REVEAL);
  493. }
  494. /* Generate our commit object and validate it has the appropriate field
  495. * that we can then use to build a representation that we'll find in a
  496. * vote coming from the network. */
  497. {
  498. sr_commit_t *saved_commit;
  499. our_commit = sr_generate_our_commit(now, mock_cert);
  500. tt_assert(our_commit);
  501. sr_state_add_commit(our_commit);
  502. /* Make sure it's there. */
  503. saved_commit = sr_state_get_commit(our_commit->rsa_identity);
  504. tt_assert(saved_commit);
  505. }
  506. /* Also setup the SRVs */
  507. test_sr_setup_srv(1);
  508. { /* Now test the vote generation */
  509. smartlist_t *chunks = smartlist_new();
  510. smartlist_t *tokens = smartlist_new();
  511. /* Get our vote line and validate it. */
  512. char *lines = sr_get_string_for_vote();
  513. tt_assert(lines);
  514. /* Split the lines. We expect 2 here. */
  515. ret = smartlist_split_string(chunks, lines, "\n", SPLIT_IGNORE_BLANK, 0);
  516. tt_int_op(ret, OP_EQ, 4);
  517. tt_str_op(smartlist_get(chunks, 0), OP_EQ, "shared-rand-participate");
  518. /* Get our commitment line and will validate it against our commit. The
  519. * format is as follow:
  520. * "shared-rand-commitment" SP version SP algname SP identity
  521. * SP COMMIT [SP REVEAL] NL
  522. */
  523. char *commit_line = smartlist_get(chunks, 1);
  524. tt_assert(commit_line);
  525. ret = smartlist_split_string(tokens, commit_line, " ", 0, 0);
  526. tt_int_op(ret, OP_EQ, 6);
  527. tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-commit");
  528. tt_str_op(smartlist_get(tokens, 1), OP_EQ, "1");
  529. tt_str_op(smartlist_get(tokens, 2), OP_EQ,
  530. crypto_digest_algorithm_get_name(DIGEST_SHA3_256));
  531. char digest[DIGEST_LEN];
  532. base16_decode(digest, sizeof(digest), smartlist_get(tokens, 3),
  533. HEX_DIGEST_LEN);
  534. tt_mem_op(digest, OP_EQ, our_commit->rsa_identity, sizeof(digest));
  535. tt_str_op(smartlist_get(tokens, 4), OP_EQ, our_commit->encoded_commit);
  536. tt_str_op(smartlist_get(tokens, 5), OP_EQ, our_commit->encoded_reveal)
  537. ;
  538. /* Finally, does this vote line creates a valid commit object? */
  539. smartlist_t *args = smartlist_new();
  540. smartlist_add(args, smartlist_get(tokens, 1));
  541. smartlist_add(args, smartlist_get(tokens, 2));
  542. smartlist_add(args, smartlist_get(tokens, 3));
  543. smartlist_add(args, smartlist_get(tokens, 4));
  544. smartlist_add(args, smartlist_get(tokens, 5));
  545. sr_commit_t *parsed_commit = sr_parse_commit(args);
  546. tt_assert(parsed_commit);
  547. /* Set valid flag explicitly here to compare since it's not set by
  548. * simply parsing the commit. */
  549. parsed_commit->valid = 1;
  550. tt_mem_op(parsed_commit, OP_EQ, our_commit, sizeof(*our_commit));
  551. /* minor cleanup */
  552. SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
  553. smartlist_clear(tokens);
  554. /* Now test the previous SRV */
  555. char *prev_srv_line = smartlist_get(chunks, 2);
  556. tt_assert(prev_srv_line);
  557. ret = smartlist_split_string(tokens, prev_srv_line, " ", 0, 0);
  558. tt_int_op(ret, OP_EQ, 3);
  559. tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-previous-value");
  560. tt_str_op(smartlist_get(tokens, 1), OP_EQ, "42");
  561. tt_str_op(smartlist_get(tokens, 2), OP_EQ,
  562. "WlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlo=");
  563. /* minor cleanup */
  564. SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
  565. smartlist_clear(tokens);
  566. /* Now test the current SRV */
  567. char *current_srv_line = smartlist_get(chunks, 3);
  568. tt_assert(current_srv_line);
  569. ret = smartlist_split_string(tokens, current_srv_line, " ", 0, 0);
  570. tt_int_op(ret, OP_EQ, 3);
  571. tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-current-value");
  572. tt_str_op(smartlist_get(tokens, 1), OP_EQ, "128");
  573. tt_str_op(smartlist_get(tokens, 2), OP_EQ,
  574. "Tk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk4=");
  575. /* Clean up */
  576. sr_commit_free(parsed_commit);
  577. SMARTLIST_FOREACH(chunks, char *, s, tor_free(s));
  578. smartlist_free(chunks);
  579. SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
  580. smartlist_free(tokens);
  581. smartlist_clear(args);
  582. smartlist_free(args);
  583. tor_free(lines);
  584. }
  585. done:
  586. sr_commit_free(our_commit);
  587. UNMOCK(trusteddirserver_get_by_v3_auth_digest);
  588. }
  589. static const char *sr_state_str = "Version 1\n"
  590. "TorVersion 0.2.9.0-alpha-dev\n"
  591. "ValidAfter 2037-04-19 07:16:00\n"
  592. "ValidUntil 2037-04-20 07:16:00\n"
  593. "Commit 1 sha3-256 FA3CEC2C99DC68D3166B9B6E4FA21A4026C2AB1C "
  594. "7M8GdubCAAdh7WUG0DiwRyxTYRKji7HATa7LLJEZ/UAAAAAAVmfUSg== "
  595. "AAAAAFZn1EojfIheIw42bjK3VqkpYyjsQFSbv/dxNna3Q8hUEPKpOw==\n"
  596. "Commit 1 sha3-256 41E89EDFBFBA44983E21F18F2230A4ECB5BFB543 "
  597. "17aUsYuMeRjd2N1r8yNyg7aHqRa6gf4z7QPoxxAZbp0AAAAAVmfUSg==\n"
  598. "Commit 1 sha3-256 36637026573A04110CF3E6B1D201FB9A98B88734 "
  599. "DDDYtripvdOU+XPEUm5xpU64d9IURSds1xSwQsgeB8oAAAAAVmfUSg==\n"
  600. "SharedRandPreviousValue 4 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqo=\n"
  601. "SharedRandCurrentValue 3 8dWeW12KEzTGEiLGgO1UVJ7Z91CekoRcxt6Q9KhnOFI=\n";
  602. /** Create an SR disk state, parse it and validate that the parsing went
  603. * well. Yes! */
  604. static void
  605. test_state_load_from_disk(void *arg)
  606. {
  607. int ret;
  608. char *dir = tor_strdup(get_fname("test_sr_state"));
  609. char *sr_state_path = tor_strdup(get_fname("test_sr_state/sr_state"));
  610. sr_state_t *the_sr_state = NULL;
  611. (void) arg;
  612. MOCK(trusteddirserver_get_by_v3_auth_digest,
  613. trusteddirserver_get_by_v3_auth_digest_m);
  614. /* First try with a nonexistent path. */
  615. ret = disk_state_load_from_disk_impl("NONEXISTENTNONEXISTENT");
  616. tt_int_op(ret, OP_EQ, -ENOENT);
  617. /* Now create a mock state directory and state file */
  618. #ifdef _WIN32
  619. ret = mkdir(dir);
  620. #else
  621. ret = mkdir(dir, 0700);
  622. #endif
  623. tt_int_op(ret, OP_EQ, 0);
  624. ret = write_str_to_file(sr_state_path, sr_state_str, 0);
  625. tt_int_op(ret, OP_EQ, 0);
  626. /* Try to load the directory itself. Should fail. */
  627. ret = disk_state_load_from_disk_impl(dir);
  628. tt_int_op(ret, OP_LT, 0);
  629. /* State should be non-existent at this point. */
  630. the_sr_state = get_sr_state();
  631. tt_ptr_op(the_sr_state, OP_EQ, NULL);
  632. /* Now try to load the correct file! */
  633. ret = disk_state_load_from_disk_impl(sr_state_path);
  634. tt_int_op(ret, OP_EQ, 0);
  635. /* Check the content of the state */
  636. /* XXX check more deeply!!! */
  637. the_sr_state = get_sr_state();
  638. tt_assert(the_sr_state);
  639. tt_assert(the_sr_state->version == 1);
  640. tt_assert(digestmap_size(the_sr_state->commits) == 3);
  641. tt_assert(the_sr_state->current_srv);
  642. tt_assert(the_sr_state->current_srv->num_reveals == 3);
  643. tt_assert(the_sr_state->previous_srv);
  644. /* XXX Now also try loading corrupted state files and make sure parsing
  645. fails */
  646. done:
  647. tor_free(dir);
  648. tor_free(sr_state_path);
  649. UNMOCK(trusteddirserver_get_by_v3_auth_digest);
  650. }
  651. /** Generate three specially crafted commits (based on the test
  652. * vector at sr_srv_calc_ref.py). Helper of test_sr_compute_srv(). */
  653. static void
  654. test_sr_setup_commits(void)
  655. {
  656. time_t now = time(NULL);
  657. sr_commit_t *commit_a, *commit_b, *commit_c, *commit_d;
  658. sr_commit_t *place_holder = tor_malloc_zero(sizeof(*place_holder));
  659. authority_cert_t *auth_cert = NULL;
  660. { /* Setup a minimal dirauth environment for this test */
  661. or_options_t *options = get_options_mutable();
  662. auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  663. tt_assert(auth_cert);
  664. options->AuthoritativeDir = 1;
  665. tt_int_op(0, OP_EQ, load_ed_keys(options, now));
  666. }
  667. /* Generate three dummy commits according to sr_srv_calc_ref.py . Then
  668. register them to the SR state. Also register a fourth commit 'd' with no
  669. reveal info, to make sure that it will get ignored during SRV
  670. calculation. */
  671. { /* Commit from auth 'a' */
  672. commit_a = sr_generate_our_commit(now, auth_cert);
  673. tt_assert(commit_a);
  674. /* Do some surgery on the commit */
  675. memset(commit_a->rsa_identity, 'A', sizeof(commit_a->rsa_identity));
  676. base16_encode(commit_a->rsa_identity_hex,
  677. sizeof(commit_a->rsa_identity_hex), commit_a->rsa_identity,
  678. sizeof(commit_a->rsa_identity));
  679. strlcpy(commit_a->encoded_reveal,
  680. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  681. sizeof(commit_a->encoded_reveal));
  682. memcpy(commit_a->hashed_reveal,
  683. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  684. sizeof(commit_a->hashed_reveal));
  685. }
  686. { /* Commit from auth 'b' */
  687. commit_b = sr_generate_our_commit(now, auth_cert);
  688. tt_assert(commit_b);
  689. /* Do some surgery on the commit */
  690. memset(commit_b->rsa_identity, 'B', sizeof(commit_b->rsa_identity));
  691. base16_encode(commit_b->rsa_identity_hex,
  692. sizeof(commit_b->rsa_identity_hex), commit_b->rsa_identity,
  693. sizeof(commit_b->rsa_identity));
  694. strlcpy(commit_b->encoded_reveal,
  695. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
  696. sizeof(commit_b->encoded_reveal));
  697. memcpy(commit_b->hashed_reveal,
  698. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
  699. sizeof(commit_b->hashed_reveal));
  700. }
  701. { /* Commit from auth 'c' */
  702. commit_c = sr_generate_our_commit(now, auth_cert);
  703. tt_assert(commit_c);
  704. /* Do some surgery on the commit */
  705. memset(commit_c->rsa_identity, 'C', sizeof(commit_c->rsa_identity));
  706. base16_encode(commit_c->rsa_identity_hex,
  707. sizeof(commit_c->rsa_identity_hex), commit_c->rsa_identity,
  708. sizeof(commit_c->rsa_identity));
  709. strlcpy(commit_c->encoded_reveal,
  710. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
  711. sizeof(commit_c->encoded_reveal));
  712. memcpy(commit_c->hashed_reveal,
  713. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
  714. sizeof(commit_c->hashed_reveal));
  715. }
  716. { /* Commit from auth 'd' */
  717. commit_d = sr_generate_our_commit(now, auth_cert);
  718. tt_assert(commit_d);
  719. /* Do some surgery on the commit */
  720. memset(commit_d->rsa_identity, 'D', sizeof(commit_d->rsa_identity));
  721. base16_encode(commit_d->rsa_identity_hex,
  722. sizeof(commit_d->rsa_identity_hex), commit_d->rsa_identity,
  723. sizeof(commit_d->rsa_identity));
  724. strlcpy(commit_d->encoded_reveal,
  725. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
  726. sizeof(commit_d->encoded_reveal));
  727. memcpy(commit_d->hashed_reveal,
  728. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
  729. sizeof(commit_d->hashed_reveal));
  730. /* Clean up its reveal info */
  731. memcpy(place_holder, commit_d, sizeof(*place_holder));
  732. memset(commit_d->encoded_reveal, 0, sizeof(commit_d->encoded_reveal));
  733. tt_assert(!commit_has_reveal_value(commit_d));
  734. }
  735. /* Register commits to state (during commit phase) */
  736. set_sr_phase(SR_PHASE_COMMIT);
  737. save_commit_to_state(commit_a);
  738. save_commit_to_state(commit_b);
  739. save_commit_to_state(commit_c);
  740. save_commit_to_state(commit_d);
  741. tt_int_op(digestmap_size(get_sr_state()->commits), OP_EQ, 4);
  742. /* Now during REVEAL phase save commit D by restoring its reveal. */
  743. set_sr_phase(SR_PHASE_REVEAL);
  744. save_commit_to_state(place_holder);
  745. tt_str_op(commit_d->encoded_reveal, OP_EQ,
  746. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
  747. /* Go back to an empty encoded reveal value. */
  748. memset(commit_d->encoded_reveal, 0, sizeof(commit_d->encoded_reveal));
  749. memset(commit_d->random_number, 0, sizeof(commit_d->random_number));
  750. tt_assert(!commit_has_reveal_value(commit_d));
  751. done:
  752. authority_cert_free(auth_cert);
  753. }
  754. /** Verify that the SRV generation procedure is proper by testing it against
  755. * the test vector from ./sr_srv_calc_ref.py. */
  756. static void
  757. test_sr_compute_srv(void *arg)
  758. {
  759. (void) arg;
  760. const sr_srv_t *current_srv = NULL;
  761. #define SRV_TEST_VECTOR \
  762. "2A9B1D6237DAB312A40F575DA85C147663E7ED3F80E9555395F15B515C74253D"
  763. MOCK(trusteddirserver_get_by_v3_auth_digest,
  764. trusteddirserver_get_by_v3_auth_digest_m);
  765. init_authority_state();
  766. /* Setup the commits for this unittest */
  767. test_sr_setup_commits();
  768. test_sr_setup_srv(0);
  769. /* Now switch to reveal phase */
  770. set_sr_phase(SR_PHASE_REVEAL);
  771. /* Compute the SRV */
  772. sr_compute_srv();
  773. /* Check the result against the test vector */
  774. current_srv = sr_state_get_current_srv();
  775. tt_assert(current_srv);
  776. tt_u64_op(current_srv->num_reveals, OP_EQ, 3);
  777. tt_str_op(hex_str((char*)current_srv->value, 32),
  778. OP_EQ,
  779. SRV_TEST_VECTOR);
  780. done:
  781. UNMOCK(trusteddirserver_get_by_v3_auth_digest);
  782. }
  783. /** Return a minimal vote document with a current SRV value set to
  784. * <b>srv</b>. */
  785. static networkstatus_t *
  786. get_test_vote_with_curr_srv(const char *srv)
  787. {
  788. networkstatus_t *vote = tor_malloc_zero(sizeof(networkstatus_t));
  789. vote->type = NS_TYPE_VOTE;
  790. vote->sr_info.participate = 1;
  791. vote->sr_info.current_srv = tor_malloc_zero(sizeof(sr_srv_t));
  792. vote->sr_info.current_srv->num_reveals = 42;
  793. memcpy(vote->sr_info.current_srv->value,
  794. srv,
  795. sizeof(vote->sr_info.current_srv->value));
  796. return vote;
  797. }
  798. /* Test the function that picks the right SRV given a bunch of votes. Make sure
  799. * that the function returns an SRV iff the majority/agreement requirements are
  800. * met. */
  801. static void
  802. test_sr_get_majority_srv_from_votes(void *arg)
  803. {
  804. sr_srv_t *chosen_srv;
  805. smartlist_t *votes = smartlist_new();
  806. #define SRV_1 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  807. #define SRV_2 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  808. (void) arg;
  809. init_authority_state();
  810. /* Make sure our SRV is fresh so we can consider the super majority with
  811. * the consensus params of number of agreements needed. */
  812. sr_state_set_fresh_srv();
  813. /* The test relies on the dirauth list being initialized. */
  814. clear_dir_servers();
  815. add_default_trusted_dir_authorities(V3_DIRINFO);
  816. { /* Prepare voting environment with just a single vote. */
  817. networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
  818. smartlist_add(votes, vote);
  819. }
  820. /* Since it's only one vote with an SRV, it should not achieve majority and
  821. hence no SRV will be returned. */
  822. chosen_srv = get_majority_srv_from_votes(votes, 1);
  823. tt_ptr_op(chosen_srv, OP_EQ, NULL);
  824. { /* Now put in 8 more votes. Let SRV_1 have majority. */
  825. int i;
  826. /* Now 7 votes believe in SRV_1 */
  827. for (i = 0; i < 3; i++) {
  828. networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
  829. smartlist_add(votes, vote);
  830. }
  831. /* and 2 votes believe in SRV_2 */
  832. for (i = 0; i < 2; i++) {
  833. networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_2);
  834. smartlist_add(votes, vote);
  835. }
  836. for (i = 0; i < 3; i++) {
  837. networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
  838. smartlist_add(votes, vote);
  839. }
  840. tt_int_op(smartlist_len(votes), OP_EQ, 9);
  841. }
  842. /* Now we achieve majority for SRV_1, but not the AuthDirNumSRVAgreements
  843. requirement. So still not picking an SRV. */
  844. set_num_srv_agreements(8);
  845. chosen_srv = get_majority_srv_from_votes(votes, 1);
  846. tt_ptr_op(chosen_srv, OP_EQ, NULL);
  847. /* We will now lower the AuthDirNumSRVAgreements requirement by tweaking the
  848. * consensus parameter and we will try again. This time it should work. */
  849. set_num_srv_agreements(7);
  850. chosen_srv = get_majority_srv_from_votes(votes, 1);
  851. tt_assert(chosen_srv);
  852. tt_u64_op(chosen_srv->num_reveals, OP_EQ, 42);
  853. tt_mem_op(chosen_srv->value, OP_EQ, SRV_1, sizeof(chosen_srv->value));
  854. done:
  855. SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
  856. networkstatus_vote_free(vote));
  857. smartlist_free(votes);
  858. }
  859. static void
  860. test_utils(void *arg)
  861. {
  862. (void) arg;
  863. /* Testing srv_dup(). */
  864. {
  865. sr_srv_t *srv = NULL, *dup_srv = NULL;
  866. const char *srv_value =
  867. "1BDB7C3E973936E4D13A49F37C859B3DC69C429334CF9412E3FEF6399C52D47A";
  868. srv = tor_malloc_zero(sizeof(*srv));
  869. srv->num_reveals = 42;
  870. memcpy(srv->value, srv_value, sizeof(srv->value));
  871. dup_srv = srv_dup(srv);
  872. tt_assert(dup_srv);
  873. tt_u64_op(dup_srv->num_reveals, OP_EQ, srv->num_reveals);
  874. tt_mem_op(dup_srv->value, OP_EQ, srv->value, sizeof(srv->value));
  875. tor_free(srv);
  876. tor_free(dup_srv);
  877. }
  878. /* Testing commitments_are_the_same(). Currently, the check is to test the
  879. * value of the encoded commit so let's make sure that actually works. */
  880. {
  881. /* Payload of 57 bytes that is the length of sr_commit_t->encoded_commit.
  882. * 56 bytes of payload and a NUL terminated byte at the end ('\x00')
  883. * which comes down to SR_COMMIT_BASE64_LEN + 1. */
  884. const char *payload =
  885. "\x5d\xb9\x60\xb6\xcc\x51\x68\x52\x31\xd9\x88\x88\x71\x71\xe0\x30"
  886. "\x59\x55\x7f\xcd\x61\xc0\x4b\x05\xb8\xcd\xc1\x48\xe9\xcd\x16\x1f"
  887. "\x70\x15\x0c\xfc\xd3\x1a\x75\xd0\x93\x6c\xc4\xe0\x5c\xbe\xe2\x18"
  888. "\xc7\xaf\x72\xb6\x7c\x9b\x52\x00";
  889. sr_commit_t commit1, commit2;
  890. memcpy(commit1.encoded_commit, payload, sizeof(commit1.encoded_commit));
  891. memcpy(commit2.encoded_commit, payload, sizeof(commit2.encoded_commit));
  892. tt_int_op(commitments_are_the_same(&commit1, &commit2), OP_EQ, 1);
  893. /* Let's corrupt one of them. */
  894. memset(commit1.encoded_commit, 'A', sizeof(commit1.encoded_commit));
  895. tt_int_op(commitments_are_the_same(&commit1, &commit2), OP_EQ, 0);
  896. }
  897. /* Testing commit_is_authoritative(). */
  898. {
  899. crypto_pk_t *k = crypto_pk_new();
  900. char digest[DIGEST_LEN];
  901. sr_commit_t commit;
  902. tt_assert(!crypto_pk_generate_key(k));
  903. tt_int_op(0, OP_EQ, crypto_pk_get_digest(k, digest));
  904. memcpy(commit.rsa_identity, digest, sizeof(commit.rsa_identity));
  905. tt_int_op(commit_is_authoritative(&commit, digest), OP_EQ, 1);
  906. /* Change the pubkey. */
  907. memset(commit.rsa_identity, 0, sizeof(commit.rsa_identity));
  908. tt_int_op(commit_is_authoritative(&commit, digest), OP_EQ, 0);
  909. crypto_pk_free(k);
  910. }
  911. /* Testing get_phase_str(). */
  912. {
  913. tt_str_op(get_phase_str(SR_PHASE_REVEAL), OP_EQ, "reveal");
  914. tt_str_op(get_phase_str(SR_PHASE_COMMIT), OP_EQ, "commit");
  915. }
  916. /* Testing phase transition */
  917. {
  918. init_authority_state();
  919. set_sr_phase(SR_PHASE_COMMIT);
  920. tt_int_op(is_phase_transition(SR_PHASE_REVEAL), OP_EQ, 1);
  921. tt_int_op(is_phase_transition(SR_PHASE_COMMIT), OP_EQ, 0);
  922. set_sr_phase(SR_PHASE_REVEAL);
  923. tt_int_op(is_phase_transition(SR_PHASE_REVEAL), OP_EQ, 0);
  924. tt_int_op(is_phase_transition(SR_PHASE_COMMIT), OP_EQ, 1);
  925. /* Junk. */
  926. tt_int_op(is_phase_transition(42), OP_EQ, 1);
  927. }
  928. done:
  929. return;
  930. }
  931. static void
  932. test_state_transition(void *arg)
  933. {
  934. sr_state_t *state = NULL;
  935. time_t now = time(NULL);
  936. (void) arg;
  937. { /* Setup a minimal dirauth environment for this test */
  938. init_authority_state();
  939. state = get_sr_state();
  940. tt_assert(state);
  941. }
  942. /* Test our state reset for a new protocol run. */
  943. {
  944. /* Add a commit to the state so we can test if the reset cleans the
  945. * commits. Also, change all params that we expect to be updated. */
  946. sr_commit_t *commit = sr_generate_our_commit(now, mock_cert);
  947. tt_assert(commit);
  948. sr_state_add_commit(commit);
  949. tt_int_op(digestmap_size(state->commits), OP_EQ, 1);
  950. /* Let's test our delete feature. */
  951. sr_state_delete_commits();
  952. tt_int_op(digestmap_size(state->commits), OP_EQ, 0);
  953. /* Add it back so we can continue the rest of the test because after
  954. * deletiong our commit will be freed so generate a new one. */
  955. commit = sr_generate_our_commit(now, mock_cert);
  956. tt_assert(commit);
  957. sr_state_add_commit(commit);
  958. tt_int_op(digestmap_size(state->commits), OP_EQ, 1);
  959. state->n_reveal_rounds = 42;
  960. state->n_commit_rounds = 43;
  961. state->n_protocol_runs = 44;
  962. reset_state_for_new_protocol_run(now);
  963. tt_int_op(state->n_reveal_rounds, OP_EQ, 0);
  964. tt_int_op(state->n_commit_rounds, OP_EQ, 0);
  965. tt_u64_op(state->n_protocol_runs, OP_EQ, 45);
  966. tt_int_op(digestmap_size(state->commits), OP_EQ, 0);
  967. }
  968. /* Test SRV rotation in our state. */
  969. {
  970. const sr_srv_t *cur, *prev;
  971. test_sr_setup_srv(1);
  972. cur = sr_state_get_current_srv();
  973. tt_assert(cur);
  974. /* After, current srv should be the previous and then set to NULL. */
  975. state_rotate_srv();
  976. prev = sr_state_get_previous_srv();
  977. tt_assert(prev == cur);
  978. tt_ptr_op(sr_state_get_current_srv(), OP_EQ, NULL);
  979. sr_state_clean_srvs();
  980. }
  981. /* New protocol run. */
  982. {
  983. const sr_srv_t *cur;
  984. /* Setup some new SRVs so we can confirm that a new protocol run
  985. * actually makes them rotate and compute new ones. */
  986. test_sr_setup_srv(1);
  987. cur = sr_state_get_current_srv();
  988. tt_assert(cur);
  989. set_sr_phase(SR_PHASE_REVEAL);
  990. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  991. new_protocol_run(now);
  992. UNMOCK(get_my_v3_authority_cert);
  993. /* Rotation happened. */
  994. tt_assert(sr_state_get_previous_srv() == cur);
  995. /* We are going into COMMIT phase so we had to rotate our SRVs. Usually
  996. * our current SRV would be NULL but a new protocol run should make us
  997. * compute a new SRV. */
  998. tt_assert(sr_state_get_current_srv());
  999. /* Also, make sure we did change the current. */
  1000. tt_assert(sr_state_get_current_srv() != cur);
  1001. /* We should have our commitment alone. */
  1002. tt_int_op(digestmap_size(state->commits), OP_EQ, 1);
  1003. tt_int_op(state->n_reveal_rounds, OP_EQ, 0);
  1004. tt_int_op(state->n_commit_rounds, OP_EQ, 0);
  1005. /* 46 here since we were at 45 just before. */
  1006. tt_u64_op(state->n_protocol_runs, OP_EQ, 46);
  1007. }
  1008. /* Cleanup of SRVs. */
  1009. {
  1010. sr_state_clean_srvs();
  1011. tt_ptr_op(sr_state_get_current_srv(), OP_EQ, NULL);
  1012. tt_ptr_op(sr_state_get_previous_srv(), OP_EQ, NULL);
  1013. }
  1014. done:
  1015. return;
  1016. }
  1017. static void
  1018. test_keep_commit(void *arg)
  1019. {
  1020. char fp[FINGERPRINT_LEN + 1];
  1021. sr_commit_t *commit = NULL, *dup_commit = NULL;
  1022. sr_state_t *state;
  1023. time_t now = time(NULL);
  1024. crypto_pk_t *k = NULL;
  1025. (void) arg;
  1026. MOCK(trusteddirserver_get_by_v3_auth_digest,
  1027. trusteddirserver_get_by_v3_auth_digest_m);
  1028. {
  1029. k = pk_generate(1);
  1030. /* Setup a minimal dirauth environment for this test */
  1031. /* Have a key that is not the one from our commit. */
  1032. init_authority_state();
  1033. state = get_sr_state();
  1034. }
  1035. crypto_rand((char*)fp, sizeof(fp));
  1036. /* Test this very important function that tells us if we should keep a
  1037. * commit or not in our state. Most of it depends on the phase and what's
  1038. * in the commit so we'll change the commit as we go. */
  1039. commit = sr_generate_our_commit(now, mock_cert);
  1040. tt_assert(commit);
  1041. /* Set us in COMMIT phase for starter. */
  1042. set_sr_phase(SR_PHASE_COMMIT);
  1043. /* We should never keep a commit from a non authoritative authority. */
  1044. tt_int_op(should_keep_commit(commit, fp, SR_PHASE_COMMIT), OP_EQ, 0);
  1045. /* This should NOT be kept because it has a reveal value in it. */
  1046. tt_assert(commit_has_reveal_value(commit));
  1047. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  1048. SR_PHASE_COMMIT), OP_EQ, 0);
  1049. /* Add it to the state which should return to not keep it. */
  1050. sr_state_add_commit(commit);
  1051. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  1052. SR_PHASE_COMMIT), OP_EQ, 0);
  1053. /* Remove it from state so we can continue our testing. */
  1054. digestmap_remove(state->commits, commit->rsa_identity);
  1055. /* Let's remove our reveal value which should make it OK to keep it. */
  1056. memset(commit->encoded_reveal, 0, sizeof(commit->encoded_reveal));
  1057. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  1058. SR_PHASE_COMMIT), OP_EQ, 1);
  1059. /* Let's reset our commit and go into REVEAL phase. */
  1060. sr_commit_free(commit);
  1061. commit = sr_generate_our_commit(now, mock_cert);
  1062. tt_assert(commit);
  1063. /* Dup the commit so we have one with and one without a reveal value. */
  1064. dup_commit = tor_malloc_zero(sizeof(*dup_commit));
  1065. memcpy(dup_commit, commit, sizeof(*dup_commit));
  1066. memset(dup_commit->encoded_reveal, 0, sizeof(dup_commit->encoded_reveal));
  1067. set_sr_phase(SR_PHASE_REVEAL);
  1068. /* We should never keep a commit from a non authoritative authority. */
  1069. tt_int_op(should_keep_commit(commit, fp, SR_PHASE_REVEAL), OP_EQ, 0);
  1070. /* We shouldn't accept a commit that is not in our state. */
  1071. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  1072. SR_PHASE_REVEAL), OP_EQ, 0);
  1073. /* Important to add the commit _without_ the reveal here. */
  1074. sr_state_add_commit(dup_commit);
  1075. tt_int_op(digestmap_size(state->commits), OP_EQ, 1);
  1076. /* Our commit should be valid that is authoritative, contains a reveal, be
  1077. * in the state and commitment and reveal values match. */
  1078. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  1079. SR_PHASE_REVEAL), OP_EQ, 1);
  1080. /* The commit shouldn't be kept if it's not verified that is no matchin
  1081. * hashed reveal. */
  1082. {
  1083. /* Let's save the hash reveal so we can restore it. */
  1084. sr_commit_t place_holder;
  1085. memcpy(place_holder.hashed_reveal, commit->hashed_reveal,
  1086. sizeof(place_holder.hashed_reveal));
  1087. memset(commit->hashed_reveal, 0, sizeof(commit->hashed_reveal));
  1088. setup_full_capture_of_logs(LOG_WARN);
  1089. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  1090. SR_PHASE_REVEAL), OP_EQ, 0);
  1091. expect_log_msg_containing("doesn't match the commit value.");
  1092. expect_log_msg_containing("has an invalid reveal value.");
  1093. assert_log_predicate(mock_saved_log_n_entries() == 2,
  1094. "expected 2 log entries");
  1095. teardown_capture_of_logs();
  1096. memcpy(commit->hashed_reveal, place_holder.hashed_reveal,
  1097. sizeof(commit->hashed_reveal));
  1098. }
  1099. /* We shouldn't keep a commit that has no reveal. */
  1100. tt_int_op(should_keep_commit(dup_commit, dup_commit->rsa_identity,
  1101. SR_PHASE_REVEAL), OP_EQ, 0);
  1102. /* We must not keep a commit that is not the same from the commit phase. */
  1103. memset(commit->encoded_commit, 0, sizeof(commit->encoded_commit));
  1104. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  1105. SR_PHASE_REVEAL), OP_EQ, 0);
  1106. done:
  1107. teardown_capture_of_logs();
  1108. sr_commit_free(commit);
  1109. sr_commit_free(dup_commit);
  1110. crypto_pk_free(k);
  1111. UNMOCK(trusteddirserver_get_by_v3_auth_digest);
  1112. }
  1113. static void
  1114. test_state_update(void *arg)
  1115. {
  1116. time_t commit_phase_time = 1452076000;
  1117. time_t reveal_phase_time = 1452086800;
  1118. sr_state_t *state;
  1119. (void) arg;
  1120. {
  1121. init_authority_state();
  1122. state = get_sr_state();
  1123. set_sr_phase(SR_PHASE_COMMIT);
  1124. /* We'll cheat a bit here and reset the creation time of the state which
  1125. * will avoid us to compute a valid_after time that fits the commit
  1126. * phase. */
  1127. state->valid_after = 0;
  1128. state->n_reveal_rounds = 0;
  1129. state->n_commit_rounds = 0;
  1130. state->n_protocol_runs = 0;
  1131. }
  1132. /* We need to mock for the state update function call. */
  1133. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1134. /* We are in COMMIT phase here and we'll trigger a state update but no
  1135. * transition. */
  1136. sr_state_update(commit_phase_time);
  1137. tt_int_op(state->valid_after, OP_EQ, commit_phase_time);
  1138. tt_int_op(state->n_commit_rounds, OP_EQ, 1);
  1139. tt_int_op(state->phase, OP_EQ, SR_PHASE_COMMIT);
  1140. tt_int_op(digestmap_size(state->commits), OP_EQ, 1);
  1141. /* We are still in the COMMIT phase here but we'll trigger a state
  1142. * transition to the REVEAL phase. */
  1143. sr_state_update(reveal_phase_time);
  1144. tt_int_op(state->phase, OP_EQ, SR_PHASE_REVEAL);
  1145. tt_int_op(state->valid_after, OP_EQ, reveal_phase_time);
  1146. /* Only our commit should be in there. */
  1147. tt_int_op(digestmap_size(state->commits), OP_EQ, 1);
  1148. tt_int_op(state->n_reveal_rounds, OP_EQ, 1);
  1149. /* We can't update a state with a valid after _lower_ than the creation
  1150. * time so here it is. */
  1151. sr_state_update(commit_phase_time);
  1152. tt_int_op(state->valid_after, OP_EQ, reveal_phase_time);
  1153. /* Finally, let's go back in COMMIT phase so we can test the state update
  1154. * of a new protocol run. */
  1155. state->valid_after = 0;
  1156. sr_state_update(commit_phase_time);
  1157. tt_int_op(state->valid_after, OP_EQ, commit_phase_time);
  1158. tt_int_op(state->n_commit_rounds, OP_EQ, 1);
  1159. tt_int_op(state->n_reveal_rounds, OP_EQ, 0);
  1160. tt_u64_op(state->n_protocol_runs, OP_EQ, 1);
  1161. tt_int_op(state->phase, OP_EQ, SR_PHASE_COMMIT);
  1162. tt_int_op(digestmap_size(state->commits), OP_EQ, 1);
  1163. tt_assert(state->current_srv);
  1164. done:
  1165. sr_state_free_all();
  1166. UNMOCK(get_my_v3_authority_cert);
  1167. }
  1168. struct testcase_t sr_tests[] = {
  1169. { "get_sr_protocol_phase", test_get_sr_protocol_phase, TT_FORK,
  1170. NULL, NULL },
  1171. { "sr_commit", test_sr_commit, TT_FORK,
  1172. NULL, NULL },
  1173. { "keep_commit", test_keep_commit, TT_FORK,
  1174. NULL, NULL },
  1175. { "encoding", test_encoding, TT_FORK,
  1176. NULL, NULL },
  1177. { "get_start_time_of_current_run", test_get_start_time_of_current_run,
  1178. TT_FORK, NULL, NULL },
  1179. { "get_start_time_functions", test_get_start_time_functions,
  1180. TT_FORK, NULL, NULL },
  1181. { "get_sr_protocol_duration", test_get_sr_protocol_duration, TT_FORK,
  1182. NULL, NULL },
  1183. { "get_state_valid_until_time", test_get_state_valid_until_time, TT_FORK,
  1184. NULL, NULL },
  1185. { "vote", test_vote, TT_FORK,
  1186. NULL, NULL },
  1187. { "state_load_from_disk", test_state_load_from_disk, TT_FORK,
  1188. NULL, NULL },
  1189. { "sr_compute_srv", test_sr_compute_srv, TT_FORK, NULL, NULL },
  1190. { "sr_get_majority_srv_from_votes", test_sr_get_majority_srv_from_votes,
  1191. TT_FORK, NULL, NULL },
  1192. { "utils", test_utils, TT_FORK, NULL, NULL },
  1193. { "state_transition", test_state_transition, TT_FORK, NULL, NULL },
  1194. { "state_update", test_state_update, TT_FORK,
  1195. NULL, NULL },
  1196. END_OF_TESTCASES
  1197. };