incremental improvements, testcase which (should) work once dv works
[oweals/gnunet.git] / src / dv / test_transport_api_dv.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 2, 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_dv.c
22  * @brief base test case for dv transport (separated from other transport
23  * testcases for two reasons. 1) dv-service relies on core and other
24  * transport plugins, dv plugin relies on dv-service, so dv-plugin needs
25  * to live here, and 2) a dv plugin testcase is different from other
26  * tranport plugin testcases because we need at least three peer to test
27  * it.
28  *
29  * This test case tests DV functionality.  Specifically it starts three
30  * peers connected in a line (1 <-> 2 <-> 3).  Then a message is transmitted
31  * from peer 1 to peer 3.  Assuming that DV is working, peer 2 should have
32  * gossiped about peer 3 to 1, and should then forward a message from one
33  * to 3.
34  */
35 #include "platform.h"
36 #include "gnunet_common.h"
37 #include "gnunet_hello_lib.h"
38 #include "gnunet_getopt_lib.h"
39 #include "gnunet_os_lib.h"
40 #include "gnunet_program_lib.h"
41 #include "gnunet_scheduler_lib.h"
42 #include "gnunet_transport_service.h"
43 #include "../transport/transport.h"
44
45 #define VERBOSE GNUNET_YES
46
47 #define VERBOSE_ARM GNUNET_YES
48
49 #define START_ARM GNUNET_YES
50
51 /**
52  * How long until we give up on transmitting the message?
53  */
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
55
56 #define MTYPE 12345
57
58 struct PeerContext
59 {
60   struct GNUNET_CONFIGURATION_Handle *cfg;
61   struct GNUNET_TRANSPORT_Handle *th;
62   struct GNUNET_PeerIdentity id;
63 #if START_ARM
64   pid_t arm_pid;
65 #endif
66 };
67
68 static struct PeerContext p1;
69
70 static struct PeerContext p2;
71
72 static struct PeerContext p3;
73
74 static struct GNUNET_SCHEDULER_Handle *sched;
75
76 static int ok;
77
78 GNUNET_SCHEDULER_TaskIdentifier die_task;
79
80 #if VERBOSE
81 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
82 #else
83 #define OKPP do { ok++; } while (0)
84 #endif
85
86
87 static void
88 end ()
89 {
90   /* do work here */
91   GNUNET_assert (ok == 6);
92   GNUNET_SCHEDULER_cancel (sched, die_task);
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
94   GNUNET_TRANSPORT_disconnect (p1.th);
95   GNUNET_TRANSPORT_disconnect (p2.th);
96   GNUNET_TRANSPORT_disconnect (p3.th);
97
98   die_task = GNUNET_SCHEDULER_NO_TASK;
99   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
100   ok = 0;
101 }
102
103 static void
104 stop_arm (struct PeerContext *p)
105 {
106 #if START_ARM
107   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
108     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
109   GNUNET_OS_process_wait (p->arm_pid);
110 #endif
111   GNUNET_CONFIGURATION_destroy (p->cfg);
112 }
113
114
115 static void
116 end_badly ()
117 {
118   /* do work here */
119 #if VERBOSE
120   fprintf(stderr, "Ending on an unhappy note.\n");
121 #endif
122
123   GNUNET_TRANSPORT_disconnect (p1.th);
124   GNUNET_TRANSPORT_disconnect (p2.th);
125   GNUNET_TRANSPORT_disconnect (p3.th);
126
127   ok = 1;
128   return;
129 }
130
131 static void
132 notify_receive (void *cls,
133                 const struct GNUNET_PeerIdentity *peer,
134                 const struct GNUNET_MessageHeader *message,
135                 struct GNUNET_TIME_Relative latency,
136                 uint32_t distance)
137 {
138
139   if (ntohs(message->type) != MTYPE)
140     return;
141
142   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
143                 ntohs(message->type), cls);
144
145   GNUNET_assert (ok == 5);
146   OKPP;
147
148   GNUNET_assert (MTYPE == ntohs (message->type));
149   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
150                  ntohs (message->size));
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
152               cls);
153   end ();
154 }
155
156
157 static size_t
158 notify_ready (void *cls, size_t size, void *buf)
159 {
160   struct GNUNET_MessageHeader *hdr;
161
162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163               "Transmitting message to peer (%p) - %u!\n", cls, size);
164   GNUNET_assert (size >= 256);
165   GNUNET_assert (ok == 4);
166   OKPP;
167   if (buf != NULL)
168   {
169     hdr = buf;
170     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
171     hdr->type = htons (MTYPE);
172   }
173
174   return sizeof (struct GNUNET_MessageHeader);
175 }
176
177
178 static void
179 notify_connect (void *cls,
180                 const struct GNUNET_PeerIdentity *peer,
181                 struct GNUNET_TIME_Relative latency,
182                 uint32_t distance)
183 {
184   if ((cls == &p1) && (memcmp(peer, &p3.id, sizeof(struct GNUNET_PeerIdentity)) == 0))
185     {
186       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187                  "Peer 1 notified about connection to peer 3!\n", GNUNET_i2s (peer), cls);
188                 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
189                                               &p3.id,
190                                               256, 0, TIMEOUT, &notify_ready,
191                                               &p1);
192     }
193   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194               "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
195 }
196
197
198 static void
199 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
200 {
201   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202               "Peer `%4s' disconnected (%p)!\n",
203               GNUNET_i2s (peer), cls);
204 }
205
206
207 static void
208 setup_peer (struct PeerContext *p, const char *cfgname)
209 {
210   p->cfg = GNUNET_CONFIGURATION_create ();
211 #if START_ARM
212   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
213                                         "gnunet-service-arm",
214 #if VERBOSE_ARM
215                                         "-L", "DEBUG",
216 #endif
217                                         "-c", cfgname, NULL);
218 #endif
219   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
220
221   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
222                                     p,
223                                     &notify_receive,
224                                     &notify_connect, &notify_disconnect);
225   GNUNET_assert (p->th != NULL);
226 }
227
228
229 static void
230 exchange_hello_next (void *cls,
231                 const struct GNUNET_MessageHeader *message)
232 {
233   struct PeerContext *me = cls;
234
235   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_next, me);
236   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237               "Exchanging HELLO with peer (%p)!\n", cls);
238   GNUNET_assert (ok >= 3);
239   OKPP;
240   GNUNET_assert (message != NULL);
241   GNUNET_assert (GNUNET_OK ==
242                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
243                                       message, &me->id));
244
245   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
247
248   GNUNET_TRANSPORT_offer_hello (p3.th, message);
249
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251               "Finished exchanging HELLOs, now waiting for transmission!\n");
252
253 }
254
255
256 static void
257 exchange_hello (void *cls,
258                 const struct GNUNET_MessageHeader *message)
259 {
260   struct PeerContext *me = cls;
261
262   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
263   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264               "Exchanging HELLO with peer (%p)!\n", cls);
265   GNUNET_assert (ok >= 2);
266   OKPP;
267   GNUNET_assert (message != NULL);
268   GNUNET_assert (GNUNET_OK ==
269                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
270                                       message, &me->id));
271
272   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
274
275   GNUNET_TRANSPORT_offer_hello (p2.th, message);
276   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_next, &p2);
277 }
278
279 static void
280 run (void *cls,
281      struct GNUNET_SCHEDULER_Handle *s,
282      char *const *args,
283      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
284 {
285   GNUNET_assert (ok == 1);
286   OKPP;
287   sched = s;
288
289   die_task = GNUNET_SCHEDULER_add_delayed (sched,
290       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
291
292   setup_peer (&p1, "test_transport_api_dv_peer1.conf");
293   setup_peer (&p2, "test_transport_api_dv_peer2.conf");
294   setup_peer (&p3, "test_transport_api_dv_peer3.conf");
295
296   GNUNET_assert(p1.th != NULL);
297   GNUNET_assert(p2.th != NULL);
298   GNUNET_assert(p3.th != NULL);
299
300   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
301 }
302
303 static int
304 check ()
305 {
306
307   char *const argv[] = { "test-transport-api",
308     "-c",
309     "test_transport_api_data.conf",
310 #if VERBOSE
311     "-L", "DEBUG",
312 #endif
313     NULL
314   };
315
316   struct GNUNET_GETOPT_CommandLineOption options[] = {
317     GNUNET_GETOPT_OPTION_END
318   };
319
320   ok = 1;
321   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
322                       argv, "test-transport-api", "nohelp",
323                       options, &run, &ok);
324   stop_arm (&p1);
325   stop_arm (&p2);
326   stop_arm (&p3);
327   return ok;
328 }
329
330
331 int
332 main (int argc, char *argv[])
333 {
334   int ret;
335 #ifdef MINGW
336   return GNUNET_SYSERR;
337 #endif
338
339   GNUNET_log_setup ("test-transport-api-dv",
340 #if VERBOSE
341                     "DEBUG",
342 #else
343                     "WARNING",
344 #endif
345                     NULL);
346   ret = check ();
347   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
348   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
349   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-3");
350   return ret;
351 }
352
353 /* end of test_transport_api_dv.c */