aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_api_timeout.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/test_transport_api_timeout.c')
-rw-r--r--src/transport/test_transport_api_timeout.c283
1 files changed, 52 insertions, 231 deletions
diff --git a/src/transport/test_transport_api_timeout.c b/src/transport/test_transport_api_timeout.c
index 29d69c879..05fb01d1d 100644
--- a/src/transport/test_transport_api_timeout.c
+++ b/src/transport/test_transport_api_timeout.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.x
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
@@ -40,129 +40,67 @@
40 40
41#define MTYPE 12345 41#define MTYPE 12345
42 42
43static char *test_source; 43static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
44
45static char *test_plugin;
46
47static char *test_name;
48
49static int ok;
50
51static struct GNUNET_SCHEDULER_Task * die_task;
52
53static struct GNUNET_SCHEDULER_Task * timer_task;
54
55static struct GNUNET_TRANSPORT_TESTING_Handle *tth;
56
57static struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
58
59static struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
60
61static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
62
63static char *cfg_file_p1;
64
65static char *cfg_file_p2;
66 44
67static struct GNUNET_TIME_Relative time_running; 45static struct GNUNET_TIME_Relative time_running;
68 46
69static int shutdown_flag; 47static struct GNUNET_SCHEDULER_Task *timer_task;
70
71static int disconnects;
72 48
49static int shutdown_flag;
73 50
74#if VERBOSE 51static unsigned int disconnects;
75#define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
76#else
77#define OKPP do { ok++; } while (0)
78#endif
79 52
80 53
81static void 54static void
82end () 55custom_shutdown (void *cls)
83{ 56{
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n"); 57 if (NULL != timer_task)
85
86 if (timer_task != NULL)
87 { 58 {
88 GNUNET_SCHEDULER_cancel (timer_task); 59 GNUNET_SCHEDULER_cancel (timer_task);
89 timer_task = NULL; 60 timer_task = NULL;
90 } 61 }
91 62 if (0 == disconnects)
92 if (die_task != NULL)
93 { 63 {
94 GNUNET_SCHEDULER_cancel (die_task); 64 ccc->global_ret = GNUNET_OK;
95 die_task = NULL;
96 } 65 }
97
98 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
99 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
100
101 if (disconnects == 0)
102 ok = 0;
103 else 66 else
104 { 67 {
105 ok = disconnects; 68 ccc->global_ret =- GNUNET_SYSERR;
106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
107 "Fail! Had %u disconnects while waiting %s\n", 70 "Fail! Had %u disconnects while waiting %s\n",
108 disconnects, 71 disconnects,
109 GNUNET_STRINGS_relative_time_to_string (WAIT, 72 GNUNET_STRINGS_relative_time_to_string (WAIT,
110 GNUNET_YES)); 73 GNUNET_YES));
111 } 74 }
112
113 GNUNET_TRANSPORT_TESTING_done (tth);
114} 75}
115 76
116 77
117static void 78static void
118end_badly (void *cls) 79notify_receive (void *cls,
119{ 80 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
120 die_task = NULL; 81 const struct GNUNET_PeerIdentity *sender,
121
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
123
124 if (timer_task != NULL)
125 {
126 GNUNET_SCHEDULER_cancel (timer_task);
127 timer_task = NULL;
128 }
129 if (cc != NULL)
130 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
131 if (p1 != NULL)
132 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
133 if (p2 != NULL)
134 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
135 ok = GNUNET_SYSERR;
136
137 GNUNET_TRANSPORT_TESTING_done (tth);
138}
139
140
141static void
142notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
143 const struct GNUNET_MessageHeader *message) 82 const struct GNUNET_MessageHeader *message)
144{ 83{
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Received message of type %d from peer %s!\n", 85 "Received message of type %d from peer %s!\n",
147 ntohs (message->type), GNUNET_i2s (peer)); 86 ntohs (message->type),
148} 87 GNUNET_i2s (sender));
149
150static void
151notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
152{
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
154 GNUNET_i2s (peer), cls);
155} 88}
156 89
157 90
158static void 91static void
159notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) 92notify_disconnect (void *cls,
93 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
94 const struct GNUNET_PeerIdentity *other)
160{ 95{
96 GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
97 me,
98 other);
161 if (shutdown_flag != GNUNET_YES) 99 if (shutdown_flag != GNUNET_YES)
162 { 100 {
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
164 "FAIL! Peer `%4s' disconnected during waiting period!\n", 102 "FAIL! Peer `%s' disconnected during waiting period!\n",
165 GNUNET_i2s (peer)); 103 GNUNET_i2s (other));
166 disconnects++; 104 disconnects++;
167 } 105 }
168} 106}
@@ -171,172 +109,55 @@ notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
171static void 109static void
172timer (void *cls) 110timer (void *cls)
173{ 111{
174 static int percentage; 112 static unsigned int percentage;
175 113
176 timer_task = NULL; 114 timer_task = NULL;
177 percentage += 10; 115 percentage += 10;
178 time_running = 116 time_running = GNUNET_TIME_relative_add (time_running,
179 GNUNET_TIME_relative_add (time_running, 117 GNUNET_TIME_relative_divide (WAIT,
180 GNUNET_TIME_relative_divide (WAIT, 10)); 118 10));
181 119
182 if (time_running.rel_value_us == 120 if (time_running.rel_value_us ==
183 GNUNET_TIME_relative_max (time_running, WAIT).rel_value_us) 121 GNUNET_TIME_relative_max (time_running, WAIT).rel_value_us)
184 { 122 {
185 FPRINTF (stderr, "%s", "100%%\n"); 123 FPRINTF (stderr, "%s", "100%%\n");
186 shutdown_flag = GNUNET_YES; 124 shutdown_flag = GNUNET_YES;
187 GNUNET_SCHEDULER_add_now (&end, NULL); 125 GNUNET_SCHEDULER_shutdown ();
188 } 126 }
189 else 127 else
190 { 128 {
191 FPRINTF (stderr, "%u%%..", percentage); 129 FPRINTF (stderr,
130 "%u%%..",
131 percentage);
192 timer_task = 132 timer_task =
193 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide (WAIT, 10), 133 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide (WAIT, 10),
194 &timer, NULL); 134 &timer,
195 } 135 NULL);
196}
197
198
199static void
200testing_connect_cb (void *cls)
201{
202 cc = NULL;
203 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
204
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Peers connected: %s <-> %s\n", p1_c,
207 GNUNET_i2s (&p2->id));
208 GNUNET_free (p1_c);
209
210 shutdown_flag = GNUNET_NO;
211
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Waiting for %s\n",
214 GNUNET_STRINGS_relative_time_to_string (WAIT,
215 GNUNET_YES));
216
217 if (die_task != NULL)
218 GNUNET_SCHEDULER_cancel (die_task);
219 die_task = GNUNET_SCHEDULER_add_delayed (WAIT, &end_badly, NULL);
220
221 timer_task = GNUNET_SCHEDULER_add_now (&timer, NULL);
222}
223
224
225static void
226start_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p, void *cls)
227{
228 static int started;
229
230 started++;
231
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
233 GNUNET_i2s (&p->id));
234
235 if (started != 2)
236 return;
237
238 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
239
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
242 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
243 GNUNET_free (sender_c);
244
245 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
246 p2,
247 &testing_connect_cb,
248 NULL);
249
250}
251
252static void
253run (void *cls, char *const *args, const char *cfgfile,
254 const struct GNUNET_CONFIGURATION_Handle *cfg)
255{
256 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
257
258 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
259 cfg_file_p1,
260 1,
261 &notify_receive,
262 &notify_connect,
263 &notify_disconnect,
264 NULL,
265 &start_cb,
266 NULL);
267 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
268 cfg_file_p2,
269 2,
270 &notify_receive,
271 &notify_connect,
272 &notify_disconnect,
273 NULL,
274 &start_cb,
275 NULL);
276
277 if ((p1 == NULL) || (p2 == NULL))
278 {
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
280 if (die_task != NULL)
281 GNUNET_SCHEDULER_cancel (die_task);
282 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
283 return;
284 } 136 }
285} 137}
286 138
287static int
288check ()
289{
290 static char *const argv[] = { "test-transport-api-timeout",
291 "-c",
292 "test_transport_api_data.conf",
293 NULL
294 };
295 static struct GNUNET_GETOPT_CommandLineOption options[] = {
296 GNUNET_GETOPT_OPTION_END
297 };
298
299 timer_task = NULL;
300
301 ok = 1;
302 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
303 "test-transport-api-timeout", "nohelp", options, &run,
304 &ok);
305
306 return ok;
307}
308 139
309int 140int
310main (int argc, char *argv[]) 141main (int argc,
142 char *argv[])
311{ 143{
312 int ret; 144 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
313 145 .connect_continuation = &timer,
314 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv[0]); 146 .config_file = "test_transport_api_data.conf",
315 147 .rec = &notify_receive,
316 GNUNET_log_setup (test_name, 148 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
317 "WARNING", 149 .nd = &notify_disconnect,
318 NULL); 150 .shutdown_task = &custom_shutdown,
319 151 .timeout = TIMEOUT
320 test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__); 152 };
321 test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0],
322 test_source);
323
324 tth = GNUNET_TRANSPORT_TESTING_init ();
325
326 cfg_file_p1 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 1);
327 cfg_file_p2 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 2);
328
329 ret = check ();
330
331 GNUNET_free (cfg_file_p1);
332 GNUNET_free (cfg_file_p2);
333
334 GNUNET_free (test_source);
335 GNUNET_free (test_plugin);
336 GNUNET_free (test_name);
337
338 153
339 return ret; 154 ccc = &my_ccc;
155 if (GNUNET_OK !=
156 GNUNET_TRANSPORT_TESTING_main (2,
157 &GNUNET_TRANSPORT_TESTING_connect_check,
158 ccc))
159 return 1;
160 return 0;
340} 161}
341 162
342/* end of test_transport_api_timeout.c*/ 163/* end of test_transport_api_timeout.c*/