test_shared_random.c 48 KB

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