test_shared_random.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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 < 3; 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. for (i = 0; i < 3; i++) {
  723. networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
  724. smartlist_add(votes, vote);
  725. }
  726. tt_int_op(smartlist_len(votes), ==, 9);
  727. }
  728. /* Now we achieve majority for SRV_1, but not the AuthDirNumSRVAgreements
  729. requirement. So still not picking an SRV. */
  730. chosen_srv = get_majority_srv_from_votes(votes, 1);
  731. tt_assert(!chosen_srv);
  732. /* We will now lower the AuthDirNumSRVAgreements requirement by tweaking the
  733. * consensus parameter and we will try again. This time it should work. */
  734. {
  735. char *my_net_params;
  736. /* Set a dummy consensus parameter in every vote */
  737. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, vote) {
  738. vote->net_params = smartlist_new();
  739. smartlist_split_string(vote->net_params,
  740. "AuthDirNumSRVAgreements=7", NULL, 0, 0);
  741. } SMARTLIST_FOREACH_END(vote);
  742. /* Pretend you are making a consensus out of the votes */
  743. mock_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  744. mock_consensus->net_params = smartlist_new();
  745. my_net_params = dirvote_compute_params(votes, 66, smartlist_len(votes));
  746. smartlist_split_string(mock_consensus->net_params, my_net_params, " ",
  747. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  748. }
  749. /* Use our fake consensus for finding consensus parameters. */
  750. MOCK(networkstatus_get_latest_consensus,
  751. mock_networkstatus_get_latest_consensus);
  752. chosen_srv = get_majority_srv_from_votes(votes, 1);
  753. tt_assert(chosen_srv);
  754. tt_int_op(chosen_srv->num_reveals, ==, 42);
  755. tt_mem_op(chosen_srv->value, OP_EQ, SRV_1, sizeof(chosen_srv->value));
  756. done:
  757. SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
  758. networkstatus_vote_free(vote));
  759. smartlist_free(votes);
  760. networkstatus_vote_free(mock_consensus);
  761. UNMOCK(networkstatus_get_latest_consensus);
  762. }
  763. static void
  764. test_utils(void *arg)
  765. {
  766. (void) arg;
  767. /* Testing srv_dup(). */
  768. {
  769. sr_srv_t *srv = NULL, *dup_srv = NULL;
  770. const char *srv_value =
  771. "1BDB7C3E973936E4D13A49F37C859B3DC69C429334CF9412E3FEF6399C52D47A";
  772. srv = tor_malloc_zero(sizeof(*srv));
  773. srv->num_reveals = 42;
  774. memcpy(srv->value, srv_value, sizeof(srv->value));
  775. dup_srv = srv_dup(srv);
  776. tt_assert(dup_srv);
  777. tt_int_op(dup_srv->num_reveals, ==, srv->num_reveals);
  778. tt_mem_op(dup_srv->value, OP_EQ, srv->value, sizeof(srv->value));
  779. tor_free(srv);
  780. tor_free(dup_srv);
  781. }
  782. /* Testing commitments_are_the_same(). Currently, the check is to test the
  783. * value of the encoded commit so let's make sure that actually works. */
  784. {
  785. /* Payload of 55 bytes that is the length of
  786. * sr_commit_t->encoded_commit. */
  787. const char *payload =
  788. "\x5d\xb9\x60\xb6\xcc\x51\x68\x52\x31\xd9\x88\x88\x71\x71\xe0\x30"
  789. "\x59\x55\x7f\xcd\x61\xc0\x4b\x05\xb8\xcd\xc1\x48\xe9\xcd\x16\x1f"
  790. "\x70\x15\x0c\xfc\xd3\x1a\x75\xd0\x93\x6c\xc4\xe0\x5c\xbe\xe2\x18"
  791. "\xc7\xaf\x72\xb6\x7c\x9b\x52";
  792. sr_commit_t commit1, commit2;
  793. memcpy(commit1.encoded_commit, payload, sizeof(commit1.encoded_commit));
  794. memcpy(commit2.encoded_commit, payload, sizeof(commit2.encoded_commit));
  795. tt_int_op(commitments_are_the_same(&commit1, &commit2), ==, 1);
  796. /* Let's corrupt one of them. */
  797. memset(commit1.encoded_commit, 'A', sizeof(commit1.encoded_commit));
  798. tt_int_op(commitments_are_the_same(&commit1, &commit2), ==, 0);
  799. }
  800. /* Testing commit_is_authoritative(). */
  801. {
  802. crypto_pk_t *k = crypto_pk_new();
  803. char digest[DIGEST_LEN];
  804. sr_commit_t commit;
  805. tt_assert(!crypto_pk_generate_key(k));
  806. tt_int_op(0, ==, crypto_pk_get_digest(k, digest));
  807. memcpy(commit.rsa_identity, digest, sizeof(commit.rsa_identity));
  808. tt_int_op(commit_is_authoritative(&commit, digest), ==, 1);
  809. /* Change the pubkey. */
  810. memset(commit.rsa_identity, 0, sizeof(commit.rsa_identity));
  811. tt_int_op(commit_is_authoritative(&commit, digest), ==, 0);
  812. }
  813. /* Testing get_phase_str(). */
  814. {
  815. tt_str_op(get_phase_str(SR_PHASE_REVEAL), ==, "reveal");
  816. tt_str_op(get_phase_str(SR_PHASE_COMMIT), ==, "commit");
  817. }
  818. /* Testing phase transition */
  819. {
  820. init_authority_state();
  821. set_sr_phase(SR_PHASE_COMMIT);
  822. tt_int_op(is_phase_transition(SR_PHASE_REVEAL), ==, 1);
  823. tt_int_op(is_phase_transition(SR_PHASE_COMMIT), ==, 0);
  824. set_sr_phase(SR_PHASE_REVEAL);
  825. tt_int_op(is_phase_transition(SR_PHASE_REVEAL), ==, 0);
  826. tt_int_op(is_phase_transition(SR_PHASE_COMMIT), ==, 1);
  827. /* Junk. */
  828. tt_int_op(is_phase_transition(42), ==, 1);
  829. }
  830. done:
  831. return;
  832. }
  833. static void
  834. test_state_transition(void *arg)
  835. {
  836. sr_state_t *state = NULL;
  837. time_t now = time(NULL);
  838. (void) arg;
  839. { /* Setup a minimal dirauth environment for this test */
  840. init_authority_state();
  841. state = get_sr_state();
  842. tt_assert(state);
  843. }
  844. /* Test our state reset for a new protocol run. */
  845. {
  846. /* Add a commit to the state so we can test if the reset cleans the
  847. * commits. Also, change all params that we expect to be updated. */
  848. sr_commit_t *commit = sr_generate_our_commit(now, mock_cert);
  849. tt_assert(commit);
  850. sr_state_add_commit(commit);
  851. tt_int_op(digestmap_size(state->commits), ==, 1);
  852. /* Let's test our delete feature. */
  853. sr_state_delete_commits();
  854. tt_int_op(digestmap_size(state->commits), ==, 0);
  855. /* Add it back so we can continue the rest of the test because after
  856. * deletiong our commit will be freed so generate a new one. */
  857. commit = sr_generate_our_commit(now, mock_cert);
  858. tt_assert(commit);
  859. sr_state_add_commit(commit);
  860. tt_int_op(digestmap_size(state->commits), ==, 1);
  861. state->n_reveal_rounds = 42;
  862. state->n_commit_rounds = 43;
  863. state->n_protocol_runs = 44;
  864. reset_state_for_new_protocol_run(now);
  865. tt_int_op(state->n_reveal_rounds, ==, 0);
  866. tt_int_op(state->n_commit_rounds, ==, 0);
  867. tt_u64_op(state->n_protocol_runs, ==, 45);
  868. tt_int_op(digestmap_size(state->commits), ==, 0);
  869. }
  870. /* Test SRV rotation in our state. */
  871. {
  872. sr_srv_t *cur, *prev;
  873. test_sr_setup_srv(1);
  874. cur = sr_state_get_current_srv();
  875. tt_assert(cur);
  876. /* After, current srv should be the previous and then set to NULL. */
  877. state_rotate_srv();
  878. prev = sr_state_get_previous_srv();
  879. tt_assert(prev == cur);
  880. tt_assert(!sr_state_get_current_srv());
  881. }
  882. /* New protocol run. */
  883. {
  884. sr_srv_t *cur;
  885. /* Setup some new SRVs so we can confirm that a new protocol run
  886. * actually makes them rotate and compute new ones. */
  887. test_sr_setup_srv(1);
  888. cur = sr_state_get_current_srv();
  889. tt_assert(cur);
  890. set_sr_phase(SR_PHASE_REVEAL);
  891. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  892. new_protocol_run(now);
  893. UNMOCK(get_my_v3_authority_cert);
  894. /* Rotation happened. */
  895. tt_assert(sr_state_get_previous_srv() == cur);
  896. /* We are going into COMMIT phase so we had to rotate our SRVs. Usually
  897. * our current SRV would be NULL but a new protocol run should make us
  898. * compute a new SRV. */
  899. tt_assert(sr_state_get_current_srv());
  900. /* Also, make sure we did change the current. */
  901. tt_assert(sr_state_get_current_srv() != cur);
  902. /* We should have our commitment alone. */
  903. tt_int_op(digestmap_size(state->commits), ==, 1);
  904. tt_int_op(state->n_reveal_rounds, ==, 0);
  905. tt_int_op(state->n_commit_rounds, ==, 0);
  906. /* 46 here since we were at 45 just before. */
  907. tt_u64_op(state->n_protocol_runs, ==, 46);
  908. }
  909. /* Cleanup of SRVs. */
  910. {
  911. sr_state_clean_srvs();
  912. tt_assert(!sr_state_get_current_srv());
  913. tt_assert(!sr_state_get_previous_srv());
  914. }
  915. done:
  916. return;
  917. }
  918. static void
  919. test_keep_commit(void *arg)
  920. {
  921. char fp[FINGERPRINT_LEN + 1];
  922. sr_commit_t *commit = NULL, *dup_commit = NULL;
  923. sr_state_t *state;
  924. time_t now = time(NULL);
  925. (void) arg;
  926. { /* Setup a minimal dirauth environment for this test */
  927. crypto_pk_t *k = crypto_pk_new();
  928. /* Have a key that is not the one from our commit. */
  929. tt_int_op(0, ==, crypto_pk_generate_key(k));
  930. tt_int_op(0, ==, crypto_pk_get_fingerprint(k, fp, 0));
  931. init_authority_state();
  932. state = get_sr_state();
  933. }
  934. /* Test this very important function that tells us if we should keep a
  935. * commit or not in our state. Most of it depends on the phase and what's
  936. * in the commit so we'll change the commit as we go. */
  937. commit = sr_generate_our_commit(now, mock_cert);
  938. tt_assert(commit);
  939. /* Set us in COMMIT phase for starter. */
  940. set_sr_phase(SR_PHASE_COMMIT);
  941. /* We should never keep a commit from a non authoritative authority. */
  942. tt_int_op(should_keep_commit(commit, fp, SR_PHASE_COMMIT), ==, 0);
  943. /* This should NOT be kept because it has a reveal value in it. */
  944. tt_assert(commit_has_reveal_value(commit));
  945. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  946. SR_PHASE_COMMIT), ==, 0);
  947. /* Add it to the state which should return to not keep it. */
  948. sr_state_add_commit(commit);
  949. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  950. SR_PHASE_COMMIT), ==, 0);
  951. /* Remove it from state so we can continue our testing. */
  952. digestmap_remove(state->commits, commit->rsa_identity);
  953. /* Let's remove our reveal value which should make it OK to keep it. */
  954. memset(commit->encoded_reveal, 0, sizeof(commit->encoded_reveal));
  955. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  956. SR_PHASE_COMMIT), ==, 1);
  957. /* Let's reset our commit and go into REVEAL phase. */
  958. sr_commit_free(commit);
  959. commit = sr_generate_our_commit(now, mock_cert);
  960. tt_assert(commit);
  961. /* Dup the commit so we have one with and one without a reveal value. */
  962. dup_commit = tor_malloc_zero(sizeof(*dup_commit));
  963. memcpy(dup_commit, commit, sizeof(*dup_commit));
  964. memset(dup_commit->encoded_reveal, 0, sizeof(dup_commit->encoded_reveal));
  965. set_sr_phase(SR_PHASE_REVEAL);
  966. /* We should never keep a commit from a non authoritative authority. */
  967. tt_int_op(should_keep_commit(commit, fp, SR_PHASE_REVEAL), ==, 0);
  968. /* We shouldn't accept a commit that is not in our state. */
  969. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  970. SR_PHASE_REVEAL), ==, 0);
  971. /* Important to add the commit _without_ the reveal here. */
  972. sr_state_add_commit(dup_commit);
  973. tt_int_op(digestmap_size(state->commits), ==, 1);
  974. /* Our commit should be valid that is authoritative, contains a reveal, be
  975. * in the state and commitment and reveal values match. */
  976. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  977. SR_PHASE_REVEAL), ==, 1);
  978. /* The commit shouldn't be kept if it's not verified that is no matchin
  979. * hashed reveal. */
  980. {
  981. /* Let's save the hash reveal so we can restore it. */
  982. sr_commit_t place_holder;
  983. memcpy(place_holder.hashed_reveal, commit->hashed_reveal,
  984. sizeof(place_holder.hashed_reveal));
  985. memset(commit->hashed_reveal, 0, sizeof(commit->hashed_reveal));
  986. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  987. SR_PHASE_REVEAL), ==, 0);
  988. memcpy(commit->hashed_reveal, place_holder.hashed_reveal,
  989. sizeof(commit->hashed_reveal));
  990. }
  991. /* We shouldn't keep a commit that has no reveal. */
  992. tt_int_op(should_keep_commit(dup_commit, dup_commit->rsa_identity,
  993. SR_PHASE_REVEAL), ==, 0);
  994. /* We must not keep a commit that is not the same from the commit phase. */
  995. memset(commit->encoded_commit, 0, sizeof(commit->encoded_commit));
  996. tt_int_op(should_keep_commit(commit, commit->rsa_identity,
  997. SR_PHASE_REVEAL), ==, 0);
  998. done:
  999. sr_commit_free(commit);
  1000. sr_commit_free(dup_commit);
  1001. }
  1002. static void
  1003. test_state_update(void *arg)
  1004. {
  1005. time_t commit_phase_time = 1452076000;
  1006. time_t reveal_phase_time = 1452086800;
  1007. sr_state_t *state;
  1008. (void) arg;
  1009. {
  1010. init_authority_state();
  1011. state = get_sr_state();
  1012. set_sr_phase(SR_PHASE_COMMIT);
  1013. /* We'll cheat a bit here and reset the creation time of the state which
  1014. * will avoid us to compute a valid_after time that fits the commit
  1015. * phase. */
  1016. state->valid_after = 0;
  1017. state->n_reveal_rounds = 0;
  1018. state->n_commit_rounds = 0;
  1019. }
  1020. /* We need to mock for the state update function call. */
  1021. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1022. /* We are in COMMIT phase here and we'll trigger a state update but no
  1023. * transition. */
  1024. sr_state_update(commit_phase_time);
  1025. tt_int_op(state->valid_after, ==, commit_phase_time);
  1026. tt_int_op(state->n_commit_rounds, ==, 1);
  1027. tt_int_op(state->phase, ==, SR_PHASE_COMMIT);
  1028. tt_int_op(digestmap_size(state->commits), ==, 1);
  1029. /* We are still in the COMMIT phase here but we'll trigger a state
  1030. * transition to the REVEAL phase. */
  1031. sr_state_update(reveal_phase_time);
  1032. tt_int_op(state->phase, ==, SR_PHASE_REVEAL);
  1033. tt_int_op(state->valid_after, ==, reveal_phase_time);
  1034. /* Only our commit should be in there. */
  1035. tt_int_op(digestmap_size(state->commits), ==, 1);
  1036. tt_int_op(state->n_reveal_rounds, ==, 1);
  1037. /* We can't update a state with a valid after _lower_ than the creation
  1038. * time so here it is. */
  1039. sr_state_update(commit_phase_time);
  1040. tt_int_op(state->valid_after, ==, reveal_phase_time);
  1041. /* Finally, let's go back in COMMIT phase so we can test the state update
  1042. * of a new protocol run. */
  1043. state->valid_after = 0;
  1044. sr_state_update(commit_phase_time);
  1045. tt_int_op(state->valid_after, ==, commit_phase_time);
  1046. tt_int_op(state->n_commit_rounds, ==, 1);
  1047. tt_int_op(state->n_reveal_rounds, ==, 0);
  1048. tt_u64_op(state->n_protocol_runs, ==, 1);
  1049. tt_int_op(state->phase, ==, SR_PHASE_COMMIT);
  1050. tt_int_op(digestmap_size(state->commits), ==, 1);
  1051. tt_assert(state->current_srv);
  1052. done:
  1053. sr_state_free();
  1054. UNMOCK(get_my_v3_authority_cert);
  1055. }
  1056. struct testcase_t sr_tests[] = {
  1057. { "get_sr_protocol_phase", test_get_sr_protocol_phase, TT_FORK,
  1058. NULL, NULL },
  1059. { "sr_commit", test_sr_commit, TT_FORK,
  1060. NULL, NULL },
  1061. { "keep_commit", test_keep_commit, TT_FORK,
  1062. NULL, NULL },
  1063. { "encoding", test_encoding, TT_FORK,
  1064. NULL, NULL },
  1065. { "get_next_valid_after_time", test_get_next_valid_after_time, TT_FORK,
  1066. NULL, NULL },
  1067. { "get_state_valid_until_time", test_get_state_valid_until_time, TT_FORK,
  1068. NULL, NULL },
  1069. { "vote", test_vote, TT_FORK,
  1070. NULL, NULL },
  1071. { "state_load_from_disk", test_state_load_from_disk, TT_FORK,
  1072. NULL, NULL },
  1073. { "sr_compute_srv", test_sr_compute_srv, TT_FORK, NULL, NULL },
  1074. { "sr_get_majority_srv_from_votes", test_sr_get_majority_srv_from_votes,
  1075. TT_FORK, NULL, NULL },
  1076. { "utils", test_utils, TT_FORK, NULL, NULL },
  1077. { "state_transition", test_state_transition, TT_FORK, NULL, NULL },
  1078. { "state_update", test_state_update, TT_FORK,
  1079. NULL, NULL },
  1080. END_OF_TESTCASES
  1081. };