summaryrefslogtreecommitdiff
path: root/auto_tests/lossy_packet_test.c
diff options
context:
space:
mode:
authorRobin Lindén <dev@robinlinden.eu>2018-02-18 01:57:45 +0100
committeriphydf <iphydf@users.noreply.github.com>2018-02-18 14:50:18 +0000
commitc3515c49e012f76c5d2ca104476e372d924a79f9 (patch)
tree56512d94a79ad08d768ea51afa735c56833338b1 /auto_tests/lossy_packet_test.c
parent13706de14bce8f8bfd1cb7db226d12cbd927b1b4 (diff)
Split up tox_test into multiple smaller tests
Diffstat (limited to 'auto_tests/lossy_packet_test.c')
-rw-r--r--auto_tests/lossy_packet_test.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/auto_tests/lossy_packet_test.c b/auto_tests/lossy_packet_test.c
new file mode 100644
index 00000000..d225a62b
--- /dev/null
+++ b/auto_tests/lossy_packet_test.c
@@ -0,0 +1,109 @@
1/* Tests that we can send lossy 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 LOSSY_PACKET_FILLER 200
25
26static void handle_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data)
27{
28 uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE];
29 memset(cmp_packet, LOSSY_PACKET_FILLER, sizeof(cmp_packet));
30
31 if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) {
32 bool *custom_packet_received = (bool *)user_data;
33 *custom_packet_received = true;
34 }
35}
36
37static void test_lossy_packet(void)
38{
39 printf("initialising 2 toxes\n");
40 uint32_t index[] = { 1, 2 };
41 const time_t cur_time = time(nullptr);
42 Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
43 Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);
44
45 ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");
46
47 printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
48 uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
49 tox_self_get_public_key(tox2, public_key);
50 tox_friend_add_norequest(tox1, public_key, nullptr);
51 tox_self_get_public_key(tox1, public_key);
52 tox_friend_add_norequest(tox2, public_key, nullptr);
53
54 printf("bootstrapping tox2 off tox1\n");
55 uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
56 tox_self_get_dht_id(tox1, dht_key);
57 const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
58
59 tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
60
61 while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
62 tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) {
63 tox_iterate(tox1, nullptr);
64 tox_iterate(tox2, nullptr);
65
66 c_sleep(200);
67 }
68
69 printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
70 const time_t con_time = time(nullptr);
71
72 while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
73 tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
74 tox_iterate(tox1, nullptr);
75 tox_iterate(tox2, nullptr);
76 c_sleep(200);
77 }
78
79 printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);
80
81 tox_callback_friend_lossy_packet(tox2, &handle_lossy_packet);
82 uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1];
83 memset(packet, LOSSY_PACKET_FILLER, sizeof(packet));
84 bool ret = tox_friend_send_lossy_packet(tox1, 0, packet, sizeof(packet), nullptr);
85 ck_assert_msg(ret == false, "tox_friend_send_lossy_packet bigger fail %i", ret);
86 ret = tox_friend_send_lossy_packet(tox1, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
87 ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret);
88
89 bool received_lossy_packet = false;
90
91 while (!received_lossy_packet) {
92 tox_iterate(tox1, nullptr);
93 tox_iterate(tox2, &received_lossy_packet);
94 c_sleep(200);
95 }
96
97 printf("test_lossy_packet succeeded, took %ld seconds\n", time(nullptr) - cur_time);
98
99 tox_kill(tox1);
100 tox_kill(tox2);
101}
102
103int main(int argc, char *argv[])
104{
105 setvbuf(stdout, nullptr, _IONBF, 0);
106
107 test_lossy_packet();
108 return 0;
109}