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