test_shared_random.c 48 KB

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