Skip to content

Commit

Permalink
Make more functions take const pointers to IP_Port
Browse files Browse the repository at this point in the history
We additionally now make local copies of the IP_Port param instead
of modifying the passed argument
  • Loading branch information
JFreegman committed Feb 3, 2022
1 parent ce7f51a commit 740de6f
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 154 deletions.
28 changes: 14 additions & 14 deletions auto_tests/dht_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static void test_addto_lists_update(DHT *dht,
ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port);

random_bytes(test_id, sizeof(test_id));
used = addto_lists(dht, test_ipp, test_id);
used = addto_lists(dht, &test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
// it is possible to have ip_port duplicates in the list, so ip_port @ found not always equal to ip_port @ test
found = client_in_list(list, length, test_id);
Expand All @@ -107,7 +107,7 @@ static void test_addto_lists_update(DHT *dht,
test_ipp.port = random_u32() % TOX_PORT_DEFAULT;
id_copy(test_id, list[test].public_key);

used = addto_lists(dht, test_ipp, test_id);
used = addto_lists(dht, &test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
// it is not possible to have id duplicates in the list, so id @ found must be equal id @ test
ck_assert_msg(client_in_list(list, length, test_id) == test, "Client id is not in the list");
Expand All @@ -127,7 +127,7 @@ static void test_addto_lists_update(DHT *dht,
list[test2].assoc4.ip_port.port = -1;
}

used = addto_lists(dht, test_ipp, test_id);
used = addto_lists(dht, &test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
ck_assert_msg(client_in_list(list, length, test_id) == test2, "Client id is not in the list");
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port),
Expand All @@ -146,7 +146,7 @@ static void test_addto_lists_update(DHT *dht,
list[test1].assoc4.ip_port.port = -1;
}

used = addto_lists(dht, test_ipp, test_id);
used = addto_lists(dht, &test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
ck_assert_msg(client_in_list(list, length, test_id) == test1, "Client id is not in the list");
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port),
Expand Down Expand Up @@ -188,7 +188,7 @@ static void test_addto_lists_bad(DHT *dht,
}

ip_port->port += 1;
used = addto_lists(dht, *ip_port, public_key);
used = addto_lists(dht, ip_port, public_key);
ck_assert_msg(used >= 1, "Wrong number of added clients");

ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Client id is not in the list");
Expand All @@ -213,7 +213,7 @@ static void test_addto_lists_good(DHT *dht,
} while (is_furthest(comp_client_id, list, length, public_key));

ip_port->port += 1;
addto_lists(dht, *ip_port, public_key);
addto_lists(dht, ip_port, public_key);
ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Good client id is not in the list");

// check "good" client id skip
Expand All @@ -222,7 +222,7 @@ static void test_addto_lists_good(DHT *dht,
} while (!is_furthest(comp_client_id, list, length, public_key));

ip_port->port += 1;
addto_lists(dht, *ip_port, public_key);
addto_lists(dht, ip_port, public_key);
ck_assert_msg(client_in_list(list, length, public_key) == -1, "Good client id is in the list");
}

Expand All @@ -239,7 +239,7 @@ static void test_addto_lists(IP ip)
Mono_Time *mono_time = mono_time_new();
ck_assert_msg(mono_time != nullptr, "Failed to create Mono_Time");

Networking_Core *net = new_networking(log, ip, TOX_PORT_DEFAULT);
Networking_Core *net = new_networking(log, &ip, TOX_PORT_DEFAULT);
ck_assert_msg(net != nullptr, "Failed to create Networking_Core");

DHT *dht = new_dht(log, mono_time, net, true);
Expand All @@ -254,20 +254,20 @@ static void test_addto_lists(IP ip)
// check lists filling
for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
random_bytes(public_key, sizeof(public_key));
used = addto_lists(dht, ip_port, public_key);
used = addto_lists(dht, &ip_port, public_key);
ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing ip_port");
}

for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
ip_port.port += 1;
used = addto_lists(dht, ip_port, public_key);
used = addto_lists(dht, &ip_port, public_key);
ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing public_key");
}

for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
ip_port.port += 1;
random_bytes(public_key, sizeof(public_key));
used = addto_lists(dht, ip_port, public_key);
used = addto_lists(dht, &ip_port, public_key);
ck_assert_msg(used >= 1, "Wrong number of added clients");
}

Expand Down Expand Up @@ -379,7 +379,7 @@ static void test_list_main(void)

mono_times[i] = mono_time_new();

dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true);
dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], &ip, DHT_DEFAULT_PORT + i), true);
ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i,
"Bound to wrong port: %d", net_port(dhts[i]->net));
Expand All @@ -402,7 +402,7 @@ static void test_list_main(void)
ip_port.ip.ip.v4.uint32 = random_u32();
ip_port.port = random_u32() % (UINT16_MAX - 1);
++ip_port.port;
addto_lists(dhts[i], ip_port, dhts[j]->self_public_key);
addto_lists(dhts[i], &ip_port, dhts[j]->self_public_key);
}
}

Expand Down Expand Up @@ -536,7 +536,7 @@ static void test_DHT_test(void)
clock[i] = current_time_monotonic(mono_times[i]);
mono_time_set_current_time_callback(mono_times[i], get_clock_callback, &clock[i]);

dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true);
dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], &ip, DHT_DEFAULT_PORT + i), true);
ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port");
}
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/network_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static void test_ip_equal(void)
ip2.ip.v6.uint32[2] = net_htonl(0xFFFF);
ip2.ip.v6.uint32[3] = net_htonl(0x7F000001);

ck_assert_msg(ipv6_ipv4_in_v6(ip2.ip.v6) != 0,
ck_assert_msg(ipv6_ipv4_in_v6(&ip2.ip.v6) != 0,
"ipv6_ipv4_in_v6(::ffff:127.0.0.1): expected != 0, got 0.");

res = ip_equal(&ip1, &ip2);
Expand Down
8 changes: 4 additions & 4 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ static void test_basic(void)
Mono_Time *mono_time2 = mono_time_new();

IP ip = get_loopback();
Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, mono_time1, new_networking(log1, ip, 36567), true));
Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, mono_time2, new_networking(log2, ip, 36568), true));
Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, mono_time1, new_networking(log1, &ip, 36567), true));
Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, mono_time2, new_networking(log2, &ip, 36568), true));
ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2);

Expand Down Expand Up @@ -270,7 +270,7 @@ static void test_basic(void)

Mono_Time *mono_time3 = mono_time_new();

Onion *onion3 = new_onion(log3, mono_time3, new_dht(log3, mono_time3, new_networking(log3, ip, 36569), true));
Onion *onion3 = new_onion(log3, mono_time3, new_dht(log3, mono_time3, new_networking(log3, &ip, 36569), true));
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");

random_nonce(nonce);
Expand Down Expand Up @@ -362,7 +362,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index)
return nullptr;
}

Networking_Core *net = new_networking(on->log, ip, port);
Networking_Core *net = new_networking(on->log, &ip, port);

if (!net) {
mono_time_free(on->mono_time);
Expand Down
Loading

0 comments on commit 740de6f

Please sign in to comment.