adding some debug messages
[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_NO
28
29 #define NUM_PEERS 10
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       GNUNET_TESTING_daemons_stop (pg);
70       ok = 0;     
71     }
72 }
73
74
75 static void my_cb(void *cls,
76                    const struct GNUNET_PeerIdentity *id,
77                    const struct GNUNET_CONFIGURATION_Handle *cfg,
78                    struct GNUNET_TESTING_Daemon *d,
79                    const char *emsg)
80 {
81   GNUNET_assert (id != NULL);
82   peers_left--;  
83   if (first == NULL)
84     {
85       connect_left = NUM_PEERS;
86       first = d;
87       last = d;
88       return;
89     }
90   GNUNET_TESTING_daemons_connect (last, d, TIMEOUT,
91                                   &notify_connect_complete,
92                                   NULL);
93   if (peers_left == 0)
94     {
95       /* close circle */
96       GNUNET_TESTING_daemons_connect (d, first, TIMEOUT,
97                                       &notify_connect_complete,
98                                       NULL);
99     }
100 }
101
102
103 static void
104 run (void *cls,
105      struct GNUNET_SCHEDULER_Handle *s,
106      char *const *args,
107      const char *cfgfile,
108      const struct GNUNET_CONFIGURATION_Handle *cfg)
109 {
110   sched = s;
111   ok = 1;
112 #if VERBOSE
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114               "Starting daemons.\n");
115 #endif
116   peers_left = NUM_PEERS;
117   pg = GNUNET_TESTING_daemons_start (sched, cfg, 
118                                      peers_left,
119                                      &my_cb, NULL, NULL);
120   GNUNET_assert (pg != NULL);
121 }
122
123 static int
124 check ()
125 {
126   char *const argv[] = { "test-testing",
127     "-c",
128     "test_gnunet_service_topology_data.conf",
129 #if VERBOSE
130     "-L", "DEBUG",
131 #endif
132     NULL
133   };
134   struct GNUNET_GETOPT_CommandLineOption options[] = {
135     GNUNET_GETOPT_OPTION_END
136   };
137   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
138                       argv, "test-gnunet-service-topology", "nohelp",
139                       options, &run, &ok);
140   return ok;
141 }
142
143 int
144 main (int argc, char *argv[])
145 {
146   int ret;
147
148   GNUNET_log_setup ("test-gnunet-service-topology",
149 #if VERBOSE
150                     "DEBUG",
151 #else
152                     "WARNING",
153 #endif
154                     NULL);
155   ret = check ();
156   sleep (1);
157   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-topology");
158   return ret;
159 }
160
161 /* end of test_gnunet_service_topology.c */