aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_api.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-07-10 01:06:56 +0000
committerChristian Grothoff <christian@grothoff.org>2016-07-10 01:06:56 +0000
commit53f0f13a919c33f6bfcb7823ee4908084b259f20 (patch)
treebb018f7df1bd3d677d9fa41656ff44957a83a43c /src/transport/test_transport_api.c
parentc0b81510ffe194dc662c3a6267d5e66a69873c4b (diff)
downloadgnunet-53f0f13a919c33f6bfcb7823ee4908084b259f20.tar.gz
gnunet-53f0f13a919c33f6bfcb7823ee4908084b259f20.zip
-towards nicer transport-testing lib
Diffstat (limited to 'src/transport/test_transport_api.c')
-rw-r--r--src/transport/test_transport_api.c398
1 files changed, 85 insertions, 313 deletions
diff --git a/src/transport/test_transport_api.c b/src/transport/test_transport_api.c
index 9983356de..bb10116e2 100644
--- a/src/transport/test_transport_api.c
+++ b/src/transport/test_transport_api.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 GNUnet e.V. 3 Copyright (C) 2009, 2010, 2016 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -44,169 +44,80 @@
44 44
45#define TEST_MESSAGE_TYPE 12345 45#define TEST_MESSAGE_TYPE 12345
46 46
47static char *test_source;
48 47
49static char *test_plugin; 48static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
50
51static char *test_name;
52
53static int ok;
54
55static int s_started;
56
57static int s_connected;
58
59static int s_sending;
60
61static struct GNUNET_SCHEDULER_Task * die_task;
62
63static struct GNUNET_SCHEDULER_Task * send_task;
64
65static struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
66
67static struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
68
69static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
70 49
71static struct GNUNET_TRANSPORT_TransmitHandle *th; 50static struct GNUNET_TRANSPORT_TransmitHandle *th;
72 51
73static struct GNUNET_TRANSPORT_TESTING_Handle *tth;
74
75static char *cfg_file_p1;
76
77static char *cfg_file_p2;
78
79
80static void
81end ()
82{
83 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping peers\n");
84
85 if (send_task != NULL)
86 GNUNET_SCHEDULER_cancel (send_task);
87
88 if (die_task != NULL)
89 GNUNET_SCHEDULER_cancel (die_task);
90
91 if (th != NULL)
92 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
93 th = NULL;
94
95 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
96 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
97}
98
99 52
100static void 53static void
101end_badly (void *cls) 54custom_shutdown (void *cls)
102{ 55{
103 die_task = NULL; 56 if (NULL != th)
104
105 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
106
107
108 if (send_task != NULL)
109 GNUNET_SCHEDULER_cancel (send_task);
110
111 if (cc != NULL)
112 { 57 {
113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
114 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
115 cc = NULL;
116 }
117
118 if (th != NULL)
119 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); 58 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
59 th = NULL;
60 }
120 else 61 else
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n")); 62 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
122 63 "Peers were not ready to send data\n");
123 if (s_started == GNUNET_NO)
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
125 else
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
127
128 if (s_connected == GNUNET_NO)
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
130 else
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
132
133 if (s_sending == GNUNET_NO)
134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
135 else
136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
137
138 th = NULL;
139
140 if (p1 != NULL)
141 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
142 else
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
144 if (p2 != NULL)
145 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
146 else
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
148
149 ok = GNUNET_SYSERR;
150} 64}
151 65
152 66
153static void 67static void
154notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, 68notify_receive (void *cls,
69 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
70 const struct GNUNET_PeerIdentity *sender,
155 const struct GNUNET_MessageHeader *message) 71 const struct GNUNET_MessageHeader *message)
156{ 72{
157 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; 73 {
158 struct GNUNET_TRANSPORT_TESTING_PeerContext *t = NULL; 74 char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
159
160 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
161 t = p1;
162 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
163 t = p2;
164 GNUNET_assert (t != NULL);
165
166 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
167 75
168 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 76 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
169 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n", 77 "Peer %u (`%4s') received message of type %d and size %u size from peer %s!\n",
170 p->no, ps, ntohs (message->type), ntohs (message->size), t->no, 78 receiver->no,
171 GNUNET_i2s (&t->id)); 79 ps,
172 GNUNET_free (ps); 80 ntohs (message->type),
81 ntohs (message->size),
82 GNUNET_i2s (sender));
83 GNUNET_free (ps);
84 }
173 85
174 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) && 86 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
175 (TEST_MESSAGE_SIZE == ntohs (message->size))) 87 (TEST_MESSAGE_SIZE == ntohs (message->size)))
176 { 88 {
177 ok = 0; 89 ccc->global_ret = GNUNET_OK;
178 end (); 90 GNUNET_SCHEDULER_shutdown ();
179 } 91 }
180 else 92 else
181 { 93 {
182 GNUNET_break (0); 94 GNUNET_break (0);
183 ok = 1; 95 ccc->global_ret = GNUNET_SYSERR;
184 end (); 96 GNUNET_SCHEDULER_shutdown ();
185 } 97 }
186} 98}
187 99
188 100
189static size_t 101static size_t
190notify_ready (void *cls, size_t size, void *buf) 102notify_ready (void *cls,
103 size_t size,
104 void *buf)
191{ 105{
192 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; 106 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
193 struct GNUNET_MessageHeader *hdr; 107 struct GNUNET_MessageHeader *hdr;
194 108
195 th = NULL; 109 th = NULL;
196
197 if (buf == NULL) 110 if (buf == NULL)
198 { 111 {
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200 "Timeout occurred while waiting for transmit_ready\n"); 113 "Timeout occurred while waiting for transmit_ready\n");
201 if (NULL != die_task) 114 GNUNET_SCHEDULER_shutdown ();
202 GNUNET_SCHEDULER_cancel (die_task); 115 ccc->global_ret = 42;
203 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
204 ok = 42;
205 return 0; 116 return 0;
206 } 117 }
207 118
208 GNUNET_assert (size >= TEST_MESSAGE_SIZE); 119 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
209 if (buf != NULL) 120 if (NULL != buf)
210 { 121 {
211 memset (buf, '\0', TEST_MESSAGE_SIZE); 122 memset (buf, '\0', TEST_MESSAGE_SIZE);
212 hdr = buf; 123 hdr = buf;
@@ -214,222 +125,83 @@ notify_ready (void *cls, size_t size, void *buf)
214 hdr->type = htons (TEST_MESSAGE_TYPE); 125 hdr->type = htons (TEST_MESSAGE_TYPE);
215 } 126 }
216 127
217 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
218 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
219 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
220 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
221 GNUNET_i2s (&p->id));
222 GNUNET_free (ps);
223
224 return TEST_MESSAGE_SIZE;
225}
226
227
228static void
229sendtask (void *cls)
230{
231 send_task = NULL;
232 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
233
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
236 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
237 GNUNET_free (receiver_s);
238 s_sending = GNUNET_YES;
239 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE,
240 TIMEOUT_TRANSMIT, &notify_ready,
241 p1);
242}
243
244
245static void
246notify_connect (void *cls,
247 const struct GNUNET_PeerIdentity *peer)
248{
249 static int c;
250
251 c++;
252 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
253 struct GNUNET_TRANSPORT_TESTING_PeerContext *t = NULL;
254
255 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
256 t = p1;
257 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
258 t = p2;
259 GNUNET_assert (t != NULL);
260
261 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
262
263 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
264 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
265 t->no, GNUNET_i2s (peer));
266 GNUNET_free (ps);
267}
268
269
270static void
271notify_disconnect (void *cls,
272 const struct GNUNET_PeerIdentity *peer)
273{
274 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
275 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
276
277 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
278 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
279 p->no,
280 ps,
281 GNUNET_i2s (peer));
282 GNUNET_free (ps);
283
284 if (th != NULL)
285 { 128 {
286 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); 129 char *ps = GNUNET_strdup (GNUNET_i2s (&ccc->p[1]->id));
287 th = NULL; 130 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
132 ccc->p[1]->no,
133 ps,
134 ntohs (hdr->type),
135 ntohs (hdr->size),
136 p->no,
137 GNUNET_i2s (&p->id));
138 GNUNET_free (ps);
288 } 139 }
140 return TEST_MESSAGE_SIZE;
289} 141}
290 142
291 143
292static void 144static void
293testing_connect_cb (void *cls) 145sendtask (void *cls)
294{
295 cc = NULL;
296 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
297
298 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
299 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
300 GNUNET_free (p1_c);
301
302 s_connected = GNUNET_YES;
303 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
304}
305
306
307static void
308start_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
309 void *cls)
310{ 146{
311 static int started;
312
313 started++;
314
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
316 "Peer %u (`%s') started\n",
317 p->no,
318 GNUNET_i2s (&p->id));
319 if (started != 2)
320 return;
321 s_started = GNUNET_YES;
322 { 147 {
323 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id)); 148 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));
324 149
325 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 150 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
326 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n", 151 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
327 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id)); 152 ccc->p[1]->no,
328 GNUNET_free (sender_c); 153 GNUNET_i2s (&ccc->p[1]->id),
154 ccc->p[0]->no,
155 receiver_s);
156 GNUNET_free (receiver_s);
329 } 157 }
330 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1, 158 ccc->global_ret = GNUNET_SYSERR;
331 p2, 159 th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
332 &testing_connect_cb, 160 &ccc->p[0]->id,
333 NULL); 161 TEST_MESSAGE_SIZE,
162 TIMEOUT_TRANSMIT,
163 &notify_ready,
164 ccc->p[0]);
334} 165}
335 166
336 167
337static void 168static void
338run (void *cls, 169notify_disconnect (void *cls,
339 char *const *args, 170 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
340 const char *cfgfile, 171 const struct GNUNET_PeerIdentity *other)
341 const struct GNUNET_CONFIGURATION_Handle *cfg)
342{ 172{
343 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, 173 GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
344 &end_badly, 174 me,
345 NULL); 175 other);
346 176 if (NULL != th)
347 s_started = GNUNET_NO;
348 s_connected = GNUNET_NO;
349 s_sending = GNUNET_NO;
350
351 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
352 &notify_receive, &notify_connect,
353 &notify_disconnect, &start_cb,
354 NULL);
355
356 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
357 &notify_receive, &notify_connect,
358 &notify_disconnect, &start_cb,
359 NULL);
360
361 if ((p1 == NULL) || (p2 == NULL))
362 { 177 {
363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 178 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
364 "Fail! Could not start peers!\n"); 179 th = NULL;
365 if (die_task != NULL)
366 GNUNET_SCHEDULER_cancel (die_task);
367 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
368 return;
369 } 180 }
370} 181}
371 182
372 183
373static int
374check ()
375{
376 static char *const argv[] = {
377 "test-transport-api",
378 "-c",
379 "test_transport_api_data.conf",
380 NULL
381 };
382 static struct GNUNET_GETOPT_CommandLineOption options[] = {
383 GNUNET_GETOPT_OPTION_END
384 };
385
386 send_task = NULL;
387
388 ok = 1;
389 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
390 argv,
391 test_name,
392 "nohelp",
393 options,
394 &run,
395 &ok);
396
397 return ok;
398}
399
400
401int 184int
402main (int argc, char *argv[]) 185main (int argc,
186 char *argv[])
403{ 187{
404 int ret; 188 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
405 189 .connect_continuation = &sendtask,
406 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv[0]); 190 .config_file = "test_transport_api_data.conf",
407 test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__); 191 .rec = &notify_receive,
408 test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], 192 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
409 test_source); 193 .nd = &notify_disconnect,
410 194 .shutdown_task = &custom_shutdown,
411 GNUNET_log_setup (test_name, 195 .timeout = TIMEOUT
412 "WARNING", 196 };
413 NULL);
414 tth = GNUNET_TRANSPORT_TESTING_init ();
415
416 cfg_file_p1 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0],
417 1);
418 cfg_file_p2 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0],
419 2);
420
421 ret = check ();
422
423 GNUNET_free (cfg_file_p1);
424 GNUNET_free (cfg_file_p2);
425
426 GNUNET_free (test_source);
427 GNUNET_free (test_plugin);
428 GNUNET_free (test_name);
429
430 GNUNET_TRANSPORT_TESTING_done (tth);
431 197
432 return ret; 198 ccc = &my_ccc;
199 if (GNUNET_OK !=
200 GNUNET_TRANSPORT_TESTING_main (2,
201 &GNUNET_TRANSPORT_TESTING_connect_check,
202 ccc))
203 return 1;
204 return 0;
433} 205}
434 206
435/* end of test_transport_api.c */ 207/* end of test_transport_api.c */