test_shared_random.c 43 KB

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