test_shared_random.c 48 KB

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