test_shared_random.c 50 KB

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