test_shared_random.c 48 KB

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