test_shared_random.c 48 KB

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