test_shared_random.c 50 KB

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