test_shared_random.c 57 KB

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