test_shared_random.c 48 KB

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