summaryrefslogtreecommitdiff
path: root/auto_tests/friends_test.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-07 16:43:56 -0700
committerirungentoo <irungentoo@gmail.com>2013-08-07 16:43:56 -0700
commitf63a89bfc6715d4ae048b749ee4f8cbeab93186b (patch)
tree3e1bb9d47261699e977db8dc29aade537ad56bd7 /auto_tests/friends_test.c
parent81fccf68d6ef0363599ce8ab14cf45f5f021e5d6 (diff)
parent8b47b6166edfca163870641f051a3d2e8be22d7a (diff)
Merge pull request #381 from CharmlessCoin/auto_tests
Much Needed Tests
Diffstat (limited to 'auto_tests/friends_test.c')
-rwxr-xr-xauto_tests/friends_test.c217
1 files changed, 217 insertions, 0 deletions
diff --git a/auto_tests/friends_test.c b/auto_tests/friends_test.c
new file mode 100755
index 00000000..4f777ab5
--- /dev/null
+++ b/auto_tests/friends_test.c
@@ -0,0 +1,217 @@
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#include "../core/friend_requests.h"
22#include "../core/Messenger.h"
23#include <assert.h>
24#include <unistd.h>
25#include <sys/mman.h>
26#include <signal.h>
27#include <sys/wait.h>
28
29#define WAIT_COUNT 30
30#define WAIT_TIME 500
31
32/* first step, second step */
33#define FIRST_FLAG 0x1
34#define SECOND_FLAG 0x2
35
36/* ensure that we sleep in milliseconds */
37#ifdef WIN32
38#define c_sleep(x) Sleep(x)
39#else
40#define c_sleep(x) usleep(1000*x)
41#endif
42
43uint8_t *parent_id = NULL;
44uint8_t *child_id = NULL;
45
46pid_t child_pid = 0;
47int request_flags = 0;
48
49void do_tox(void)
50{
51 static int dht_on = 0;
52
53 if(!dht_on && DHT_isconnected()) {
54 dht_on = 1;
55 } else if(dht_on && !DHT_isconnected()) {
56 dht_on = 0;
57 }
58
59 doMessenger();
60}
61
62void parent_confirm_message(int num, uint8_t *data, uint16_t length)
63{
64 puts("OK");
65 request_flags |= SECOND_FLAG;
66}
67
68void parent_confirm_status(int num, USERSTATUS_KIND status, uint8_t *data, uint16_t length)
69{
70 puts("OK");
71 request_flags |= FIRST_FLAG;
72}
73
74int parent_friend_request(void)
75{
76 char *message = "Watson, come here, I need you.";
77 int len = strlen(message);
78 int i = 0;
79
80 fputs("Sending child request.", stdout);
81 fflush(stdout);
82
83 m_addfriend(child_id, (uint8_t *)message, len);
84
85 /* wait on the status change */
86 for(i = 0; i < WAIT_COUNT; i++) {
87 do_tox();
88 if(request_flags & FIRST_FLAG)
89 break;
90 fputs(".", stdout);
91 fflush(stdout);
92 c_sleep(WAIT_TIME);
93 }
94
95 if(!(request_flags & FIRST_FLAG)) {
96 fputs("\nfriends_test: The child took to long to respond!\n"
97 "Friend requests may be broken, failing build!\n", stderr);
98 kill(child_pid, SIGKILL);
99 return -1;
100 }
101
102 return 0;
103}
104
105void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length)
106{
107 fputs("OK\nsending status to parent", stdout);
108 fflush(stdout);
109 m_addfriend_norequest(public_key);
110 request_flags |= FIRST_FLAG;
111}
112
113void child_got_statuschange(int friend_num, USERSTATUS_KIND status, uint8_t *string, uint16_t length)
114{
115 request_flags |= SECOND_FLAG;
116}
117
118int parent_wait_for_message(void)
119{
120 int i = 0;
121
122 fputs("Parent waiting for message.", stdout);
123 fflush(stdout);
124
125 for(i = 0; i < WAIT_COUNT; i++) {
126 do_tox();
127 if(request_flags & SECOND_FLAG)
128 break;
129 fputs(".", stdout);
130 fflush(stdout);
131 c_sleep(WAIT_TIME);
132 }
133
134 if(!(request_flags & SECOND_FLAG)) {
135 fputs("\nParent hasn't recieved the message yet!\n"
136 "Messaging may be broken, failing the build!\n", stderr);
137 kill(child_pid, SIGKILL);
138 return -1;
139 }
140
141 return 0;
142}
143
144void cleanup(void)
145{
146 munmap(parent_id, crypto_box_PUBLICKEYBYTES);
147 munmap(child_id, crypto_box_PUBLICKEYBYTES);
148 puts("============= END TEST =============");
149}
150
151int main(int argc, char *argv[])
152{
153 puts("=========== FRIENDS_TEST ===========");
154
155 /* set up the global memory */
156 parent_id = mmap(NULL, crypto_box_PUBLICKEYBYTES, PROT_READ | PROT_WRITE,
157 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
158 child_id = mmap(NULL, crypto_box_PUBLICKEYBYTES, PROT_READ | PROT_WRITE,
159 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
160
161 fputs("friends_test: Starting test...\n", stdout);
162 if((child_pid = fork()) == 0) {
163 /* child */
164 int i = 0;
165 char *message = "Y-yes Mr. Watson?";
166
167 initMessenger();
168 Messenger_save(child_id);
169 msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
170
171 m_callback_friendrequest(child_got_request);
172 m_callback_userstatus(child_got_statuschange);
173
174 /* wait on the friend request */
175 while(!(request_flags & FIRST_FLAG))
176 do_tox();
177
178 /* wait for the status change */
179 while(!(request_flags & SECOND_FLAG))
180 do_tox();
181
182 for(i = 0; i < 6; i++) {
183 /* send the message six times, just to be sure */
184 m_sendmessage(0, (uint8_t *)message, strlen(message));
185 do_tox();
186 }
187
188 return 0;
189 }
190
191 /* parent */
192 if(atexit(cleanup) != 0) {
193 fputs("friends_test: atexit() failed!\nFailing build...\n", stderr);
194 kill(child_pid, SIGKILL);
195 return -1;
196 }
197
198 msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
199 m_callback_userstatus(parent_confirm_status);
200 m_callback_friendmessage(parent_confirm_message);
201
202 /* hacky way to give the child time to set up */
203 c_sleep(50);
204
205 initMessenger();
206 Messenger_save(parent_id);
207
208 if(parent_friend_request() == -1)
209 return -1;
210
211 if(parent_wait_for_message() == -1)
212 return -1;
213
214 wait(NULL);
215 fputs("friends_test: Build passed!\n", stdout);
216 return 0;
217}