test_shared_random.c 59 KB

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