test_shared_random.c 42 KB

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