29e5cd6cc6a6ec6a6210bc36edad89580a84f194
[oweals/gnunet.git] / src / nse / test_nse_multipeer.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 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 nse/test_nse_multipeer.c
22  *
23  * @brief Testcase for the network size estimation service.  Starts
24  *        a peergroup with a given number of peers, then waits to
25  *        receive size estimates from each peer.  Expects to wait
26  *        for one message from each peer.
27  */
28 #include "platform.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_nse_service.h"
31
32 #define VERBOSE GNUNET_NO
33
34 #define NUM_PEERS 4
35
36 struct NSEPeer
37 {
38   struct NSEPeer *prev;
39
40   struct NSEPeer *next;
41
42   struct GNUNET_TESTING_Daemon *daemon;
43
44   struct GNUNET_NSE_Handle *nse_handle;
45 };
46
47 struct NSEPeer *peer_head;
48
49 struct NSEPeer *peer_tail;
50
51 /**
52  * How long do we run the test?
53  */
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
55
56 static int ok;
57
58 static int peers_left;
59
60 static unsigned int num_peers;
61
62 static unsigned int total_connections;
63
64 static struct GNUNET_TESTING_PeerGroup *pg;
65
66 /**
67  * Check whether peers successfully shut down.
68  */
69 static void
70 shutdown_callback (void *cls, const char *emsg)
71 {
72   if (emsg != NULL)
73   {
74 #if VERBOSE
75     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76                 "Shutdown of peers failed: %s!\n", emsg);
77 #endif
78     if (ok == 0)
79       ok = 666;
80   }
81   else
82   {
83 #if VERBOSE
84     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
85 #endif
86     ok = 0;
87   }
88 }
89
90 static void
91 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 {
93   struct NSEPeer *pos;
94
95 #if VERBOSE
96   fprintf (stderr, "Ending test.\n");
97 #endif
98
99   while (NULL != (pos = peer_head))
100   {
101     GNUNET_NSE_disconnect (pos->nse_handle);
102     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
103     GNUNET_free (pos);
104   }
105
106   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
107 }
108
109 /**
110  * Callback to call when network size estimate is updated.
111  *
112  * @param cls closure
113  * @param timestamp server timestamp
114  * @param estimate the value of the current network size estimate
115  * @param std_dev standard deviation (rounded down to nearest integer)
116  *                of the size estimation values seen
117  *
118  */
119 static void
120 handle_estimate (void *cls,
121                  struct GNUNET_TIME_Absolute timestamp,
122                  double estimate, double std_dev)
123 {
124   struct NSEPeer *peer = cls;
125
126   fprintf (stderr,
127            "Received network size estimate from peer %s. logSize: %f std.dev. %f (%f/%u)\n",
128            GNUNET_i2s (&peer->daemon->id),
129            estimate,
130            std_dev, GNUNET_NSE_log_estimate_to_n (estimate), num_peers);
131 }
132
133
134 static void
135 connect_nse_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 {
137   struct NSEPeer *current_peer;
138   unsigned int i;
139
140 #if VERBOSE
141   fprintf (stderr, "TEST_NSE_MULTIPEER: connecting to nse service of peers\n");
142 #endif
143   for (i = 0; i < num_peers; i++)
144   {
145     current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
146     current_peer->daemon = GNUNET_TESTING_daemon_get (pg, i);
147     current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
148                                                    &handle_estimate,
149                                                    current_peer);
150     GNUNET_assert (current_peer->nse_handle != NULL);
151     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
152   }
153 }
154
155
156 static void
157 my_cb (void *cls, const char *emsg)
158 {
159   if (emsg != NULL)
160   {
161     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162                 "Peergroup callback called with error, aborting test!\n");
163     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
164     ok = 1;
165     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
166     return;
167   }
168 #if VERBOSE
169   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170               "Peer Group started successfully, connecting to NSE service for each peer!\n");
171 #endif
172   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
173               "Have %u connections\n", total_connections);
174
175   GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
176 }
177
178
179 /**
180  * Function that will be called whenever
181  * two daemons are connected by the testing library.
182  *
183  * @param cls closure
184  * @param first peer id for first daemon
185  * @param second peer id for the second daemon
186  * @param distance distance between the connected peers
187  * @param first_cfg config for the first daemon
188  * @param second_cfg config for the second daemon
189  * @param first_daemon handle for the first daemon
190  * @param second_daemon handle for the second daemon
191  * @param emsg error message (NULL on success)
192  */
193 static void
194 connect_cb (void *cls,
195             const struct GNUNET_PeerIdentity *first,
196             const struct GNUNET_PeerIdentity *second,
197             uint32_t distance,
198             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
199             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
200             struct GNUNET_TESTING_Daemon *first_daemon,
201             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
202 {
203   if (emsg == NULL)
204   {
205     //fprintf(stderr, "Connected %s -> %s\n", GNUNET_i2s(first), second_id);
206     total_connections++;
207   }
208 }
209
210
211
212 static void
213 run (void *cls,
214      char *const *args,
215      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
216 {
217   struct GNUNET_CONFIGURATION_Handle *testing_cfg;
218   unsigned long long total_peers;
219
220   ok = 1;
221   testing_cfg = GNUNET_CONFIGURATION_create ();
222   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (testing_cfg, cfgfile));
223
224 #if VERBOSE
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
226   GNUNET_CONFIGURATION_set_value_string (testing_cfg,
227                                          "testing", "use_progressbars", "YES");
228 #endif
229   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
230                                                           "testing",
231                                                           "num_peers",
232                                                           &total_peers))
233     total_peers = NUM_PEERS;
234
235   peers_left = total_peers;
236   num_peers = peers_left;
237   pg = GNUNET_TESTING_peergroup_start (testing_cfg,
238                                        peers_left,
239                                        TIMEOUT,
240                                        &connect_cb, &my_cb, NULL, NULL);
241   GNUNET_assert (pg != NULL);
242   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
243 }
244
245
246 static int
247 check ()
248 {
249   char *const argv[] = { "test-nse-multipeer",
250     "-c",
251     "test_nse.conf",
252 #if VERBOSE
253     "-L", "DEBUG",
254 #endif
255     NULL
256   };
257   struct GNUNET_GETOPT_CommandLineOption options[] = {
258     GNUNET_GETOPT_OPTION_END
259   };
260   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
261                       argv, "test-nse-multipeer", "nohelp", options, &run, &ok);
262   return ok;
263 }
264
265
266 int
267 main (int argc, char *argv[])
268 {
269   int ret;
270
271   GNUNET_log_setup ("test-nse-multipeer",
272 #if VERBOSE
273                     "DEBUG",
274 #else
275                     "WARNING",
276 #endif
277                     NULL);
278   ret = check ();
279   // GNUNET_DISK_directory_remove ("/tmp/test-nse-multipeer");
280   return ret;
281 }
282
283 /* end of test_nse_multipeer.c */