diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-07-15 18:52:30 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-07-15 18:52:30 +0000 |
commit | 6a53889a55d1417d3ae08cff4ce3dcd486e4a1d5 (patch) | |
tree | cbc6fa94da675aa1136886665530cc801d74d52d /src | |
parent | a19338987e56b9af8ed94f808f9b4d90ff48eac1 (diff) | |
download | gnunet-6a53889a55d1417d3ae08cff4ce3dcd486e4a1d5.tar.gz gnunet-6a53889a55d1417d3ae08cff4ce3dcd486e4a1d5.zip |
make more use of testing lib
Diffstat (limited to 'src')
-rw-r--r-- | src/transport/test_transport_api_limited_sockets.c | 286 | ||||
-rw-r--r-- | src/transport/test_transport_api_timeout.c | 283 |
2 files changed, 109 insertions, 460 deletions
diff --git a/src/transport/test_transport_api_limited_sockets.c b/src/transport/test_transport_api_limited_sockets.c index 0b7fd645c..25e8550c0 100644 --- a/src/transport/test_transport_api_limited_sockets.c +++ b/src/transport/test_transport_api_limited_sockets.c | |||
@@ -47,109 +47,31 @@ | |||
47 | 47 | ||
48 | #if HAVE_SETRLIMIT | 48 | #if HAVE_SETRLIMIT |
49 | 49 | ||
50 | static char *test_source; | ||
51 | |||
52 | static char *test_plugin; | ||
53 | |||
54 | static char *test_name; | ||
55 | |||
56 | static int ok; | ||
57 | |||
58 | static struct GNUNET_SCHEDULER_Task * die_task; | ||
59 | |||
60 | static struct GNUNET_SCHEDULER_Task * send_task; | ||
61 | |||
62 | static struct GNUNET_TRANSPORT_TESTING_PeerContext *p1; | ||
63 | |||
64 | static struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; | ||
65 | |||
66 | static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc; | ||
67 | |||
68 | static struct GNUNET_TRANSPORT_TransmitHandle *th; | 50 | static struct GNUNET_TRANSPORT_TransmitHandle *th; |
69 | 51 | ||
70 | static struct GNUNET_TRANSPORT_TESTING_Handle *tth; | 52 | static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc; |
71 | |||
72 | static char *cfg_file_p1; | ||
73 | |||
74 | static char *cfg_file_p2; | ||
75 | |||
76 | #if VERBOSE | ||
77 | #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0) | ||
78 | #else | ||
79 | #define OKPP do { ok++; } while (0) | ||
80 | #endif | ||
81 | 53 | ||
82 | 54 | ||
83 | static void | 55 | static void |
84 | end () | 56 | notify_receive (void *cls, |
85 | { | 57 | struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver, |
86 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n"); | 58 | const struct GNUNET_PeerIdentity *sender, |
87 | |||
88 | if (send_task != NULL) | ||
89 | GNUNET_SCHEDULER_cancel (send_task); | ||
90 | |||
91 | if (die_task != NULL) | ||
92 | GNUNET_SCHEDULER_cancel (die_task); | ||
93 | |||
94 | if (th != NULL) | ||
95 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
96 | th = NULL; | ||
97 | |||
98 | GNUNET_TRANSPORT_TESTING_stop_peer (p1); | ||
99 | GNUNET_TRANSPORT_TESTING_stop_peer (p2); | ||
100 | GNUNET_TRANSPORT_TESTING_done (tth); | ||
101 | |||
102 | } | ||
103 | |||
104 | static void | ||
105 | end_badly (void *cls) | ||
106 | { | ||
107 | die_task = NULL; | ||
108 | |||
109 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n"); | ||
110 | |||
111 | if (send_task != NULL) | ||
112 | GNUNET_SCHEDULER_cancel (send_task); | ||
113 | |||
114 | if (cc != NULL) | ||
115 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
116 | |||
117 | if (th != NULL) | ||
118 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
119 | th = NULL; | ||
120 | |||
121 | if (p1 != NULL) | ||
122 | GNUNET_TRANSPORT_TESTING_stop_peer (p1); | ||
123 | if (p2 != NULL) | ||
124 | GNUNET_TRANSPORT_TESTING_stop_peer (p2); | ||
125 | |||
126 | if (NULL != th) | ||
127 | GNUNET_TRANSPORT_TESTING_done (tth); | ||
128 | |||
129 | ok = GNUNET_SYSERR; | ||
130 | } | ||
131 | |||
132 | |||
133 | static void | ||
134 | notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
135 | const struct GNUNET_MessageHeader *message) | 59 | const struct GNUNET_MessageHeader *message) |
136 | { | 60 | { |
137 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 61 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
138 | "Received message of type %d from peer %s!\n", | 62 | "Received message of type %d from peer %s!\n", |
139 | ntohs (message->type), GNUNET_i2s (peer)); | 63 | ntohs (message->type), |
140 | 64 | GNUNET_i2s (sender)); | |
141 | if ((MTYPE == ntohs (message->type)) && | 65 | if ((MTYPE == ntohs (message->type)) && |
142 | (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size))) | 66 | (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size))) |
143 | { | 67 | { |
144 | ok = 0; | 68 | ccc->global_ret = GNUNET_OK; |
145 | end (); | ||
146 | } | 69 | } |
147 | else | 70 | else |
148 | { | 71 | { |
149 | GNUNET_break (0); | 72 | GNUNET_break (0); |
150 | ok = 1; | ||
151 | end (); | ||
152 | } | 73 | } |
74 | GNUNET_SCHEDULER_shutdown (); | ||
153 | } | 75 | } |
154 | 76 | ||
155 | 77 | ||
@@ -160,13 +82,11 @@ notify_ready (void *cls, size_t size, void *buf) | |||
160 | struct GNUNET_MessageHeader *hdr; | 82 | struct GNUNET_MessageHeader *hdr; |
161 | 83 | ||
162 | th = NULL; | 84 | th = NULL; |
163 | |||
164 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 85 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
165 | "Transmitting message with %u bytes to peer %s\n", | 86 | "Transmitting message with %u bytes to peer %s\n", |
166 | (unsigned int) sizeof (struct GNUNET_MessageHeader), | 87 | (unsigned int) sizeof (struct GNUNET_MessageHeader), |
167 | GNUNET_i2s (&p->id)); | 88 | GNUNET_i2s (&p->id)); |
168 | GNUNET_assert (size >= 256); | 89 | GNUNET_assert (size >= 256); |
169 | |||
170 | if (buf != NULL) | 90 | if (buf != NULL) |
171 | { | 91 | { |
172 | hdr = buf; | 92 | hdr = buf; |
@@ -178,174 +98,82 @@ notify_ready (void *cls, size_t size, void *buf) | |||
178 | 98 | ||
179 | 99 | ||
180 | static void | 100 | static void |
181 | notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer) | 101 | notify_disconnect (void *cls, |
102 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, | ||
103 | const struct GNUNET_PeerIdentity *other) | ||
182 | { | 104 | { |
183 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n", | 105 | GNUNET_TRANSPORT_TESTING_log_disconnect (cls, |
184 | GNUNET_i2s (peer), cls); | 106 | me, |
185 | } | 107 | other); |
186 | 108 | if (NULL != th) | |
187 | 109 | { | |
188 | static void | ||
189 | notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
190 | { | ||
191 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n", | ||
192 | GNUNET_i2s (peer), cls); | ||
193 | if (th != NULL) | ||
194 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | 110 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); |
195 | th = NULL; | 111 | th = NULL; |
196 | } | 112 | } |
197 | |||
198 | |||
199 | static void | ||
200 | sendtask (void *cls) | ||
201 | { | ||
202 | send_task = NULL; | ||
203 | th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, 256, TIMEOUT, | ||
204 | ¬ify_ready, &p1); | ||
205 | } | ||
206 | |||
207 | |||
208 | static void | ||
209 | testing_connect_cb (void *cls) | ||
210 | { | ||
211 | cc = NULL; | ||
212 | char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id)); | ||
213 | |||
214 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c, | ||
215 | GNUNET_i2s (&p2->id)); | ||
216 | GNUNET_free (p1_c); | ||
217 | |||
218 | // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG! | ||
219 | send_task = | ||
220 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL); | ||
221 | } | 113 | } |
222 | 114 | ||
223 | static void | ||
224 | start_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p, void *cls) | ||
225 | { | ||
226 | static int started; | ||
227 | |||
228 | started++; | ||
229 | |||
230 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no, | ||
231 | GNUNET_i2s (&p->id)); | ||
232 | |||
233 | if (started != 2) | ||
234 | return; | ||
235 | |||
236 | cc = GNUNET_TRANSPORT_TESTING_connect_peers (p2, | ||
237 | p1, | ||
238 | &testing_connect_cb, | ||
239 | NULL); | ||
240 | |||
241 | } | ||
242 | 115 | ||
243 | static void | 116 | static void |
244 | run (void *cls, char *const *args, const char *cfgfile, | 117 | custom_shutdown (void *cls) |
245 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
246 | { | 118 | { |
247 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); | 119 | if (NULL != th) |
248 | |||
249 | tth = GNUNET_TRANSPORT_TESTING_init (); | ||
250 | |||
251 | p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, | ||
252 | cfg_file_p1, | ||
253 | 1, | ||
254 | ¬ify_receive, | ||
255 | ¬ify_connect, | ||
256 | ¬ify_disconnect, | ||
257 | NULL, | ||
258 | &start_cb, | ||
259 | NULL); | ||
260 | p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, | ||
261 | cfg_file_p2, | ||
262 | 2, | ||
263 | ¬ify_receive, | ||
264 | ¬ify_connect, | ||
265 | ¬ify_disconnect, | ||
266 | NULL, | ||
267 | &start_cb, | ||
268 | NULL); | ||
269 | if ((p1 == NULL) || (p2 == NULL)) | ||
270 | { | 120 | { |
271 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n"); | 121 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); |
272 | if (die_task != NULL) | 122 | th = NULL; |
273 | GNUNET_SCHEDULER_cancel (die_task); | ||
274 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
275 | return; | ||
276 | } | 123 | } |
277 | } | 124 | } |
278 | 125 | ||
279 | 126 | ||
280 | static int | 127 | static void |
281 | check () | 128 | sendtask (void *cls) |
282 | { | 129 | { |
283 | static char *const argv[] = { "test-transport-api", | 130 | th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[0]->th, |
284 | "-c", | 131 | &ccc->p[1]->id, |
285 | "test_transport_api_data.conf", | 132 | 256, |
286 | NULL | 133 | TIMEOUT, |
287 | }; | 134 | ¬ify_ready, |
288 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | 135 | ccc->p[0]); |
289 | GNUNET_GETOPT_OPTION_END | ||
290 | }; | ||
291 | |||
292 | send_task = NULL; | ||
293 | |||
294 | ok = 1; | ||
295 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name, | ||
296 | "nohelp", options, &run, &ok); | ||
297 | |||
298 | return ok; | ||
299 | } | 136 | } |
300 | 137 | ||
301 | 138 | ||
302 | int | 139 | int |
303 | main (int argc, char *argv[]) | 140 | main (int argc, char *argv[]) |
304 | { | 141 | { |
142 | struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = { | ||
143 | .connect_continuation = &sendtask, | ||
144 | .config_file = "test_transport_api_data.conf", | ||
145 | .rec = ¬ify_receive, | ||
146 | .nc = &GNUNET_TRANSPORT_TESTING_log_connect, | ||
147 | .nd = ¬ify_disconnect, | ||
148 | .shutdown_task = &custom_shutdown, | ||
149 | .timeout = TIMEOUT | ||
150 | }; | ||
305 | struct rlimit r_file_old; | 151 | struct rlimit r_file_old; |
306 | struct rlimit r_file_new; | 152 | struct rlimit r_file_new; |
307 | int res; | 153 | int res; |
308 | int ret = 0; | 154 | |
309 | 155 | res = getrlimit (RLIMIT_NOFILE, | |
310 | test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__); | 156 | &r_file_old); |
311 | test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], | ||
312 | test_source); | ||
313 | test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv[0]); | ||
314 | GNUNET_log_setup (test_name, | ||
315 | "WARNING", | ||
316 | NULL); | ||
317 | |||
318 | res = getrlimit (RLIMIT_NOFILE, &r_file_old); | ||
319 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
320 | "Maximum number of open files was: %u/%u\n", | ||
321 | (unsigned int) r_file_old.rlim_cur, | ||
322 | (unsigned int) r_file_old.rlim_max); | ||
323 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
324 | "Setting maximum number of open files to: %u\n", | ||
325 | MAX_FILES); | ||
326 | r_file_new.rlim_cur = MAX_FILES; | 157 | r_file_new.rlim_cur = MAX_FILES; |
327 | r_file_new.rlim_max = r_file_old.rlim_max; | 158 | r_file_new.rlim_max = r_file_old.rlim_max; |
328 | res = setrlimit (RLIMIT_NOFILE, &r_file_new); | 159 | res = setrlimit (RLIMIT_NOFILE, |
329 | 160 | &r_file_new); | |
330 | if (res != 0) | 161 | if (0 != res) |
331 | { | 162 | { |
332 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Setting limit failed!\n"); | 163 | fprintf (stderr, |
333 | GNUNET_free (test_source); | 164 | "Setting limit failed: %s\n", |
334 | GNUNET_free (test_plugin); | 165 | strerror (errno)); |
335 | GNUNET_free (test_name); | 166 | return 77; |
336 | return 0; | ||
337 | } | 167 | } |
338 | 168 | ||
339 | cfg_file_p1 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 1); | 169 | ccc = &my_ccc; |
340 | cfg_file_p2 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 2); | 170 | ccc->global_ret = GNUNET_SYSERR; |
341 | ret = check (); | 171 | if (GNUNET_OK != |
342 | 172 | GNUNET_TRANSPORT_TESTING_main (2, | |
343 | GNUNET_free (cfg_file_p1); | 173 | &GNUNET_TRANSPORT_TESTING_connect_check, |
344 | GNUNET_free (cfg_file_p2); | 174 | ccc)) |
345 | GNUNET_free (test_source); | 175 | return 1; |
346 | GNUNET_free (test_plugin); | 176 | return 0; |
347 | GNUNET_free (test_name); | ||
348 | return ret; | ||
349 | } | 177 | } |
350 | 178 | ||
351 | #else | 179 | #else |
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 | ||
43 | static char *test_source; | 43 | static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc; |
44 | |||
45 | static char *test_plugin; | ||
46 | |||
47 | static char *test_name; | ||
48 | |||
49 | static int ok; | ||
50 | |||
51 | static struct GNUNET_SCHEDULER_Task * die_task; | ||
52 | |||
53 | static struct GNUNET_SCHEDULER_Task * timer_task; | ||
54 | |||
55 | static struct GNUNET_TRANSPORT_TESTING_Handle *tth; | ||
56 | |||
57 | static struct GNUNET_TRANSPORT_TESTING_PeerContext *p1; | ||
58 | |||
59 | static struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; | ||
60 | |||
61 | static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc; | ||
62 | |||
63 | static char *cfg_file_p1; | ||
64 | |||
65 | static char *cfg_file_p2; | ||
66 | 44 | ||
67 | static struct GNUNET_TIME_Relative time_running; | 45 | static struct GNUNET_TIME_Relative time_running; |
68 | 46 | ||
69 | static int shutdown_flag; | 47 | static struct GNUNET_SCHEDULER_Task *timer_task; |
70 | |||
71 | static int disconnects; | ||
72 | 48 | ||
49 | static int shutdown_flag; | ||
73 | 50 | ||
74 | #if VERBOSE | 51 | static 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 | ||
81 | static void | 54 | static void |
82 | end () | 55 | custom_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 | ||
117 | static void | 78 | static void |
118 | end_badly (void *cls) | 79 | notify_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 | |||
141 | static void | ||
142 | notify_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 | |||
150 | static void | ||
151 | notify_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 | ||
158 | static void | 91 | static void |
159 | notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | 92 | notify_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) | |||
171 | static void | 109 | static void |
172 | timer (void *cls) | 110 | timer (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 | |||
199 | static void | ||
200 | testing_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 | |||
225 | static void | ||
226 | start_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 | |||
252 | static void | ||
253 | run (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 | ¬ify_receive, | ||
262 | ¬ify_connect, | ||
263 | ¬ify_disconnect, | ||
264 | NULL, | ||
265 | &start_cb, | ||
266 | NULL); | ||
267 | p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, | ||
268 | cfg_file_p2, | ||
269 | 2, | ||
270 | ¬ify_receive, | ||
271 | ¬ify_connect, | ||
272 | ¬ify_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 | ||
287 | static int | ||
288 | check () | ||
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 | ||
309 | int | 140 | int |
310 | main (int argc, char *argv[]) | 141 | main (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 = ¬ify_receive, | |
316 | GNUNET_log_setup (test_name, | 148 | .nc = &GNUNET_TRANSPORT_TESTING_log_connect, |
317 | "WARNING", | 149 | .nd = ¬ify_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*/ |