/** Run unit tests for concatenate-a-smartlist-of-strings functions. */ static void test_container_smartlist_join(void) { smartlist_t *sl = smartlist_new(); smartlist_t *sl2 = smartlist_new(), *sl3 = smartlist_new(), *sl4 = smartlist_new(); char *joined=NULL; /* unique, sorted. */ smartlist_split_string(sl, "Abashments Ambush Anchorman Bacon Banks Borscht " "Bunks Inhumane Insurance Knish Know Manners " "Maraschinos Stamina Sunbonnets Unicorns Wombats", " ", 0, 0); /* non-unique, sorted. */ smartlist_split_string(sl2, "Ambush Anchorman Anchorman Anemias Anemias Bacon " "Crossbowmen Inhumane Insurance Knish Know Manners " "Manners Maraschinos Wombats Wombats Work", " ", 0, 0); SMARTLIST_FOREACH_JOIN(sl, char *, cp1, sl2, char *, cp2, strcmp(cp1,cp2), smartlist_add(sl3, cp2)) { test_streq(cp1, cp2); smartlist_add(sl4, cp1); } SMARTLIST_FOREACH_JOIN_END(cp1, cp2);
static void test_rend_cache_store_v2_desc_as_dir_with_different_content(void *data) { (void)data; rend_cache_store_status_t ret; rend_service_descriptor_t *generated = NULL; smartlist_t *descs = smartlist_new(); time_t t; char *service_id = NULL; rend_encoded_v2_service_descriptor_t *desc_holder_one = NULL; rend_encoded_v2_service_descriptor_t *desc_holder_two = NULL; NS_MOCK(router_get_my_routerinfo); NS_MOCK(hid_serv_responsible_for_desc_id); rend_cache_init(); t = time(NULL); create_descriptor(&generated, &service_id, 3); generated->timestamp = t + RECENT_TIME; rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0, REND_NO_AUTH, NULL, NULL); desc_holder_one = ((rend_encoded_v2_service_descriptor_t *) smartlist_get(descs, 0)); smartlist_set(descs, 0, NULL); SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d, rend_encoded_v2_service_descriptor_free(d)); smartlist_free(descs); descs = smartlist_new(); generated->timestamp = t + RECENT_TIME; generated->protocols = 41; rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0, REND_NO_AUTH, NULL, NULL); desc_holder_two = ((rend_encoded_v2_service_descriptor_t *) smartlist_get(descs, 0)); smartlist_set(descs, 0, NULL); // Test when we have another descriptor stored, with a different descriptor mock_routerinfo = tor_malloc(sizeof(routerinfo_t)); hid_serv_responsible_for_desc_id_response = 1; rend_cache_store_v2_desc_as_dir(desc_holder_one->desc_str); ret = rend_cache_store_v2_desc_as_dir(desc_holder_two->desc_str); tt_int_op(ret, OP_EQ, RCS_OKAY); done: NS_UNMOCK(router_get_my_routerinfo); NS_UNMOCK(hid_serv_responsible_for_desc_id); rend_cache_free_all(); rend_service_descriptor_free(generated); tor_free(service_id); SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d, rend_encoded_v2_service_descriptor_free(d)); smartlist_free(descs); rend_encoded_v2_service_descriptor_free(desc_holder_one); rend_encoded_v2_service_descriptor_free(desc_holder_two); }
static void setup_mock_consensus(void) { current_md_consensus = current_ns_consensus = tor_malloc_zero(sizeof(networkstatus_t)); current_md_consensus->net_params = smartlist_new(); current_md_consensus->routerstatus_list = smartlist_new(); }
/** Return a new empty routerset. */ routerset_t * routerset_new(void) { routerset_t *result = tor_malloc_zero(sizeof(routerset_t)); result->list = smartlist_new(); result->names = strmap_new(); result->digests = digestmap_new(); result->policies = smartlist_new(); result->country_names = smartlist_new(); return result; }
static rend_service_t * helper_create_rend_service(const char *path) { rend_service_t *s = tor_malloc_zero(sizeof(rend_service_t)); s->ports = smartlist_new(); s->intro_nodes = smartlist_new(); s->expiring_nodes = smartlist_new(); if (path) { s->directory = tor_strdup(path); } return s; }
/** Initialize networkstatus vote object attributes. */ void dir_common_setup_vote(networkstatus_t **vote, time_t now) { *vote = tor_malloc_zero(sizeof(networkstatus_t)); (*vote)->type = NS_TYPE_VOTE; (*vote)->published = now; (*vote)->supported_methods = smartlist_new(); (*vote)->known_flags = smartlist_new(); (*vote)->net_params = smartlist_new(); (*vote)->routerstatus_list = smartlist_new(); (*vote)->voters = smartlist_new(); }
/** Run unit tests for smartlist set manipulation functions. */ static void test_container_smartlist_overlap(void *arg) { smartlist_t *sl = smartlist_new(); smartlist_t *ints = smartlist_new(); smartlist_t *odds = smartlist_new(); smartlist_t *evens = smartlist_new(); smartlist_t *primes = smartlist_new(); int i; (void)arg; for (i=1; i < 10; i += 2) smartlist_add(odds, (void*)(uintptr_t)i); for (i=0; i < 10; i += 2) smartlist_add(evens, (void*)(uintptr_t)i); /* add_all */ smartlist_add_all(ints, odds); smartlist_add_all(ints, evens); tt_int_op(smartlist_len(ints),OP_EQ, 10); smartlist_add(primes, (void*)2); smartlist_add(primes, (void*)3); smartlist_add(primes, (void*)5); smartlist_add(primes, (void*)7); /* overlap */ tt_assert(smartlist_overlap(ints, odds)); tt_assert(smartlist_overlap(odds, primes)); tt_assert(smartlist_overlap(evens, primes)); tt_assert(!smartlist_overlap(odds, evens)); /* intersect */ smartlist_add_all(sl, odds); smartlist_intersect(sl, primes); tt_int_op(smartlist_len(sl),OP_EQ, 3); tt_assert(smartlist_contains(sl, (void*)3)); tt_assert(smartlist_contains(sl, (void*)5)); tt_assert(smartlist_contains(sl, (void*)7)); /* subtract */ smartlist_add_all(sl, primes); smartlist_subtract(sl, odds); tt_int_op(smartlist_len(sl),OP_EQ, 1); tt_assert(smartlist_contains(sl, (void*)2)); done: smartlist_free(odds); smartlist_free(evens); smartlist_free(ints); smartlist_free(primes); smartlist_free(sl); }
static void test_container_smartlist_strings_eq(void *arg) { (void)arg; smartlist_t *sl1 = smartlist_new(); smartlist_t *sl2 = smartlist_new(); #define EQ_SHOULD_SAY(s1,s2,val) \ do { \ SMARTLIST_FOREACH(sl1, char *, cp, tor_free(cp)); \ SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp)); \ smartlist_clear(sl1); \ smartlist_clear(sl2); \ smartlist_split_string(sl1, (s1), ":", 0, 0); \ smartlist_split_string(sl2, (s2), ":", 0, 0); \ tt_int_op((val), OP_EQ, smartlist_strings_eq(sl1, sl2)); \ } while (0) /* Both NULL, so equal */ tt_int_op(1, ==, smartlist_strings_eq(NULL, NULL)); /* One NULL, not equal. */ tt_int_op(0, ==, smartlist_strings_eq(NULL, sl1)); tt_int_op(0, ==, smartlist_strings_eq(sl1, NULL)); /* Both empty, both equal. */ EQ_SHOULD_SAY("", "", 1); /* One empty, not equal */ EQ_SHOULD_SAY("", "ab", 0); EQ_SHOULD_SAY("", "xy:z", 0); EQ_SHOULD_SAY("abc", "", 0); EQ_SHOULD_SAY("abc:cd", "", 0); /* Different lengths, not equal. */ EQ_SHOULD_SAY("hello:world", "hello", 0); EQ_SHOULD_SAY("hello", "hello:friends", 0); /* Same lengths, not equal */ EQ_SHOULD_SAY("Hello:world", "goodbye:world", 0); EQ_SHOULD_SAY("Hello:world", "Hello:stars", 0); /* Actually equal */ EQ_SHOULD_SAY("ABC", "ABC", 1); EQ_SHOULD_SAY(" ab : cd : e", " ab : cd : e", 1); done: SMARTLIST_FOREACH(sl1, char *, cp, tor_free(cp)); SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp)); smartlist_free(sl1); smartlist_free(sl2); }
static smartlist_t * cert_dl_status_sks_for_auth_id_mock(const char *digest) { smartlist_t *list = NULL; char sk[DIGEST_LEN]; char digest_str[HEX_DIGEST_LEN+1]; char *tmp; int len; tt_assert(digest != NULL); base16_encode(digest_str, HEX_DIGEST_LEN + 1, digest, DIGEST_LEN); digest_str[HEX_DIGEST_LEN] = '\0'; /* * Build a list of two hard-coded digests, depending on what we * were just passed. */ if (strcmp(digest_str, auth_id_digest_1_str) == 0) { list = smartlist_new(); len = base16_decode(sk, DIGEST_LEN, auth_1_sk_1_str, strlen(auth_1_sk_1_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, sk, DIGEST_LEN); smartlist_add(list, tmp); len = base16_decode(sk, DIGEST_LEN, auth_1_sk_2_str, strlen(auth_1_sk_2_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, sk, DIGEST_LEN); smartlist_add(list, tmp); } else if (strcmp(digest_str, auth_id_digest_2_str) == 0) { list = smartlist_new(); len = base16_decode(sk, DIGEST_LEN, auth_2_sk_1_str, strlen(auth_2_sk_1_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, sk, DIGEST_LEN); smartlist_add(list, tmp); len = base16_decode(sk, DIGEST_LEN, auth_2_sk_2_str, strlen(auth_2_sk_2_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, sk, DIGEST_LEN); smartlist_add(list, tmp); } done: return list; }
/* Using a list of <b>votes</b>, return the SRV object from them that has * been voted by the majority of dirauths. If <b>current</b> is set, we look * for the current SRV value else the previous one. The returned pointer is * an object located inside a vote. NULL is returned if no appropriate value * could be found. */ STATIC sr_srv_t * get_majority_srv_from_votes(const smartlist_t *votes, int current) { int count = 0; sr_srv_t *most_frequent_srv = NULL; sr_srv_t *the_srv = NULL; smartlist_t *srv_list; tor_assert(votes); srv_list = smartlist_new(); /* Walk over votes and register any SRVs found. */ SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) { sr_srv_t *srv_tmp = NULL; if (!v->sr_info.participate) { /* Ignore vote that do not participate. */ continue; } /* Do we want previous or current SRV? */ srv_tmp = current ? v->sr_info.current_srv : v->sr_info.previous_srv; if (!srv_tmp) { continue; } smartlist_add(srv_list, srv_tmp); } SMARTLIST_FOREACH_END(v);
/* Given the previous SRV and the current SRV, return a heap allocated * string with their data that could be put in a vote or a consensus. Caller * must free the returned string. Return NULL if no SRVs were provided. */ static char * get_ns_str_from_sr_values(const sr_srv_t *prev_srv, const sr_srv_t *cur_srv) { smartlist_t *chunks = NULL; char *srv_str; if (!prev_srv && !cur_srv) { return NULL; } chunks = smartlist_new(); if (prev_srv) { char *srv_line = srv_to_ns_string(prev_srv, previous_srv_str); smartlist_add(chunks, srv_line); } if (cur_srv) { char *srv_line = srv_to_ns_string(cur_srv, current_srv_str); smartlist_add(chunks, srv_line); } /* Join the line(s) here in one string to return. */ srv_str = smartlist_join_strings(chunks, "", 0, NULL); SMARTLIST_FOREACH(chunks, char *, s, tor_free(s)); smartlist_free(chunks); return srv_str; }
static smartlist_t * pick_oos_victims_mock(int n) { smartlist_t *l = NULL; int i; ++pick_oos_mock_calls; tt_int_op(n, OP_GT, 0); if (!pick_oos_mock_fail) { /* * connection_check_oos() just passes the list onto * kill_conn_list_for_oos(); we don't need to simulate * its content for this mock, just its existence, but * we do need to check the parameter. */ l = smartlist_new(); for (i = 0; i < n; ++i) smartlist_add(l, NULL); } else { l = NULL; } pick_oos_mock_last_n = n; done: return l; }
/** Helper: Make a new routerinfo containing the right information for a * given vote_routerstatus_t. */ routerinfo_t * dir_common_generate_ri_from_rs(const vote_routerstatus_t *vrs) { routerinfo_t *r; const routerstatus_t *rs = &vrs->status; static time_t published = 0; r = tor_malloc_zero(sizeof(routerinfo_t)); r->cert_expiration_time = TIME_MAX; memcpy(r->cache_info.identity_digest, rs->identity_digest, DIGEST_LEN); memcpy(r->cache_info.signed_descriptor_digest, rs->descriptor_digest, DIGEST_LEN); r->cache_info.do_not_cache = 1; r->cache_info.routerlist_index = -1; r->cache_info.signed_descriptor_body = tor_strdup("123456789012345678901234567890123"); r->cache_info.signed_descriptor_len = strlen(r->cache_info.signed_descriptor_body); r->exit_policy = smartlist_new(); r->cache_info.published_on = ++published + time(NULL); if (rs->has_bandwidth) { /* * Multiply by 1000 because the routerinfo_t and the routerstatus_t * seem to use different units (*sigh*) and because we seem stuck on * icky and perverse decimal kilobytes (*double sigh*) - see * router_get_advertised_bandwidth_capped() of routerlist.c and * routerstatus_format_entry() of dirserv.c. */ r->bandwidthrate = rs->bandwidth_kb * 1000; r->bandwidthcapacity = rs->bandwidth_kb * 1000; } return r; }
/** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure * that policies_summarize() produces the string <b>expected_summary</b> from * it. */ static void test_policy_summary_helper(const char *policy_str, const char *expected_summary) { config_line_t line; smartlist_t *policy = smartlist_new(); char *summary = NULL; char *summary_after = NULL; int r; short_policy_t *short_policy = NULL; line.key = (char*)"foo"; line.value = (char *)policy_str; line.next = NULL; r = policies_parse_exit_policy(&line, &policy, 1, 0, 0, 1); test_eq(r, 0); summary = policy_summarize(policy, AF_INET); test_assert(summary != NULL); test_streq(summary, expected_summary); short_policy = parse_short_policy(summary); tt_assert(short_policy); summary_after = write_short_policy(short_policy); test_streq(summary, summary_after); done: tor_free(summary_after); tor_free(summary); if (policy) addr_policy_list_free(policy); short_policy_free(short_policy); }
void tor_capture_bugs_(int n) { tor_end_capture_bugs_(); bug_messages = smartlist_new(); n_bugs_to_capture = n; }
static smartlist_t * cert_dl_status_auth_ids_mock(void) { char digest[DIGEST_LEN], *tmp; int len; smartlist_t *list = NULL; /* Just pretend we have only the two hard-coded digests listed above */ list = smartlist_new(); len = base16_decode(digest, DIGEST_LEN, auth_id_digest_1_str, strlen(auth_id_digest_1_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, digest, DIGEST_LEN); smartlist_add(list, tmp); len = base16_decode(digest, DIGEST_LEN, auth_id_digest_2_str, strlen(auth_id_digest_2_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, digest, DIGEST_LEN); smartlist_add(list, tmp); done: return list; }
/** Decode the hashed, base64'd passwords stored in <b>passwords</b>. * Return a smartlist of acceptable passwords (unterminated strings of * length S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN) on success, or NULL on * failure. */ smartlist_t * decode_hashed_passwords(config_line_t *passwords) { char decoded[64]; config_line_t *cl; smartlist_t *sl = smartlist_new(); tor_assert(passwords); for (cl = passwords; cl; cl = cl->next) { const char *hashed = cl->value; if (!strcmpstart(hashed, "16:")) { if (base16_decode(decoded, sizeof(decoded), hashed+3, strlen(hashed+3)) != S2K_RFC2440_SPECIFIER_LEN + DIGEST_LEN || strlen(hashed+3) != (S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN)*2) { goto err; } } else { if (base64_decode(decoded, sizeof(decoded), hashed, strlen(hashed)) != S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN) { goto err; } } smartlist_add(sl, tor_memdup(decoded, S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN)); } return sl; err: SMARTLIST_FOREACH(sl, char*, cp, tor_free(cp)); smartlist_free(sl); return NULL; }
static void test_routerlist_launch_descriptor_downloads(void *arg) { smartlist_t *downloadable = smartlist_new(); time_t now = time(NULL); char *cp; (void)arg; for (int i = 0; i < 100; i++) { cp = tor_malloc(DIGEST256_LEN); tt_assert(cp); crypto_rand(cp, DIGEST256_LEN); smartlist_add(downloadable, cp); } MOCK(initiate_descriptor_downloads, mock_initiate_descriptor_downloads); launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC, downloadable, NULL, now); tt_int_op(3, ==, count); UNMOCK(initiate_descriptor_downloads); done: SMARTLIST_FOREACH(downloadable, char *, cp1, tor_free(cp1)); smartlist_free(downloadable); }
memarea_t * memarea_new(void) { memarea_t *ma = tor_malloc_zero(sizeof(memarea_t)); ma->pieces = smartlist_new(); return ma; }
/** Return the config line for transport <b>transport</b> in the current state. * Return NULL if there is no config line for <b>transport</b>. */ static config_line_t * get_transport_in_state_by_name(const char *transport) { or_state_t *or_state = get_or_state(); config_line_t *line; config_line_t *ret = NULL; smartlist_t *items = NULL; for (line = or_state->TransportProxies ; line ; line = line->next) { tor_assert(!strcmp(line->key, "TransportProxy")); items = smartlist_new(); smartlist_split_string(items, line->value, NULL, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1); if (smartlist_len(items) != 2) /* broken state */ goto done; if (!strcmp(smartlist_get(items, 0), transport)) { ret = line; goto done; } SMARTLIST_FOREACH(items, char*, s, tor_free(s)); smartlist_free(items); items = NULL; } done: if (items) { SMARTLIST_FOREACH(items, char*, s, tor_free(s)); smartlist_free(items); } return ret; }
static void * test_conn_get_rend_setup(const struct testcase_t *tc) { dir_connection_t *conn = DOWNCAST(dir_connection_t, test_conn_get_basic_setup(tc)); tt_assert(conn); assert_connection_ok(&conn->base_, time(NULL)); rend_cache_init(); /* TODO: use directory_initiate_command_rend() to do this - maybe? */ conn->rend_data = tor_malloc_zero(sizeof(rend_data_t)); tor_assert(strlen(TEST_CONN_REND_ADDR) == REND_SERVICE_ID_LEN_BASE32); memcpy(conn->rend_data->onion_address, TEST_CONN_REND_ADDR, REND_SERVICE_ID_LEN_BASE32+1); conn->rend_data->hsdirs_fp = smartlist_new(); conn->base_.purpose = TEST_CONN_REND_PURPOSE; assert_connection_ok(&conn->base_, time(NULL)); return conn; /* On failure */ done: test_conn_get_rend_teardown(tc, conn); /* Returning NULL causes the unit test to fail */ return NULL; }
static smartlist_t * descbr_get_digests_mock(void) { char digest[DIGEST_LEN], *tmp; int len; smartlist_t *list = NULL; if (!disable_descbr) { /* Just pretend we have only the two hard-coded digests listed above */ list = smartlist_new(); len = base16_decode(digest, DIGEST_LEN, descbr_digest_1_str, strlen(descbr_digest_1_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, digest, DIGEST_LEN); smartlist_add(list, tmp); len = base16_decode(digest, DIGEST_LEN, descbr_digest_2_str, strlen(descbr_digest_2_str)); tt_int_op(len, OP_EQ, DIGEST_LEN); tmp = tor_malloc(DIGEST_LEN); memcpy(tmp, digest, DIGEST_LEN); smartlist_add(list, tmp); } done: return list; }
static void NS(test_main)(void *arg) { /* Choose origin_circuit_t wlog. */ origin_circuit_t *mock_circuit1, *mock_circuit2; int expected_circuits = 2, actual_circuits; (void)arg; mock_circuit1 = tor_malloc_zero(sizeof(origin_circuit_t)); mock_circuit2 = tor_malloc_zero(sizeof(origin_circuit_t)); mock_global_circuitlist = smartlist_new(); smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit1)); smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit2)); NS_MOCK(circuit_get_global_list); actual_circuits = count_circuits(); tt_assert(expected_circuits == actual_circuits); done: tor_free(mock_circuit1); tor_free(mock_circuit2); smartlist_free(mock_global_circuitlist); mock_global_circuitlist = NULL; NS_UNMOCK(circuit_get_global_list); }
/* Using the given descriptor intro point ip, the node of the * rendezvous point rp_node and the service's subcredential, populate the * already allocated intro1_data object with the needed key material and link * specifiers. * * If rp_node has an invalid primary address, intro1_data->link_specifiers * will be an empty list. Otherwise, this function can't fail. The ip * MUST be a valid object containing the needed keys and authentication * method. */ static void setup_introduce1_data(const hs_desc_intro_point_t *ip, const node_t *rp_node, const uint8_t *subcredential, hs_cell_introduce1_data_t *intro1_data) { smartlist_t *rp_lspecs; tor_assert(ip); tor_assert(rp_node); tor_assert(subcredential); tor_assert(intro1_data); /* Build the link specifiers from the extend information of the rendezvous * circuit that we've picked previously. */ rp_lspecs = smartlist_new(); get_lspecs_from_node(rp_node, rp_lspecs); /* Populate the introduce1 data object. */ memset(intro1_data, 0, sizeof(hs_cell_introduce1_data_t)); if (ip->legacy.key != NULL) { intro1_data->is_legacy = 1; intro1_data->legacy_key = ip->legacy.key; } intro1_data->auth_pk = &ip->auth_key_cert->signed_key; intro1_data->enc_pk = &ip->enc_key; intro1_data->subcredential = subcredential; intro1_data->onion_pk = node_get_curve25519_onion_key(rp_node); intro1_data->link_specifiers = rp_lspecs; }
static void test_routerlist_initiate_descriptor_downloads(void *arg) { const char *prose = "unhurried and wise, we perceive."; smartlist_t *digests = smartlist_new(); (void)arg; for (int i = 0; i < 20; i++) { smartlist_add(digests, (char*)prose); } MOCK(directory_get_from_dirserver, mock_get_from_dirserver); initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC, digests, 3, 7, 0); UNMOCK(directory_get_from_dirserver); tt_str_op(output, OP_EQ, "d/" "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-" "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-" "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-" "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4" ".z"); done: smartlist_free(digests); }
/** Write the <b>datalen</b> bytes from <b>data</b> to the file named * <b>fname</b> in the tagged-data format. This format contains a * 32-byte header, followed by the data itself. The header is the * NUL-padded string "== <b>typestring</b>: <b>tag</b> ==". The length * of <b>typestring</b> and <b>tag</b> must therefore be no more than * 24. **/ int crypto_write_tagged_contents_to_file(const char *fname, const char *typestring, const char *tag, const uint8_t *data, size_t datalen) { char header[32]; smartlist_t *chunks = smartlist_new(); sized_chunk_t ch0, ch1; int r = -1; memset(header, 0, sizeof(header)); if (tor_snprintf(header, sizeof(header), "== %s: %s ==", typestring, tag) < 0) goto end; ch0.bytes = header; ch0.len = 32; ch1.bytes = (const char*) data; ch1.len = datalen; smartlist_add(chunks, &ch0); smartlist_add(chunks, &ch1); r = write_chunks_to_file(fname, chunks, 1, 0); end: smartlist_free(chunks); return r; }
/** Run unit tests for smartlist-of-digests functions. */ static void test_container_smartlist_digests(void) { smartlist_t *sl = smartlist_new(); /* contains_digest */ smartlist_add(sl, tor_memdup("AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN)); smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN)); smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN)); test_eq(0, smartlist_contains_digest(NULL, "AAAAAAAAAAAAAAAAAAAA")); test_assert(smartlist_contains_digest(sl, "AAAAAAAAAAAAAAAAAAAA")); test_assert(smartlist_contains_digest(sl, "\00090AAB2AAAAaasdAAAAA")); test_eq(0, smartlist_contains_digest(sl, "\00090AAB2AAABaasdAAAAA")); /* sort digests */ smartlist_sort_digests(sl); test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN); test_memeq(smartlist_get(sl, 1), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN); test_memeq(smartlist_get(sl, 2), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN); test_eq(3, smartlist_len(sl)); /* uniq_digests */ smartlist_uniq_digests(sl); test_eq(2, smartlist_len(sl)); test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN); test_memeq(smartlist_get(sl, 1), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN); done: SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp)); smartlist_free(sl); }
/** Replace all "private" entries in *<b>policy</b> with their expanded * equivalents. */ void policy_expand_private(smartlist_t **policy) { uint16_t port_min, port_max; int i; smartlist_t *tmp; if (!*policy) /*XXXX disallow NULL policies? */ return; tmp = smartlist_new(); SMARTLIST_FOREACH(*policy, addr_policy_t *, p, { if (! p->is_private) { smartlist_add(tmp, p); continue; } for (i = 0; private_nets[i]; ++i) { addr_policy_t newpolicy; memcpy(&newpolicy, p, sizeof(addr_policy_t)); newpolicy.is_private = 0; newpolicy.is_canonical = 0; if (tor_addr_parse_mask_ports(private_nets[i], &newpolicy.addr, &newpolicy.maskbits, &port_min, &port_max)<0) { tor_assert(0); } smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy)); } addr_policy_free(p); });
/** Add an entry to the GeoIP table, parsing it from <b>line</b>. The * format is as for geoip_load_file(). */ /*private*/ int geoip_parse_entry(const char *line) { unsigned int low, high; char b[3]; if (!geoip_countries) init_geoip_countries(); if (!geoip_entries) geoip_entries = smartlist_new(); while (TOR_ISSPACE(*line)) ++line; if (*line == '#') return 0; if (tor_sscanf(line,"%u,%u,%2s", &low, &high, b) == 3) { geoip_add_entry(low, high, b); return 0; } else if (tor_sscanf(line,"\"%u\",\"%u\",\"%2s\",", &low, &high, b) == 3) { geoip_add_entry(low, high, b); return 0; } else { log_warn(LD_GENERAL, "Unable to parse line from GEOIP file: %s", escaped(line)); return -1; } }
/* Note the cell digest in the circuit sendme last digests FIFO if applicable. * It is safe to pass a circuit that isn't meant to track those digests. */ void sendme_record_cell_digest(circuit_t *circ) { const uint8_t *digest; tor_assert(circ); /* We only keep the cell digest if we are the Exit on that circuit and if * this cell is the last one before the client should send a SENDME. */ if (CIRCUIT_IS_ORIGIN(circ)) { return; } /* Is this the last cell before a SENDME? The idea is that if the * package_window reaches a multiple of the increment, after this cell, we * should expect a SENDME. */ if (!sendme_circuit_cell_is_next(circ->package_window)) { return; } /* Add the digest to the last seen list in the circuit. */ digest = relay_crypto_get_sendme_digest(&TO_OR_CIRCUIT(circ)->crypto); if (circ->sendme_last_digests == NULL) { circ->sendme_last_digests = smartlist_new(); } smartlist_add(circ->sendme_last_digests, tor_memdup(digest, DIGEST_LEN)); }