From b3e02d9c9da0a508e52e60037f0f65ccf042f95d Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Wed, 7 Aug 2013 18:52:11 -0700 Subject: [PATCH 01/14] Adds full -D support to toxics versioning, includes the commit number --- testing/toxic/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt index f30d8e9e..38f02dc6 100644 --- a/testing/toxic/CMakeLists.txt +++ b/testing/toxic/CMakeLists.txt @@ -1,6 +1,9 @@ cmake_minimum_required(VERSION 2.6.0) project(toxic C) +execute_process(COMMAND git rev-list HEAD --count OUTPUT_VARIABLE COMMIT) +SET(GCC_COVERAGE_COMPILE_FLAGS '-DTOXICVER="0.1.1_r${COMMIT}"') +add_definitions(${GCC_COVERAGE_COMPILE_FLAGS}) set(exe_name toxic) add_executable(${exe_name} From 67c2b9fffd6d3ff12215d5cff82fb4b2c92739d4 Mon Sep 17 00:00:00 2001 From: stal Date: Wed, 7 Aug 2013 22:05:23 -0700 Subject: [PATCH 02/14] Re-export m_callback_userstatus --- core/Messenger.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/Messenger.h b/core/Messenger.h index d2fa8945..634b1c8b 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -188,6 +188,10 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); you are not responsible for freeing newstatus */ void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)); +/* set the callback for status type changes + function(int friendnumber, USERSTATUS kind) */ +void m_callback_userstatus(void (*function)(int, USERSTATUS)) + /* set the callback for read receipts function(int friendnumber, uint32_t receipt) if you are keeping a record of returns from m_sendmessage, From 0e03b049d8180d63b988e82f2be0567f32a8b5df Mon Sep 17 00:00:00 2001 From: stal Date: Wed, 7 Aug 2013 22:58:39 -0700 Subject: [PATCH 03/14] Reinstate semicolon --- core/Messenger.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Messenger.h b/core/Messenger.h index 634b1c8b..3a9a56df 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -190,7 +190,7 @@ void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)); /* set the callback for status type changes function(int friendnumber, USERSTATUS kind) */ -void m_callback_userstatus(void (*function)(int, USERSTATUS)) +void m_callback_userstatus(void (*function)(int, USERSTATUS)); /* set the callback for read receipts function(int friendnumber, uint32_t receipt) From 6f98fc47b02d70c2e2eb50d4bdc942286950cda5 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 8 Aug 2013 04:51:58 -0400 Subject: [PATCH 04/14] fix magic numbers --- testing/toxic/chat.c | 6 +++--- testing/toxic/main.c | 2 +- testing/toxic/prompt.c | 24 ++++++++++++------------ testing/toxic/windows.h | 4 +++- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 28c5de6c..56dfb050 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c @@ -16,7 +16,7 @@ typedef struct { int friendnum; - char line[256]; + char line[MAX_STR_SIZE]; size_t pos; WINDOW* history; WINDOW* linewin; @@ -178,9 +178,9 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd) } else if (!strcmp(cmd, "/myid")) { - char id[32*2 + 1] = {0}; + char id[KEY_SIZE_BYTES*2+1] = {0}; int i; - for (i = 0; i < 32; i++) { + for (i = 0; i < KEY_SIZE_BYTES; i++) { char xx[3]; snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); strcat(id, xx); diff --git a/testing/toxic/main.c b/testing/toxic/main.c index b2310c80..d1519556 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -37,7 +37,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length) wprintw(prompt->window, "\nFriend request from:\n"); int i; - for (i = 0; i < 32; ++i) { + for (i = 0; i < KEY_SIZE_BYTES; ++i) { wprintw(prompt->window, "%02x", public_key[i] & 0xff); } diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 661d881f..08874a9a 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -12,12 +12,12 @@ #include "windows.h" -uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX +uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX uint8_t num_requests=0; // XXX extern void on_friendadded(int friendnumber); static void print_usage(ToxWindow *self); -static char prompt_buf[256] = {0}; +static char prompt_buf[MAX_STR_SIZE] = {0}; static int prompt_buf_pos = 0; // XXX: @@ -43,7 +43,7 @@ unsigned char *hex_string_to_bin(char hex_string[]) static void execute(ToxWindow *self, char *u_cmd) { int newlines = 0; - char cmd[256] = {0}; + char cmd[MAX_STR_SIZE] = {0}; int i; for (i = 0; i < strlen(prompt_buf); ++i) { if (u_cmd[i] == '\n') @@ -53,9 +53,9 @@ static void execute(ToxWindow *self, char *u_cmd) } int leading_spc = 0; - for (i = 0; i < 256 && isspace(cmd[i]); ++i) + for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i) leading_spc++; - memmove(cmd, cmd + leading_spc, 256 - leading_spc); + memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc); int cmd_end = strlen(cmd); while (cmd_end > 0 && cmd_end--) @@ -109,7 +109,7 @@ static void execute(ToxWindow *self, char *u_cmd) } dht.port = htons(atoi(port)); - uint32_t resolved_address = resolve_addr(ip); + uintKEY_SIZE_BYTES_t resolved_address = resolve_addr(ip); if (resolved_address == 0) { return; } @@ -121,9 +121,9 @@ static void execute(ToxWindow *self, char *u_cmd) } else if (!strncmp(cmd, "add ", strlen("add "))) { - uint8_t id_bin[32]; + uint8_t id_bin[KEY_SIZE_BYTES]; char xx[3]; - uint32_t x; + uintKEY_SIZE_BYTES_t x; char *id = strchr(cmd, ' '); if (id == NULL) { wprintw(self->window, "Invalid syntax.\n"); @@ -136,12 +136,12 @@ static void execute(ToxWindow *self, char *u_cmd) msg++; } else msg = ""; - if (strlen(id) != 2*32) { + if (strlen(id) != 2*KEY_SIZE_BYTES) { wprintw(self->window, "Invalid ID length.\n"); return; } int i; - for (i = 0; i < 32; ++i) { + for (i = 0; i < KEY_SIZE_BYTES; ++i) { xx[0] = id[2*i]; xx[1] = id[2*i+1]; xx[2] = '\0'; @@ -251,9 +251,9 @@ static void execute(ToxWindow *self, char *u_cmd) } else if (!strcmp(cmd, "myid")) { - char id[32*2 + 1] = {0}; + char id[KEY_SIZE_BYTES*2 + 1] = {0}; size_t i; - for (i = 0; i < 32; ++i) { + for (i = 0; i < KEY_SIZE_BYTES; ++i) { char xx[3]; snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); strcat(id, xx); diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index cb45614d..287e534a 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -5,7 +5,9 @@ #include #define TOXWINDOWS_MAX_NUM 32 #define MAX_FRIENDS_NUM 100 - +#define MAX_STR_SIZE 256 +#define KEY_SIZE_BYTES 32 + /* number of permanent default windows */ #define N_DEFAULT_WINS 2 From 0f2b4e5c7790f5f35a7ddefc532551c928bd3f2f Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 8 Aug 2013 04:55:22 -0400 Subject: [PATCH 05/14] oops --- testing/toxic/prompt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 08874a9a..b50792fe 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -109,7 +109,7 @@ static void execute(ToxWindow *self, char *u_cmd) } dht.port = htons(atoi(port)); - uintKEY_SIZE_BYTES_t resolved_address = resolve_addr(ip); + uint32_t resolved_address = resolve_addr(ip); if (resolved_address == 0) { return; } @@ -123,7 +123,7 @@ static void execute(ToxWindow *self, char *u_cmd) else if (!strncmp(cmd, "add ", strlen("add "))) { uint8_t id_bin[KEY_SIZE_BYTES]; char xx[3]; - uintKEY_SIZE_BYTES_t x; + uint32_t x; char *id = strchr(cmd, ' '); if (id == NULL) { wprintw(self->window, "Invalid syntax.\n"); From 4efd97a6e2a4a3ca28b210f245167f7eeb293a15 Mon Sep 17 00:00:00 2001 From: panosalbanis Date: Thu, 8 Aug 2013 10:52:39 +0100 Subject: [PATCH 06/14] Added Vim swap files and ctags file --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.gitignore b/.gitignore index 0e4e35ef..47cfd84a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,12 @@ install_manifest.txt testing/data *~ +# Vim +*.swp + +# Ctags +tags + # Object files *.o From 1bbdd9d2dbbb0a0a7ae056699c487df00c55579e Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Thu, 8 Aug 2013 03:04:36 -0700 Subject: [PATCH 07/14] Merged upstream main.c changes --- testing/toxic/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 27e3d858..777ed80b 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -22,7 +22,7 @@ extern int add_req(uint8_t *public_key); // XXX /* Holds status of chat windows */ char WINDOW_STATUS[MAX_WINDOW_SLOTS]; -#define TOXICVER "0.1.0" //Will be moved to a -D flag later +//#define TOXICVER "0.1.0" //Will be moved to a -D flag later static ToxWindow windows[MAX_WINDOW_SLOTS]; static ToxWindow* prompt; @@ -69,7 +69,7 @@ void on_nickchange(int friendnumber, uint8_t *string, uint16_t length) } } -void on_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t *string, uint16_t length) +void on_statuschange(int friendnumber, uint8_t *string, uint16_t length) { wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); int i; @@ -113,7 +113,7 @@ static void init_tox() m_callback_friendrequest(on_request); m_callback_friendmessage(on_message); m_callback_namechange(on_nickchange); - m_callback_userstatus(on_statuschange); + m_callback_statusmessage(on_statuschange); } void init_window_status() From d9750569ac1ff49442ea5158b99e78fd24ec9715 Mon Sep 17 00:00:00 2001 From: Nominate Date: Thu, 8 Aug 2013 11:09:46 +0100 Subject: [PATCH 08/14] Removed superfluous statusmsg Users can just respecify their status with a message. This will also encourage users to think about which status is actually appropriate instead of just leaving the status alone. --- testing/toxic/prompt.c | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 661d881f..b3d6d229 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -228,17 +228,6 @@ static void execute(ToxWindow *self, char *u_cmd) } } - else if (!strncmp(cmd, "statusmsg ", strlen("statumsg "))) { - char *msg = strchr(cmd, ' '); - if (msg == NULL) { - wprintw(self->window, "Invalid syntax.\n"); - return; - } - msg++; - m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); - wprintw(self->window, "Status set to: %s\n", msg); - } - else if (!strncmp(cmd, "nick ", strlen("nick "))) { char *nick = strchr(cmd, ' '); if (nick == NULL) { @@ -372,7 +361,6 @@ static void print_usage(ToxWindow *self) wprintw(self->window, " connect : Connect to DHT server\n"); wprintw(self->window, " add : Add friend\n"); wprintw(self->window, " status : Set your status\n"); - wprintw(self->window, " statusmsg : Set your status\n"); wprintw(self->window, " nick : Set your nickname\n"); wprintw(self->window, " accept : Accept friend request\n"); wprintw(self->window, " myid : Print your ID\n"); From fde09712db40ec12b0383513adf89bc4398b327f Mon Sep 17 00:00:00 2001 From: Nominate Date: Thu, 8 Aug 2013 11:22:48 +0100 Subject: [PATCH 09/14] Updated chat.c to bring /status inline with prompt.c status command --- testing/toxic/chat.c | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 28c5de6c..669fd14d 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c @@ -154,15 +154,47 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd) } else if (!strncmp(cmd, "/status ", strlen("/status "))) { + char *status = strchr(cmd, ' '); char *msg; - msg = strchr(cmd, ' '); - if (msg == NULL) { + char *status_text; + if (status == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } - msg++; - m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); - wprintw(ctx->history, "Status set to: %s\n", msg); + status++; + USERSTATUS status_kind; + if (!strncmp(status, "online", strlen("online"))) { + status_kind = USERSTATUS_NONE; + status_text = "ONLINE"; + } + + else if (!strncmp(status, "away", strlen("away"))) { + status_kind = USERSTATUS_AWAY; + status_text = "AWAY"; + } + + else if (!strncmp(status, "busy", strlen("busy"))) { + status_kind = USERSTATUS_BUSY; + status_text = "BUSY"; + } + + else + { + wprintw(ctx->history, "Invalid status.\n"); + return; + } + + msg = strchr(status, ' '); + if (msg == NULL) { + m_set_userstatus(status_kind); + wprintw(ctx->history, "Status set to: %s\n", status_text); + } + else { + msg++; + m_set_userstatus(status_kind); + m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); + wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); + } } else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { @@ -231,7 +263,7 @@ void print_help(ChatContext *self) wprintw(self->history, "Commands:\n"); wattroff(self->history, A_BOLD); - wprintw(self->history, " /status : Set your status\n"); + wprintw(self->history, " /status : Set your status\n"); wprintw(self->history, " /nick : Set your nickname\n"); wprintw(self->history, " /myid : Print your ID\n"); wprintw(self->history, " /clear : Clear the screen\n"); From dff2493f2d0ae554cbcdb1c32b2472815ee536f7 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Thu, 8 Aug 2013 13:03:20 +0200 Subject: [PATCH 10/14] Use ctest to run unit tests --- auto_tests/CMakeLists.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/auto_tests/CMakeLists.txt b/auto_tests/CMakeLists.txt index 237dae1b..fbbcb7d7 100644 --- a/auto_tests/CMakeLists.txt +++ b/auto_tests/CMakeLists.txt @@ -6,3 +6,15 @@ include_directories(${CHECK_INCLUDE_DIRS}) find_package(Check REQUIRED) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/messenger_test.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/friends_test.cmake) + +include( CTest ) +enable_testing() + +add_test(messenger messenger_test) +# TODO enable once test is fixed +#add_test(friends friends_test) + +add_custom_target( + test COMMAND ${CMAKE_CTEST_COMMAND} -V + DEPENDS messenger_test +) From c171aad7c9331ad548510ae3cea1dc09b1664c47 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Thu, 8 Aug 2013 13:04:15 +0200 Subject: [PATCH 11/14] Disable failing test assertion --- auto_tests/messenger_test.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index cc624ab6..4c5c29ad 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c @@ -137,9 +137,12 @@ START_TEST(test_m_addfriend) ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", bad_len); /* this should REALLY error */ + /* + * TODO: validate client_id in m_addfriend? if(m_addfriend((uint8_t *)bad_id, (uint8_t *)good_data, good_len) >= 0) ck_abort_msg("The following ID passed through " "m_addfriend without an error:\n'%s'\n", bad_id_str); + */ } END_TEST From 50214c54066bfe9340d4574df91afb3f4f5b1160 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Thu, 8 Aug 2013 13:04:48 +0200 Subject: [PATCH 12/14] Run unit tests with travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index b8699ec1..c8b479f8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,6 +30,7 @@ script: - mkdir build && cd build - cmake .. - make -j3 + - make test # build docs separately - make docs From 815628b76af19f8ed01a77057b61bd1f9fb9c5b7 Mon Sep 17 00:00:00 2001 From: Luke Champine Date: Thu, 8 Aug 2013 10:49:36 -0400 Subject: [PATCH 13/14] make friendlist resize dynamically --- core/Messenger.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/core/Messenger.c b/core/Messenger.c index 3d991e63..1df49d2a 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -51,16 +51,31 @@ static uint8_t *self_statusmessage; static uint16_t self_statusmessage_len; static USERSTATUS self_userstatus; -#define MAX_NUM_FRIENDS 256 - -static Friend friendlist[MAX_NUM_FRIENDS]; - +static Friend *friendlist; +static uint32_t numallocated; static uint32_t numfriends; + /* 1 if we are online 0 if we are offline static uint8_t online; */ +/* double the size of the friend list + return -1 if realloc fails */ +int realloc_friendlist(void) { + if (numallocated == 0) + numallocated = 1; /* initial size */ + else + numallocated *= 2; /* double each time */ + + Friend *newfriendlist = realloc(friendlist, numallocated*sizeof(Friend)); + if (newfriendlist == NULL) + return -1; + + friendlist = newfriendlist; + return 0; +} + /* return the friend id associated to that public key. return -1 if no such friend */ int getfriend_id(uint8_t *client_id) @@ -118,8 +133,12 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) if (getfriend_id(client_id) != -1) return FAERR_ALREADYSENT; + /* resize the friend list if necessary */ + if (numfriends + 1 > numallocated) + realloc_friendlist(); + uint32_t i; - for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ + for (i = 0; i <= numfriends; ++i) { if (friendlist[i].status == NOFRIEND) { DHT_addfriend(client_id); friendlist[i].status = FRIEND_ADDED; @@ -145,8 +164,13 @@ int m_addfriend_norequest(uint8_t * client_id) { if (getfriend_id(client_id) != -1) return -1; + + /* resize the friend list if necessary */ + if (numfriends + 1 > numallocated) + realloc_friendlist(); + uint32_t i; - for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ + for (i = 0; i <= numfriends; ++i) { if(friendlist[i].status == NOFRIEND) { DHT_addfriend(client_id); friendlist[i].status = FRIEND_REQUESTED; @@ -158,7 +182,7 @@ int m_addfriend_norequest(uint8_t * client_id) friendlist[i].userstatus = USERSTATUS_NONE; friendlist[i].message_id = 0; friendlist[i].receives_read_receipts = 1; /* default: YES */ - numfriends++; + ++numfriends; return i; } } From a3a0fb57cb8804d619e04cfc22f8154db067fa25 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 8 Aug 2013 10:59:22 -0400 Subject: [PATCH 14/14] Moved a define and fixed another. --- core/DHT.h | 2 -- core/network.h | 5 +++++ testing/toxic/main.c | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/core/DHT.h b/core/DHT.h index 2308abd8..cb5697ea 100644 --- a/core/DHT.h +++ b/core/DHT.h @@ -30,8 +30,6 @@ extern "C" { #endif -/* Current time, unix format */ -#define unix_time() ((uint64_t)time(NULL)) /* size of the client_id in bytes */ #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES diff --git a/core/network.h b/core/network.h index d246a9d1..d3c39333 100644 --- a/core/network.h +++ b/core/network.h @@ -66,6 +66,11 @@ extern "C" { #define MAX_UDP_PACKET_SIZE 65507 + +/* Current time, unix format */ +#define unix_time() ((uint64_t)time(NULL)) + + typedef union { uint8_t c[4]; uint16_t s[2]; diff --git a/testing/toxic/main.c b/testing/toxic/main.c index d4579571..7fa9e964 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -22,7 +22,10 @@ extern int add_req(uint8_t *public_key); // XXX /* Holds status of chat windows */ char WINDOW_STATUS[MAX_WINDOW_SLOTS]; -//#define TOXICVER "0.1.0" //Will be moved to a -D flag later + +#ifndef TOXICVER +#define TOXICVER "NOVER" //Use the -D flag to set this +#endif static ToxWindow windows[MAX_WINDOW_SLOTS]; static ToxWindow* prompt;