Skip to content

Commit 29d1043

Browse files
committed
test: friend request test now tests min/max message sizes
1 parent 93aafd7 commit 29d1043

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

auto_tests/friend_request_test.c

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,57 @@
1010
#include "../toxcore/tox.h"
1111
#include "../toxcore/util.h"
1212
#include "../testing/misc_tools.h"
13+
1314
#include "auto_test_support.h"
1415
#include "check_compat.h"
1516

16-
#define FR_MESSAGE "Gentoo"
17+
#define REQUEST_MESSAGE "Hello, I would like to be your friend. Please respond."
18+
19+
typedef struct Callback_Data {
20+
Tox *tox1; // request sender
21+
Tox *tox2; // request receiver
22+
uint8_t message[TOX_MAX_FRIEND_REQUEST_LENGTH];
23+
uint16_t length;
24+
} Callback_Data;
1725

18-
static void accept_friend_request(const Tox_Event_Friend_Request *event,
19-
void *userdata)
26+
static void accept_friend_request(const Tox_Event_Friend_Request *event, void *userdata)
2027
{
21-
Tox *state_tox = (Tox *)userdata;
28+
Callback_Data *cb_data = (Callback_Data *)userdata;
2229

2330
const uint8_t *public_key = tox_event_friend_request_get_public_key(event);
2431
const uint8_t *data = tox_event_friend_request_get_message(event);
2532
const size_t length = tox_event_friend_request_get_message_length(event);
2633

27-
ck_assert_msg(length == sizeof(FR_MESSAGE) && memcmp(FR_MESSAGE, data, sizeof(FR_MESSAGE)) == 0,
34+
ck_assert_msg(length == cb_data->length && memcmp(cb_data->message, data, cb_data->length) == 0,
2835
"unexpected friend request message");
29-
tox_friend_add_norequest(state_tox, public_key, nullptr);
36+
37+
fprintf(stderr, "Tox2 accepts friend request.\n");
38+
39+
Tox_Err_Friend_Add err;
40+
tox_friend_add_norequest(cb_data->tox2, public_key, &err);
41+
42+
ck_assert_msg(err == TOX_ERR_FRIEND_ADD_OK, "tox_friend_add_norequest failed: %d", err);
3043
}
3144

32-
static void iterate2_wait(const Tox_Dispatch *dispatch, Tox *tox1, Tox *tox2)
45+
static void iterate2_wait(const Tox_Dispatch *dispatch, Callback_Data *cb_data)
3346
{
3447
Tox_Err_Events_Iterate err;
3548
Tox_Events *events;
3649

37-
events = tox_events_iterate(tox1, true, &err);
50+
events = tox_events_iterate(cb_data->tox1, true, &err);
3851
ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
39-
tox_dispatch_invoke(dispatch, events, tox1);
52+
tox_dispatch_invoke(dispatch, events, cb_data);
4053
tox_events_free(events);
4154

42-
events = tox_events_iterate(tox2, true, &err);
55+
events = tox_events_iterate(cb_data->tox2, true, &err);
4356
ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
44-
tox_dispatch_invoke(dispatch, events, tox2);
57+
tox_dispatch_invoke(dispatch, events, cb_data);
4558
tox_events_free(events);
4659

4760
c_sleep(ITERATION_INTERVAL);
4861
}
4962

50-
static void test_friend_request(void)
63+
static void test_friend_request(const uint8_t *message, uint16_t length)
5164
{
5265
printf("Initialising 2 toxes.\n");
5366
uint32_t index[] = { 1, 2 };
@@ -60,7 +73,7 @@ static void test_friend_request(void)
6073
tox_events_init(tox1);
6174
tox_events_init(tox2);
6275

63-
printf("Bootstrapping tox2 off tox1.\n");
76+
printf("Bootstrapping Tox2 off Tox1.\n");
6477
uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
6578
tox_self_get_dht_id(tox1, dht_key);
6679
const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
@@ -70,25 +83,36 @@ static void test_friend_request(void)
7083
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
7184
ck_assert(dispatch != nullptr);
7285

86+
Callback_Data cb_data = {nullptr};
87+
cb_data.tox1 = tox1;
88+
cb_data.tox2 = tox2;
89+
90+
ck_assert(length <= sizeof(cb_data.message));
91+
memcpy(cb_data.message, message, length);
92+
cb_data.length = length;
93+
7394
do {
74-
iterate2_wait(dispatch, tox1, tox2);
95+
iterate2_wait(dispatch, &cb_data);
7596
} while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
7697
tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE);
7798

7899
printf("Toxes are online, took %lu seconds.\n", (unsigned long)(time(nullptr) - cur_time));
79100
const time_t con_time = time(nullptr);
80101

81-
printf("Tox1 adds tox2 as friend, tox2 accepts.\n");
102+
printf("Tox1 adds Tox2 as friend. Waiting for Tox2 to accept.\n");
82103
tox_events_callback_friend_request(dispatch, accept_friend_request);
83104

84105
uint8_t address[TOX_ADDRESS_SIZE];
85106
tox_self_get_address(tox2, address);
86107

87-
const uint32_t test = tox_friend_add(tox1, address, (const uint8_t *)FR_MESSAGE, sizeof(FR_MESSAGE), nullptr);
108+
Tox_Err_Friend_Add err;
109+
const uint32_t test = tox_friend_add(tox1, address, message, length, &err);
110+
111+
ck_assert_msg(err == TOX_ERR_FRIEND_ADD_OK, "tox_friend_add failed: %d", err);
88112
ck_assert_msg(test == 0, "failed to add friend error code: %u", test);
89113

90114
do {
91-
iterate2_wait(dispatch, tox1, tox2);
115+
iterate2_wait(dispatch, &cb_data);
92116
} while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
93117
tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP);
94118

@@ -104,6 +128,20 @@ int main(void)
104128
{
105129
setvbuf(stdout, nullptr, _IONBF, 0);
106130

107-
test_friend_request();
131+
fprintf(stderr, "Testing friend request with the smallest allowed message length.\n");
132+
test_friend_request((const uint8_t *)"a", 1);
133+
134+
fprintf(stderr, "Testing friend request with an average sized message length.\n");
135+
test_friend_request((const uint8_t *)REQUEST_MESSAGE, sizeof(REQUEST_MESSAGE) - 1);
136+
137+
uint8_t long_message[TOX_MAX_FRIEND_REQUEST_LENGTH];
138+
139+
for (uint16_t i = 0; i < sizeof(long_message); ++i) {
140+
long_message[i] = 'a';
141+
}
142+
143+
fprintf(stderr, "Testing friend request with the largest allowed message length.\n");
144+
test_friend_request(long_message, sizeof(long_message));
145+
108146
return 0;
109147
}

toxcore/friend_connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ void set_friend_request_callback(Friend_Connections *fr_c, fr_request_cb *fr_req
875875
int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *data,
876876
uint16_t length)
877877
{
878-
// FIXME: This max packet size is too large to be handled by receiving clients
878+
// TODO(Jfreegman): This max packet size is too large to be handled by receiving clients
879879
// when sent via the onion. We currently limit the length at a higher level, but
880880
// this bounds check should be fixed to represent the max size of a packet that
881881
// the onion client can handle.

toxcore/friend_requests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "attributes.h"
1515
#include "friend_connection.h"
1616

17-
// FIXME: This should be the maximum size that an onion client can handle.
17+
// TODO(Jfreegman): This should be the maximum size that an onion client can handle.
1818
#define MAX_FRIEND_REQUEST_DATA_SIZE (ONION_CLIENT_MAX_DATA_SIZE - 100)
1919

2020
typedef struct Friend_Requests Friend_Requests;

toxcore/onion_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ unsigned int onion_getfriend_dht_pubkey(const Onion_Client *onion_c, int friend_
184184

185185
#define ONION_DATA_IN_RESPONSE_MIN_SIZE (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE)
186186

187-
// FIXME: This is not the correct value; data this large will be dropped by the onion client.
187+
// TODO(Jfreegman): This is not the correct value; data this large will be dropped by the onion client.
188188
#define ONION_CLIENT_MAX_DATA_SIZE (MAX_DATA_REQUEST_SIZE - ONION_DATA_IN_RESPONSE_MIN_SIZE)
189189

190190
/** @brief Send data of length length to friendnum.

0 commit comments

Comments
 (0)