test_shared_random.c 44 KB

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