summaryrefslogtreecommitdiff
path: root/auto_tests/lossless_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/lossless_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/lossless_packet_test.c')
-rw-r--r--auto_tests/lossless_packet_test.c84
1 files changed, 22 insertions, 62 deletions
diff --git a/auto_tests/lossless_packet_test.c b/auto_tests/lossless_packet_test.c
index dfe713a8..dc4dc0b5 100644
--- a/auto_tests/lossless_packet_test.c
+++ b/auto_tests/lossless_packet_test.c
@@ -16,92 +16,52 @@
16#include "../toxcore/util.h" 16#include "../toxcore/util.h"
17#include "check_compat.h" 17#include "check_compat.h"
18 18
19typedef struct State {
20 uint32_t index;
21 bool custom_packet_received;
22} State;
23
24#include "run_auto_test.h"
25
19#define LOSSLESS_PACKET_FILLER 160 26#define LOSSLESS_PACKET_FILLER 160
20 27
21static void handle_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, 28static void handle_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length,
22 void *user_data) 29 void *user_data)
23{ 30{
31 State *state = (State *)user_data;
32
24 uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE]; 33 uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE];
25 memset(cmp_packet, LOSSLESS_PACKET_FILLER, sizeof(cmp_packet)); 34 memset(cmp_packet, LOSSLESS_PACKET_FILLER, sizeof(cmp_packet));
26 35
27 if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) { 36 if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) {
28 bool *custom_packet_received = (bool *)user_data; 37 state->custom_packet_received = true;
29 *custom_packet_received = true;
30 } 38 }
31} 39}
32 40
33static void test_lossless_packet(void) 41static void test_lossless_packet(Tox **toxes, State *state)
34{ 42{
35 printf("initialising 2 toxes\n"); 43 tox_callback_friend_lossless_packet(toxes[1], &handle_lossless_packet);
36 uint32_t index[] = { 1, 2 };
37 const time_t cur_time = time(nullptr);
38 Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
39 Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);
40
41 ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");
42
43 printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
44 uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
45 tox_self_get_public_key(tox2, public_key);
46 tox_friend_add_norequest(tox1, public_key, nullptr);
47 tox_self_get_public_key(tox1, public_key);
48 tox_friend_add_norequest(tox2, public_key, nullptr);
49
50 printf("bootstrapping tox2 off tox1\n");
51 uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
52 tox_self_get_dht_id(tox1, dht_key);
53 const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
54
55 tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
56
57 while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
58 tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) {
59 tox_iterate(tox1, nullptr);
60 tox_iterate(tox2, nullptr);
61
62 c_sleep(200);
63 }
64
65 printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
66 const time_t con_time = time(nullptr);
67
68 while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
69 tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
70 tox_iterate(tox1, nullptr);
71 tox_iterate(tox2, nullptr);
72
73 c_sleep(200);
74 }
75
76 printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);
77
78 tox_callback_friend_lossless_packet(tox2, &handle_lossless_packet);
79 uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1]; 44 uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1];
80 memset(packet, LOSSLESS_PACKET_FILLER, sizeof(packet)); 45 memset(packet, LOSSLESS_PACKET_FILLER, sizeof(packet));
81 bool ret = tox_friend_send_lossless_packet(tox1, 0, packet, sizeof(packet), nullptr);
82 ck_assert_msg(ret == false, "tox_friend_send_lossless_packet bigger fail %i", ret);
83 ret = tox_friend_send_lossless_packet(tox1, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
84 ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret);
85
86 bool received_lossless_packet = false;
87 46
88 while (!received_lossless_packet) { 47 bool ret = tox_friend_send_lossless_packet(toxes[0], 0, packet, sizeof(packet), nullptr);
89 tox_iterate(tox1, nullptr); 48 ck_assert_msg(ret == false, "should not be able to send custom packets this big %i", ret);
90 tox_iterate(tox2, &received_lossless_packet);
91 49
92 c_sleep(200); 50 ret = tox_friend_send_lossless_packet(toxes[0], 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
93 } 51 ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret);
94 52
95 printf("test_lossless_packet succeeded, took %ld seconds\n", time(nullptr) - cur_time); 53 while (!state[1].custom_packet_received) {
54 tox_iterate(toxes[0], nullptr);
55 tox_iterate(toxes[1], &state[1]);
96 56
97 tox_kill(tox1); 57 c_sleep(ITERATION_INTERVAL);
98 tox_kill(tox2); 58 }
99} 59}
100 60
101int main(void) 61int main(void)
102{ 62{
103 setvbuf(stdout, nullptr, _IONBF, 0); 63 setvbuf(stdout, nullptr, _IONBF, 0);
104 64
105 test_lossless_packet(); 65 run_auto_test(2, test_lossless_packet);
106 return 0; 66 return 0;
107} 67}