test_shared_random.c 43 KB

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