diff options
Diffstat (limited to 'src/transport/test_transport_api_limited_sockets.c')
-rw-r--r-- | src/transport/test_transport_api_limited_sockets.c | 286 |
1 files changed, 57 insertions, 229 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 |