diff options
author | Robin Lindén <dev@robinlinden.eu> | 2018-02-18 01:57:45 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-02-18 14:50:18 +0000 |
commit | c3515c49e012f76c5d2ca104476e372d924a79f9 (patch) | |
tree | 56512d94a79ad08d768ea51afa735c56833338b1 /auto_tests/lossless_packet_test.c | |
parent | 13706de14bce8f8bfd1cb7db226d12cbd927b1b4 (diff) |
Split up tox_test into multiple smaller tests
Diffstat (limited to 'auto_tests/lossless_packet_test.c')
-rw-r--r-- | auto_tests/lossless_packet_test.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/auto_tests/lossless_packet_test.c b/auto_tests/lossless_packet_test.c new file mode 100644 index 00000000..b5185757 --- /dev/null +++ b/auto_tests/lossless_packet_test.c | |||
@@ -0,0 +1,112 @@ | |||
1 | /* Tests that we can send lossless packets. | ||
2 | */ | ||
3 | |||
4 | #ifndef _XOPEN_SOURCE | ||
5 | #define _XOPEN_SOURCE 600 | ||
6 | #endif | ||
7 | |||
8 | #ifdef HAVE_CONFIG_H | ||
9 | #include "config.h" | ||
10 | #endif | ||
11 | |||
12 | #include "check_compat.h" | ||
13 | |||
14 | #include <stdio.h> | ||
15 | #include <stdlib.h> | ||
16 | #include <time.h> | ||
17 | |||
18 | #include "../toxcore/ccompat.h" | ||
19 | #include "../toxcore/tox.h" | ||
20 | #include "../toxcore/util.h" | ||
21 | |||
22 | #include "helpers.h" | ||
23 | |||
24 | #define LOSSLESS_PACKET_FILLER 160 | ||
25 | |||
26 | static void handle_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, | ||
27 | void *user_data) | ||
28 | { | ||
29 | uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE]; | ||
30 | memset(cmp_packet, LOSSLESS_PACKET_FILLER, sizeof(cmp_packet)); | ||
31 | |||
32 | if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) { | ||
33 | bool *custom_packet_received = (bool *)user_data; | ||
34 | *custom_packet_received = true; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | static void test_lossless_packet(void) | ||
39 | { | ||
40 | printf("initialising 2 toxes\n"); | ||
41 | uint32_t index[] = { 1, 2 }; | ||
42 | const time_t cur_time = time(nullptr); | ||
43 | Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]); | ||
44 | Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]); | ||
45 | |||
46 | ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances"); | ||
47 | |||
48 | printf("tox1 adds tox2 as friend, tox2 adds tox1\n"); | ||
49 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; | ||
50 | tox_self_get_public_key(tox2, public_key); | ||
51 | tox_friend_add_norequest(tox1, public_key, nullptr); | ||
52 | tox_self_get_public_key(tox1, public_key); | ||
53 | tox_friend_add_norequest(tox2, public_key, nullptr); | ||
54 | |||
55 | printf("bootstrapping tox2 off tox1\n"); | ||
56 | uint8_t dht_key[TOX_PUBLIC_KEY_SIZE]; | ||
57 | tox_self_get_dht_id(tox1, dht_key); | ||
58 | const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr); | ||
59 | |||
60 | tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr); | ||
61 | |||
62 | while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || | ||
63 | tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) { | ||
64 | tox_iterate(tox1, nullptr); | ||
65 | tox_iterate(tox2, nullptr); | ||
66 | |||
67 | c_sleep(200); | ||
68 | } | ||
69 | |||
70 | printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time); | ||
71 | const time_t con_time = time(nullptr); | ||
72 | |||
73 | while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP || | ||
74 | tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) { | ||
75 | tox_iterate(tox1, nullptr); | ||
76 | tox_iterate(tox2, nullptr); | ||
77 | |||
78 | c_sleep(200); | ||
79 | } | ||
80 | |||
81 | printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time); | ||
82 | |||
83 | tox_callback_friend_lossless_packet(tox2, &handle_lossless_packet); | ||
84 | uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1]; | ||
85 | memset(packet, LOSSLESS_PACKET_FILLER, sizeof(packet)); | ||
86 | bool ret = tox_friend_send_lossless_packet(tox1, 0, packet, sizeof(packet), nullptr); | ||
87 | ck_assert_msg(ret == false, "tox_friend_send_lossless_packet bigger fail %i", ret); | ||
88 | ret = tox_friend_send_lossless_packet(tox1, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr); | ||
89 | ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret); | ||
90 | |||
91 | bool received_lossless_packet = false; | ||
92 | |||
93 | while (!received_lossless_packet) { | ||
94 | tox_iterate(tox1, nullptr); | ||
95 | tox_iterate(tox2, &received_lossless_packet); | ||
96 | |||
97 | c_sleep(200); | ||
98 | } | ||
99 | |||
100 | printf("test_lossless_packet succeeded, took %ld seconds\n", time(nullptr) - cur_time); | ||
101 | |||
102 | tox_kill(tox1); | ||
103 | tox_kill(tox2); | ||
104 | } | ||
105 | |||
106 | int main(int argc, char *argv[]) | ||
107 | { | ||
108 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
109 | |||
110 | test_lossless_packet(); | ||
111 | return 0; | ||
112 | } | ||