1cb9d6181831e6540940f4fac38d5abcc2c197af
[oweals/gnunet.git] / src / mesh / test_mesh_small.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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 mesh/test_mesh_small.c
22  *
23  * @brief Test for the mesh service: retransmission of traffic.
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_mesh_service_new.h"
28
29 #define VERBOSE GNUNET_YES
30 #define REMOVE_DIR GNUNET_YES
31
32 struct MeshPeer
33 {
34   struct MeshPeer *prev;
35
36   struct MeshPeer *next;
37
38   struct GNUNET_TESTING_Daemon *daemon;
39
40   struct GNUNET_MESH_Handle *mesh_handle;
41 };
42
43
44 struct StatsContext
45 {
46   unsigned long long total_mesh_bytes;
47 };
48
49
50 /**
51  * How long until we give up on connecting the peers?
52  */
53 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
54
55 /**
56  * Time to wait for stuff that should be rather fast
57  */
58 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
59
60 /**
61  * DIFFERENT TESTS TO RUN
62  */
63 #define SETUP 0
64 #define UNICAST 1
65 #define MULTICAST 2
66
67 /**
68  * Which test are we running?
69  */
70 static int test;
71
72 /**
73  * How many events have happened
74  */
75 static int ok;
76
77 static int peers_in_tunnel;
78
79 static int peers_responded;
80
81 /**
82  * Be verbose
83  */
84 static int verbose;
85
86 /**
87  * Total number of peers in the test.
88  */
89 static unsigned long long num_peers;
90
91 /**
92  * Global configuration file
93  */
94 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
95
96 /**
97  * Total number of currently running peers.
98  */
99 static unsigned long long peers_running;
100
101 /**
102  * Total number of connections in the whole network.
103  */
104 static unsigned int total_connections;
105
106 /**
107  * The currently running peer group.
108  */
109 static struct GNUNET_TESTING_PeerGroup *pg;
110
111 /**
112  * File to report results to.
113  */
114 static struct GNUNET_DISK_FileHandle *output_file;
115
116 /**
117  * File to log connection info, statistics to.
118  */
119 static struct GNUNET_DISK_FileHandle *data_file;
120
121 /**
122  * How many data points to capture before triggering next round?
123  */
124 static struct GNUNET_TIME_Relative wait_time;
125
126 /**
127  * Task called to disconnect peers.
128  */
129 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
130
131 /**
132  * Task To perform tests
133  */
134 static GNUNET_SCHEDULER_TaskIdentifier test_task;
135
136 /**
137  * Task called to shutdown test.
138  */
139 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
140
141 static char *topology_file;
142
143 static char *data_filename;
144
145 static struct GNUNET_TESTING_Daemon *d1;
146
147 static GNUNET_PEER_Id pid1;
148
149 static struct GNUNET_TESTING_Daemon *d2;
150
151 static struct GNUNET_TESTING_Daemon *d3;
152
153 static struct GNUNET_MESH_Handle *h1;
154
155 static struct GNUNET_MESH_Handle *h2;
156
157 static struct GNUNET_MESH_Handle *h3;
158
159 static struct GNUNET_MESH_Tunnel *t;
160
161 static struct GNUNET_MESH_Tunnel *incoming_t;
162
163 static struct GNUNET_MESH_Tunnel *incoming_t2;
164
165 static uint16_t *mesh_peers;
166
167 /**
168  * Check whether peers successfully shut down.
169  */
170 static void
171 shutdown_callback (void *cls, const char *emsg)
172 {
173   if (emsg != NULL)
174   {
175 #if VERBOSE
176     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Shutdown of peers failed!\n");
177 #endif
178     ok--;
179   }
180   else
181   {
182 #if VERBOSE
183     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: All peers successfully shut down!\n");
184 #endif
185   }
186 }
187
188
189 static void
190 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
191 {
192 #if VERBOSE
193   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Ending test.\n");
194 #endif
195
196   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
197   {
198     GNUNET_SCHEDULER_cancel (disconnect_task);
199     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
200   }
201
202   if (data_file != NULL)
203     GNUNET_DISK_file_close (data_file);
204   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
205   GNUNET_CONFIGURATION_destroy (testing_cfg);
206 }
207
208
209 static void
210 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
211 {
212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213               "***************** test: disconnecting mesh service of peers\n");
214   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
215   GNUNET_MESH_disconnect(h1);
216   GNUNET_MESH_disconnect(h2);
217   if (test == MULTICAST)
218     GNUNET_MESH_disconnect(h3);
219   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
220   {
221     GNUNET_SCHEDULER_cancel (shutdown_handle);
222     shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
223   }
224 }
225
226
227 /**
228  * Transmit ready callback
229  */
230 size_t
231 tmt_rdy (void *cls, size_t size, void *buf)
232 {
233   struct GNUNET_MessageHeader *msg = buf;
234
235   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test:  tmt_rdy called\n");
236   if (size < sizeof(struct GNUNET_MessageHeader) || NULL == buf)
237     return 0;
238   msg->size = htons (sizeof(struct GNUNET_MessageHeader));
239   msg->type = htons ((long) cls);
240   return sizeof(struct GNUNET_MessageHeader);
241 }
242
243
244 /**
245  * Function is called whenever a message is received.
246  *
247  * @param cls closure (set from GNUNET_MESH_connect)
248  * @param tunnel connection to the other end
249  * @param tunnel_ctx place to store local state associated with the tunnel
250  * @param sender who sent the message
251  * @param message the actual message
252  * @param atsi performance data for the connection
253  * @return GNUNET_OK to keep the connection open,
254  *         GNUNET_SYSERR to close it (signal serious error)
255  */
256 int
257 data_callback (void *cls,
258           struct GNUNET_MESH_Tunnel * tunnel,
259           void **tunnel_ctx,
260           const struct GNUNET_PeerIdentity *sender,
261           const struct GNUNET_MessageHeader *message,
262           const struct GNUNET_ATS_Information *atsi)
263 {
264   long client = (long) cls;
265
266   switch (client)
267   {
268     case 1L:
269       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270                   "***************** test: Origin client got a response!\n");
271       ok++;
272       peers_responded++;
273       if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
274       {
275         GNUNET_SCHEDULER_cancel (disconnect_task);
276         disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
277                                                      &disconnect_mesh_peers,
278                                                      NULL);
279       }
280       if (test == MULTICAST && peers_responded < 2)
281         return GNUNET_OK;
282       GNUNET_MESH_tunnel_destroy (tunnel);
283       break;
284     case 2L:
285     case 3L:
286       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287                   "***************** test: Destination client %u got a message.\n",
288                   client);
289       ok++;
290       GNUNET_MESH_notify_transmit_ready(tunnel,
291                                         GNUNET_NO,
292                                         0,
293                                         GNUNET_TIME_UNIT_FOREVER_REL,
294                                         sender,
295                                         sizeof(struct GNUNET_MessageHeader),
296                                         &tmt_rdy,
297                                         (void *) 1L);
298       if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
299       {
300         GNUNET_SCHEDULER_cancel (disconnect_task);
301         disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
302                                                       &disconnect_mesh_peers,
303                                                       NULL);
304       }
305       break;
306     default:
307       break;
308   }
309   return GNUNET_OK;
310 }
311
312
313 /**
314  * Handlers, for diverse services
315  */
316 static struct GNUNET_MESH_MessageHandler handlers[] = {
317   {&data_callback, 1, sizeof(struct GNUNET_MessageHeader)},
318   {NULL, 0, 0}
319 };
320
321
322 /**
323  * Method called whenever another peer has added us to a tunnel
324  * the other peer initiated.
325  *
326  * @param cls closure
327  * @param tunnel new handle to the tunnel
328  * @param initiator peer that started the tunnel
329  * @param atsi performance information for the tunnel
330  * @return initial tunnel context for the tunnel
331  *         (can be NULL -- that's not an error)
332  */
333 static void *
334 incoming_tunnel (void *cls,
335                  struct GNUNET_MESH_Tunnel * tunnel,
336                  const struct GNUNET_PeerIdentity * initiator,
337                  const struct GNUNET_ATS_Information * atsi)
338 {
339   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340               "***************** test: Incoming tunnel from %s to peer %d\n",
341               GNUNET_i2s(initiator),
342               (long) cls);
343   ok++;
344   if ((long) cls == 1L)
345     incoming_t = tunnel;
346   else
347     incoming_t2 = tunnel;
348   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
349   {
350     GNUNET_SCHEDULER_cancel (disconnect_task);
351     disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
352                                                   &disconnect_mesh_peers,
353                                                   NULL);
354   }
355   return NULL;
356 }
357
358 /**
359  * Function called whenever an inbound tunnel is destroyed.  Should clean up
360  * any associated state.
361  *
362  * @param cls closure (set from GNUNET_MESH_connect)
363  * @param tunnel connection to the other end (henceforth invalid)
364  * @param tunnel_ctx place where local state associated
365  *                   with the tunnel is stored
366  */
367 static void
368 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
369                 void *tunnel_ctx)
370 {
371   long i = (long) cls;
372
373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374               "***************** test: Incoming tunnel disconnected at peer %d\n",
375               i);
376   if (2L == i)
377     ok++;
378   else if (3L == i)
379     ok++;
380   else
381     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382               "***************** test: Unknown peer! %d\n",
383               i);
384   peers_in_tunnel--;
385   if (peers_in_tunnel > 0)
386     return;
387
388   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
389   {
390     GNUNET_SCHEDULER_cancel (disconnect_task);
391     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers, NULL);
392   }
393
394   return;
395 }
396
397
398 /**
399  * Method called whenever a tunnel falls apart.
400  *
401  * @param cls closure
402  * @param peer peer identity the tunnel stopped working with
403  */
404 static void
405 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
406 {
407   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408               "***************** test: peer %s disconnected\n",
409               GNUNET_i2s(peer));
410   return;
411 }
412
413
414 /**
415  * Method called whenever a peer connects to a tunnel.
416  *
417  * @param cls closure
418  * @param peer peer identity the tunnel was created to, NULL on timeout
419  * @param atsi performance data for the connection
420  */
421 static void
422 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
423     const struct GNUNET_ATS_Information *atsi)
424 {
425   struct GNUNET_PeerIdentity *dest;
426
427   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428               "***************** test: peer %s connected\n",
429               GNUNET_i2s(peer));
430
431   if (0 == memcmp(&d2->id, peer, sizeof(d2->id)) && (long) cls == 1L)
432     ok++;
433   if (test == MULTICAST &&
434       0 == memcmp(&d3->id, peer, sizeof(d3->id)) &&
435       (long) cls == 1L)
436     ok++;
437   switch (test)
438   {
439     case UNICAST:
440       dest = &d2->id;
441       break;
442     case MULTICAST:
443       peers_in_tunnel++;
444       if (peers_in_tunnel < 2)
445         return;
446       dest = NULL;
447       break;
448     default:
449       return;
450   }
451   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
452   {
453     GNUNET_SCHEDULER_cancel (disconnect_task);
454     disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
455                                                     &disconnect_mesh_peers,
456                                                     NULL);
457     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458                 "***************** test: Sending data...\n");
459     peers_responded = 0;
460     GNUNET_MESH_notify_transmit_ready(t,
461                                       GNUNET_NO,
462                                       0,
463                                       GNUNET_TIME_UNIT_FOREVER_REL,
464                                       dest,
465                                       sizeof(struct GNUNET_MessageHeader),
466                                       &tmt_rdy,
467                                       (void *) 1L);
468   }
469   else
470   {
471     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Disconnect already run?\n");
472     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Aborting...\n");
473   }
474   return;
475 }
476
477
478 static void
479 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
480 {
481   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: test_task\n");
482   GNUNET_MESH_peer_request_connect_add(t, &d2->id);
483   if (test == MULTICAST)
484   {
485     GNUNET_MESH_peer_request_connect_add(t, &d3->id);
486   }
487   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
488   {
489     GNUNET_SCHEDULER_cancel (disconnect_task);
490     disconnect_task = GNUNET_SCHEDULER_add_delayed(
491             GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
492             &disconnect_mesh_peers,
493             NULL);
494   }
495 }
496
497
498 /**
499  * Prototype of a callback function indicating that two peers
500  * are currently connected.
501  *
502  * @param cls closure
503  * @param first peer id for first daemon
504  * @param second peer id for the second daemon
505  * @param distance distance between the connected peers
506  * @param emsg error message (NULL on success)
507  */
508 void
509 topo_cb (void *cls,
510          const struct GNUNET_PeerIdentity* first,
511          const struct GNUNET_PeerIdentity* second,
512          const char *emsg)
513 {
514   GNUNET_PEER_Id p1;
515   GNUNET_PEER_Id p2;
516   struct GNUNET_PeerIdentity id;
517
518   GNUNET_PEER_resolve(1, &id);
519   p1 = GNUNET_PEER_search(first);
520   if (p1 == pid1)
521   {
522     p2 = GNUNET_PEER_search(second);
523     GNUNET_assert(p2 < num_peers);
524     GNUNET_assert(p2 > 0);
525     mesh_peers[p2]++;
526     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527                 "***************** test: %s IS a neighbor\n",
528                 GNUNET_i2s(second));
529     return;
530   }
531   p1 = GNUNET_PEER_search(second);
532   if (p1 == pid1)
533   {
534     p2 = GNUNET_PEER_search(first);
535     GNUNET_assert(p2 < num_peers);
536     GNUNET_assert(p2 > 0);
537     mesh_peers[p2]++;
538     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539                 "***************** test: %s IS a neighbor\n",
540                 GNUNET_i2s(first));
541     return;
542   }
543 }
544
545 /**
546  * connect_mesh_service: connect to the mesh service of one of the peers
547  *
548  */
549 static void
550 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
551 {
552   GNUNET_MESH_ApplicationType app;
553   unsigned int i;
554   struct GNUNET_PeerIdentity id;
555
556   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: connect_mesh_service\n");
557
558   for (i = 1; i <= num_peers; i++)
559   {
560     GNUNET_PEER_resolve(i, &id);
561     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562               "***************** test:   peer %s has %u conns to d1\n",
563               GNUNET_i2s (&id),
564               mesh_peers[i]);
565     if (mesh_peers[i] == 0)
566       break;
567   }
568   GNUNET_assert (i < num_peers);
569   d2 = GNUNET_TESTING_daemon_get_by_id (pg, &id);
570   if (test == MULTICAST)
571   {
572     for (i++; i <= num_peers; i++)
573     {
574       GNUNET_PEER_resolve(i, &id);
575       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576                 "***************** test:   peer %s has %u conns to d1\n",
577                 GNUNET_i2s (&id),
578                 mesh_peers[i]);
579       if (mesh_peers[i] == 0)
580         break;
581     }
582     GNUNET_assert (i < num_peers);
583     d3 = GNUNET_TESTING_daemon_get_by_id (pg, &id);
584   }
585   app = (GNUNET_MESH_ApplicationType) 0;
586
587 #if VERBOSE
588   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589               "***************** test: connecting to mesh service of peer %s (%u)\n",
590               GNUNET_i2s (&d1->id),
591               mesh_peers[0]);
592   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593               "***************** test: connecting to mesh service of peer %s (%u)\n",
594               GNUNET_i2s (&d2->id),
595               i);
596   if (test == MULTICAST)
597   {
598     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599                 "***************** test: connecting to mesh service of peer %s (%u)\n",
600                 GNUNET_i2s (&d3->id),
601                 i);
602   }
603 #endif
604   h1 = GNUNET_MESH_connect (d1->cfg,
605                             10,
606                             (void *) 1L,
607                             NULL,
608                             &tunnel_cleaner,
609                             handlers,
610                             &app);
611   h2 = GNUNET_MESH_connect (d2->cfg,
612                             10,
613                             (void *) 2L,
614                             &incoming_tunnel,
615                             &tunnel_cleaner,
616                             handlers,
617                             &app);
618   if (test == MULTICAST)
619   {
620     h3 = GNUNET_MESH_connect (d3->cfg,
621                               10,
622                               (void *) 3L,
623                               &incoming_tunnel,
624                               &tunnel_cleaner,
625                               handlers,
626                               &app);
627   }
628   t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) 1L);
629   peers_in_tunnel = 0;
630   test_task =
631       GNUNET_SCHEDULER_add_delayed(
632           GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 6),
633           &do_test, NULL);
634 }
635
636
637
638 /**
639  * peergroup_ready: start test when all peers are connected
640  * @param cls closure
641  * @param emsg error message
642  */
643 static void
644 peergroup_ready (void *cls, const char *emsg)
645 {
646   char *buf;
647   int buf_len;
648   unsigned int i;
649
650   if (emsg != NULL)
651   {
652     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653                 "***************** test: Peergroup callback called with error, aborting test!\n");
654     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655                 "***************** test: Error from testing: `%s'\n", emsg);
656     ok--;
657     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
658     return;
659   }
660 #if VERBOSE
661   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
662               "************************************************************\n");
663   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664               "***************** test: Peer Group started successfully!\n");
665   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666               "***************** test: Have %u connections\n",
667               total_connections);
668 #endif
669
670   if (data_file != NULL)
671   {
672     buf = NULL;
673     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
674     if (buf_len > 0)
675       GNUNET_DISK_file_write (data_file, buf, buf_len);
676     GNUNET_free (buf);
677   }
678   peers_running = GNUNET_TESTING_daemons_running (pg);
679   for (i = 0; i < num_peers; i++)
680   {
681     d1 = GNUNET_TESTING_daemon_get (pg, i);
682     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683                 "***************** test:   %u: %s\n",
684                 GNUNET_PEER_intern(&d1->id),
685                 GNUNET_i2s (&d1->id));
686   }
687   d1 = GNUNET_TESTING_daemon_get (pg, 0);
688   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689               "***************** test: Peer looking: %s\n",
690               GNUNET_i2s (&d1->id));
691   pid1 = GNUNET_PEER_intern(&d1->id);
692   mesh_peers[pid1] = 100;
693   GNUNET_TESTING_get_topology(pg, &topo_cb, NULL);
694
695   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
696                                     GNUNET_TIME_UNIT_SECONDS,
697                                     4),
698                                 &connect_mesh_service,
699                                 NULL);
700   disconnect_task =
701       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
702
703 }
704
705
706 /**
707  * Function that will be called whenever two daemons are connected by
708  * the testing library.
709  *
710  * @param cls closure
711  * @param first peer id for first daemon
712  * @param second peer id for the second daemon
713  * @param distance distance between the connected peers
714  * @param first_cfg config for the first daemon
715  * @param second_cfg config for the second daemon
716  * @param first_daemon handle for the first daemon
717  * @param second_daemon handle for the second daemon
718  * @param emsg error message (NULL on success)
719  */
720 static void
721 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
722             const struct GNUNET_PeerIdentity *second, uint32_t distance,
723             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
724             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
725             struct GNUNET_TESTING_Daemon *first_daemon,
726             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
727 {
728   if (emsg == NULL)
729   {
730     total_connections++;
731   }
732   else
733   {
734     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735                 "***************** test: Problem with new connection (%s)\n",
736                 emsg);
737   }
738
739 }
740
741
742 /**
743  * run: load configuration options and schedule test to run (start peergroup)
744  * @param cls closure
745  * @param args argv
746  * @param cfgfile configuration file name (can be NULL)
747  * @param cfg configuration handle
748  */
749 static void
750 run (void *cls, char *const *args, const char *cfgfile,
751      const struct GNUNET_CONFIGURATION_Handle *cfg)
752 {
753   char *temp_str;
754   unsigned long long temp_wait;
755   struct GNUNET_TESTING_Host *hosts;
756
757   ok = 0;
758   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
759
760   GNUNET_log_setup ("test_mesh_small",
761 #if VERBOSE
762                     "DEBUG",
763 #else
764                     "WARNING",
765 #endif
766                     NULL);
767
768 #if VERBOSE
769   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Starting daemons.\n");
770   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
771                                          "use_progressbars", "YES");
772 #endif
773
774   if (GNUNET_OK !=
775       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
776                                              "num_peers", &num_peers))
777   {
778     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
779                 "Option TESTING:NUM_PEERS is required!\n");
780     return;
781   }
782
783   mesh_peers = GNUNET_malloc (sizeof(GNUNET_PEER_Id) * (num_peers + 1));
784
785   if (GNUNET_OK !=
786       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
787                                              "wait_time", &temp_wait))
788   {
789     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
790                 "Option test_mesh_small:wait_time is required!\n");
791     return;
792   }
793
794   if (GNUNET_OK !=
795       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
796                                              "topology_output_file",
797                                              &topology_file))
798   {
799     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
800                 "Option test_mesh_small:topology_output_file is required!\n");
801     return;
802   }
803
804   if (GNUNET_OK !=
805       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
806                                              "data_output_file",
807                                              &data_filename))
808   {
809     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
810                 "Option test_mesh_small:data_output_file is required!\n");
811     return;
812   }
813
814   data_file =
815       GNUNET_DISK_file_open (data_filename,
816                              GNUNET_DISK_OPEN_READWRITE |
817                              GNUNET_DISK_OPEN_CREATE,
818                              GNUNET_DISK_PERM_USER_READ |
819                              GNUNET_DISK_PERM_USER_WRITE);
820   if (data_file == NULL)
821   {
822     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
823                 data_filename);
824     GNUNET_free (data_filename);
825   }
826
827   wait_time =
828       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
829
830   if (GNUNET_YES ==
831       GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
832                                              "output_file", &temp_str))
833   {
834     output_file =
835         GNUNET_DISK_file_open (temp_str,
836                                GNUNET_DISK_OPEN_READWRITE |
837                                GNUNET_DISK_OPEN_CREATE,
838                                GNUNET_DISK_PERM_USER_READ |
839                                GNUNET_DISK_PERM_USER_WRITE);
840     if (output_file == NULL)
841       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
842                   temp_str);
843   }
844   GNUNET_free_non_null (temp_str);
845
846   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
847
848   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
849                                        &connect_cb, &peergroup_ready, NULL,
850                                        hosts);
851   GNUNET_assert (pg != NULL);
852   shutdown_handle =
853       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
854                                     &shutdown_task, NULL);
855 }
856
857
858
859 /**
860  * test_mesh_small command line options
861  */
862 static struct GNUNET_GETOPT_CommandLineOption options[] = {
863   {'V', "verbose", NULL,
864    gettext_noop ("be verbose (print progress information)"),
865    0, &GNUNET_GETOPT_set_one, &verbose},
866   GNUNET_GETOPT_OPTION_END
867 };
868
869
870 /**
871  * Main: start test
872  */
873 int
874 main (int argc, char *argv[])
875 {
876   char *const argv2[] = {
877     argv[0],
878     "-c",
879     "test_mesh_small.conf",
880 #if VERBOSE
881     "-L",
882     "DEBUG",
883 #endif
884     NULL
885   };
886   int ok_goal;
887
888   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Start\n");
889   if (strstr (argv[0], "test_mesh_small_unicast") != NULL)
890   {
891     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: UNICAST\n");
892     test = UNICAST;
893     ok_goal = 5;
894   }
895   else if (strstr (argv[0], "test_mesh_small_multicast") != NULL)
896   {
897     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: MULTICAST\n");
898     test = MULTICAST;
899     ok_goal = 10;
900   }
901   else
902   {
903     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: UNKNOWN\n");
904     test = SETUP;
905     ok_goal = 0;
906   }
907
908   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
909                       "test_mesh_small",
910                       gettext_noop ("Test mesh in a small network."),
911                       options, &run, NULL);
912 #if REMOVE_DIR
913   GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
914 #endif
915   if (ok_goal != ok)
916   {
917     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
918                 "***************** test: FAILED! (%d/%d)\n", ok, ok_goal);
919     return 1;
920   }
921   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922               "***************** test: success\n");
923   return 0;
924 }
925
926 /* end of test_mesh_small.c */