summaryrefslogtreecommitdiff
path: root/auto_tests/lossy_packet_test.c
diff options
context:
space:
mode:
authorhugbubby <hugbubby@protonmail.com>2018-07-25 16:19:05 -0700
committeriphydf <iphydf@users.noreply.github.com>2018-07-28 16:14:06 +0000
commit051eb1d5a78ac196362fde9b917641ddb4e59c4f (patch)
tree8ae556512375faf11069173d4fd3865d42be0597 /auto_tests/lossy_packet_test.c
parentc4d58403f966328fb26e062b71adf05842ff3039 (diff)
auto_test fixture and filenames
Renamed a poorly named test, fixed up a few printf statements, substituted some unsigned integers with fixed size counterparts, and implemmented the auto_run_test.h fixture for the lossy and lossless packet tests.
Diffstat (limited to 'auto_tests/lossy_packet_test.c')
-rw-r--r--auto_tests/lossy_packet_test.c82
1 files changed, 21 insertions, 61 deletions
diff --git a/auto_tests/lossy_packet_test.c b/auto_tests/lossy_packet_test.c
index 71f45149..ef4ff7bf 100644
--- a/auto_tests/lossy_packet_test.c
+++ b/auto_tests/lossy_packet_test.c
@@ -14,6 +14,13 @@
14#include "../toxcore/util.h" 14#include "../toxcore/util.h"
15#include "check_compat.h" 15#include "check_compat.h"
16 16
17typedef struct State {
18 uint32_t index;
19 bool custom_packet_received;
20} State;
21
22#include "run_auto_test.h"
23
17#define LOSSY_PACKET_FILLER 200 24#define LOSSY_PACKET_FILLER 200
18 25
19static void handle_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) 26static void handle_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data)
@@ -22,81 +29,34 @@ static void handle_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t
22 memset(cmp_packet, LOSSY_PACKET_FILLER, sizeof(cmp_packet)); 29 memset(cmp_packet, LOSSY_PACKET_FILLER, sizeof(cmp_packet));
23 30
24 if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) { 31 if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) {
25 bool *custom_packet_received = (bool *)user_data; 32 State *state = (State *)user_data;
26 *custom_packet_received = true; 33 state->custom_packet_received = true;
27 } 34 }
28} 35}
29 36
30static void test_lossy_packet(void) 37static void test_lossy_packet(Tox **toxes, State *state)
31{ 38{
32 printf("initialising 2 toxes\n"); 39 tox_callback_friend_lossy_packet(toxes[1], &handle_lossy_packet);
33 uint32_t index[] = { 1, 2 };
34 const time_t cur_time = time(nullptr);
35 Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
36 Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);
37
38 ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");
39
40 printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
41 uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
42 tox_self_get_public_key(tox2, public_key);
43 tox_friend_add_norequest(tox1, public_key, nullptr);
44 tox_self_get_public_key(tox1, public_key);
45 tox_friend_add_norequest(tox2, public_key, nullptr);
46
47 printf("bootstrapping tox2 off tox1\n");
48 uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
49 tox_self_get_dht_id(tox1, dht_key);
50 const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
51
52 tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
53
54 while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
55 tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) {
56 tox_iterate(tox1, nullptr);
57 tox_iterate(tox2, nullptr);
58
59 c_sleep(200);
60 }
61
62 printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
63 const time_t con_time = time(nullptr);
64
65 while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
66 tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
67 tox_iterate(tox1, nullptr);
68 tox_iterate(tox2, nullptr);
69 c_sleep(200);
70 }
71
72 printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);
73
74 tox_callback_friend_lossy_packet(tox2, &handle_lossy_packet);
75 uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1]; 40 uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1];
76 memset(packet, LOSSY_PACKET_FILLER, sizeof(packet)); 41 memset(packet, LOSSY_PACKET_FILLER, sizeof(packet));
77 bool ret = tox_friend_send_lossy_packet(tox1, 0, packet, sizeof(packet), nullptr);
78 ck_assert_msg(ret == false, "tox_friend_send_lossy_packet bigger fail %i", ret);
79 ret = tox_friend_send_lossy_packet(tox1, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
80 ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret);
81 42
82 bool received_lossy_packet = false; 43 bool ret = tox_friend_send_lossy_packet(toxes[0], 0, packet, sizeof(packet), nullptr);
44 ck_assert_msg(ret == false, "should not be able to send custom packets this big %i", ret);
83 45
84 while (!received_lossy_packet) { 46 ret = tox_friend_send_lossy_packet(toxes[0], 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
85 tox_iterate(tox1, nullptr); 47 ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret);
86 tox_iterate(tox2, &received_lossy_packet);
87 c_sleep(200);
88 }
89
90 printf("test_lossy_packet succeeded, took %ld seconds\n", time(nullptr) - cur_time);
91 48
92 tox_kill(tox1); 49 while (!state[1].custom_packet_received) {
93 tox_kill(tox2); 50 tox_iterate(toxes[0], nullptr);
51 tox_iterate(toxes[1], &state[1]);
52 c_sleep(ITERATION_INTERVAL);
53 }
94} 54}
95 55
96int main(void) 56int main(void)
97{ 57{
98 setvbuf(stdout, nullptr, _IONBF, 0); 58 setvbuf(stdout, nullptr, _IONBF, 0);
99 59
100 test_lossy_packet(); 60 run_auto_test(2, test_lossy_packet);
101 return 0; 61 return 0;
102} 62}