test_shared_random.c 59 KB

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