test_shared_random.c 50 KB

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