Skip to content

Commit

Permalink
Merge 422334a into 8739f7f
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jul 5, 2018
2 parents 8739f7f + 422334a commit 2980bf4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 188 deletions.
142 changes: 38 additions & 104 deletions auto_tests/conference_double_invite_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,153 +2,87 @@
#define _XOPEN_SOURCE 600
#endif

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

#include <assert.h>
#include <stdlib.h>

#include "helpers.h"
#include <stdbool.h>
#include <stdint.h>

typedef struct State {
uint32_t id;
uint32_t index;
bool self_online;
bool friend_online;

bool joined;
uint32_t conference;
} State;

static void handle_self_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data)
{
State *state = (State *)user_data;

fprintf(stderr, "self_connection_status(#%u, %d, _)\n", state->id, connection_status);
state->self_online = connection_status != TOX_CONNECTION_NONE;
}

static void handle_friend_connection_status(Tox *tox, uint32_t friend_number, TOX_CONNECTION connection_status,
void *user_data)
{
State *state = (State *)user_data;

fprintf(stderr, "handle_friend_connection_status(#%u, %u, %d, _)\n", state->id, friend_number, connection_status);
state->friend_online = connection_status != TOX_CONNECTION_NONE;
}
#include "run_auto_test.h"

static void handle_conference_invite(Tox *tox, uint32_t friend_number, TOX_CONFERENCE_TYPE type, const uint8_t *cookie,
size_t length, void *user_data)
static void handle_conference_invite(
Tox *tox, uint32_t friend_number, TOX_CONFERENCE_TYPE type,
const uint8_t *cookie, size_t length, void *user_data)
{
State *state = (State *)user_data;

fprintf(stderr, "handle_conference_invite(#%u, %u, %d, uint8_t[%u], _)\n",
state->id, friend_number, type, (unsigned)length);
fprintf(stderr, "tox%u joining conference\n", state->id);
state->index, friend_number, type, (unsigned)length);
fprintf(stderr, "tox%u joining conference\n", state->index);

if (friend_number != -1) {
TOX_ERR_CONFERENCE_JOIN err;
state->conference = tox_conference_join(tox, friend_number, cookie, length, &err);
assert(err == TOX_ERR_CONFERENCE_JOIN_OK);
fprintf(stderr, "tox%u Joined conference %u\n", state->id, state->conference);
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK,
"attempting to join the conference returned with an error: %d", err);
fprintf(stderr, "tox%u joined conference %u\n", state->index, state->conference);
state->joined = true;
}
}

int main(void)
static void conference_double_invite_test(Tox **toxes, State *state)
{
setvbuf(stdout, nullptr, _IONBF, 0);

State state1 = {1};
State state2 = {2};

// Create toxes.
Tox *tox1 = tox_new_log(nullptr, nullptr, &state1.id);
Tox *tox2 = tox_new_log(nullptr, nullptr, &state2.id);

// tox1 <-> tox2
uint8_t key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_public_key(tox2, key);
tox_friend_add_norequest(tox1, key, nullptr); // tox1 -> tox2
tox_self_get_public_key(tox1, key);
tox_friend_add_norequest(tox2, key, nullptr); // tox2 -> tox1

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);

// Connection callbacks.
tox_callback_self_connection_status(tox1, handle_self_connection_status);
tox_callback_self_connection_status(tox2, handle_self_connection_status);

tox_callback_friend_connection_status(tox1, handle_friend_connection_status);
tox_callback_friend_connection_status(tox2, handle_friend_connection_status);

// Conference callbacks.
tox_callback_conference_invite(tox1, handle_conference_invite);
tox_callback_conference_invite(tox2, handle_conference_invite);

// Wait for self connection.
fprintf(stderr, "Waiting for toxes to come online\n");

while (!state1.self_online || !state2.self_online) {
tox_iterate(tox1, &state1);
tox_iterate(tox2, &state2);

c_sleep(100);
}

fprintf(stderr, "Toxes are online\n");

// Wait for friend connection.
fprintf(stderr, "Waiting for friends to connect\n");

while (!state1.friend_online || !state2.friend_online) {
tox_iterate(tox1, &state1);
tox_iterate(tox2, &state2);

c_sleep(100);
}

fprintf(stderr, "Friends are connected\n");
tox_callback_conference_invite(toxes[0], handle_conference_invite);
tox_callback_conference_invite(toxes[1], handle_conference_invite);

{
// Create new conference, tox1 is the founder.
// Create new conference, tox0 is the founder.
TOX_ERR_CONFERENCE_NEW err;
state1.conference = tox_conference_new(tox1, &err);
state1.joined = true;
assert(err == TOX_ERR_CONFERENCE_NEW_OK);
fprintf(stderr, "Created conference: id=%u\n", state1.conference);
state[0].conference = tox_conference_new(toxes[0], &err);
state[0].joined = true;
ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK,
"attempting to create a new conference returned with an error: %d", err);
fprintf(stderr, "Created conference: index=%u\n", state[0].conference);
}

{
// Invite friend.
TOX_ERR_CONFERENCE_INVITE err;
tox_conference_invite(tox1, 0, state1.conference, &err);
assert(err == TOX_ERR_CONFERENCE_INVITE_OK);
fprintf(stderr, "tox1 invited tox2\n");
tox_conference_invite(toxes[0], 0, state[0].conference, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK,
"attempting to invite a friend returned with an error: %d", err);
fprintf(stderr, "tox0 invited tox1\n");
}

fprintf(stderr, "Waiting for invitation to arrive\n");

while (!state1.joined || !state2.joined) {
tox_iterate(tox1, &state1);
tox_iterate(tox2, &state2);
while (!state[0].joined || !state[1].joined) {
tox_iterate(toxes[0], &state[0]);
tox_iterate(toxes[1], &state[1]);

c_sleep(100);
c_sleep(ITERATION_INTERVAL);
}

fprintf(stderr, "Invitations accepted\n");

// Invite one more time, resulting in friend -1 inviting tox2.
tox_conference_invite(tox1, 0, state1.conference, 0);
// Invite one more time, resulting in friend -1 inviting tox1 (toxes[1]).
tox_conference_invite(toxes[0], 0, state[0].conference, 0);

tox_iterate(tox1, &state1);
tox_iterate(tox2, &state2);
tox_iterate(toxes[0], &state[0]);
tox_iterate(toxes[1], &state[1]);
}

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

run_auto_test(2, conference_double_invite_test);
return 0;
}
4 changes: 1 addition & 3 deletions auto_tests/friend_connection_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
#define _XOPEN_SOURCE 600
#endif

#include "check_compat.h"

#include "../toxcore/tox.h"
#include <stdint.h>

typedef struct State {
uint32_t index;
Expand Down
4 changes: 1 addition & 3 deletions auto_tests/overflow_recvq_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#define _XOPEN_SOURCE 600
#endif

#include "check_compat.h"

#include "../toxcore/tox.h"
#include <stdint.h>

typedef struct State {
uint32_t index;
Expand Down
4 changes: 1 addition & 3 deletions auto_tests/overflow_sendq_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#define _XOPEN_SOURCE 600
#endif

#include "check_compat.h"

#include "../toxcore/tox.h"
#include <stdint.h>

typedef struct State {
uint32_t index;
Expand Down
3 changes: 3 additions & 0 deletions auto_tests/run_auto_test.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <stdlib.h> // calloc, free

#include "check_compat.h"
#include "helpers.h"

static bool all_connected(uint32_t tox_count, Tox **toxes)
Expand Down
96 changes: 21 additions & 75 deletions auto_tests/send_message_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@
#define _XOPEN_SOURCE 600
#endif

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "check_compat.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <stdint.h>

#include "../toxcore/ccompat.h"
#include "../toxcore/tox.h"
#include "../toxcore/util.h"
typedef struct State {
uint32_t index;
bool message_received;
} State;

#include "helpers.h"
#include "run_auto_test.h"

#define MESSAGE_FILLER 'G'

static void message_callback(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type, const uint8_t *string, size_t length,
void *userdata)
static void message_callback(
Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type,
const uint8_t *string, size_t length, void *userdata)
{
State *state = (State *)userdata;

if (type != TOX_MESSAGE_TYPE_NORMAL) {
ck_abort_msg("Bad type");
}
Expand All @@ -34,87 +31,36 @@ static void message_callback(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE typ
memset(cmp_msg, MESSAGE_FILLER, sizeof(cmp_msg));

if (length == TOX_MAX_MESSAGE_LENGTH && memcmp(string, cmp_msg, sizeof(cmp_msg)) == 0) {
bool *message_received = (bool *)userdata;
*message_received = true;
state->message_received = true;
}
}

static void test_send_message(void)
static void send_message_test(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(ITERATION_INTERVAL);
}

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(ITERATION_INTERVAL);
}

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

tox_callback_friend_message(tox2, &message_callback);
tox_callback_friend_message(toxes[1], &message_callback);

uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1];
memset(msgs, MESSAGE_FILLER, sizeof(msgs));

TOX_ERR_FRIEND_SEND_MESSAGE errm;
tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm);
tox_friend_send_message(toxes[0], 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm);
ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG, "TOX_MAX_MESSAGE_LENGTH is too small? error=%d", errm);

tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm);
tox_friend_send_message(toxes[0], 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm);
ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm);

bool message_received = false;

while (!message_received) {
tox_iterate(tox1, nullptr);
tox_iterate(tox2, &message_received);
while (!state[1].message_received) {
tox_iterate(toxes[0], &state[0]);
tox_iterate(toxes[1], &state[1]);

c_sleep(ITERATION_INTERVAL);
}

printf("tox clients messaging succeeded\n");

tox_kill(tox1);
tox_kill(tox2);
}

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

test_send_message();
run_auto_test(2, send_message_test);
return 0;
}

0 comments on commit 2980bf4

Please sign in to comment.