12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304 |
- #define SHARED_RANDOM_PRIVATE
- #define SHARED_RANDOM_STATE_PRIVATE
- #define CONFIG_PRIVATE
- #define DIRVOTE_PRIVATE
- #include "or.h"
- #include "test.h"
- #include "config.h"
- #include "dirvote.h"
- #include "shared_random.h"
- #include "shared_random_state.h"
- #include "routerkeys.h"
- #include "routerlist.h"
- #include "router.h"
- #include "routerparse.h"
- #include "networkstatus.h"
- #include "log_test_helpers.h"
- static authority_cert_t *mock_cert;
- static authority_cert_t *
- get_my_v3_authority_cert_m(void)
- {
- tor_assert(mock_cert);
- return mock_cert;
- }
- static dir_server_t ds;
- static dir_server_t *
- trusteddirserver_get_by_v3_auth_digest_m(const char *digest)
- {
- (void) digest;
-
- return &ds;
- }
- static void
- init_authority_state(void)
- {
- MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
- or_options_t *options = get_options_mutable();
- mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
- tt_assert(mock_cert);
- options->AuthoritativeDir = 1;
- tt_int_op(0, ==, load_ed_keys(options, time(NULL)));
- sr_state_init(0, 0);
-
- sr_state_delete_commits();
-
- sr_state_clean_srvs();
- done:
- UNMOCK(get_my_v3_authority_cert);
- }
- static void
- test_get_sr_protocol_phase(void *arg)
- {
- time_t the_time;
- sr_phase_t phase;
- int retval;
- (void) arg;
-
- init_authority_state();
- {
- retval = parse_rfc1123_time("Wed, 20 Apr 2015 23:59:00 UTC", &the_time);
- tt_int_op(retval, ==, 0);
- phase = get_sr_protocol_phase(the_time);
- tt_int_op(phase, ==, SR_PHASE_REVEAL);
- }
- {
- retval = parse_rfc1123_time("Wed, 20 Apr 2015 00:00:00 UTC", &the_time);
- tt_int_op(retval, ==, 0);
- phase = get_sr_protocol_phase(the_time);
- tt_int_op(phase, ==, SR_PHASE_COMMIT);
- }
- {
- retval = parse_rfc1123_time("Wed, 20 Apr 2015 00:00:01 UTC", &the_time);
- tt_int_op(retval, ==, 0);
- phase = get_sr_protocol_phase(the_time);
- tt_int_op(phase, ==, SR_PHASE_COMMIT);
- }
- {
- retval = parse_rfc1123_time("Wed, 20 Apr 2015 11:59:00 UTC", &the_time);
- tt_int_op(retval, ==, 0);
- phase = get_sr_protocol_phase(the_time);
- tt_int_op(phase, ==, SR_PHASE_COMMIT);
- }
- {
- retval = parse_rfc1123_time("Wed, 20 Apr 2015 12:00:00 UTC", &the_time);
- tt_int_op(retval, ==, 0);
- phase = get_sr_protocol_phase(the_time);
- tt_int_op(phase, ==, SR_PHASE_REVEAL);
- }
- {
- retval = parse_rfc1123_time("Wed, 20 Apr 2015 12:00:01 UTC", &the_time);
- tt_int_op(retval, ==, 0);
- phase = get_sr_protocol_phase(the_time);
- tt_int_op(phase, ==, SR_PHASE_REVEAL);
- }
- {
- retval = parse_rfc1123_time("Wed, 20 Apr 2015 13:00:00 UTC", &the_time);
- tt_int_op(retval, ==, 0);
- phase = get_sr_protocol_phase(the_time);
- tt_int_op(phase, ==, SR_PHASE_REVEAL);
- }
- done:
- ;
- }
- static networkstatus_t *mock_consensus = NULL;
- static void
- test_get_state_valid_until_time(void *arg)
- {
- time_t current_time;
- time_t valid_until_time;
- char tbuf[ISO_TIME_LEN + 1];
- int retval;
- (void) arg;
- {
-
- retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:01 UTC",
- ¤t_time);
- tt_int_op(retval, ==, 0);
- valid_until_time = get_state_valid_until_time(current_time);
-
- format_iso_time(tbuf, valid_until_time);
- tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
- }
- {
- retval = parse_rfc1123_time("Mon, 20 Apr 2015 19:22:00 UTC",
- ¤t_time);
- tt_int_op(retval, ==, 0);
- valid_until_time = get_state_valid_until_time(current_time);
- format_iso_time(tbuf, valid_until_time);
- tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
- }
- {
- retval = parse_rfc1123_time("Mon, 20 Apr 2015 23:59:00 UTC",
- ¤t_time);
- tt_int_op(retval, ==, 0);
- valid_until_time = get_state_valid_until_time(current_time);
- format_iso_time(tbuf, valid_until_time);
- tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
- }
- {
- retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
- ¤t_time);
- tt_int_op(retval, ==, 0);
- valid_until_time = get_state_valid_until_time(current_time);
- format_iso_time(tbuf, valid_until_time);
- tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
- }
- done:
- ;
- }
- static networkstatus_t *
- mock_networkstatus_get_live_consensus(time_t now)
- {
- (void) now;
- return mock_consensus;
- }
- static void
- test_get_next_valid_after_time(void *arg)
- {
- time_t current_time;
- time_t valid_after_time;
- char tbuf[ISO_TIME_LEN + 1];
- int retval;
- (void) arg;
- {
-
- mock_consensus = tor_malloc_zero(sizeof(networkstatus_t));
- retval = parse_rfc1123_time("Mon, 13 Jan 2016 16:00:00 UTC",
- &mock_consensus->fresh_until);
- tt_int_op(retval, ==, 0);
- retval = parse_rfc1123_time("Mon, 13 Jan 2016 15:00:00 UTC",
- &mock_consensus->valid_after);
- tt_int_op(retval, ==, 0);
- MOCK(networkstatus_get_live_consensus,
- mock_networkstatus_get_live_consensus);
- }
- {
-
- retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
- ¤t_time);
- tt_int_op(retval, ==, 0);
- valid_after_time = get_next_valid_after_time(current_time);
-
- format_iso_time(tbuf, valid_after_time);
- tt_str_op("2015-04-20 01:00:00", OP_EQ, tbuf);
- }
- {
-
- retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:01 UTC",
- ¤t_time);
- tt_int_op(retval, ==, 0);
- valid_after_time = get_next_valid_after_time(current_time);
-
- format_iso_time(tbuf, valid_after_time);
- tt_str_op("2015-04-20 01:00:00", OP_EQ, tbuf);
- }
- {
- retval = parse_rfc1123_time("Mon, 20 Apr 2015 23:30:01 UTC",
- ¤t_time);
- tt_int_op(retval, ==, 0);
- valid_after_time = get_next_valid_after_time(current_time);
-
- format_iso_time(tbuf, valid_after_time);
- tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
- }
- done:
- networkstatus_vote_free(mock_consensus);
- }
- static void
- test_sr_commit(void *arg)
- {
- authority_cert_t *auth_cert = NULL;
- time_t now = time(NULL);
- sr_commit_t *our_commit = NULL;
- smartlist_t *args = smartlist_new();
- sr_commit_t *parsed_commit = NULL;
- (void) arg;
- {
- or_options_t *options = get_options_mutable();
- auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
- tt_assert(auth_cert);
- options->AuthoritativeDir = 1;
- tt_int_op(0, ==, load_ed_keys(options, now));
- }
-
- {
- sr_commit_t test_commit;
- our_commit = sr_generate_our_commit(now, auth_cert);
- tt_assert(our_commit);
-
- tt_assert(our_commit->alg == DIGEST_SHA3_256);
-
- tt_assert(commit_has_reveal_value(our_commit));
-
- tt_assert(!tor_mem_is_zero((char *) our_commit->random_number,
- sizeof(our_commit->random_number)));
-
- tt_u64_op(our_commit->commit_ts, ==, our_commit->reveal_ts);
-
- tt_assert(!tor_mem_is_zero(our_commit->hashed_reveal,
- sizeof(our_commit->hashed_reveal)));
-
- tt_int_op(0, ==, reveal_decode(our_commit->encoded_reveal,
- &test_commit));
- tt_int_op(0, ==, commit_decode(our_commit->encoded_commit,
- &test_commit));
- tt_int_op(0, ==, verify_commit_and_reveal(our_commit));
- }
-
- {
-
- sr_commit_t test_commit;
- memcpy(&test_commit, our_commit, sizeof(test_commit));
-
- test_commit.commit_ts = test_commit.reveal_ts - 42;
- setup_full_capture_of_logs(LOG_WARN);
- tt_int_op(-1, ==, verify_commit_and_reveal(&test_commit));
- expect_log_msg_containing("doesn't match reveal timestamp");
- teardown_capture_of_logs();
- memcpy(&test_commit, our_commit, sizeof(test_commit));
- tt_int_op(0, ==, verify_commit_and_reveal(&test_commit));
-
- memset(test_commit.hashed_reveal, 'X',
- sizeof(test_commit.hashed_reveal));
- setup_full_capture_of_logs(LOG_WARN);
- tt_int_op(-1, ==, verify_commit_and_reveal(&test_commit));
- expect_single_log_msg_containing("doesn't match the commit value");
- teardown_capture_of_logs();
- memcpy(&test_commit, our_commit, sizeof(test_commit));
- tt_int_op(0, ==, verify_commit_and_reveal(&test_commit));
- }
-
- {
- smartlist_add(args, tor_strdup("1"));
- smartlist_add(args,
- tor_strdup(crypto_digest_algorithm_get_name(our_commit->alg)));
- smartlist_add(args, tor_strdup(sr_commit_get_rsa_fpr(our_commit)));
- smartlist_add(args, tor_strdup(our_commit->encoded_commit));
- smartlist_add(args, tor_strdup(our_commit->encoded_reveal));
- parsed_commit = sr_parse_commit(args);
- tt_assert(parsed_commit);
-
- parsed_commit->valid = 1;
- tt_mem_op(parsed_commit, OP_EQ, our_commit, sizeof(*parsed_commit));
-
- }
- done:
- teardown_capture_of_logs();
- SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
- smartlist_free(args);
- sr_commit_free(our_commit);
- sr_commit_free(parsed_commit);
- authority_cert_free(auth_cert);
- }
- static void
- test_encoding(void *arg)
- {
- (void) arg;
- int ret;
-
- char raw_rand[32];
- time_t ts = 1454333590;
- char hashed_rand[DIGEST256_LEN], hashed_reveal[DIGEST256_LEN];
- sr_commit_t parsed_commit;
-
- static const char *encoded_reveal =
- "AAAAAFavXpZJxbwTupvaJCTeIUCQmOPxAMblc7ChL5H2nZKuGchdaA==";
- static const char *encoded_commit =
- "AAAAAFavXpbkBMzMQG7aNoaGLFNpm2Wkk1ozXhuWWqL//GynltxVAg==";
-
- memset(raw_rand, 'A', sizeof(raw_rand));
-
- ret = crypto_digest256(hashed_rand, raw_rand,
- sizeof(raw_rand), SR_DIGEST_ALG);
- tt_int_op(0, ==, ret);
-
- tt_int_op(SR_REVEAL_BASE64_LEN, ==, strlen(encoded_reveal));
- ret = crypto_digest256(hashed_reveal, encoded_reveal,
- strlen(encoded_reveal), SR_DIGEST_ALG);
- tt_int_op(0, ==, ret);
- tt_int_op(SR_COMMIT_BASE64_LEN, ==, strlen(encoded_commit));
-
- {
-
- tt_int_op(0, ==, reveal_decode(encoded_reveal, &parsed_commit));
- tt_u64_op(ts, ==, parsed_commit.reveal_ts);
- tt_mem_op(hashed_rand, OP_EQ, parsed_commit.random_number,
- sizeof(hashed_rand));
-
- memset(&parsed_commit, 0, sizeof(parsed_commit));
- tt_int_op(0, ==, commit_decode(encoded_commit, &parsed_commit));
- tt_u64_op(ts, ==, parsed_commit.commit_ts);
- tt_mem_op(encoded_commit, OP_EQ, parsed_commit.encoded_commit,
- sizeof(parsed_commit.encoded_commit));
- tt_mem_op(hashed_reveal, OP_EQ, parsed_commit.hashed_reveal,
- sizeof(hashed_reveal));
- }
-
- {
-
- char encoded[SR_REVEAL_BASE64_LEN + 1];
- parsed_commit.reveal_ts = ts;
- memcpy(parsed_commit.random_number, hashed_rand,
- sizeof(parsed_commit.random_number));
- ret = reveal_encode(&parsed_commit, encoded, sizeof(encoded));
- tt_int_op(SR_REVEAL_BASE64_LEN, ==, ret);
- tt_mem_op(encoded_reveal, OP_EQ, encoded, strlen(encoded_reveal));
- }
- {
-
- char encoded[SR_COMMIT_BASE64_LEN + 1];
- parsed_commit.commit_ts = ts;
- memcpy(parsed_commit.hashed_reveal, hashed_reveal,
- sizeof(parsed_commit.hashed_reveal));
- ret = commit_encode(&parsed_commit, encoded, sizeof(encoded));
- tt_int_op(SR_COMMIT_BASE64_LEN, ==, ret);
- tt_mem_op(encoded_commit, OP_EQ, encoded, strlen(encoded_commit));
- }
- done:
- ;
- }
- static void
- test_sr_setup_srv(int also_current)
- {
-
- sr_state_clean_srvs();
- sr_srv_t *srv = tor_malloc_zero(sizeof(sr_srv_t));
- srv->num_reveals = 42;
- memcpy(srv->value,
- "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
- sizeof(srv->value));
- sr_state_set_previous_srv(srv);
- if (also_current) {
- srv = tor_malloc_zero(sizeof(sr_srv_t));
- srv->num_reveals = 128;
- memcpy(srv->value,
- "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN",
- sizeof(srv->value));
- sr_state_set_current_srv(srv);
- }
- }
- static void
- test_vote(void *arg)
- {
- int ret;
- time_t now = time(NULL);
- sr_commit_t *our_commit = NULL;
- (void) arg;
- MOCK(trusteddirserver_get_by_v3_auth_digest,
- trusteddirserver_get_by_v3_auth_digest_m);
- {
- init_authority_state();
-
- set_sr_phase(SR_PHASE_REVEAL);
- }
-
- {
- sr_commit_t *saved_commit;
- our_commit = sr_generate_our_commit(now, mock_cert);
- tt_assert(our_commit);
- sr_state_add_commit(our_commit);
-
- saved_commit = sr_state_get_commit(our_commit->rsa_identity);
- tt_assert(saved_commit);
- }
-
- test_sr_setup_srv(1);
- {
- smartlist_t *chunks = smartlist_new();
- smartlist_t *tokens = smartlist_new();
-
- char *lines = sr_get_string_for_vote();
- tt_assert(lines);
-
- ret = smartlist_split_string(chunks, lines, "\n", SPLIT_IGNORE_BLANK, 0);
- tt_int_op(ret, ==, 4);
- tt_str_op(smartlist_get(chunks, 0), OP_EQ, "shared-rand-participate");
-
- char *commit_line = smartlist_get(chunks, 1);
- tt_assert(commit_line);
- ret = smartlist_split_string(tokens, commit_line, " ", 0, 0);
- tt_int_op(ret, ==, 6);
- tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-commit");
- tt_str_op(smartlist_get(tokens, 1), OP_EQ, "1");
- tt_str_op(smartlist_get(tokens, 2), OP_EQ,
- crypto_digest_algorithm_get_name(DIGEST_SHA3_256));
- char digest[DIGEST_LEN];
- base16_decode(digest, sizeof(digest), smartlist_get(tokens, 3),
- HEX_DIGEST_LEN);
- tt_mem_op(digest, ==, our_commit->rsa_identity, sizeof(digest));
- tt_str_op(smartlist_get(tokens, 4), OP_EQ, our_commit->encoded_commit);
- tt_str_op(smartlist_get(tokens, 5), OP_EQ, our_commit->encoded_reveal)
- ;
-
- smartlist_t *args = smartlist_new();
- smartlist_add(args, smartlist_get(tokens, 1));
- smartlist_add(args, smartlist_get(tokens, 2));
- smartlist_add(args, smartlist_get(tokens, 3));
- smartlist_add(args, smartlist_get(tokens, 4));
- smartlist_add(args, smartlist_get(tokens, 5));
- sr_commit_t *parsed_commit = sr_parse_commit(args);
- tt_assert(parsed_commit);
-
- parsed_commit->valid = 1;
- tt_mem_op(parsed_commit, ==, our_commit, sizeof(*our_commit));
-
- SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
- smartlist_clear(tokens);
-
- char *prev_srv_line = smartlist_get(chunks, 2);
- tt_assert(prev_srv_line);
- ret = smartlist_split_string(tokens, prev_srv_line, " ", 0, 0);
- tt_int_op(ret, ==, 3);
- tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-previous-value");
- tt_str_op(smartlist_get(tokens, 1), OP_EQ, "42");
- tt_str_op(smartlist_get(tokens, 2), OP_EQ,
- "WlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlo=");
-
- SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
- smartlist_clear(tokens);
-
- char *current_srv_line = smartlist_get(chunks, 3);
- tt_assert(current_srv_line);
- ret = smartlist_split_string(tokens, current_srv_line, " ", 0, 0);
- tt_int_op(ret, ==, 3);
- tt_str_op(smartlist_get(tokens, 0), OP_EQ, "shared-rand-current-value");
- tt_str_op(smartlist_get(tokens, 1), OP_EQ, "128");
- tt_str_op(smartlist_get(tokens, 2), OP_EQ,
- "Tk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk5OTk4=");
-
- sr_commit_free(parsed_commit);
- SMARTLIST_FOREACH(chunks, char *, s, tor_free(s));
- smartlist_free(chunks);
- SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
- smartlist_free(tokens);
- smartlist_clear(args);
- smartlist_free(args);
- tor_free(lines);
- }
- done:
- UNMOCK(trusteddirserver_get_by_v3_auth_digest);
- sr_state_free();
- }
- static const char *sr_state_str = "Version 1\n"
- "TorVersion 0.2.9.0-alpha-dev\n"
- "ValidAfter 2037-04-19 07:16:00\n"
- "ValidUntil 2037-04-20 07:16:00\n"
- "Commit 1 sha3-256 FA3CEC2C99DC68D3166B9B6E4FA21A4026C2AB1C "
- "7M8GdubCAAdh7WUG0DiwRyxTYRKji7HATa7LLJEZ/UAAAAAAVmfUSg== "
- "AAAAAFZn1EojfIheIw42bjK3VqkpYyjsQFSbv/dxNna3Q8hUEPKpOw==\n"
- "Commit 1 sha3-256 41E89EDFBFBA44983E21F18F2230A4ECB5BFB543 "
- "17aUsYuMeRjd2N1r8yNyg7aHqRa6gf4z7QPoxxAZbp0AAAAAVmfUSg==\n"
- "Commit 1 sha3-256 36637026573A04110CF3E6B1D201FB9A98B88734 "
- "DDDYtripvdOU+XPEUm5xpU64d9IURSds1xSwQsgeB8oAAAAAVmfUSg==\n"
- "SharedRandPreviousValue 4 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqo=\n"
- "SharedRandCurrentValue 3 8dWeW12KEzTGEiLGgO1UVJ7Z91CekoRcxt6Q9KhnOFI=\n";
- static void
- test_state_load_from_disk(void *arg)
- {
- int ret;
- char *dir = tor_strdup(get_fname("test_sr_state"));
- char *sr_state_path = tor_strdup(get_fname("test_sr_state/sr_state"));
- sr_state_t *the_sr_state = NULL;
- (void) arg;
- MOCK(trusteddirserver_get_by_v3_auth_digest,
- trusteddirserver_get_by_v3_auth_digest_m);
-
- ret = disk_state_load_from_disk_impl("NONEXISTENTNONEXISTENT");
- tt_assert(ret == -ENOENT);
-
- #ifdef _WIN32
- ret = mkdir(dir);
- #else
- ret = mkdir(dir, 0700);
- #endif
- tt_assert(ret == 0);
- ret = write_str_to_file(sr_state_path, sr_state_str, 0);
- tt_assert(ret == 0);
-
- ret = disk_state_load_from_disk_impl(dir);
- tt_int_op(ret, OP_LT, 0);
-
- the_sr_state = get_sr_state();
- tt_assert(!the_sr_state);
-
- ret = disk_state_load_from_disk_impl(sr_state_path);
- tt_assert(ret == 0);
-
-
- the_sr_state = get_sr_state();
- tt_assert(the_sr_state);
- tt_assert(the_sr_state->version == 1);
- tt_assert(digestmap_size(the_sr_state->commits) == 3);
- tt_assert(the_sr_state->current_srv);
- tt_assert(the_sr_state->current_srv->num_reveals == 3);
- tt_assert(the_sr_state->previous_srv);
-
- done:
- tor_free(dir);
- tor_free(sr_state_path);
- UNMOCK(trusteddirserver_get_by_v3_auth_digest);
- }
- static void
- test_sr_setup_commits(void)
- {
- time_t now = time(NULL);
- sr_commit_t *commit_a, *commit_b, *commit_c, *commit_d;
- sr_commit_t *place_holder = tor_malloc_zero(sizeof(*place_holder));
- authority_cert_t *auth_cert = NULL;
- {
- or_options_t *options = get_options_mutable();
- auth_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
- tt_assert(auth_cert);
- options->AuthoritativeDir = 1;
- tt_int_op(0, ==, load_ed_keys(options, now));
- }
-
- {
- commit_a = sr_generate_our_commit(now, auth_cert);
- tt_assert(commit_a);
-
- memset(commit_a->rsa_identity, 'A', sizeof(commit_a->rsa_identity));
- base16_encode(commit_a->rsa_identity_hex,
- sizeof(commit_a->rsa_identity_hex), commit_a->rsa_identity,
- sizeof(commit_a->rsa_identity));
- strlcpy(commit_a->encoded_reveal,
- "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
- sizeof(commit_a->encoded_reveal));
- memcpy(commit_a->hashed_reveal,
- "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
- sizeof(commit_a->hashed_reveal));
- }
- {
- commit_b = sr_generate_our_commit(now, auth_cert);
- tt_assert(commit_b);
-
- memset(commit_b->rsa_identity, 'B', sizeof(commit_b->rsa_identity));
- base16_encode(commit_b->rsa_identity_hex,
- sizeof(commit_b->rsa_identity_hex), commit_b->rsa_identity,
- sizeof(commit_b->rsa_identity));
- strlcpy(commit_b->encoded_reveal,
- "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
- sizeof(commit_b->encoded_reveal));
- memcpy(commit_b->hashed_reveal,
- "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
- sizeof(commit_b->hashed_reveal));
- }
- {
- commit_c = sr_generate_our_commit(now, auth_cert);
- tt_assert(commit_c);
-
- memset(commit_c->rsa_identity, 'C', sizeof(commit_c->rsa_identity));
- base16_encode(commit_c->rsa_identity_hex,
- sizeof(commit_c->rsa_identity_hex), commit_c->rsa_identity,
- sizeof(commit_c->rsa_identity));
- strlcpy(commit_c->encoded_reveal,
- "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
- sizeof(commit_c->encoded_reveal));
- memcpy(commit_c->hashed_reveal,
- "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
- sizeof(commit_c->hashed_reveal));
- }
- {
- commit_d = sr_generate_our_commit(now, auth_cert);
- tt_assert(commit_d);
-
- memset(commit_d->rsa_identity, 'D', sizeof(commit_d->rsa_identity));
- base16_encode(commit_d->rsa_identity_hex,
- sizeof(commit_d->rsa_identity_hex), commit_d->rsa_identity,
- sizeof(commit_d->rsa_identity));
- strlcpy(commit_d->encoded_reveal,
- "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
- sizeof(commit_d->encoded_reveal));
- memcpy(commit_d->hashed_reveal,
- "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
- sizeof(commit_d->hashed_reveal));
-
- memcpy(place_holder, commit_d, sizeof(*place_holder));
- memset(commit_d->encoded_reveal, 0, sizeof(commit_d->encoded_reveal));
- tt_assert(!commit_has_reveal_value(commit_d));
- }
-
- set_sr_phase(SR_PHASE_COMMIT);
- save_commit_to_state(commit_a);
- save_commit_to_state(commit_b);
- save_commit_to_state(commit_c);
- save_commit_to_state(commit_d);
- tt_int_op(digestmap_size(get_sr_state()->commits), ==, 4);
-
- set_sr_phase(SR_PHASE_REVEAL);
- save_commit_to_state(place_holder);
- place_holder = NULL;
- tt_str_op(commit_d->encoded_reveal, OP_EQ,
- "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
-
- memset(commit_d->encoded_reveal, 0, sizeof(commit_d->encoded_reveal));
- memset(commit_d->random_number, 0, sizeof(commit_d->random_number));
- tt_assert(!commit_has_reveal_value(commit_d));
- done:
- tor_free(place_holder);
- authority_cert_free(auth_cert);
- }
- static void
- test_sr_compute_srv(void *arg)
- {
- (void) arg;
- const sr_srv_t *current_srv = NULL;
- #define SRV_TEST_VECTOR \
- "2A9B1D6237DAB312A40F575DA85C147663E7ED3F80E9555395F15B515C74253D"
- MOCK(trusteddirserver_get_by_v3_auth_digest,
- trusteddirserver_get_by_v3_auth_digest_m);
- init_authority_state();
-
- test_sr_setup_commits();
- test_sr_setup_srv(0);
-
- set_sr_phase(SR_PHASE_REVEAL);
-
- sr_compute_srv();
-
- current_srv = sr_state_get_current_srv();
- tt_assert(current_srv);
- tt_u64_op(current_srv->num_reveals, ==, 3);
- tt_str_op(hex_str((char*)current_srv->value, 32),
- ==,
- SRV_TEST_VECTOR);
- done:
- UNMOCK(trusteddirserver_get_by_v3_auth_digest);
- sr_state_free();
- }
- static networkstatus_t *
- get_test_vote_with_curr_srv(const char *srv)
- {
- networkstatus_t *vote = tor_malloc_zero(sizeof(networkstatus_t));
- vote->type = NS_TYPE_VOTE;
- vote->sr_info.participate = 1;
- vote->sr_info.current_srv = tor_malloc_zero(sizeof(sr_srv_t));
- vote->sr_info.current_srv->num_reveals = 42;
- memcpy(vote->sr_info.current_srv->value,
- srv,
- sizeof(vote->sr_info.current_srv->value));
- return vote;
- }
- static void
- test_sr_get_majority_srv_from_votes(void *arg)
- {
- sr_srv_t *chosen_srv;
- smartlist_t *votes = smartlist_new();
- #define SRV_1 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
- #define SRV_2 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
- (void) arg;
- init_authority_state();
-
- sr_state_set_fresh_srv();
-
- clear_dir_servers();
- add_default_trusted_dir_authorities(V3_DIRINFO);
- {
- networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
- smartlist_add(votes, vote);
- }
-
- chosen_srv = get_majority_srv_from_votes(votes, 1);
- tt_assert(!chosen_srv);
- {
- int i;
-
- for (i = 0; i < 3; i++) {
- networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
- smartlist_add(votes, vote);
- }
-
- for (i = 0; i < 2; i++) {
- networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_2);
- smartlist_add(votes, vote);
- }
- for (i = 0; i < 3; i++) {
- networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
- smartlist_add(votes, vote);
- }
- tt_int_op(smartlist_len(votes), ==, 9);
- }
-
- set_num_srv_agreements(8);
- chosen_srv = get_majority_srv_from_votes(votes, 1);
- tt_assert(!chosen_srv);
-
- set_num_srv_agreements(7);
- chosen_srv = get_majority_srv_from_votes(votes, 1);
- tt_assert(chosen_srv);
- tt_u64_op(chosen_srv->num_reveals, ==, 42);
- tt_mem_op(chosen_srv->value, OP_EQ, SRV_1, sizeof(chosen_srv->value));
- done:
- SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
- networkstatus_vote_free(vote));
- smartlist_free(votes);
- }
- static void
- test_utils(void *arg)
- {
- (void) arg;
-
- {
- sr_srv_t *srv = NULL, *dup_srv = NULL;
- const char *srv_value =
- "1BDB7C3E973936E4D13A49F37C859B3DC69C429334CF9412E3FEF6399C52D47A";
- srv = tor_malloc_zero(sizeof(*srv));
- srv->num_reveals = 42;
- memcpy(srv->value, srv_value, sizeof(srv->value));
- dup_srv = srv_dup(srv);
- tt_assert(dup_srv);
- tt_u64_op(dup_srv->num_reveals, ==, srv->num_reveals);
- tt_mem_op(dup_srv->value, OP_EQ, srv->value, sizeof(srv->value));
- tor_free(srv);
- tor_free(dup_srv);
- }
-
- {
-
- const char *payload =
- "\x5d\xb9\x60\xb6\xcc\x51\x68\x52\x31\xd9\x88\x88\x71\x71\xe0\x30"
- "\x59\x55\x7f\xcd\x61\xc0\x4b\x05\xb8\xcd\xc1\x48\xe9\xcd\x16\x1f"
- "\x70\x15\x0c\xfc\xd3\x1a\x75\xd0\x93\x6c\xc4\xe0\x5c\xbe\xe2\x18"
- "\xc7\xaf\x72\xb6\x7c\x9b\x52\x00";
- sr_commit_t commit1, commit2;
- memcpy(commit1.encoded_commit, payload, sizeof(commit1.encoded_commit));
- memcpy(commit2.encoded_commit, payload, sizeof(commit2.encoded_commit));
- tt_int_op(commitments_are_the_same(&commit1, &commit2), ==, 1);
-
- memset(commit1.encoded_commit, 'A', sizeof(commit1.encoded_commit));
- tt_int_op(commitments_are_the_same(&commit1, &commit2), ==, 0);
- }
-
- {
- crypto_pk_t *k = crypto_pk_new();
- char digest[DIGEST_LEN];
- sr_commit_t commit;
- tt_assert(!crypto_pk_generate_key(k));
- tt_int_op(0, ==, crypto_pk_get_digest(k, digest));
- memcpy(commit.rsa_identity, digest, sizeof(commit.rsa_identity));
- tt_int_op(commit_is_authoritative(&commit, digest), ==, 1);
-
- memset(commit.rsa_identity, 0, sizeof(commit.rsa_identity));
- tt_int_op(commit_is_authoritative(&commit, digest), ==, 0);
- crypto_pk_free(k);
- }
-
- {
- tt_str_op(get_phase_str(SR_PHASE_REVEAL), ==, "reveal");
- tt_str_op(get_phase_str(SR_PHASE_COMMIT), ==, "commit");
- }
-
- {
- init_authority_state();
- set_sr_phase(SR_PHASE_COMMIT);
- tt_int_op(is_phase_transition(SR_PHASE_REVEAL), ==, 1);
- tt_int_op(is_phase_transition(SR_PHASE_COMMIT), ==, 0);
- set_sr_phase(SR_PHASE_REVEAL);
- tt_int_op(is_phase_transition(SR_PHASE_REVEAL), ==, 0);
- tt_int_op(is_phase_transition(SR_PHASE_COMMIT), ==, 1);
-
- tt_int_op(is_phase_transition(42), ==, 1);
- }
- done:
- return;
- }
- static void
- test_state_transition(void *arg)
- {
- sr_state_t *state = NULL;
- time_t now = time(NULL);
- (void) arg;
- {
- init_authority_state();
- state = get_sr_state();
- tt_assert(state);
- }
-
- {
-
- sr_commit_t *commit = sr_generate_our_commit(now, mock_cert);
- tt_assert(commit);
- sr_state_add_commit(commit);
- tt_int_op(digestmap_size(state->commits), ==, 1);
-
- sr_state_delete_commits();
- tt_int_op(digestmap_size(state->commits), ==, 0);
-
- commit = sr_generate_our_commit(now, mock_cert);
- tt_assert(commit);
- sr_state_add_commit(commit);
- tt_int_op(digestmap_size(state->commits), ==, 1);
- state->n_reveal_rounds = 42;
- state->n_commit_rounds = 43;
- state->n_protocol_runs = 44;
- reset_state_for_new_protocol_run(now);
- tt_int_op(state->n_reveal_rounds, ==, 0);
- tt_int_op(state->n_commit_rounds, ==, 0);
- tt_u64_op(state->n_protocol_runs, ==, 45);
- tt_int_op(digestmap_size(state->commits), ==, 0);
- }
-
- {
- const sr_srv_t *cur, *prev;
- test_sr_setup_srv(1);
- cur = sr_state_get_current_srv();
- tt_assert(cur);
-
- state_rotate_srv();
- prev = sr_state_get_previous_srv();
- tt_assert(prev == cur);
- tt_assert(!sr_state_get_current_srv());
- sr_state_clean_srvs();
- }
-
- {
- const sr_srv_t *cur;
-
- test_sr_setup_srv(1);
- cur = sr_state_get_current_srv();
- tt_assert(cur);
- set_sr_phase(SR_PHASE_REVEAL);
- MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
- new_protocol_run(now);
- UNMOCK(get_my_v3_authority_cert);
-
- tt_assert(sr_state_get_previous_srv() == cur);
-
- tt_assert(sr_state_get_current_srv());
-
- tt_assert(sr_state_get_current_srv() != cur);
-
- tt_int_op(digestmap_size(state->commits), ==, 1);
- tt_int_op(state->n_reveal_rounds, ==, 0);
- tt_int_op(state->n_commit_rounds, ==, 0);
-
- tt_u64_op(state->n_protocol_runs, ==, 46);
- }
-
- {
- sr_state_clean_srvs();
- tt_assert(!sr_state_get_current_srv());
- tt_assert(!sr_state_get_previous_srv());
- }
- done:
- sr_state_free();
- }
- static void
- test_keep_commit(void *arg)
- {
- char fp[FINGERPRINT_LEN + 1];
- sr_commit_t *commit = NULL, *dup_commit = NULL;
- sr_state_t *state;
- time_t now = time(NULL);
- crypto_pk_t *k = NULL;
- (void) arg;
- MOCK(trusteddirserver_get_by_v3_auth_digest,
- trusteddirserver_get_by_v3_auth_digest_m);
- {
- k = pk_generate(1);
-
-
- init_authority_state();
- state = get_sr_state();
- }
-
- commit = sr_generate_our_commit(now, mock_cert);
- tt_assert(commit);
-
- set_sr_phase(SR_PHASE_COMMIT);
-
- tt_int_op(should_keep_commit(commit, fp, SR_PHASE_COMMIT), ==, 0);
-
- tt_assert(commit_has_reveal_value(commit));
- tt_int_op(should_keep_commit(commit, commit->rsa_identity,
- SR_PHASE_COMMIT), ==, 0);
-
- sr_state_add_commit(commit);
- tt_int_op(should_keep_commit(commit, commit->rsa_identity,
- SR_PHASE_COMMIT), ==, 0);
-
- digestmap_remove(state->commits, commit->rsa_identity);
-
- memset(commit->encoded_reveal, 0, sizeof(commit->encoded_reveal));
- tt_int_op(should_keep_commit(commit, commit->rsa_identity,
- SR_PHASE_COMMIT), ==, 1);
-
- sr_commit_free(commit);
- commit = sr_generate_our_commit(now, mock_cert);
- tt_assert(commit);
-
- dup_commit = tor_malloc_zero(sizeof(*dup_commit));
- memcpy(dup_commit, commit, sizeof(*dup_commit));
- memset(dup_commit->encoded_reveal, 0, sizeof(dup_commit->encoded_reveal));
- set_sr_phase(SR_PHASE_REVEAL);
-
- tt_int_op(should_keep_commit(commit, fp, SR_PHASE_REVEAL), ==, 0);
-
- tt_int_op(should_keep_commit(commit, commit->rsa_identity,
- SR_PHASE_REVEAL), ==, 0);
-
- sr_state_add_commit(dup_commit);
- tt_int_op(digestmap_size(state->commits), ==, 1);
-
- tt_int_op(should_keep_commit(commit, commit->rsa_identity,
- SR_PHASE_REVEAL), ==, 1);
-
- {
-
- sr_commit_t place_holder;
- memcpy(place_holder.hashed_reveal, commit->hashed_reveal,
- sizeof(place_holder.hashed_reveal));
- memset(commit->hashed_reveal, 0, sizeof(commit->hashed_reveal));
- setup_full_capture_of_logs(LOG_WARN);
- tt_int_op(should_keep_commit(commit, commit->rsa_identity,
- SR_PHASE_REVEAL), ==, 0);
- expect_log_msg_containing("doesn't match the commit value.");
- expect_log_msg_containing("has an invalid reveal value.");
- assert_log_predicate(mock_saved_log_n_entries() == 2,
- "expected 2 log entries");
- teardown_capture_of_logs();
- memcpy(commit->hashed_reveal, place_holder.hashed_reveal,
- sizeof(commit->hashed_reveal));
- }
-
- tt_int_op(should_keep_commit(dup_commit, dup_commit->rsa_identity,
- SR_PHASE_REVEAL), ==, 0);
-
- memset(commit->encoded_commit, 0, sizeof(commit->encoded_commit));
- tt_int_op(should_keep_commit(commit, commit->rsa_identity,
- SR_PHASE_REVEAL), ==, 0);
- done:
- teardown_capture_of_logs();
- sr_commit_free(commit);
- sr_commit_free(dup_commit);
- crypto_pk_free(k);
- UNMOCK(trusteddirserver_get_by_v3_auth_digest);
- }
- static void
- test_state_update(void *arg)
- {
- time_t commit_phase_time = 1452076000;
- time_t reveal_phase_time = 1452086800;
- sr_state_t *state;
- (void) arg;
- {
- init_authority_state();
- state = get_sr_state();
- set_sr_phase(SR_PHASE_COMMIT);
-
- state->valid_after = 0;
- state->n_reveal_rounds = 0;
- state->n_commit_rounds = 0;
- state->n_protocol_runs = 0;
- }
-
- MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
-
- sr_state_update(commit_phase_time);
- tt_int_op(state->valid_after, ==, commit_phase_time);
- tt_int_op(state->n_commit_rounds, ==, 1);
- tt_int_op(state->phase, ==, SR_PHASE_COMMIT);
- tt_int_op(digestmap_size(state->commits), ==, 1);
-
- sr_state_update(reveal_phase_time);
- tt_int_op(state->phase, ==, SR_PHASE_REVEAL);
- tt_int_op(state->valid_after, ==, reveal_phase_time);
-
- tt_int_op(digestmap_size(state->commits), ==, 1);
- tt_int_op(state->n_reveal_rounds, ==, 1);
-
- sr_state_update(commit_phase_time);
- tt_int_op(state->valid_after, ==, reveal_phase_time);
-
- state->valid_after = 0;
- sr_state_update(commit_phase_time);
- tt_int_op(state->valid_after, ==, commit_phase_time);
- tt_int_op(state->n_commit_rounds, ==, 1);
- tt_int_op(state->n_reveal_rounds, ==, 0);
- tt_u64_op(state->n_protocol_runs, ==, 1);
- tt_int_op(state->phase, ==, SR_PHASE_COMMIT);
- tt_int_op(digestmap_size(state->commits), ==, 1);
- tt_assert(state->current_srv);
- done:
- sr_state_free();
- UNMOCK(get_my_v3_authority_cert);
- }
- struct testcase_t sr_tests[] = {
- { "get_sr_protocol_phase", test_get_sr_protocol_phase, TT_FORK,
- NULL, NULL },
- { "sr_commit", test_sr_commit, TT_FORK,
- NULL, NULL },
- { "keep_commit", test_keep_commit, TT_FORK,
- NULL, NULL },
- { "encoding", test_encoding, TT_FORK,
- NULL, NULL },
- { "get_next_valid_after_time", test_get_next_valid_after_time, TT_FORK,
- NULL, NULL },
- { "get_state_valid_until_time", test_get_state_valid_until_time, TT_FORK,
- NULL, NULL },
- { "vote", test_vote, TT_FORK,
- NULL, NULL },
- { "state_load_from_disk", test_state_load_from_disk, TT_FORK,
- NULL, NULL },
- { "sr_compute_srv", test_sr_compute_srv, TT_FORK, NULL, NULL },
- { "sr_get_majority_srv_from_votes", test_sr_get_majority_srv_from_votes,
- TT_FORK, NULL, NULL },
- { "utils", test_utils, TT_FORK, NULL, NULL },
- { "state_transition", test_state_transition, TT_FORK, NULL, NULL },
- { "state_update", test_state_update, TT_FORK,
- NULL, NULL },
- END_OF_TESTCASES
- };
|