test_shared_random.c 48 KB

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