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