diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2011-09-12 12:54:48 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2011-09-12 12:54:48 +0000 |
commit | 04ddcb878f651d8f0fd5862ed3723a4ccf6b1711 (patch) | |
tree | 6f3de972c793194e9b0b54153eafb917737a00f8 /src/transport/test_transport_api_limited_sockets.c | |
parent | 6b26c96e92ab48d830e8d66a1238444e7a061740 (diff) | |
download | gnunet-04ddcb878f651d8f0fd5862ed3723a4ccf6b1711.tar.gz gnunet-04ddcb878f651d8f0fd5862ed3723a4ccf6b1711.zip |
basics for a testcase with a limited number of open files
Diffstat (limited to 'src/transport/test_transport_api_limited_sockets.c')
-rw-r--r-- | src/transport/test_transport_api_limited_sockets.c | 378 |
1 files changed, 378 insertions, 0 deletions
diff --git a/src/transport/test_transport_api_limited_sockets.c b/src/transport/test_transport_api_limited_sockets.c new file mode 100644 index 000000000..0dc6c2a86 --- /dev/null +++ b/src/transport/test_transport_api_limited_sockets.c | |||
@@ -0,0 +1,378 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
4 | |||
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 | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file transport/test_transport_api.c | ||
22 | * @brief base test case for transport implementations | ||
23 | * | ||
24 | * This test case serves as a base for tcp, udp, and udp-nat | ||
25 | * transport test cases. Based on the executable being run | ||
26 | * the correct test case will be performed. Conservation of | ||
27 | * C code apparently. | ||
28 | */ | ||
29 | #include "platform.h" | ||
30 | #include "gnunet_common.h" | ||
31 | #include "gnunet_hello_lib.h" | ||
32 | #include "gnunet_getopt_lib.h" | ||
33 | #include "gnunet_os_lib.h" | ||
34 | #include "gnunet_program_lib.h" | ||
35 | #include "gnunet_scheduler_lib.h" | ||
36 | #include "gnunet_transport_service.h" | ||
37 | #include "transport.h" | ||
38 | #include "transport-testing.h" | ||
39 | |||
40 | #define VERBOSE GNUNET_NO | ||
41 | |||
42 | #define VERBOSE_ARM GNUNET_NO | ||
43 | |||
44 | #define START_ARM GNUNET_YES | ||
45 | |||
46 | /** | ||
47 | * How long until we give up on transmitting the message? | ||
48 | */ | ||
49 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300) | ||
50 | |||
51 | /** | ||
52 | * How long until we give up on transmitting the message? | ||
53 | */ | ||
54 | #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) | ||
55 | |||
56 | #define MTYPE 12345 | ||
57 | |||
58 | #define MAX_FILES 20 | ||
59 | |||
60 | static char *test_source; | ||
61 | |||
62 | static char *test_plugin; | ||
63 | |||
64 | static char *test_name; | ||
65 | |||
66 | static int ok; | ||
67 | |||
68 | static GNUNET_SCHEDULER_TaskIdentifier die_task; | ||
69 | |||
70 | static GNUNET_SCHEDULER_TaskIdentifier send_task; | ||
71 | |||
72 | struct PeerContext *p1; | ||
73 | |||
74 | struct PeerContext *p2; | ||
75 | |||
76 | static GNUNET_TRANSPORT_TESTING_ConnectRequest cc; | ||
77 | |||
78 | struct GNUNET_TRANSPORT_TransmitHandle *th; | ||
79 | |||
80 | char *cfg_file_p1; | ||
81 | |||
82 | char *cfg_file_p2; | ||
83 | |||
84 | #if VERBOSE | ||
85 | #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0) | ||
86 | #else | ||
87 | #define OKPP do { ok++; } while (0) | ||
88 | #endif | ||
89 | |||
90 | |||
91 | static void | ||
92 | end () | ||
93 | { | ||
94 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n"); | ||
95 | |||
96 | if (send_task != GNUNET_SCHEDULER_NO_TASK) | ||
97 | GNUNET_SCHEDULER_cancel (send_task); | ||
98 | |||
99 | if (die_task != GNUNET_SCHEDULER_NO_TASK) | ||
100 | GNUNET_SCHEDULER_cancel (die_task); | ||
101 | |||
102 | if (th != NULL) | ||
103 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
104 | th = NULL; | ||
105 | |||
106 | GNUNET_TRANSPORT_TESTING_stop_peer (p1); | ||
107 | GNUNET_TRANSPORT_TESTING_stop_peer (p2); | ||
108 | } | ||
109 | |||
110 | static void | ||
111 | end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
112 | { | ||
113 | die_task = GNUNET_SCHEDULER_NO_TASK; | ||
114 | |||
115 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n"); | ||
116 | |||
117 | if (send_task != GNUNET_SCHEDULER_NO_TASK) | ||
118 | GNUNET_SCHEDULER_cancel (send_task); | ||
119 | |||
120 | if (cc != NULL) | ||
121 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
122 | |||
123 | if (th != NULL) | ||
124 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
125 | th = NULL; | ||
126 | |||
127 | if (p1 != NULL) | ||
128 | GNUNET_TRANSPORT_TESTING_stop_peer (p1); | ||
129 | if (p2 != NULL) | ||
130 | GNUNET_TRANSPORT_TESTING_stop_peer (p2); | ||
131 | |||
132 | ok = GNUNET_SYSERR; | ||
133 | } | ||
134 | |||
135 | |||
136 | static void | ||
137 | notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
138 | const struct GNUNET_MessageHeader *message, | ||
139 | const struct GNUNET_TRANSPORT_ATS_Information *ats, | ||
140 | uint32_t ats_count) | ||
141 | { | ||
142 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
143 | "Received message of type %d from peer %s!\n", | ||
144 | ntohs (message->type), GNUNET_i2s (peer)); | ||
145 | |||
146 | if ((MTYPE == ntohs (message->type)) && | ||
147 | (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size))) | ||
148 | { | ||
149 | ok = 0; | ||
150 | end (); | ||
151 | } | ||
152 | else | ||
153 | { | ||
154 | GNUNET_break (0); | ||
155 | ok = 1; | ||
156 | end (); | ||
157 | } | ||
158 | } | ||
159 | |||
160 | |||
161 | static size_t | ||
162 | notify_ready (void *cls, size_t size, void *buf) | ||
163 | { | ||
164 | struct PeerContext *p = cls; | ||
165 | struct GNUNET_MessageHeader *hdr; | ||
166 | |||
167 | th = NULL; | ||
168 | |||
169 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
170 | "Transmitting message with %u bytes to peer %s\n", | ||
171 | sizeof (struct GNUNET_MessageHeader), GNUNET_i2s (&p->id)); | ||
172 | GNUNET_assert (size >= 256); | ||
173 | |||
174 | if (buf != NULL) | ||
175 | { | ||
176 | hdr = buf; | ||
177 | hdr->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
178 | hdr->type = htons (MTYPE); | ||
179 | } | ||
180 | return sizeof (struct GNUNET_MessageHeader); | ||
181 | } | ||
182 | |||
183 | |||
184 | static void | ||
185 | notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
186 | const struct GNUNET_TRANSPORT_ATS_Information *ats, | ||
187 | uint32_t ats_count) | ||
188 | { | ||
189 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n", | ||
190 | GNUNET_i2s (peer), cls); | ||
191 | } | ||
192 | |||
193 | |||
194 | static void | ||
195 | notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
196 | { | ||
197 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n", | ||
198 | GNUNET_i2s (peer), cls); | ||
199 | } | ||
200 | |||
201 | static void | ||
202 | sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
203 | { | ||
204 | send_task = GNUNET_SCHEDULER_NO_TASK; | ||
205 | |||
206 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
207 | return; | ||
208 | |||
209 | th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, 256, 0, TIMEOUT, | ||
210 | ¬ify_ready, &p1); | ||
211 | } | ||
212 | |||
213 | static void | ||
214 | testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls) | ||
215 | { | ||
216 | cc = NULL; | ||
217 | char *p1_c = strdup (GNUNET_i2s (&p1->id)); | ||
218 | |||
219 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c, | ||
220 | GNUNET_i2s (&p2->id)); | ||
221 | GNUNET_free (p1_c); | ||
222 | |||
223 | // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG! | ||
224 | send_task = | ||
225 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL); | ||
226 | } | ||
227 | |||
228 | static void | ||
229 | run (void *cls, char *const *args, const char *cfgfile, | ||
230 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
231 | { | ||
232 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); | ||
233 | |||
234 | p1 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p1, ¬ify_receive, | ||
235 | ¬ify_connect, ¬ify_disconnect, | ||
236 | NULL); | ||
237 | p2 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p2, ¬ify_receive, | ||
238 | ¬ify_connect, ¬ify_disconnect, | ||
239 | NULL); | ||
240 | |||
241 | if ((p1 == NULL) || (p2 == NULL)) | ||
242 | { | ||
243 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n"); | ||
244 | if (die_task != GNUNET_SCHEDULER_NO_TASK) | ||
245 | GNUNET_SCHEDULER_cancel (die_task); | ||
246 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
247 | return; | ||
248 | } | ||
249 | |||
250 | cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, | ||
251 | NULL); | ||
252 | } | ||
253 | |||
254 | |||
255 | static int | ||
256 | check () | ||
257 | { | ||
258 | static char *const argv[] = { "test-transport-api", | ||
259 | "-c", | ||
260 | "test_transport_api_data.conf", | ||
261 | #if VERBOSE | ||
262 | "-L", "DEBUG", | ||
263 | #endif | ||
264 | NULL | ||
265 | }; | ||
266 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
267 | GNUNET_GETOPT_OPTION_END | ||
268 | }; | ||
269 | |||
270 | #if WRITECONFIG | ||
271 | setTransportOptions ("test_transport_api_data.conf"); | ||
272 | #endif | ||
273 | send_task = GNUNET_SCHEDULER_NO_TASK; | ||
274 | |||
275 | ok = 1; | ||
276 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name, | ||
277 | "nohelp", options, &run, &ok); | ||
278 | |||
279 | return ok; | ||
280 | } | ||
281 | |||
282 | int | ||
283 | main (int argc, char *argv[]) | ||
284 | { | ||
285 | int ret = 0; | ||
286 | int nat_res; | ||
287 | test_plugin = NULL; | ||
288 | |||
289 | GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source); | ||
290 | GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source, | ||
291 | &test_plugin); | ||
292 | GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name); | ||
293 | |||
294 | GNUNET_log_setup (test_name, | ||
295 | #if VERBOSE | ||
296 | "DEBUG", | ||
297 | #else | ||
298 | "WARNING", | ||
299 | #endif | ||
300 | NULL); | ||
301 | |||
302 | if ((test_plugin != NULL) && ((strcmp (test_plugin, "tcp_nat") == 0) || | ||
303 | (strcmp (test_plugin, "udp_nat") == 0))) | ||
304 | { | ||
305 | nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server"); | ||
306 | if (GNUNET_NO == nat_res) | ||
307 | { | ||
308 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n", | ||
309 | "gnunet-nat-server", "SUID not set"); | ||
310 | return 0; | ||
311 | } | ||
312 | if (GNUNET_SYSERR == nat_res) | ||
313 | { | ||
314 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n", | ||
315 | "gnunet-nat-server", "file not found"); | ||
316 | return 0; | ||
317 | } | ||
318 | } | ||
319 | |||
320 | #if !HAVE_SETRLIMIT | ||
321 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot run test on this system\n"); | ||
322 | |||
323 | GNUNET_free (test_source); | ||
324 | GNUNET_free (test_plugin); | ||
325 | GNUNET_free (test_name); | ||
326 | |||
327 | return 0; | ||
328 | #else | ||
329 | struct rlimit r_file_old; | ||
330 | struct rlimit r_file_new; | ||
331 | int res; | ||
332 | res = getrlimit(RLIMIT_NOFILE, &r_file_old); | ||
333 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Maximum number of open files was: %u/%u\n", r_file_old.rlim_cur, r_file_old.rlim_max); | ||
334 | |||
335 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Setting maximum number of open files to: %u\n", MAX_FILES); | ||
336 | r_file_new.rlim_cur = MAX_FILES; | ||
337 | r_file_new.rlim_max = r_file_old.rlim_max; | ||
338 | res = setrlimit(RLIMIT_NOFILE, &r_file_new); | ||
339 | |||
340 | if (res != 0) | ||
341 | { | ||
342 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Setting limit failed!\n"); | ||
343 | GNUNET_free (test_source); | ||
344 | GNUNET_free (test_plugin); | ||
345 | GNUNET_free (test_name); | ||
346 | return 0; | ||
347 | } | ||
348 | |||
349 | GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1); | ||
350 | GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2); | ||
351 | ret = check (); | ||
352 | #endif | ||
353 | |||
354 | |||
355 | GNUNET_free (cfg_file_p1); | ||
356 | GNUNET_free (cfg_file_p2); | ||
357 | |||
358 | GNUNET_free (test_source); | ||
359 | GNUNET_free (test_plugin); | ||
360 | GNUNET_free (test_name); | ||
361 | |||
362 | #if HAVE_SETRLIMIT | ||
363 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Restoring previous value maximum number of open files\n"); | ||
364 | res = setrlimit(RLIMIT_NOFILE, &r_file_old); | ||
365 | if (res != 0) | ||
366 | { | ||
367 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Restoring limit failed!\n"); | ||
368 | return 0; | ||
369 | } | ||
370 | #endif | ||
371 | |||
372 | |||
373 | |||
374 | return ret; | ||
375 | check (); | ||
376 | } | ||
377 | |||
378 | /* end of test_transport_api.c */ | ||