test_shared_random.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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, our_commit->rsa_identity_fpr);
  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_fpr);
  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. tt_str_op(smartlist_get(tokens, 2), OP_EQ,
  433. our_commit->rsa_identity_fpr);
  434. tt_str_op(smartlist_get(tokens, 3), OP_EQ, our_commit->encoded_commit);
  435. tt_str_op(smartlist_get(tokens, 4), OP_EQ, our_commit->encoded_reveal);
  436. /* Finally, does this vote line creates a valid commit object? */
  437. smartlist_t *args = smartlist_new();
  438. smartlist_add(args, smartlist_get(tokens, 1));
  439. smartlist_add(args, smartlist_get(tokens, 2));
  440. smartlist_add(args, smartlist_get(tokens, 3));
  441. smartlist_add(args, smartlist_get(tokens, 4));
  442. sr_commit_t *parsed_commit = sr_parse_commit(args);
  443. tt_assert(parsed_commit);
  444. tt_mem_op(parsed_commit, ==, our_commit, sizeof(*our_commit));
  445. /* minor cleanup */
  446. SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
  447. smartlist_clear(tokens);
  448. /* Now test the previous SRV */
  449. char *prev_srv_line = smartlist_get(chunks, 2);
  450. tt_assert(prev_srv_line);
  451. ret = smartlist_split_string(tokens, prev_srv_line, " ", 0, 0);
  452. tt_int_op(ret, ==, 3);
  453. tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-previous-value");
  454. tt_str_op(smartlist_get(tokens, 1), OP_EQ, "42");
  455. tt_str_op(smartlist_get(tokens, 2), OP_EQ,
  456. "WlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlo=");
  457. /* minor cleanup */
  458. SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
  459. smartlist_clear(tokens);
  460. /* Now test the current SRV */
  461. char *current_srv_line = smartlist_get(chunks, 3);
  462. tt_assert(current_srv_line);
  463. ret = smartlist_split_string(tokens, current_srv_line, " ", 0, 0);
  464. tt_int_op(ret, ==, 3);
  465. tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-current-value");
  466. tt_str_op(smartlist_get(tokens, 1), OP_EQ, "128");
  467. tt_str_op(smartlist_get(tokens, 2), OP_EQ,
  468. "Tk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk4=");
  469. /* Clean up */
  470. sr_commit_free(parsed_commit);
  471. SMARTLIST_FOREACH(chunks, char *, s, tor_free(s));
  472. smartlist_free(chunks);
  473. SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
  474. smartlist_free(tokens);
  475. smartlist_clear(args);
  476. smartlist_free(args);
  477. }
  478. done:
  479. sr_commit_free(our_commit);
  480. }
  481. const char *sr_state_str = "Version 1\n"
  482. "ValidUntil 2666-04-20 07:16:00\n"
  483. "ValidAfter 2666-04-19 07:16:00\n"
  484. "Commit sha3-256 FA3CEC2C99DC68D3166B9B6E4FA21A4026C2AB1C "
  485. "7M8GdubCAAdh7WUG0DiwRyxTYRKji7HATa7LLJEZ/UAAAAAAVmfUSg== "
  486. "AAAAAFZn1EojfIheIw42bjK3VqkpYyjsQFSbv/dxNna3Q8hUEPKpOw==\n"
  487. "Commit sha3-256 41E89EDFBFBA44983E21F18F2230A4ECB5BFB543 "
  488. "17aUsYuMeRjd2N1r8yNyg7aHqRa6gf4z7QPoxxAZbp0AAAAAVmfUSg==\n"
  489. "Commit sha3-256 36637026573A04110CF3E6B1D201FB9A98B88734 "
  490. "DDDYtripvdOU+XPEUm5xpU64d9IURSds1xSwQsgeB8oAAAAAVmfUSg==\n"
  491. "SharedRandCurrentValue 3 8dWeW12KEzTGEiLGgO1UVJ7Z91CekoRcxt6Q9KhnOFI=\n"
  492. "SharedRandPreviousValue 4 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqo=\n";
  493. /** Create an SR disk state, parse it and validate that the parsing went
  494. * well. Yes! */
  495. static void
  496. test_state_load_from_disk(void *arg)
  497. {
  498. int ret;
  499. char *dir = tor_strdup(get_fname("test_sr_state"));
  500. char *sr_state_path = tor_strdup(get_fname("test_sr_state/sr_state"));
  501. sr_state_t *the_sr_state = NULL;
  502. (void) arg;
  503. /* First try with a nonexistent path. */
  504. ret = disk_state_load_from_disk_impl("NONEXISTENTNONEXISTENT");
  505. tt_assert(ret == -ENOENT);
  506. /* Now create a mock state directory and state file */
  507. #ifdef _WIN32
  508. ret = mkdir(dir);
  509. #else
  510. ret = mkdir(dir, 0700);
  511. #endif
  512. tt_assert(ret == 0);
  513. ret = write_str_to_file(sr_state_path, sr_state_str, 0);
  514. tt_assert(ret == 0);
  515. /* Try to load the directory itself. Should fail. */
  516. ret = disk_state_load_from_disk_impl(dir);
  517. tt_assert(ret == -EINVAL);
  518. /* State should be non-existent at this point. */
  519. the_sr_state = get_sr_state();
  520. tt_assert(!the_sr_state);
  521. /* Now try to load the correct file! */
  522. ret = disk_state_load_from_disk_impl(sr_state_path);
  523. tt_assert(ret == 0);
  524. /* Check the content of the state */
  525. /* XXX check more deeply!!! */
  526. the_sr_state = get_sr_state();
  527. tt_assert(the_sr_state);
  528. tt_assert(the_sr_state->version == 1);
  529. tt_assert(digestmap_size(the_sr_state->commits) == 3);
  530. tt_assert(the_sr_state->current_srv);
  531. tt_assert(the_sr_state->current_srv->num_reveals == 3);
  532. tt_assert(the_sr_state->previous_srv);
  533. /* XXX Now also try loading corrupted state files and make sure parsing
  534. fails */
  535. done:
  536. tor_free(dir);
  537. tor_free(sr_state_path);
  538. }
  539. /** Generate three specially crafted commits (based on the test
  540. * vector at sr_srv_calc_ref.py). Helper of test_sr_compute_srv(). */
  541. static void
  542. test_sr_setup_commits(void)
  543. {
  544. time_t now = time(NULL);
  545. sr_commit_t *commit_a, *commit_b, *commit_c, *commit_d;
  546. sr_commit_t *place_holder = tor_malloc_zero(sizeof(*place_holder));
  547. authority_cert_t *auth_cert = NULL;
  548. { /* Setup a minimal dirauth environment for this test */
  549. or_options_t *options = get_options_mutable();
  550. auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  551. tt_assert(auth_cert);
  552. options->AuthoritativeDir = 1;
  553. tt_int_op(0, ==, load_ed_keys(options, now));
  554. }
  555. /* Generate three dummy commits according to sr_srv_calc_ref.py . Then
  556. register them to the SR state. Also register a fourth commit 'd' with no
  557. reveal info, to make sure that it will get ignored during SRV
  558. calculation. */
  559. { /* Commit from auth 'a' */
  560. commit_a = sr_generate_our_commit(now, auth_cert);
  561. tt_assert(commit_a);
  562. /* Do some surgery on the commit */
  563. strlcpy(commit_a->rsa_identity_fpr,
  564. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  565. sizeof(commit_a->rsa_identity_fpr));
  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. strlcpy(commit_b->rsa_identity_fpr,
  578. "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
  579. sizeof(commit_b->rsa_identity_fpr));
  580. strlcpy(commit_b->encoded_reveal,
  581. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
  582. sizeof(commit_b->encoded_reveal));
  583. memcpy(commit_b->hashed_reveal,
  584. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
  585. sizeof(commit_b->hashed_reveal));
  586. }
  587. { /* Commit from auth 'c' */
  588. commit_c = sr_generate_our_commit(now, auth_cert);
  589. tt_assert(commit_c);
  590. /* Do some surgery on the commit */
  591. strlcpy(commit_c->rsa_identity_fpr,
  592. "ccccccccccccccccccccccccccccccccccccccccccccccccc",
  593. sizeof(commit_c->rsa_identity_fpr));
  594. strlcpy(commit_c->encoded_reveal,
  595. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
  596. sizeof(commit_c->encoded_reveal));
  597. memcpy(commit_c->hashed_reveal,
  598. "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
  599. sizeof(commit_c->hashed_reveal));
  600. }
  601. { /* Commit from auth 'd' */
  602. commit_d = sr_generate_our_commit(now, auth_cert);
  603. tt_assert(commit_d);
  604. /* Do some surgery on the commit */
  605. strlcpy(commit_d->rsa_identity_fpr,
  606. "ddddddddddddddddddddddddddddddddddddddddddddddddd",
  607. sizeof(commit_d->rsa_identity_fpr));
  608. strlcpy(commit_d->encoded_reveal,
  609. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
  610. sizeof(commit_d->encoded_reveal));
  611. memcpy(commit_d->hashed_reveal,
  612. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
  613. sizeof(commit_d->hashed_reveal));
  614. /* Clean up its reveal info */
  615. memcpy(place_holder, commit_d, sizeof(*place_holder));
  616. memset(commit_d->encoded_reveal, 0, sizeof(commit_d->encoded_reveal));
  617. tt_assert(!commit_has_reveal_value(commit_d));
  618. }
  619. /* Register commits to state (during commit phase) */
  620. set_sr_phase(SR_PHASE_COMMIT);
  621. save_commit_to_state(commit_a);
  622. save_commit_to_state(commit_b);
  623. save_commit_to_state(commit_c);
  624. save_commit_to_state(commit_d);
  625. tt_int_op(digestmap_size(get_sr_state()->commits), ==, 4);
  626. /* Now during REVEAL phase save commit D by restoring its reveal. */
  627. set_sr_phase(SR_PHASE_REVEAL);
  628. save_commit_to_state(place_holder);
  629. tt_str_op(commit_d->encoded_reveal, OP_EQ,
  630. "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
  631. /* Go back to an empty encoded reveal value. */
  632. memset(commit_d->encoded_reveal, 0, sizeof(commit_d->encoded_reveal));
  633. memset(commit_d->random_number, 0, sizeof(commit_d->random_number));
  634. tt_assert(!commit_has_reveal_value(commit_d));
  635. done:
  636. return;
  637. }
  638. /** Verify that the SRV generation procedure is proper by testing it against
  639. * the test vector from ./sr_srv_calc_ref.py. */
  640. static void
  641. test_sr_compute_srv(void *arg)
  642. {
  643. (void) arg;
  644. sr_srv_t *current_srv = NULL;
  645. #define SRV_TEST_VECTOR \
  646. "2A9B1D6237DAB312A40F575DA85C147663E7ED3F80E9555395F15B515C74253D"
  647. MOCK(trusteddirserver_get_by_v3_auth_digest,
  648. trusteddirserver_get_by_v3_auth_digest_m);
  649. init_authority_state();
  650. /* Setup the commits for this unittest */
  651. test_sr_setup_commits();
  652. test_sr_setup_srv(0);
  653. /* Now switch to reveal phase */
  654. set_sr_phase(SR_PHASE_REVEAL);
  655. /* Compute the SRV */
  656. sr_compute_srv();
  657. /* Check the result against the test vector */
  658. current_srv = sr_state_get_current_srv();
  659. tt_assert(current_srv);
  660. tt_int_op(current_srv->num_reveals, ==, 3);
  661. tt_str_op(hex_str((char*)current_srv->value, 32),
  662. ==,
  663. SRV_TEST_VECTOR);
  664. done:
  665. ;
  666. }
  667. /** Return a minimal vote document with a current SRV value set to
  668. * <b>srv</b>. */
  669. static networkstatus_t *
  670. get_test_vote_with_curr_srv(const char *srv)
  671. {
  672. networkstatus_t *vote = tor_malloc_zero(sizeof(networkstatus_t));
  673. vote->type = NS_TYPE_VOTE;
  674. vote->sr_info.participate = 1;
  675. vote->sr_info.current_srv = tor_malloc_zero(sizeof(sr_srv_t));
  676. vote->sr_info.current_srv->num_reveals = 42;
  677. memcpy(vote->sr_info.current_srv->value,
  678. srv,
  679. sizeof(vote->sr_info.current_srv->value));
  680. return vote;
  681. }
  682. /* Mock function to return the value located in the options instead of the
  683. * consensus so we can modify it at will. */
  684. static networkstatus_t *
  685. mock_networkstatus_get_latest_consensus(void)
  686. {
  687. return mock_consensus;
  688. }
  689. /* Test the function that picks the right SRV given a bunch of votes. Make sure
  690. * that the function returns an SRV iff the majority/agreement requirements are
  691. * met. */
  692. static void
  693. test_sr_get_majority_srv_from_votes(void *arg)
  694. {
  695. sr_srv_t *chosen_srv;
  696. smartlist_t *votes = smartlist_new();
  697. #define SRV_1 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  698. #define SRV_2 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  699. (void) arg;
  700. init_authority_state();
  701. /* Make sure our SRV is fresh so we can consider the super majority with
  702. * the consensus params of number of agreements needed. */
  703. sr_state_set_fresh_srv();
  704. /* The test relies on the dirauth list being initialized. */
  705. clear_dir_servers();
  706. add_default_trusted_dir_authorities(V3_DIRINFO);
  707. tt_int_op(get_n_authorities(V3_DIRINFO), ==, 9);
  708. { /* Prepare voting environment with just a single vote. */
  709. networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
  710. smartlist_add(votes, vote);
  711. }
  712. /* Since it's only one vote with an SRV, it should not achieve majority and
  713. hence no SRV will be returned. */
  714. chosen_srv = get_majority_srv_from_votes(votes, 1);
  715. tt_assert(!chosen_srv);
  716. { /* Now put in 8 more votes. Let SRV_1 have majority. */
  717. int i;
  718. /* Now 7 votes believe in SRV_1 */
  719. for (i = 0; i < 6; i++) {
  720. networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
  721. smartlist_add(votes, vote);
  722. }
  723. /* and 2 votes believe in SRV_2 */
  724. for (i = 0; i < 2; i++) {
  725. networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_2);
  726. smartlist_add(votes, vote);
  727. }
  728. tt_int_op(smartlist_len(votes), ==, 9);
  729. }
  730. /* Now we achieve majority for SRV_1, but not the AuthDirNumSRVAgreements
  731. requirement. So still not picking an SRV. */
  732. chosen_srv = get_majority_srv_from_votes(votes, 1);
  733. tt_assert(!chosen_srv);
  734. /* We will now lower the AuthDirNumSRVAgreements requirement by tweaking the
  735. * consensus parameter and we will try again. This time it should work. */
  736. {
  737. char *my_net_params;
  738. /* Set a dummy consensus parameter in every vote */
  739. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, vote) {
  740. vote->net_params = smartlist_new();
  741. smartlist_split_string(vote->net_params,
  742. "AuthDirNumSRVAgreements=7", NULL, 0, 0);
  743. } SMARTLIST_FOREACH_END(vote);
  744. /* Pretend you are making a consensus out of the votes */
  745. mock_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  746. mock_consensus->net_params = smartlist_new();
  747. my_net_params = dirvote_compute_params(votes, 66, smartlist_len(votes));
  748. smartlist_split_string(mock_consensus->net_params, my_net_params, " ",
  749. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  750. }
  751. /* Use our fake consensus for finding consensus parameters. */
  752. MOCK(networkstatus_get_latest_consensus,
  753. mock_networkstatus_get_latest_consensus);
  754. chosen_srv = get_majority_srv_from_votes(votes, 1);
  755. tt_assert(chosen_srv);
  756. tt_int_op(chosen_srv->num_reveals, ==, 42);
  757. tt_mem_op(chosen_srv->value, OP_EQ, SRV_1, sizeof(chosen_srv->value));
  758. done:
  759. SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
  760. networkstatus_vote_free(vote));
  761. smartlist_free(votes);
  762. networkstatus_vote_free(mock_consensus);
  763. UNMOCK(networkstatus_get_latest_consensus);
  764. }
  765. static void
  766. test_utils(void *arg)
  767. {
  768. (void) arg;
  769. /* Testing srv_dup(). */
  770. {
  771. sr_srv_t *srv = NULL, *dup_srv = NULL;
  772. const char *srv_value =
  773. "1BDB7C3E973936E4D13A49F37C859B3DC69C429334CF9412E3FEF6399C52D47A";
  774. srv = tor_malloc_zero(sizeof(*srv));
  775. srv->num_reveals = 42;
  776. memcpy(srv->value, srv_value, sizeof(srv->value));
  777. dup_srv = srv_dup(srv);
  778. tt_assert(dup_srv);
  779. tt_int_op(dup_srv->num_reveals, ==, srv->num_reveals);
  780. tt_mem_op(dup_srv->value, OP_EQ, srv->value, sizeof(srv->value));
  781. tor_free(srv);
  782. tor_free(dup_srv);
  783. }
  784. /* Testing commitments_are_the_same(). Currently, the check is to test the
  785. * value of the encoded commit so let's make sure that actually works. */
  786. {
  787. /* Payload of 55 bytes that is the length of
  788. * sr_commit_t->encoded_commit. */
  789. const char *payload =
  790. "\x5d\xb9\x60\xb6\xcc\x51\x68\x52\x31\xd9\x88\x88\x71\x71\xe0\x30"
  791. "\x59\x55\x7f\xcd\x61\xc0\x4b\x05\xb8\xcd\xc1\x48\xe9\xcd\x16\x1f"
  792. "\x70\x15\x0c\xfc\xd3\x1a\x75\xd0\x93\x6c\xc4\xe0\x5c\xbe\xe2\x18"
  793. "\xc7\xaf\x72\xb6\x7c\x9b\x52";
  794. sr_commit_t commit1, commit2;
  795. memcpy(commit1.encoded_commit, payload, sizeof(commit1.encoded_commit));
  796. memcpy(commit2.encoded_commit, payload, sizeof(commit2.encoded_commit));
  797. tt_int_op(commitments_are_the_same(&commit1, &commit2), ==, 1);
  798. /* Let's corrupt one of them. */
  799. memset(commit1.encoded_commit, 'A', sizeof(commit1.encoded_commit));
  800. tt_int_op(commitments_are_the_same(&commit1, &commit2), ==, 0);
  801. }
  802. /* Testing commit_is_authoritative(). */
  803. {
  804. crypto_pk_t *k = crypto_pk_new();
  805. char fp[FINGERPRINT_LEN + 1];
  806. sr_commit_t commit;
  807. tt_assert(!crypto_pk_generate_key(k));
  808. tt_int_op(0, ==, crypto_pk_get_fingerprint(k, fp, 0));
  809. memcpy(fp, commit.rsa_identity_fpr, sizeof(fp));
  810. tt_int_op(commit_is_authoritative(&commit, fp), ==, 1);
  811. /* Change the pubkey. */
  812. memset(commit.rsa_identity_fpr, 0, sizeof(commit.rsa_identity_fpr));
  813. tt_int_op(commit_is_authoritative(&commit, fp), ==, 0);
  814. }
  815. /* Testing get_phase_str(). */
  816. {
  817. tt_str_op(get_phase_str(SR_PHASE_REVEAL), ==, "reveal");
  818. tt_str_op(get_phase_str(SR_PHASE_COMMIT), ==, "commit");
  819. }
  820. /* Testing phase transition */
  821. {
  822. init_authority_state();
  823. set_sr_phase(SR_PHASE_COMMIT);
  824. tt_int_op(is_phase_transition(SR_PHASE_REVEAL), ==, 1);
  825. tt_int_op(is_phase_transition(SR_PHASE_COMMIT), ==, 0);
  826. set_sr_phase(SR_PHASE_REVEAL);
  827. tt_int_op(is_phase_transition(SR_PHASE_REVEAL), ==, 0);
  828. tt_int_op(is_phase_transition(SR_PHASE_COMMIT), ==, 1);
  829. /* Junk. */
  830. tt_int_op(is_phase_transition(42), ==, 1);
  831. }
  832. done:
  833. return;
  834. }
  835. static void
  836. test_state_transition(void *arg)
  837. {
  838. sr_state_t *state = NULL;
  839. time_t now = time(NULL);
  840. (void) arg;
  841. { /* Setup a minimal dirauth environment for this test */
  842. init_authority_state();
  843. state = get_sr_state();
  844. tt_assert(state);
  845. }
  846. /* Test our state reset for a new protocol run. */
  847. {
  848. /* Add a commit to the state so we can test if the reset cleans the
  849. * commits. Also, change all params that we expect to be updated. */
  850. sr_commit_t *commit = sr_generate_our_commit(now, mock_cert);
  851. tt_assert(commit);
  852. sr_state_add_commit(commit);
  853. tt_int_op(digestmap_size(state->commits), ==, 1);
  854. /* Let's test our delete feature. */
  855. sr_state_delete_commits();
  856. tt_int_op(digestmap_size(state->commits), ==, 0);
  857. /* Add it back so we can continue the rest of the test because after
  858. * deletiong our commit will be freed so generate a new one. */
  859. commit = sr_generate_our_commit(now, mock_cert);
  860. tt_assert(commit);
  861. sr_state_add_commit(commit);
  862. tt_int_op(digestmap_size(state->commits), ==, 1);
  863. state->n_reveal_rounds = 42;
  864. state->n_commit_rounds = 43;
  865. state->n_protocol_runs = 44;
  866. reset_state_for_new_protocol_run(now);
  867. tt_int_op(state->n_reveal_rounds, ==, 0);
  868. tt_int_op(state->n_commit_rounds, ==, 0);
  869. tt_u64_op(state->n_protocol_runs, ==, 45);
  870. tt_int_op(digestmap_size(state->commits), ==, 0);
  871. }
  872. /* Test SRV rotation in our state. */
  873. {
  874. sr_srv_t *cur, *prev;
  875. test_sr_setup_srv(1);
  876. cur = sr_state_get_current_srv();
  877. tt_assert(cur);
  878. /* After, current srv should be the previous and then set to NULL. */
  879. state_rotate_srv();
  880. prev = sr_state_get_previous_srv();
  881. tt_assert(prev == cur);
  882. tt_assert(!sr_state_get_current_srv());
  883. }
  884. /* New protocol run. */
  885. {
  886. sr_srv_t *cur;
  887. /* Setup some new SRVs so we can confirm that a new protocol run
  888. * actually makes them rotate and compute new ones. */
  889. test_sr_setup_srv(1);
  890. cur = sr_state_get_current_srv();
  891. tt_assert(cur);
  892. set_sr_phase(SR_PHASE_REVEAL);
  893. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  894. new_protocol_run(now);
  895. UNMOCK(get_my_v3_authority_cert);
  896. /* Rotation happened. */
  897. tt_assert(sr_state_get_previous_srv() == cur);
  898. /* We are going into COMMIT phase so we had to rotate our SRVs. Usually
  899. * our current SRV would be NULL but a new protocol run should make us
  900. * compute a new SRV. */
  901. tt_assert(sr_state_get_current_srv());
  902. /* Also, make sure we did change the current. */
  903. tt_assert(sr_state_get_current_srv() != cur);
  904. /* We should have our commitment alone. */
  905. tt_int_op(digestmap_size(state->commits), ==, 1);
  906. tt_int_op(state->n_reveal_rounds, ==, 0);
  907. tt_int_op(state->n_commit_rounds, ==, 0);
  908. /* 46 here since we were at 45 just before. */
  909. tt_u64_op(state->n_protocol_runs, ==, 46);
  910. }
  911. /* Cleanup of SRVs. */
  912. {
  913. sr_state_clean_srvs();
  914. tt_assert(!sr_state_get_current_srv());
  915. tt_assert(!sr_state_get_previous_srv());
  916. }
  917. done:
  918. return;
  919. }
  920. static void
  921. test_keep_commit(void *arg)
  922. {
  923. char fp[FINGERPRINT_LEN + 1];
  924. sr_commit_t *commit = NULL, *dup_commit = NULL;
  925. sr_state_t *state;
  926. time_t now = time(NULL);
  927. (void) arg;
  928. { /* Setup a minimal dirauth environment for this test */
  929. crypto_pk_t *k = crypto_pk_new();
  930. /* Have a key that is not the one from our commit. */
  931. tt_int_op(0, ==, crypto_pk_generate_key(k));
  932. tt_int_op(0, ==, crypto_pk_get_fingerprint(k, fp, 0));
  933. init_authority_state();
  934. state = get_sr_state();
  935. }
  936. /* Test this very important function that tells us if we should keep a
  937. * commit or not in our state. Most of it depends on the phase and what's
  938. * in the commit so we'll change the commit as we go. */
  939. commit = sr_generate_our_commit(now, mock_cert);
  940. tt_assert(commit);
  941. /* Set us in COMMIT phase for starter. */
  942. set_sr_phase(SR_PHASE_COMMIT);
  943. /* We should never keep a commit from a non authoritative authority. */
  944. tt_int_op(should_keep_commit(commit, fp, SR_PHASE_COMMIT), ==, 0);
  945. /* This should NOT be kept because it has a reveal value in it. */
  946. tt_assert(commit_has_reveal_value(commit));
  947. tt_int_op(should_keep_commit(commit, commit->rsa_identity_fpr,
  948. SR_PHASE_COMMIT), ==, 0);
  949. /* Add it to the state which should return to not keep it. */
  950. sr_state_add_commit(commit);
  951. tt_int_op(should_keep_commit(commit, commit->rsa_identity_fpr,
  952. SR_PHASE_COMMIT), ==, 0);
  953. /* Remove it from state so we can continue our testing. */
  954. digestmap_remove(state->commits, commit->rsa_identity_fpr);
  955. /* Let's remove our reveal value which should make it OK to keep it. */
  956. memset(commit->encoded_reveal, 0, sizeof(commit->encoded_reveal));
  957. tt_int_op(should_keep_commit(commit, commit->rsa_identity_fpr,
  958. SR_PHASE_COMMIT), ==, 1);
  959. /* Let's reset our commit and go into REVEAL phase. */
  960. sr_commit_free(commit);
  961. commit = sr_generate_our_commit(now, mock_cert);
  962. tt_assert(commit);
  963. /* Dup the commit so we have one with and one without a reveal value. */
  964. dup_commit = tor_malloc_zero(sizeof(*dup_commit));
  965. memcpy(dup_commit, commit, sizeof(*dup_commit));
  966. memset(dup_commit->encoded_reveal, 0, sizeof(dup_commit->encoded_reveal));
  967. set_sr_phase(SR_PHASE_REVEAL);
  968. /* We should never keep a commit from a non authoritative authority. */
  969. tt_int_op(should_keep_commit(commit, fp, SR_PHASE_REVEAL), ==, 0);
  970. /* We shouldn't accept a commit that is not in our state. */
  971. tt_int_op(should_keep_commit(commit, commit->rsa_identity_fpr,
  972. SR_PHASE_REVEAL), ==, 0);
  973. /* Important to add the commit _without_ the reveal here. */
  974. sr_state_add_commit(dup_commit);
  975. tt_int_op(digestmap_size(state->commits), ==, 1);
  976. /* Our commit should be valid that is authoritative, contains a reveal, be
  977. * in the state and commitment and reveal values match. */
  978. tt_int_op(should_keep_commit(commit, commit->rsa_identity_fpr,
  979. SR_PHASE_REVEAL), ==, 1);
  980. /* The commit shouldn't be kept if it's not verified that is no matchin
  981. * hashed reveal. */
  982. {
  983. /* Let's save the hash reveal so we can restore it. */
  984. sr_commit_t place_holder;
  985. memcpy(place_holder.hashed_reveal, commit->hashed_reveal,
  986. sizeof(place_holder.hashed_reveal));
  987. memset(commit->hashed_reveal, 0, sizeof(commit->hashed_reveal));
  988. tt_int_op(should_keep_commit(commit, commit->rsa_identity_fpr,
  989. SR_PHASE_REVEAL), ==, 0);
  990. memcpy(commit->hashed_reveal, place_holder.hashed_reveal,
  991. sizeof(commit->hashed_reveal));
  992. }
  993. /* We shouldn't keep a commit that has no reveal. */
  994. tt_int_op(should_keep_commit(dup_commit, dup_commit->rsa_identity_fpr,
  995. SR_PHASE_REVEAL), ==, 0);
  996. /* We must not keep a commit that is not the same from the commit phase. */
  997. memset(commit->encoded_commit, 0, sizeof(commit->encoded_commit));
  998. tt_int_op(should_keep_commit(commit, commit->rsa_identity_fpr,
  999. SR_PHASE_REVEAL), ==, 0);
  1000. done:
  1001. sr_commit_free(commit);
  1002. sr_commit_free(dup_commit);
  1003. }
  1004. static void
  1005. test_state_update(void *arg)
  1006. {
  1007. time_t commit_phase_time = 1452076000;
  1008. time_t reveal_phase_time = 1452086800;
  1009. sr_state_t *state;
  1010. (void) arg;
  1011. {
  1012. init_authority_state();
  1013. state = get_sr_state();
  1014. set_sr_phase(SR_PHASE_COMMIT);
  1015. /* We'll cheat a bit here and reset the creation time of the state which
  1016. * will avoid us to compute a valid_after time that fits the commit
  1017. * phase. */
  1018. state->valid_after = 0;
  1019. state->n_reveal_rounds = 0;
  1020. state->n_commit_rounds = 0;
  1021. }
  1022. /* We need to mock for the state update function call. */
  1023. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1024. /* We are in COMMIT phase here and we'll trigger a state update but no
  1025. * transition. */
  1026. sr_state_update(commit_phase_time);
  1027. tt_int_op(state->valid_after, ==, commit_phase_time);
  1028. tt_int_op(state->n_commit_rounds, ==, 1);
  1029. tt_int_op(state->phase, ==, SR_PHASE_COMMIT);
  1030. tt_int_op(digestmap_size(state->commits), ==, 1);
  1031. /* We are still in the COMMIT phase here but we'll trigger a state
  1032. * transition to the REVEAL phase. */
  1033. sr_state_update(reveal_phase_time);
  1034. tt_int_op(state->phase, ==, SR_PHASE_REVEAL);
  1035. tt_int_op(state->valid_after, ==, reveal_phase_time);
  1036. /* Only our commit should be in there. */
  1037. tt_int_op(digestmap_size(state->commits), ==, 1);
  1038. tt_int_op(state->n_reveal_rounds, ==, 1);
  1039. /* We can't update a state with a valid after _lower_ than the creation
  1040. * time so here it is. */
  1041. sr_state_update(commit_phase_time);
  1042. tt_int_op(state->valid_after, ==, reveal_phase_time);
  1043. /* Finally, let's go back in COMMIT phase so we can test the state update
  1044. * of a new protocol run. */
  1045. state->valid_after = 0;
  1046. sr_state_update(commit_phase_time);
  1047. tt_int_op(state->valid_after, ==, commit_phase_time);
  1048. tt_int_op(state->n_commit_rounds, ==, 1);
  1049. tt_int_op(state->n_reveal_rounds, ==, 0);
  1050. tt_u64_op(state->n_protocol_runs, ==, 1);
  1051. tt_int_op(state->phase, ==, SR_PHASE_COMMIT);
  1052. tt_int_op(digestmap_size(state->commits), ==, 1);
  1053. tt_assert(state->current_srv);
  1054. done:
  1055. sr_state_free();
  1056. UNMOCK(get_my_v3_authority_cert);
  1057. }
  1058. struct testcase_t sr_tests[] = {
  1059. { "get_sr_protocol_phase", test_get_sr_protocol_phase, TT_FORK,
  1060. NULL, NULL },
  1061. { "sr_commit", test_sr_commit, TT_FORK,
  1062. NULL, NULL },
  1063. { "keep_commit", test_keep_commit, TT_FORK,
  1064. NULL, NULL },
  1065. { "encoding", test_encoding, TT_FORK,
  1066. NULL, NULL },
  1067. { "get_next_valid_after_time", test_get_next_valid_after_time, TT_FORK,
  1068. NULL, NULL },
  1069. { "get_state_valid_until_time", test_get_state_valid_until_time, TT_FORK,
  1070. NULL, NULL },
  1071. { "vote", test_vote, TT_FORK,
  1072. NULL, NULL },
  1073. { "state_load_from_disk", test_state_load_from_disk, TT_FORK,
  1074. NULL, NULL },
  1075. { "sr_compute_srv", test_sr_compute_srv, TT_FORK, NULL, NULL },
  1076. { "sr_get_majority_srv_from_votes", test_sr_get_majority_srv_from_votes,
  1077. TT_FORK, NULL, NULL },
  1078. { "utils", test_utils, TT_FORK, NULL, NULL },
  1079. { "state_transition", test_state_transition, TT_FORK, NULL, NULL },
  1080. { "state_update", test_state_update, TT_FORK,
  1081. NULL, NULL },
  1082. END_OF_TESTCASES
  1083. };