summaryrefslogtreecommitdiff
path: root/auto_tests
diff options
context:
space:
mode:
Diffstat (limited to 'auto_tests')
-rw-r--r--auto_tests/Makefile.inc1
-rw-r--r--auto_tests/friends_test.c238
2 files changed, 0 insertions, 239 deletions
diff --git a/auto_tests/Makefile.inc b/auto_tests/Makefile.inc
index 8ab7c896..6cb9d5f1 100644
--- a/auto_tests/Makefile.inc
+++ b/auto_tests/Makefile.inc
@@ -107,5 +107,4 @@ encryptsave_test_CFLAGS = $(AUTOTEST_CFLAGS)
107encryptsave_test_LDADD = $(AUTOTEST_LDADD) 107encryptsave_test_LDADD = $(AUTOTEST_LDADD)
108 108
109 109
110EXTRA_DIST += $(top_srcdir)/auto_tests/friends_test.c
111EXTRA_DIST += $(top_srcdir)/auto_tests/helpers.h 110EXTRA_DIST += $(top_srcdir)/auto_tests/helpers.h
diff --git a/auto_tests/friends_test.c b/auto_tests/friends_test.c
deleted file mode 100644
index 2448f97c..00000000
--- a/auto_tests/friends_test.c
+++ /dev/null
@@ -1,238 +0,0 @@
1/* Unit testing for friend requests, statuses, and messages.
2 * Purpose: Check that messaging functions actually do what
3 * they're supposed to by setting up two local clients.
4 *
5 * Design: (Subject to change.)
6 * 1. Parent sends a friend request, and waits for a response.
7 * It it doesn't get one, it kills the child.
8 * 2. Child gets friend request, accepts, then waits for a status change.
9 * 3. The parent waits on a status change, killing the child if it takes
10 * too long.
11 * 4. The child gets the status change, then sends a message. After that,
12 * it returns. If if doesn't get the status change, it just loops forever.
13 * 5. After getting the status change, the parent waits for a message, on getting
14 * one, it waits on the child to return, then returns 0.
15 *
16 * Note about "waiting":
17 * Wait time is decided by WAIT_COUNT and WAIT_TIME. c_sleep(WAIT_TIME) WAIT_COUNT
18 * times. This is used both to ensure that we don't loop forever on a broken build,
19 * and that we don't get too slow with messaging. The current time is 15 seconds. */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include "../toxcore/friend_requests.h"
26#include "../toxcore/Messenger.h"
27#include <assert.h>
28#include <unistd.h>
29#include <sys/mman.h>
30#include <signal.h>
31#include <sys/wait.h>
32
33#define WAIT_COUNT 30
34#define WAIT_TIME 500
35
36#ifndef MAP_ANONYMOUS
37#define MAP_ANONYMOUS MAP_ANON
38#endif
39
40/* first step, second step */
41#define FIRST_FLAG 0x1
42#define SECOND_FLAG 0x2
43
44/* ensure that we sleep in milliseconds */
45#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
46#define c_sleep(x) Sleep(x)
47#else
48#define c_sleep(x) usleep(1000*x)
49#endif
50
51#define PORT 33445
52
53static Messenger *m;
54
55uint8_t *parent_id = NULL;
56uint8_t *child_id = NULL;
57
58pid_t child_pid = 0;
59int request_flags = 0;
60
61void do_tox(DHT *dht)
62{
63 static int dht_on = 0;
64
65 if (!dht_on && DHT_isconnected(dht)) {
66 dht_on = 1;
67 } else if (dht_on && !DHT_isconnected(dht)) {
68 dht_on = 0;
69 }
70
71 doMessenger(m);
72}
73
74void parent_confirm_message(Messenger *m, int num, uint8_t *data, uint16_t length, void *userdata)
75{
76 puts("OK");
77 request_flags |= SECOND_FLAG;
78}
79
80void parent_confirm_status(Messenger *m, int num, uint8_t *data, uint16_t length, void *userdata)
81{
82 puts("OK");
83 request_flags |= FIRST_FLAG;
84}
85
86int parent_friend_request(DHT *dht)
87{
88 char *message = "Watson, come here, I need you.";
89 int len = strlen(message);
90 int i = 0;
91
92 fputs("Sending child request.", stdout);
93 fflush(stdout);
94
95 m_addfriend(m, child_id, (uint8_t *)message, len);
96
97 /* wait on the status change */
98 for (i = 0; i < WAIT_COUNT; i++) {
99 do_tox(dht);
100
101 if (request_flags & FIRST_FLAG)
102 break;
103
104 fputs(".", stdout);
105 fflush(stdout);
106 c_sleep(WAIT_TIME);
107 }
108
109 if (!(request_flags & FIRST_FLAG)) {
110 fputs("\nfriends_test: The child took to long to respond!\n"
111 "Friend requests may be broken, failing build!\n", stderr);
112 kill(child_pid, SIGKILL);
113 return -1;
114 }
115
116 return 0;
117}
118
119void child_got_request(Messenger *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
120{
121 fputs("OK\nsending status to parent", stdout);
122 fflush(stdout);
123 m_addfriend_norequest(m, public_key);
124 request_flags |= FIRST_FLAG;
125}
126
127void child_got_statuschange(Messenger *m, int friend_num, uint8_t *string, uint16_t length, void *userdata)
128{
129 request_flags |= SECOND_FLAG;
130}
131
132int parent_wait_for_message(DHT *dht)
133{
134 int i = 0;
135
136 fputs("Parent waiting for message.", stdout);
137 fflush(stdout);
138
139 for (i = 0; i < WAIT_COUNT; i++) {
140 do_tox(dht);
141
142 if (request_flags & SECOND_FLAG)
143 break;
144
145 fputs(".", stdout);
146 fflush(stdout);
147 c_sleep(WAIT_TIME);
148 }
149
150 if (!(request_flags & SECOND_FLAG)) {
151 fputs("\nParent hasn't received the message yet!\n"
152 "Messaging may be broken, failing the build!\n", stderr);
153 kill(child_pid, SIGKILL);
154 return -1;
155 }
156
157 return 0;
158}
159
160void cleanup(void)
161{
162 munmap(parent_id, crypto_box_PUBLICKEYBYTES);
163 munmap(child_id, crypto_box_PUBLICKEYBYTES);
164 puts("============= END TEST =============");
165}
166
167int main(int argc, char *argv[])
168{
169 puts("=========== FRIENDS_TEST ===========");
170
171 /* set up the global memory */
172 parent_id = mmap(NULL, crypto_box_PUBLICKEYBYTES, PROT_READ | PROT_WRITE,
173 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
174 child_id = mmap(NULL, crypto_box_PUBLICKEYBYTES, PROT_READ | PROT_WRITE,
175 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
176
177 fputs("friends_test: Starting test...\n", stdout);
178
179 if ((child_pid = fork()) == 0) {
180 /* child */
181 int i = 0;
182 char *message = "Y-yes Mr. Watson?";
183
184 m = initMessenger();
185
186 Messenger_save(m, child_id);
187 msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
188
189 m_callback_friendrequest(m, child_got_request, NULL);
190 m_callback_statusmessage(m, child_got_statuschange, NULL);
191
192 /* wait on the friend request */
193 while (!(request_flags & FIRST_FLAG))
194 do_tox(m->dht);
195
196 /* wait for the status change */
197 while (!(request_flags & SECOND_FLAG))
198 do_tox(m->dht);
199
200 for (i = 0; i < 6; i++) {
201 /* send the message six times, just to be sure */
202 m_sendmessage(m, 0, (uint8_t *)message, strlen(message));
203 do_tox(m->dht);
204 }
205
206 cleanupMessenger(m);
207
208 return 0;
209 }
210
211 /* parent */
212 if (atexit(cleanup) != 0) {
213 fputs("friends_test: atexit() failed!\nFailing build...\n", stderr);
214 kill(child_pid, SIGKILL);
215 return -1;
216 }
217
218 m = initMessenger();
219
220 msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
221 m_callback_statusmessage(m, parent_confirm_status, NULL);
222 m_callback_friendmessage(m, parent_confirm_message, NULL);
223
224 /* hacky way to give the child time to set up */
225 c_sleep(50);
226
227 Messenger_save(m, parent_id);
228
229 if (parent_friend_request(m->dht) == -1)
230 return -1;
231
232 if (parent_wait_for_message(m->dht) == -1)
233 return -1;
234
235 wait(NULL);
236 fputs("friends_test: Build passed!\n", stdout);
237 return 0;
238}