static void test_md_corrupt_desc(void *arg) { char *cp = NULL; smartlist_t *sl = NULL; (void) arg; sl = microdescs_add_to_cache(get_microdesc_cache(), "@last-listed 2015-06-22 10:00:00\n" "onion-k\n", NULL, SAVED_IN_JOURNAL, 0, time(NULL), NULL); tt_int_op(smartlist_len(sl), OP_EQ, 0); smartlist_free(sl); sl = microdescs_add_to_cache(get_microdesc_cache(), "@last-listed 2015-06-22 10:00:00\n" "wiggly\n", NULL, SAVED_IN_JOURNAL, 0, time(NULL), NULL); tt_int_op(smartlist_len(sl), OP_EQ, 0); smartlist_free(sl); tor_asprintf(&cp, "%s\n%s", test_md1, "@foobar\nonion-wobble\n"); sl = microdescs_add_to_cache(get_microdesc_cache(), cp, cp+strlen(cp), SAVED_IN_JOURNAL, 0, time(NULL), NULL); tt_int_op(smartlist_len(sl), OP_EQ, 0); done: tor_free(cp); smartlist_free(sl); }
static void test_address_get_if_addrs_list_internal(void *arg) { smartlist_t *results = NULL; (void)arg; results = get_interface_address_list(LOG_ERR, 1); tt_ptr_op(results, OP_NE, NULL); /* When the network is down, a system might not have any non-local * non-multicast addresseses, not even internal ones. * Unit tests shouldn't fail because of this. */ tt_int_op(smartlist_len(results),OP_GE,0); tt_assert(!smartlist_contains_localhost_tor_addr(results)); tt_assert(!smartlist_contains_multicast_tor_addr(results)); /* The list may or may not contain internal addresses */ tt_assert(!smartlist_contains_null_tor_addr(results)); /* if there are any addresses, they must be IPv4 */ if (smartlist_len(results) > 0) { tt_assert(smartlist_contains_ipv4_tor_addr(results)); } tt_assert(!smartlist_contains_ipv6_tor_addr(results)); done: interface_address_list_free(results); return; }
static void test_address_get_if_addrs_win32(void *arg) { smartlist_t *results = NULL; (void)arg; results = get_interface_addresses_win32(LOG_ERR, AF_UNSPEC); tt_int_op(smartlist_len(results),OP_GE,1); tt_assert(smartlist_contains_localhost_tor_addr(results)); tt_assert(!smartlist_contains_null_tor_addr(results)); /* If there are addresses, they must be IPv4 or IPv6 */ if (smartlist_len(results) > 0) { tt_assert(smartlist_contains_ipv4_tor_addr(results) || smartlist_contains_ipv6_tor_addr(results)); } done: SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t)); tor_free(results); return; }
static void test_address_get_if_addrs_list_no_internal(void *arg) { smartlist_t *results = NULL; (void)arg; results = get_interface_address_list(LOG_ERR, 0); tt_ptr_op(results, OP_NE, NULL); /* Work even on systems with only internal IPv4 addresses */ tt_int_op(smartlist_len(results),OP_GE,0); tt_assert(!smartlist_contains_localhost_tor_addr(results)); tt_assert(!smartlist_contains_multicast_tor_addr(results)); tt_assert(!smartlist_contains_internal_tor_addr(results)); tt_assert(!smartlist_contains_null_tor_addr(results)); /* if there are any addresses, they must be IPv4 */ if (smartlist_len(results) > 0) { tt_assert(smartlist_contains_ipv4_tor_addr(results)); } tt_assert(!smartlist_contains_ipv6_tor_addr(results)); done: interface_address_list_free(results); return; }
/** Parse a file containing router descriptors and load them to our routerlist. This function is used to setup an artificial network so that we can conduct entry guard tests. */ static void setup_fake_routerlist(void) { int retval; routerlist_t *our_routerlist = NULL; smartlist_t *our_nodelist = NULL; /* Read the file that contains our test descriptors. */ /* We need to mock this function otherwise the descriptors will not accepted as they are too old. */ MOCK(router_descriptor_is_older_than, router_descriptor_is_older_than_replacement); /* Load all the test descriptors to the routerlist. */ retval = router_load_routers_from_string(TEST_DESCRIPTORS, NULL, SAVED_IN_JOURNAL, NULL, 0, NULL); tt_int_op(retval, OP_EQ, NUMBER_OF_DESCRIPTORS); /* Sanity checking of routerlist and nodelist. */ our_routerlist = router_get_routerlist(); tt_int_op(smartlist_len(our_routerlist->routers), OP_EQ, NUMBER_OF_DESCRIPTORS); routerlist_assert_ok(our_routerlist); our_nodelist = nodelist_get_list(); tt_int_op(smartlist_len(our_nodelist), OP_EQ, NUMBER_OF_DESCRIPTORS); /* Mark all routers as non-guards but up and running! */ SMARTLIST_FOREACH_BEGIN(our_nodelist, node_t *, node) { node->is_running = 1; node->is_valid = 1; node->is_possible_guard = 0; } SMARTLIST_FOREACH_END(node);
static void test_storagedir_empty(void *arg) { char *dirname = tor_strdup(get_fname_rnd("store_dir")); storage_dir_t *d = NULL; (void)arg; tt_int_op(FN_NOENT, OP_EQ, file_status(dirname)); d = storage_dir_new(dirname, 10); tt_assert(d); tt_int_op(FN_DIR, OP_EQ, file_status(dirname)); tt_int_op(0, OP_EQ, smartlist_len(storage_dir_list(d))); tt_u64_op(0, OP_EQ, storage_dir_get_usage(d)); storage_dir_free(d); d = storage_dir_new(dirname, 10); tt_assert(d); tt_int_op(FN_DIR, OP_EQ, file_status(dirname)); tt_int_op(0, OP_EQ, smartlist_len(storage_dir_list(d))); tt_u64_op(0, OP_EQ, storage_dir_get_usage(d)); done: storage_dir_free(d); tor_free(dirname); }
static void test_address_get_if_addrs_no_internal_fail(void *arg) { smartlist_t *results1 = NULL, *results2 = NULL; (void)arg; MOCK(get_interface_addresses_raw, mock_get_interface_addresses_raw_fail); MOCK(get_interface_address6_via_udp_socket_hack, mock_get_interface_address6_via_udp_socket_hack_fail); results1 = get_interface_address6_list(LOG_ERR, AF_INET6, 0); tt_ptr_op(results1, OP_NE, NULL); tt_int_op(smartlist_len(results1),OP_EQ,0); results2 = get_interface_address_list(LOG_ERR, 0); tt_ptr_op(results2, OP_NE, NULL); tt_int_op(smartlist_len(results2),OP_EQ,0); done: UNMOCK(get_interface_addresses_raw); UNMOCK(get_interface_address6_via_udp_socket_hack); interface_address6_list_free(results1); interface_address6_list_free(results2); return; }
/** 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); }
/** Assuming the members of <b>sl</b> are in order, return the index of the * member that matches <b>key</b>. If no member matches, return the index of * the first member greater than <b>key</b>, or smartlist_len(sl) if no member * is greater than <b>key</b>. Set <b>found_out</b> to true on a match, to * false otherwise. Ordering and matching are defined by a <b>compare</b> * function that returns 0 on a match; less than 0 if key is less than member, * and greater than 0 if key is greater then member. */ int smartlist_bsearch_idx(const smartlist_t *sl, const void *key, int (*compare)(const void *key, const void **member), int *found_out) { int hi = smartlist_len(sl) - 1, lo = 0, cmp, mid; while (lo <= hi) { mid = (lo + hi) / 2; cmp = compare(key, (const void**) &(sl->list[mid])); if (cmp>0) { /* key > sl[mid] */ lo = mid+1; } else if (cmp<0) { /* key < sl[mid] */ hi = mid-1; } else { /* key == sl[mid] */ *found_out = 1; return mid; } } /* lo > hi. */ { tor_assert(lo >= 0); if (lo < smartlist_len(sl)) { cmp = compare(key, (const void**) &(sl->list[lo])); tor_assert(cmp < 0); } else if (smartlist_len(sl)) { cmp = compare(key, (const void**) &(sl->list[smartlist_len(sl)-1])); tor_assert(cmp > 0); } } *found_out = 0; return lo; }
static void NS(test_main)(void *arg) { routerset_t *set = routerset_new(); smartlist_t *list = smartlist_new(); const char *nickname = "foo"; routerinfo_t ri; node_t mock_node; (void)arg; strmap_set_lc(set->names, nickname, (void *)1); ri.nickname = (char *)nickname; mock_node.rs = NULL; mock_node.ri = &ri; smartlist_add(list, (void *)&mock_node); tt_int_op(smartlist_len(list), OP_NE, 0); routerset_subtract_nodes(list, set); tt_int_op(smartlist_len(list), OP_EQ, 0); done: routerset_free(set); smartlist_free(list); }
static void test_halfstream_wrap(void *arg) { origin_circuit_t *circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_C_GENERAL, 0); edge_connection_t *edgeconn; entry_connection_t *entryconn; circ->cpath->state = CPATH_STATE_AWAITING_KEYS; circ->cpath->deliver_window = CIRCWINDOW_START; entryconn = fake_entry_conn(circ, 23); edgeconn = ENTRY_TO_EDGE_CONN(entryconn); (void)arg; /* Suppress the WARN message we generate in this test */ setup_full_capture_of_logs(LOG_WARN); MOCK(connection_mark_for_close_internal_, mock_mark_for_close); /* Verify that get_unique_stream_id_by_circ() can wrap uint16_t */ circ->next_stream_id = 65530; halfstream_insert(circ, edgeconn, NULL, 7, 0); tt_int_op(circ->next_stream_id, OP_EQ, 2); tt_int_op(smartlist_len(circ->half_streams), OP_EQ, 7); /* Insert full-1 */ halfstream_insert(circ, edgeconn, NULL, 65534-smartlist_len(circ->half_streams), 0); tt_int_op(smartlist_len(circ->half_streams), OP_EQ, 65534); /* Verify that we can get_unique_stream_id_by_circ() successfully */ edgeconn->stream_id = get_unique_stream_id_by_circ(circ); tt_int_op(edgeconn->stream_id, OP_NE, 0); /* 0 is failure */ /* Insert an opened stream on the circ with that id */ ENTRY_TO_CONN(entryconn)->marked_for_close = 0; ENTRY_TO_CONN(entryconn)->outbuf_flushlen = 0; edgeconn->base_.state = AP_CONN_STATE_CONNECT_WAIT; circ->p_streams = edgeconn; /* Verify that get_unique_stream_id_by_circ() fails */ tt_int_op(get_unique_stream_id_by_circ(circ), OP_EQ, 0); /* 0 is failure */ /* eof the one opened stream. Verify it is now in half-closed */ tt_int_op(smartlist_len(circ->half_streams), OP_EQ, 65534); connection_edge_reached_eof(edgeconn); tt_int_op(smartlist_len(circ->half_streams), OP_EQ, 65535); /* Verify get_unique_stream_id_by_circ() fails due to full half-closed */ circ->p_streams = NULL; tt_int_op(get_unique_stream_id_by_circ(circ), OP_EQ, 0); /* 0 is failure */ done: circuit_free_(TO_CIRCUIT(circ)); connection_free_minimal(ENTRY_TO_CONN(entryconn)); UNMOCK(connection_mark_for_close_internal_); }
static void test_storagedir_deletion(void *arg) { (void)arg; char *dirname = tor_strdup(get_fname_rnd("store_dir")); storage_dir_t *d = NULL; char *fn1 = NULL, *fn2 = NULL; char *bytes = NULL; int r; const char str1[] = "There are nine and sixty ways to disguise communiques"; const char str2[] = "And rather more than one of them is right"; // Make sure the directory is there. */ d = storage_dir_new(dirname, 10); storage_dir_free(d); d = NULL; tor_asprintf(&fn1, "%s/1007", dirname); r = write_str_to_file(fn1, str1, 0); tt_int_op(r, OP_EQ, 0); tor_asprintf(&fn2, "%s/1003.tmp", dirname); r = write_str_to_file(fn2, str2, 0); tt_int_op(r, OP_EQ, 0); // The tempfile should be deleted the next time we list the directory. d = storage_dir_new(dirname, 10); tt_int_op(1, OP_EQ, smartlist_len(storage_dir_list(d))); tt_u64_op(strlen(str1), OP_EQ, storage_dir_get_usage(d)); tt_int_op(FN_FILE, OP_EQ, file_status(fn1)); tt_int_op(FN_NOENT, OP_EQ, file_status(fn2)); bytes = (char*) storage_dir_read(d, "1007", 1, NULL); tt_str_op(bytes, OP_EQ, str1); // Should have no effect; file already gone. storage_dir_remove_file(d, "1003.tmp"); tt_int_op(1, OP_EQ, smartlist_len(storage_dir_list(d))); tt_u64_op(strlen(str1), OP_EQ, storage_dir_get_usage(d)); // Actually remove a file. storage_dir_remove_file(d, "1007"); tt_int_op(FN_NOENT, OP_EQ, file_status(fn1)); tt_int_op(0, OP_EQ, smartlist_len(storage_dir_list(d))); tt_u64_op(0, OP_EQ, storage_dir_get_usage(d)); done: tor_free(dirname); tor_free(fn1); tor_free(fn2); storage_dir_free(d); tor_free(bytes); }
static void test_cntev_append_cell_stats(void *arg) { smartlist_t *event_parts; char *cp = NULL; const char *key = "Z"; uint64_t include_if_non_zero[CELL_COMMAND_MAX_ + 1], number_to_include[CELL_COMMAND_MAX_ + 1]; (void)arg; event_parts = smartlist_new(); memset(include_if_non_zero, 0, (CELL_COMMAND_MAX_ + 1) * sizeof(uint64_t)); memset(number_to_include, 0, (CELL_COMMAND_MAX_ + 1) * sizeof(uint64_t)); /* All array entries empty. */ append_cell_stats_by_command(event_parts, key, include_if_non_zero, number_to_include); tt_int_op(0, OP_EQ, smartlist_len(event_parts)); /* There's a RELAY cell to include, but the corresponding field in * include_if_non_zero is still zero. */ number_to_include[CELL_RELAY] = 1; append_cell_stats_by_command(event_parts, key, include_if_non_zero, number_to_include); tt_int_op(0, OP_EQ, smartlist_len(event_parts)); /* Now include single RELAY cell. */ include_if_non_zero[CELL_RELAY] = 2; append_cell_stats_by_command(event_parts, key, include_if_non_zero, number_to_include); cp = smartlist_pop_last(event_parts); tt_str_op("Z=relay:1", OP_EQ, cp); tor_free(cp); /* Add four CREATE cells. */ include_if_non_zero[CELL_CREATE] = 3; number_to_include[CELL_CREATE] = 4; append_cell_stats_by_command(event_parts, key, include_if_non_zero, number_to_include); cp = smartlist_pop_last(event_parts); tt_str_op("Z=create:4,relay:1", OP_EQ, cp); done: tor_free(cp); smartlist_free(event_parts); }
/** 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); }
/** Return true iff the two lists contain the same strings in the same * order, or if they are both NULL. */ int smartlist_strings_eq(const smartlist_t *sl1, const smartlist_t *sl2) { if (sl1 == NULL) return sl2 == NULL; if (sl2 == NULL) return 0; if (smartlist_len(sl1) != smartlist_len(sl2)) return 0; SMARTLIST_FOREACH(sl1, const char *, cp1, { const char *cp2 = smartlist_get(sl2, cp1_sl_idx); if (strcmp(cp1, cp2)) return 0; });
static void NS(test_main)(void *arg) { smartlist_t *out = smartlist_new(); routerset_t *set = routerset_new(); int out_len; node_t *ent; (void)arg; NS_MOCK(node_get_by_nickname); NS(mock_nickname) = tor_strdup("foo"); smartlist_add(set->list, NS(mock_nickname)); routerset_get_all_nodes(out, set, NULL, 0); out_len = smartlist_len(out); ent = (node_t *)smartlist_get(out, 0); smartlist_free(out); routerset_free(set); tt_int_op(out_len, ==, 1); tt_ptr_op(ent, ==, &NS(mock_node)); tt_int_op(CALLED(node_get_by_nickname), ==, 1); done: ; }
/* Parse a share random value line from the disk state and save it to dst * which is an allocated srv object. Return 0 on success else -1. */ static int disk_state_parse_srv(const char *value, sr_srv_t *dst) { int ret = -1; smartlist_t *args; sr_srv_t *srv; tor_assert(value); tor_assert(dst); args = smartlist_new(); smartlist_split_string(args, value, " ", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0); if (smartlist_len(args) < 2) { log_warn(LD_BUG, "SR: Too few arguments in shared random value. " "Line: %s", escaped(value)); goto error; } srv = sr_parse_srv(args); if (srv == NULL) { goto error; } dst->num_reveals = srv->num_reveals; memcpy(dst->value, srv->value, sizeof(dst->value)); tor_free(srv); ret = 0; error: SMARTLIST_FOREACH(args, char *, s, tor_free(s)); smartlist_free(args); return ret; }
static void NS(test_main)(void *arg) { smartlist_t *out = smartlist_new(); routerset_t *set = routerset_new(); int out_len; (void)arg; NS_MOCK(node_get_by_nickname); NS(mock_node).is_running = 0; NS(mock_nickname) = "foo"; smartlist_add(set->list, tor_strdup(NS(mock_nickname))); routerset_get_all_nodes(out, set, NULL, 1); out_len = smartlist_len(out); smartlist_free(out); routerset_free(set); tt_int_op(out_len, ==, 0); tt_int_op(CALLED(node_get_by_nickname), ==, 1); done: ; }
static void test_address_get_if_addrs_ifaddrs(void *arg) { smartlist_t *results = NULL; (void)arg; results = get_interface_addresses_ifaddrs(LOG_ERR, AF_UNSPEC); tt_assert(results); /* Some FreeBSD jails don't have localhost IP address. Instead, they only * have the address assigned to the jail (whatever that may be). * And a jail without a network connection might not have any addresses at * all. */ tt_assert(!smartlist_contains_null_tor_addr(results)); /* If there are addresses, they must be IPv4 or IPv6 */ if (smartlist_len(results) > 0) { tt_assert(smartlist_contains_ipv4_tor_addr(results) || smartlist_contains_ipv6_tor_addr(results)); } done: if (results) { SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t)); } smartlist_free(results); return; }
/** 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 NS(test_main)(void *arg) { routerset_t *set = routerset_new(); smartlist_t *out = smartlist_new(); int r; (void)arg; NS_MOCK(nodelist_get_list); smartlist_add(set->country_names, tor_strdup("{xx}")); NS(mock_smartlist) = smartlist_new(); NS(mock_node).is_running = 0; smartlist_add(NS(mock_smartlist), (void *)&NS(mock_node)); routerset_get_all_nodes(out, set, NULL, 1); r = smartlist_len(out); routerset_free(set); smartlist_free(out); smartlist_free(NS(mock_smartlist)); tt_int_op(r, ==, 0); tt_int_op(CALLED(nodelist_get_list), ==, 1); done: ; }
/** Add an entry to the GeoIP table, mapping all IPs between <b>low</b> and * <b>high</b>, inclusive, to the 2-letter country code <b>country</b>. */ static void geoip_add_entry(uint32_t low, uint32_t high, const char *country) { intptr_t idx; geoip_entry_t *ent; void *idxplus1_; if (high < low) return; idxplus1_ = strmap_get_lc(country_idxplus1_by_lc_code, country); if (!idxplus1_) { geoip_country_t *c = tor_malloc_zero(sizeof(geoip_country_t)); strlcpy(c->countrycode, country, sizeof(c->countrycode)); tor_strlower(c->countrycode); smartlist_add(geoip_countries, c); idx = smartlist_len(geoip_countries) - 1; strmap_set_lc(country_idxplus1_by_lc_code, country, (void*)(idx+1)); } else { idx = ((uintptr_t)idxplus1_)-1; } { geoip_country_t *c = smartlist_get(geoip_countries, idx); tor_assert(!strcasecmp(c->countrycode, country)); } ent = tor_malloc_zero(sizeof(geoip_entry_t)); ent->ip_low = low; ent->ip_high = high; ent->country = idx; smartlist_add(geoip_entries, ent); }
/** Return the number of countries recognized by the GeoIP database. */ int geoip_get_n_countries(void) { if (!geoip_countries) init_geoip_countries(); return (int) smartlist_len(geoip_countries); }
static void test_rend_cache_validate_intro_point_failure(void *data) { (void)data; rend_service_descriptor_t *desc = NULL; char *service_id = NULL; rend_intro_point_t *intro = NULL; const char *identity = NULL; rend_cache_failure_t *failure; rend_cache_failure_intro_t *ip; rend_cache_init(); create_descriptor(&desc, &service_id, 3); desc->timestamp = time(NULL) + RECENT_TIME; intro = (rend_intro_point_t *)smartlist_get(desc->intro_nodes, 0); identity = intro->extend_info->identity_digest; failure = rend_cache_failure_entry_new(); ip = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT); digestmap_set(failure->intro_failures, identity, ip); strmap_set_lc(rend_cache_failure, service_id, failure); // Test when we have an intro point in our cache validate_intro_point_failure(desc, service_id); tt_int_op(smartlist_len(desc->intro_nodes), OP_EQ, 2); done: rend_cache_free_all(); rend_service_descriptor_free(desc); tor_free(service_id); }
/* replacement for torflow in Tor. for now just grab the bandwidth we configured * in the XML and use that as the measured bandwidth value. since our configured * bandwidth doesnt change over time, this could just be run once (by setting the * time far in the future so the file is not seen as outdated). but we need to * run it after all routers are loaded, so its best to re-run periodically. * * eventually we will want an option to run something similar to the actual * torflow scripts that download files over Tor and computes bandwidth values. * in that case it needs to run more often to keep monitoring the actual state * of the network. * * torflow writes a few things to the v3bwfile. all Tor currently uses is: * * 0123456789 * node_id=$0123456789ABCDEF0123456789ABCDEF01234567 bw=12345 * ... * * where 0123456789 is the time, 0123456789ABCDEF0123456789ABCDEF01234567 is * the relay's fingerprint, and 12345 is the measured bandwidth in ?. */ void scalliontor_init_v3bw(ScallionTor* stor) { /* open the bw file, clearing it if it exists */ FILE *v3bw = fopen(stor->v3bw_name, "w"); if(v3bw == NULL) { stor->shadowlibFuncs->log(SHADOW_LOG_LEVEL_MESSAGE, __FUNCTION__, "v3bandwidth file not updated: can not open file '%s'\n", stor->v3bw_name); return; } time_t maxtime = -1; /* print time part on first line */ if(fprintf(v3bw, "%lu\n", maxtime) < 0) { /* uhhhh... */ stor->shadowlibFuncs->log(SHADOW_LOG_LEVEL_MESSAGE, __FUNCTION__, "v3bandwidth file not updated: can write time '%u' to file '%s'\n", maxtime, stor->v3bw_name); return; } routerlist_t *rlist = router_get_routerlist(); routerinfo_t *rinfo; /* print an entry for each router */ for (int i=0; i < smartlist_len(rlist->routers); i++) { rinfo = smartlist_get(rlist->routers, i); /* get the fingerprint from its digest */ char node_id[HEX_DIGEST_LEN+1]; base16_encode(node_id, HEX_DIGEST_LEN+1, rinfo->cache_info.identity_digest, DIGEST_LEN); /* the network address */ in_addr_t netaddr = htonl(rinfo->addr); /* ask shadow for this node's configured bandwidth */ guint bwdown = 0, bwup = 0; stor->shadowlibFuncs->getBandwidth(netaddr, &bwdown, &bwup); /* XXX careful here! shadow bandwidth may be different than the consensus * right now i believe this v3bw file is not used to compute the consensus * "w Bandwidth" line, and * intercept_rep_hist_bandwidth_assess and * intercept_router_get_advertised_bandwidth_capped * takes care of things. so leave it for now. */ guint bw = MIN(bwup, bwdown); if(fprintf(v3bw, "node_id=$%s bw=%u\n", node_id, bw) < 0) { /* uhhhh... */ stor->shadowlibFuncs->log(SHADOW_LOG_LEVEL_MESSAGE, __FUNCTION__, "v3bandwidth file not updated: can write line 'node_id=$%s bw=%u\n' to file '%s'\n", node_id, bw, stor->v3bw_name); return; } } fclose(v3bw); /* reschedule */ stor->shadowlibFuncs->createCallback((ShadowPluginCallbackFunc)scalliontor_init_v3bw, (gpointer)stor, VTORFLOW_SCHED_PERIOD); }
static int ewma_cmp_cmux(circuitmux_t *cmux_1, circuitmux_policy_data_t *pol_data_1, circuitmux_t *cmux_2, circuitmux_policy_data_t *pol_data_2) { ewma_policy_data_t *p1 = NULL, *p2 = NULL; cell_ewma_t *ce1 = NULL, *ce2 = NULL; tor_assert(cmux_1); tor_assert(pol_data_1); tor_assert(cmux_2); tor_assert(pol_data_2); p1 = TO_EWMA_POL_DATA(pol_data_1); p2 = TO_EWMA_POL_DATA(pol_data_2); if (p1 != p2) { /* Get the head cell_ewma_t from each queue */ if (smartlist_len(p1->active_circuit_pqueue) > 0) { ce1 = smartlist_get(p1->active_circuit_pqueue, 0); } if (smartlist_len(p2->active_circuit_pqueue) > 0) { ce2 = smartlist_get(p2->active_circuit_pqueue, 0); } /* Got both of them? */ if (ce1 != NULL && ce2 != NULL) { /* Pick whichever one has the better best circuit */ return compare_cell_ewma_counts(ce1, ce2); } else { if (ce1 != NULL ) { /* We only have a circuit on cmux_1, so prefer it */ return -1; } else if (ce2 != NULL) { /* We only have a circuit on cmux_2, so prefer it */ return 1; } else { /* No circuits at all; no preference */ return 0; } } } else { /* We got identical params */ return 0; } }
/** Return the two-letter country code associated with the number <b>num</b>, * or "??" for an unknown value. */ const char * geoip_get_country_name(country_t num) { if (geoip_countries && num >= 0 && num < smartlist_len(geoip_countries)) { geoip_country_t *c = smartlist_get(geoip_countries, num); return c->countrycode; } else return "??"; }
/** Any ports used lately? These are pre-seeded if we just started * up or if we're running a hidden service. */ int any_predicted_circuits(time_t now) { int predicted_circs_relevance_time; predicted_circs_relevance_time = (int)prediction_timeout; return smartlist_len(predicted_ports_list) || predicted_internal_time + predicted_circs_relevance_time >= now; }
/** Build a formatted command line used for the NT service. Return a * pointer to the formatted string on success, or NULL on failure. Set * *<b>using_default_torrc</b> to true if we're going to use the default * location to torrc, or 1 if an option was specified on the command line. */ static char * nt_service_command_line(int *using_default_torrc) { TCHAR tor_exe[MAX_PATH+1]; char tor_exe_ascii[MAX_PATH+1]; char *command, *options=NULL; smartlist_t *sl; int i, cmdlen; *using_default_torrc = 1; /* Get the location of tor.exe */ if (0 == GetModuleFileName(NULL, tor_exe, MAX_PATH)) return NULL; /* Get the service arguments */ sl = smartlist_create(); for (i = 1; i < backup_argc; ++i) { if (!strcmp(backup_argv[i], "--options") || !strcmp(backup_argv[i], "-options")) { while (++i < backup_argc) { if (!strcmp(backup_argv[i], "-f")) *using_default_torrc = 0; smartlist_add(sl, backup_argv[i]); } } } if (smartlist_len(sl)) options = smartlist_join_strings(sl,"\" \"",0,NULL); smartlist_free(sl); #ifdef UNICODE wcstombs(tor_exe_ascii, tor_exe, sizeof(tor_exe_ascii)); #else strlcpy(tor_exe_ascii, tor_exe, sizeof(tor_exe_ascii)); #endif /* Allocate a string for the NT service command line */ cmdlen = strlen(tor_exe_ascii) + (options?strlen(options):0) + 32; command = tor_malloc(cmdlen); /* Format the service command */ if (options) { if (tor_snprintf(command, cmdlen, "\"%s\" --nt-service \"%s\"", tor_exe_ascii, options)<0) { tor_free(command); /* sets command to NULL. */ } } else { /* ! options */ if (tor_snprintf(command, cmdlen, "\"%s\" --nt-service", tor_exe_ascii)<0) { tor_free(command); /* sets command to NULL. */ } } tor_free(options); return command; }
static void test_address_get_if_addrs6_list_no_internal(void *arg) { smartlist_t *results = NULL; (void)arg; /* We might drop a log_err */ setup_full_capture_of_logs(LOG_ERR); results = get_interface_address6_list(LOG_ERR, AF_INET6, 0); tt_int_op(smartlist_len(mock_saved_logs()), OP_LE, 1); if (smartlist_len(mock_saved_logs()) == 1) { expect_log_msg_containing_either4("connect() failed", "unable to create socket", "Address that we determined via UDP " "socket magic is unsuitable for public " "comms.", "getsockname() to determine interface " "failed"); } teardown_capture_of_logs(); tt_ptr_op(results, OP_NE, NULL); /* Work even on systems without IPv6 interfaces */ tt_int_op(smartlist_len(results),OP_GE,0); tt_assert(!smartlist_contains_localhost_tor_addr(results)); tt_assert(!smartlist_contains_multicast_tor_addr(results)); tt_assert(!smartlist_contains_internal_tor_addr(results)); tt_assert(!smartlist_contains_null_tor_addr(results)); /* if there are any addresses, they must be IPv6 */ tt_assert(!smartlist_contains_ipv4_tor_addr(results)); if (smartlist_len(results) > 0) { tt_assert(smartlist_contains_ipv6_tor_addr(results)); } done: teardown_capture_of_logs(); interface_address6_list_free(results); return; }