summaryrefslogtreecommitdiff
path: root/auto_tests/send_message_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'auto_tests/send_message_test.c')
-rw-r--r--auto_tests/send_message_test.c96
1 files changed, 21 insertions, 75 deletions
diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c
index 35ef5a62..822ea64f 100644
--- a/auto_tests/send_message_test.c
+++ b/auto_tests/send_message_test.c
@@ -5,27 +5,24 @@
5#define _XOPEN_SOURCE 600 5#define _XOPEN_SOURCE 600
6#endif 6#endif
7 7
8#ifdef HAVE_CONFIG_H 8#include <stdbool.h>
9#include "config.h" 9#include <stdint.h>
10#endif
11
12#include "check_compat.h"
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <time.h>
17 10
18#include "../toxcore/ccompat.h" 11typedef struct State {
19#include "../toxcore/tox.h" 12 uint32_t index;
20#include "../toxcore/util.h" 13 bool message_received;
14} State;
21 15
22#include "helpers.h" 16#include "run_auto_test.h"
23 17
24#define MESSAGE_FILLER 'G' 18#define MESSAGE_FILLER 'G'
25 19
26static void message_callback(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type, const uint8_t *string, size_t length, 20static void message_callback(
27 void *userdata) 21 Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type,
22 const uint8_t *string, size_t length, void *userdata)
28{ 23{
24 State *state = (State *)userdata;
25
29 if (type != TOX_MESSAGE_TYPE_NORMAL) { 26 if (type != TOX_MESSAGE_TYPE_NORMAL) {
30 ck_abort_msg("Bad type"); 27 ck_abort_msg("Bad type");
31 } 28 }
@@ -34,87 +31,36 @@ static void message_callback(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE typ
34 memset(cmp_msg, MESSAGE_FILLER, sizeof(cmp_msg)); 31 memset(cmp_msg, MESSAGE_FILLER, sizeof(cmp_msg));
35 32
36 if (length == TOX_MAX_MESSAGE_LENGTH && memcmp(string, cmp_msg, sizeof(cmp_msg)) == 0) { 33 if (length == TOX_MAX_MESSAGE_LENGTH && memcmp(string, cmp_msg, sizeof(cmp_msg)) == 0) {
37 bool *message_received = (bool *)userdata; 34 state->message_received = true;
38 *message_received = true;
39 } 35 }
40} 36}
41 37
42static void test_send_message(void) 38static void send_message_test(Tox **toxes, State *state)
43{ 39{
44 printf("initialising 2 toxes\n"); 40 tox_callback_friend_message(toxes[1], &message_callback);
45 uint32_t index[] = { 1, 2 };
46 const time_t cur_time = time(nullptr);
47 Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
48 Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);
49
50 ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");
51
52 printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
53 uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
54 tox_self_get_public_key(tox2, public_key);
55 tox_friend_add_norequest(tox1, public_key, nullptr);
56 tox_self_get_public_key(tox1, public_key);
57 tox_friend_add_norequest(tox2, public_key, nullptr);
58
59 printf("bootstrapping tox2 off tox1\n");
60 uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
61 tox_self_get_dht_id(tox1, dht_key);
62 const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
63
64 tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
65
66 while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
67 tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) {
68 tox_iterate(tox1, nullptr);
69 tox_iterate(tox2, nullptr);
70
71 c_sleep(ITERATION_INTERVAL);
72 }
73
74 printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
75 const time_t con_time = time(nullptr);
76
77 while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
78 tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
79 tox_iterate(tox1, nullptr);
80 tox_iterate(tox2, nullptr);
81
82 c_sleep(ITERATION_INTERVAL);
83 }
84
85 printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);
86
87 tox_callback_friend_message(tox2, &message_callback);
88 41
89 uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1]; 42 uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1];
90 memset(msgs, MESSAGE_FILLER, sizeof(msgs)); 43 memset(msgs, MESSAGE_FILLER, sizeof(msgs));
91 44
92 TOX_ERR_FRIEND_SEND_MESSAGE errm; 45 TOX_ERR_FRIEND_SEND_MESSAGE errm;
93 tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm); 46 tox_friend_send_message(toxes[0], 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm);
94 ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG, "TOX_MAX_MESSAGE_LENGTH is too small? error=%d", errm); 47 ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG, "TOX_MAX_MESSAGE_LENGTH is too small? error=%d", errm);
95 48
96 tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm); 49 tox_friend_send_message(toxes[0], 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm);
97 ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm); 50 ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm);
98 51
99 bool message_received = false; 52 while (!state[1].message_received) {
100 53 tox_iterate(toxes[0], &state[0]);
101 while (!message_received) { 54 tox_iterate(toxes[1], &state[1]);
102 tox_iterate(tox1, nullptr);
103 tox_iterate(tox2, &message_received);
104 55
105 c_sleep(ITERATION_INTERVAL); 56 c_sleep(ITERATION_INTERVAL);
106 } 57 }
107
108 printf("tox clients messaging succeeded\n");
109
110 tox_kill(tox1);
111 tox_kill(tox2);
112} 58}
113 59
114int main(void) 60int main(void)
115{ 61{
116 setvbuf(stdout, nullptr, _IONBF, 0); 62 setvbuf(stdout, nullptr, _IONBF, 0);
117 63
118 test_send_message(); 64 run_auto_test(2, send_message_test);
119 return 0; 65 return 0;
120} 66}