2f538b62c7932de4ad36146042e9406806f31873
[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 until we give up on connecting the peers?
53  */
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
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 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, "Shutdown of peers failed!\n");
76 #endif
77       if (ok == 0)
78         ok = 666;
79     }
80   else
81     {
82 #if VERBOSE
83       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84                   "All peers successfully shut down!\n");
85 #endif
86       ok = 0;
87     }
88 }
89
90 static void
91 shutdown_task (void *cls,
92                const struct GNUNET_SCHEDULER_TaskContext *tc)
93 {
94   struct NSEPeer *pos;
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 estimate the value of the current network size estimate
114  * @param std_dev standard deviation (rounded down to nearest integer)
115  *                of the size estimation values seen
116  *
117  */
118 static void
119 handle_estimate (void *cls, double estimate, double std_dev)
120 {
121   struct NSEPeer *peer = cls;
122   fprintf(stderr, "Received network size estimate from peer %s. Size: %f std.dev. %f\n", GNUNET_i2s(&peer->daemon->id), estimate, std_dev);
123 }
124
125 static void
126 connect_nse_service (void *cls,
127                      const struct GNUNET_SCHEDULER_TaskContext *tc)
128 {
129   struct NSEPeer *current_peer;
130   unsigned int i;
131 #if VERBOSE
132   fprintf(stderr, "TEST_NSE_MULTIPEER: connecting to nse service of peers\n");
133 #endif
134   for (i = 0; i < num_peers; i++)
135     {
136       current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
137       current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
138       current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg, &handle_estimate, current_peer);
139       GNUNET_assert(current_peer->nse_handle != NULL);
140
141       GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
142     }
143 }
144
145 static void
146 my_cb (void *cls,
147        const char *emsg)
148 {
149   if (emsg != NULL)
150     {
151       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152                   "Peergroup callback called with error, aborting test!\n");
153       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
154       ok = 1;
155       GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
156       return;
157     }
158 #if VERBOSE
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160               "Peer Group started successfully, connecting to NSE service for each peer!\n");
161 #endif
162   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n", total_connections);
163
164   GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
165 }
166
167 /**
168  * Prototype of a function that will be called whenever
169  * two daemons are connected by the testing library.
170  *
171  * @param cls closure
172  * @param first peer id for first daemon
173  * @param second peer id for the second daemon
174  * @param distance distance between the connected peers
175  * @param first_cfg config for the first daemon
176  * @param second_cfg config for the second daemon
177  * @param first_daemon handle for the first daemon
178  * @param second_daemon handle for the second daemon
179  * @param emsg error message (NULL on success)
180  */
181 void connect_cb (void *cls,
182                  const struct GNUNET_PeerIdentity *first,
183                  const struct GNUNET_PeerIdentity *second,
184                  uint32_t distance,
185                  const struct GNUNET_CONFIGURATION_Handle *first_cfg,
186                  const struct GNUNET_CONFIGURATION_Handle *second_cfg,
187                  struct GNUNET_TESTING_Daemon *first_daemon,
188                  struct GNUNET_TESTING_Daemon *second_daemon,
189                  const char *emsg)
190 {
191   char *second_id;
192
193   second_id = GNUNET_strdup(GNUNET_i2s(second));
194   if (emsg == NULL)
195     {
196       //fprintf(stderr, "Connected %s -> %s\n", GNUNET_i2s(first), second_id);
197       total_connections++;
198     }
199 }
200
201
202
203 static void
204 run (void *cls,
205      char *const *args,
206      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
207 {
208   struct GNUNET_CONFIGURATION_Handle *testing_cfg;
209   unsigned long long total_peers;
210   ok = 1;
211   testing_cfg = GNUNET_CONFIGURATION_create();
212   GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(testing_cfg, cfgfile));
213 #if VERBOSE
214   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
215   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
216                                            "use_progressbars",
217                                            "YES");
218 #endif
219   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing", "num_peers", &total_peers))
220     total_peers = NUM_PEERS;
221
222   peers_left = total_peers;
223   num_peers = peers_left;
224   pg = GNUNET_TESTING_peergroup_start(testing_cfg,
225                                       peers_left,
226                                       TIMEOUT,
227                                       &connect_cb,
228                                       &my_cb, NULL,
229                                       NULL);
230   GNUNET_assert (pg != NULL);
231   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
232 }
233
234 static int
235 check ()
236 {
237   char *const argv[] = { "test-nse-multipeer",
238     "-c",
239     "test_nse.conf",
240 #if VERBOSE
241     "-L", "DEBUG",
242 #endif
243     NULL
244   };
245   struct GNUNET_GETOPT_CommandLineOption options[] = {
246     GNUNET_GETOPT_OPTION_END
247   };
248   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
249                       argv, "test-nse-multipeer", "nohelp",
250                       options, &run, &ok);
251   return ok;
252 }
253
254 int
255 main (int argc, char *argv[])
256 {
257   int ret;
258
259   GNUNET_log_setup ("test-nse-multipeer",
260 #if VERBOSE
261                     "DEBUG",
262 #else
263                     "WARNING",
264 #endif
265                     NULL);
266   ret = check ();
267   GNUNET_DISK_directory_remove ("/tmp/test-nse-multipeer");
268   return ret;
269 }
270
271 /* end of test_nse_multipeer.c */