aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_ats.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-03-31 08:55:22 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-03-31 08:55:22 +0000
commite25aa5c0890e25e2433372698fa5b09a11920559 (patch)
tree7a78a8d4ec04318485ad9875753cfe71b35f8970 /src/transport/test_transport_ats.c
parentc8b9eb9ae59cb2dccf47e5ebcc91a190f6619da7 (diff)
downloadgnunet-e25aa5c0890e25e2433372698fa5b09a11920559.tar.gz
gnunet-e25aa5c0890e25e2433372698fa5b09a11920559.zip
new testcase for ats based on testing framework
Diffstat (limited to 'src/transport/test_transport_ats.c')
-rw-r--r--src/transport/test_transport_ats.c359
1 files changed, 108 insertions, 251 deletions
diff --git a/src/transport/test_transport_ats.c b/src/transport/test_transport_ats.c
index a8012da29..ed4f280f6 100644
--- a/src/transport/test_transport_ats.c
+++ b/src/transport/test_transport_ats.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors) 3 (C) 2009 Christian Grothoff (and other contributing authors)
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
@@ -18,263 +18,126 @@
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20/** 20/**
21 * @file transport/test_transport_api.c 21 * @file testing/test_testing_group.c
22 * @brief base test case for transport implementations 22 * @brief testcase for functions to connect peers in testing.c
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 */ 23 */
29#include "platform.h" 24#include "platform.h"
30#include "gnunet_common.h" 25#include "gnunet_testing_lib.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" 26#include "gnunet_scheduler_lib.h"
36#include "gnunet_transport_service.h"
37#include "transport.h"
38 27
39#define VERBOSE GNUNET_NO 28#define VERBOSE GNUNET_YES
40 29
41#define VERBOSE_ARM GNUNET_NO 30#define NUM_PEERS 4
42 31
43#define START_ARM GNUNET_YES 32#define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
44 33
45/** 34/**
46 * How long until we give up on transmitting the message? 35 * How long until we give up on connecting the peers?
47 */ 36 */
48#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) 37#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
49
50/**
51 * How long until we give up on transmitting the message?
52 */
53#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
54
55#define MTYPE 12345
56
57struct PeerContext
58{
59 struct GNUNET_CONFIGURATION_Handle *cfg;
60 struct GNUNET_TRANSPORT_Handle *th;
61 struct GNUNET_PeerIdentity id;
62#if START_ARM
63 struct GNUNET_OS_Process *arm_proc;
64#endif
65};
66
67static struct PeerContext p1;
68
69static struct PeerContext p2;
70 38
71static int ok; 39static int ok;
72 40
41static int peers_left;
73 42
43static int failed_peers;
74 44
75static GNUNET_SCHEDULER_TaskIdentifier die_task; 45static struct GNUNET_TESTING_PeerGroup *pg;
76 46
77#if VERBOSE 47static GNUNET_SCHEDULER_TaskIdentifier task;
78#define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
79#else
80#define OKPP do { ok++; } while (0)
81#endif
82
83
84static void
85end ()
86{
87 /* do work here */
88 GNUNET_assert (ok == 6);
89 GNUNET_SCHEDULER_cancel (die_task);
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
91 GNUNET_TRANSPORT_disconnect (p1.th);
92 GNUNET_TRANSPORT_disconnect (p2.th);
93 48
94 die_task = GNUNET_SCHEDULER_NO_TASK;
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
96 ok = 0;
97}
98 49
99static void 50/**
100stop_arm (struct PeerContext *p) 51 * Check whether peers successfully shut down.
52 */
53void
54shutdown_callback (void *cls, const char *emsg)
101{ 55{
102#if START_ARM 56 if (emsg != NULL)
103 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM)) 57 {
104 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 58#if VERBOSE
105 GNUNET_OS_process_wait (p->arm_proc); 59 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
106 GNUNET_OS_process_close (p->arm_proc); 60#endif
107 p->arm_proc = NULL; 61 if (ok == 0)
62 ok = 666;
63 }
64 else
65 {
66#if VERBOSE
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
68 "All peers successfully shut down!\n");
108#endif 69#endif
109 GNUNET_CONFIGURATION_destroy (p->cfg); 70 }
110} 71}
111 72
112 73static void shutdown_peers()
113static void
114end_badly ()
115{ 74{
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n"); 75 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
117 GNUNET_break (0);
118 GNUNET_TRANSPORT_disconnect (p1.th);
119 GNUNET_TRANSPORT_disconnect (p2.th);
120 ok = 1;
121} 76}
122 77
123static void 78void
124notify_receive (void *cls, 79delay_task (void *cls,
125 const struct GNUNET_PeerIdentity *peer, 80 const struct GNUNET_SCHEDULER_TaskContext *tc)
126 const struct GNUNET_MessageHeader *message,
127 const struct GNUNET_TRANSPORT_ATS_Information *ats,
128 uint32_t ats_count)
129{ 81{
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n", 82 task = GNUNET_SCHEDULER_NO_TASK;
131 ok); 83 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
132 84 return;
133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
134 ntohs(message->type), cls);
135 85
136 GNUNET_assert (ok == 5); 86#if VERBOSE
137 OKPP; 87 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Delay over\n");
138 88#endif
139 GNUNET_assert (MTYPE == ntohs (message->type)); 89 shutdown_peers ();
140 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
141 ntohs (message->size));
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
143 cls);
144 sleep(5);
145 end ();
146} 90}
147 91
148 92static void connect_peers()
149static size_t
150notify_ready (void *cls, size_t size, void *buf)
151{ 93{
152 struct GNUNET_MessageHeader *hdr; 94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peers!\n");
153 95
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 96 task = GNUNET_SCHEDULER_add_delayed(DELAY, &delay_task, NULL);
155 "Transmitting message to peer (%p) - %u!\n", cls, sizeof (struct GNUNET_MessageHeader));
156 GNUNET_assert (size >= 256);
157 GNUNET_assert (ok == 4);
158 OKPP;
159 97
160 if (buf != NULL)
161 {
162 hdr = buf;
163 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
164 hdr->type = htons (MTYPE);
165 }
166 98
167 return sizeof (struct GNUNET_MessageHeader); 99 //GNUNET_TESTING_daemons_connect();
168} 100 //shutdown_peers();
169 101
170static size_t
171notify_ready_connect (void *cls, size_t size, void *buf)
172{
173 return 0;
174} 102}
175 103
176
177static void 104static void
178notify_connect (void *cls, 105my_cb (void *cls,
179 const struct GNUNET_PeerIdentity *peer, 106 const struct GNUNET_PeerIdentity *id,
180 const struct GNUNET_TRANSPORT_ATS_Information *ats, 107 const struct GNUNET_CONFIGURATION_Handle *cfg,
181 uint32_t ats_count) 108 struct GNUNET_TESTING_Daemon *d, const char *emsg)
182{ 109{
183 if (cls == &p1) 110 if (id == NULL)
184 { 111 {
185 GNUNET_SCHEDULER_cancel (die_task); 112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT, 113 "Start callback called with error (too long starting peers), aborting test!\n");
187 &end_badly, NULL); 114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
188 115 failed_peers++;
189 GNUNET_TRANSPORT_notify_transmit_ready (p1.th, 116 if (failed_peers == peers_left)
190 &p2.id, 117 {
191 256, 0, TIMEOUT, &notify_ready, 118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 &p1); 119 "Too many peers failed, ending test!\n");
120 ok = 1;
121 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
122 }
123 return;
193 } 124 }
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
196}
197
198 125
199static void 126 peers_left--;
200notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) 127 if (peers_left == 0)
201{ 128 {
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Peer `%4s' disconnected (%p)!\n", 130 "All peers started successfully!\n");
204 GNUNET_i2s (peer), cls); 131 connect_peers();
205} 132 ok = 0;
206 133 }
207 134 else if (failed_peers == peers_left)
208static void 135 {
209setup_peer (struct PeerContext *p, const char *cfgname) 136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210{ 137 "Too many peers failed, ending test!\n");
211 p->cfg = GNUNET_CONFIGURATION_create (); 138 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
212#if START_ARM 139 ok = 1;
213 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm", 140 }
214 "gnunet-service-arm",
215#if VERBOSE_ARM
216 "-L", "DEBUG",
217#endif
218 "-c", cfgname, NULL);
219#endif
220 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
221
222 p->th = GNUNET_TRANSPORT_connect (p->cfg,
223 NULL, p,
224 &notify_receive,
225 &notify_connect, &notify_disconnect);
226 GNUNET_assert (p->th != NULL);
227}
228
229
230static void
231exchange_hello_last (void *cls,
232 const struct GNUNET_MessageHeader *message)
233{
234 struct PeerContext *me = cls;
235
236 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Exchanging HELLO with peer (%p)!\n", cls);
239 GNUNET_assert (ok >= 3);
240 OKPP;
241 GNUNET_assert (message != NULL);
242 GNUNET_assert (GNUNET_OK ==
243 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
244 message, &me->id));
245
246 GNUNET_assert(NULL != GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
247 &p1.id,
248 sizeof (struct GNUNET_MessageHeader), 0,
249 TIMEOUT,
250 &notify_ready_connect,
251 NULL));
252
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Finished exchanging HELLOs, now waiting for transmission!\n");
255}
256
257static void
258exchange_hello (void *cls,
259 const struct GNUNET_MessageHeader *message)
260{
261 struct PeerContext *me = cls;
262
263 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Exchanging HELLO with peer (%p)!\n", cls);
266 GNUNET_assert (ok >= 2);
267 OKPP;
268 GNUNET_assert (message != NULL);
269 GNUNET_assert (GNUNET_OK ==
270 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
271 message, &me->id));
272
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
275
276 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
277 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
278} 141}
279 142
280 143
@@ -283,69 +146,63 @@ run (void *cls,
283 char *const *args, 146 char *const *args,
284 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) 147 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
285{ 148{
286 GNUNET_assert (ok == 1); 149 ok = 1;
287 OKPP; 150#if VERBOSE
288 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, 151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", NUM_PEERS);
289 &end_badly, NULL); 152#endif
290 153 peers_left = NUM_PEERS;
291 setup_peer (&p1, "test_transport_ats_peer1.conf"); 154 pg = GNUNET_TESTING_daemons_start (cfg,
292 setup_peer (&p2, "test_transport_ats_peer2.conf"); 155 peers_left, /* Total number of peers */
293 GNUNET_assert(p1.th != NULL); 156 peers_left, /* Number of outstanding connections */
294 GNUNET_assert(p2.th != NULL); 157 peers_left, /* Number of parallel ssh connections, or peers being started at once */
295 158 TIMEOUT,
296 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1); 159 NULL, NULL,
160 &my_cb, NULL, NULL, NULL, NULL);
161 GNUNET_assert (pg != NULL);
297} 162}
298 163
299static int 164static int
300check () 165check ()
301{ 166{
302 static char *const argv[] = { "test-transport-api", 167 char *const argv[] = { "test-testing",
303 "-c", 168 "-c",
304 "test_transport_api_data.conf", 169 "test_testing_data.conf",
305#if VERBOSE 170#if VERBOSE
306 "-L", "DEBUG", 171 "-L", "DEBUG",
307#endif 172#endif
308 NULL 173 NULL
309 }; 174 };
310 static struct GNUNET_GETOPT_CommandLineOption options[] = { 175 struct GNUNET_GETOPT_CommandLineOption options[] = {
311 GNUNET_GETOPT_OPTION_END 176 GNUNET_GETOPT_OPTION_END
312 }; 177 };
313
314#if WRITECONFIG
315 setTransportOptions("test_transport_api_data.conf");
316#endif
317 ok = 1;
318 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 178 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
319 argv, "test-transport-api", "nohelp", 179 argv, "test-testing-group", "nohelp",
320 options, &run, &ok); 180 options, &run, &ok);
321 stop_arm (&p1);
322 stop_arm (&p2);
323 return ok; 181 return ok;
324} 182}
325 183
326
327
328int 184int
329main (int argc, char *argv[]) 185main (int argc, char *argv[])
330{ 186{
331 int ret; 187 int ret;
332#ifdef MINGW
333 return GNUNET_SYSERR;
334#endif
335 188
336 GNUNET_log_setup ("test-transport-api", 189 GNUNET_log_setup ("test-testing-group",
337#if VERBOSE 190#if VERBOSE
338 "DEBUG", 191 "DEBUG",
339#else 192#else
340 "WARNING", 193 "WARNING",
341#endif 194#endif
342 NULL); 195 NULL);
343
344 ret = check (); 196 ret = check ();
345 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1"); 197 /**
346 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2"); 198 * Still need to remove the base testing directory here,
347 199 * because group starts will create subdirectories under this
200 * main dir. However, we no longer need to sleep, as the
201 * shutdown sequence won't return until everything is cleaned
202 * up.
203 */
204 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
348 return ret; 205 return ret;
349} 206}
350 207
351/* end of test_transport_api.c */ 208/* end of test_testing_group.c */