test_shared_random.c 48 KB

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