From 4f1e02bafa1f7ad27699859ebdd4073dbc1e934b Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 31 Mar 2014 20:37:03 -0400 Subject: [PATCH] tox_group_get_names now returns a list of name lengths along with the list of names. TCP test now also tests pings. --- auto_tests/TCP_test.c | 10 ++++++++++ testing/nTox.c | 7 ++++--- toxcore/Messenger.c | 6 ++++-- toxcore/Messenger.h | 4 +++- toxcore/group_chats.c | 4 ++-- toxcore/group_chats.h | 2 +- toxcore/tox.c | 7 +++++-- toxcore/tox.h | 5 ++++- 8 files changed, 33 insertions(+), 12 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 03ca8893..20bf6592 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -274,6 +274,16 @@ START_TEST(test_some) ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); + + uint8_t ping_packet[1 + sizeof(uint64_t)] = {4, 8, 6, 9, 67}; + write_packet_TCP_secure_connection(con1, ping_packet, sizeof(ping_packet)); + c_sleep(50); + do_TCP_server(tcp_s); + c_sleep(50); + len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + crypto_box_MACBYTES); + ck_assert_msg(len == sizeof(ping_packet), "wrong len %u", len); + ck_assert_msg(data[0] == 5, "wrong packet id %u", data[0]); + ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data"); } END_TEST diff --git a/testing/nTox.c b/testing/nTox.c index 59d0c4c1..7af3124d 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -1020,7 +1020,8 @@ void print_groupchatpeers(Tox *m, int groupnumber) } uint8_t names[num][TOX_MAX_NAME_LENGTH]; - tox_group_get_names(m, groupnumber, names, num); + uint16_t lengths[num]; + tox_group_get_names(m, groupnumber, names, lengths, num); int i; char numstr[16]; char header[] = "[g]+ "; @@ -1030,7 +1031,7 @@ void print_groupchatpeers(Tox *m, int groupnumber) size_t len_total = header_len; for (i = 0; i < num; ++i) { - size_t len_name = strlen((char *)names[i]); + size_t len_name = lengths[i]; size_t len_num = sprintf(numstr, "%i: ", i); if (len_num + len_name + len_total + 3 >= STRING_LENGTH) { @@ -1042,7 +1043,7 @@ void print_groupchatpeers(Tox *m, int groupnumber) strcpy(msg + len_total, numstr); len_total += len_num; - strcpy(msg + len_total, (char *)names[i]); + memcpy(msg + len_total, (char *)names[i], len_name); len_total += len_name; if (i < num - 1) { diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 6e103036..e014c267 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1243,16 +1243,18 @@ int group_number_peers(Messenger *m, int groupnumber) * * Copies the names of the peers to the name[length][MAX_NICK_BYTES] array. * + * Copies the lengths of the names to lengths[length] + * * returns the number of peers on success. * * return -1 on failure. */ -int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t length) +int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length) { if (groupnumber_not_valid(m, groupnumber)) return -1; - return group_client_names(m->chats[groupnumber], names, length); + return group_client_names(m->chats[groupnumber], names, lengths, length); } static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length) diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index a50d4d79..c29b6594 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -596,11 +596,13 @@ int group_number_peers(Messenger *m, int groupnumber); * * Copies the names of the peers to the name[length][MAX_NICK_BYTES] array. * + * Copies the lengths of the names to lengths[length] + * * returns the number of peers on success. * * return -1 on failure. */ -int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t length); +int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length); /****************FILE SENDING*****************/ diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 22cac286..58ec156b 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -719,12 +719,12 @@ uint32_t group_numpeers(Group_Chat *chat) return chat->numpeers; } -uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t length) +uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length) { uint32_t i; for (i = 0; i < chat->numpeers && i < length; ++i) { - group_peername(chat, i, names[i]); + lengths[i] = group_peername(chat, i, names[i]); } return i; diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index e31aa229..f218fe42 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -174,7 +174,7 @@ uint32_t group_numpeers(Group_Chat *chat); * * returns the number of peers. */ -uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t length); +uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length); /* Kill a group chat * diff --git a/toxcore/tox.c b/toxcore/tox.c index 16180b30..1e149a5c 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -593,14 +593,17 @@ int tox_group_number_peers(Tox *tox, int groupnumber) * * Copies the names of the peers to the name[length][MAX_NICK_BYTES] array. * + * Copies the lengths of the names to lengths[length] + * * returns the number of peers on success. * * return -1 on failure. */ -int tox_group_get_names(Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t length) +int tox_group_get_names(Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t lengths[], + uint16_t length) { Messenger *m = tox; - return group_names(m, groupnumber, names, length); + return group_names(m, groupnumber, names, lengths, length); } /* Return the number of chats in the instance m. diff --git a/toxcore/tox.h b/toxcore/tox.h index 8f8621b3..52706433 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -462,11 +462,14 @@ int tox_group_number_peers(Tox *tox, int groupnumber); * * Copies the names of the peers to the name[length][TOX_MAX_NAME_LENGTH] array. * + * Copies the lengths of the names to lengths[length] + * * returns the number of peers on success. * * return -1 on failure. */ -int tox_group_get_names(Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t length); +int tox_group_get_names(Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t lengths[], + uint16_t length); /* Return the number of chats in the instance m. * You should use this to determine how much memory to allocate