test_shared_random.c 48 KB

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