600e0c2f2c7e786a7d73c677e361551e49b146ea
[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 struct GNUNET_TESTING_PeerGroup *pg;
63
64 /**
65  * Check whether peers successfully shut down.
66  */
67 void
68 shutdown_callback (void *cls, const char *emsg)
69 {
70   if (emsg != NULL)
71     {
72 #if VERBOSE
73       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
74 #endif
75       if (ok == 0)
76         ok = 666;
77     }
78   else
79     {
80 #if VERBOSE
81       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
82                   "All peers successfully shut down!\n");
83 #endif
84       ok = 0;
85     }
86 }
87
88 static void
89 shutdown_task (void *cls,
90                const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92   struct NSEPeer *pos;
93 #if VERBOSE
94   fprintf(stderr, "Ending test.\n");
95 #endif
96
97   while (NULL != (pos = peer_head))
98     {
99       GNUNET_NSE_disconnect(pos->nse_handle);
100       GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
101       GNUNET_free(pos);
102     }
103
104   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
105 }
106
107 /**
108  * Callback to call when network size estimate is updated.
109  *
110  * @param cls closure
111  * @param estimate the value of the current network size estimate
112  * @param std_dev standard deviation (rounded down to nearest integer)
113  *                of the size estimation values seen
114  *
115  */
116 static void
117 handle_estimate (void *cls, double estimate, double std_dev)
118 {
119   struct NSEPeer *peer = cls;
120   fprintf(stderr, "Received network size estimate from peer %s. Size: %f std.dev. %f\n", GNUNET_i2s(&peer->daemon->id), estimate, std_dev);
121 }
122
123 static void
124 connect_nse_service (void *cls,
125                      const struct GNUNET_SCHEDULER_TaskContext *tc)
126 {
127   struct NSEPeer *current_peer;
128   unsigned int i;
129 #if VERBOSE
130   fprintf(stderr, "TEST_NSE_MULTIPEER: connecting to nse service of peers\n");
131 #endif
132   for (i = 0; i < num_peers; i++)
133     {
134       current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
135       current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
136       current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg, &handle_estimate, current_peer);
137       GNUNET_assert(current_peer->nse_handle != NULL);
138
139       GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
140     }
141 }
142
143 static void
144 my_cb (void *cls,
145        const char *emsg)
146 {
147   if (emsg != NULL)
148     {
149       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150                   "Peergroup callback called with error, aborting test!\n");
151       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
152       ok = 1;
153       GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
154       return;
155     }
156 #if VERBOSE
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158               "Peer Group started successfully, connecting to NSE service for each peer!\n");
159 #endif
160
161   GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
162 }
163
164
165 static void
166 run (void *cls,
167      char *const *args,
168      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
169 {
170   struct GNUNET_CONFIGURATION_Handle *testing_cfg;
171   unsigned long long total_peers;
172   ok = 1;
173   testing_cfg = GNUNET_CONFIGURATION_create();
174   GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(testing_cfg, cfgfile));
175 #if VERBOSE
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
177   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
178                                            "use_progressbars",
179                                            "YES");
180 #endif
181   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing", "num_peers", &total_peers))
182     total_peers = NUM_PEERS;
183
184   peers_left = total_peers;
185   num_peers = peers_left;
186   pg = GNUNET_TESTING_peergroup_start(testing_cfg,
187                                       peers_left,
188                                       TIMEOUT,
189                                       NULL,
190                                       &my_cb, NULL,
191                                       NULL);
192   GNUNET_assert (pg != NULL);
193   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
194 }
195
196 static int
197 check ()
198 {
199   char *const argv[] = { "test-nse-multipeer",
200     "-c",
201     "test_nse.conf",
202 #if VERBOSE
203     "-L", "DEBUG",
204 #endif
205     NULL
206   };
207   struct GNUNET_GETOPT_CommandLineOption options[] = {
208     GNUNET_GETOPT_OPTION_END
209   };
210   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
211                       argv, "test-nse-multipeer", "nohelp",
212                       options, &run, &ok);
213   return ok;
214 }
215
216 int
217 main (int argc, char *argv[])
218 {
219   int ret;
220
221   GNUNET_log_setup ("test-nse-multipeer",
222 #if VERBOSE
223                     "DEBUG",
224 #else
225                     "WARNING",
226 #endif
227                     NULL);
228   ret = check ();
229   GNUNET_DISK_directory_remove ("/tmp/test-nse-multipeer");
230   return ret;
231 }
232
233 /* end of test_nse_multipeer.c */