doxygen-fixes
[oweals/gnunet.git] / src / topology / test_gnunet_service_topology.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 topology/test_gnunet_service_topology.c
22  * @brief testcase for topology maintenance code
23  */
24 #include "platform.h"
25 #include "gnunet_testing_lib.h"
26
27 #define VERBOSE GNUNET_YES
28
29 #define NUM_PEERS 2
30
31 /**
32  * How long until we give up on connecting the peers?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
35
36
37 static int ok;
38
39 static int peers_left;
40
41 static int connect_left;
42
43 static struct GNUNET_TESTING_PeerGroup *pg;
44
45 static struct GNUNET_TESTING_Daemon *first;
46
47 static struct GNUNET_TESTING_Daemon *last;
48
49 static struct GNUNET_SCHEDULER_Handle *sched;
50
51
52 static void 
53 notify_connect_complete(void *cls,
54                         const char *emsg)
55 {
56   if (NULL != emsg)
57     {
58       fprintf (stderr,
59                "Failed to connect two peers: %s\n",
60                emsg);
61       GNUNET_assert (0);
62       return;
63     }
64   connect_left--;
65   if (connect_left == 0)
66     {
67       /* FIXME: check that topology adds a few more links
68          in addition to those that were seeded */
69       /* For now, sleep so we can have the daemon do some work */
70       sleep (10);
71       GNUNET_TESTING_daemons_stop (pg);
72       ok = 0;     
73     }
74 }
75
76
77 static void my_cb(void *cls,
78                    const struct GNUNET_PeerIdentity *id,
79                    const struct GNUNET_CONFIGURATION_Handle *cfg,
80                    struct GNUNET_TESTING_Daemon *d,
81                    const char *emsg)
82 {
83   GNUNET_assert (id != NULL);
84   peers_left--;  
85   if (first == NULL)
86     {
87       connect_left = NUM_PEERS;
88       first = d;
89       last = d;
90       return;
91     }
92   GNUNET_TESTING_daemons_connect (last, d, TIMEOUT,
93                                   &notify_connect_complete,
94                                   NULL);
95   if (peers_left == 0)
96     {
97       /* close circle */
98       GNUNET_TESTING_daemons_connect (d, first, TIMEOUT,
99                                       &notify_connect_complete,
100                                       NULL);
101     }
102 }
103
104
105 static void
106 run (void *cls,
107      struct GNUNET_SCHEDULER_Handle *s,
108      char *const *args,
109      const char *cfgfile,
110      const struct GNUNET_CONFIGURATION_Handle *cfg)
111 {
112   sched = s;
113   ok = 1;
114 #if VERBOSE
115   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116               "Starting daemons.\n");
117 #endif
118   peers_left = NUM_PEERS;
119   pg = GNUNET_TESTING_daemons_start (sched, cfg, 
120                                      peers_left,
121                                      &my_cb, NULL, NULL);
122   GNUNET_assert (pg != NULL);
123 }
124
125 static int
126 check ()
127 {
128   char *const argv[] = { "test-testing",
129     "-c",
130     "test_gnunet_service_topology_data.conf",
131 #if VERBOSE
132     "-L", "DEBUG",
133 #endif
134     NULL
135   };
136   struct GNUNET_GETOPT_CommandLineOption options[] = {
137     GNUNET_GETOPT_OPTION_END
138   };
139   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
140                       argv, "test-gnunet-service-topology", "nohelp",
141                       options, &run, &ok);
142   return ok;
143 }
144
145 int
146 main (int argc, char *argv[])
147 {
148   int ret;
149
150   GNUNET_log_setup ("test-gnunet-service-topology",
151 #if VERBOSE
152                     "DEBUG",
153 #else
154                     "WARNING",
155 #endif
156                     NULL);
157   ret = check ();
158   sleep (1); /* FIXME: needed? */
159   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-topology");
160   return ret;
161 }
162
163 /* end of test_gnunet_service_topology.c */