test_shared_random.c 44 KB

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