Skip to content

Commit

Permalink
cleanup: Move test-only functions into tests.
Browse files Browse the repository at this point in the history
Also inlined `new_networking` in most places.
  • Loading branch information
iphydf committed Mar 3, 2022
1 parent 2cc8daf commit 21688d1
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 73 deletions.
36 changes: 28 additions & 8 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ static int handle_test_4(void *object, const IP_Port *source, const uint8_t *pac
return 0;
}

/** Create and send a onion packet.
*
* Use Onion_Path path to send data of length to dest.
* Maximum length of data is ONION_MAX_DATA_SIZE.
*/
static void send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, uint16_t length)
{
uint8_t packet[ONION_MAX_PACKET_SIZE];
const int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length);
ck_assert_msg(len != -1, "failed to create onion packet");
ck_assert_msg(sendpacket(net, &path->ip_port1, packet, len) == len, "failed to send onion packet");
}

/** Initialize networking.
* Added for reverse compatibility with old new_networking calls.
*/
static Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port)
{
return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr);
}

static void test_basic(void)
{
uint32_t index[] = { 1, 2, 3 };
Expand Down Expand Up @@ -203,8 +224,7 @@ static void test_basic(void)
nodes[3] = n2;
Onion_Path path;
create_onion_path(onion1->dht, &path, nodes);
int ret = send_onion_packet(onion1->net, &path, &nodes[3].ip_port, req_packet, sizeof(req_packet));
ck_assert_msg(ret == 0, "Failed to create/send onion packet.");
send_onion_packet(onion1->net, &path, &nodes[3].ip_port, req_packet, sizeof(req_packet));

handled_test_1 = 0;

Expand All @@ -230,12 +250,12 @@ static void test_basic(void)
uint64_t s;
memcpy(&s, sb_data, sizeof(uint64_t));
memcpy(test_3_pub_key, nodes[3].public_key, CRYPTO_PUBLIC_KEY_SIZE);
ret = send_announce_request(onion1->net, &path, &nodes[3],
dht_get_self_public_key(onion1->dht),
dht_get_self_secret_key(onion1->dht),
zeroes,
dht_get_self_public_key(onion1->dht),
dht_get_self_public_key(onion1->dht), s);
int ret = send_announce_request(onion1->net, &path, &nodes[3],
dht_get_self_public_key(onion1->dht),
dht_get_self_secret_key(onion1->dht),
zeroes,
dht_get_self_public_key(onion1->dht),
dht_get_self_public_key(onion1->dht), s);
ck_assert_msg(ret == 0, "Failed to create/send onion announce_request packet.");
handled_test_3 = 0;

Expand Down
4 changes: 3 additions & 1 deletion auto_tests/tox_dispatch_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ static void test_tox_events(void)
ck_assert_msg(toxes[i] != nullptr, "failed to create tox instances %u", i);
}

Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
Tox_Err_Dispatch_New err_new;
Tox_Dispatch *dispatch = tox_dispatch_new(&err_new);
ck_assert_msg(dispatch != nullptr, "failed to create event dispatcher");
ck_assert(err_new == TOX_ERR_DISPATCH_NEW_OK);

tox_events_callback_friend_message(dispatch, handle_events_friend_message);

Expand Down
2 changes: 1 addition & 1 deletion other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ int main(int argc, char *argv[])
}

Mono_Time *mono_time = mono_time_new();
DHT *dht = new_dht(logger, mono_time, new_networking(logger, &ip, PORT), true);
DHT *dht = new_dht(logger, mono_time, new_networking_ex(logger, &ip, PORT, PORT, nullptr), true);
Onion *onion = new_onion(logger, mono_time, dht);
const Onion_Announce *onion_a = new_onion_announce(logger, mono_time, dht);

Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ac88db3ee0c9ca621846897bb508c469eba97c263637966d74483e266b5564b0 /usr/local/bin/tox-bootstrapd
8174a792e8e5092b6bb322b612534db544c25f9a448f55723a12d328101000c9 /usr/local/bin/tox-bootstrapd
15 changes: 8 additions & 7 deletions other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ int main(int argc, char *argv[])

char *pid_file_path = nullptr;
char *keys_file_path = nullptr;
int port;
int start_port;
int enable_ipv6;
int enable_ipv4_fallback;
int enable_lan_discovery;
Expand All @@ -244,16 +244,16 @@ int main(int argc, char *argv[])
int enable_motd;
char *motd = nullptr;

if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_ipv4_fallback,
if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &start_port, &enable_ipv6, &enable_ipv4_fallback,
&enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) {
log_write(LOG_LEVEL_INFO, "General config read successfully\n");
} else {
log_write(LOG_LEVEL_ERROR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
return 1;
}

if (port < MIN_ALLOWED_PORT || port > MAX_ALLOWED_PORT) {
log_write(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT,
if (start_port < MIN_ALLOWED_PORT || start_port > MAX_ALLOWED_PORT) {
log_write(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", start_port, MIN_ALLOWED_PORT,
MAX_ALLOWED_PORT);
free(motd);
free(tcp_relay_ports);
Expand All @@ -277,14 +277,15 @@ int main(int argc, char *argv[])
logger_callback_log(logger, toxcore_logger_callback, nullptr, nullptr);
}

Networking_Core *net = new_networking(logger, &ip, port);
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
Networking_Core *net = new_networking_ex(logger, &ip, start_port, end_port, nullptr);

if (net == nullptr) {
if (enable_ipv6 && enable_ipv4_fallback) {
log_write(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n");
enable_ipv6 = 0;
ip_init(&ip, enable_ipv6);
net = new_networking(logger, &ip, port);
net = new_networking_ex(logger, &ip, start_port, end_port, nullptr);

if (net == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n");
Expand Down Expand Up @@ -470,7 +471,7 @@ int main(int argc, char *argv[])
print_public_key(dht_get_self_public_key(dht));

uint64_t last_LANdiscovery = 0;
const uint16_t net_htons_port = net_htons(port);
const uint16_t net_htons_port = net_htons(start_port);

int waiting_for_dht_connection = 1;

Expand Down
12 changes: 0 additions & 12 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,14 +813,6 @@ static void at_shutdown(void)
}
#endif

/** Initialize networking.
* Added for reverse compatibility with old new_networking calls.
*/
Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port)
{
return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr);
}

/** Initialize networking.
* Bind to ip and port.
* ip must be in network order EX: 127.0.0.1 = (7F000001).
Expand Down Expand Up @@ -1280,17 +1272,13 @@ bool ip_parse_addr(const IP *ip, char *address, size_t length)

if (net_family_is_ipv4(ip->family)) {
struct in_addr addr;
static_assert(sizeof(addr) == sizeof(ip->ip.v4.uint32),
"assumption does not hold: in_addr should be 4 bytes");
assert(make_family(ip->family) == AF_INET);
fill_addr4(&ip->ip.v4, &addr);
return inet_ntop4(&addr, address, length) != nullptr;
}

if (net_family_is_ipv6(ip->family)) {
struct in6_addr addr;
static_assert(sizeof(addr) == sizeof(ip->ip.v6.uint8),
"assumption does not hold: in6_addr should be 16 bytes");
assert(make_family(ip->family) == AF_INET6);
fill_addr6(&ip->ip.v6, &addr);
return inet_ntop6(&addr, address, length) != nullptr;
Expand Down
5 changes: 0 additions & 5 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,6 @@ char *net_new_strerror(int error);
non_null()
void net_kill_strerror(char *strerror);

/** Initialize networking.
* Added for reverse compatibility with old new_networking calls.
*/
non_null()
Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port);
/** Initialize networking.
* Bind to ip and port.
* ip must be in network order EX: 127.0.0.1 = (7F000001).
Expand Down
25 changes: 0 additions & 25 deletions toxcore/onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,31 +268,6 @@ int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const O
return CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE + len;
}

/** Create and send a onion packet.
*
* Use Onion_Path path to send data of length to dest.
* Maximum length of data is ONION_MAX_DATA_SIZE.
*
* return -1 on failure.
* return 0 on success.
*/
int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data,
uint16_t length)
{
uint8_t packet[ONION_MAX_PACKET_SIZE];
const int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length);

if (len == -1) {
return -1;
}

if (sendpacket(net, &path->ip_port1, packet, len) != len) {
return -1;
}

return 0;
}

/** Create and send a onion response sent initially to dest with.
* Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
*
Expand Down
14 changes: 1 addition & 13 deletions toxcore/onion.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,6 @@ non_null()
int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, const IP_Port *dest,
const uint8_t *data, uint16_t length);

/** Create and send a onion packet.
*
* Use Onion_Path path to send data of length to dest.
* Maximum length of data is ONION_MAX_DATA_SIZE.
*
* return -1 on failure.
* return 0 on success.
*/
non_null()
int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data,
uint16_t length);

/** Create and send a onion response sent initially to dest with.
* Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
*
Expand All @@ -137,7 +125,7 @@ non_null()
int send_onion_response(const Networking_Core *net, const IP_Port *dest, const uint8_t *data, uint16_t length,
const uint8_t *ret);

/** Function to handle/send received decrypted versions of the packet sent with send_onion_packet.
/** Function to handle/send received decrypted versions of the packet created by create_onion_packet.
*
* return 0 on success.
* return 1 on failure.
Expand Down
3 changes: 3 additions & 0 deletions toxcore/tox_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Tox_Dispatch *tox_dispatch_new(Tox_Err_Dispatch_New *error)
*dispatch = (Tox_Dispatch) {
nullptr
};
if (error != nullptr) {
*error = TOX_ERR_DISPATCH_NEW_OK;
}
return dispatch;
}

Expand Down

0 comments on commit 21688d1

Please sign in to comment.