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;
57 * Handles to all of the running peers
59 static struct GNUNET_TESTBED_Peer **testbed_peers;
70 struct OpListEntry *next;
75 struct OpListEntry *prev;
78 * The testbed operation
80 struct GNUNET_TESTBED_Operation *op;
83 * Depending on whether we start or stop NSE service at the peer set this to 1
89 * Index of the regarding peer
97 static struct OpListEntry *oplist_head;
102 static struct OpListEntry *oplist_tail;
106 * Information we track for each peer.
116 * Handle for RPS connect operation.
118 struct GNUNET_TESTBED_Operation *op;
121 * Handle to RPS service.
123 struct GNUNET_RPS_Handle *rps_handle;
128 struct GNUNET_PeerIdentity *peer_id;
131 * A request handle to check for an request
133 //struct GNUNET_RPS_Request_Handle *req_handle;
136 * Peer on- or offline?
143 struct GNUNET_PeerIdentity *rec_ids;
146 * Number of received PeerIDs
148 unsigned int num_rec_ids;
153 * Information for all the peers.
155 static struct RPSPeer rps_peers[NUM_PEERS];
160 static struct GNUNET_PeerIdentity rps_peer_ids[NUM_PEERS];
163 * Number of online peers.
165 static unsigned int num_peers_online;
168 * Return value from 'main'.
174 * Identifier for the churn task that runs periodically
176 static struct GNUNET_SCHEDULER_Task *churn_task;
180 * Called directly after connecting to the service
182 typedef void (*PreTest) (void *cls, struct GNUNET_RPS_Handle *h);
185 * Called from within #rps_connect_complete_cb ()
186 * Executes functions to test the api/service
188 typedef void (*MainTest) (struct RPSPeer *rps_peer);
191 * Called directly before disconnecting from the service
193 typedef void (*PostTest) (void *cls, struct GNUNET_RPS_Handle *h);
196 * Function called after disconnect to evaluate test success
198 typedef int (*EvaluationCallback) (void);
202 * Structure to define a single test
207 * Called directly after connecting to the service
212 * Function to execute the functions to be tested
217 * Called directly before disconnecting from the service
222 * Function to evaluate the test results
224 EvaluationCallback eval_cb;
229 * Test the success of a single test
232 evaluate (struct RPSPeer *loc_rps_peers,
233 unsigned int num_loc_rps_peers,
234 unsigned int expected_recv)
239 tmp_ok = (1 == loc_rps_peers[0].num_rec_ids);
241 for (i = 0 ; i < num_loc_rps_peers ; i++)
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
246 GNUNET_i2s (loc_rps_peers[i].peer_id),
247 loc_rps_peers[i].num_rec_ids,
249 (1 == loc_rps_peers[i].num_rec_ids));
250 tmp_ok &= (1 == loc_rps_peers[i].num_rec_ids);
252 return tmp_ok? 0 : 1;
257 * Creates an oplist entry and adds it to the oplist DLL
259 static struct OpListEntry *
262 struct OpListEntry *entry;
264 entry = GNUNET_new (struct OpListEntry);
265 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
271 * Callback to be called when NSE service is started or stopped at peers
274 * @param op the operation handle
275 * @param emsg NULL on success; otherwise an error description
279 struct GNUNET_TESTBED_Operation *op,
282 struct OpListEntry *entry = cls;
284 GNUNET_TESTBED_operation_done (entry->op);
287 //LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop NSE at a peer\n");
288 GNUNET_SCHEDULER_shutdown ();
291 GNUNET_assert (0 != entry->delta);
293 num_peers_online += entry->delta;
295 if (0 < entry->delta)
296 { /* Peer hopefully just went online */
297 GNUNET_break (GNUNET_NO == rps_peers[entry->index].online);
298 rps_peers[entry->index].online = GNUNET_YES;
300 else if (0 > entry->delta)
301 { /* Peer hopefully just went offline */
302 GNUNET_break (GNUNET_YES == rps_peers[entry->index].online);
303 rps_peers[entry->index].online = GNUNET_NO;
306 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
308 //if (num_peers_in_round[current_round] == peers_running)
314 * Task run on timeout to shut everything down.
317 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
321 for (i=0;i<NUM_PEERS;i++)
322 GNUNET_TESTBED_operation_done (rps_peers[i].op);
323 GNUNET_SCHEDULER_shutdown ();
328 * Callback to call on receipt of a reply
331 * @param n number of peers
332 * @param recv_peers the received peers
335 handle_reply (void *cls, uint64_t n, const struct GNUNET_PeerIdentity *recv_peers)
337 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "[%s] got %" PRIu64 " peers:\n",
342 GNUNET_i2s (rps_peer->peer_id),
345 for (i = 0 ; i < n ; i++)
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 GNUNET_i2s (&recv_peers[i]));
352 GNUNET_array_append (rps_peer->rec_ids, rps_peer->num_rec_ids, recv_peers[i]);
358 * Request random peers.
361 request_peers (void *cls,
362 const struct GNUNET_SCHEDULER_TaskContext *tc)
364 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Requesting one peer\n");
369 (void) GNUNET_RPS_request_peers (rps_peer->rps_handle, 1, handle_reply, rps_peer);
370 //rps_peer->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle, 1, handle_reply, rps_peer);
378 seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
381 struct RPSPeer *peer = (struct RPSPeer *) cls;
384 // TODO if malicious don't seed mal peers
385 amount = round (.5 * NUM_PEERS);
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
388 for (i = 0 ; i < amount ; i++)
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
391 GNUNET_i2s (&rps_peer_ids[i]));
393 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
398 * Get the id of peer i.
401 info_cb (void *cb_cls,
402 struct GNUNET_TESTBED_Operation *op,
403 const struct GNUNET_TESTBED_PeerInformation *pinfo,
406 unsigned int i = *((unsigned int *) cb_cls);
408 if (NULL == pinfo || NULL != emsg)
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
414 GNUNET_free (cb_cls);
416 rps_peer_ids[i] = *(pinfo->result.id);
417 rps_peers[i].peer_id = &rps_peer_ids[i];
418 rps_peers[i].rec_ids = NULL;
419 rps_peers[i].num_rec_ids = 0;
425 * Callback to be called when RPS service connect operation is completed
427 * @param cls the callback closure from functions generating an operation
428 * @param op the operation that has been finished
429 * @param ca_result the RPS service handle returned from rps_connect_adapter
430 * @param emsg error message in case the operation has failed; will be NULL if
431 * operation has executed successfully.
434 rps_connect_complete_cb (void *cls,
435 struct GNUNET_TESTBED_Operation *op,
439 struct RPSPeer *rps_peer = cls;
440 struct GNUNET_RPS_Handle *rps = ca_result;
442 rps_peer->rps_handle = rps;
443 rps_peer->online = GNUNET_YES;
446 GNUNET_assert (op == rps_peer->op);
449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
450 "Failed to connect to RPS service: %s\n",
453 GNUNET_SCHEDULER_shutdown ();
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
459 cur_test_run.main_test (rps_peer);
464 * Adapter function called to establish a connection to
468 * @param cfg configuration of the peer to connect to; will be available until
469 * GNUNET_TESTBED_operation_done() is called on the operation returned
470 * from GNUNET_TESTBED_service_connect()
471 * @return service handle to return in 'op_result', NULL on error
474 rps_connect_adapter (void *cls,
475 const struct GNUNET_CONFIGURATION_Handle *cfg)
477 struct GNUNET_RPS_Handle *h;
479 h = GNUNET_RPS_connect (cfg);
481 if (NULL != cur_test_run.pre_test)
482 cur_test_run.pre_test (cls, h);
489 * Adapter function called to destroy connection to
493 * @param op_result service handle returned from the connect adapter
496 rps_disconnect_adapter (void *cls,
499 struct GNUNET_RPS_Handle *h = op_result;
500 GNUNET_RPS_disconnect (h);
504 /***********************************************************************
505 * Definition of tests
506 ***********************************************************************/
509 default_eval_cb (void)
511 return evaluate (rps_peers, NUM_PEERS, 1);
513 /***********************************
515 ***********************************/
517 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
519 #ifdef ENABLE_MALICIOUS
520 uint32_t num_mal_peers;
521 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
523 GNUNET_assert (1 >= portion
525 num_mal_peers = round (portion * NUM_PEERS);
527 if (rps_peer->index < num_mal_peers)
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
532 GNUNET_i2s (rps_peer->peer_id),
535 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers, rps_peer_ids);
537 #endif /* ENABLE_MALICIOUS */
541 mal_cb (struct RPSPeer *rps_peer)
543 uint32_t num_mal_peers;
545 #ifdef ENABLE_MALICIOUS
546 GNUNET_assert (1 >= portion
548 num_mal_peers = round (portion * NUM_PEERS);
550 if (rps_peer->index >= num_mal_peers)
551 { /* It's useless to ask a malicious peer about a random sample -
553 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
554 seed_peers, rps_peer);
555 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
556 request_peers, rps_peer);
558 #endif /* ENABLE_MALICIOUS */
564 unsigned int num_mal_peers;
566 num_mal_peers = round (NUM_PEERS * portion);
567 return evaluate (&rps_peers[num_mal_peers],
568 NUM_PEERS - (num_mal_peers),
573 /***********************************
575 ***********************************/
577 single_req_cb (struct RPSPeer *rps_peer)
579 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
580 request_peers, rps_peer);
583 /***********************************
585 ***********************************/
587 delay_req_cb (struct RPSPeer *rps_peer)
589 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
590 request_peers, rps_peer);
591 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
592 request_peers, rps_peer);
595 /***********************************
597 ***********************************/
599 seed_cb (struct RPSPeer *rps_peer)
601 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
602 seed_peers, rps_peer);
611 /***********************************
613 ***********************************/
615 seed_big_cb (struct RPSPeer *rps_peer)
617 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
620 /***********************************
622 ***********************************/
624 single_peer_seed_cb (struct RPSPeer *rps_peer)
629 /***********************************
631 ***********************************/
633 seed_req_cb (struct RPSPeer *rps_peer)
635 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
636 seed_peers, rps_peer);
637 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15),
638 request_peers, rps_peer);
643 /***********************************
645 ***********************************/
647 req_cancel_cb (struct RPSPeer *rps_peer)
652 /***********************************
654 ***********************************/
656 churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
658 struct OpListEntry *entry;
661 double portion_online;
662 unsigned int *permut;
663 double prob_go_offline;
664 double portion_go_online;
665 double portion_go_offline;
668 portion_online = num_peers_online / NUM_PEERS;
669 portion_go_online = ((1 - portion_online) * .5 * .66);
670 portion_go_offline = (portion_online + portion_go_online) - .75;
671 prob_go_offline = portion_go_offline / (portion_online * .5);
673 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
674 (unsigned int) NUM_PEERS);
676 for (i = 0 ; i < .5 * NUM_PEERS ; i++)
680 if (GNUNET_YES == rps_peers[j].online)
682 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
684 if (prob < prob_go_offline * UINT32_MAX)
686 entry = make_oplist_entry ();
689 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
698 else if (GNUNET_NO == rps_peers[j].online)
700 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
702 if (prob < .66 * UINT32_MAX)
704 entry = make_oplist_entry ();
707 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
717 churn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
723 profiler_pre (void *cls, struct GNUNET_RPS_Handle *h)
725 churn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
732 profiler_cb (struct RPSPeer *rps_peer)
734 // We're not requesting peers
739 /***********************************************************************
740 * /Definition of tests
741 ***********************************************************************/
745 * Actual "main" function for the testcase.
748 * @param h the run handle
749 * @param num_peers number of peers in 'peers'
750 * @param peers handle to peers run in the testbed
751 * @param links_succeeded the number of overlay link connection attempts that
753 * @param links_failed the number of overlay link connection attempts that
758 struct GNUNET_TESTBED_RunHandle *h,
759 unsigned int num_peers,
760 struct GNUNET_TESTBED_Peer **peers,
761 unsigned int links_succeeded,
762 unsigned int links_failed)
767 testbed_peers = peers;
768 num_peers_online = 0;
770 for (i = 0 ; i < NUM_PEERS ; i++)
772 tmp_i = GNUNET_new (unsigned int);
775 (void) GNUNET_TESTBED_peer_get_information (peers[i],
776 GNUNET_TESTBED_PIT_IDENTITY,
781 GNUNET_assert (NUM_PEERS == num_peers);
782 for (i = 0 ; i < num_peers ; i++)
784 rps_peers[i].index = i;
786 GNUNET_TESTBED_service_connect (&rps_peers[i],
789 &rps_connect_complete_cb,
791 &rps_connect_adapter,
792 &rps_disconnect_adapter,
795 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
800 * Entry point for the testcase, sets up the testbed.
804 * @return 0 on success
807 main (int argc, char *argv[])
809 cur_test_run.pre_test = NULL;
810 cur_test_run.eval_cb = default_eval_cb;
813 if (strstr (argv[0], "malicious") != NULL)
815 cur_test_run.pre_test = mal_pre;
816 cur_test_run.main_test = mal_cb;
817 cur_test_run.eval_cb = mal_eval;
819 if (strstr (argv[0], "_1") != NULL)
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
824 else if (strstr (argv[0], "_2") != NULL)
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
829 else if (strstr (argv[0], "_3") != NULL)
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
836 else if (strstr (argv[0], "_single_req") != NULL)
838 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
839 cur_test_run.main_test = single_req_cb;
841 else if (strstr (argv[0], "_delayed_reqs") != NULL)
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
844 cur_test_run.main_test = delay_req_cb;
846 else if (strstr (argv[0], "_seed_big") != NULL)
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
849 cur_test_run.main_test = seed_big_cb;
851 else if (strstr (argv[0], "_single_peer_seed") != NULL)
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
854 cur_test_run.main_test = single_peer_seed_cb;
856 else if (strstr (argv[0], "_seed_request") != NULL)
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
859 cur_test_run.main_test = seed_req_cb;
861 else if (strstr (argv[0], "_seed") != NULL)
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
864 cur_test_run.main_test = seed_cb;
865 cur_test_run.eval_cb = seed_eval;
867 else if (strstr (argv[0], "_req_cancel") != NULL)
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
870 cur_test_run.main_test = req_cancel_cb;
872 else if (strstr (argv[0], "profiler") != NULL)
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
876 cur_test_run.pre_test = profiler_pre;
877 cur_test_run.main_test = profiler_cb;
878 churn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
884 (void) GNUNET_TESTBED_test_run ("test-rps-multipeer",
890 if (NULL != churn_task)
891 GNUNET_SCHEDULER_cancel (churn_task);
893 return cur_test_run.eval_cb();
896 /* end of test_rps_multipeer.c */