Skip to content

Commit

Permalink
auto_test fixture and filenames
Browse files Browse the repository at this point in the history
Renamed a poorly named test, fixed up a few printf statements,
substituted some unsigned integers with fixed size counterparts,
and implemmented the auto_run_test.h fixture for the lossy and
lossless packet tests.
  • Loading branch information
hugbubby authored and iphydf committed Jul 28, 2018
1 parent c4d5840 commit 051eb1d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 135 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ auto_test(file_transfer)
auto_test(file_saving)
auto_test(friend_connection)
auto_test(friend_request)
auto_test(invalid_proxy)
auto_test(invalid_tcp_proxy)
auto_test(invalid_udp_proxy)
auto_test(lan_discovery)
auto_test(lossless_packet)
auto_test(lossy_packet)
Expand Down
14 changes: 7 additions & 7 deletions auto_tests/friend_request_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ static void accept_friend_request(Tox *tox, const uint8_t *public_key, const uin

static void test_friend_request(void)
{
printf("initialising 2 toxes\n");
printf("Initialising 2 toxes.\n");
uint32_t index[] = { 1, 2 };
const time_t cur_time = time(nullptr);
Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);

ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");

printf("bootstrapping tox2 off tox1\n");
printf("Bootstrapping tox2 off tox1.\n");
uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(tox1, dht_key);
const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
Expand All @@ -51,17 +51,17 @@ static void test_friend_request(void)
c_sleep(ITERATION_INTERVAL);
}

printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
printf("Toxes are online, took %ld seconds.\n", time(nullptr) - cur_time);
const time_t con_time = time(nullptr);

printf("tox1 adds tox2 as friend, tox2 accepts\n");
printf("Tox1 adds tox2 as friend, tox2 accepts.\n");
tox_callback_friend_request(tox2, accept_friend_request);

uint8_t address[TOX_ADDRESS_SIZE];
tox_self_get_address(tox2, address);

const uint32_t test = tox_friend_add(tox1, address, (const uint8_t *)FR_MESSAGE, sizeof(FR_MESSAGE), nullptr);
ck_assert_msg(test == 0, "Failed to add friend error code: %i", test);
ck_assert_msg(test == 0, "failed to add friend error code: %i", test);

while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
Expand All @@ -71,8 +71,8 @@ static void test_friend_request(void)
c_sleep(ITERATION_INTERVAL);
}

printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);
printf("test_friend_request succeeded, took %ld seconds\n", time(nullptr) - cur_time);
printf("Tox clients connected took %ld seconds.\n", time(nullptr) - con_time);
printf("friend_request_test succeeded, took %ld seconds.\n", time(nullptr) - cur_time);

tox_kill(tox1);
tox_kill(tox2);
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/invalid_tcp_proxy_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ int main(void)
tox_add_tcp_relay(tox, "tox.ngc.zone", 33445, key, nullptr);
tox_bootstrap(tox, "tox.ngc.zone", 33445, key, nullptr);

printf("Waiting for connection\n");
printf("Waiting for connection...\n");

for (unsigned i = 0; i < NUM_ITERATIONS; i++) {
for (uint16_t i = 0; i < NUM_ITERATIONS; i++) {
tox_iterate(tox, nullptr);
c_sleep(ITERATION_INTERVAL);
// None of the iterations should have a connection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ int main(void)
tox_add_tcp_relay(tox, "tox.ngc.zone", 33445, key, nullptr);
tox_bootstrap(tox, "tox.ngc.zone", 33445, key, nullptr);

printf("Waiting for connection");
printf("Waiting for connection...");

for (unsigned i = 0; i < NUM_ITERATIONS; i++) {
for (uint16_t i = 0; i < NUM_ITERATIONS; i++) {
tox_iterate(tox, nullptr);
c_sleep(ITERATION_INTERVAL);
// None of the iterations should have a connection.
Expand Down
84 changes: 22 additions & 62 deletions auto_tests/lossless_packet_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,92 +16,52 @@
#include "../toxcore/util.h"
#include "check_compat.h"

typedef struct State {
uint32_t index;
bool custom_packet_received;
} State;

#include "run_auto_test.h"

#define LOSSLESS_PACKET_FILLER 160

static void handle_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length,
void *user_data)
{
State *state = (State *)user_data;

uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE];
memset(cmp_packet, LOSSLESS_PACKET_FILLER, sizeof(cmp_packet));

if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) {
bool *custom_packet_received = (bool *)user_data;
*custom_packet_received = true;
state->custom_packet_received = true;
}
}

static void test_lossless_packet(void)
static void test_lossless_packet(Tox **toxes, State *state)
{
printf("initialising 2 toxes\n");
uint32_t index[] = { 1, 2 };
const time_t cur_time = time(nullptr);
Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);

ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");

printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_public_key(tox2, public_key);
tox_friend_add_norequest(tox1, public_key, nullptr);
tox_self_get_public_key(tox1, public_key);
tox_friend_add_norequest(tox2, public_key, nullptr);

printf("bootstrapping tox2 off tox1\n");
uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(tox1, dht_key);
const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);

tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);

while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) {
tox_iterate(tox1, nullptr);
tox_iterate(tox2, nullptr);

c_sleep(200);
}

printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
const time_t con_time = time(nullptr);

while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
tox_iterate(tox1, nullptr);
tox_iterate(tox2, nullptr);

c_sleep(200);
}

printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);

tox_callback_friend_lossless_packet(tox2, &handle_lossless_packet);
tox_callback_friend_lossless_packet(toxes[1], &handle_lossless_packet);
uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1];
memset(packet, LOSSLESS_PACKET_FILLER, sizeof(packet));
bool ret = tox_friend_send_lossless_packet(tox1, 0, packet, sizeof(packet), nullptr);
ck_assert_msg(ret == false, "tox_friend_send_lossless_packet bigger fail %i", ret);
ret = tox_friend_send_lossless_packet(tox1, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret);

bool received_lossless_packet = false;

while (!received_lossless_packet) {
tox_iterate(tox1, nullptr);
tox_iterate(tox2, &received_lossless_packet);
bool ret = tox_friend_send_lossless_packet(toxes[0], 0, packet, sizeof(packet), nullptr);
ck_assert_msg(ret == false, "should not be able to send custom packets this big %i", ret);

c_sleep(200);
}
ret = tox_friend_send_lossless_packet(toxes[0], 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret);

printf("test_lossless_packet succeeded, took %ld seconds\n", time(nullptr) - cur_time);
while (!state[1].custom_packet_received) {
tox_iterate(toxes[0], nullptr);
tox_iterate(toxes[1], &state[1]);

tox_kill(tox1);
tox_kill(tox2);
c_sleep(ITERATION_INTERVAL);
}
}

int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);

test_lossless_packet();
run_auto_test(2, test_lossless_packet);
return 0;
}
82 changes: 21 additions & 61 deletions auto_tests/lossy_packet_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
#include "../toxcore/util.h"
#include "check_compat.h"

typedef struct State {
uint32_t index;
bool custom_packet_received;
} State;

#include "run_auto_test.h"

#define LOSSY_PACKET_FILLER 200

static void handle_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data)
Expand All @@ -22,81 +29,34 @@ static void handle_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t
memset(cmp_packet, LOSSY_PACKET_FILLER, sizeof(cmp_packet));

if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) {
bool *custom_packet_received = (bool *)user_data;
*custom_packet_received = true;
State *state = (State *)user_data;
state->custom_packet_received = true;
}
}

static void test_lossy_packet(void)
static void test_lossy_packet(Tox **toxes, State *state)
{
printf("initialising 2 toxes\n");
uint32_t index[] = { 1, 2 };
const time_t cur_time = time(nullptr);
Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);

ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");

printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_public_key(tox2, public_key);
tox_friend_add_norequest(tox1, public_key, nullptr);
tox_self_get_public_key(tox1, public_key);
tox_friend_add_norequest(tox2, public_key, nullptr);

printf("bootstrapping tox2 off tox1\n");
uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(tox1, dht_key);
const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);

tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);

while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) {
tox_iterate(tox1, nullptr);
tox_iterate(tox2, nullptr);

c_sleep(200);
}

printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
const time_t con_time = time(nullptr);

while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
tox_iterate(tox1, nullptr);
tox_iterate(tox2, nullptr);
c_sleep(200);
}

printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);

tox_callback_friend_lossy_packet(tox2, &handle_lossy_packet);
tox_callback_friend_lossy_packet(toxes[1], &handle_lossy_packet);
uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1];
memset(packet, LOSSY_PACKET_FILLER, sizeof(packet));
bool ret = tox_friend_send_lossy_packet(tox1, 0, packet, sizeof(packet), nullptr);
ck_assert_msg(ret == false, "tox_friend_send_lossy_packet bigger fail %i", ret);
ret = tox_friend_send_lossy_packet(tox1, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret);

bool received_lossy_packet = false;
bool ret = tox_friend_send_lossy_packet(toxes[0], 0, packet, sizeof(packet), nullptr);
ck_assert_msg(ret == false, "should not be able to send custom packets this big %i", ret);

while (!received_lossy_packet) {
tox_iterate(tox1, nullptr);
tox_iterate(tox2, &received_lossy_packet);
c_sleep(200);
}

printf("test_lossy_packet succeeded, took %ld seconds\n", time(nullptr) - cur_time);
ret = tox_friend_send_lossy_packet(toxes[0], 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret);

tox_kill(tox1);
tox_kill(tox2);
while (!state[1].custom_packet_received) {
tox_iterate(toxes[0], nullptr);
tox_iterate(toxes[1], &state[1]);
c_sleep(ITERATION_INTERVAL);
}
}

int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);

test_lossy_packet();
run_auto_test(2, test_lossy_packet);
return 0;
}

0 comments on commit 051eb1d

Please sign in to comment.