test_shared_random.c 46 KB

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