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_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_rps_service.h"
36 * How many peers do we start?
41 * How long do we run the test?
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 * Portion of malicious peers
49 static double portion = .1;
52 * Type of malicious peer to test
54 static unsigned int mal_type = 0;
58 * Information we track for each peer.
68 * Handle for RPS connect operation.
70 struct GNUNET_TESTBED_Operation *op;
73 * Handle to RPS service.
75 struct GNUNET_RPS_Handle *rps_handle;
80 struct GNUNET_PeerIdentity *peer_id;
83 * A request handle to check for an request
85 //struct GNUNET_RPS_Request_Handle *req_handle;
90 struct GNUNET_PeerIdentity *rec_ids;
93 * Number of received PeerIDs
95 unsigned int num_rec_ids;
100 * Information for all the peers.
102 static struct RPSPeer rps_peers[NUM_PEERS];
107 static struct GNUNET_PeerIdentity rps_peer_ids[NUM_PEERS];
110 * Return value from 'main'.
116 * Called directly after connecting to the service
118 typedef void (*PreTest) (void *cls, struct GNUNET_RPS_Handle *h);
121 * Called from within #rps_connect_complete_cb ()
122 * Executes functions to test the api/service
124 typedef void (*MainTest) (struct RPSPeer *rps_peer);
127 * Function called after disconnect to evaluate test success
129 typedef int (*EvaluationCallback) (void);
133 * Structure to define a single test
138 * Called directly after connecting to the service
143 * Function to execute the functions to be tested
148 * Function to evaluate the test results
150 EvaluationCallback eval_cb;
155 * Test the success of a single test
158 evaluate (struct RPSPeer *loc_rps_peers,
159 unsigned int num_loc_rps_peers,
160 unsigned int expected_recv)
165 tmp_ok = (1 == loc_rps_peers[0].num_rec_ids);
167 for (i = 0 ; i < num_loc_rps_peers ; i++)
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
172 GNUNET_i2s (loc_rps_peers[i].peer_id),
173 loc_rps_peers[i].num_rec_ids,
175 (1 == loc_rps_peers[i].num_rec_ids));
176 tmp_ok &= (1 == loc_rps_peers[i].num_rec_ids);
178 return tmp_ok? 0 : 1;
183 * Task run on timeout to shut everything down.
186 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 for (i=0;i<NUM_PEERS;i++)
191 GNUNET_TESTBED_operation_done (rps_peers[i].op);
192 GNUNET_SCHEDULER_shutdown ();
197 * Callback to call on receipt of a reply
200 * @param n number of peers
201 * @param recv_peers the received peers
204 handle_reply (void *cls, uint64_t n, const struct GNUNET_PeerIdentity *recv_peers)
206 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "[%s] got %" PRIu64 " peers:\n",
211 GNUNET_i2s (rps_peer->peer_id),
214 for (i = 0 ; i < n ; i++)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 GNUNET_i2s (&recv_peers[i]));
221 GNUNET_array_append (rps_peer->rec_ids, rps_peer->num_rec_ids, recv_peers[i]);
227 * Request random peers.
230 request_peers (void *cls,
231 const struct GNUNET_SCHEDULER_TaskContext *tc)
233 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Requesting one peer\n");
238 (void) GNUNET_RPS_request_peers (rps_peer->rps_handle, 1, handle_reply, rps_peer);
239 //rps_peer->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle, 1, handle_reply, rps_peer);
247 seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
250 struct RPSPeer *peer = (struct RPSPeer *) cls;
253 // TODO if malicious don't seed mal peers
254 amount = round (.5 * NUM_PEERS);
256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
257 for (i = 0 ; i < amount ; i++)
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
260 GNUNET_i2s (&rps_peer_ids[i]));
262 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
267 * Get the id of peer i.
270 info_cb (void *cb_cls,
271 struct GNUNET_TESTBED_Operation *op,
272 const struct GNUNET_TESTBED_PeerInformation *pinfo,
275 unsigned int i = *((unsigned int *) cb_cls);
277 if (NULL == pinfo || NULL != emsg)
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
283 GNUNET_free (cb_cls);
285 rps_peer_ids[i] = *(pinfo->result.id);
286 rps_peers[i].peer_id = &rps_peer_ids[i];
287 rps_peers[i].rec_ids = NULL;
288 rps_peers[i].num_rec_ids = 0;
294 * Callback to be called when RPS service connect operation is completed
296 * @param cls the callback closure from functions generating an operation
297 * @param op the operation that has been finished
298 * @param ca_result the RPS service handle returned from rps_connect_adapter
299 * @param emsg error message in case the operation has failed; will be NULL if
300 * operation has executed successfully.
303 rps_connect_complete_cb (void *cls,
304 struct GNUNET_TESTBED_Operation *op,
308 struct RPSPeer *rps_peer = cls;
309 struct GNUNET_RPS_Handle *rps = ca_result;
311 rps_peer->rps_handle = rps;
313 GNUNET_assert (op == rps_peer->op);
316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
317 "Failed to connect to RPS service: %s\n",
320 GNUNET_SCHEDULER_shutdown ();
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
326 cur_test_run.main_test (rps_peer);
331 * Adapter function called to establish a connection to
335 * @param cfg configuration of the peer to connect to; will be available until
336 * GNUNET_TESTBED_operation_done() is called on the operation returned
337 * from GNUNET_TESTBED_service_connect()
338 * @return service handle to return in 'op_result', NULL on error
341 rps_connect_adapter (void *cls,
342 const struct GNUNET_CONFIGURATION_Handle *cfg)
344 struct GNUNET_RPS_Handle *h;
346 h = GNUNET_RPS_connect (cfg);
348 if (NULL != cur_test_run.pre_test)
349 cur_test_run.pre_test (cls, h);
356 * Adapter function called to destroy connection to
360 * @param op_result service handle returned from the connect adapter
363 rps_disconnect_adapter (void *cls,
366 struct GNUNET_RPS_Handle *h = op_result;
367 GNUNET_RPS_disconnect (h);
371 /***********************************************************************
372 * Definition of tests
373 ***********************************************************************/
376 default_eval_cb (void)
378 return evaluate (rps_peers, NUM_PEERS, 1);
380 /***********************************
382 ***********************************/
384 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
386 #ifdef ENABLE_MALICIOUS
387 uint32_t num_mal_peers;
388 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
390 GNUNET_assert (1 >= portion
392 num_mal_peers = round (portion * NUM_PEERS);
394 if (rps_peer->index < num_mal_peers)
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
399 GNUNET_i2s (rps_peer->peer_id),
402 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers, rps_peer_ids);
404 #endif /* ENABLE_MALICIOUS */
408 mal_cb (struct RPSPeer *rps_peer)
410 uint32_t num_mal_peers;
412 #ifdef ENABLE_MALICIOUS
413 GNUNET_assert (1 >= portion
415 num_mal_peers = round (portion * NUM_PEERS);
417 if (rps_peer->index >= num_mal_peers)
418 { /* It's useless to ask a malicious peer about a random sample -
420 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
421 seed_peers, rps_peer);
422 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
423 request_peers, rps_peer);
425 #endif /* ENABLE_MALICIOUS */
431 unsigned int num_mal_peers;
433 num_mal_peers = round (NUM_PEERS * portion);
434 return evaluate (&rps_peers[num_mal_peers],
435 NUM_PEERS - (num_mal_peers),
440 /***********************************
442 ***********************************/
444 single_req_cb (struct RPSPeer *rps_peer)
446 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
447 request_peers, rps_peer);
450 /***********************************
452 ***********************************/
454 delay_req_cb (struct RPSPeer *rps_peer)
456 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
457 request_peers, rps_peer);
458 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
459 request_peers, rps_peer);
462 /***********************************
464 ***********************************/
466 seed_cb (struct RPSPeer *rps_peer)
468 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
469 seed_peers, rps_peer);
478 /***********************************
480 ***********************************/
482 seed_big_cb (struct RPSPeer *rps_peer)
484 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
487 /***********************************
489 ***********************************/
491 single_peer_seed_cb (struct RPSPeer *rps_peer)
496 /***********************************
498 ***********************************/
500 seed_req_cb (struct RPSPeer *rps_peer)
502 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
503 seed_peers, rps_peer);
504 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15),
505 request_peers, rps_peer);
510 /***********************************
512 ***********************************/
514 req_cancel_cb (struct RPSPeer *rps_peer)
519 /***********************************************************************
520 * /Definition of tests
521 ***********************************************************************/
525 * Actual "main" function for the testcase.
528 * @param h the run handle
529 * @param num_peers number of peers in 'peers'
530 * @param peers handle to peers run in the testbed
531 * @param links_succeeded the number of overlay link connection attempts that
533 * @param links_failed the number of overlay link connection attempts that
538 struct GNUNET_TESTBED_RunHandle *h,
539 unsigned int num_peers,
540 struct GNUNET_TESTBED_Peer **peers,
541 unsigned int links_succeeded,
542 unsigned int links_failed)
547 for (i = 0 ; i < NUM_PEERS ; i++)
549 tmp_i = GNUNET_new (unsigned int);
552 (void) GNUNET_TESTBED_peer_get_information (peers[i],
553 GNUNET_TESTBED_PIT_IDENTITY,
558 GNUNET_assert (NUM_PEERS == num_peers);
559 for (i = 0 ; i < num_peers ; i++)
561 rps_peers[i].index = i;
563 GNUNET_TESTBED_service_connect (&rps_peers[i],
566 &rps_connect_complete_cb,
568 &rps_connect_adapter,
569 &rps_disconnect_adapter,
572 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
577 * Entry point for the testcase, sets up the testbed.
581 * @return 0 on success
584 main (int argc, char *argv[])
586 cur_test_run.pre_test = NULL;
587 cur_test_run.eval_cb = default_eval_cb;
589 if (strstr (argv[0], "malicious") != NULL)
591 cur_test_run.pre_test = mal_pre;
592 cur_test_run.main_test = mal_cb;
593 cur_test_run.eval_cb = mal_eval;
595 if (strstr (argv[0], "_1") != NULL)
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
600 else if (strstr (argv[0], "_2") != NULL)
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
605 else if (strstr (argv[0], "_3") != NULL)
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
612 else if (strstr (argv[0], "_single_req") != NULL)
614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
615 cur_test_run.main_test = single_req_cb;
617 else if (strstr (argv[0], "_delayed_reqs") != NULL)
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
620 cur_test_run.main_test = delay_req_cb;
622 else if (strstr (argv[0], "_seed_big") != NULL)
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
625 cur_test_run.main_test = seed_big_cb;
627 else if (strstr (argv[0], "_single_peer_seed") != NULL)
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
630 cur_test_run.main_test = single_peer_seed_cb;
632 else if (strstr (argv[0], "_seed_request") != NULL)
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
635 cur_test_run.main_test = seed_req_cb;
637 else if (strstr (argv[0], "_seed") != NULL)
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
640 cur_test_run.main_test = seed_cb;
641 cur_test_run.eval_cb = seed_eval;
643 else if (strstr (argv[0], "_req_cancel") != NULL)
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
646 cur_test_run.main_test = req_cancel_cb;
650 (void) GNUNET_TESTBED_test_run ("test-rps-multipeer",
656 return cur_test_run.eval_cb();
659 /* end of test_rps_multipeer.c */