Merge remote-tracking branch 'public/ticket_13119_v3'

This commit is contained in:
Nick Mathewson 2014-09-17 10:27:40 -04:00
commit 35156ffcc3
24 changed files with 2768 additions and 2682 deletions

6
changes/ticket_13119 Normal file
View File

@ -0,0 +1,6 @@
o Code refactoring:
- Revise all unit tests that used the legacy test_* macros to
instead use the recommended tt_* macros. This patch was
generated with coccinelle, to avoid manual errors. Closes
ticket 13119.

View File

@ -232,7 +232,7 @@ free_pregenerated_keys(void)
/** Run unit tests for the onion handshake code. */
static void
test_onion_handshake(void)
test_onion_handshake(void *arg)
{
/* client-side */
crypto_dh_t *c_dh = NULL;
@ -245,12 +245,13 @@ test_onion_handshake(void)
/* shared */
crypto_pk_t *pk = NULL, *pk2 = NULL;
(void)arg;
pk = pk_generate(0);
pk2 = pk_generate(1);
/* client handshake 1. */
memset(c_buf, 0, TAP_ONIONSKIN_CHALLENGE_LEN);
test_assert(! onion_skin_TAP_create(pk, &c_dh, c_buf));
tt_assert(! onion_skin_TAP_create(pk, &c_dh, c_buf));
for (i = 1; i <= 3; ++i) {
crypto_pk_t *k1, *k2;
@ -267,16 +268,16 @@ test_onion_handshake(void)
memset(s_buf, 0, TAP_ONIONSKIN_REPLY_LEN);
memset(s_keys, 0, 40);
test_assert(! onion_skin_TAP_server_handshake(c_buf, k1, k2,
tt_assert(! onion_skin_TAP_server_handshake(c_buf, k1, k2,
s_buf, s_keys, 40));
/* client handshake 2 */
memset(c_keys, 0, 40);
test_assert(! onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40));
tt_assert(! onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40));
test_memeq(c_keys, s_keys, 40);
tt_mem_op(c_keys,==, s_keys, 40);
memset(s_buf, 0, 40);
test_memneq(c_keys, s_buf, 40);
tt_mem_op(c_keys,!=, s_buf, 40);
}
done:
crypto_dh_free(c_dh);
@ -323,7 +324,7 @@ test_bad_onion_handshake(void *arg)
/* client handshake 1: do it straight. */
memset(c_buf, 0, TAP_ONIONSKIN_CHALLENGE_LEN);
test_assert(! onion_skin_TAP_create(pk, &c_dh, c_buf));
tt_assert(! onion_skin_TAP_create(pk, &c_dh, c_buf));
/* Server: Case 3: we just don't have the right key. */
tt_int_op(-1, ==,
@ -351,7 +352,7 @@ test_bad_onion_handshake(void *arg)
/* Let the client finish; make sure it can. */
tt_int_op(0, ==,
onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40));
test_memeq(s_keys, c_keys, 40);
tt_mem_op(s_keys,==, c_keys, 40);
/* Client: Case 2: The server sent back a degenerate DH. */
memset(s_buf, 0, sizeof(s_buf));
@ -408,9 +409,9 @@ test_ntor_handshake(void *arg)
tt_int_op(0, ==, onion_skin_ntor_client_handshake(c_state, s_buf,
c_keys, 400));
test_memeq(c_keys, s_keys, 400);
tt_mem_op(c_keys,==, s_keys, 400);
memset(s_buf, 0, 40);
test_memneq(c_keys, s_buf, 40);
tt_mem_op(c_keys,!=, s_buf, 40);
done:
ntor_handshake_state_free(c_state);
@ -420,7 +421,7 @@ test_ntor_handshake(void *arg)
/** Run unit tests for the onion queues. */
static void
test_onion_queues(void)
test_onion_queues(void *arg)
{
uint8_t buf1[TAP_ONIONSKIN_CHALLENGE_LEN] = {0};
uint8_t buf2[NTOR_ONIONSKIN_LEN] = {0};
@ -431,6 +432,7 @@ test_onion_queues(void)
create_cell_t *onionskin = NULL, *create2_ptr;
create_cell_t *create1 = tor_malloc_zero(sizeof(create_cell_t));
create_cell_t *create2 = tor_malloc_zero(sizeof(create_cell_t));
(void)arg;
create2_ptr = create2; /* remember, but do not free */
create_cell_init(create1, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
@ -438,24 +440,24 @@ test_onion_queues(void)
create_cell_init(create2, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
NTOR_ONIONSKIN_LEN, buf2);
test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
test_eq(0, onion_pending_add(circ1, create1));
tt_int_op(0,==, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
tt_int_op(0,==, onion_pending_add(circ1, create1));
create1 = NULL;
test_eq(1, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
tt_int_op(1,==, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
test_eq(0, onion_pending_add(circ2, create2));
tt_int_op(0,==, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
tt_int_op(0,==, onion_pending_add(circ2, create2));
create2 = NULL;
test_eq(1, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
tt_int_op(1,==, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
test_eq_ptr(circ2, onion_next_task(&onionskin));
test_eq(1, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
tt_ptr_op(circ2,==, onion_next_task(&onionskin));
tt_int_op(1,==, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
tt_int_op(0,==, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
tt_ptr_op(onionskin, ==, create2_ptr);
clear_pending_onions();
test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
tt_int_op(0,==, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
tt_int_op(0,==, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
done:
circuit_free(TO_CIRCUIT(circ1));
@ -466,7 +468,7 @@ test_onion_queues(void)
}
static void
test_circuit_timeout(void)
test_circuit_timeout(void *arg)
{
/* Plan:
* 1. Generate 1000 samples
@ -484,6 +486,7 @@ test_circuit_timeout(void)
or_state_t *state=NULL;
int i, runs;
double close_ms;
(void)arg;
circuit_build_times_init(&initial);
circuit_build_times_init(&estimate);
circuit_build_times_init(&final);
@ -518,11 +521,11 @@ test_circuit_timeout(void)
} while (fabs(circuit_build_times_cdf(&initial, timeout0) -
circuit_build_times_cdf(&initial, timeout1)) > 0.02);
test_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
tt_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
circuit_build_times_update_state(&estimate, state);
circuit_build_times_free_timeouts(&final);
test_assert(circuit_build_times_parse_state(&final, state) == 0);
tt_assert(circuit_build_times_parse_state(&final, state) == 0);
circuit_build_times_update_alpha(&final);
timeout2 = circuit_build_times_calculate_timeout(&final,
@ -532,7 +535,7 @@ test_circuit_timeout(void)
log_notice(LD_CIRC, "Timeout2 is %f, Xm is %d", timeout2, final.Xm);
/* 5% here because some accuracy is lost due to histogram conversion */
test_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
tt_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
circuit_build_times_cdf(&initial, timeout2)) < 0.05);
for (runs = 0; runs < 50; runs++) {
@ -555,8 +558,8 @@ test_circuit_timeout(void)
CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
}
test_assert(!circuit_build_times_network_check_changed(&estimate));
test_assert(!circuit_build_times_network_check_changed(&final));
tt_assert(!circuit_build_times_network_check_changed(&estimate));
tt_assert(!circuit_build_times_network_check_changed(&final));
/* Reset liveness to be non-live */
final.liveness.network_last_live = 0;
@ -565,27 +568,27 @@ test_circuit_timeout(void)
build_times_idx = estimate.build_times_idx;
total_build_times = estimate.total_build_times;
test_assert(circuit_build_times_network_check_live(&estimate));
test_assert(circuit_build_times_network_check_live(&final));
tt_assert(circuit_build_times_network_check_live(&estimate));
tt_assert(circuit_build_times_network_check_live(&final));
circuit_build_times_count_close(&estimate, 0,
(time_t)(approx_time()-estimate.close_ms/1000.0-1));
circuit_build_times_count_close(&final, 0,
(time_t)(approx_time()-final.close_ms/1000.0-1));
test_assert(!circuit_build_times_network_check_live(&estimate));
test_assert(!circuit_build_times_network_check_live(&final));
tt_assert(!circuit_build_times_network_check_live(&estimate));
tt_assert(!circuit_build_times_network_check_live(&final));
log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
build_times_idx, estimate.build_times_idx,
total_build_times, estimate.total_build_times);
/* Check rollback index. Should match top of loop. */
test_assert(build_times_idx == estimate.build_times_idx);
tt_assert(build_times_idx == estimate.build_times_idx);
// This can fail if estimate.total_build_times == 1000, because
// in that case, rewind actually causes us to lose timeouts
if (total_build_times != CBT_NCIRCUITS_TO_OBSERVE)
test_assert(total_build_times == estimate.total_build_times);
tt_assert(total_build_times == estimate.total_build_times);
/* Now simulate that the network has become live and we need
* a change */
@ -600,12 +603,12 @@ test_circuit_timeout(void)
}
}
test_assert(estimate.liveness.after_firsthop_idx == 0);
test_assert(final.liveness.after_firsthop_idx ==
tt_assert(estimate.liveness.after_firsthop_idx == 0);
tt_assert(final.liveness.after_firsthop_idx ==
CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1);
test_assert(circuit_build_times_network_check_live(&estimate));
test_assert(circuit_build_times_network_check_live(&final));
tt_assert(circuit_build_times_network_check_live(&estimate));
tt_assert(circuit_build_times_network_check_live(&final));
circuit_build_times_count_timeout(&final, 1);
}
@ -619,7 +622,7 @@ test_circuit_timeout(void)
/** Test encoding and parsing of rendezvous service descriptors. */
static void
test_rend_fns(void)
test_rend_fns(void *arg)
{
rend_service_descriptor_t *generated = NULL, *parsed = NULL;
char service_id[DIGEST_LEN];
@ -642,16 +645,17 @@ test_rend_fns(void)
char address6[] = "foo.bar.abcdefghijklmnop.onion";
char address7[] = ".abcdefghijklmnop.onion";
test_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
test_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
test_streq(address2, "aaaaaaaaaaaaaaaa");
test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
test_assert(ONION_HOSTNAME == parse_extended_hostname(address5));
test_streq(address5, "abcdefghijklmnop");
test_assert(ONION_HOSTNAME == parse_extended_hostname(address6));
test_streq(address6, "abcdefghijklmnop");
test_assert(BAD_HOSTNAME == parse_extended_hostname(address7));
(void)arg;
tt_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
tt_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
tt_str_op(address2,==, "aaaaaaaaaaaaaaaa");
tt_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
tt_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
tt_assert(ONION_HOSTNAME == parse_extended_hostname(address5));
tt_str_op(address5,==, "abcdefghijklmnop");
tt_assert(ONION_HOSTNAME == parse_extended_hostname(address6));
tt_str_op(address6,==, "abcdefghijklmnop");
tt_assert(BAD_HOSTNAME == parse_extended_hostname(address7));
pk1 = pk_generate(0);
pk2 = pk_generate(1);
@ -684,40 +688,41 @@ test_rend_fns(void)
intro->intro_key = crypto_pk_dup_key(pk2);
smartlist_add(generated->intro_nodes, intro);
}
test_assert(rend_encode_v2_descriptors(descs, generated, now, 0,
tt_assert(rend_encode_v2_descriptors(descs, generated, now, 0,
REND_NO_AUTH, NULL, NULL) > 0);
test_assert(rend_compute_v2_desc_id(computed_desc_id, service_id_base32,
tt_assert(rend_compute_v2_desc_id(computed_desc_id, service_id_base32,
NULL, now, 0) == 0);
test_memeq(((rend_encoded_v2_service_descriptor_t *)
smartlist_get(descs, 0))->desc_id, computed_desc_id, DIGEST_LEN);
test_assert(rend_parse_v2_service_descriptor(&parsed, parsed_desc_id,
tt_mem_op(((rend_encoded_v2_service_descriptor_t *)
smartlist_get(descs, 0))->desc_id, ==,
computed_desc_id, DIGEST_LEN);
tt_assert(rend_parse_v2_service_descriptor(&parsed, parsed_desc_id,
&intro_points_encrypted,
&intro_points_size,
&encoded_size,
&next_desc,
((rend_encoded_v2_service_descriptor_t *)
smartlist_get(descs, 0))->desc_str) == 0);
test_assert(parsed);
test_memeq(((rend_encoded_v2_service_descriptor_t *)
smartlist_get(descs, 0))->desc_id, parsed_desc_id, DIGEST_LEN);
test_eq(rend_parse_introduction_points(parsed, intro_points_encrypted,
intro_points_size), 3);
test_assert(!crypto_pk_cmp_keys(generated->pk, parsed->pk));
test_eq(parsed->timestamp, now);
test_eq(parsed->version, 2);
test_eq(parsed->protocols, 42);
test_eq(smartlist_len(parsed->intro_nodes), 3);
tt_assert(parsed);
tt_mem_op(((rend_encoded_v2_service_descriptor_t *)
smartlist_get(descs, 0))->desc_id,==, parsed_desc_id, DIGEST_LEN);
tt_int_op(rend_parse_introduction_points(parsed, intro_points_encrypted,
intro_points_size),==, 3);
tt_assert(!crypto_pk_cmp_keys(generated->pk, parsed->pk));
tt_int_op(parsed->timestamp,==, now);
tt_int_op(parsed->version,==, 2);
tt_int_op(parsed->protocols,==, 42);
tt_int_op(smartlist_len(parsed->intro_nodes),==, 3);
for (i = 0; i < smartlist_len(parsed->intro_nodes); i++) {
rend_intro_point_t *par_intro = smartlist_get(parsed->intro_nodes, i),
*gen_intro = smartlist_get(generated->intro_nodes, i);
extend_info_t *par_info = par_intro->extend_info;
extend_info_t *gen_info = gen_intro->extend_info;
test_assert(!crypto_pk_cmp_keys(gen_info->onion_key, par_info->onion_key));
test_memeq(gen_info->identity_digest, par_info->identity_digest,
tt_assert(!crypto_pk_cmp_keys(gen_info->onion_key, par_info->onion_key));
tt_mem_op(gen_info->identity_digest,==, par_info->identity_digest,
DIGEST_LEN);
test_streq(gen_info->nickname, par_info->nickname);
test_assert(tor_addr_eq(&gen_info->addr, &par_info->addr));
test_eq(gen_info->port, par_info->port);
tt_str_op(gen_info->nickname,==, par_info->nickname);
tt_assert(tor_addr_eq(&gen_info->addr, &par_info->addr));
tt_int_op(gen_info->port,==, par_info->port);
}
rend_service_descriptor_free(parsed);
@ -761,17 +766,17 @@ test_rend_fns(void)
} while (0)
#define CHECK_COUNTRY(country, val) do { \
/* test ipv4 country lookup */ \
test_streq(country, \
tt_str_op(country, ==, \
geoip_get_country_name(geoip_get_country_by_ipv4(val))); \
/* test ipv6 country lookup */ \
SET_TEST_IPV6(val); \
test_streq(country, \
tt_str_op(country, ==, \
geoip_get_country_name(geoip_get_country_by_ipv6(&in6))); \
} while (0)
/** Run unit tests for GeoIP code. */
static void
test_geoip(void)
test_geoip(void *arg)
{
int i, j;
time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
@ -825,23 +830,24 @@ test_geoip(void)
/* Populate the DB a bit. Add these in order, since we can't do the final
* 'sort' step. These aren't very good IP addresses, but they're perfectly
* fine uint32_t values. */
test_eq(0, geoip_parse_entry("10,50,AB", AF_INET));
test_eq(0, geoip_parse_entry("52,90,XY", AF_INET));
test_eq(0, geoip_parse_entry("95,100,AB", AF_INET));
test_eq(0, geoip_parse_entry("\"105\",\"140\",\"ZZ\"", AF_INET));
test_eq(0, geoip_parse_entry("\"150\",\"190\",\"XY\"", AF_INET));
test_eq(0, geoip_parse_entry("\"200\",\"250\",\"AB\"", AF_INET));
(void)arg;
tt_int_op(0,==, geoip_parse_entry("10,50,AB", AF_INET));
tt_int_op(0,==, geoip_parse_entry("52,90,XY", AF_INET));
tt_int_op(0,==, geoip_parse_entry("95,100,AB", AF_INET));
tt_int_op(0,==, geoip_parse_entry("\"105\",\"140\",\"ZZ\"", AF_INET));
tt_int_op(0,==, geoip_parse_entry("\"150\",\"190\",\"XY\"", AF_INET));
tt_int_op(0,==, geoip_parse_entry("\"200\",\"250\",\"AB\"", AF_INET));
/* Populate the IPv6 DB equivalently with fake IPs in the same range */
test_eq(0, geoip_parse_entry("::a,::32,AB", AF_INET6));
test_eq(0, geoip_parse_entry("::34,::5a,XY", AF_INET6));
test_eq(0, geoip_parse_entry("::5f,::64,AB", AF_INET6));
test_eq(0, geoip_parse_entry("::69,::8c,ZZ", AF_INET6));
test_eq(0, geoip_parse_entry("::96,::be,XY", AF_INET6));
test_eq(0, geoip_parse_entry("::c8,::fa,AB", AF_INET6));
tt_int_op(0,==, geoip_parse_entry("::a,::32,AB", AF_INET6));
tt_int_op(0,==, geoip_parse_entry("::34,::5a,XY", AF_INET6));
tt_int_op(0,==, geoip_parse_entry("::5f,::64,AB", AF_INET6));
tt_int_op(0,==, geoip_parse_entry("::69,::8c,ZZ", AF_INET6));
tt_int_op(0,==, geoip_parse_entry("::96,::be,XY", AF_INET6));
tt_int_op(0,==, geoip_parse_entry("::c8,::fa,AB", AF_INET6));
/* We should have 4 countries: ??, ab, xy, zz. */
test_eq(4, geoip_get_n_countries());
tt_int_op(4,==, geoip_get_n_countries());
memset(&in6, 0, sizeof(in6));
CHECK_COUNTRY("??", 3);
@ -852,9 +858,9 @@ test_geoip(void)
CHECK_COUNTRY("xy", 190);
CHECK_COUNTRY("??", 2000);
test_eq(0, geoip_get_country_by_ipv4(3));
tt_int_op(0,==, geoip_get_country_by_ipv4(3));
SET_TEST_IPV6(3);
test_eq(0, geoip_get_country_by_ipv6(&in6));
tt_int_op(0,==, geoip_get_country_by_ipv6(&in6));
get_options_mutable()->BridgeRelay = 1;
get_options_mutable()->BridgeRecordUsageByCountry = 1;
@ -877,41 +883,41 @@ test_geoip(void)
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
}
geoip_get_client_history(GEOIP_CLIENT_CONNECT, &s, &v);
test_assert(s);
test_assert(v);
test_streq("zz=24,ab=16,xy=8", s);
test_streq("v4=16,v6=16", v);
tt_assert(s);
tt_assert(v);
tt_str_op("zz=24,ab=16,xy=8",==, s);
tt_str_op("v4=16,v6=16",==, v);
tor_free(s);
tor_free(v);
/* Now clear out all the AB observations. */
geoip_remove_old_clients(now-6000);
geoip_get_client_history(GEOIP_CLIENT_CONNECT, &s, &v);
test_assert(s);
test_assert(v);
test_streq("zz=24,xy=8", s);
test_streq("v4=16,v6=16", v);
tt_assert(s);
tt_assert(v);
tt_str_op("zz=24,xy=8",==, s);
tt_str_op("v4=16,v6=16",==, v);
tor_free(s);
tor_free(v);
/* Start testing bridge statistics by making sure that we don't output
* bridge stats without initializing them. */
s = geoip_format_bridge_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Initialize stats and generate the bridge-stats history string out of
* the connecting clients added above. */
geoip_bridge_stats_init(now);
s = geoip_format_bridge_stats(now + 86400);
test_assert(s);
test_streq(bridge_stats_1, s);
tt_assert(s);
tt_str_op(bridge_stats_1,==, s);
tor_free(s);
/* Stop collecting bridge stats and make sure we don't write a history
* string anymore. */
geoip_bridge_stats_term();
s = geoip_format_bridge_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Stop being a bridge and start being a directory mirror that gathers
* directory request statistics. */
@ -925,7 +931,7 @@ test_geoip(void)
SET_TEST_ADDRESS(100);
geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
s = geoip_format_dirreq_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Initialize stats, note one connecting client, and generate the
* dirreq-stats history string. */
@ -933,7 +939,7 @@ test_geoip(void)
SET_TEST_ADDRESS(100);
geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
s = geoip_format_dirreq_stats(now + 86400);
test_streq(dirreq_stats_1, s);
tt_str_op(dirreq_stats_1,==, s);
tor_free(s);
/* Stop collecting stats, add another connecting client, and ensure we
@ -942,7 +948,7 @@ test_geoip(void)
SET_TEST_ADDRESS(101);
geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
s = geoip_format_dirreq_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Re-start stats, add a connecting client, reset stats, and make sure
* that we get an all empty history string. */
@ -951,20 +957,20 @@ test_geoip(void)
geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
geoip_reset_dirreq_stats(now);
s = geoip_format_dirreq_stats(now + 86400);
test_streq(dirreq_stats_2, s);
tt_str_op(dirreq_stats_2,==, s);
tor_free(s);
/* Note a successful network status response and make sure that it
* appears in the history string. */
geoip_note_ns_response(GEOIP_SUCCESS);
s = geoip_format_dirreq_stats(now + 86400);
test_streq(dirreq_stats_3, s);
tt_str_op(dirreq_stats_3,==, s);
tor_free(s);
/* Start a tunneled directory request. */
geoip_start_dirreq((uint64_t) 1, 1024, DIRREQ_TUNNELED);
s = geoip_format_dirreq_stats(now + 86400);
test_streq(dirreq_stats_4, s);
tt_str_op(dirreq_stats_4,==, s);
tor_free(s);
/* Stop collecting directory request statistics and start gathering
@ -978,7 +984,7 @@ test_geoip(void)
SET_TEST_ADDRESS(100);
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
s = geoip_format_entry_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Initialize stats, note one connecting client, and generate the
* entry-stats history string. */
@ -986,7 +992,7 @@ test_geoip(void)
SET_TEST_ADDRESS(100);
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
s = geoip_format_entry_stats(now + 86400);
test_streq(entry_stats_1, s);
tt_str_op(entry_stats_1,==, s);
tor_free(s);
/* Stop collecting stats, add another connecting client, and ensure we
@ -995,7 +1001,7 @@ test_geoip(void)
SET_TEST_ADDRESS(101);
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
s = geoip_format_entry_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Re-start stats, add a connecting client, reset stats, and make sure
* that we get an all empty history string. */
@ -1004,7 +1010,7 @@ test_geoip(void)
geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
geoip_reset_entry_stats(now);
s = geoip_format_entry_stats(now + 86400);
test_streq(entry_stats_2, s);
tt_str_op(entry_stats_2,==, s);
tor_free(s);
/* Stop collecting entry statistics. */
@ -1017,7 +1023,7 @@ test_geoip(void)
}
static void
test_geoip_with_pt(void)
test_geoip_with_pt(void *arg)
{
time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
char *s = NULL;
@ -1025,6 +1031,7 @@ test_geoip_with_pt(void)
tor_addr_t addr;
struct in6_addr in6;
(void)arg;
get_options_mutable()->BridgeRelay = 1;
get_options_mutable()->BridgeRecordUsageByCountry = 1;
@ -1076,7 +1083,7 @@ test_geoip_with_pt(void)
/* Test the transport history string. */
s = geoip_get_transport_history();
tor_assert(s);
test_streq(s, "<OR>=8,alpha=16,beta=8,charlie=16,ddr=136,"
tt_str_op(s,==, "<OR>=8,alpha=16,beta=8,charlie=16,ddr=136,"
"entropy=8,fire=8,google=8");
/* Stop collecting entry statistics. */
@ -1093,7 +1100,7 @@ test_geoip_with_pt(void)
/** Run unit tests for stats code. */
static void
test_stats(void)
test_stats(void *arg)
{
time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
char *s = NULL;
@ -1101,10 +1108,11 @@ test_stats(void)
/* Start with testing exit port statistics; we shouldn't collect exit
* stats without initializing them. */
(void)arg;
rep_hist_note_exit_stream_opened(80);
rep_hist_note_exit_bytes(80, 100, 10000);
s = rep_hist_format_exit_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Initialize stats, note some streams and bytes, and generate history
* string. */
@ -1115,10 +1123,10 @@ test_stats(void)
rep_hist_note_exit_bytes(443, 100, 10000);
rep_hist_note_exit_bytes(443, 100, 10000);
s = rep_hist_format_exit_stats(now + 86400);
test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
tt_str_op("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
"exit-kibibytes-written 80=1,443=1,other=0\n"
"exit-kibibytes-read 80=10,443=20,other=0\n"
"exit-streams-opened 80=4,443=4,other=0\n", s);
"exit-streams-opened 80=4,443=4,other=0\n",==, s);
tor_free(s);
/* Add a few bytes on 10 more ports and ensure that only the top 10
@ -1128,13 +1136,13 @@ test_stats(void)
rep_hist_note_exit_stream_opened(i);
}
s = rep_hist_format_exit_stats(now + 86400);
test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
tt_str_op("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
"exit-kibibytes-written 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
"59=1,80=1,443=1,other=1\n"
"exit-kibibytes-read 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
"59=1,80=10,443=20,other=1\n"
"exit-streams-opened 52=4,53=4,54=4,55=4,56=4,57=4,58=4,"
"59=4,80=4,443=4,other=4\n", s);
"59=4,80=4,443=4,other=4\n",==, s);
tor_free(s);
/* Stop collecting stats, add some bytes, and ensure we don't generate
@ -1142,7 +1150,7 @@ test_stats(void)
rep_hist_exit_stats_term();
rep_hist_note_exit_bytes(80, 100, 10000);
s = rep_hist_format_exit_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Re-start stats, add some bytes, reset stats, and see what history we
* get when observing no streams or bytes at all. */
@ -1151,17 +1159,17 @@ test_stats(void)
rep_hist_note_exit_bytes(80, 100, 10000);
rep_hist_reset_exit_stats(now);
s = rep_hist_format_exit_stats(now + 86400);
test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
tt_str_op("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
"exit-kibibytes-written other=0\n"
"exit-kibibytes-read other=0\n"
"exit-streams-opened other=0\n", s);
"exit-streams-opened other=0\n",==, s);
tor_free(s);
/* Continue with testing connection statistics; we shouldn't collect
* conn stats without initializing them. */
rep_hist_note_or_conn_bytes(1, 20, 400, now);
s = rep_hist_format_conn_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Initialize stats, note bytes, and generate history string. */
rep_hist_conn_stats_init(now);
@ -1170,7 +1178,7 @@ test_stats(void)
rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
s = rep_hist_format_conn_stats(now + 86400);
test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n", s);
tt_str_op("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n",==, s);
tor_free(s);
/* Stop collecting stats, add some bytes, and ensure we don't generate
@ -1178,7 +1186,7 @@ test_stats(void)
rep_hist_conn_stats_term();
rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
s = rep_hist_format_conn_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Re-start stats, add some bytes, reset stats, and see what history we
* get when observing no bytes at all. */
@ -1189,26 +1197,26 @@ test_stats(void)
rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
rep_hist_reset_conn_stats(now);
s = rep_hist_format_conn_stats(now + 86400);
test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n", s);
tt_str_op("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n",==, s);
tor_free(s);
/* Continue with testing buffer statistics; we shouldn't collect buffer
* stats without initializing them. */
rep_hist_add_buffer_stats(2.0, 2.0, 20);
s = rep_hist_format_buffer_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Initialize stats, add statistics for a single circuit, and generate
* the history string. */
rep_hist_buffer_stats_init(now);
rep_hist_add_buffer_stats(2.0, 2.0, 20);
s = rep_hist_format_buffer_stats(now + 86400);
test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
tt_str_op("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
"cell-processed-cells 20,0,0,0,0,0,0,0,0,0\n"
"cell-queued-cells 2.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
"0.00,0.00\n"
"cell-time-in-queue 2,0,0,0,0,0,0,0,0,0\n"
"cell-circuits-per-decile 1\n", s);
"cell-circuits-per-decile 1\n",==, s);
tor_free(s);
/* Add nineteen more circuit statistics to the one that's already in the
@ -1218,12 +1226,12 @@ test_stats(void)
for (i = 20; i < 30; i++)
rep_hist_add_buffer_stats(3.5, 3.5, i);
s = rep_hist_format_buffer_stats(now + 86400);
test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
tt_str_op("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
"cell-processed-cells 29,28,27,26,25,24,23,22,21,20\n"
"cell-queued-cells 2.75,2.75,2.75,2.75,2.75,2.75,2.75,2.75,"
"2.75,2.75\n"
"cell-time-in-queue 3,3,3,3,3,3,3,3,3,3\n"
"cell-circuits-per-decile 2\n", s);
"cell-circuits-per-decile 2\n",==, s);
tor_free(s);
/* Stop collecting stats, add statistics for one circuit, and ensure we
@ -1231,7 +1239,7 @@ test_stats(void)
rep_hist_buffer_stats_term();
rep_hist_add_buffer_stats(2.0, 2.0, 20);
s = rep_hist_format_buffer_stats(now + 86400);
test_assert(!s);
tt_assert(!s);
/* Re-start stats, add statistics for one circuit, reset stats, and make
* sure that the history has all zeros. */
@ -1239,46 +1247,21 @@ test_stats(void)
rep_hist_add_buffer_stats(2.0, 2.0, 20);
rep_hist_reset_buffer_stats(now);
s = rep_hist_format_buffer_stats(now + 86400);
test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
tt_str_op("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
"cell-processed-cells 0,0,0,0,0,0,0,0,0,0\n"
"cell-queued-cells 0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
"0.00,0.00\n"
"cell-time-in-queue 0,0,0,0,0,0,0,0,0,0\n"
"cell-circuits-per-decile 0\n", s);
"cell-circuits-per-decile 0\n",==, s);
done:
tor_free(s);
}
static void *
legacy_test_setup(const struct testcase_t *testcase)
{
return testcase->setup_data;
}
void
legacy_test_helper(void *data)
{
void (*fn)(void) = data;
fn();
}
static int
legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
{
(void)ptr;
(void)testcase;
return 1;
}
const struct testcase_setup_t legacy_setup = {
legacy_test_setup, legacy_test_cleanup
};
#define ENT(name) \
{ #name, legacy_test_helper, 0, &legacy_setup, test_ ## name }
{ #name, test_ ## name , 0, NULL, NULL }
#define FORK(name) \
{ #name, legacy_test_helper, TT_FORK, &legacy_setup, test_ ## name }
{ #name, test_ ## name , TT_FORK, NULL, NULL }
static struct testcase_t test_array[] = {
ENT(onion_handshake),

View File

@ -22,25 +22,6 @@
#define PRETTY_FUNCTION ""
#endif
#define test_fail_msg(msg) TT_DIE((msg))
#define test_fail() test_fail_msg("Assertion failed.")
#define test_assert(expr) tt_assert(expr)
#define test_eq(expr1, expr2) tt_int_op((expr1), ==, (expr2))
#define test_eq_ptr(expr1, expr2) tt_ptr_op((expr1), ==, (expr2))
#define test_neq(expr1, expr2) tt_int_op((expr1), !=, (expr2))
#define test_neq_ptr(expr1, expr2) tt_ptr_op((expr1), !=, (expr2))
#define test_streq(expr1, expr2) tt_str_op((expr1), ==, (expr2))
#define test_strneq(expr1, expr2) tt_str_op((expr1), !=, (expr2))
#define test_mem_op(expr1, op, expr2, len) \
tt_mem_op((expr1), op, (expr2), (len))
#define test_memeq(expr1, expr2, len) test_mem_op((expr1), ==, (expr2), len)
#define test_memneq(expr1, expr2, len) test_mem_op((expr1), !=, (expr2), len)
/* As test_mem_op, but decodes 'hex' before comparing. There must be a
* local char* variable called mem_op_hex_tmp for this to work. */
#define test_mem_op_hex(expr1, op, hex) \
@ -50,7 +31,7 @@
mem_op_hex_tmp = tor_malloc(length/2); \
tor_assert((length&1)==0); \
base16_decode(mem_op_hex_tmp, length/2, hex, length); \
test_mem_op(expr1, op, mem_op_hex_tmp, length/2); \
tt_mem_op(expr1, op, mem_op_hex_tmp, length/2); \
STMT_END
#define test_memeq_hex(expr1, hex) test_mem_op_hex(expr1, ==, hex)
@ -85,9 +66,6 @@
const char *get_fname(const char *name);
crypto_pk_t *pk_generate(int idx);
void legacy_test_helper(void *data);
extern const struct testcase_setup_t legacy_setup;
#define US2_CONCAT_2__(a, b) a ## __ ## b
#define US_CONCAT_2__(a, b) a ## _ ## b
#define US_CONCAT_3__(a, b, c) a ## _ ## b ## _ ## c

View File

@ -10,49 +10,50 @@
#include "addressmap.h"
static void
test_addr_basic(void)
test_addr_basic(void *arg)
{
uint32_t u32;
uint16_t u16;
char *cp;
/* Test addr_port_lookup */
(void)arg;
cp = NULL; u32 = 3; u16 = 3;
test_assert(!addr_port_lookup(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
test_streq(cp, "1.2.3.4");
test_eq(u32, 0x01020304u);
test_eq(u16, 0);
tt_assert(!addr_port_lookup(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
tt_str_op(cp,==, "1.2.3.4");
tt_int_op(u32,==, 0x01020304u);
tt_int_op(u16,==, 0);
tor_free(cp);
test_assert(!addr_port_lookup(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
test_streq(cp, "4.3.2.1");
test_eq(u32, 0x04030201u);
test_eq(u16, 99);
tt_assert(!addr_port_lookup(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
tt_str_op(cp,==, "4.3.2.1");
tt_int_op(u32,==, 0x04030201u);
tt_int_op(u16,==, 99);
tor_free(cp);
test_assert(!addr_port_lookup(LOG_WARN, "nonexistent.address:4040",
tt_assert(!addr_port_lookup(LOG_WARN, "nonexistent.address:4040",
&cp, NULL, &u16));
test_streq(cp, "nonexistent.address");
test_eq(u16, 4040);
tt_str_op(cp,==, "nonexistent.address");
tt_int_op(u16,==, 4040);
tor_free(cp);
test_assert(!addr_port_lookup(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
test_streq(cp, "localhost");
test_eq(u32, 0x7f000001u);
test_eq(u16, 9999);
tt_assert(!addr_port_lookup(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
tt_str_op(cp,==, "localhost");
tt_int_op(u32,==, 0x7f000001u);
tt_int_op(u16,==, 9999);
tor_free(cp);
u32 = 3;
test_assert(!addr_port_lookup(LOG_WARN, "localhost", NULL, &u32, &u16));
test_eq_ptr(cp, NULL);
test_eq(u32, 0x7f000001u);
test_eq(u16, 0);
tt_assert(!addr_port_lookup(LOG_WARN, "localhost", NULL, &u32, &u16));
tt_ptr_op(cp,==, NULL);
tt_int_op(u32,==, 0x7f000001u);
tt_int_op(u16,==, 0);
tor_free(cp);
test_assert(addr_port_lookup(LOG_WARN, "localhost:3", &cp, &u32, NULL));
tt_assert(addr_port_lookup(LOG_WARN, "localhost:3", &cp, &u32, NULL));
tor_free(cp);
test_eq(0, addr_mask_get_bits(0x0u));
test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
test_eq(16, addr_mask_get_bits(0xFFFF0000u));
test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
test_eq(1, addr_mask_get_bits(0x80000000u));
tt_int_op(0,==, addr_mask_get_bits(0x0u));
tt_int_op(32,==, addr_mask_get_bits(0xFFFFFFFFu));
tt_int_op(16,==, addr_mask_get_bits(0xFFFF0000u));
tt_int_op(31,==, addr_mask_get_bits(0xFFFFFFFEu));
tt_int_op(1,==, addr_mask_get_bits(0x80000000u));
/* Test inet_ntop */
{
@ -61,15 +62,15 @@ test_addr_basic(void)
struct in_addr in;
/* good round trip */
test_eq(tor_inet_pton(AF_INET, ip, &in), 1);
test_eq_ptr(tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf)), &tmpbuf);
test_streq(tmpbuf, ip);
tt_int_op(tor_inet_pton(AF_INET, ip, &in),==, 1);
tt_ptr_op(tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf)),==, &tmpbuf);
tt_str_op(tmpbuf,==, ip);
/* just enough buffer length */
test_streq(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip) + 1), ip);
tt_str_op(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip) + 1),==, ip);
/* too short buffer */
test_eq_ptr(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip)), NULL);
tt_ptr_op(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip)),==, NULL);
}
done:
@ -96,67 +97,68 @@ test_addr_basic(void)
/** Helper: Assert that two strings both decode as IPv6 addresses with
* tor_inet_pton(), and both decode to the same address. */
#define test_pton6_same(a,b) STMT_BEGIN \
test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
test_op_ip6_(&a1,==,&a2,#a,#b); \
#define test_pton6_same(a,b) STMT_BEGIN \
tt_int_op(tor_inet_pton(AF_INET6, a, &a1), ==, 1); \
tt_int_op(tor_inet_pton(AF_INET6, b, &a2), ==, 1); \
test_op_ip6_(&a1,==,&a2,#a,#b); \
STMT_END
/** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
* tor_inet_pton(). */
#define test_pton6_bad(a) \
test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
tt_int_op(0, ==, tor_inet_pton(AF_INET6, a, &a1))
/** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
* with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
* the same value as <b>a</b>. */
#define test_ntop6_reduces(a,b) STMT_BEGIN \
test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
test_op_ip6_(&a1, ==, &a2, a, b); \
#define test_ntop6_reduces(a,b) STMT_BEGIN \
tt_int_op(tor_inet_pton(AF_INET6, a, &a1), ==, 1); \
tt_str_op(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), ==, b); \
tt_int_op(tor_inet_pton(AF_INET6, b, &a2), ==, 1); \
test_op_ip6_(&a1, ==, &a2, a, b); \
STMT_END
/** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
* passes tor_addr_is_internal() with <b>for_listening</b>. */
#define test_internal_ip(a,for_listening) STMT_BEGIN \
test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), ==, 1); \
t1.family = AF_INET6; \
if (!tor_addr_is_internal(&t1, for_listening)) \
test_fail_msg( a "was not internal."); \
TT_DIE(("%s was not internal", a)); \
STMT_END
/** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
* does not pass tor_addr_is_internal() with <b>for_listening</b>. */
#define test_external_ip(a,for_listening) STMT_BEGIN \
test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), ==, 1); \
t1.family = AF_INET6; \
if (tor_addr_is_internal(&t1, for_listening)) \
test_fail_msg(a "was not external."); \
TT_DIE(("%s was not internal", a)); \
STMT_END
/** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
* tor_inet_pton(), give addresses that compare in the order defined by
* <b>op</b> with tor_addr_compare(). */
#define test_addr_compare(a, op, b) STMT_BEGIN \
test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), ==, 1); \
tt_int_op(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), ==, 1); \
t1.family = t2.family = AF_INET6; \
r = tor_addr_compare(&t1,&t2,CMP_SEMANTIC); \
if (!(r op 0)) \
test_fail_msg("failed: tor_addr_compare("a","b") "#op" 0"); \
TT_DIE(("Failed: tor_addr_compare(%s,%s) %s 0", a, b, #op));\
STMT_END
/** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
* tor_inet_pton(), give addresses that compare in the order defined by
* <b>op</b> with tor_addr_compare_masked() with <b>m</b> masked. */
#define test_addr_compare_masked(a, op, b, m) STMT_BEGIN \
test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), ==, 1); \
tt_int_op(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), ==, 1); \
t1.family = t2.family = AF_INET6; \
r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC); \
if (!(r op 0)) \
test_fail_msg("failed: tor_addr_compare_masked("a","b","#m") "#op" 0"); \
TT_DIE(("Failed: tor_addr_compare_masked(%s,%s,%d) %s 0", \
a, b, m, #op)); \
STMT_END
/** Helper: assert that <b>xx</b> is parseable as a masked IPv6 address with
@ -165,21 +167,21 @@ test_addr_basic(void)
* as <b>pt1..pt2</b>. */
#define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
STMT_BEGIN \
test_eq(tor_addr_parse_mask_ports(xx, 0, &t1, &mask, &port1, &port2), \
f); \
tt_int_op(tor_addr_parse_mask_ports(xx, 0, &t1, &mask, &port1, &port2), \
==, f); \
p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
test_eq(htonl(ip1), tor_addr_to_in6_addr32(&t1)[0]); \
test_eq(htonl(ip2), tor_addr_to_in6_addr32(&t1)[1]); \
test_eq(htonl(ip3), tor_addr_to_in6_addr32(&t1)[2]); \
test_eq(htonl(ip4), tor_addr_to_in6_addr32(&t1)[3]); \
test_eq(mask, mm); \
test_eq(port1, pt1); \
test_eq(port2, pt2); \
tt_int_op(htonl(ip1), ==, tor_addr_to_in6_addr32(&t1)[0]); \
tt_int_op(htonl(ip2), ==, tor_addr_to_in6_addr32(&t1)[1]); \
tt_int_op(htonl(ip3), ==, tor_addr_to_in6_addr32(&t1)[2]); \
tt_int_op(htonl(ip4), ==, tor_addr_to_in6_addr32(&t1)[3]); \
tt_int_op(mask, ==, mm); \
tt_uint_op(port1, ==, pt1); \
tt_uint_op(port2, ==, pt2); \
STMT_END
/** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
static void
test_addr_ip6_helpers(void)
test_addr_ip6_helpers(void *arg)
{
char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
char rbuf[REVERSE_LOOKUP_NAME_BUF_LEN];
@ -194,28 +196,29 @@ test_addr_ip6_helpers(void)
struct sockaddr_in6 *sin6;
/* Test tor_inet_ntop and tor_inet_pton: IPv6 */
(void)arg;
{
const char *ip = "2001::1234";
const char *ip_ffff = "::ffff:192.168.1.2";
/* good round trip */
test_eq(tor_inet_pton(AF_INET6, ip, &a1), 1);
test_eq_ptr(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), &buf);
test_streq(buf, ip);
tt_int_op(tor_inet_pton(AF_INET6, ip, &a1),==, 1);
tt_ptr_op(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)),==, &buf);
tt_str_op(buf,==, ip);
/* good round trip - ::ffff:0:0 style */
test_eq(tor_inet_pton(AF_INET6, ip_ffff, &a2), 1);
test_eq_ptr(tor_inet_ntop(AF_INET6, &a2, buf, sizeof(buf)), &buf);
test_streq(buf, ip_ffff);
tt_int_op(tor_inet_pton(AF_INET6, ip_ffff, &a2),==, 1);
tt_ptr_op(tor_inet_ntop(AF_INET6, &a2, buf, sizeof(buf)),==, &buf);
tt_str_op(buf,==, ip_ffff);
/* just long enough buffer (remember \0) */
test_streq(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)+1), ip);
test_streq(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)+1),
tt_str_op(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)+1),==, ip);
tt_str_op(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)+1),==,
ip_ffff);
/* too short buffer (remember \0) */
test_eq_ptr(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)), NULL);
test_eq_ptr(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)), NULL);
tt_ptr_op(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)),==, NULL);
tt_ptr_op(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)),==, NULL);
}
/* ==== Converting to and from sockaddr_t. */
@ -224,16 +227,16 @@ test_addr_ip6_helpers(void)
sin->sin_port = htons(9090);
sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, &port1);
test_eq(tor_addr_family(&t1), AF_INET);
test_eq(tor_addr_to_ipv4h(&t1), 0x7f7f0102);
tt_int_op(tor_addr_family(&t1),==, AF_INET);
tt_int_op(tor_addr_to_ipv4h(&t1),==, 0x7f7f0102);
tt_int_op(port1, ==, 9090);
memset(&sa_storage, 0, sizeof(sa_storage));
test_eq(sizeof(struct sockaddr_in),
tt_int_op(sizeof(struct sockaddr_in),==,
tor_addr_to_sockaddr(&t1, 1234, (struct sockaddr *)&sa_storage,
sizeof(sa_storage)));
test_eq(1234, ntohs(sin->sin_port));
test_eq(0x7f7f0102, ntohl(sin->sin_addr.s_addr));
tt_int_op(1234,==, ntohs(sin->sin_port));
tt_int_op(0x7f7f0102,==, ntohl(sin->sin_addr.s_addr));
memset(&sa_storage, 0, sizeof(sa_storage));
sin6 = (struct sockaddr_in6 *)&sa_storage;
@ -241,37 +244,37 @@ test_addr_ip6_helpers(void)
sin6->sin6_port = htons(7070);
sin6->sin6_addr.s6_addr[0] = 128;
tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, &port1);
test_eq(tor_addr_family(&t1), AF_INET6);
tt_int_op(tor_addr_family(&t1),==, AF_INET6);
tt_int_op(port1, ==, 7070);
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
test_streq(p1, "8000::");
tt_str_op(p1,==, "8000::");
memset(&sa_storage, 0, sizeof(sa_storage));
test_eq(sizeof(struct sockaddr_in6),
tt_int_op(sizeof(struct sockaddr_in6),==,
tor_addr_to_sockaddr(&t1, 9999, (struct sockaddr *)&sa_storage,
sizeof(sa_storage)));
test_eq(AF_INET6, sin6->sin6_family);
test_eq(9999, ntohs(sin6->sin6_port));
test_eq(0x80000000, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
tt_int_op(AF_INET6,==, sin6->sin6_family);
tt_int_op(9999,==, ntohs(sin6->sin6_port));
tt_int_op(0x80000000,==, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
/* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
* have a good resolver. */
test_eq(0, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
test_eq(AF_INET, tor_addr_family(&t1));
test_eq(tor_addr_to_ipv4h(&t1), 0x7f808182);
tt_int_op(0,==, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
tt_int_op(AF_INET,==, tor_addr_family(&t1));
tt_int_op(tor_addr_to_ipv4h(&t1),==, 0x7f808182);
test_eq(0, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
test_eq(AF_INET6, tor_addr_family(&t1));
test_eq(0x90, tor_addr_to_in6_addr8(&t1)[0]);
test_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
test_eq(0x05, tor_addr_to_in6_addr8(&t1)[15]);
tt_int_op(0,==, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
tt_int_op(AF_INET6,==, tor_addr_family(&t1));
tt_int_op(0x90,==, tor_addr_to_in6_addr8(&t1)[0]);
tt_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
tt_int_op(0x05,==, tor_addr_to_in6_addr8(&t1)[15]);
/* === Test pton: valid af_inet6 */
/* Simple, valid parsing. */
r = tor_inet_pton(AF_INET6,
"0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
test_assert(r==1);
for (i=0;i<16;++i) { test_eq(i+1, (int)a1.s6_addr[i]); }
tt_int_op(r, ==, 1);
for (i=0;i<16;++i) { tt_int_op(i+1,==, (int)a1.s6_addr[i]); }
/* ipv4 ending. */
test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
"0102:0304:0506:0708:090A:0B0C:13.14.15.16");
@ -311,7 +314,7 @@ test_addr_ip6_helpers(void)
"1000:1:0:7::");
/* Bad af param */
test_eq(tor_inet_pton(AF_UNSPEC, 0, 0), -1);
tt_int_op(tor_inet_pton(AF_UNSPEC, 0, 0),==, -1);
/* === Test pton: invalid in6. */
test_pton6_bad("foobar.");
@ -414,10 +417,10 @@ test_addr_ip6_helpers(void)
test_addr_compare("0::ffff:5.2.2.1", <, "::ffff:6.0.0.0"); /* XXXX wrong. */
tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", 0, &t1, NULL, NULL, NULL);
tor_addr_parse_mask_ports("2.3.4.5", 0, &t2, NULL, NULL, NULL);
test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) == 0);
tt_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) == 0);
tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", 0, &t1, NULL, NULL, NULL);
tor_addr_parse_mask_ports("2.3.4.5", 0, &t2, NULL, NULL, NULL);
test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) < 0);
tt_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) < 0);
/* test compare_masked */
test_addr_compare_masked("ffff::", ==, "ffff::0", 128);
@ -426,113 +429,113 @@ test_addr_ip6_helpers(void)
test_addr_compare_masked("0::2:2:1", ==, "0::8000:2:1", 80);
/* Test undecorated tor_addr_to_str */
test_eq(AF_INET6, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
tt_int_op(AF_INET6,==, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
test_streq(p1, "123:45:6789::5005:11");
test_eq(AF_INET, tor_addr_parse(&t1, "18.0.0.1"));
tt_str_op(p1,==, "123:45:6789::5005:11");
tt_int_op(AF_INET,==, tor_addr_parse(&t1, "18.0.0.1"));
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
test_streq(p1, "18.0.0.1");
tt_str_op(p1,==, "18.0.0.1");
/* Test decorated tor_addr_to_str */
test_eq(AF_INET6, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
tt_int_op(AF_INET6,==, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
test_streq(p1, "[123:45:6789::5005:11]");
test_eq(AF_INET, tor_addr_parse(&t1, "18.0.0.1"));
tt_str_op(p1,==, "[123:45:6789::5005:11]");
tt_int_op(AF_INET,==, tor_addr_parse(&t1, "18.0.0.1"));
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
test_streq(p1, "18.0.0.1");
tt_str_op(p1,==, "18.0.0.1");
/* Test buffer bounds checking of tor_addr_to_str */
test_eq(AF_INET6, tor_addr_parse(&t1, "::")); /* 2 + \0 */
test_eq_ptr(tor_addr_to_str(buf, &t1, 2, 0), NULL); /* too short buf */
test_streq(tor_addr_to_str(buf, &t1, 3, 0), "::");
test_eq_ptr(tor_addr_to_str(buf, &t1, 4, 1), NULL); /* too short buf */
test_streq(tor_addr_to_str(buf, &t1, 5, 1), "[::]");
tt_int_op(AF_INET6,==, tor_addr_parse(&t1, "::")); /* 2 + \0 */
tt_ptr_op(tor_addr_to_str(buf, &t1, 2, 0),==, NULL); /* too short buf */
tt_str_op(tor_addr_to_str(buf, &t1, 3, 0),==, "::");
tt_ptr_op(tor_addr_to_str(buf, &t1, 4, 1),==, NULL); /* too short buf */
tt_str_op(tor_addr_to_str(buf, &t1, 5, 1),==, "[::]");
test_eq(AF_INET6, tor_addr_parse(&t1, "2000::1337")); /* 10 + \0 */
test_eq_ptr(tor_addr_to_str(buf, &t1, 10, 0), NULL); /* too short buf */
test_streq(tor_addr_to_str(buf, &t1, 11, 0), "2000::1337");
test_eq_ptr(tor_addr_to_str(buf, &t1, 12, 1), NULL); /* too short buf */
test_streq(tor_addr_to_str(buf, &t1, 13, 1), "[2000::1337]");
tt_int_op(AF_INET6,==, tor_addr_parse(&t1, "2000::1337")); /* 10 + \0 */
tt_ptr_op(tor_addr_to_str(buf, &t1, 10, 0),==, NULL); /* too short buf */
tt_str_op(tor_addr_to_str(buf, &t1, 11, 0),==, "2000::1337");
tt_ptr_op(tor_addr_to_str(buf, &t1, 12, 1),==, NULL); /* too short buf */
tt_str_op(tor_addr_to_str(buf, &t1, 13, 1),==, "[2000::1337]");
test_eq(AF_INET, tor_addr_parse(&t1, "1.2.3.4")); /* 7 + \0 */
test_eq_ptr(tor_addr_to_str(buf, &t1, 7, 0), NULL); /* too short buf */
test_streq(tor_addr_to_str(buf, &t1, 8, 0), "1.2.3.4");
tt_int_op(AF_INET,==, tor_addr_parse(&t1, "1.2.3.4")); /* 7 + \0 */
tt_ptr_op(tor_addr_to_str(buf, &t1, 7, 0),==, NULL); /* too short buf */
tt_str_op(tor_addr_to_str(buf, &t1, 8, 0),==, "1.2.3.4");
test_eq(AF_INET, tor_addr_parse(&t1, "255.255.255.255")); /* 15 + \0 */
test_eq_ptr(tor_addr_to_str(buf, &t1, 15, 0), NULL); /* too short buf */
test_streq(tor_addr_to_str(buf, &t1, 16, 0), "255.255.255.255");
test_eq_ptr(tor_addr_to_str(buf, &t1, 15, 1), NULL); /* too short buf */
test_streq(tor_addr_to_str(buf, &t1, 16, 1), "255.255.255.255");
tt_int_op(AF_INET,==, tor_addr_parse(&t1, "255.255.255.255")); /* 15 + \0 */
tt_ptr_op(tor_addr_to_str(buf, &t1, 15, 0),==, NULL); /* too short buf */
tt_str_op(tor_addr_to_str(buf, &t1, 16, 0),==, "255.255.255.255");
tt_ptr_op(tor_addr_to_str(buf, &t1, 15, 1),==, NULL); /* too short buf */
tt_str_op(tor_addr_to_str(buf, &t1, 16, 1),==, "255.255.255.255");
t1.family = AF_UNSPEC;
test_eq_ptr(tor_addr_to_str(buf, &t1, sizeof(buf), 0), NULL);
tt_ptr_op(tor_addr_to_str(buf, &t1, sizeof(buf), 0),==, NULL);
/* Test tor_addr_parse_PTR_name */
i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
test_eq(0, i);
tt_int_op(0,==, i);
i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 1);
test_eq(0, i);
tt_int_op(0,==, i);
i = tor_addr_parse_PTR_name(&t1, "9999999999999999999999999999.in-addr.arpa",
AF_UNSPEC, 1);
test_eq(-1, i);
tt_int_op(-1,==, i);
i = tor_addr_parse_PTR_name(&t1, "1.0.168.192.in-addr.arpa",
AF_UNSPEC, 1);
test_eq(1, i);
test_eq(tor_addr_family(&t1), AF_INET);
tt_int_op(1,==, i);
tt_int_op(tor_addr_family(&t1),==, AF_INET);
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
test_streq(p1, "192.168.0.1");
tt_str_op(p1,==, "192.168.0.1");
i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 0);
test_eq(0, i);
tt_int_op(0,==, i);
i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 1);
test_eq(1, i);
tt_int_op(1,==, i);
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
test_streq(p1, "192.168.0.99");
tt_str_op(p1,==, "192.168.0.99");
memset(&t1, 0, sizeof(t1));
i = tor_addr_parse_PTR_name(&t1,
"0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f."
"f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
"ip6.ARPA",
AF_UNSPEC, 0);
test_eq(1, i);
tt_int_op(1,==, i);
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
test_streq(p1, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
tt_str_op(p1,==, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
/* Failing cases. */
i = tor_addr_parse_PTR_name(&t1,
"6.7.8.9.a.b.c.d.e.f."
"f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
"ip6.ARPA",
AF_UNSPEC, 0);
test_eq(i, -1);
tt_int_op(i,==, -1);
i = tor_addr_parse_PTR_name(&t1,
"6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.f.0."
"f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
"ip6.ARPA",
AF_UNSPEC, 0);
test_eq(i, -1);
tt_int_op(i,==, -1);
i = tor_addr_parse_PTR_name(&t1,
"6.7.8.9.a.b.c.d.e.f.X.0.0.0.0.9."
"f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
"ip6.ARPA",
AF_UNSPEC, 0);
test_eq(i, -1);
tt_int_op(i,==, -1);
i = tor_addr_parse_PTR_name(&t1, "32.1.1.in-addr.arpa",
AF_UNSPEC, 0);
test_eq(i, -1);
tt_int_op(i,==, -1);
i = tor_addr_parse_PTR_name(&t1, ".in-addr.arpa",
AF_UNSPEC, 0);
test_eq(i, -1);
tt_int_op(i,==, -1);
i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
AF_UNSPEC, 0);
test_eq(i, -1);
tt_int_op(i,==, -1);
i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
AF_INET6, 0);
test_eq(i, -1);
tt_int_op(i,==, -1);
i = tor_addr_parse_PTR_name(&t1,
"6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.0."
"f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
"ip6.ARPA",
AF_INET, 0);
test_eq(i, -1);
tt_int_op(i,==, -1);
/* === Test tor_addr_to_PTR_name */
@ -544,19 +547,19 @@ test_addr_ip6_helpers(void)
tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
/* Check IPv4 PTR - too short buffer */
test_eq(tor_addr_to_PTR_name(rbuf, 1, &t1), -1);
test_eq(tor_addr_to_PTR_name(rbuf,
tt_int_op(tor_addr_to_PTR_name(rbuf, 1, &t1),==, -1);
tt_int_op(tor_addr_to_PTR_name(rbuf,
strlen("3.2.1.127.in-addr.arpa") - 1,
&t1), -1);
&t1),==, -1);
/* Check IPv4 PTR - valid addr */
test_eq(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),
tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),==,
strlen("3.2.1.127.in-addr.arpa"));
test_streq(rbuf, "3.2.1.127.in-addr.arpa");
tt_str_op(rbuf,==, "3.2.1.127.in-addr.arpa");
/* Invalid addr family */
t1.family = AF_UNSPEC;
test_eq(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1), -1);
tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),==, -1);
/* Stage IPv6 addr */
memset(&sa_storage, 0, sizeof(sa_storage));
@ -573,114 +576,114 @@ test_addr_ip6_helpers(void)
"0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.ip6.arpa";
/* Check IPv6 PTR - too short buffer */
test_eq(tor_addr_to_PTR_name(rbuf, 0, &t1), -1);
test_eq(tor_addr_to_PTR_name(rbuf, strlen(addr_PTR) - 1, &t1), -1);
tt_int_op(tor_addr_to_PTR_name(rbuf, 0, &t1),==, -1);
tt_int_op(tor_addr_to_PTR_name(rbuf, strlen(addr_PTR) - 1, &t1),==, -1);
/* Check IPv6 PTR - valid addr */
test_eq(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),
tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),==,
strlen(addr_PTR));
test_streq(rbuf, addr_PTR);
tt_str_op(rbuf,==, addr_PTR);
}
/* XXXX turn this into a separate function; it's not all IPv6. */
/* test tor_addr_parse_mask_ports */
test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6,
0, 0, 0, 0x0000000f, 17, 47, 95);
test_streq(p1, "::f");
tt_str_op(p1,==, "::f");
//test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
//test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6,
0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
test_streq(p1, "::ffff:4.1.1.7");
tt_str_op(p1,==, "::ffff:4.1.1.7");
test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6,
0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
test_streq(p1, "abcd:2::44a:0");
tt_str_op(p1,==, "abcd:2::44a:0");
/* Try some long addresses. */
r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111]",
0, &t1, NULL, NULL, NULL);
test_assert(r == AF_INET6);
tt_assert(r == AF_INET6);
r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:11111]",
0, &t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111:1]",
0, &t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports(
"[ffff:1111:1111:1111:1111:1111:1111:ffff:"
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:"
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:"
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]",
0, &t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
/* Try some failing cases. */
r=tor_addr_parse_mask_ports("[fefef::]/112", 0, &t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("[fefe::/112", 0, &t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("[fefe::", 0, &t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("[fefe::X]", 0, &t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("efef::/112", 0, &t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]",0,&t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]",0,&t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]",0,&t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("[f:f:f:f:f::]/fred",0,&t1,&mask, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("[f:f:f:f:f::]/255.255.0.0",
0,&t1, NULL, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
/* This one will get rejected because it isn't a pure prefix. */
r=tor_addr_parse_mask_ports("1.1.2.3/255.255.64.0",0,&t1, &mask,NULL,NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
/* Test for V4-mapped address with mask < 96. (arguably not valid) */
r=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]",0,&t1, &mask, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("1.1.2.2/33",0,&t1, &mask, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
/* Try extended wildcard addresses with out TAPMP_EXTENDED_STAR*/
r=tor_addr_parse_mask_ports("*4",0,&t1, &mask, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r=tor_addr_parse_mask_ports("*6",0,&t1, &mask, NULL, NULL);
test_assert(r == -1);
tt_int_op(r, ==, -1);
#if 0
/* Try a mask with a wildcard. */
r=tor_addr_parse_mask_ports("*/16",0,&t1, &mask, NULL, NULL);
test_assert(r == -1);
tt_assert(r == -1);
r=tor_addr_parse_mask_ports("*4/16",TAPMP_EXTENDED_STAR,
&t1, &mask, NULL, NULL);
test_assert(r == -1);
tt_assert(r == -1);
r=tor_addr_parse_mask_ports("*6/30",TAPMP_EXTENDED_STAR,
&t1, &mask, NULL, NULL);
test_assert(r == -1);
tt_assert(r == -1);
#endif
/* Basic mask tests*/
r=tor_addr_parse_mask_ports("1.1.2.2/31",0,&t1, &mask, NULL, NULL);
test_assert(r == AF_INET);
tt_assert(r == AF_INET);
tt_int_op(mask,==,31);
tt_int_op(tor_addr_family(&t1),==,AF_INET);
tt_int_op(tor_addr_to_ipv4h(&t1),==,0x01010202);
r=tor_addr_parse_mask_ports("3.4.16.032:1-2",0,&t1, &mask, &port1, &port2);
test_assert(r == AF_INET);
tt_assert(r == AF_INET);
tt_int_op(mask,==,32);
tt_int_op(tor_addr_family(&t1),==,AF_INET);
tt_int_op(tor_addr_to_ipv4h(&t1),==,0x03041020);
test_assert(port1 == 1);
test_assert(port2 == 2);
tt_assert(port1 == 1);
tt_assert(port2 == 2);
r=tor_addr_parse_mask_ports("1.1.2.3/255.255.128.0",0,&t1, &mask,NULL,NULL);
test_assert(r == AF_INET);
tt_assert(r == AF_INET);
tt_int_op(mask,==,17);
tt_int_op(tor_addr_family(&t1),==,AF_INET);
tt_int_op(tor_addr_to_ipv4h(&t1),==,0x01010203);
r=tor_addr_parse_mask_ports("[efef::]/112",0,&t1, &mask, &port1, &port2);
test_assert(r == AF_INET6);
test_assert(port1 == 1);
test_assert(port2 == 65535);
tt_assert(r == AF_INET6);
tt_assert(port1 == 1);
tt_assert(port2 == 65535);
/* Try regular wildcard behavior without TAPMP_EXTENDED_STAR */
r=tor_addr_parse_mask_ports("*:80-443",0,&t1,&mask,&port1,&port2);
tt_int_op(r,==,AF_INET); /* Old users of this always get inet */
@ -715,11 +718,11 @@ test_addr_ip6_helpers(void)
tt_int_op(port2,==,65535);
/* make sure inet address lengths >= max */
test_assert(INET_NTOA_BUF_LEN >= sizeof("255.255.255.255"));
test_assert(TOR_ADDR_BUF_LEN >=
tt_assert(INET_NTOA_BUF_LEN >= sizeof("255.255.255.255"));
tt_assert(TOR_ADDR_BUF_LEN >=
sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
test_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
tt_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
/* get interface addresses */
r = get_interface_address6(LOG_DEBUG, AF_INET, &t1);
@ -736,7 +739,7 @@ test_addr_ip6_helpers(void)
/** Test tor_addr_port_parse(). */
static void
test_addr_parse(void)
test_addr_parse(void *arg)
{
int r;
tor_addr_t addr;
@ -744,89 +747,90 @@ test_addr_parse(void)
uint16_t port = 0;
/* Correct call. */
(void)arg;
r= tor_addr_port_parse(LOG_DEBUG,
"192.0.2.1:1234",
&addr, &port, -1);
test_assert(r == 0);
tt_int_op(r, ==, 0);
tor_addr_to_str(buf, &addr, sizeof(buf), 0);
test_streq(buf, "192.0.2.1");
test_eq(port, 1234);
tt_str_op(buf,==, "192.0.2.1");
tt_int_op(port,==, 1234);
r= tor_addr_port_parse(LOG_DEBUG,
"[::1]:1234",
&addr, &port, -1);
test_assert(r == 0);
tt_int_op(r, ==, 0);
tor_addr_to_str(buf, &addr, sizeof(buf), 0);
test_streq(buf, "::1");
test_eq(port, 1234);
tt_str_op(buf,==, "::1");
tt_int_op(port,==, 1234);
/* Domain name. */
r= tor_addr_port_parse(LOG_DEBUG,
"torproject.org:1234",
&addr, &port, -1);
test_assert(r == -1);
tt_int_op(r, ==, -1);
/* Only IP. */
r= tor_addr_port_parse(LOG_DEBUG,
"192.0.2.2",
&addr, &port, -1);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r= tor_addr_port_parse(LOG_DEBUG,
"192.0.2.2",
&addr, &port, 200);
test_assert(r == 0);
tt_int_op(r, ==, 0);
tt_int_op(port,==,200);
r= tor_addr_port_parse(LOG_DEBUG,
"[::1]",
&addr, &port, -1);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r= tor_addr_port_parse(LOG_DEBUG,
"[::1]",
&addr, &port, 400);
test_assert(r == 0);
tt_int_op(r, ==, 0);
tt_int_op(port,==,400);
/* Bad port. */
r= tor_addr_port_parse(LOG_DEBUG,
"192.0.2.2:66666",
&addr, &port, -1);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r= tor_addr_port_parse(LOG_DEBUG,
"192.0.2.2:66666",
&addr, &port, 200);
test_assert(r == -1);
tt_int_op(r, ==, -1);
/* Only domain name */
r= tor_addr_port_parse(LOG_DEBUG,
"torproject.org",
&addr, &port, -1);
test_assert(r == -1);
tt_int_op(r, ==, -1);
r= tor_addr_port_parse(LOG_DEBUG,
"torproject.org",
&addr, &port, 200);
test_assert(r == -1);
tt_int_op(r, ==, -1);
/* Bad IP address */
r= tor_addr_port_parse(LOG_DEBUG,
"192.0.2:1234",
&addr, &port, -1);
test_assert(r == -1);
tt_int_op(r, ==, -1);
/* Make sure that the default port has lower priority than the real
one */
r= tor_addr_port_parse(LOG_DEBUG,
"192.0.2.2:1337",
&addr, &port, 200);
test_assert(r == 0);
tt_int_op(r, ==, 0);
tt_int_op(port,==,1337);
r= tor_addr_port_parse(LOG_DEBUG,
"[::1]:1369",
&addr, &port, 200);
test_assert(r == 0);
tt_int_op(r, ==, 0);
tt_int_op(port,==,1369);
done:
@ -1047,7 +1051,7 @@ test_addr_make_null(void *data)
}
#define ADDR_LEGACY(name) \
{ #name, legacy_test_helper, 0, &legacy_setup, test_addr_ ## name }
{ #name, test_addr_ ## name , 0, NULL, NULL }
struct testcase_t addr_tests[] = {
ADDR_LEGACY(basic),

View File

@ -27,10 +27,10 @@ test_buffers_basic(void *arg)
* buf_new
****/
if (!(buf = buf_new()))
test_fail();
TT_DIE(("Assertion failed."));
//test_eq(buf_capacity(buf), 4096);
test_eq(buf_datalen(buf), 0);
tt_int_op(buf_datalen(buf),==, 0);
/****
* General pointer frobbing
@ -40,16 +40,16 @@ test_buffers_basic(void *arg)
}
write_to_buf(str, 256, buf);
write_to_buf(str, 256, buf);
test_eq(buf_datalen(buf), 512);
tt_int_op(buf_datalen(buf),==, 512);
fetch_from_buf(str2, 200, buf);
test_memeq(str, str2, 200);
test_eq(buf_datalen(buf), 312);
tt_mem_op(str,==, str2, 200);
tt_int_op(buf_datalen(buf),==, 312);
memset(str2, 0, sizeof(str2));
fetch_from_buf(str2, 256, buf);
test_memeq(str+200, str2, 56);
test_memeq(str, str2+56, 200);
test_eq(buf_datalen(buf), 56);
tt_mem_op(str+200,==, str2, 56);
tt_mem_op(str,==, str2+56, 200);
tt_int_op(buf_datalen(buf),==, 56);
memset(str2, 0, sizeof(str2));
/* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
* another 3584 bytes, we hit the end. */
@ -57,16 +57,16 @@ test_buffers_basic(void *arg)
write_to_buf(str, 256, buf);
}
assert_buf_ok(buf);
test_eq(buf_datalen(buf), 3896);
tt_int_op(buf_datalen(buf),==, 3896);
fetch_from_buf(str2, 56, buf);
test_eq(buf_datalen(buf), 3840);
test_memeq(str+200, str2, 56);
tt_int_op(buf_datalen(buf),==, 3840);
tt_mem_op(str+200,==, str2, 56);
for (j=0;j<15;++j) {
memset(str2, 0, sizeof(str2));
fetch_from_buf(str2, 256, buf);
test_memeq(str, str2, 256);
tt_mem_op(str,==, str2, 256);
}
test_eq(buf_datalen(buf), 0);
tt_int_op(buf_datalen(buf),==, 0);
buf_free(buf);
buf = NULL;
@ -76,7 +76,7 @@ test_buffers_basic(void *arg)
write_to_buf(str+1, 255, buf);
//test_eq(buf_capacity(buf), 256);
fetch_from_buf(str2, 254, buf);
test_memeq(str+1, str2, 254);
tt_mem_op(str+1,==, str2, 254);
//test_eq(buf_capacity(buf), 256);
assert_buf_ok(buf);
write_to_buf(str, 32, buf);
@ -85,15 +85,15 @@ test_buffers_basic(void *arg)
write_to_buf(str, 256, buf);
assert_buf_ok(buf);
//test_eq(buf_capacity(buf), 512);
test_eq(buf_datalen(buf), 33+256);
tt_int_op(buf_datalen(buf),==, 33+256);
fetch_from_buf(str2, 33, buf);
test_eq(*str2, str[255]);
tt_int_op(*str2,==, str[255]);
test_memeq(str2+1, str, 32);
tt_mem_op(str2+1,==, str, 32);
//test_eq(buf_capacity(buf), 512);
test_eq(buf_datalen(buf), 256);
tt_int_op(buf_datalen(buf),==, 256);
fetch_from_buf(str2, 256, buf);
test_memeq(str, str2, 256);
tt_mem_op(str,==, str2, 256);
/* now try shrinking: case 1. */
buf_free(buf);
@ -102,10 +102,10 @@ test_buffers_basic(void *arg)
write_to_buf(str,255, buf);
}
//test_eq(buf_capacity(buf), 33668);
test_eq(buf_datalen(buf), 17085);
tt_int_op(buf_datalen(buf),==, 17085);
for (j=0; j < 40; ++j) {
fetch_from_buf(str2, 255,buf);
test_memeq(str2, str, 255);
tt_mem_op(str2,==, str, 255);
}
/* now try shrinking: case 2. */
@ -116,7 +116,7 @@ test_buffers_basic(void *arg)
}
for (j=0; j < 20; ++j) {
fetch_from_buf(str2, 255,buf);
test_memeq(str2, str, 255);
tt_mem_op(str2,==, str, 255);
}
for (j=0;j<80;++j) {
write_to_buf(str,255, buf);
@ -124,7 +124,7 @@ test_buffers_basic(void *arg)
//test_eq(buf_capacity(buf),33668);
for (j=0; j < 120; ++j) {
fetch_from_buf(str2, 255,buf);
test_memeq(str2, str, 255);
tt_mem_op(str2,==, str, 255);
}
/* Move from buf to buf. */
@ -133,27 +133,27 @@ test_buffers_basic(void *arg)
buf2 = buf_new_with_capacity(4096);
for (j=0;j<100;++j)
write_to_buf(str, 255, buf);
test_eq(buf_datalen(buf), 25500);
tt_int_op(buf_datalen(buf),==, 25500);
for (j=0;j<100;++j) {
r = 10;
move_buf_to_buf(buf2, buf, &r);
test_eq(r, 0);
tt_int_op(r,==, 0);
}
test_eq(buf_datalen(buf), 24500);
test_eq(buf_datalen(buf2), 1000);
tt_int_op(buf_datalen(buf),==, 24500);
tt_int_op(buf_datalen(buf2),==, 1000);
for (j=0;j<3;++j) {
fetch_from_buf(str2, 255, buf2);
test_memeq(str2, str, 255);
tt_mem_op(str2,==, str, 255);
}
r = 8192; /*big move*/
move_buf_to_buf(buf2, buf, &r);
test_eq(r, 0);
tt_int_op(r,==, 0);
r = 30000; /* incomplete move */
move_buf_to_buf(buf2, buf, &r);
test_eq(r, 13692);
tt_int_op(r,==, 13692);
for (j=0;j<97;++j) {
fetch_from_buf(str2, 255, buf2);
test_memeq(str2, str, 255);
tt_mem_op(str2,==, str, 255);
}
buf_free(buf);
buf_free(buf2);
@ -163,16 +163,16 @@ test_buffers_basic(void *arg)
cp = "Testing. This is a moderately long Testing string.";
for (j = 0; cp[j]; j++)
write_to_buf(cp+j, 1, buf);
test_eq(0, buf_find_string_offset(buf, "Testing", 7));
test_eq(1, buf_find_string_offset(buf, "esting", 6));
test_eq(1, buf_find_string_offset(buf, "est", 3));
test_eq(39, buf_find_string_offset(buf, "ing str", 7));
test_eq(35, buf_find_string_offset(buf, "Testing str", 11));
test_eq(32, buf_find_string_offset(buf, "ng ", 3));
test_eq(43, buf_find_string_offset(buf, "string.", 7));
test_eq(-1, buf_find_string_offset(buf, "shrdlu", 6));
test_eq(-1, buf_find_string_offset(buf, "Testing thing", 13));
test_eq(-1, buf_find_string_offset(buf, "ngx", 3));
tt_int_op(0,==, buf_find_string_offset(buf, "Testing", 7));
tt_int_op(1,==, buf_find_string_offset(buf, "esting", 6));
tt_int_op(1,==, buf_find_string_offset(buf, "est", 3));
tt_int_op(39,==, buf_find_string_offset(buf, "ing str", 7));
tt_int_op(35,==, buf_find_string_offset(buf, "Testing str", 11));
tt_int_op(32,==, buf_find_string_offset(buf, "ng ", 3));
tt_int_op(43,==, buf_find_string_offset(buf, "string.", 7));
tt_int_op(-1,==, buf_find_string_offset(buf, "shrdlu", 6));
tt_int_op(-1,==, buf_find_string_offset(buf, "Testing thing", 13));
tt_int_op(-1,==, buf_find_string_offset(buf, "ngx", 3));
buf_free(buf);
buf = NULL;
@ -240,16 +240,16 @@ test_buffer_pullup(void *arg)
/* Make room for 3000 bytes in the first chunk, so that the pullup-move code
* can get tested. */
tt_int_op(fetch_from_buf(tmp, 3000, buf), ==, 3000);
test_memeq(tmp, stuff, 3000);
tt_mem_op(tmp,==, stuff, 3000);
buf_pullup(buf, 2048, 0);
assert_buf_ok(buf);
buf_get_first_chunk_data(buf, &cp, &sz);
tt_ptr_op(cp, !=, NULL);
tt_int_op(sz, >=, 2048);
test_memeq(cp, stuff+3000, 2048);
tt_mem_op(cp,==, stuff+3000, 2048);
tt_int_op(3000, ==, buf_datalen(buf));
tt_int_op(fetch_from_buf(tmp, 3000, buf), ==, 0);
test_memeq(tmp, stuff+3000, 2048);
tt_mem_op(tmp,==, stuff+3000, 2048);
buf_free(buf);
@ -269,16 +269,16 @@ test_buffer_pullup(void *arg)
buf_get_first_chunk_data(buf, &cp, &sz);
tt_ptr_op(cp, !=, NULL);
tt_int_op(sz, >=, 12500);
test_memeq(cp, stuff, 12500);
tt_mem_op(cp,==, stuff, 12500);
tt_int_op(buf_datalen(buf), ==, 16000);
fetch_from_buf(tmp, 12400, buf);
test_memeq(tmp, stuff, 12400);
tt_mem_op(tmp,==, stuff, 12400);
tt_int_op(buf_datalen(buf), ==, 3600);
fetch_from_buf(tmp, 3500, buf);
test_memeq(tmp, stuff+12400, 3500);
tt_mem_op(tmp,==, stuff+12400, 3500);
fetch_from_buf(tmp, 100, buf);
test_memeq(tmp, stuff+15900, 10);
tt_mem_op(tmp,==, stuff+15900, 10);
buf_free(buf);
@ -292,7 +292,7 @@ test_buffer_pullup(void *arg)
buf_get_first_chunk_data(buf, &cp, &sz);
tt_ptr_op(cp, !=, NULL);
tt_int_op(sz, ==, 7900);
test_memeq(cp, stuff+100, 7900);
tt_mem_op(cp,==, stuff+100, 7900);
buf_free(buf);
buf = NULL;
@ -335,14 +335,14 @@ test_buffer_copy(void *arg)
tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
tt_int_op(len, ==, generic_buffer_len(buf2));
generic_buffer_get(buf2, b, len);
test_mem_op(b, ==, s, len);
tt_mem_op(b, ==, s, len);
/* Now free buf2 and retry so we can test allocating */
generic_buffer_free(buf2);
buf2 = NULL;
tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
tt_int_op(len, ==, generic_buffer_len(buf2));
generic_buffer_get(buf2, b, len);
test_mem_op(b, ==, s, len);
tt_mem_op(b, ==, s, len);
/* Clear buf for next test */
generic_buffer_get(buf, b, len);
tt_int_op(generic_buffer_len(buf),==,0);
@ -362,7 +362,7 @@ test_buffer_copy(void *arg)
for (i = 0; i < 256; ++i) {
generic_buffer_get(buf2, b, len+1);
tt_int_op((unsigned char)b[0],==,i);
test_mem_op(b+1, ==, s, len);
tt_mem_op(b+1, ==, s, len);
}
done:
@ -410,7 +410,7 @@ test_buffer_ext_or_cmd(void *arg)
tt_ptr_op(NULL, !=, cmd);
tt_int_op(0x1021, ==, cmd->cmd);
tt_int_op(6, ==, cmd->len);
test_mem_op("abcdef", ==, cmd->body, 6);
tt_mem_op("abcdef", ==, cmd->body, 6);
tt_int_op(0, ==, generic_buffer_len(buf));
ext_or_cmd_free(cmd);
cmd = NULL;
@ -422,7 +422,7 @@ test_buffer_ext_or_cmd(void *arg)
tt_ptr_op(NULL, !=, cmd);
tt_int_op(0xffff, ==, cmd->cmd);
tt_int_op(10, ==, cmd->len);
test_mem_op("loremipsum", ==, cmd->body, 10);
tt_mem_op("loremipsum", ==, cmd->body, 10);
tt_int_op(4, ==, generic_buffer_len(buf));
ext_or_cmd_free(cmd);
cmd = NULL;
@ -436,7 +436,7 @@ test_buffer_ext_or_cmd(void *arg)
tt_ptr_op(NULL, !=, cmd);
tt_int_op(0x1000, ==, cmd->cmd);
tt_int_op(0xffff, ==, cmd->len);
test_mem_op(tmp, ==, cmd->body, 65535);
tt_mem_op(tmp, ==, cmd->body, 65535);
tt_int_op(0, ==, generic_buffer_len(buf));
ext_or_cmd_free(cmd);
cmd = NULL;

View File

@ -35,11 +35,11 @@ test_cfmt_relay_header(void *arg)
tt_int_op(rh.command, ==, 3);
tt_int_op(rh.recognized, ==, 0);
tt_int_op(rh.stream_id, ==, 0x2122);
test_mem_op(rh.integrity, ==, "ABCD", 4);
tt_mem_op(rh.integrity, ==, "ABCD", 4);
tt_int_op(rh.length, ==, 0x103);
relay_header_pack(hdr_out, &rh);
test_mem_op(hdr_out, ==, hdr_1, RELAY_HEADER_SIZE);
tt_mem_op(hdr_out, ==, hdr_1, RELAY_HEADER_SIZE);
done:
;
@ -402,10 +402,10 @@ test_cfmt_create_cells(void *arg)
tt_int_op(CELL_CREATE, ==, cc.cell_type);
tt_int_op(ONION_HANDSHAKE_TYPE_TAP, ==, cc.handshake_type);
tt_int_op(TAP_ONIONSKIN_CHALLENGE_LEN, ==, cc.handshake_len);
test_memeq(cc.onionskin, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
tt_mem_op(cc.onionskin,==, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
tt_int_op(0, ==, create_cell_format(&cell2, &cc));
tt_int_op(cell.command, ==, cell2.command);
test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
/* A valid create_fast cell. */
memset(&cell, 0, sizeof(cell));
@ -417,10 +417,10 @@ test_cfmt_create_cells(void *arg)
tt_int_op(CELL_CREATE_FAST, ==, cc.cell_type);
tt_int_op(ONION_HANDSHAKE_TYPE_FAST, ==, cc.handshake_type);
tt_int_op(CREATE_FAST_LEN, ==, cc.handshake_len);
test_memeq(cc.onionskin, b, CREATE_FAST_LEN + 10);
tt_mem_op(cc.onionskin,==, b, CREATE_FAST_LEN + 10);
tt_int_op(0, ==, create_cell_format(&cell2, &cc));
tt_int_op(cell.command, ==, cell2.command);
test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
/* A valid create2 cell with a TAP payload */
memset(&cell, 0, sizeof(cell));
@ -433,10 +433,10 @@ test_cfmt_create_cells(void *arg)
tt_int_op(CELL_CREATE2, ==, cc.cell_type);
tt_int_op(ONION_HANDSHAKE_TYPE_TAP, ==, cc.handshake_type);
tt_int_op(TAP_ONIONSKIN_CHALLENGE_LEN, ==, cc.handshake_len);
test_memeq(cc.onionskin, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
tt_mem_op(cc.onionskin,==, b, TAP_ONIONSKIN_CHALLENGE_LEN + 10);
tt_int_op(0, ==, create_cell_format(&cell2, &cc));
tt_int_op(cell.command, ==, cell2.command);
test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
/* A valid create2 cell with an ntor payload */
memset(&cell, 0, sizeof(cell));
@ -450,10 +450,10 @@ test_cfmt_create_cells(void *arg)
tt_int_op(CELL_CREATE2, ==, cc.cell_type);
tt_int_op(ONION_HANDSHAKE_TYPE_NTOR, ==, cc.handshake_type);
tt_int_op(NTOR_ONIONSKIN_LEN, ==, cc.handshake_len);
test_memeq(cc.onionskin, b, NTOR_ONIONSKIN_LEN + 10);
tt_mem_op(cc.onionskin,==, b, NTOR_ONIONSKIN_LEN + 10);
tt_int_op(0, ==, create_cell_format(&cell2, &cc));
tt_int_op(cell.command, ==, cell2.command);
test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
#else
tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
#endif
@ -470,10 +470,10 @@ test_cfmt_create_cells(void *arg)
tt_int_op(CELL_CREATE, ==, cc.cell_type);
tt_int_op(ONION_HANDSHAKE_TYPE_NTOR, ==, cc.handshake_type);
tt_int_op(NTOR_ONIONSKIN_LEN, ==, cc.handshake_len);
test_memeq(cc.onionskin, b, NTOR_ONIONSKIN_LEN + 10);
tt_mem_op(cc.onionskin,==, b, NTOR_ONIONSKIN_LEN + 10);
tt_int_op(0, ==, create_cell_format(&cell2, &cc));
tt_int_op(cell.command, ==, cell2.command);
test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
#else
tt_int_op(-1, ==, create_cell_parse(&cc, &cell));
#endif
@ -527,10 +527,10 @@ test_cfmt_created_cells(void *arg)
tt_int_op(0, ==, created_cell_parse(&cc, &cell));
tt_int_op(CELL_CREATED, ==, cc.cell_type);
tt_int_op(TAP_ONIONSKIN_REPLY_LEN, ==, cc.handshake_len);
test_memeq(cc.reply, b, TAP_ONIONSKIN_REPLY_LEN + 10);
tt_mem_op(cc.reply,==, b, TAP_ONIONSKIN_REPLY_LEN + 10);
tt_int_op(0, ==, created_cell_format(&cell2, &cc));
tt_int_op(cell.command, ==, cell2.command);
test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
/* A good CREATED_FAST cell */
memset(&cell, 0, sizeof(cell));
@ -541,10 +541,10 @@ test_cfmt_created_cells(void *arg)
tt_int_op(0, ==, created_cell_parse(&cc, &cell));
tt_int_op(CELL_CREATED_FAST, ==, cc.cell_type);
tt_int_op(CREATED_FAST_LEN, ==, cc.handshake_len);
test_memeq(cc.reply, b, CREATED_FAST_LEN + 10);
tt_mem_op(cc.reply,==, b, CREATED_FAST_LEN + 10);
tt_int_op(0, ==, created_cell_format(&cell2, &cc));
tt_int_op(cell.command, ==, cell2.command);
test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
/* A good CREATED2 cell with short reply */
memset(&cell, 0, sizeof(cell));
@ -556,10 +556,10 @@ test_cfmt_created_cells(void *arg)
tt_int_op(0, ==, created_cell_parse(&cc, &cell));
tt_int_op(CELL_CREATED2, ==, cc.cell_type);
tt_int_op(64, ==, cc.handshake_len);
test_memeq(cc.reply, b, 80);
tt_mem_op(cc.reply,==, b, 80);
tt_int_op(0, ==, created_cell_format(&cell2, &cc));
tt_int_op(cell.command, ==, cell2.command);
test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
/* A good CREATED2 cell with maximal reply */
memset(&cell, 0, sizeof(cell));
@ -571,10 +571,10 @@ test_cfmt_created_cells(void *arg)
tt_int_op(0, ==, created_cell_parse(&cc, &cell));
tt_int_op(CELL_CREATED2, ==, cc.cell_type);
tt_int_op(496, ==, cc.handshake_len);
test_memeq(cc.reply, b, 496);
tt_mem_op(cc.reply,==, b, 496);
tt_int_op(0, ==, created_cell_format(&cell2, &cc));
tt_int_op(cell.command, ==, cell2.command);
test_memeq(cell.payload, cell2.payload, CELL_PAYLOAD_SIZE);
tt_mem_op(cell.payload,==, cell2.payload, CELL_PAYLOAD_SIZE);
/* Bogus CREATED2 cell: too long! */
memset(&cell, 0, sizeof(cell));
@ -620,15 +620,15 @@ test_cfmt_extend_cells(void *arg)
tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
tt_int_op(258, ==, ec.orport_ipv4.port);
tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
test_memeq(ec.node_id, "electroencephalogram", 20);
tt_mem_op(ec.node_id,==, "electroencephalogram", 20);
tt_int_op(cc->cell_type, ==, CELL_CREATE);
tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_TAP);
tt_int_op(cc->handshake_len, ==, TAP_ONIONSKIN_CHALLENGE_LEN);
test_memeq(cc->onionskin, b, TAP_ONIONSKIN_CHALLENGE_LEN+20);
tt_mem_op(cc->onionskin,==, b, TAP_ONIONSKIN_CHALLENGE_LEN+20);
tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND);
tt_int_op(p2_len, ==, 26+TAP_ONIONSKIN_CHALLENGE_LEN);
test_memeq(p2, p, RELAY_PAYLOAD_SIZE);
tt_mem_op(p2,==, p, RELAY_PAYLOAD_SIZE);
/* Let's do an ntor stuffed in a legacy EXTEND cell */
memset(p, 0, sizeof(p));
@ -644,15 +644,15 @@ test_cfmt_extend_cells(void *arg)
tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
tt_int_op(258, ==, ec.orport_ipv4.port);
tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
test_memeq(ec.node_id, "electroencephalogram", 20);
tt_mem_op(ec.node_id,==, "electroencephalogram", 20);
tt_int_op(cc->cell_type, ==, CELL_CREATE2);
tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_NTOR);
tt_int_op(cc->handshake_len, ==, NTOR_ONIONSKIN_LEN);
test_memeq(cc->onionskin, b, NTOR_ONIONSKIN_LEN+20);
tt_mem_op(cc->onionskin,==, b, NTOR_ONIONSKIN_LEN+20);
tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND);
tt_int_op(p2_len, ==, 26+TAP_ONIONSKIN_CHALLENGE_LEN);
test_memeq(p2, p, RELAY_PAYLOAD_SIZE);
tt_mem_op(p2,==, p, RELAY_PAYLOAD_SIZE);
tt_int_op(0, ==, create_cell_format_relayed(&cell, cc));
/* Now let's do a minimal ntor EXTEND2 cell. */
@ -673,15 +673,15 @@ test_cfmt_extend_cells(void *arg)
tt_str_op("18.244.0.1", ==, fmt_addr(&ec.orport_ipv4.addr));
tt_int_op(61681, ==, ec.orport_ipv4.port);
tt_int_op(AF_UNSPEC, ==, tor_addr_family(&ec.orport_ipv6.addr));
test_memeq(ec.node_id, "anarchoindividualist", 20);
tt_mem_op(ec.node_id,==, "anarchoindividualist", 20);
tt_int_op(cc->cell_type, ==, CELL_CREATE2);
tt_int_op(cc->handshake_type, ==, ONION_HANDSHAKE_TYPE_NTOR);
tt_int_op(cc->handshake_len, ==, NTOR_ONIONSKIN_LEN);
test_memeq(cc->onionskin, b, NTOR_ONIONSKIN_LEN+20);
tt_mem_op(cc->onionskin,==, b, NTOR_ONIONSKIN_LEN+20);
tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND2);
tt_int_op(p2_len, ==, 35+NTOR_ONIONSKIN_LEN);
test_memeq(p2, p, RELAY_PAYLOAD_SIZE);
tt_mem_op(p2,==, p, RELAY_PAYLOAD_SIZE);
/* Now let's do a fanciful EXTEND2 cell. */
memset(&ec, 0xff, sizeof(ec));
@ -706,11 +706,11 @@ test_cfmt_extend_cells(void *arg)
tt_int_op(61681, ==, ec.orport_ipv4.port);
tt_str_op("2002::f0:c51e", ==, fmt_addr(&ec.orport_ipv6.addr));
tt_int_op(4370, ==, ec.orport_ipv6.port);
test_memeq(ec.node_id, "anthropomorphization", 20);
tt_mem_op(ec.node_id,==, "anthropomorphization", 20);
tt_int_op(cc->cell_type, ==, CELL_CREATE2);
tt_int_op(cc->handshake_type, ==, 0x105);
tt_int_op(cc->handshake_len, ==, 99);
test_memeq(cc->onionskin, b, 99+20);
tt_mem_op(cc->onionskin,==, b, 99+20);
tt_int_op(0, ==, extend_cell_format(&p2_cmd, &p2_len, p2, &ec));
tt_int_op(p2_cmd, ==, RELAY_COMMAND_EXTEND2);
/* We'll generate it minus the IPv6 address and minus the konami code */
@ -722,7 +722,7 @@ test_cfmt_extend_cells(void *arg)
"0214616e7468726f706f6d6f727068697a6174696f6e"
/* Now the handshake prologue */
"01050063");
test_memeq(p2+1+8+22+4, b, 99+20);
tt_mem_op(p2+1+8+22+4,==, b, 99+20);
tt_int_op(0, ==, create_cell_format_relayed(&cell, cc));
/* == Now try parsing some junk */
@ -836,11 +836,11 @@ test_cfmt_extended_cells(void *arg)
tt_int_op(RELAY_COMMAND_EXTENDED, ==, ec.cell_type);
tt_int_op(cc->cell_type, ==, CELL_CREATED);
tt_int_op(cc->handshake_len, ==, TAP_ONIONSKIN_REPLY_LEN);
test_memeq(cc->reply, b, TAP_ONIONSKIN_REPLY_LEN);
tt_mem_op(cc->reply,==, b, TAP_ONIONSKIN_REPLY_LEN);
tt_int_op(0, ==, extended_cell_format(&p2_cmd, &p2_len, p2, &ec));
tt_int_op(RELAY_COMMAND_EXTENDED, ==, p2_cmd);
tt_int_op(TAP_ONIONSKIN_REPLY_LEN, ==, p2_len);
test_memeq(p2, p, sizeof(p2));
tt_mem_op(p2,==, p, sizeof(p2));
/* Try an EXTENDED2 cell */
memset(&ec, 0xff, sizeof(ec));
@ -853,11 +853,11 @@ test_cfmt_extended_cells(void *arg)
tt_int_op(RELAY_COMMAND_EXTENDED2, ==, ec.cell_type);
tt_int_op(cc->cell_type, ==, CELL_CREATED2);
tt_int_op(cc->handshake_len, ==, 42);
test_memeq(cc->reply, b, 42+10);
tt_mem_op(cc->reply,==, b, 42+10);
tt_int_op(0, ==, extended_cell_format(&p2_cmd, &p2_len, p2, &ec));
tt_int_op(RELAY_COMMAND_EXTENDED2, ==, p2_cmd);
tt_int_op(2+42, ==, p2_len);
test_memeq(p2, p, sizeof(p2));
tt_mem_op(p2,==, p, sizeof(p2));
/* Try an almost-too-long EXTENDED2 cell */
memcpy(p, "\x01\xf0", 2);

View File

@ -69,15 +69,15 @@ test_cq_manip(void *arg)
pc_tmp = cell_queue_pop(&cq);
tt_int_op(cq.n, ==, 1);
tt_ptr_op(pc_tmp, !=, NULL);
test_mem_op(pc_tmp->body, ==, "\x12\x34\x56\x78\x0a", 5);
test_mem_op(pc_tmp->body+5, ==, cell.payload, sizeof(cell.payload));
tt_mem_op(pc_tmp->body, ==, "\x12\x34\x56\x78\x0a", 5);
tt_mem_op(pc_tmp->body+5, ==, cell.payload, sizeof(cell.payload));
packed_cell_free(pc_tmp);
pc_tmp = cell_queue_pop(&cq);
tt_int_op(cq.n, ==, 0);
tt_ptr_op(pc_tmp, !=, NULL);
test_mem_op(pc_tmp->body, ==, "\x20\x13\x0a", 3);
test_mem_op(pc_tmp->body+3, ==, cell.payload, sizeof(cell.payload));
tt_mem_op(pc_tmp->body, ==, "\x20\x13\x0a", 3);
tt_mem_op(pc_tmp->body+3, ==, cell.payload, sizeof(cell.payload));
packed_cell_free(pc_tmp);
pc_tmp = NULL;

View File

@ -251,7 +251,7 @@ test_rend_token_maps(void *arg)
tt_ptr_op(c3->rendinfo, ==, NULL);
tt_ptr_op(c4->rendinfo, !=, NULL);
test_mem_op(c4->rendinfo, ==, tok3, REND_TOKEN_LEN);
tt_mem_op(c4->rendinfo, ==, tok3, REND_TOKEN_LEN);
/* Now clear c4's cookie. */
circuit_set_intro_point_digest(c4, NULL);

View File

@ -65,7 +65,7 @@ test_cmux_destroy_cell_queue(void *arg)
pc = cell_queue_pop(cq);
tt_assert(pc);
test_mem_op(pc->body, ==, "\x00\x00\x00\x64\x04\x0a\x00\x00\x00", 9);
tt_mem_op(pc->body, ==, "\x00\x00\x00\x64\x04\x0a\x00\x00\x00", 9);
packed_cell_free(pc);
pc = NULL;

View File

@ -53,57 +53,57 @@ test_config_addressmap(void *arg)
/* MapAddress .invalidwildcard.com .torserver.exit - no match */
strlcpy(address, "www.invalidwildcard.com", sizeof(address));
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
/* MapAddress *invalidasterisk.com .torserver.exit - no match */
strlcpy(address, "www.invalidasterisk.com", sizeof(address));
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
/* Where no mapping for FQDN match on top-level domain */
/* MapAddress .google.com .torserver.exit */
strlcpy(address, "reader.google.com", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "reader.torserver.exit");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "reader.torserver.exit");
/* MapAddress *.yahoo.com *.google.com.torserver.exit */
strlcpy(address, "reader.yahoo.com", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "reader.google.com.torserver.exit");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "reader.google.com.torserver.exit");
/*MapAddress *.cnn.com www.cnn.com */
strlcpy(address, "cnn.com", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "www.cnn.com");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "www.cnn.com");
/* MapAddress .cn.com www.cnn.com */
strlcpy(address, "www.cn.com", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "www.cnn.com");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "www.cnn.com");
/* MapAddress ex.com www.cnn.com - no match */
strlcpy(address, "www.ex.com", sizeof(address));
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
/* MapAddress ey.com *.cnn.com - invalid expression */
strlcpy(address, "ey.com", sizeof(address));
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
/* Where mapping for FQDN match on FQDN */
strlcpy(address, "www.google.com", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "3.3.3.3");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "3.3.3.3");
strlcpy(address, "www.torproject.org", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "1.1.1.1");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "1.1.1.1");
strlcpy(address, "other.torproject.org", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "this.torproject.org.otherserver.exit");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "this.torproject.org.otherserver.exit");
strlcpy(address, "test.torproject.org", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "2.2.2.2");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "2.2.2.2");
/* Test a chain of address mappings and the order in which they were added:
"MapAddress www.example.org 4.4.4.4"
@ -111,17 +111,17 @@ test_config_addressmap(void *arg)
"MapAddress 4.4.4.4 5.5.5.5"
*/
strlcpy(address, "www.example.org", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "5.5.5.5");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "5.5.5.5");
/* Test infinite address mapping results in no change */
strlcpy(address, "www.infiniteloop.org", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "www.infiniteloop.org");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "www.infiniteloop.org");
/* Test we don't find false positives */
strlcpy(address, "www.example.com", sizeof(address));
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
/* Test top-level-domain matching a bit harder */
config_free_lines(get_options_mutable()->AddressMap);
@ -134,24 +134,24 @@ test_config_addressmap(void *arg)
config_register_addressmaps(get_options());
strlcpy(address, "www.abc.com", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "www.abc.torserver.exit");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "www.abc.torserver.exit");
strlcpy(address, "www.def.com", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "www.def.torserver.exit");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "www.def.torserver.exit");
strlcpy(address, "www.torproject.org", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "1.1.1.1");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "1.1.1.1");
strlcpy(address, "test.torproject.org", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "1.1.1.1");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "1.1.1.1");
strlcpy(address, "torproject.net", sizeof(address));
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
test_streq(address, "2.2.2.2");
tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_str_op(address,==, "2.2.2.2");
/* We don't support '*' as a mapping directive */
config_free_lines(get_options_mutable()->AddressMap);
@ -161,13 +161,13 @@ test_config_addressmap(void *arg)
config_register_addressmaps(get_options());
strlcpy(address, "www.abc.com", sizeof(address));
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
strlcpy(address, "www.def.net", sizeof(address));
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
strlcpy(address, "www.torproject.org", sizeof(address));
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
#undef addressmap_rewrite
@ -220,13 +220,13 @@ test_config_check_or_create_data_subdir(void *arg)
// The subdirectory shouldn't exist yet,
// but should be created by the call to check_or_create_data_subdir.
test_assert(r && (errno == ENOENT));
test_assert(!check_or_create_data_subdir(subdir));
test_assert(is_private_dir(subpath));
tt_assert(r && (errno == ENOENT));
tt_assert(!check_or_create_data_subdir(subdir));
tt_assert(is_private_dir(subpath));
// The check should return 0, if the directory already exists
// and is private to the user.
test_assert(!check_or_create_data_subdir(subdir));
tt_assert(!check_or_create_data_subdir(subdir));
r = stat(subpath, &st);
if (r) {
@ -243,9 +243,9 @@ test_config_check_or_create_data_subdir(void *arg)
// If the directory exists, but its mode is too permissive
// a call to check_or_create_data_subdir should reset the mode.
test_assert(!is_private_dir(subpath));
test_assert(!check_or_create_data_subdir(subdir));
test_assert(is_private_dir(subpath));
tt_assert(!is_private_dir(subpath));
tt_assert(!check_or_create_data_subdir(subdir));
tt_assert(is_private_dir(subpath));
#endif
done:
@ -291,20 +291,20 @@ test_config_write_to_data_subdir(void *arg)
#endif
// Write attempt shoudl fail, if subdirectory doesn't exist.
test_assert(write_to_data_subdir(subdir, fname, str, NULL));
test_assert(! check_or_create_data_subdir(subdir));
tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
tt_assert(! check_or_create_data_subdir(subdir));
// Content of file after write attempt should be
// equal to the original string.
test_assert(!write_to_data_subdir(subdir, fname, str, NULL));
tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
cp = read_file_to_str(filepath, 0, NULL);
test_streq(cp, str);
tt_str_op(cp,==, str);
tor_free(cp);
// A second write operation should overwrite the old content.
test_assert(!write_to_data_subdir(subdir, fname, str, NULL));
tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
cp = read_file_to_str(filepath, 0, NULL);
test_streq(cp, str);
tt_str_op(cp,==, str);
tor_free(cp);
done:
@ -325,48 +325,48 @@ good_bridge_line_test(const char *string, const char *test_addrport,
{
char *tmp = NULL;
bridge_line_t *bridge_line = parse_bridge_line(string);
test_assert(bridge_line);
tt_assert(bridge_line);
/* test addrport */
tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
test_streq(test_addrport, tmp);
tt_str_op(test_addrport,==, tmp);
tor_free(tmp);
/* If we were asked to validate a digest, but we did not get a
digest after parsing, we failed. */
if (test_digest && tor_digest_is_zero(bridge_line->digest))
test_assert(0);
tt_assert(0);
/* If we were not asked to validate a digest, and we got a digest
after parsing, we failed again. */
if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
test_assert(0);
tt_assert(0);
/* If we were asked to validate a digest, and we got a digest after
parsing, make sure it's correct. */
if (test_digest) {
tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
tor_strlower(tmp);
test_streq(test_digest, tmp);
tt_str_op(test_digest,==, tmp);
tor_free(tmp);
}
/* If we were asked to validate a transport name, make sure tha it
matches with the transport name that was parsed. */
if (test_transport && !bridge_line->transport_name)
test_assert(0);
tt_assert(0);
if (!test_transport && bridge_line->transport_name)
test_assert(0);
tt_assert(0);
if (test_transport)
test_streq(test_transport, bridge_line->transport_name);
tt_str_op(test_transport,==, bridge_line->transport_name);
/* Validate the SOCKS argument smartlist. */
if (test_socks_args && !bridge_line->socks_args)
test_assert(0);
tt_assert(0);
if (!test_socks_args && bridge_line->socks_args)
test_assert(0);
tt_assert(0);
if (test_socks_args)
test_assert(smartlist_strings_eq(test_socks_args,
tt_assert(smartlist_strings_eq(test_socks_args,
bridge_line->socks_args));
done:
@ -382,7 +382,7 @@ bad_bridge_line_test(const char *string)
bridge_line_t *bridge_line = parse_bridge_line(string);
if (bridge_line)
TT_FAIL(("%s was supposed to fail, but it didn't.", string));
test_assert(!bridge_line);
tt_assert(!bridge_line);
done:
bridge_line_free(bridge_line);
@ -490,18 +490,18 @@ test_config_parse_transport_options_line(void *arg)
{ /* too small line */
options_sl = get_options_from_transport_options_line("valley", NULL);
test_assert(!options_sl);
tt_assert(!options_sl);
}
{ /* no k=v values */
options_sl = get_options_from_transport_options_line("hit it!", NULL);
test_assert(!options_sl);
tt_assert(!options_sl);
}
{ /* correct line, but wrong transport specified */
options_sl =
get_options_from_transport_options_line("trebuchet k=v", "rook");
test_assert(!options_sl);
tt_assert(!options_sl);
}
{ /* correct -- no transport specified */
@ -512,8 +512,8 @@ test_config_parse_transport_options_line(void *arg)
options_sl =
get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
NULL);
test_assert(options_sl);
test_assert(smartlist_strings_eq(options_sl, sl_tmp));
tt_assert(options_sl);
tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
smartlist_free(sl_tmp);
@ -531,8 +531,8 @@ test_config_parse_transport_options_line(void *arg)
options_sl =
get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
"rook");
test_assert(options_sl);
test_assert(smartlist_strings_eq(options_sl, sl_tmp));
tt_assert(options_sl);
tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
smartlist_free(sl_tmp);
sl_tmp = NULL;
@ -576,7 +576,7 @@ test_config_fix_my_family(void *arg)
TT_FAIL(("options_validate failed: %s", err));
}
test_streq(options->MyFamily, "$1111111111111111111111111111111111111111, "
tt_str_op(options->MyFamily,==, "$1111111111111111111111111111111111111111, "
"$1111111111111111111111111111111111111112, "
"$1111111111111111111111111111111111111113");

View File

@ -29,7 +29,7 @@ compare_strs_for_bsearch_(const void *a, const void **b)
/** Helper: return a tristate based on comparing the strings in *<b>a</b> and
* *<b>b</b>, excluding a's first character, and ignoring case. */
static int
compare_without_first_ch_(const void *a, const void **b)
cmp_without_first_(const void *a, const void **b)
{
const char *s1 = a, *s2 = *b;
return strcasecmp(s1+1, s2);
@ -37,7 +37,7 @@ compare_without_first_ch_(const void *a, const void **b)
/** Run unit tests for basic dynamic-sized array functionality. */
static void
test_container_smartlist_basic(void)
test_container_smartlist_basic(void *arg)
{
smartlist_t *sl;
char *v0 = tor_strdup("v0");
@ -52,6 +52,7 @@ test_container_smartlist_basic(void)
/* XXXX test sort_digests, uniq_strings, uniq_digests */
/* Test smartlist add, del_keeporder, insert, get. */
(void)arg;
sl = smartlist_new();
smartlist_add(sl, v1);
smartlist_add(sl, v2);
@ -61,22 +62,22 @@ test_container_smartlist_basic(void)
smartlist_insert(sl, 1, v22);
smartlist_insert(sl, 0, v0);
smartlist_insert(sl, 5, v555);
test_eq_ptr(v0, smartlist_get(sl,0));
test_eq_ptr(v1, smartlist_get(sl,1));
test_eq_ptr(v22, smartlist_get(sl,2));
test_eq_ptr(v3, smartlist_get(sl,3));
test_eq_ptr(v4, smartlist_get(sl,4));
test_eq_ptr(v555, smartlist_get(sl,5));
tt_ptr_op(v0,==, smartlist_get(sl,0));
tt_ptr_op(v1,==, smartlist_get(sl,1));
tt_ptr_op(v22,==, smartlist_get(sl,2));
tt_ptr_op(v3,==, smartlist_get(sl,3));
tt_ptr_op(v4,==, smartlist_get(sl,4));
tt_ptr_op(v555,==, smartlist_get(sl,5));
/* Try deleting in the middle. */
smartlist_del(sl, 1);
test_eq_ptr(v555, smartlist_get(sl, 1));
tt_ptr_op(v555,==, smartlist_get(sl, 1));
/* Try deleting at the end. */
smartlist_del(sl, 4);
test_eq(4, smartlist_len(sl));
tt_int_op(4,==, smartlist_len(sl));
/* test isin. */
test_assert(smartlist_contains(sl, v3));
test_assert(!smartlist_contains(sl, v99));
tt_assert(smartlist_contains(sl, v3));
tt_assert(!smartlist_contains(sl, v99));
done:
smartlist_free(sl);
@ -92,198 +93,200 @@ test_container_smartlist_basic(void)
/** Run unit tests for smartlist-of-strings functionality. */
static void
test_container_smartlist_strings(void)
test_container_smartlist_strings(void *arg)
{
smartlist_t *sl = smartlist_new();
char *cp=NULL, *cp_alloc=NULL;
size_t sz;
/* Test split and join */
test_eq(0, smartlist_len(sl));
(void)arg;
tt_int_op(0,==, smartlist_len(sl));
smartlist_split_string(sl, "abc", ":", 0, 0);
test_eq(1, smartlist_len(sl));
test_streq("abc", smartlist_get(sl, 0));
tt_int_op(1,==, smartlist_len(sl));
tt_str_op("abc",==, smartlist_get(sl, 0));
smartlist_split_string(sl, "a::bc::", "::", 0, 0);
test_eq(4, smartlist_len(sl));
test_streq("a", smartlist_get(sl, 1));
test_streq("bc", smartlist_get(sl, 2));
test_streq("", smartlist_get(sl, 3));
tt_int_op(4,==, smartlist_len(sl));
tt_str_op("a",==, smartlist_get(sl, 1));
tt_str_op("bc",==, smartlist_get(sl, 2));
tt_str_op("",==, smartlist_get(sl, 3));
cp_alloc = smartlist_join_strings(sl, "", 0, NULL);
test_streq(cp_alloc, "abcabc");
tt_str_op(cp_alloc,==, "abcabc");
tor_free(cp_alloc);
cp_alloc = smartlist_join_strings(sl, "!", 0, NULL);
test_streq(cp_alloc, "abc!a!bc!");
tt_str_op(cp_alloc,==, "abc!a!bc!");
tor_free(cp_alloc);
cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
test_streq(cp_alloc, "abcXYaXYbcXY");
tt_str_op(cp_alloc,==, "abcXYaXYbcXY");
tor_free(cp_alloc);
cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
test_streq(cp_alloc, "abcXYaXYbcXYXY");
tt_str_op(cp_alloc,==, "abcXYaXYbcXYXY");
tor_free(cp_alloc);
cp_alloc = smartlist_join_strings(sl, "", 1, NULL);
test_streq(cp_alloc, "abcabc");
tt_str_op(cp_alloc,==, "abcabc");
tor_free(cp_alloc);
smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
test_eq(8, smartlist_len(sl));
test_streq("", smartlist_get(sl, 4));
test_streq("def", smartlist_get(sl, 5));
test_streq(" ", smartlist_get(sl, 6));
test_streq("ghijk", smartlist_get(sl, 7));
tt_int_op(8,==, smartlist_len(sl));
tt_str_op("",==, smartlist_get(sl, 4));
tt_str_op("def",==, smartlist_get(sl, 5));
tt_str_op(" ",==, smartlist_get(sl, 6));
tt_str_op("ghijk",==, smartlist_get(sl, 7));
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
smartlist_clear(sl);
smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
test_eq(3, smartlist_len(sl));
test_streq("a", smartlist_get(sl,0));
test_streq("bbd", smartlist_get(sl,1));
test_streq("cdef", smartlist_get(sl,2));
tt_int_op(3,==, smartlist_len(sl));
tt_str_op("a",==, smartlist_get(sl,0));
tt_str_op("bbd",==, smartlist_get(sl,1));
tt_str_op("cdef",==, smartlist_get(sl,2));
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
SPLIT_SKIP_SPACE, 0);
test_eq(8, smartlist_len(sl));
test_streq("z", smartlist_get(sl,3));
test_streq("zhasd", smartlist_get(sl,4));
test_streq("", smartlist_get(sl,5));
test_streq("bnud", smartlist_get(sl,6));
test_streq("", smartlist_get(sl,7));
tt_int_op(8,==, smartlist_len(sl));
tt_str_op("z",==, smartlist_get(sl,3));
tt_str_op("zhasd",==, smartlist_get(sl,4));
tt_str_op("",==, smartlist_get(sl,5));
tt_str_op("bnud",==, smartlist_get(sl,6));
tt_str_op("",==, smartlist_get(sl,7));
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
smartlist_clear(sl);
smartlist_split_string(sl, " ab\tc \td ef ", NULL,
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
test_eq(4, smartlist_len(sl));
test_streq("ab", smartlist_get(sl,0));
test_streq("c", smartlist_get(sl,1));
test_streq("d", smartlist_get(sl,2));
test_streq("ef", smartlist_get(sl,3));
tt_int_op(4,==, smartlist_len(sl));
tt_str_op("ab",==, smartlist_get(sl,0));
tt_str_op("c",==, smartlist_get(sl,1));
tt_str_op("d",==, smartlist_get(sl,2));
tt_str_op("ef",==, smartlist_get(sl,3));
smartlist_split_string(sl, "ghi\tj", NULL,
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
test_eq(6, smartlist_len(sl));
test_streq("ghi", smartlist_get(sl,4));
test_streq("j", smartlist_get(sl,5));
tt_int_op(6,==, smartlist_len(sl));
tt_str_op("ghi",==, smartlist_get(sl,4));
tt_str_op("j",==, smartlist_get(sl,5));
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
smartlist_clear(sl);
cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
test_streq(cp_alloc, "");
tt_str_op(cp_alloc,==, "");
tor_free(cp_alloc);
cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
test_streq(cp_alloc, "XY");
tt_str_op(cp_alloc,==, "XY");
tor_free(cp_alloc);
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
test_eq(3, smartlist_len(sl));
test_streq("z", smartlist_get(sl, 0));
test_streq("zhasd", smartlist_get(sl, 1));
test_streq("bnud", smartlist_get(sl, 2));
tt_int_op(3,==, smartlist_len(sl));
tt_str_op("z",==, smartlist_get(sl, 0));
tt_str_op("zhasd",==, smartlist_get(sl, 1));
tt_str_op("bnud",==, smartlist_get(sl, 2));
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
test_eq(5, smartlist_len(sl));
test_streq("z", smartlist_get(sl, 3));
test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
tt_int_op(5,==, smartlist_len(sl));
tt_str_op("z",==, smartlist_get(sl, 3));
tt_str_op("zhasd <> <> bnud<>",==, smartlist_get(sl, 4));
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
smartlist_clear(sl);
smartlist_split_string(sl, "abcd\n", "\n",
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
test_eq(1, smartlist_len(sl));
test_streq("abcd", smartlist_get(sl, 0));
tt_int_op(1,==, smartlist_len(sl));
tt_str_op("abcd",==, smartlist_get(sl, 0));
smartlist_split_string(sl, "efgh", "\n",
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
test_eq(2, smartlist_len(sl));
test_streq("efgh", smartlist_get(sl, 1));
tt_int_op(2,==, smartlist_len(sl));
tt_str_op("efgh",==, smartlist_get(sl, 1));
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
smartlist_clear(sl);
/* Test swapping, shuffling, and sorting. */
smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
test_eq(7, smartlist_len(sl));
tt_int_op(7,==, smartlist_len(sl));
smartlist_sort(sl, compare_strs_);
cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
test_streq(cp_alloc,"and,arma,by,nickm,onion,router,the");
tt_str_op(cp_alloc,==, "and,arma,by,nickm,onion,router,the");
tor_free(cp_alloc);
smartlist_swap(sl, 1, 5);
cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
test_streq(cp_alloc,"and,router,by,nickm,onion,arma,the");
tt_str_op(cp_alloc,==, "and,router,by,nickm,onion,arma,the");
tor_free(cp_alloc);
smartlist_shuffle(sl);
test_eq(7, smartlist_len(sl));
test_assert(smartlist_contains_string(sl, "and"));
test_assert(smartlist_contains_string(sl, "router"));
test_assert(smartlist_contains_string(sl, "by"));
test_assert(smartlist_contains_string(sl, "nickm"));
test_assert(smartlist_contains_string(sl, "onion"));
test_assert(smartlist_contains_string(sl, "arma"));
test_assert(smartlist_contains_string(sl, "the"));
tt_int_op(7,==, smartlist_len(sl));
tt_assert(smartlist_contains_string(sl, "and"));
tt_assert(smartlist_contains_string(sl, "router"));
tt_assert(smartlist_contains_string(sl, "by"));
tt_assert(smartlist_contains_string(sl, "nickm"));
tt_assert(smartlist_contains_string(sl, "onion"));
tt_assert(smartlist_contains_string(sl, "arma"));
tt_assert(smartlist_contains_string(sl, "the"));
/* Test bsearch. */
smartlist_sort(sl, compare_strs_);
test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
compare_without_first_ch_));
test_streq("and", smartlist_bsearch(sl, " AND", compare_without_first_ch_));
test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", compare_without_first_ch_));
tt_str_op("nickm",==, smartlist_bsearch(sl, "zNicKM",
cmp_without_first_));
tt_str_op("and",==,
smartlist_bsearch(sl, " AND", cmp_without_first_));
tt_ptr_op(NULL,==, smartlist_bsearch(sl, " ANz", cmp_without_first_));
/* Test bsearch_idx */
{
int f;
smartlist_t *tmp = NULL;
test_eq(0, smartlist_bsearch_idx(sl," aaa",compare_without_first_ch_,&f));
test_eq(f, 0);
test_eq(0, smartlist_bsearch_idx(sl," and",compare_without_first_ch_,&f));
test_eq(f, 1);
test_eq(1, smartlist_bsearch_idx(sl," arm",compare_without_first_ch_,&f));
test_eq(f, 0);
test_eq(1, smartlist_bsearch_idx(sl," arma",compare_without_first_ch_,&f));
test_eq(f, 1);
test_eq(2, smartlist_bsearch_idx(sl," armb",compare_without_first_ch_,&f));
test_eq(f, 0);
test_eq(7, smartlist_bsearch_idx(sl," zzzz",compare_without_first_ch_,&f));
test_eq(f, 0);
tt_int_op(0,==,smartlist_bsearch_idx(sl," aaa",cmp_without_first_,&f));
tt_int_op(f,==, 0);
tt_int_op(0,==, smartlist_bsearch_idx(sl," and",cmp_without_first_,&f));
tt_int_op(f,==, 1);
tt_int_op(1,==, smartlist_bsearch_idx(sl," arm",cmp_without_first_,&f));
tt_int_op(f,==, 0);
tt_int_op(1,==, smartlist_bsearch_idx(sl," arma",cmp_without_first_,&f));
tt_int_op(f,==, 1);
tt_int_op(2,==, smartlist_bsearch_idx(sl," armb",cmp_without_first_,&f));
tt_int_op(f,==, 0);
tt_int_op(7,==, smartlist_bsearch_idx(sl," zzzz",cmp_without_first_,&f));
tt_int_op(f,==, 0);
/* Test trivial cases for list of length 0 or 1 */
tmp = smartlist_new();
test_eq(0, smartlist_bsearch_idx(tmp, "foo",
tt_int_op(0,==, smartlist_bsearch_idx(tmp, "foo",
compare_strs_for_bsearch_, &f));
test_eq(f, 0);
tt_int_op(f,==, 0);
smartlist_insert(tmp, 0, (void *)("bar"));
test_eq(1, smartlist_bsearch_idx(tmp, "foo",
tt_int_op(1,==, smartlist_bsearch_idx(tmp, "foo",
compare_strs_for_bsearch_, &f));
test_eq(f, 0);
test_eq(0, smartlist_bsearch_idx(tmp, "aaa",
tt_int_op(f,==, 0);
tt_int_op(0,==, smartlist_bsearch_idx(tmp, "aaa",
compare_strs_for_bsearch_, &f));
test_eq(f, 0);
test_eq(0, smartlist_bsearch_idx(tmp, "bar",
tt_int_op(f,==, 0);
tt_int_op(0,==, smartlist_bsearch_idx(tmp, "bar",
compare_strs_for_bsearch_, &f));
test_eq(f, 1);
tt_int_op(f,==, 1);
/* ... and one for length 2 */
smartlist_insert(tmp, 1, (void *)("foo"));
test_eq(1, smartlist_bsearch_idx(tmp, "foo",
tt_int_op(1,==, smartlist_bsearch_idx(tmp, "foo",
compare_strs_for_bsearch_, &f));
test_eq(f, 1);
test_eq(2, smartlist_bsearch_idx(tmp, "goo",
tt_int_op(f,==, 1);
tt_int_op(2,==, smartlist_bsearch_idx(tmp, "goo",
compare_strs_for_bsearch_, &f));
test_eq(f, 0);
tt_int_op(f,==, 0);
smartlist_free(tmp);
}
/* Test reverse() and pop_last() */
smartlist_reverse(sl);
cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
test_streq(cp_alloc,"the,router,onion,nickm,by,arma,and");
tt_str_op(cp_alloc,==, "the,router,onion,nickm,by,arma,and");
tor_free(cp_alloc);
cp_alloc = smartlist_pop_last(sl);
test_streq(cp_alloc, "and");
tt_str_op(cp_alloc,==, "and");
tor_free(cp_alloc);
test_eq(smartlist_len(sl), 6);
tt_int_op(smartlist_len(sl),==, 6);
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
smartlist_clear(sl);
cp_alloc = smartlist_pop_last(sl);
test_eq_ptr(cp_alloc, NULL);
tt_ptr_op(cp_alloc,==, NULL);
/* Test uniq() */
smartlist_split_string(sl,
@ -292,16 +295,16 @@ test_container_smartlist_strings(void)
smartlist_sort(sl, compare_strs_);
smartlist_uniq(sl, compare_strs_, tor_free_);
cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
test_streq(cp_alloc, "50,a,canal,man,noon,panama,plan,radar");
tt_str_op(cp_alloc,==, "50,a,canal,man,noon,panama,plan,radar");
tor_free(cp_alloc);
/* Test contains_string, contains_string_case and contains_int_as_string */
test_assert(smartlist_contains_string(sl, "noon"));
test_assert(!smartlist_contains_string(sl, "noonoon"));
test_assert(smartlist_contains_string_case(sl, "nOOn"));
test_assert(!smartlist_contains_string_case(sl, "nooNooN"));
test_assert(smartlist_contains_int_as_string(sl, 50));
test_assert(!smartlist_contains_int_as_string(sl, 60));
tt_assert(smartlist_contains_string(sl, "noon"));
tt_assert(!smartlist_contains_string(sl, "noonoon"));
tt_assert(smartlist_contains_string_case(sl, "nOOn"));
tt_assert(!smartlist_contains_string_case(sl, "nooNooN"));
tt_assert(smartlist_contains_int_as_string(sl, 50));
tt_assert(!smartlist_contains_int_as_string(sl, 60));
/* Test smartlist_choose */
{
@ -309,7 +312,7 @@ test_container_smartlist_strings(void)
int allsame = 1;
int allin = 1;
void *first = smartlist_choose(sl);
test_assert(smartlist_contains(sl, first));
tt_assert(smartlist_contains(sl, first));
for (i = 0; i < 100; ++i) {
void *second = smartlist_choose(sl);
if (second != first)
@ -317,8 +320,8 @@ test_container_smartlist_strings(void)
if (!smartlist_contains(sl, second))
allin = 0;
}
test_assert(!allsame);
test_assert(allin);
tt_assert(!allsame);
tt_assert(allin);
}
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
smartlist_clear(sl);
@ -328,17 +331,17 @@ test_container_smartlist_strings(void)
"Some say the Earth will end in ice and some in fire",
" ", 0, 0);
cp = smartlist_get(sl, 4);
test_streq(cp, "will");
tt_str_op(cp,==, "will");
smartlist_add(sl, cp);
smartlist_remove(sl, cp);
tor_free(cp);
cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
test_streq(cp_alloc, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
tt_str_op(cp_alloc,==, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
tor_free(cp_alloc);
smartlist_string_remove(sl, "in");
cp_alloc = smartlist_join_strings2(sl, "+XX", 1, 0, &sz);
test_streq(cp_alloc, "Some+say+the+Earth+fire+end+some+ice+and");
test_eq((int)sz, 40);
tt_str_op(cp_alloc,==, "Some+say+the+Earth+fire+end+some+ice+and");
tt_int_op((int)sz,==, 40);
done:
@ -349,7 +352,7 @@ test_container_smartlist_strings(void)
/** Run unit tests for smartlist set manipulation functions. */
static void
test_container_smartlist_overlap(void)
test_container_smartlist_overlap(void *arg)
{
smartlist_t *sl = smartlist_new();
smartlist_t *ints = smartlist_new();
@ -357,6 +360,7 @@ test_container_smartlist_overlap(void)
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)
@ -365,7 +369,7 @@ test_container_smartlist_overlap(void)
/* add_all */
smartlist_add_all(ints, odds);
smartlist_add_all(ints, evens);
test_eq(smartlist_len(ints), 10);
tt_int_op(smartlist_len(ints),==, 10);
smartlist_add(primes, (void*)2);
smartlist_add(primes, (void*)3);
@ -373,24 +377,24 @@ test_container_smartlist_overlap(void)
smartlist_add(primes, (void*)7);
/* overlap */
test_assert(smartlist_overlap(ints, odds));
test_assert(smartlist_overlap(odds, primes));
test_assert(smartlist_overlap(evens, primes));
test_assert(!smartlist_overlap(odds, evens));
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);
test_eq(smartlist_len(sl), 3);
test_assert(smartlist_contains(sl, (void*)3));
test_assert(smartlist_contains(sl, (void*)5));
test_assert(smartlist_contains(sl, (void*)7));
tt_int_op(smartlist_len(sl),==, 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);
test_eq(smartlist_len(sl), 1);
test_assert(smartlist_contains(sl, (void*)2));
tt_int_op(smartlist_len(sl),==, 1);
tt_assert(smartlist_contains(sl, (void*)2));
done:
smartlist_free(odds);
@ -402,31 +406,32 @@ test_container_smartlist_overlap(void)
/** Run unit tests for smartlist-of-digests functions. */
static void
test_container_smartlist_digests(void)
test_container_smartlist_digests(void *arg)
{
smartlist_t *sl = smartlist_new();
/* contains_digest */
(void)arg;
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"));
tt_int_op(0,==, smartlist_contains_digest(NULL, "AAAAAAAAAAAAAAAAAAAA"));
tt_assert(smartlist_contains_digest(sl, "AAAAAAAAAAAAAAAAAAAA"));
tt_assert(smartlist_contains_digest(sl, "\00090AAB2AAAAaasdAAAAA"));
tt_int_op(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));
tt_mem_op(smartlist_get(sl, 0),==, "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
tt_mem_op(smartlist_get(sl, 1),==, "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
tt_mem_op(smartlist_get(sl, 2),==, "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
tt_int_op(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);
tt_int_op(2,==, smartlist_len(sl));
tt_mem_op(smartlist_get(sl, 0),==, "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
tt_mem_op(smartlist_get(sl, 1),==, "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
done:
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
@ -435,13 +440,14 @@ test_container_smartlist_digests(void)
/** Run unit tests for concatenate-a-smartlist-of-strings functions. */
static void
test_container_smartlist_join(void)
test_container_smartlist_join(void *arg)
{
smartlist_t *sl = smartlist_new();
smartlist_t *sl2 = smartlist_new(), *sl3 = smartlist_new(),
*sl4 = smartlist_new();
char *joined=NULL;
/* unique, sorted. */
(void)arg;
smartlist_split_string(sl,
"Abashments Ambush Anchorman Bacon Banks Borscht "
"Bunks Inhumane Insurance Knish Know Manners "
@ -457,21 +463,21 @@ test_container_smartlist_join(void)
sl2, char *, cp2,
strcmp(cp1,cp2),
smartlist_add(sl3, cp2)) {
test_streq(cp1, cp2);
tt_str_op(cp1,==, cp2);
smartlist_add(sl4, cp1);
} SMARTLIST_FOREACH_JOIN_END(cp1, cp2);
SMARTLIST_FOREACH(sl3, const char *, cp,
test_assert(smartlist_contains(sl2, cp) &&
tt_assert(smartlist_contains(sl2, cp) &&
!smartlist_contains_string(sl, cp)));
SMARTLIST_FOREACH(sl4, const char *, cp,
test_assert(smartlist_contains(sl, cp) &&
tt_assert(smartlist_contains(sl, cp) &&
smartlist_contains_string(sl2, cp)));
joined = smartlist_join_strings(sl3, ",", 0, NULL);
test_streq(joined, "Anemias,Anemias,Crossbowmen,Work");
tt_str_op(joined,==, "Anemias,Anemias,Crossbowmen,Work");
tor_free(joined);
joined = smartlist_join_strings(sl4, ",", 0, NULL);
test_streq(joined, "Ambush,Anchorman,Anchorman,Bacon,Inhumane,Insurance,"
tt_str_op(joined,==, "Ambush,Anchorman,Anchorman,Bacon,Inhumane,Insurance,"
"Knish,Know,Manners,Manners,Maraschinos,Wombats,Wombats");
tor_free(joined);
@ -532,18 +538,19 @@ test_container_smartlist_ints_eq(void *arg)
/** Run unit tests for bitarray code */
static void
test_container_bitarray(void)
test_container_bitarray(void *arg)
{
bitarray_t *ba = NULL;
int i, j, ok=1;
(void)arg;
ba = bitarray_init_zero(1);
test_assert(ba);
test_assert(! bitarray_is_set(ba, 0));
tt_assert(ba);
tt_assert(! bitarray_is_set(ba, 0));
bitarray_set(ba, 0);
test_assert(bitarray_is_set(ba, 0));
tt_assert(bitarray_is_set(ba, 0));
bitarray_clear(ba, 0);
test_assert(! bitarray_is_set(ba, 0));
tt_assert(! bitarray_is_set(ba, 0));
bitarray_free(ba);
ba = bitarray_init_zero(1023);
@ -558,7 +565,7 @@ test_container_bitarray(void)
if (!bool_eq(bitarray_is_set(ba, j), j%i))
ok = 0;
}
test_assert(ok);
tt_assert(ok);
if (i < 7)
++i;
else if (i == 28)
@ -575,7 +582,7 @@ test_container_bitarray(void)
/** Run unit tests for digest set code (implemented as a hashtable or as a
* bloom filter) */
static void
test_container_digestset(void)
test_container_digestset(void *arg)
{
smartlist_t *included = smartlist_new();
char d[DIGEST_LEN];
@ -584,6 +591,7 @@ test_container_digestset(void)
int false_positives = 0;
digestset_t *set = NULL;
(void)arg;
for (i = 0; i < 1000; ++i) {
crypto_rand(d, DIGEST_LEN);
smartlist_add(included, tor_memdup(d, DIGEST_LEN));
@ -592,19 +600,19 @@ test_container_digestset(void)
SMARTLIST_FOREACH(included, const char *, cp,
if (digestset_contains(set, cp))
ok = 0);
test_assert(ok);
tt_assert(ok);
SMARTLIST_FOREACH(included, const char *, cp,
digestset_add(set, cp));
SMARTLIST_FOREACH(included, const char *, cp,
if (!digestset_contains(set, cp))
ok = 0);
test_assert(ok);
tt_assert(ok);
for (i = 0; i < 1000; ++i) {
crypto_rand(d, DIGEST_LEN);
if (digestset_contains(set, d))
++false_positives;
}
test_assert(false_positives < 50); /* Should be far lower. */
tt_int_op(50, >, false_positives); /* Should be far lower. */
done:
if (set)
@ -628,7 +636,7 @@ compare_strings_for_pqueue_(const void *p1, const void *p2)
/** Run unit tests for heap-based priority queue functions. */
static void
test_container_pqueue(void)
test_container_pqueue(void *arg)
{
smartlist_t *sl = smartlist_new();
int (*cmp)(const void *, const void*);
@ -650,6 +658,8 @@ test_container_pqueue(void)
#define OK() smartlist_pqueue_assert_ok(sl, cmp, offset)
(void)arg;
cmp = compare_strings_for_pqueue_;
smartlist_pqueue_add(sl, cmp, offset, &cows);
smartlist_pqueue_add(sl, cmp, offset, &zebras);
@ -665,31 +675,31 @@ test_container_pqueue(void)
OK();
test_eq(smartlist_len(sl), 11);
test_eq_ptr(smartlist_get(sl, 0), &apples);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &apples);
test_eq(smartlist_len(sl), 10);
tt_int_op(smartlist_len(sl),==, 11);
tt_ptr_op(smartlist_get(sl, 0),==, &apples);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &apples);
tt_int_op(smartlist_len(sl),==, 10);
OK();
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &cows);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &daschunds);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &cows);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &daschunds);
smartlist_pqueue_add(sl, cmp, offset, &chinchillas);
OK();
smartlist_pqueue_add(sl, cmp, offset, &fireflies);
OK();
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &chinchillas);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &eggplants);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fireflies);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &chinchillas);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &eggplants);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &fireflies);
OK();
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &lobsters);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &roquefort);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &fish);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &frogs);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &lobsters);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &roquefort);
OK();
test_eq(smartlist_len(sl), 3);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &weissbier);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &zebras);
test_eq(smartlist_len(sl), 0);
tt_int_op(smartlist_len(sl),==, 3);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &squid);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &weissbier);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &zebras);
tt_int_op(smartlist_len(sl),==, 0);
OK();
/* Now test remove. */
@ -699,21 +709,21 @@ test_container_pqueue(void)
smartlist_pqueue_add(sl, cmp, offset, &apples);
smartlist_pqueue_add(sl, cmp, offset, &squid);
smartlist_pqueue_add(sl, cmp, offset, &zebras);
test_eq(smartlist_len(sl), 6);
tt_int_op(smartlist_len(sl),==, 6);
OK();
smartlist_pqueue_remove(sl, cmp, offset, &zebras);
test_eq(smartlist_len(sl), 5);
tt_int_op(smartlist_len(sl),==, 5);
OK();
smartlist_pqueue_remove(sl, cmp, offset, &cows);
test_eq(smartlist_len(sl), 4);
tt_int_op(smartlist_len(sl),==, 4);
OK();
smartlist_pqueue_remove(sl, cmp, offset, &apples);
test_eq(smartlist_len(sl), 3);
tt_int_op(smartlist_len(sl),==, 3);
OK();
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
test_eq(smartlist_len(sl), 0);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &fish);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &frogs);
tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),==, &squid);
tt_int_op(smartlist_len(sl),==, 0);
OK();
#undef OK
@ -725,7 +735,7 @@ test_container_pqueue(void)
/** Run unit tests for string-to-void* map functions */
static void
test_container_strmap(void)
test_container_strmap(void *arg)
{
strmap_t *map;
strmap_iter_t *iter;
@ -742,32 +752,33 @@ test_container_strmap(void)
char *v104 = tor_strdup("v104");
char *v105 = tor_strdup("v105");
(void)arg;
map = strmap_new();
test_assert(map);
test_eq(strmap_size(map), 0);
test_assert(strmap_isempty(map));
tt_assert(map);
tt_int_op(strmap_size(map),==, 0);
tt_assert(strmap_isempty(map));
v = strmap_set(map, "K1", v99);
test_eq_ptr(v, NULL);
test_assert(!strmap_isempty(map));
tt_ptr_op(v,==, NULL);
tt_assert(!strmap_isempty(map));
v = strmap_set(map, "K2", v101);
test_eq_ptr(v, NULL);
tt_ptr_op(v,==, NULL);
v = strmap_set(map, "K1", v100);
test_eq_ptr(v, v99);
test_eq_ptr(strmap_get(map,"K1"), v100);
test_eq_ptr(strmap_get(map,"K2"), v101);
test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
tt_ptr_op(v,==, v99);
tt_ptr_op(strmap_get(map,"K1"),==, v100);
tt_ptr_op(strmap_get(map,"K2"),==, v101);
tt_ptr_op(strmap_get(map,"K-not-there"),==, NULL);
strmap_assert_ok(map);
v = strmap_remove(map,"K2");
strmap_assert_ok(map);
test_eq_ptr(v, v101);
test_eq_ptr(strmap_get(map,"K2"), NULL);
test_eq_ptr(strmap_remove(map,"K2"), NULL);
tt_ptr_op(v,==, v101);
tt_ptr_op(strmap_get(map,"K2"),==, NULL);
tt_ptr_op(strmap_remove(map,"K2"),==, NULL);
strmap_set(map, "K2", v101);
strmap_set(map, "K3", v102);
strmap_set(map, "K4", v103);
test_eq(strmap_size(map), 4);
tt_int_op(strmap_size(map),==, 4);
strmap_assert_ok(map);
strmap_set(map, "K5", v104);
strmap_set(map, "K6", v105);
@ -779,7 +790,7 @@ test_container_strmap(void)
while (!strmap_iter_done(iter)) {
strmap_iter_get(iter,&k,&v);
smartlist_add(found_keys, tor_strdup(k));
test_eq_ptr(v, strmap_get(map, k));
tt_ptr_op(v,==, strmap_get(map, k));
if (!strcmp(k, "K2")) {
iter = strmap_iter_next_rmv(map,iter);
@ -789,12 +800,12 @@ test_container_strmap(void)
}
/* Make sure we removed K2, but not the others. */
test_eq_ptr(strmap_get(map, "K2"), NULL);
test_eq_ptr(strmap_get(map, "K5"), v104);
tt_ptr_op(strmap_get(map, "K2"),==, NULL);
tt_ptr_op(strmap_get(map, "K5"),==, v104);
/* Make sure we visited everyone once */
smartlist_sort_strings(found_keys);
visited = smartlist_join_strings(found_keys, ":", 0, NULL);
test_streq(visited, "K1:K2:K3:K4:K5:K6");
tt_str_op(visited,==, "K1:K2:K3:K4:K5:K6");
strmap_assert_ok(map);
/* Clean up after ourselves. */
@ -804,13 +815,13 @@ test_container_strmap(void)
/* Now try some lc functions. */
map = strmap_new();
strmap_set_lc(map,"Ab.C", v1);
test_eq_ptr(strmap_get(map,"ab.c"), v1);
tt_ptr_op(strmap_get(map,"ab.c"),==, v1);
strmap_assert_ok(map);
test_eq_ptr(strmap_get_lc(map,"AB.C"), v1);
test_eq_ptr(strmap_get(map,"AB.C"), NULL);
test_eq_ptr(strmap_remove_lc(map,"aB.C"), v1);
tt_ptr_op(strmap_get_lc(map,"AB.C"),==, v1);
tt_ptr_op(strmap_get(map,"AB.C"),==, NULL);
tt_ptr_op(strmap_remove_lc(map,"aB.C"),==, v1);
strmap_assert_ok(map);
test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
tt_ptr_op(strmap_get_lc(map,"AB.C"),==, NULL);
done:
if (map)
@ -832,7 +843,7 @@ test_container_strmap(void)
/** Run unit tests for getting the median of a list. */
static void
test_container_order_functions(void)
test_container_order_functions(void *arg)
{
int lst[25], n = 0;
unsigned int lst2[25];
@ -840,42 +851,43 @@ test_container_order_functions(void)
#define median() median_int(lst, n)
(void)arg;
lst[n++] = 12;
test_eq(12, median()); /* 12 */
tt_int_op(12,==, median()); /* 12 */
lst[n++] = 77;
//smartlist_shuffle(sl);
test_eq(12, median()); /* 12, 77 */
tt_int_op(12,==, median()); /* 12, 77 */
lst[n++] = 77;
//smartlist_shuffle(sl);
test_eq(77, median()); /* 12, 77, 77 */
tt_int_op(77,==, median()); /* 12, 77, 77 */
lst[n++] = 24;
test_eq(24, median()); /* 12,24,77,77 */
tt_int_op(24,==, median()); /* 12,24,77,77 */
lst[n++] = 60;
lst[n++] = 12;
lst[n++] = 25;
//smartlist_shuffle(sl);
test_eq(25, median()); /* 12,12,24,25,60,77,77 */
tt_int_op(25,==, median()); /* 12,12,24,25,60,77,77 */
#undef median
#define third_quartile() third_quartile_uint32(lst2, n)
n = 0;
lst2[n++] = 1;
test_eq(1, third_quartile()); /* ~1~ */
tt_int_op(1,==, third_quartile()); /* ~1~ */
lst2[n++] = 2;
test_eq(2, third_quartile()); /* 1, ~2~ */
tt_int_op(2,==, third_quartile()); /* 1, ~2~ */
lst2[n++] = 3;
lst2[n++] = 4;
lst2[n++] = 5;
test_eq(4, third_quartile()); /* 1, 2, 3, ~4~, 5 */
tt_int_op(4,==, third_quartile()); /* 1, 2, 3, ~4~, 5 */
lst2[n++] = 6;
lst2[n++] = 7;
lst2[n++] = 8;
lst2[n++] = 9;
test_eq(7, third_quartile()); /* 1, 2, 3, 4, 5, 6, ~7~, 8, 9 */
tt_int_op(7,==, third_quartile()); /* 1, 2, 3, 4, 5, 6, ~7~, 8, 9 */
lst2[n++] = 10;
lst2[n++] = 11;
test_eq(9, third_quartile()); /* 1, 2, 3, 4, 5, 6, 7, 8, ~9~, 10, 11 */
tt_int_op(9,==, third_quartile()); /* 1, 2, 3, 4, 5, 6, 7, 8, ~9~, 10, 11 */
#undef third_quartile
@ -929,7 +941,7 @@ test_container_di_map(void *arg)
/** Run unit tests for fp_pair-to-void* map functions */
static void
test_container_fp_pair_map(void)
test_container_fp_pair_map(void *arg)
{
fp_pair_map_t *map;
fp_pair_t fp1, fp2, fp3, fp4, fp5, fp6;
@ -944,10 +956,11 @@ test_container_fp_pair_map(void)
char *v104 = tor_strdup("v104");
char *v105 = tor_strdup("v105");
(void)arg;
map = fp_pair_map_new();
test_assert(map);
test_eq(fp_pair_map_size(map), 0);
test_assert(fp_pair_map_isempty(map));
tt_assert(map);
tt_int_op(fp_pair_map_size(map),==, 0);
tt_assert(fp_pair_map_isempty(map));
memset(fp1.first, 0x11, DIGEST_LEN);
memset(fp1.second, 0x12, DIGEST_LEN);
@ -964,26 +977,26 @@ test_container_fp_pair_map(void)
v = fp_pair_map_set(map, &fp1, v99);
tt_ptr_op(v, ==, NULL);
test_assert(!fp_pair_map_isempty(map));
tt_assert(!fp_pair_map_isempty(map));
v = fp_pair_map_set(map, &fp2, v101);
tt_ptr_op(v, ==, NULL);
v = fp_pair_map_set(map, &fp1, v100);
tt_ptr_op(v, ==, v99);
test_eq_ptr(fp_pair_map_get(map, &fp1), v100);
test_eq_ptr(fp_pair_map_get(map, &fp2), v101);
test_eq_ptr(fp_pair_map_get(map, &fp3), NULL);
tt_ptr_op(fp_pair_map_get(map, &fp1),==, v100);
tt_ptr_op(fp_pair_map_get(map, &fp2),==, v101);
tt_ptr_op(fp_pair_map_get(map, &fp3),==, NULL);
fp_pair_map_assert_ok(map);
v = fp_pair_map_remove(map, &fp2);
fp_pair_map_assert_ok(map);
test_eq_ptr(v, v101);
test_eq_ptr(fp_pair_map_get(map, &fp2), NULL);
test_eq_ptr(fp_pair_map_remove(map, &fp2), NULL);
tt_ptr_op(v,==, v101);
tt_ptr_op(fp_pair_map_get(map, &fp2),==, NULL);
tt_ptr_op(fp_pair_map_remove(map, &fp2),==, NULL);
fp_pair_map_set(map, &fp2, v101);
fp_pair_map_set(map, &fp3, v102);
fp_pair_map_set(map, &fp4, v103);
test_eq(fp_pair_map_size(map), 4);
tt_int_op(fp_pair_map_size(map),==, 4);
fp_pair_map_assert_ok(map);
fp_pair_map_set(map, &fp5, v104);
fp_pair_map_set(map, &fp6, v105);
@ -993,7 +1006,7 @@ test_container_fp_pair_map(void)
iter = fp_pair_map_iter_init(map);
while (!fp_pair_map_iter_done(iter)) {
fp_pair_map_iter_get(iter, &k, &v);
test_eq_ptr(v, fp_pair_map_get(map, &k));
tt_ptr_op(v,==, fp_pair_map_get(map, &k));
if (tor_memeq(&fp2, &k, sizeof(fp2))) {
iter = fp_pair_map_iter_next_rmv(map, iter);
@ -1003,8 +1016,8 @@ test_container_fp_pair_map(void)
}
/* Make sure we removed fp2, but not the others. */
test_eq_ptr(fp_pair_map_get(map, &fp2), NULL);
test_eq_ptr(fp_pair_map_get(map, &fp5), v104);
tt_ptr_op(fp_pair_map_get(map, &fp2),==, NULL);
tt_ptr_op(fp_pair_map_get(map, &fp5),==, v104);
fp_pair_map_assert_ok(map);
/* Clean up after ourselves. */
@ -1024,7 +1037,7 @@ test_container_fp_pair_map(void)
}
#define CONTAINER_LEGACY(name) \
{ #name, legacy_test_helper, 0, &legacy_setup, test_container_ ## name }
{ #name, test_container_ ## name , 0, NULL, NULL }
#define CONTAINER(name, flags) \
{ #name, test_container_ ## name, (flags), NULL, NULL }

View File

@ -20,7 +20,7 @@ extern const char AUTHORITY_SIGNKEY_A_DIGEST256[];
/** Run unit tests for Diffie-Hellman functionality. */
static void
test_crypto_dh(void)
test_crypto_dh(void *arg)
{
crypto_dh_t *dh1 = crypto_dh_new(DH_TYPE_CIRCUIT);
crypto_dh_t *dh2 = crypto_dh_new(DH_TYPE_CIRCUIT);
@ -30,24 +30,25 @@ test_crypto_dh(void)
char s2[DH_BYTES];
ssize_t s1len, s2len;
test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
(void)arg;
tt_int_op(crypto_dh_get_bytes(dh1),==, DH_BYTES);
tt_int_op(crypto_dh_get_bytes(dh2),==, DH_BYTES);
memset(p1, 0, DH_BYTES);
memset(p2, 0, DH_BYTES);
test_memeq(p1, p2, DH_BYTES);
test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
test_memneq(p1, p2, DH_BYTES);
test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
test_memneq(p1, p2, DH_BYTES);
tt_mem_op(p1,==, p2, DH_BYTES);
tt_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
tt_mem_op(p1,!=, p2, DH_BYTES);
tt_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
tt_mem_op(p1,!=, p2, DH_BYTES);
memset(s1, 0, DH_BYTES);
memset(s2, 0xFF, DH_BYTES);
s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p2, DH_BYTES, s1, 50);
s2len = crypto_dh_compute_secret(LOG_WARN, dh2, p1, DH_BYTES, s2, 50);
test_assert(s1len > 0);
test_eq(s1len, s2len);
test_memeq(s1, s2, s1len);
tt_assert(s1len > 0);
tt_int_op(s1len,==, s2len);
tt_mem_op(s1,==, s2, s1len);
{
/* XXXX Now fabricate some bad values and make sure they get caught,
@ -63,17 +64,18 @@ test_crypto_dh(void)
/** Run unit tests for our random number generation function and its wrappers.
*/
static void
test_crypto_rng(void)
test_crypto_rng(void *arg)
{
int i, j, allok;
char data1[100], data2[100];
double d;
/* Try out RNG. */
test_assert(! crypto_seed_rng(0));
(void)arg;
tt_assert(! crypto_seed_rng(0));
crypto_rand(data1, 100);
crypto_rand(data2, 100);
test_memneq(data1,data2,100);
tt_mem_op(data1,!=, data2,100);
allok = 1;
for (i = 0; i < 100; ++i) {
uint64_t big;
@ -88,8 +90,8 @@ test_crypto_rng(void)
if (big >= 5)
allok = 0;
d = crypto_rand_double();
test_assert(d >= 0);
test_assert(d < 1.0);
tt_assert(d >= 0);
tt_assert(d < 1.0);
host = crypto_random_hostname(3,8,"www.",".onion");
if (strcmpstart(host,"www.") ||
strcmpend(host,".onion") ||
@ -98,7 +100,7 @@ test_crypto_rng(void)
allok = 0;
tor_free(host);
}
test_assert(allok);
tt_assert(allok);
done:
;
}
@ -128,15 +130,15 @@ test_crypto_aes(void *arg)
memset(data2, 0, 1024);
memset(data3, 0, 1024);
env1 = crypto_cipher_new(NULL);
test_neq_ptr(env1, 0);
tt_ptr_op(env1, !=, NULL);
env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
test_neq_ptr(env2, 0);
tt_ptr_op(env2, !=, NULL);
/* Try encrypting 512 chars. */
crypto_cipher_encrypt(env1, data2, data1, 512);
crypto_cipher_decrypt(env2, data3, data2, 512);
test_memeq(data1, data3, 512);
test_memneq(data1, data2, 512);
tt_mem_op(data1,==, data3, 512);
tt_mem_op(data1,!=, data2, 512);
/* Now encrypt 1 at a time, and get 1 at a time. */
for (j = 512; j < 560; ++j) {
@ -145,7 +147,7 @@ test_crypto_aes(void *arg)
for (j = 512; j < 560; ++j) {
crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
}
test_memeq(data1, data3, 560);
tt_mem_op(data1,==, data3, 560);
/* Now encrypt 3 at a time, and get 5 at a time. */
for (j = 560; j < 1024-5; j += 3) {
crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
@ -153,7 +155,7 @@ test_crypto_aes(void *arg)
for (j = 560; j < 1024-5; j += 5) {
crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
}
test_memeq(data1, data3, 1024-5);
tt_mem_op(data1,==, data3, 1024-5);
/* Now make sure that when we encrypt with different chunk sizes, we get
the same results. */
crypto_cipher_free(env2);
@ -161,7 +163,7 @@ test_crypto_aes(void *arg)
memset(data3, 0, 1024);
env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
test_neq_ptr(env2, NULL);
tt_ptr_op(env2, !=, NULL);
for (j = 0; j < 1024-16; j += 17) {
crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
}
@ -170,7 +172,7 @@ test_crypto_aes(void *arg)
printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
}
}
test_memeq(data2, data3, 1024-16);
tt_mem_op(data2,==, data3, 1024-16);
crypto_cipher_free(env1);
env1 = NULL;
crypto_cipher_free(env2);
@ -237,7 +239,7 @@ test_crypto_aes(void *arg)
"\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff");
crypto_cipher_crypt_inplace(env1, data2, 64);
test_assert(tor_mem_is_zero(data2, 64));
tt_assert(tor_mem_is_zero(data2, 64));
done:
tor_free(mem_op_hex_tmp);
@ -252,7 +254,7 @@ test_crypto_aes(void *arg)
/** Run unit tests for our SHA-1 functionality */
static void
test_crypto_sha(void)
test_crypto_sha(void *arg)
{
crypto_digest_t *d1 = NULL, *d2 = NULL;
int i;
@ -263,6 +265,7 @@ test_crypto_sha(void)
char *mem_op_hex_tmp=NULL;
/* Test SHA-1 with a test vector from the specification. */
(void)arg;
i = crypto_digest(data, "abc", 3);
test_memeq_hex(data, "A9993E364706816ABA3E25717850C26C9CD0D89D");
tt_int_op(i, ==, 0);
@ -277,13 +280,13 @@ test_crypto_sha(void)
/* Case empty (wikipedia) */
crypto_hmac_sha256(digest, "", 0, "", 0);
test_streq(hex_str(digest, 32),
tt_str_op(hex_str(digest, 32),==,
"B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
/* Case quick-brown (wikipedia) */
crypto_hmac_sha256(digest, "key", 3,
"The quick brown fox jumps over the lazy dog", 43);
test_streq(hex_str(digest, 32),
tt_str_op(hex_str(digest, 32),==,
"F7BC83F430538424B13298E6AA6FB143EF4D59A14946175997479DBC2D1A3CD8");
/* "Test Case 1" from RFC 4231 */
@ -344,43 +347,43 @@ test_crypto_sha(void)
/* Incremental digest code. */
d1 = crypto_digest_new();
test_assert(d1);
tt_assert(d1);
crypto_digest_add_bytes(d1, "abcdef", 6);
d2 = crypto_digest_dup(d1);
test_assert(d2);
tt_assert(d2);
crypto_digest_add_bytes(d2, "ghijkl", 6);
crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
crypto_digest(d_out2, "abcdefghijkl", 12);
test_memeq(d_out1, d_out2, DIGEST_LEN);
tt_mem_op(d_out1,==, d_out2, DIGEST_LEN);
crypto_digest_assign(d2, d1);
crypto_digest_add_bytes(d2, "mno", 3);
crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
crypto_digest(d_out2, "abcdefmno", 9);
test_memeq(d_out1, d_out2, DIGEST_LEN);
tt_mem_op(d_out1,==, d_out2, DIGEST_LEN);
crypto_digest_get_digest(d1, d_out1, sizeof(d_out1));
crypto_digest(d_out2, "abcdef", 6);
test_memeq(d_out1, d_out2, DIGEST_LEN);
tt_mem_op(d_out1,==, d_out2, DIGEST_LEN);
crypto_digest_free(d1);
crypto_digest_free(d2);
/* Incremental digest code with sha256 */
d1 = crypto_digest256_new(DIGEST_SHA256);
test_assert(d1);
tt_assert(d1);
crypto_digest_add_bytes(d1, "abcdef", 6);
d2 = crypto_digest_dup(d1);
test_assert(d2);
tt_assert(d2);
crypto_digest_add_bytes(d2, "ghijkl", 6);
crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
crypto_digest256(d_out2, "abcdefghijkl", 12, DIGEST_SHA256);
test_memeq(d_out1, d_out2, DIGEST_LEN);
tt_mem_op(d_out1,==, d_out2, DIGEST_LEN);
crypto_digest_assign(d2, d1);
crypto_digest_add_bytes(d2, "mno", 3);
crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
crypto_digest256(d_out2, "abcdefmno", 9, DIGEST_SHA256);
test_memeq(d_out1, d_out2, DIGEST_LEN);
tt_mem_op(d_out1,==, d_out2, DIGEST_LEN);
crypto_digest_get_digest(d1, d_out1, sizeof(d_out1));
crypto_digest256(d_out2, "abcdef", 6, DIGEST_SHA256);
test_memeq(d_out1, d_out2, DIGEST_LEN);
tt_mem_op(d_out1,==, d_out2, DIGEST_LEN);
done:
if (d1)
@ -392,7 +395,7 @@ test_crypto_sha(void)
/** Run unit tests for our public key crypto functions */
static void
test_crypto_pk(void)
test_crypto_pk(void *arg)
{
crypto_pk_t *pk1 = NULL, *pk2 = NULL;
char *encoded = NULL;
@ -401,74 +404,83 @@ test_crypto_pk(void)
int i, len;
/* Public-key ciphers */
(void)arg;
pk1 = pk_generate(0);
pk2 = crypto_pk_new();
test_assert(pk1 && pk2);
test_assert(! crypto_pk_write_public_key_to_string(pk1, &encoded, &size));
test_assert(! crypto_pk_read_public_key_from_string(pk2, encoded, size));
test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
tt_assert(pk1 && pk2);
tt_assert(! crypto_pk_write_public_key_to_string(pk1, &encoded, &size));
tt_assert(! crypto_pk_read_public_key_from_string(pk2, encoded, size));
tt_int_op(0,==, crypto_pk_cmp_keys(pk1, pk2));
/* comparison between keys and NULL */
tt_int_op(crypto_pk_cmp_keys(NULL, pk1), <, 0);
tt_int_op(crypto_pk_cmp_keys(NULL, NULL), ==, 0);
tt_int_op(crypto_pk_cmp_keys(pk1, NULL), >, 0);
test_eq(128, crypto_pk_keysize(pk1));
test_eq(1024, crypto_pk_num_bits(pk1));
test_eq(128, crypto_pk_keysize(pk2));
test_eq(1024, crypto_pk_num_bits(pk2));
tt_int_op(128,==, crypto_pk_keysize(pk1));
tt_int_op(1024,==, crypto_pk_num_bits(pk1));
tt_int_op(128,==, crypto_pk_keysize(pk2));
tt_int_op(1024,==, crypto_pk_num_bits(pk2));
test_eq(128, crypto_pk_public_encrypt(pk2, data1, sizeof(data1),
tt_int_op(128,==, crypto_pk_public_encrypt(pk2, data1, sizeof(data1),
"Hello whirled.", 15,
PK_PKCS1_OAEP_PADDING));
test_eq(128, crypto_pk_public_encrypt(pk1, data2, sizeof(data1),
tt_int_op(128,==, crypto_pk_public_encrypt(pk1, data2, sizeof(data1),
"Hello whirled.", 15,
PK_PKCS1_OAEP_PADDING));
/* oaep padding should make encryption not match */
test_memneq(data1, data2, 128);
test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data1, 128,
tt_mem_op(data1,!=, data2, 128);
tt_int_op(15,==,
crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data1, 128,
PK_PKCS1_OAEP_PADDING,1));
test_streq(data3, "Hello whirled.");
tt_str_op(data3,==, "Hello whirled.");
memset(data3, 0, 1024);
test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
tt_int_op(15,==,
crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
PK_PKCS1_OAEP_PADDING,1));
test_streq(data3, "Hello whirled.");
tt_str_op(data3,==, "Hello whirled.");
/* Can't decrypt with public key. */
test_eq(-1, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data2, 128,
tt_int_op(-1,==,
crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data2, 128,
PK_PKCS1_OAEP_PADDING,1));
/* Try again with bad padding */
memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
test_eq(-1, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
tt_int_op(-1,==,
crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
PK_PKCS1_OAEP_PADDING,1));
/* File operations: save and load private key */
test_assert(! crypto_pk_write_private_key_to_filename(pk1,
tt_assert(! crypto_pk_write_private_key_to_filename(pk1,
get_fname("pkey1")));
/* failing case for read: can't read. */
test_assert(crypto_pk_read_private_key_from_filename(pk2,
tt_assert(crypto_pk_read_private_key_from_filename(pk2,
get_fname("xyzzy")) < 0);
write_str_to_file(get_fname("xyzzy"), "foobar", 6);
/* Failing case for read: no key. */
test_assert(crypto_pk_read_private_key_from_filename(pk2,
tt_assert(crypto_pk_read_private_key_from_filename(pk2,
get_fname("xyzzy")) < 0);
test_assert(! crypto_pk_read_private_key_from_filename(pk2,
tt_assert(! crypto_pk_read_private_key_from_filename(pk2,
get_fname("pkey1")));
test_eq(15, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data1, 128,
tt_int_op(15,==,
crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data1, 128,
PK_PKCS1_OAEP_PADDING,1));
/* Now try signing. */
strlcpy(data1, "Ossifrage", 1024);
test_eq(128, crypto_pk_private_sign(pk1, data2, sizeof(data2), data1, 10));
test_eq(10,
tt_int_op(128,==,
crypto_pk_private_sign(pk1, data2, sizeof(data2), data1, 10));
tt_int_op(10,==,
crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128));
test_streq(data3, "Ossifrage");
tt_str_op(data3,==, "Ossifrage");
/* Try signing digests. */
test_eq(128, crypto_pk_private_sign_digest(pk1, data2, sizeof(data2),
tt_int_op(128,==, crypto_pk_private_sign_digest(pk1, data2, sizeof(data2),
data1, 10));
test_eq(20,
tt_int_op(20,==,
crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128));
test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
tt_int_op(0,==,
crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
tt_int_op(-1,==,
crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
/*XXXX test failed signing*/
@ -476,9 +488,9 @@ test_crypto_pk(void)
crypto_pk_free(pk2);
pk2 = NULL;
i = crypto_pk_asn1_encode(pk1, data1, 1024);
test_assert(i>0);
tt_int_op(i, >, 0);
pk2 = crypto_pk_asn1_decode(data1, i);
test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
tt_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
/* Try with hybrid encryption wrappers. */
crypto_rand(data1, 1024);
@ -487,19 +499,19 @@ test_crypto_pk(void)
memset(data3,0,1024);
len = crypto_pk_public_hybrid_encrypt(pk1,data2,sizeof(data2),
data1,i,PK_PKCS1_OAEP_PADDING,0);
test_assert(len>=0);
tt_int_op(len, >=, 0);
len = crypto_pk_private_hybrid_decrypt(pk1,data3,sizeof(data3),
data2,len,PK_PKCS1_OAEP_PADDING,1);
test_eq(len,i);
test_memeq(data1,data3,i);
tt_int_op(len,==, i);
tt_mem_op(data1,==, data3,i);
}
/* Try copy_full */
crypto_pk_free(pk2);
pk2 = crypto_pk_copy_full(pk1);
test_assert(pk2 != NULL);
test_neq_ptr(pk1, pk2);
test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
tt_assert(pk2 != NULL);
tt_ptr_op(pk1, !=, pk2);
tt_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
done:
if (pk1)
@ -532,7 +544,7 @@ test_crypto_pk_fingerprints(void *arg)
/* Is digest as expected? */
crypto_digest(d, encoded, n);
tt_int_op(0, ==, crypto_pk_get_digest(pk, d2));
test_memeq(d, d2, DIGEST_LEN);
tt_mem_op(d,==, d2, DIGEST_LEN);
/* Is fingerprint right? */
tt_int_op(0, ==, crypto_pk_get_fingerprint(pk, fingerprint, 0));
@ -561,28 +573,29 @@ test_crypto_pk_fingerprints(void *arg)
/** Sanity check for crypto pk digests */
static void
test_crypto_digests(void)
test_crypto_digests(void *arg)
{
crypto_pk_t *k = NULL;
ssize_t r;
digests_t pkey_digests;
char digest[DIGEST_LEN];
(void)arg;
k = crypto_pk_new();
test_assert(k);
tt_assert(k);
r = crypto_pk_read_private_key_from_string(k, AUTHORITY_SIGNKEY_3, -1);
test_assert(!r);
tt_assert(!r);
r = crypto_pk_get_digest(k, digest);
test_assert(r == 0);
test_memeq(hex_str(digest, DIGEST_LEN),
tt_assert(r == 0);
tt_mem_op(hex_str(digest, DIGEST_LEN),==,
AUTHORITY_SIGNKEY_A_DIGEST, HEX_DIGEST_LEN);
r = crypto_pk_get_all_digests(k, &pkey_digests);
test_memeq(hex_str(pkey_digests.d[DIGEST_SHA1], DIGEST_LEN),
tt_mem_op(hex_str(pkey_digests.d[DIGEST_SHA1], DIGEST_LEN),==,
AUTHORITY_SIGNKEY_A_DIGEST, HEX_DIGEST_LEN);
test_memeq(hex_str(pkey_digests.d[DIGEST_SHA256], DIGEST256_LEN),
tt_mem_op(hex_str(pkey_digests.d[DIGEST_SHA256], DIGEST256_LEN),==,
AUTHORITY_SIGNKEY_A_DIGEST256, HEX_DIGEST256_LEN);
done:
crypto_pk_free(k);
@ -591,58 +604,59 @@ test_crypto_digests(void)
/** Run unit tests for misc crypto formatting functionality (base64, base32,
* fingerprints, etc) */
static void
test_crypto_formats(void)
test_crypto_formats(void *arg)
{
char *data1 = NULL, *data2 = NULL, *data3 = NULL;
int i, j, idx;
(void)arg;
data1 = tor_malloc(1024);
data2 = tor_malloc(1024);
data3 = tor_malloc(1024);
test_assert(data1 && data2 && data3);
tt_assert(data1 && data2 && data3);
/* Base64 tests */
memset(data1, 6, 1024);
for (idx = 0; idx < 10; ++idx) {
i = base64_encode(data2, 1024, data1, idx);
test_assert(i >= 0);
tt_int_op(i, >=, 0);
j = base64_decode(data3, 1024, data2, i);
test_eq(j,idx);
test_memeq(data3, data1, idx);
tt_int_op(j,==, idx);
tt_mem_op(data3,==, data1, idx);
}
strlcpy(data1, "Test string that contains 35 chars.", 1024);
strlcat(data1, " 2nd string that contains 35 chars.", 1024);
i = base64_encode(data2, 1024, data1, 71);
test_assert(i >= 0);
tt_int_op(i, >=, 0);
j = base64_decode(data3, 1024, data2, i);
test_eq(j, 71);
test_streq(data3, data1);
test_assert(data2[i] == '\0');
tt_int_op(j,==, 71);
tt_str_op(data3,==, data1);
tt_int_op(data2[i], ==, '\0');
crypto_rand(data1, DIGEST_LEN);
memset(data2, 100, 1024);
digest_to_base64(data2, data1);
test_eq(BASE64_DIGEST_LEN, strlen(data2));
test_eq(100, data2[BASE64_DIGEST_LEN+2]);
tt_int_op(BASE64_DIGEST_LEN,==, strlen(data2));
tt_int_op(100,==, data2[BASE64_DIGEST_LEN+2]);
memset(data3, 99, 1024);
test_eq(digest_from_base64(data3, data2), 0);
test_memeq(data1, data3, DIGEST_LEN);
test_eq(99, data3[DIGEST_LEN+1]);
tt_int_op(digest_from_base64(data3, data2),==, 0);
tt_mem_op(data1,==, data3, DIGEST_LEN);
tt_int_op(99,==, data3[DIGEST_LEN+1]);
test_assert(digest_from_base64(data3, "###") < 0);
tt_assert(digest_from_base64(data3, "###") < 0);
/* Encoding SHA256 */
crypto_rand(data2, DIGEST256_LEN);
memset(data2, 100, 1024);
digest256_to_base64(data2, data1);
test_eq(BASE64_DIGEST256_LEN, strlen(data2));
test_eq(100, data2[BASE64_DIGEST256_LEN+2]);
tt_int_op(BASE64_DIGEST256_LEN,==, strlen(data2));
tt_int_op(100,==, data2[BASE64_DIGEST256_LEN+2]);
memset(data3, 99, 1024);
test_eq(digest256_from_base64(data3, data2), 0);
test_memeq(data1, data3, DIGEST256_LEN);
test_eq(99, data3[DIGEST256_LEN+1]);
tt_int_op(digest256_from_base64(data3, data2),==, 0);
tt_mem_op(data1,==, data3, DIGEST256_LEN);
tt_int_op(99,==, data3[DIGEST256_LEN+1]);
/* Base32 tests */
strlcpy(data1, "5chrs", 1024);
@ -651,27 +665,27 @@ test_crypto_formats(void)
* By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
*/
base32_encode(data2, 9, data1, 5);
test_streq(data2, "gvrwq4tt");
tt_str_op(data2,==, "gvrwq4tt");
strlcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
base32_encode(data2, 30, data1, 10);
test_streq(data2, "772w2rfobvomsywe");
tt_str_op(data2,==, "772w2rfobvomsywe");
/* Base16 tests */
strlcpy(data1, "6chrs\xff", 1024);
base16_encode(data2, 13, data1, 6);
test_streq(data2, "3663687273FF");
tt_str_op(data2,==, "3663687273FF");
strlcpy(data1, "f0d678affc000100", 1024);
i = base16_decode(data2, 8, data1, 16);
test_eq(i,0);
test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
tt_int_op(i,==, 0);
tt_mem_op(data2,==, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
/* now try some failing base16 decodes */
test_eq(-1, base16_decode(data2, 8, data1, 15)); /* odd input len */
test_eq(-1, base16_decode(data2, 7, data1, 16)); /* dest too short */
tt_int_op(-1,==, base16_decode(data2, 8, data1, 15)); /* odd input len */
tt_int_op(-1,==, base16_decode(data2, 7, data1, 16)); /* dest too short */
strlcpy(data1, "f0dz!8affc000100", 1024);
test_eq(-1, base16_decode(data2, 8, data1, 16));
tt_int_op(-1,==, base16_decode(data2, 8, data1, 16));
tor_free(data1);
tor_free(data2);
@ -680,10 +694,10 @@ test_crypto_formats(void)
/* Add spaces to fingerprint */
{
data1 = tor_strdup("ABCD1234ABCD56780000ABCD1234ABCD56780000");
test_eq(strlen(data1), 40);
tt_int_op(strlen(data1),==, 40);
data2 = tor_malloc(FINGERPRINT_LEN+1);
crypto_add_spaces_to_fp(data2, FINGERPRINT_LEN+1, data1);
test_streq(data2, "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000");
tt_str_op(data2,==, "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000");
tor_free(data1);
tor_free(data2);
}
@ -696,13 +710,14 @@ test_crypto_formats(void)
/** Run unit tests for our secret-to-key passphrase hashing functionality. */
static void
test_crypto_s2k(void)
test_crypto_s2k(void *arg)
{
char buf[29];
char buf2[29];
char *buf3 = NULL;
int i;
(void)arg;
memset(buf, 0, sizeof(buf));
memset(buf2, 0, sizeof(buf2));
buf3 = tor_malloc(65536);
@ -710,7 +725,7 @@ test_crypto_s2k(void)
secret_to_key(buf+9, 20, "", 0, buf);
crypto_digest(buf2+9, buf3, 1024);
test_memeq(buf, buf2, 29);
tt_mem_op(buf,==, buf2, 29);
memcpy(buf,"vrbacrda",8);
memcpy(buf2,"vrbacrda",8);
@ -721,7 +736,7 @@ test_crypto_s2k(void)
memcpy(buf3+i, "vrbacrda12345678", 16);
}
crypto_digest(buf2+9, buf3, 65536);
test_memeq(buf, buf2, 29);
tt_mem_op(buf,==, buf2, 29);
done:
tor_free(buf3);
@ -757,79 +772,79 @@ test_crypto_aes_iv(void *arg)
encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 4095,
plain, 4095);
test_eq(encrypted_size, 16 + 4095);
tt_int_op(encrypted_size,==, 16 + 4095);
tt_assert(encrypted_size > 0); /* This is obviously true, since 4111 is
* greater than 0, but its truth is not
* obvious to all analysis tools. */
decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
encrypted1, encrypted_size);
test_eq(decrypted_size, 4095);
tt_int_op(decrypted_size,==, 4095);
tt_assert(decrypted_size > 0);
test_memeq(plain, decrypted1, 4095);
tt_mem_op(plain,==, decrypted1, 4095);
/* Encrypt a second time (with a new random initialization vector). */
encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted2, 16 + 4095,
plain, 4095);
test_eq(encrypted_size, 16 + 4095);
tt_int_op(encrypted_size,==, 16 + 4095);
tt_assert(encrypted_size > 0);
decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted2, 4095,
encrypted2, encrypted_size);
test_eq(decrypted_size, 4095);
tt_int_op(decrypted_size,==, 4095);
tt_assert(decrypted_size > 0);
test_memeq(plain, decrypted2, 4095);
test_memneq(encrypted1, encrypted2, encrypted_size);
tt_mem_op(plain,==, decrypted2, 4095);
tt_mem_op(encrypted1,!=, encrypted2, encrypted_size);
/* Decrypt with the wrong key. */
decrypted_size = crypto_cipher_decrypt_with_iv(key2, decrypted2, 4095,
encrypted1, encrypted_size);
test_eq(decrypted_size, 4095);
test_memneq(plain, decrypted2, decrypted_size);
tt_int_op(decrypted_size,==, 4095);
tt_mem_op(plain,!=, decrypted2, decrypted_size);
/* Alter the initialization vector. */
encrypted1[0] += 42;
decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
encrypted1, encrypted_size);
test_eq(decrypted_size, 4095);
test_memneq(plain, decrypted2, 4095);
tt_int_op(decrypted_size,==, 4095);
tt_mem_op(plain,!=, decrypted2, 4095);
/* Special length case: 1. */
encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 1,
plain_1, 1);
test_eq(encrypted_size, 16 + 1);
tt_int_op(encrypted_size,==, 16 + 1);
tt_assert(encrypted_size > 0);
decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 1,
encrypted1, encrypted_size);
test_eq(decrypted_size, 1);
tt_int_op(decrypted_size,==, 1);
tt_assert(decrypted_size > 0);
test_memeq(plain_1, decrypted1, 1);
tt_mem_op(plain_1,==, decrypted1, 1);
/* Special length case: 15. */
encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 15,
plain_15, 15);
test_eq(encrypted_size, 16 + 15);
tt_int_op(encrypted_size,==, 16 + 15);
tt_assert(encrypted_size > 0);
decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 15,
encrypted1, encrypted_size);
test_eq(decrypted_size, 15);
tt_int_op(decrypted_size,==, 15);
tt_assert(decrypted_size > 0);
test_memeq(plain_15, decrypted1, 15);
tt_mem_op(plain_15,==, decrypted1, 15);
/* Special length case: 16. */
encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 16,
plain_16, 16);
test_eq(encrypted_size, 16 + 16);
tt_int_op(encrypted_size,==, 16 + 16);
tt_assert(encrypted_size > 0);
decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 16,
encrypted1, encrypted_size);
test_eq(decrypted_size, 16);
tt_int_op(decrypted_size,==, 16);
tt_assert(decrypted_size > 0);
test_memeq(plain_16, decrypted1, 16);
tt_mem_op(plain_16,==, decrypted1, 16);
/* Special length case: 17. */
encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 17,
plain_17, 17);
test_eq(encrypted_size, 16 + 17);
tt_int_op(encrypted_size,==, 16 + 17);
tt_assert(encrypted_size > 0);
decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 17,
encrypted1, encrypted_size);
test_eq(decrypted_size, 17);
tt_int_op(decrypted_size,==, 17);
tt_assert(decrypted_size > 0);
test_memeq(plain_17, decrypted1, 17);
tt_mem_op(plain_17,==, decrypted1, 17);
done:
/* Free memory. */
@ -842,34 +857,35 @@ test_crypto_aes_iv(void *arg)
/** Test base32 decoding. */
static void
test_crypto_base32_decode(void)
test_crypto_base32_decode(void *arg)
{
char plain[60], encoded[96 + 1], decoded[60];
int res;
(void)arg;
crypto_rand(plain, 60);
/* Encode and decode a random string. */
base32_encode(encoded, 96 + 1, plain, 60);
res = base32_decode(decoded, 60, encoded, 96);
test_eq(res, 0);
test_memeq(plain, decoded, 60);
tt_int_op(res,==, 0);
tt_mem_op(plain,==, decoded, 60);
/* Encode, uppercase, and decode a random string. */
base32_encode(encoded, 96 + 1, plain, 60);
tor_strupper(encoded);
res = base32_decode(decoded, 60, encoded, 96);
test_eq(res, 0);
test_memeq(plain, decoded, 60);
tt_int_op(res,==, 0);
tt_mem_op(plain,==, decoded, 60);
/* Change encoded string and decode. */
if (encoded[0] == 'A' || encoded[0] == 'a')
encoded[0] = 'B';
else
encoded[0] = 'A';
res = base32_decode(decoded, 60, encoded, 96);
test_eq(res, 0);
test_memneq(plain, decoded, 60);
tt_int_op(res,==, 0);
tt_mem_op(plain,!=, decoded, 60);
/* Bad encodings. */
encoded[0] = '!';
res = base32_decode(decoded, 60, encoded, 96);
test_assert(res < 0);
tt_int_op(0, >, res);
done:
;
@ -1024,7 +1040,7 @@ test_crypto_curve25519_impl(void *arg)
e2k[31] |= (byte & 0x80);
}
curve25519_impl(e1e2k,e1,e2k);
test_memeq(e1e2k, e2e1k, 32);
tt_mem_op(e1e2k,==, e2e1k, 32);
if (loop == loop_max-1) {
break;
}
@ -1056,11 +1072,11 @@ test_crypto_curve25519_wrappers(void *arg)
curve25519_secret_key_generate(&seckey2, 1);
curve25519_public_key_generate(&pubkey1, &seckey1);
curve25519_public_key_generate(&pubkey2, &seckey2);
test_assert(curve25519_public_key_is_ok(&pubkey1));
test_assert(curve25519_public_key_is_ok(&pubkey2));
tt_assert(curve25519_public_key_is_ok(&pubkey1));
tt_assert(curve25519_public_key_is_ok(&pubkey2));
curve25519_handshake(output1, &seckey1, &pubkey2);
curve25519_handshake(output2, &seckey2, &pubkey1);
test_memeq(output1, output2, sizeof(output1));
tt_mem_op(output1,==, output2, sizeof(output1));
done:
;
@ -1081,12 +1097,12 @@ test_crypto_curve25519_encode(void *arg)
tt_int_op(CURVE25519_BASE64_PADDED_LEN, ==, strlen(buf));
tt_int_op(0, ==, curve25519_public_from_base64(&key2, buf));
test_memeq(key1.public_key, key2.public_key, CURVE25519_PUBKEY_LEN);
tt_mem_op(key1.public_key,==, key2.public_key, CURVE25519_PUBKEY_LEN);
buf[CURVE25519_BASE64_PADDED_LEN - 1] = '\0';
tt_int_op(CURVE25519_BASE64_PADDED_LEN-1, ==, strlen(buf));
tt_int_op(0, ==, curve25519_public_from_base64(&key3, buf));
test_memeq(key1.public_key, key3.public_key, CURVE25519_PUBKEY_LEN);
tt_mem_op(key1.public_key,==, key3.public_key, CURVE25519_PUBKEY_LEN);
/* Now try bogus parses. */
strlcpy(buf, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=", sizeof(buf));
@ -1122,10 +1138,10 @@ test_crypto_curve25519_persist(void *arg)
tt_str_op(tag,==,"testing");
tor_free(tag);
test_memeq(keypair.pubkey.public_key,
tt_mem_op(keypair.pubkey.public_key,==,
keypair2.pubkey.public_key,
CURVE25519_PUBKEY_LEN);
test_memeq(keypair.seckey.secret_key,
tt_mem_op(keypair.seckey.secret_key,==,
keypair2.seckey.secret_key,
CURVE25519_SECKEY_LEN);
@ -1137,11 +1153,11 @@ test_crypto_curve25519_persist(void *arg)
tt_assert(fast_memeq(content, "== c25519v1: testing ==", taglen));
tt_assert(tor_mem_is_zero(content+taglen, 32-taglen));
cp = content + 32;
test_memeq(keypair.seckey.secret_key,
tt_mem_op(keypair.seckey.secret_key,==,
cp,
CURVE25519_SECKEY_LEN);
cp += CURVE25519_SECKEY_LEN;
test_memeq(keypair.pubkey.public_key,
tt_mem_op(keypair.pubkey.public_key,==,
cp,
CURVE25519_SECKEY_LEN);
@ -1276,7 +1292,7 @@ static const struct testcase_setup_t pass_data = {
};
#define CRYPTO_LEGACY(name) \
{ #name, legacy_test_helper, 0, &legacy_setup, test_crypto_ ## name }
{ #name, test_crypto_ ## name , 0, NULL, NULL }
struct testcase_t crypto_tests[] = {
CRYPTO_LEGACY(formats),

File diff suppressed because it is too large Load Diff

View File

@ -139,7 +139,7 @@ test_choose_random_entry_no_guards(void *arg)
/* Unintuitively, we actually pick a random node as our entry,
because router_choose_random_node() relaxes its constraints if it
can't find a proper entry guard. */
test_assert(chosen_entry);
tt_assert(chosen_entry);
done:
;
@ -201,7 +201,7 @@ populate_live_entry_guards_test_helper(int num_needed)
SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
const node_t *node_tmp;
node_tmp = add_an_entry_guard(node, 0, 1, 0, 0);
test_assert(node_tmp);
tt_assert(node_tmp);
} SMARTLIST_FOREACH_END(node);
/* Make sure the nodes were added as entry guards. */
@ -650,7 +650,7 @@ test_entry_is_live(void *arg)
SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
const node_t *node_tmp;
node_tmp = add_an_entry_guard(node, 0, 1, 0, 0);
test_assert(node_tmp);
tt_assert(node_tmp);
tt_int_op(node->is_stable, ==, 0);
tt_int_op(node->is_fast, ==, 0);
@ -670,22 +670,22 @@ test_entry_is_live(void *arg)
test_node = entry_is_live(test_entry,
ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE,
&msg);
test_assert(!test_node);
tt_assert(!test_node);
/* Require the node to be fast, but it's not. Should fail. */
test_node = entry_is_live(test_entry,
ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
&msg);
test_assert(!test_node);
tt_assert(!test_node);
/* Don't impose any restrictions on the node. Should succeed. */
test_node = entry_is_live(test_entry, 0, &msg);
test_assert(test_node);
tt_assert(test_node);
tt_ptr_op(test_node, ==, node_get_by_id(test_entry->identity));
/* Require descriptor for this node. It has one so it should succeed. */
test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg);
test_assert(test_node);
tt_assert(test_node);
tt_ptr_op(test_node, ==, node_get_by_id(test_entry->identity));
done:

View File

@ -42,7 +42,7 @@ test_ext_or_id_map(void *arg)
/* Give c2 a new ID. */
connection_or_set_ext_or_identifier(c2);
test_mem_op(idp, !=, c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
tt_mem_op(idp, !=, c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
idp2 = tor_memdup(c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN);
tt_assert(!tor_digest_is_zero(idp2));
@ -119,7 +119,7 @@ test_ext_or_write_command(void *arg)
==, 0);
cp = buf_get_contents(TO_CONN(c1)->outbuf, &sz);
tt_int_op(sz, ==, 4);
test_mem_op(cp, ==, "\x00\x99\x00\x00", 4);
tt_mem_op(cp, ==, "\x00\x99\x00\x00", 4);
tor_free(cp);
/* Medium command. */
@ -127,7 +127,7 @@ test_ext_or_write_command(void *arg)
"Wai\0Hello", 9), ==, 0);
cp = buf_get_contents(TO_CONN(c1)->outbuf, &sz);
tt_int_op(sz, ==, 13);
test_mem_op(cp, ==, "\x00\x99\x00\x09Wai\x00Hello", 13);
tt_mem_op(cp, ==, "\x00\x99\x00\x09Wai\x00Hello", 13);
tor_free(cp);
/* Long command */
@ -137,8 +137,8 @@ test_ext_or_write_command(void *arg)
buf, 65535), ==, 0);
cp = buf_get_contents(TO_CONN(c1)->outbuf, &sz);
tt_int_op(sz, ==, 65539);
test_mem_op(cp, ==, "\xf0\x0d\xff\xff", 4);
test_mem_op(cp+4, ==, buf, 65535);
tt_mem_op(cp, ==, "\xf0\x0d\xff\xff", 4);
tt_mem_op(cp+4, ==, buf, 65535);
tor_free(cp);
done:
@ -181,7 +181,7 @@ test_ext_or_init_auth(void *arg)
/* Shouldn't be initialized already, or our tests will be a bit
* meaningless */
ext_or_auth_cookie = tor_malloc_zero(32);
test_assert(tor_mem_is_zero((char*)ext_or_auth_cookie, 32));
tt_assert(tor_mem_is_zero((char*)ext_or_auth_cookie, 32));
/* Now make sure we use a temporary file */
fn = get_fname("ext_cookie_file");
@ -203,14 +203,14 @@ test_ext_or_init_auth(void *arg)
cp = read_file_to_str(fn, RFTS_BIN, &st);
tt_ptr_op(cp, !=, NULL);
tt_u64_op((uint64_t)st.st_size, ==, 64);
test_memeq(cp, "! Extended ORPort Auth Cookie !\x0a", 32);
test_memeq(cp+32, ext_or_auth_cookie, 32);
tt_mem_op(cp,==, "! Extended ORPort Auth Cookie !\x0a", 32);
tt_mem_op(cp+32,==, ext_or_auth_cookie, 32);
memcpy(cookie0, ext_or_auth_cookie, 32);
test_assert(!tor_mem_is_zero((char*)ext_or_auth_cookie, 32));
tt_assert(!tor_mem_is_zero((char*)ext_or_auth_cookie, 32));
/* Operation should be idempotent. */
tt_int_op(0, ==, init_ext_or_cookie_authentication(1));
test_memeq(cookie0, ext_or_auth_cookie, 32);
tt_mem_op(cookie0,==, ext_or_auth_cookie, 32);
done:
tor_free(cp);
@ -280,15 +280,15 @@ test_ext_or_cookie_auth(void *arg)
46+32+32);
crypto_hmac_sha256(hmac2, (char*)ext_or_auth_cookie, 32, client_hash_input,
46+32+32);
test_memeq(hmac1, reply, 32);
test_memeq(hmac2, client_hash, 32);
tt_mem_op(hmac1,==, reply, 32);
tt_mem_op(hmac2,==, client_hash, 32);
/* Now do it again and make sure that the results are *different* */
tt_int_op(0, ==,
handle_client_auth_nonce(client_nonce, 32, &client_hash2, &reply2,
&reply_len));
test_memneq(reply2, reply, reply_len);
test_memneq(client_hash2, client_hash, 32);
tt_mem_op(reply2,!=, reply, reply_len);
tt_mem_op(client_hash2,!=, client_hash, 32);
/* But that this one checks out too. */
memcpy(server_hash_input+46+32, reply2+32, 32);
memcpy(client_hash_input+46+32, reply2+32, 32);
@ -297,8 +297,8 @@ test_ext_or_cookie_auth(void *arg)
46+32+32);
crypto_hmac_sha256(hmac2, (char*)ext_or_auth_cookie, 32, client_hash_input,
46+32+32);
test_memeq(hmac1, reply2, 32);
test_memeq(hmac2, client_hash2, 32);
tt_mem_op(hmac1,==, reply2, 32);
tt_mem_op(hmac2,==, client_hash2, 32);
done:
tor_free(reply);
@ -339,7 +339,7 @@ test_ext_or_cookie_auth_testvec(void *arg)
&reply_len));
tt_ptr_op(reply, !=, NULL );
tt_uint_op(reply_len, ==, 64);
test_memeq(reply+32, "te road There is always another ", 32);
tt_mem_op(reply+32,==, "te road There is always another ", 32);
/* HMACSHA256("Gliding wrapt in a brown mantle,"
* "ExtORPort authentication server-to-client hash"
* "But when I look ahead up the write road There is always another ");
@ -406,7 +406,7 @@ handshake_start(or_connection_t *conn, int receiving)
tt_int_op(buf_datalen(TO_CONN(conn)->outbuf), ==, (n)); \
if ((n)) { \
fetch_from_buf(b, (n), TO_CONN(conn)->outbuf); \
test_memeq(b, (s), (n)); \
tt_mem_op(b, ==, (s), (n)); \
} \
} while (0)

View File

@ -84,8 +84,8 @@ test_hs_desc_event(void *arg)
STR_HS_ID);
expected_msg = "650 HS_DESC REQUESTED "STR_HS_ADDR" NO_AUTH "\
STR_HSDIR_EXIST_LONGNAME" "STR_HS_ID"\r\n";
test_assert(received_msg);
test_streq(received_msg, expected_msg);
tt_assert(received_msg);
tt_str_op(received_msg,==, expected_msg);
tor_free(received_msg);
/* test received event */
@ -93,8 +93,8 @@ test_hs_desc_event(void *arg)
control_event_hs_descriptor_received(&rend_query, HSDIR_EXIST_ID);
expected_msg = "650 HS_DESC RECEIVED "STR_HS_ADDR" BASIC_AUTH "\
STR_HSDIR_EXIST_LONGNAME"\r\n";
test_assert(received_msg);
test_streq(received_msg, expected_msg);
tt_assert(received_msg);
tt_str_op(received_msg,==, expected_msg);
tor_free(received_msg);
/* test failed event */
@ -102,8 +102,8 @@ test_hs_desc_event(void *arg)
control_event_hs_descriptor_failed(&rend_query, HSDIR_NONE_EXIST_ID);
expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" STEALTH_AUTH "\
STR_HSDIR_NONE_EXIST_LONGNAME"\r\n";
test_assert(received_msg);
test_streq(received_msg, expected_msg);
tt_assert(received_msg);
tt_str_op(received_msg,==, expected_msg);
tor_free(received_msg);
/* test invalid auth type */
@ -111,8 +111,8 @@ test_hs_desc_event(void *arg)
control_event_hs_descriptor_failed(&rend_query, HSDIR_EXIST_ID);
expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" UNKNOWN "\
STR_HSDIR_EXIST_LONGNAME"\r\n";
test_assert(received_msg);
test_streq(received_msg, expected_msg);
tt_assert(received_msg);
tt_str_op(received_msg,==, expected_msg);
tor_free(received_msg);
done:

View File

@ -290,48 +290,48 @@ do_parse_test(uint8_t *plaintext, size_t plaintext_len, int phase)
/* Get a key */
k = crypto_pk_new();
test_assert(k);
tt_assert(k);
r = crypto_pk_read_private_key_from_string(k, AUTHORITY_SIGNKEY_1, -1);
test_assert(!r);
tt_assert(!r);
/* Get digest for future comparison */
r = crypto_pk_get_digest(k, digest);
test_assert(r >= 0);
tt_assert(r >= 0);
/* Make a cell out of it */
r = make_intro_from_plaintext(
plaintext, plaintext_len,
k, (void **)(&cell));
test_assert(r > 0);
test_assert(cell);
tt_assert(r > 0);
tt_assert(cell);
cell_len = r;
/* Do early parsing */
parsed_req = rend_service_begin_parse_intro(cell, cell_len, 2, &err_msg);
test_assert(parsed_req);
test_assert(!err_msg);
test_memeq(parsed_req->pk, digest, DIGEST_LEN);
test_assert(parsed_req->ciphertext);
test_assert(parsed_req->ciphertext_len > 0);
tt_assert(parsed_req);
tt_assert(!err_msg);
tt_mem_op(parsed_req->pk,==, digest, DIGEST_LEN);
tt_assert(parsed_req->ciphertext);
tt_assert(parsed_req->ciphertext_len > 0);
if (phase == EARLY_PARSE_ONLY)
goto done;
/* Do decryption */
r = rend_service_decrypt_intro(parsed_req, k, &err_msg);
test_assert(!r);
test_assert(!err_msg);
test_assert(parsed_req->plaintext);
test_assert(parsed_req->plaintext_len > 0);
tt_assert(!r);
tt_assert(!err_msg);
tt_assert(parsed_req->plaintext);
tt_assert(parsed_req->plaintext_len > 0);
if (phase == DECRYPT_ONLY)
goto done;
/* Do late parsing */
r = rend_service_parse_intro_plaintext(parsed_req, &err_msg);
test_assert(!r);
test_assert(!err_msg);
test_assert(parsed_req->parsed);
tt_assert(!r);
tt_assert(!err_msg);
tt_assert(parsed_req->parsed);
done:
tor_free(cell);
@ -371,14 +371,14 @@ make_intro_from_plaintext(
/* Compute key digest (will be first DIGEST_LEN octets of cell) */
r = crypto_pk_get_digest(key, cell);
test_assert(r >= 0);
tt_assert(r >= 0);
/* Do encryption */
r = crypto_pk_public_hybrid_encrypt(
key, cell + DIGEST_LEN, ciphertext_size,
buf, len,
PK_PKCS1_OAEP_PADDING, 0);
test_assert(r >= 0);
tt_assert(r >= 0);
/* Figure out cell length */
cell_len = DIGEST_LEN + r;
@ -394,8 +394,9 @@ make_intro_from_plaintext(
*/
static void
test_introduce_decrypt_v0(void)
test_introduce_decrypt_v0(void *arg)
{
(void)arg;
do_decrypt_test(v0_test_plaintext, sizeof(v0_test_plaintext));
}
@ -403,8 +404,9 @@ test_introduce_decrypt_v0(void)
*/
static void
test_introduce_decrypt_v1(void)
test_introduce_decrypt_v1(void *arg)
{
(void)arg;
do_decrypt_test(v1_test_plaintext, sizeof(v1_test_plaintext));
}
@ -412,8 +414,9 @@ test_introduce_decrypt_v1(void)
*/
static void
test_introduce_decrypt_v2(void)
test_introduce_decrypt_v2(void *arg)
{
(void)arg;
do_decrypt_test(v2_test_plaintext, sizeof(v2_test_plaintext));
}
@ -421,8 +424,9 @@ test_introduce_decrypt_v2(void)
*/
static void
test_introduce_decrypt_v3(void)
test_introduce_decrypt_v3(void *arg)
{
(void)arg;
do_decrypt_test(
v3_no_auth_test_plaintext, sizeof(v3_no_auth_test_plaintext));
do_decrypt_test(
@ -433,8 +437,9 @@ test_introduce_decrypt_v3(void)
*/
static void
test_introduce_early_parse_v0(void)
test_introduce_early_parse_v0(void *arg)
{
(void)arg;
do_early_parse_test(v0_test_plaintext, sizeof(v0_test_plaintext));
}
@ -442,8 +447,9 @@ test_introduce_early_parse_v0(void)
*/
static void
test_introduce_early_parse_v1(void)
test_introduce_early_parse_v1(void *arg)
{
(void)arg;
do_early_parse_test(v1_test_plaintext, sizeof(v1_test_plaintext));
}
@ -451,8 +457,9 @@ test_introduce_early_parse_v1(void)
*/
static void
test_introduce_early_parse_v2(void)
test_introduce_early_parse_v2(void *arg)
{
(void)arg;
do_early_parse_test(v2_test_plaintext, sizeof(v2_test_plaintext));
}
@ -460,8 +467,9 @@ test_introduce_early_parse_v2(void)
*/
static void
test_introduce_early_parse_v3(void)
test_introduce_early_parse_v3(void *arg)
{
(void)arg;
do_early_parse_test(
v3_no_auth_test_plaintext, sizeof(v3_no_auth_test_plaintext));
do_early_parse_test(
@ -472,8 +480,9 @@ test_introduce_early_parse_v3(void)
*/
static void
test_introduce_late_parse_v0(void)
test_introduce_late_parse_v0(void *arg)
{
(void)arg;
do_late_parse_test(v0_test_plaintext, sizeof(v0_test_plaintext));
}
@ -481,8 +490,9 @@ test_introduce_late_parse_v0(void)
*/
static void
test_introduce_late_parse_v1(void)
test_introduce_late_parse_v1(void *arg)
{
(void)arg;
do_late_parse_test(v1_test_plaintext, sizeof(v1_test_plaintext));
}
@ -490,8 +500,9 @@ test_introduce_late_parse_v1(void)
*/
static void
test_introduce_late_parse_v2(void)
test_introduce_late_parse_v2(void *arg)
{
(void)arg;
do_late_parse_test(v2_test_plaintext, sizeof(v2_test_plaintext));
}
@ -499,8 +510,9 @@ test_introduce_late_parse_v2(void)
*/
static void
test_introduce_late_parse_v3(void)
test_introduce_late_parse_v3(void *arg)
{
(void)arg;
do_late_parse_test(
v3_no_auth_test_plaintext, sizeof(v3_no_auth_test_plaintext));
do_late_parse_test(
@ -508,7 +520,7 @@ test_introduce_late_parse_v3(void)
}
#define INTRODUCE_LEGACY(name) \
{ #name, legacy_test_helper, 0, &legacy_setup, test_introduce_ ## name }
{ #name, test_introduce_ ## name , 0, NULL, NULL }
struct testcase_t introduce_tests[] = {
INTRODUCE_LEGACY(early_parse_v0),

View File

@ -108,7 +108,7 @@ test_md_cache(void *data)
md2 = smartlist_get(added, 0);
/* And it should have gotten removed from 'wanted' */
tt_int_op(smartlist_len(wanted), ==, 1);
test_mem_op(smartlist_get(wanted, 0), ==, d3, DIGEST256_LEN);
tt_mem_op(smartlist_get(wanted, 0), ==, d3, DIGEST256_LEN);
smartlist_free(added);
added = NULL;
@ -144,18 +144,18 @@ test_md_cache(void *data)
tt_int_op(md1->bodylen, ==, strlen(test_md1));
tt_int_op(md2->bodylen, ==, strlen(test_md2));
tt_int_op(md3->bodylen, ==, strlen(test_md3_noannotation));
test_mem_op(md1->body, ==, test_md1, strlen(test_md1));
test_mem_op(md2->body, ==, test_md2, strlen(test_md2));
test_mem_op(md3->body, ==, test_md3_noannotation,
tt_mem_op(md1->body, ==, test_md1, strlen(test_md1));
tt_mem_op(md2->body, ==, test_md2, strlen(test_md2));
tt_mem_op(md3->body, ==, test_md3_noannotation,
strlen(test_md3_noannotation));
tor_asprintf(&fn, "%s"PATH_SEPARATOR"cached-microdescs.new",
options->DataDirectory);
s = read_file_to_str(fn, RFTS_BIN, NULL);
tt_assert(s);
test_mem_op(md1->body, ==, s + md1->off, md1->bodylen);
test_mem_op(md2->body, ==, s + md2->off, md2->bodylen);
test_mem_op(md3->body, ==, s + md3->off, md3->bodylen);
tt_mem_op(md1->body, ==, s + md1->off, md1->bodylen);
tt_mem_op(md2->body, ==, s + md2->off, md2->bodylen);
tt_mem_op(md3->body, ==, s + md3->off, md3->bodylen);
tt_ptr_op(md1->family, ==, NULL);
tt_ptr_op(md3->family, !=, NULL);
@ -180,9 +180,9 @@ test_md_cache(void *data)
tor_asprintf(&fn, "%s"PATH_SEPARATOR"cached-microdescs",
options->DataDirectory);
s = read_file_to_str(fn, RFTS_BIN, NULL);
test_mem_op(md1->body, ==, s + md1->off, strlen(test_md1));
test_mem_op(md2->body, ==, s + md2->off, strlen(test_md2));
test_mem_op(md3->body, ==, s + md3->off, strlen(test_md3_noannotation));
tt_mem_op(md1->body, ==, s + md1->off, strlen(test_md1));
tt_mem_op(md2->body, ==, s + md2->off, strlen(test_md2));
tt_mem_op(md3->body, ==, s + md3->off, strlen(test_md3_noannotation));
/* Okay, now we are going to forget about the cache entirely, and reload it
* from the disk. */
@ -191,12 +191,12 @@ test_md_cache(void *data)
md1 = microdesc_cache_lookup_by_digest256(mc, d1);
md2 = microdesc_cache_lookup_by_digest256(mc, d2);
md3 = microdesc_cache_lookup_by_digest256(mc, d3);
test_assert(md1);
test_assert(md2);
test_assert(md3);
test_mem_op(md1->body, ==, s + md1->off, strlen(test_md1));
test_mem_op(md2->body, ==, s + md2->off, strlen(test_md2));
test_mem_op(md3->body, ==, s + md3->off, strlen(test_md3_noannotation));
tt_assert(md1);
tt_assert(md2);
tt_assert(md3);
tt_mem_op(md1->body, ==, s + md1->off, strlen(test_md1));
tt_mem_op(md2->body, ==, s + md2->off, strlen(test_md2));
tt_mem_op(md3->body, ==, s + md3->off, strlen(test_md3_noannotation));
tt_int_op(md1->last_listed, ==, time1);
tt_int_op(md2->last_listed, ==, time2);

View File

@ -23,9 +23,9 @@ test_nodelist_node_get_verbose_nickname_by_id_null_node(void *arg)
(void) arg;
/* make sure node_get_by_id returns NULL */
test_assert(!node_get_by_id(ID));
tt_assert(!node_get_by_id(ID));
node_get_verbose_nickname_by_id(ID, vname);
test_streq(vname, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
tt_str_op(vname,==, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
done:
return;
}
@ -54,7 +54,7 @@ test_nodelist_node_get_verbose_nickname_not_named(void *arg)
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
DIGEST_LEN);
node_get_verbose_nickname(&mock_node, vname);
test_streq(vname, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~TestOR");
tt_str_op(vname,==, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~TestOR");
done:
return;

View File

@ -48,16 +48,16 @@ test_policy_summary_helper(const char *policy_str,
line.next = NULL;
r = policies_parse_exit_policy(&line, &policy, 1, 0, 0, 1);
test_eq(r, 0);
tt_int_op(r,==, 0);
summary = policy_summarize(policy, AF_INET);
test_assert(summary != NULL);
test_streq(summary, expected_summary);
tt_assert(summary != NULL);
tt_str_op(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);
tt_str_op(summary,==, summary_after);
done:
tor_free(summary_after);
@ -86,104 +86,104 @@ test_policies_general(void *arg)
policy = smartlist_new();
p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
test_assert(p != NULL);
test_eq(ADDR_POLICY_REJECT, p->policy_type);
tt_assert(p != NULL);
tt_int_op(ADDR_POLICY_REJECT,==, p->policy_type);
tor_addr_from_ipv4h(&tar, 0xc0a80000u);
test_eq(0, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
test_eq(16, p->maskbits);
test_eq(1, p->prt_min);
test_eq(65535, p->prt_max);
tt_int_op(0,==, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
tt_int_op(16,==, p->maskbits);
tt_int_op(1,==, p->prt_min);
tt_int_op(65535,==, p->prt_max);
smartlist_add(policy, p);
tor_addr_from_ipv4h(&tar, 0x01020304u);
test_assert(ADDR_POLICY_ACCEPTED ==
tt_assert(ADDR_POLICY_ACCEPTED ==
compare_tor_addr_to_addr_policy(&tar, 2, policy));
tor_addr_make_unspec(&tar);
test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
tt_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
compare_tor_addr_to_addr_policy(&tar, 2, policy));
tor_addr_from_ipv4h(&tar, 0xc0a80102);
test_assert(ADDR_POLICY_REJECTED ==
tt_assert(ADDR_POLICY_REJECTED ==
compare_tor_addr_to_addr_policy(&tar, 2, policy));
test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, 1, 0, 1));
test_assert(policy2);
tt_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, 1, 0, 1));
tt_assert(policy2);
policy3 = smartlist_new();
p = router_parse_addr_policy_item_from_string("reject *:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy3, p);
p = router_parse_addr_policy_item_from_string("accept *:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy3, p);
policy4 = smartlist_new();
p = router_parse_addr_policy_item_from_string("accept *:443",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy4, p);
p = router_parse_addr_policy_item_from_string("accept *:443",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy4, p);
policy5 = smartlist_new();
p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy5, p);
p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy5, p);
p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy5, p);
p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy5, p);
p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy5, p);
p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy5, p);
p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy5, p);
p = router_parse_addr_policy_item_from_string("reject *:1-65534",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy5, p);
p = router_parse_addr_policy_item_from_string("reject *:65535",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy5, p);
p = router_parse_addr_policy_item_from_string("accept *:1-65535",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy5, p);
policy6 = smartlist_new();
p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy6, p);
policy7 = smartlist_new();
p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*",-1);
test_assert(p != NULL);
tt_assert(p != NULL);
smartlist_add(policy7, p);
test_assert(!exit_policy_is_general_exit(policy));
test_assert(exit_policy_is_general_exit(policy2));
test_assert(!exit_policy_is_general_exit(NULL));
test_assert(!exit_policy_is_general_exit(policy3));
test_assert(!exit_policy_is_general_exit(policy4));
test_assert(!exit_policy_is_general_exit(policy5));
test_assert(!exit_policy_is_general_exit(policy6));
test_assert(!exit_policy_is_general_exit(policy7));
tt_assert(!exit_policy_is_general_exit(policy));
tt_assert(exit_policy_is_general_exit(policy2));
tt_assert(!exit_policy_is_general_exit(NULL));
tt_assert(!exit_policy_is_general_exit(policy3));
tt_assert(!exit_policy_is_general_exit(policy4));
tt_assert(!exit_policy_is_general_exit(policy5));
tt_assert(!exit_policy_is_general_exit(policy6));
tt_assert(!exit_policy_is_general_exit(policy7));
test_assert(cmp_addr_policies(policy, policy2));
test_assert(cmp_addr_policies(policy, NULL));
test_assert(!cmp_addr_policies(policy2, policy2));
test_assert(!cmp_addr_policies(NULL, NULL));
tt_assert(cmp_addr_policies(policy, policy2));
tt_assert(cmp_addr_policies(policy, NULL));
tt_assert(!cmp_addr_policies(policy2, policy2));
tt_assert(!cmp_addr_policies(NULL, NULL));
test_assert(!policy_is_reject_star(policy2, AF_INET));
test_assert(policy_is_reject_star(policy, AF_INET));
test_assert(policy_is_reject_star(NULL, AF_INET));
tt_assert(!policy_is_reject_star(policy2, AF_INET));
tt_assert(policy_is_reject_star(policy, AF_INET));
tt_assert(policy_is_reject_star(NULL, AF_INET));
addr_policy_list_free(policy);
policy = NULL;
@ -193,11 +193,11 @@ test_policies_general(void *arg)
line.key = (char*)"foo";
line.value = (char*)"accept *:80,reject private:*,reject *:*";
line.next = NULL;
test_assert(0 == policies_parse_exit_policy(&line, &policy, 1, 0, 0, 1));
test_assert(policy);
tt_assert(0 == policies_parse_exit_policy(&line, &policy, 1, 0, 0, 1));
tt_assert(policy);
//test_streq(policy->string, "accept *:80");
//test_streq(policy->next->string, "reject *:*");
test_eq(smartlist_len(policy), 4);
tt_int_op(smartlist_len(policy),==, 4);
/* test policy summaries */
/* check if we properly ignore private IP addresses */
@ -359,7 +359,7 @@ test_dump_exit_policy_to_string(void *arg)
ri->exit_policy = NULL; // expecting "reject *:*"
ep = router_dump_exit_policy_to_string(ri,1,1);
test_streq("reject *:*",ep);
tt_str_op("reject *:*",==, ep);
tor_free(ep);
@ -372,7 +372,7 @@ test_dump_exit_policy_to_string(void *arg)
ep = router_dump_exit_policy_to_string(ri,1,1);
test_streq("accept *:*",ep);
tt_str_op("accept *:*",==, ep);
tor_free(ep);
@ -382,7 +382,7 @@ test_dump_exit_policy_to_string(void *arg)
ep = router_dump_exit_policy_to_string(ri,1,1);
test_streq("accept *:*\nreject *:25",ep);
tt_str_op("accept *:*\nreject *:25",==, ep);
tor_free(ep);
@ -393,7 +393,7 @@ test_dump_exit_policy_to_string(void *arg)
ep = router_dump_exit_policy_to_string(ri,1,1);
test_streq("accept *:*\nreject *:25\nreject 8.8.8.8:*",ep);
tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*",==, ep);
tor_free(ep);
policy_entry =
@ -403,8 +403,8 @@ test_dump_exit_policy_to_string(void *arg)
ep = router_dump_exit_policy_to_string(ri,1,1);
test_streq("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
"reject6 [fc00::]/7:*",ep);
tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
"reject6 [fc00::]/7:*",==, ep);
tor_free(ep);
policy_entry =
@ -414,8 +414,8 @@ test_dump_exit_policy_to_string(void *arg)
ep = router_dump_exit_policy_to_string(ri,1,1);
test_streq("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
"reject6 [fc00::]/7:*\naccept6 [c000::]/3:*",ep);
tt_str_op("accept *:*\nreject *:25\nreject 8.8.8.8:*\n"
"reject6 [fc00::]/7:*\naccept6 [c000::]/3:*",==, ep);
done:

View File

@ -27,75 +27,76 @@ reset_mp(managed_proxy_t *mp)
}
static void
test_pt_parsing(void)
test_pt_parsing(void *arg)
{
char line[200];
transport_t *transport = NULL;
tor_addr_t test_addr;
managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
(void)arg;
mp->conf_state = PT_PROTO_INFANT;
mp->transports = smartlist_new();
/* incomplete cmethod */
strlcpy(line,"CMETHOD trebuchet",sizeof(line));
test_assert(parse_cmethod_line(line, mp) < 0);
tt_assert(parse_cmethod_line(line, mp) < 0);
reset_mp(mp);
/* wrong proxy type */
strlcpy(line,"CMETHOD trebuchet dog 127.0.0.1:1999",sizeof(line));
test_assert(parse_cmethod_line(line, mp) < 0);
tt_assert(parse_cmethod_line(line, mp) < 0);
reset_mp(mp);
/* wrong addrport */
strlcpy(line,"CMETHOD trebuchet socks4 abcd",sizeof(line));
test_assert(parse_cmethod_line(line, mp) < 0);
tt_assert(parse_cmethod_line(line, mp) < 0);
reset_mp(mp);
/* correct line */
strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
test_assert(parse_cmethod_line(line, mp) == 0);
test_assert(smartlist_len(mp->transports) == 1);
tt_assert(parse_cmethod_line(line, mp) == 0);
tt_assert(smartlist_len(mp->transports) == 1);
transport = smartlist_get(mp->transports, 0);
/* test registered address of transport */
tor_addr_parse(&test_addr, "127.0.0.1");
test_assert(tor_addr_eq(&test_addr, &transport->addr));
tt_assert(tor_addr_eq(&test_addr, &transport->addr));
/* test registered port of transport */
test_assert(transport->port == 1999);
tt_assert(transport->port == 1999);
/* test registered SOCKS version of transport */
test_assert(transport->socks_version == PROXY_SOCKS5);
tt_assert(transport->socks_version == PROXY_SOCKS5);
/* test registered name of transport */
test_streq(transport->name, "trebuchet");
tt_str_op(transport->name,==, "trebuchet");
reset_mp(mp);
/* incomplete smethod */
strlcpy(line,"SMETHOD trebuchet",sizeof(line));
test_assert(parse_smethod_line(line, mp) < 0);
tt_assert(parse_smethod_line(line, mp) < 0);
reset_mp(mp);
/* wrong addr type */
strlcpy(line,"SMETHOD trebuchet abcd",sizeof(line));
test_assert(parse_smethod_line(line, mp) < 0);
tt_assert(parse_smethod_line(line, mp) < 0);
reset_mp(mp);
/* cowwect */
strlcpy(line,"SMETHOD trebuchy 127.0.0.2:2999",sizeof(line));
test_assert(parse_smethod_line(line, mp) == 0);
test_assert(smartlist_len(mp->transports) == 1);
tt_assert(parse_smethod_line(line, mp) == 0);
tt_assert(smartlist_len(mp->transports) == 1);
transport = smartlist_get(mp->transports, 0);
/* test registered address of transport */
tor_addr_parse(&test_addr, "127.0.0.2");
test_assert(tor_addr_eq(&test_addr, &transport->addr));
tt_assert(tor_addr_eq(&test_addr, &transport->addr));
/* test registered port of transport */
test_assert(transport->port == 2999);
tt_assert(transport->port == 2999);
/* test registered name of transport */
test_streq(transport->name, "trebuchy");
tt_str_op(transport->name,==, "trebuchy");
reset_mp(mp);
@ -103,7 +104,7 @@ test_pt_parsing(void)
strlcpy(line,"SMETHOD trebuchet 127.0.0.1:9999 "
"ARGS:counterweight=3,sling=snappy",
sizeof(line));
test_assert(parse_smethod_line(line, mp) == 0);
tt_assert(parse_smethod_line(line, mp) == 0);
tt_int_op(1, ==, smartlist_len(mp->transports));
{
const transport_t *transport = smartlist_get(mp->transports, 0);
@ -118,15 +119,15 @@ test_pt_parsing(void)
/* unsupported version */
strlcpy(line,"VERSION 666",sizeof(line));
test_assert(parse_version(line, mp) < 0);
tt_assert(parse_version(line, mp) < 0);
/* incomplete VERSION */
strlcpy(line,"VERSION ",sizeof(line));
test_assert(parse_version(line, mp) < 0);
tt_assert(parse_version(line, mp) < 0);
/* correct VERSION */
strlcpy(line,"VERSION 1",sizeof(line));
test_assert(parse_version(line, mp) == 0);
tt_assert(parse_version(line, mp) == 0);
done:
reset_mp(mp);
@ -187,11 +188,12 @@ test_pt_get_transport_options(void *arg)
}
static void
test_pt_protocol(void)
test_pt_protocol(void *arg)
{
char line[200];
managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
(void)arg;
mp->conf_state = PT_PROTO_LAUNCHED;
mp->transports = smartlist_new();
mp->argv = tor_calloc(sizeof(char *), 2);
@ -201,32 +203,32 @@ test_pt_protocol(void)
strlcpy(line,"VERSION 1",sizeof(line));
handle_proxy_line(line, mp);
test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
strlcpy(line,"VERSION 1",sizeof(line));
handle_proxy_line(line, mp);
test_assert(mp->conf_state == PT_PROTO_BROKEN);
tt_assert(mp->conf_state == PT_PROTO_BROKEN);
reset_mp(mp);
strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
handle_proxy_line(line, mp);
test_assert(mp->conf_state == PT_PROTO_BROKEN);
tt_assert(mp->conf_state == PT_PROTO_BROKEN);
reset_mp(mp);
/* correct protocol run: */
strlcpy(line,"VERSION 1",sizeof(line));
handle_proxy_line(line, mp);
test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
handle_proxy_line(line, mp);
test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
strlcpy(line,"CMETHODS DONE",sizeof(line));
handle_proxy_line(line, mp);
test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
tt_assert(mp->conf_state == PT_PROTO_CONFIGURED);
done:
reset_mp(mp);
@ -378,19 +380,19 @@ test_pt_configure_proxy(void *arg)
for (i = 0 ; i < 5 ; i++) {
retval = configure_proxy(mp);
/* retval should be zero because proxy hasn't finished configuring yet */
test_assert(retval == 0);
tt_int_op(retval, ==, 0);
/* check the number of registered transports */
test_assert(smartlist_len(mp->transports) == i+1);
tt_assert(smartlist_len(mp->transports) == i+1);
/* check that the mp is still waiting for transports */
test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
}
/* this last configure_proxy() should finalize the proxy configuration. */
retval = configure_proxy(mp);
/* retval should be 1 since the proxy finished configuring */
test_assert(retval == 1);
tt_int_op(retval, ==, 1);
/* check the mp state */
test_assert(mp->conf_state == PT_PROTO_COMPLETED);
tt_assert(mp->conf_state == PT_PROTO_COMPLETED);
tt_int_op(controlevent_n, ==, 5);
tt_int_op(controlevent_event, ==, EVENT_TRANSPORT_LAUNCHED);
@ -416,7 +418,7 @@ test_pt_configure_proxy(void *arg)
/* Get the bindaddr for "mock1" and check it against the bindaddr
that the mocked tor_get_lines_from_handle() generated. */
transport_in_state = get_transport_in_state_by_name("mock1");
test_assert(transport_in_state);
tt_assert(transport_in_state);
smartlist_split_string(transport_info_sl, transport_in_state->value,
NULL, 0, 0);
name_of_transport = smartlist_get(transport_info_sl, 0);
@ -468,7 +470,7 @@ test_get_pt_proxy_uri(void *arg)
ret = tor_addr_port_lookup(options->Socks4Proxy,
&options->Socks4ProxyAddr,
&options->Socks4ProxyPort);
tt_assert(ret == 0);
tt_int_op(ret, ==, 0);
uri = get_pt_proxy_uri();
tt_str_op(uri, ==, "socks4a://192.0.2.1:1080");
tor_free(uri);
@ -479,7 +481,7 @@ test_get_pt_proxy_uri(void *arg)
ret = tor_addr_port_lookup(options->Socks5Proxy,
&options->Socks5ProxyAddr,
&options->Socks5ProxyPort);
tt_assert(ret == 0);
tt_int_op(ret, ==, 0);
uri = get_pt_proxy_uri();
tt_str_op(uri, ==, "socks5://192.0.2.1:1080");
tor_free(uri);
@ -499,7 +501,7 @@ test_get_pt_proxy_uri(void *arg)
ret = tor_addr_port_lookup(options->HTTPSProxy,
&options->HTTPSProxyAddr,
&options->HTTPSProxyPort);
tt_assert(ret == 0);
tt_int_op(ret, ==, 0);
uri = get_pt_proxy_uri();
tt_str_op(uri, ==, "http://192.0.2.1:80");
tor_free(uri);
@ -517,7 +519,7 @@ test_get_pt_proxy_uri(void *arg)
ret = tor_addr_port_lookup(options->Socks4Proxy,
&options->Socks4ProxyAddr,
&options->Socks4ProxyPort);
tt_assert(ret == 0);
tt_int_op(ret, ==, 0);
uri = get_pt_proxy_uri();
tt_str_op(uri, ==, "socks4a://[2001:db8::1]:1080");
tor_free(uri);
@ -529,7 +531,7 @@ test_get_pt_proxy_uri(void *arg)
}
#define PT_LEGACY(name) \
{ #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
{ #name, test_pt_ ## name , 0, NULL, NULL }
struct testcase_t pt_tests[] = {
PT_LEGACY(parsing),

View File

@ -18,12 +18,13 @@ static const char *test_buffer =
" mollit anim id est laborum.";
static void
test_replaycache_alloc(void)
test_replaycache_alloc(void *arg)
{
replaycache_t *r = NULL;
(void)arg;
r = replaycache_new(600, 300);
test_assert(r != NULL);
tt_assert(r != NULL);
done:
if (r) replaycache_free(r);
@ -32,21 +33,22 @@ test_replaycache_alloc(void)
}
static void
test_replaycache_badalloc(void)
test_replaycache_badalloc(void *arg)
{
replaycache_t *r = NULL;
/* Negative horizon should fail */
(void)arg;
r = replaycache_new(-600, 300);
test_assert(r == NULL);
tt_assert(r == NULL);
/* Negative interval should get adjusted to zero */
r = replaycache_new(600, -300);
test_assert(r != NULL);
test_eq(r->scrub_interval, 0);
tt_assert(r != NULL);
tt_int_op(r->scrub_interval,==, 0);
replaycache_free(r);
/* Negative horizon and negative interval should still fail */
r = replaycache_new(-600, -300);
test_assert(r == NULL);
tt_assert(r == NULL);
done:
if (r) replaycache_free(r);
@ -55,35 +57,37 @@ test_replaycache_badalloc(void)
}
static void
test_replaycache_free_null(void)
test_replaycache_free_null(void *arg)
{
(void)arg;
replaycache_free(NULL);
/* Assert that we're here without horrible death */
test_assert(1);
tt_assert(1);
done:
return;
}
static void
test_replaycache_miss(void)
test_replaycache_miss(void *arg)
{
replaycache_t *r = NULL;
int result;
(void)arg;
r = replaycache_new(600, 300);
test_assert(r != NULL);
tt_assert(r != NULL);
result =
replaycache_add_and_test_internal(1200, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 0);
tt_int_op(result,==, 0);
/* poke the bad-parameter error case too */
result =
replaycache_add_and_test_internal(1200, NULL, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 0);
tt_int_op(result,==, 0);
done:
if (r) replaycache_free(r);
@ -92,23 +96,24 @@ test_replaycache_miss(void)
}
static void
test_replaycache_hit(void)
test_replaycache_hit(void *arg)
{
replaycache_t *r = NULL;
int result;
(void)arg;
r = replaycache_new(600, 300);
test_assert(r != NULL);
tt_assert(r != NULL);
result =
replaycache_add_and_test_internal(1200, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 0);
tt_int_op(result,==, 0);
result =
replaycache_add_and_test_internal(1300, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 1);
tt_int_op(result,==, 1);
done:
if (r) replaycache_free(r);
@ -117,28 +122,29 @@ test_replaycache_hit(void)
}
static void
test_replaycache_age(void)
test_replaycache_age(void *arg)
{
replaycache_t *r = NULL;
int result;
(void)arg;
r = replaycache_new(600, 300);
test_assert(r != NULL);
tt_assert(r != NULL);
result =
replaycache_add_and_test_internal(1200, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 0);
tt_int_op(result,==, 0);
result =
replaycache_add_and_test_internal(1300, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 1);
tt_int_op(result,==, 1);
result =
replaycache_add_and_test_internal(3000, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 0);
tt_int_op(result,==, 0);
done:
if (r) replaycache_free(r);
@ -147,25 +153,26 @@ test_replaycache_age(void)
}
static void
test_replaycache_elapsed(void)
test_replaycache_elapsed(void *arg)
{
replaycache_t *r = NULL;
int result;
time_t elapsed;
(void)arg;
r = replaycache_new(600, 300);
test_assert(r != NULL);
tt_assert(r != NULL);
result =
replaycache_add_and_test_internal(1200, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 0);
tt_int_op(result,==, 0);
result =
replaycache_add_and_test_internal(1300, r, test_buffer,
strlen(test_buffer), &elapsed);
test_eq(result, 1);
test_eq(elapsed, 100);
tt_int_op(result,==, 1);
tt_int_op(elapsed,==, 100);
done:
if (r) replaycache_free(r);
@ -174,28 +181,29 @@ test_replaycache_elapsed(void)
}
static void
test_replaycache_noexpire(void)
test_replaycache_noexpire(void *arg)
{
replaycache_t *r = NULL;
int result;
(void)arg;
r = replaycache_new(0, 0);
test_assert(r != NULL);
tt_assert(r != NULL);
result =
replaycache_add_and_test_internal(1200, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 0);
tt_int_op(result,==, 0);
result =
replaycache_add_and_test_internal(1300, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 1);
tt_int_op(result,==, 1);
result =
replaycache_add_and_test_internal(3000, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 1);
tt_int_op(result,==, 1);
done:
if (r) replaycache_free(r);
@ -204,24 +212,25 @@ test_replaycache_noexpire(void)
}
static void
test_replaycache_scrub(void)
test_replaycache_scrub(void *arg)
{
replaycache_t *r = NULL;
int result;
(void)arg;
r = replaycache_new(600, 300);
test_assert(r != NULL);
tt_assert(r != NULL);
/* Set up like in test_replaycache_hit() */
result =
replaycache_add_and_test_internal(100, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 0);
tt_int_op(result,==, 0);
result =
replaycache_add_and_test_internal(200, r, test_buffer,
strlen(test_buffer), NULL);
test_eq(result, 1);
tt_int_op(result,==, 1);
/*
* Poke a few replaycache_scrub_if_needed_internal() error cases that
@ -231,12 +240,12 @@ test_replaycache_scrub(void)
/* Null cache */
replaycache_scrub_if_needed_internal(300, NULL);
/* Assert we're still here */
test_assert(1);
tt_assert(1);
/* Make sure we hit the aging-out case too */
replaycache_scrub_if_needed_internal(1500, r);
/* Assert that we aged it */
test_eq(digestmap_size(r->digests_seen), 0);
tt_int_op(digestmap_size(r->digests_seen),==, 0);
done:
if (r) replaycache_free(r);
@ -245,29 +254,30 @@ test_replaycache_scrub(void)
}
static void
test_replaycache_future(void)
test_replaycache_future(void *arg)
{
replaycache_t *r = NULL;
int result;
time_t elapsed = 0;
(void)arg;
r = replaycache_new(600, 300);
test_assert(r != NULL);
tt_assert(r != NULL);
/* Set up like in test_replaycache_hit() */
result =
replaycache_add_and_test_internal(100, r, test_buffer,
strlen(test_buffer), &elapsed);
test_eq(result, 0);
tt_int_op(result,==, 0);
/* elapsed should still be 0, since it wasn't written */
test_eq(elapsed, 0);
tt_int_op(elapsed,==, 0);
result =
replaycache_add_and_test_internal(200, r, test_buffer,
strlen(test_buffer), &elapsed);
test_eq(result, 1);
tt_int_op(result,==, 1);
/* elapsed should be the time since the last hit */
test_eq(elapsed, 100);
tt_int_op(elapsed,==, 100);
/*
* Now let's turn the clock back to get coverage on the cache entry from the
@ -277,9 +287,9 @@ test_replaycache_future(void)
replaycache_add_and_test_internal(150, r, test_buffer,
strlen(test_buffer), &elapsed);
/* We should still get a hit */
test_eq(result, 1);
tt_int_op(result,==, 1);
/* ...but it shouldn't let us see a negative elapsed time */
test_eq(elapsed, 0);
tt_int_op(elapsed,==, 0);
done:
if (r) replaycache_free(r);
@ -288,7 +298,7 @@ test_replaycache_future(void)
}
static void
test_replaycache_realtime(void)
test_replaycache_realtime(void *arg)
{
replaycache_t *r = NULL;
/*
@ -299,26 +309,27 @@ test_replaycache_realtime(void)
int result;
/* Test the realtime as well as *_internal() entry points */
(void)arg;
r = replaycache_new(600, 300);
test_assert(r != NULL);
tt_assert(r != NULL);
/* This should miss */
result =
replaycache_add_and_test(r, test_buffer, strlen(test_buffer));
test_eq(result, 0);
tt_int_op(result,==, 0);
/* This should hit */
result =
replaycache_add_and_test(r, test_buffer, strlen(test_buffer));
test_eq(result, 1);
tt_int_op(result,==, 1);
/* This should hit and return a small elapsed time */
result =
replaycache_add_test_and_elapsed(r, test_buffer,
strlen(test_buffer), &elapsed);
test_eq(result, 1);
test_assert(elapsed >= 0);
test_assert(elapsed <= 5);
tt_int_op(result,==, 1);
tt_assert(elapsed >= 0);
tt_assert(elapsed <= 5);
/* Scrub it to exercise that entry point too */
replaycache_scrub_if_needed(r);
@ -329,7 +340,7 @@ test_replaycache_realtime(void)
}
#define REPLAYCACHE_LEGACY(name) \
{ #name, legacy_test_helper, 0, &legacy_setup, test_replaycache_ ## name }
{ #name, test_replaycache_ ## name , 0, NULL, NULL }
struct testcase_t replaycache_tests[] = {
REPLAYCACHE_LEGACY(alloc),

View File

@ -61,10 +61,10 @@ test_socks_4_unsupported_commands(void *ptr)
/* SOCKS 4 Send BIND [02] to IP address 2.2.2.2:4369 */
ADD_DATA(buf, "\x04\x02\x11\x11\x02\x02\x02\x02\x00");
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks) == -1);
test_eq(4, socks->socks_version);
test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
tt_int_op(4,==, socks->socks_version);
tt_int_op(0,==, socks->replylen); /* XXX: shouldn't tor reply? */
done:
;
@ -76,49 +76,49 @@ test_socks_4_supported_commands(void *ptr)
{
SOCKS_TEST_INIT();
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
/* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4370 */
ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x03\x00");
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks) == 1);
test_eq(4, socks->socks_version);
test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
test_eq(SOCKS_COMMAND_CONNECT, socks->command);
test_streq("2.2.2.3", socks->address);
test_eq(4370, socks->port);
test_assert(socks->got_auth == 0);
test_assert(! socks->username);
tt_int_op(4,==, socks->socks_version);
tt_int_op(0,==, socks->replylen); /* XXX: shouldn't tor reply? */
tt_int_op(SOCKS_COMMAND_CONNECT,==, socks->command);
tt_str_op("2.2.2.3",==, socks->address);
tt_int_op(4370,==, socks->port);
tt_assert(socks->got_auth == 0);
tt_assert(! socks->username);
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
socks_request_clear(socks);
/* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4369 with userid*/
ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x04me\x00");
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks) == 1);
test_eq(4, socks->socks_version);
test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
test_eq(SOCKS_COMMAND_CONNECT, socks->command);
test_streq("2.2.2.4", socks->address);
test_eq(4370, socks->port);
test_assert(socks->got_auth == 1);
test_assert(socks->username);
test_eq(2, socks->usernamelen);
test_memeq("me", socks->username, 2);
tt_int_op(4,==, socks->socks_version);
tt_int_op(0,==, socks->replylen); /* XXX: shouldn't tor reply? */
tt_int_op(SOCKS_COMMAND_CONNECT,==, socks->command);
tt_str_op("2.2.2.4",==, socks->address);
tt_int_op(4370,==, socks->port);
tt_assert(socks->got_auth == 1);
tt_assert(socks->username);
tt_int_op(2,==, socks->usernamelen);
tt_mem_op("me",==, socks->username, 2);
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
socks_request_clear(socks);
/* SOCKS 4a Send RESOLVE [F0] request for torproject.org */
ADD_DATA(buf, "\x04\xF0\x01\x01\x00\x00\x00\x02me\x00torproject.org\x00");
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks) == 1);
test_eq(4, socks->socks_version);
test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
test_streq("torproject.org", socks->address);
tt_int_op(4,==, socks->socks_version);
tt_int_op(0,==, socks->replylen); /* XXX: shouldn't tor reply? */
tt_str_op("torproject.org",==, socks->address);
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
done:
;
@ -133,16 +133,16 @@ test_socks_5_unsupported_commands(void *ptr)
/* SOCKS 5 Send unsupported BIND [02] command */
ADD_DATA(buf, "\x05\x02\x00\x01");
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks), 0);
test_eq(0, buf_datalen(buf));
test_eq(5, socks->socks_version);
test_eq(2, socks->replylen);
test_eq(5, socks->reply[0]);
test_eq(0, socks->reply[1]);
tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks),==, 0);
tt_int_op(0,==, buf_datalen(buf));
tt_int_op(5,==, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(5,==, socks->reply[0]);
tt_int_op(0,==, socks->reply[1]);
ADD_DATA(buf, "\x05\x02\x00\x01\x02\x02\x02\x01\x01\x01");
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks), -1);
tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks),==, -1);
/* XXX: shouldn't tor reply 'command not supported' [07]? */
buf_clear(buf);
@ -150,15 +150,15 @@ test_socks_5_unsupported_commands(void *ptr)
/* SOCKS 5 Send unsupported UDP_ASSOCIATE [03] command */
ADD_DATA(buf, "\x05\x03\x00\x01\x02");
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks), 0);
test_eq(5, socks->socks_version);
test_eq(2, socks->replylen);
test_eq(5, socks->reply[0]);
test_eq(2, socks->reply[1]);
tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks),==, 0);
tt_int_op(5,==, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(5,==, socks->reply[0]);
tt_int_op(2,==, socks->reply[1]);
ADD_DATA(buf, "\x05\x03\x00\x01\x02\x02\x02\x01\x01\x01");
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks), -1);
tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks),==, -1);
/* XXX: shouldn't tor reply 'command not supported' [07]? */
done:
@ -173,64 +173,64 @@ test_socks_5_supported_commands(void *ptr)
/* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
ADD_DATA(buf, "\x05\x01\x00");
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks), 0);
test_eq(5, socks->socks_version);
test_eq(2, socks->replylen);
test_eq(5, socks->reply[0]);
test_eq(0, socks->reply[1]);
tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks),==, 0);
tt_int_op(5,==, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(5,==, socks->reply[0]);
tt_int_op(0,==, socks->reply[1]);
ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks), 1);
test_streq("2.2.2.2", socks->address);
test_eq(4369, socks->port);
tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks),==, 1);
tt_str_op("2.2.2.2",==, socks->address);
tt_int_op(4369,==, socks->port);
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
socks_request_clear(socks);
/* SOCKS 5 Send CONNECT [01] to FQDN torproject.org:4369 */
ADD_DATA(buf, "\x05\x01\x00");
ADD_DATA(buf, "\x05\x01\x00\x03\x0Etorproject.org\x11\x11");
test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks), 1);
tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks),==, 1);
test_eq(5, socks->socks_version);
test_eq(2, socks->replylen);
test_eq(5, socks->reply[0]);
test_eq(0, socks->reply[1]);
test_streq("torproject.org", socks->address);
test_eq(4369, socks->port);
tt_int_op(5,==, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(5,==, socks->reply[0]);
tt_int_op(0,==, socks->reply[1]);
tt_str_op("torproject.org",==, socks->address);
tt_int_op(4369,==, socks->port);
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
socks_request_clear(socks);
/* SOCKS 5 Send RESOLVE [F0] request for torproject.org:4369 */
ADD_DATA(buf, "\x05\x01\x00");
ADD_DATA(buf, "\x05\xF0\x00\x03\x0Etorproject.org\x01\x02");
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks) == 1);
test_eq(5, socks->socks_version);
test_eq(2, socks->replylen);
test_eq(5, socks->reply[0]);
test_eq(0, socks->reply[1]);
test_streq("torproject.org", socks->address);
tt_int_op(5,==, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(5,==, socks->reply[0]);
tt_int_op(0,==, socks->reply[1]);
tt_str_op("torproject.org",==, socks->address);
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
socks_request_clear(socks);
/* SOCKS 5 Send RESOLVE_PTR [F1] for IP address 2.2.2.5 */
ADD_DATA(buf, "\x05\x01\x00");
ADD_DATA(buf, "\x05\xF1\x00\x01\x02\x02\x02\x05\x01\x03");
test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
get_options()->SafeSocks) == 1);
test_eq(5, socks->socks_version);
test_eq(2, socks->replylen);
test_eq(5, socks->reply[0]);
test_eq(0, socks->reply[1]);
test_streq("2.2.2.5", socks->address);
tt_int_op(5,==, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(5,==, socks->reply[0]);
tt_int_op(0,==, socks->reply[1]);
tt_str_op("2.2.2.5",==, socks->address);
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
done:
;
@ -244,30 +244,30 @@ test_socks_5_no_authenticate(void *ptr)
/*SOCKS 5 No Authentication */
ADD_DATA(buf,"\x05\x01\x00");
test_assert(!fetch_from_buf_socks(buf, socks,
tt_assert(!fetch_from_buf_socks(buf, socks,
get_options()->TestSocks,
get_options()->SafeSocks));
test_eq(2, socks->replylen);
test_eq(5, socks->reply[0]);
test_eq(SOCKS_NO_AUTH, socks->reply[1]);
tt_int_op(2,==, socks->replylen);
tt_int_op(5,==, socks->reply[0]);
tt_int_op(SOCKS_NO_AUTH,==, socks->reply[1]);
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
/*SOCKS 5 Send username/password anyway - pretend to be broken */
ADD_DATA(buf,"\x01\x02\x01\x01\x02\x01\x01");
test_assert(!fetch_from_buf_socks(buf, socks,
tt_assert(!fetch_from_buf_socks(buf, socks,
get_options()->TestSocks,
get_options()->SafeSocks));
test_eq(5, socks->socks_version);
test_eq(2, socks->replylen);
test_eq(1, socks->reply[0]);
test_eq(0, socks->reply[1]);
tt_int_op(5,==, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(1,==, socks->reply[0]);
tt_int_op(0,==, socks->reply[1]);
test_eq(2, socks->usernamelen);
test_eq(2, socks->passwordlen);
tt_int_op(2,==, socks->usernamelen);
tt_int_op(2,==, socks->passwordlen);
test_memeq("\x01\x01", socks->username, 2);
test_memeq("\x01\x01", socks->password, 2);
tt_mem_op("\x01\x01",==, socks->username, 2);
tt_mem_op("\x01\x01",==, socks->password, 2);
done:
;
@ -282,31 +282,31 @@ test_socks_5_authenticate(void *ptr)
/* SOCKS 5 Negotiate username/password authentication */
ADD_DATA(buf, "\x05\x01\x02");
test_assert(!fetch_from_buf_socks(buf, socks,
tt_assert(!fetch_from_buf_socks(buf, socks,
get_options()->TestSocks,
get_options()->SafeSocks));
test_eq(2, socks->replylen);
test_eq(5, socks->reply[0]);
test_eq(SOCKS_USER_PASS, socks->reply[1]);
test_eq(5, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(5,==, socks->reply[0]);
tt_int_op(SOCKS_USER_PASS,==, socks->reply[1]);
tt_int_op(5,==, socks->socks_version);
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
/* SOCKS 5 Send username/password */
ADD_DATA(buf, "\x01\x02me\x08mypasswd");
test_assert(!fetch_from_buf_socks(buf, socks,
tt_assert(!fetch_from_buf_socks(buf, socks,
get_options()->TestSocks,
get_options()->SafeSocks));
test_eq(5, socks->socks_version);
test_eq(2, socks->replylen);
test_eq(1, socks->reply[0]);
test_eq(0, socks->reply[1]);
tt_int_op(5,==, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(1,==, socks->reply[0]);
tt_int_op(0,==, socks->reply[1]);
test_eq(2, socks->usernamelen);
test_eq(8, socks->passwordlen);
tt_int_op(2,==, socks->usernamelen);
tt_int_op(8,==, socks->passwordlen);
test_memeq("me", socks->username, 2);
test_memeq("mypasswd", socks->password, 8);
tt_mem_op("me",==, socks->username, 2);
tt_mem_op("mypasswd",==, socks->password, 8);
done:
;
@ -321,34 +321,34 @@ test_socks_5_authenticate_with_data(void *ptr)
/* SOCKS 5 Negotiate username/password authentication */
ADD_DATA(buf, "\x05\x01\x02");
test_assert(!fetch_from_buf_socks(buf, socks,
tt_assert(!fetch_from_buf_socks(buf, socks,
get_options()->TestSocks,
get_options()->SafeSocks));
test_eq(2, socks->replylen);
test_eq(5, socks->reply[0]);
test_eq(SOCKS_USER_PASS, socks->reply[1]);
test_eq(5, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(5,==, socks->reply[0]);
tt_int_op(SOCKS_USER_PASS,==, socks->reply[1]);
tt_int_op(5,==, socks->socks_version);
test_eq(0, buf_datalen(buf));
tt_int_op(0,==, buf_datalen(buf));
/* SOCKS 5 Send username/password */
/* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
ADD_DATA(buf, "\x01\x02me\x03you\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
test_assert(fetch_from_buf_socks(buf, socks,
tt_assert(fetch_from_buf_socks(buf, socks,
get_options()->TestSocks,
get_options()->SafeSocks) == 1);
test_eq(5, socks->socks_version);
test_eq(2, socks->replylen);
test_eq(1, socks->reply[0]);
test_eq(0, socks->reply[1]);
tt_int_op(5,==, socks->socks_version);
tt_int_op(2,==, socks->replylen);
tt_int_op(1,==, socks->reply[0]);
tt_int_op(0,==, socks->reply[1]);
test_streq("2.2.2.2", socks->address);
test_eq(4369, socks->port);
tt_str_op("2.2.2.2",==, socks->address);
tt_int_op(4369,==, socks->port);
test_eq(2, socks->usernamelen);
test_eq(3, socks->passwordlen);
test_memeq("me", socks->username, 2);
test_memeq("you", socks->password, 3);
tt_int_op(2,==, socks->usernamelen);
tt_int_op(3,==, socks->passwordlen);
tt_mem_op("me",==, socks->username, 2);
tt_mem_op("you",==, socks->password, 3);
done:
;
@ -362,13 +362,13 @@ test_socks_5_auth_before_negotiation(void *ptr)
/* SOCKS 5 Send username/password */
ADD_DATA(buf, "\x01\x02me\x02me");
test_assert(fetch_from_buf_socks(buf, socks,
tt_assert(fetch_from_buf_socks(buf, socks,
get_options()->TestSocks,
get_options()->SafeSocks) == -1);
test_eq(0, socks->socks_version);
test_eq(0, socks->replylen);
test_eq(0, socks->reply[0]);
test_eq(0, socks->reply[1]);
tt_int_op(0,==, socks->socks_version);
tt_int_op(0,==, socks->replylen);
tt_int_op(0,==, socks->reply[0]);
tt_int_op(0,==, socks->reply[1]);
done:
;

File diff suppressed because it is too large Load Diff