2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file rps/test_rps_multipeer.c
22 * @brief Testcase for the random peer sampling service. Starts
23 * a peergroup with a given number of peers, then waits to
24 * receive size pushes/pulls from each peer. Expects to wait
25 * for one message from each peer.
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_rps_service.h"
35 * How many peers do we start?
40 * How long do we run the test?
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
46 * Portion of malicious peers
48 static double portion = .1;
51 * Type of malicious peer to test
53 static unsigned int mal_type = 0;
57 * Information we track for each peer.
67 * Handle for RPS connect operation.
69 struct GNUNET_TESTBED_Operation *op;
72 * Handle to RPS service.
74 struct GNUNET_RPS_Handle *rps_handle;
79 struct GNUNET_PeerIdentity *peer_id;
84 * Information for all the peers.
86 static struct RPSPeer rps_peers[NUM_PEERS];
91 static struct GNUNET_PeerIdentity rps_peer_ids[NUM_PEERS];
94 * Return value from 'main'.
100 * Task run on timeout to shut everything down.
103 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 for (i=0;i<NUM_PEERS;i++)
108 GNUNET_TESTBED_operation_done (rps_peers[i].op);
109 GNUNET_SCHEDULER_shutdown ();
114 * Callback to call when network size estimate is updated.
117 * @param timestamp server timestamp
118 * @param estimate the value of the current network size estimate
119 * @param std_dev standard deviation (rounded down to nearest integer)
120 * of the size estimation values seen
124 handle_reply (void *cls, uint64_t n, const struct GNUNET_PeerIdentity *peers)
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got peer %s\n", GNUNET_i2s (peers));
133 * (Randomly) request random peers.
136 request_peers (void *cls,
137 const struct GNUNET_SCHEDULER_TaskContext *tc)
139 struct RPSPeer *peer = (struct RPSPeer *) cls;
140 struct GNUNET_RPS_Request_Handle *req_handle;
142 req_handle = GNUNET_RPS_request_peers (peer->rps_handle, 1, handle_reply, NULL);
143 GNUNET_free (req_handle);
151 seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
154 struct RPSPeer *peer = (struct RPSPeer *) cls;
157 GNUNET_assert (1 >= portion
160 amount = round (portion * NUM_PEERS);
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
163 for (i = 0 ; i < amount ; i++)
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
166 GNUNET_i2s (&rps_peer_ids[i]));
168 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
173 * Get the id of peer i.
176 info_cb (void *cb_cls,
177 struct GNUNET_TESTBED_Operation *op,
178 const struct GNUNET_TESTBED_PeerInformation *pinfo,
181 unsigned int i = *((unsigned int *) cb_cls);
183 if (NULL == pinfo || NULL != emsg)
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
189 GNUNET_free (cb_cls);
191 rps_peer_ids[i] = *(pinfo->result.id);
192 rps_peers[i].peer_id = &rps_peer_ids[i];
198 * Callback to be called when RPS service connect operation is completed
200 * @param cls the callback closure from functions generating an operation
201 * @param op the operation that has been finished
202 * @param ca_result the RPS service handle returned from rps_connect_adapter
203 * @param emsg error message in case the operation has failed; will be NULL if
204 * operation has executed successfully.
207 rps_connect_complete_cb (void *cls,
208 struct GNUNET_TESTBED_Operation *op,
212 struct RPSPeer *rps_peer = cls;
213 struct GNUNET_RPS_Handle *rps = ca_result;
214 struct GNUNET_RPS_Request_Handle *req_handle;
215 uint32_t num_mal_peers;
217 rps_peer->rps_handle = rps;
219 GNUNET_assert (op == rps_peer->op);
222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223 "Failed to connect to RPS service: %s\n",
226 GNUNET_SCHEDULER_shutdown ();
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
232 #ifdef ENABLE_MALICIOUS
236 GNUNET_assert (1 >= portion
238 num_mal_peers = round (portion * NUM_PEERS);
240 if (rps_peer->index >= num_mal_peers)
241 { /* It's useless to ask a malicious peer about a random sample -
243 req_handle = GNUNET_RPS_request_peers (rps, 1, handle_reply, NULL);
244 GNUNET_free (req_handle);
248 #endif /* ENABLE_MALICIOUS */
250 req_handle = GNUNET_RPS_request_peers (rps, 1, handle_reply, NULL);
251 GNUNET_free (req_handle);
252 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
253 request_peers, rps_peer);
254 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
255 seed_peers, rps_peer);
256 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
261 * Adapter function called to establish a connection to
265 * @param cfg configuration of the peer to connect to; will be available until
266 * GNUNET_TESTBED_operation_done() is called on the operation returned
267 * from GNUNET_TESTBED_service_connect()
268 * @return service handle to return in 'op_result', NULL on error
271 rps_connect_adapter (void *cls,
272 const struct GNUNET_CONFIGURATION_Handle *cfg)
274 struct GNUNET_RPS_Handle *h;
275 #ifdef ENABLE_MALICIOUS
276 uint32_t num_mal_peers;
277 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
278 #endif /* ENABLE_MALICIOUS */
280 h = GNUNET_RPS_connect (cfg);
282 #ifdef ENABLE_MALICIOUS
283 GNUNET_assert (1 >= portion
285 num_mal_peers = round (portion * NUM_PEERS);
287 if (rps_peer->index < num_mal_peers)
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
292 GNUNET_i2s (rps_peer->peer_id),
295 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers, rps_peer_ids);
297 #endif /* ENABLE_MALICIOUS */
304 * Adapter function called to destroy connection to
308 * @param op_result service handle returned from the connect adapter
311 rps_disconnect_adapter (void *cls,
314 struct GNUNET_RPS_Handle *h = op_result;
315 GNUNET_RPS_disconnect (h);
320 * Actual "main" function for the testcase.
323 * @param h the run handle
324 * @param num_peers number of peers in 'peers'
325 * @param peers handle to peers run in the testbed
326 * @param links_succeeded the number of overlay link connection attempts that
328 * @param links_failed the number of overlay link connection attempts that
333 struct GNUNET_TESTBED_RunHandle *h,
334 unsigned int num_peers,
335 struct GNUNET_TESTBED_Peer **peers,
336 unsigned int links_succeeded,
337 unsigned int links_failed)
342 for (i = 0 ; i < NUM_PEERS ; i++)
344 tmp_i = GNUNET_new (unsigned int);
347 (void) GNUNET_TESTBED_peer_get_information (peers[i],
348 GNUNET_TESTBED_PIT_IDENTITY,
354 GNUNET_assert (NUM_PEERS == num_peers);
355 for (i = 0 ; i < num_peers ; i++)
357 rps_peers[i].index = i;
359 GNUNET_TESTBED_service_connect (&rps_peers[i],
362 &rps_connect_complete_cb,
364 &rps_connect_adapter,
365 &rps_disconnect_adapter,
368 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
373 * Entry point for the testcase, sets up the testbed.
377 * @return 0 on success
380 main (int argc, char *argv[])
382 if (strstr (argv[0], "malicious_1") != NULL)
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Malicious type 1\n");
387 else if (strstr (argv[0], "malicious_2") != NULL)
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Malicious type 2\n");
394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Type unknown using type 0 - no malicious behaviour\n");
399 (void) GNUNET_TESTBED_test_run ("test-rps-multipeer",
407 /* end of test_rps_multipeer.c */