test_shared_random.c 48 KB

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