Skip to content

Commit

Permalink
Merge a3b21b4 into 37d4a0b
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Apr 3, 2018
2 parents 37d4a0b + a3b21b4 commit 3ba95e8
Show file tree
Hide file tree
Showing 60 changed files with 18,995 additions and 1,382 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ cscope.files

# rpm
tox.spec

.idea/
24 changes: 21 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,27 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/friend_requests.c
toxcore/friend_requests.h)

# LAYER 6: Tox messenger
# LAYER 6: DHT based group chats
# ----------------------
set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/group_announce.c
toxcore/group_chats.c
toxcore/group_connection.c
toxcore/group_moderation.c)

# LAYER 7: Tox messenger
# ----------------------
set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/Messenger.c
toxcore/Messenger.h)

# LAYER 7: Group chats
# LAYER 8: Conferences: friend based group chats
# --------------------
set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/group.c
toxcore/group.h)

# LAYER 8: Public API
# LAYER 9: Public API
# -------------------
apidsl(toxcore/tox.api.h)
set(toxcore_SOURCES ${toxcore_SOURCES}
Expand Down Expand Up @@ -493,6 +501,7 @@ auto_test(dht MSVC_DONT_BUILD)
auto_test(encryptsave)
auto_test(file_transfer)
auto_test(friend_request)
auto_test(groupchat)
auto_test(lan_discovery)
auto_test(lossless_packet)
auto_test(lossy_packet)
Expand Down Expand Up @@ -597,3 +606,12 @@ target_link_modules(DHT_test toxcore)
add_executable(Messenger_test ${CPUFEATURES}
testing/Messenger_test.c)
target_link_modules(Messenger_test toxcore)

add_executable(group_announce_test testing/groupchats/group_announce_test.c)
target_link_modules(group_announce_test toxcore)

add_executable(group_bootstrap_n_chat testing/groupchats/group_bootstrap_n_chat.c)
target_link_modules(group_bootstrap_n_chat toxcore)

add_executable(group_newpeers_dos_attack testing/groupchats/group_newpeers_dos_attack.c)
target_link_modules(group_newpeers_dos_attack toxcore)
11 changes: 9 additions & 2 deletions auto_tests/Makefile.inc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if BUILD_TESTS

TESTS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test dht_autotest tox_strncasecmp_test
check_PROGRAMS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test dht_autotest tox_strncasecmp_test
TESTS = groupchat_test encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test dht_autotest tox_strncasecmp_test
check_PROGRAMS = groupchat_test encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test dht_autotest tox_strncasecmp_test

AUTOTEST_CFLAGS = \
$(LIBSODIUM_CFLAGS) \
Expand Down Expand Up @@ -114,5 +114,12 @@ tox_strncasecmp_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_strncasecmp_test_LDADD = $(AUTOTEST_LDADD)


groupchat_test_SOURCES = ../auto_tests/groupchat_test.c

groupchat_test_CFLAGS = $(AUTOTEST_CFLAGS)

groupchat_test_LDADD = $(AUTOTEST_LDADD)


EXTRA_DIST += $(top_srcdir)/auto_tests/check_compat.h
EXTRA_DIST += $(top_srcdir)/auto_tests/helpers.h
2 changes: 1 addition & 1 deletion auto_tests/conference_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "helpers.h"

#define NUM_GROUP_TOX 5
#define NUM_GROUP_TOX 16
#define GROUP_MESSAGE "Install Gentoo"

static void handle_self_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *user_data)
Expand Down
292 changes: 292 additions & 0 deletions auto_tests/groupchat_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif

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

#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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

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

#define NUM_GROUP_TOXES 5

#define PEER_LIMIT_1 NUM_GROUP_TOXES
#define PEER_LIMIT_2 1

#define PASSWORD "dadada"
#define PASS_LEN (sizeof(PASSWORD) - 1)

#define TOPIC1 "This kills the skype"
#define TOPIC1_LEN (sizeof(TOPIC1) - 1)

#define TOPIC2 "The interjection zone"
#define TOPIC2_LEN (sizeof(TOPIC2) - 1)

#define GROUP_NAME "The Gas Chamber"
#define GROUP_NAME_LEN (sizeof(GROUP_NAME) - 1)

/* Returns 0 if group state is equal to the state passed to this function.
* Returns negative integer if state is invalid.
*/
static int check_group_state(Tox *tox, uint32_t groupnumber, uint32_t peer_limit, TOX_GROUP_PRIVACY_STATE priv_state,
const uint8_t *password, size_t pass_len, const uint8_t *topic, size_t topic_len)
{
TOX_ERR_GROUP_STATE_QUERIES query_err;

TOX_GROUP_PRIVACY_STATE my_priv_state = tox_group_get_privacy_state(tox, groupnumber, &query_err);
ck_assert_msg(query_err == TOX_ERR_GROUP_STATE_QUERIES_OK, "Failed to get privacy state: %d", query_err);

if (my_priv_state != priv_state) {
return -1;
}

uint32_t my_peer_limit = tox_group_get_peer_limit(tox, groupnumber, &query_err);
ck_assert_msg(query_err == TOX_ERR_GROUP_STATE_QUERIES_OK, "Failed to get peer limit: %d", query_err);

if (my_peer_limit != peer_limit) {
return -2;
}

size_t my_topic_len = tox_group_get_topic_size(tox, groupnumber, &query_err);
ck_assert_msg(query_err == TOX_ERR_GROUP_STATE_QUERIES_OK, "Failed to get topic size: %d", query_err);

if (my_topic_len != topic_len) {
return -3;
}

VLA(uint8_t, my_topic, my_topic_len + 1);
tox_group_get_topic(tox, groupnumber, my_topic, &query_err);
my_topic[my_topic_len] = 0;
ck_assert_msg(query_err == TOX_ERR_GROUP_STATE_QUERIES_OK, "Failed to get topic: %d", query_err);

if (memcmp(my_topic, topic, my_topic_len) != 0) {
return -4;
}

size_t my_pass_len = tox_group_get_password_size(tox, groupnumber, &query_err);
ck_assert_msg(query_err == TOX_ERR_GROUP_STATE_QUERIES_OK, "Failed to get password size: %d", query_err);

if (my_pass_len != pass_len) {
return -5;
}

if (my_pass_len) {
VLA(uint8_t, my_pass, my_pass_len + 1);
tox_group_get_password(tox, groupnumber, my_pass, &query_err);
my_pass[my_pass_len] = 0;
ck_assert_msg(query_err == TOX_ERR_GROUP_STATE_QUERIES_OK, "Failed to get password: %d", query_err);

if (memcmp(my_pass, password, my_pass_len) != 0) {
return -6;
}
}

/* Group name should never change */
size_t my_gname_len = tox_group_get_name_size(tox, groupnumber, &query_err);
ck_assert_msg(query_err == TOX_ERR_GROUP_STATE_QUERIES_OK, "Failed to get group name size: %d", query_err);

if (my_gname_len != GROUP_NAME_LEN) {
return -7;
}

VLA(uint8_t, my_gname, my_gname_len + 1);
tox_group_get_name(tox, groupnumber, my_gname, &query_err);
my_gname[my_gname_len] = 0;

if (memcmp(my_gname, (const uint8_t *)GROUP_NAME, my_gname_len) != 0) {
return -8;
}

return 0;
}

static void set_group_state(Tox *tox, uint32_t groupnumber, uint32_t peer_limit, TOX_GROUP_PRIVACY_STATE priv_state,
const uint8_t *password, size_t pass_len, const uint8_t *topic, size_t topic_len)
{

TOX_ERR_GROUP_FOUNDER_SET_PEER_LIMIT limit_set_err;
tox_group_founder_set_peer_limit(tox, groupnumber, peer_limit, &limit_set_err);
ck_assert_msg(limit_set_err == TOX_ERR_GROUP_FOUNDER_SET_PEER_LIMIT_OK, "failed to set peer limit: %d", limit_set_err);

TOX_ERR_GROUP_FOUNDER_SET_PRIVACY_STATE priv_err;
tox_group_founder_set_privacy_state(tox, groupnumber, priv_state, &priv_err);
ck_assert_msg(priv_err == TOX_ERR_GROUP_FOUNDER_SET_PRIVACY_STATE_OK, "failed to set privacy state: %d", priv_err);

TOX_ERR_GROUP_FOUNDER_SET_PASSWORD pass_set_err;
tox_group_founder_set_password(tox, groupnumber, password, pass_len, &pass_set_err);
ck_assert_msg(pass_set_err == TOX_ERR_GROUP_FOUNDER_SET_PASSWORD_OK, "failed to set password: %d", pass_set_err);

TOX_ERR_GROUP_TOPIC_SET topic_set_err;
tox_group_set_topic(tox, groupnumber, topic, topic_len, &topic_set_err);
ck_assert_msg(topic_set_err == TOX_ERR_GROUP_TOPIC_SET_OK, "failed to set topic: %d", topic_set_err);
}

START_TEST(test_text_all)
{
#ifndef VANILLA_NACL
long long unsigned int cur_time = time(nullptr);
Tox *toxes[NUM_GROUP_TOXES];

ck_assert_msg(NUM_GROUP_TOXES >= 3, "NUM_GROUP_TOXES is too small: %d", NUM_GROUP_TOXES);

/* Init tox instances */
TOX_ERR_NEW error;
struct Tox_Options tox_opts;
tox_options_default(&tox_opts);

/* Tox0 is the bootstrap node */
toxes[0] = tox_new(&tox_opts, &error);

ck_assert_msg(error == TOX_ERR_NEW_OK, "tox_new failed to bootstrap: %d\n", error);

size_t i, count = 0;

for (i = 1; i < NUM_GROUP_TOXES; ++i) {
toxes[i] = tox_new(&tox_opts, &error);
ck_assert_msg(error == TOX_ERR_NEW_OK, "tox_new failed: %d\n", error);

char name[16];
snprintf(name, sizeof(name), "test-%zu", i);
tox_self_set_name(toxes[i], (const uint8_t *)name, strlen(name), nullptr);
}

while (1) {
for (i = 0; i < NUM_GROUP_TOXES; ++i) {
tox_iterate(toxes[i], nullptr);
}

count = 0;

for (i = 0; i < NUM_GROUP_TOXES; ++i) {
if (tox_self_get_connection_status(toxes[i])) {
++count;
}
}

if (count == NUM_GROUP_TOXES) {
break;
}

c_sleep(20);
}

printf("%zu Tox instances connected after %llu seconds!\n", count, time(nullptr) - cur_time);

/* Tox1 creates a group and is a founder of a newly created group */
TOX_ERR_GROUP_NEW new_err;
uint32_t groupnum = tox_group_new(toxes[1], TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *)GROUP_NAME, GROUP_NAME_LEN,
&new_err);
ck_assert_msg(new_err == TOX_ERR_GROUP_NEW_OK, "tox_group_new failed: %d", new_err);

/* Set default group state */
set_group_state(toxes[1], 0, PEER_LIMIT_1, TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *)PASSWORD, PASS_LEN,
(const uint8_t *)TOPIC1, TOPIC1_LEN);

for (i = 0; i < NUM_GROUP_TOXES; ++i) {
tox_iterate(toxes[i], nullptr);
}

/* Tox1 gets the Chat ID and implicitly shares it publicly */
TOX_ERR_GROUP_STATE_QUERIES id_err;
uint8_t chat_id[TOX_GROUP_CHAT_ID_SIZE];
tox_group_get_chat_id(toxes[1], groupnum, chat_id, &id_err);

ck_assert_msg(id_err == TOX_ERR_GROUP_STATE_QUERIES_OK, "tox_group_get_chat_id failed %d", id_err);

/* All other peers join the group using the Chat ID and password */
for (i = 2; i < NUM_GROUP_TOXES; ++i) {
TOX_ERR_GROUP_JOIN join_err;
tox_group_join(toxes[i], chat_id, (const uint8_t *)PASSWORD, PASS_LEN, &join_err);
ck_assert_msg(join_err == TOX_ERR_GROUP_JOIN_OK, "tox_group_join failed: %d", join_err);
c_sleep(1000);
}

/* Keep checking if all instances have connected to the group until test times out */
while (1) {
for (i = 0; i < NUM_GROUP_TOXES; ++i) {
tox_iterate(toxes[i], nullptr);
}

count = 0;

for (i = 1; i < NUM_GROUP_TOXES; ++i) {
if (tox_group_get_peer_limit(toxes[i], 0, nullptr) == PEER_LIMIT_1) {
++count;
}
}

if (count == NUM_GROUP_TOXES - 1) {
break;
}

c_sleep(20);
}

/* Check that all peers have the correct group state */
for (i = 1; i < NUM_GROUP_TOXES; ++i) {
tox_iterate(toxes[i], nullptr);
int ret = check_group_state(toxes[i], 0, PEER_LIMIT_1, TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *)PASSWORD,
PASS_LEN, (const uint8_t *)TOPIC1, TOPIC1_LEN);
ck_assert_msg(ret == 0, "Invalid group state: %d", ret);
c_sleep(20);
}

/* Change group state and check that all peers received the changes */
set_group_state(toxes[1], 0, PEER_LIMIT_2, TOX_GROUP_PRIVACY_STATE_PRIVATE, nullptr, 0, (const uint8_t *)TOPIC2,
TOPIC2_LEN);

while (1) {
count = 0;

for (i = 1; i < NUM_GROUP_TOXES; ++i) {
tox_iterate(toxes[i], nullptr);

if (check_group_state(toxes[i], 0, PEER_LIMIT_2, TOX_GROUP_PRIVACY_STATE_PRIVATE, nullptr, 0,
(const uint8_t *)TOPIC2, TOPIC2_LEN) == 0) {
++count;
}
}

if (count == NUM_GROUP_TOXES - 1) {
break;
}

c_sleep(20);
}

#endif /* VANILLA_NACL */
}
END_TEST

static Suite *text_groupchats_suite(void)
{
Suite *s = suite_create("text_groupchats");

DEFTESTCASE_SLOW(text_all, 80);
return s;
}

int main(void)
{
srand((unsigned int) time(nullptr));

Suite *tox = text_groupchats_suite();
SRunner *test_runner = srunner_create(tox);

srunner_run_all(test_runner, CK_NORMAL);
int number_failed = srunner_ntests_failed(test_runner);

srunner_free(test_runner);

return number_failed;
}

0 comments on commit 3ba95e8

Please sign in to comment.