Skip to content

Commit

Permalink
Add a test to try and overflow the send queue in net_crypto.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jun 25, 2018
1 parent 29b2dd6 commit 1b918ca
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,15 @@ auto_test(crypto MSVC_DONT_BUILD)
auto_test(dht MSVC_DONT_BUILD)
auto_test(encryptsave)
auto_test(file_transfer)
auto_test(friend_connection)
auto_test(friend_request)
auto_test(invalid_proxy)
auto_test(invalid_tcp_proxy)
auto_test(lan_discovery)
auto_test(lossless_packet)
auto_test(lossy_packet)
auto_test(messenger MSVC_DONT_BUILD)
auto_test(net_crypto_overflow)
auto_test(network)
auto_test(onion)
auto_test(save_friend)
Expand Down
1 change: 1 addition & 0 deletions auto_tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cc_library(
hdrs = [
"check_compat.h",
"helpers.h",
"run_auto_test.h",
],
)

Expand Down
34 changes: 34 additions & 0 deletions auto_tests/friend_connection_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Tests that we can make a friend connection.
*
* This is the simplest test that brings up two toxes that can talk to each
* other. It's useful as a copy/pasteable starting point for testing other
* features.
*/

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif

#include "check_compat.h"

#include "../toxcore/tox.h"

typedef struct State
{
uint32_t index;
} State;

static void friend_connection_test(Tox *tox0, Tox *tox1, State *state)
{
// Nothing to do here. When copying this test, add test-specific code here.
}

#include "run_auto_test.h"

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

run_auto_test(friend_connection_test);
return 0;
}
35 changes: 35 additions & 0 deletions auto_tests/net_crypto_overflow_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Try to overflow the net_crypto packet buffer.
*/

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif

#include "check_compat.h"

#include "../toxcore/tox.h"

typedef struct State
{
uint32_t index;
} State;

static void net_crypto_overflow_test(Tox *tox0, Tox *tox1, State *state)
{
const uint8_t message[] = {0};
for (uint32_t i = 0; i < 32767; i++) {
TOX_ERR_FRIEND_SEND_MESSAGE err;
tox_friend_send_message(tox0, 0, TOX_MESSAGE_TYPE_NORMAL, message, sizeof message, &err);
ck_assert_msg(err == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "failed to send message number %d: %d", i, err);
}
}

#include "run_auto_test.h"

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

run_auto_test(net_crypto_overflow_test);
return 0;
}
50 changes: 50 additions & 0 deletions auto_tests/run_auto_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "helpers.h"

static void run_auto_test(void test(Tox *tox0, Tox *tox1, State *state))
{
printf("initialising 2 toxes\n");
State state[] = { {0}, {1} };
Tox *const tox0 = tox_new_log(nullptr, nullptr, &state[0].index);
Tox *const tox1 = tox_new_log(nullptr, nullptr, &state[1].index);

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

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

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

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

while (tox_self_get_connection_status(tox0) == TOX_CONNECTION_NONE ||
tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE) {
tox_iterate(tox0, &state[0]);
tox_iterate(tox1, &state[1]);

c_sleep(ITERATION_INTERVAL);
}

printf("toxes are online\n");

while (tox_friend_get_connection_status(tox0, 0, nullptr) != TOX_CONNECTION_UDP ||
tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP) {
tox_iterate(tox0, &state[0]);
tox_iterate(tox1, &state[1]);

c_sleep(ITERATION_INTERVAL);
}

printf("tox clients connected\n");

test(tox0, tox1, state);

tox_kill(tox0);
tox_kill(tox1);
}
6 changes: 6 additions & 0 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,18 +493,22 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con
uint32_t *message_id)
{
if (type > MESSAGE_ACTION) {
LOGGER_ERROR(m->log, "Message type %d is invalid", type);
return -5;
}

if (friend_not_valid(m, friendnumber)) {
LOGGER_ERROR(m->log, "Friend number %d is invalid", friendnumber);
return -1;
}

if (length >= MAX_CRYPTO_DATA_SIZE) {
LOGGER_ERROR(m->log, "Message length %d is too large", friendnumber);
return -2;
}

if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
LOGGER_ERROR(m->log, "Friend %d is not online", friendnumber);
return -3;
}

Expand All @@ -519,6 +523,8 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con
m->friendlist[friendnumber].friendcon_id), packet, length + 1, 0);

if (packet_num == -1) {
LOGGER_ERROR(m->log, "Failed to write crypto packet for message of length %d to friend %d",
length, friendnumber);
return -4;
}

Expand Down

0 comments on commit 1b918ca

Please sign in to comment.