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)
44 static struct GNUNET_TIME_Relative timeout;
48 * Portion of malicious peers
50 static double portion = .1;
53 * Type of malicious peer to test
55 static unsigned int mal_type = 0;
58 * Handles to all of the running peers
60 static struct GNUNET_TESTBED_Peer **testbed_peers;
71 struct OpListEntry *next;
76 struct OpListEntry *prev;
79 * The testbed operation
81 struct GNUNET_TESTBED_Operation *op;
84 * Depending on whether we start or stop NSE service at the peer set this to 1
90 * Index of the regarding peer
98 static struct OpListEntry *oplist_head;
103 static struct OpListEntry *oplist_tail;
107 * Information we track for each peer.
117 * Handle for RPS connect operation.
119 struct GNUNET_TESTBED_Operation *op;
122 * Handle to RPS service.
124 struct GNUNET_RPS_Handle *rps_handle;
129 struct GNUNET_PeerIdentity *peer_id;
132 * A request handle to check for an request
134 //struct GNUNET_RPS_Request_Handle *req_handle;
137 * Peer on- or offline?
144 struct GNUNET_PeerIdentity *rec_ids;
147 * Number of received PeerIDs
149 unsigned int num_rec_ids;
154 * Information for all the peers.
156 static struct RPSPeer *rps_peers;
161 static struct GNUNET_PeerIdentity *rps_peer_ids;
164 * Number of online peers.
166 static unsigned int num_peers_online;
169 * Return value from 'main'.
175 * Identifier for the churn task that runs periodically
177 static struct GNUNET_SCHEDULER_Task *churn_task;
181 * Called directly after connecting to the service
183 typedef void (*PreTest) (void *cls, struct GNUNET_RPS_Handle *h);
186 * Called from within #rps_connect_complete_cb ()
187 * Executes functions to test the api/service
189 typedef void (*MainTest) (struct RPSPeer *rps_peer);
192 * Called directly before disconnecting from the service
194 typedef void (*PostTest) (void *cls, struct GNUNET_RPS_Handle *h);
197 * Function called after disconnect to evaluate test success
199 typedef int (*EvaluationCallback) (void);
203 * Structure to define a single test
213 * Called directly after connecting to the service
218 * Function to execute the functions to be tested
223 * Called directly before disconnecting from the service
228 * Function to evaluate the test results
230 EvaluationCallback eval_cb;
235 * Test the success of a single test
238 evaluate (struct RPSPeer *loc_rps_peers,
239 unsigned int num_loc_rps_peers,
240 unsigned int expected_recv)
245 tmp_ok = (1 == loc_rps_peers[0].num_rec_ids);
247 for (i = 0 ; i < num_loc_rps_peers ; i++)
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
252 GNUNET_i2s (loc_rps_peers[i].peer_id),
253 loc_rps_peers[i].num_rec_ids,
255 (1 == loc_rps_peers[i].num_rec_ids));
256 tmp_ok &= (1 == loc_rps_peers[i].num_rec_ids);
258 return tmp_ok? 0 : 1;
263 * Creates an oplist entry and adds it to the oplist DLL
265 static struct OpListEntry *
268 struct OpListEntry *entry;
270 entry = GNUNET_new (struct OpListEntry);
271 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
277 * Callback to be called when NSE service is started or stopped at peers
280 * @param op the operation handle
281 * @param emsg NULL on success; otherwise an error description
285 struct GNUNET_TESTBED_Operation *op,
289 struct OpListEntry *entry = cls;
291 GNUNET_TESTBED_operation_done (entry->op);
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
295 GNUNET_SCHEDULER_shutdown ();
298 GNUNET_assert (0 != entry->delta);
300 num_peers_online += entry->delta;
302 if (0 > entry->delta)
303 { /* Peer hopefully just went offline */
304 if (GNUNET_YES != rps_peers[entry->index].online)
306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
307 "peer %s was expected to go offline but is still marked as online\n",
308 GNUNET_i2s (rps_peers[entry->index].peer_id));
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "peer %s probably went offline as expected\n",
315 GNUNET_i2s (rps_peers[entry->index].peer_id));
317 rps_peers[entry->index].online = GNUNET_NO;
320 else if (0 < entry->delta)
321 { /* Peer hopefully just went online */
322 if (GNUNET_NO != rps_peers[entry->index].online)
324 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
325 "peer %s was expected to go online but is still marked as offline\n",
326 GNUNET_i2s (rps_peers[entry->index].peer_id));
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "peer %s probably went online as expected\n",
333 GNUNET_i2s (rps_peers[entry->index].peer_id));
335 rps_peers[entry->index].online = GNUNET_YES;
338 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
340 //if (num_peers_in_round[current_round] == peers_running)
346 * Task run on timeout to shut everything down.
349 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
353 if (NULL != churn_task)
354 GNUNET_SCHEDULER_cancel (churn_task);
356 for (i = 0 ; i < num_peers ; i++)
357 GNUNET_TESTBED_operation_done (rps_peers[i].op);
358 GNUNET_SCHEDULER_shutdown ();
363 * Callback to call on receipt of a reply
366 * @param n number of peers
367 * @param recv_peers the received peers
370 handle_reply (void *cls, uint64_t n, const struct GNUNET_PeerIdentity *recv_peers)
372 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "[%s] got %" PRIu64 " peers:\n",
377 GNUNET_i2s (rps_peer->peer_id),
380 for (i = 0 ; i < n ; i++)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 GNUNET_i2s (&recv_peers[i]));
387 GNUNET_array_append (rps_peer->rec_ids, rps_peer->num_rec_ids, recv_peers[i]);
393 * Request random peers.
396 request_peers (void *cls,
397 const struct GNUNET_SCHEDULER_TaskContext *tc)
399 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Requesting one peer\n");
404 (void) GNUNET_RPS_request_peers (rps_peer->rps_handle, 1, handle_reply, rps_peer);
405 //rps_peer->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle, 1, handle_reply, rps_peer);
413 seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
416 struct RPSPeer *peer = (struct RPSPeer *) cls;
419 // TODO if malicious don't seed mal peers
420 amount = round (.5 * num_peers);
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
423 for (i = 0 ; i < amount ; i++)
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
426 GNUNET_i2s (&rps_peer_ids[i]));
428 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
433 * Get the id of peer i.
436 info_cb (void *cb_cls,
437 struct GNUNET_TESTBED_Operation *op,
438 const struct GNUNET_TESTBED_PeerInformation *pinfo,
441 unsigned int i = *((unsigned int *) cb_cls);
443 if (NULL == pinfo || NULL != emsg)
445 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
449 GNUNET_free (cb_cls);
451 rps_peer_ids[i] = *(pinfo->result.id);
452 rps_peers[i].peer_id = &rps_peer_ids[i];
453 rps_peers[i].rec_ids = NULL;
454 rps_peers[i].num_rec_ids = 0;
460 * Callback to be called when RPS service connect operation is completed
462 * @param cls the callback closure from functions generating an operation
463 * @param op the operation that has been finished
464 * @param ca_result the RPS service handle returned from rps_connect_adapter
465 * @param emsg error message in case the operation has failed; will be NULL if
466 * operation has executed successfully.
469 rps_connect_complete_cb (void *cls,
470 struct GNUNET_TESTBED_Operation *op,
474 struct RPSPeer *rps_peer = cls;
475 struct GNUNET_RPS_Handle *rps = ca_result;
477 rps_peer->rps_handle = rps;
478 rps_peer->online = GNUNET_YES;
481 GNUNET_assert (op == rps_peer->op);
484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
485 "Failed to connect to RPS service: %s\n",
488 GNUNET_SCHEDULER_shutdown ();
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
494 cur_test_run.main_test (rps_peer);
499 * Adapter function called to establish a connection to
503 * @param cfg configuration of the peer to connect to; will be available until
504 * GNUNET_TESTBED_operation_done() is called on the operation returned
505 * from GNUNET_TESTBED_service_connect()
506 * @return service handle to return in 'op_result', NULL on error
509 rps_connect_adapter (void *cls,
510 const struct GNUNET_CONFIGURATION_Handle *cfg)
512 struct GNUNET_RPS_Handle *h;
514 h = GNUNET_RPS_connect (cfg);
516 if (NULL != cur_test_run.pre_test)
517 cur_test_run.pre_test (cls, h);
524 * Adapter function called to destroy connection to
528 * @param op_result service handle returned from the connect adapter
531 rps_disconnect_adapter (void *cls,
534 struct GNUNET_RPS_Handle *h = op_result;
535 GNUNET_RPS_disconnect (h);
539 /***********************************************************************
540 * Definition of tests
541 ***********************************************************************/
544 default_eval_cb (void)
546 return evaluate (rps_peers, num_peers, 1);
555 /***********************************
557 ***********************************/
559 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
561 #ifdef ENABLE_MALICIOUS
562 uint32_t num_mal_peers;
563 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
565 GNUNET_assert (1 >= portion
567 num_mal_peers = round (portion * num_peers);
569 if (rps_peer->index < num_mal_peers)
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
574 GNUNET_i2s (rps_peer->peer_id),
577 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers, rps_peer_ids);
579 #endif /* ENABLE_MALICIOUS */
583 mal_cb (struct RPSPeer *rps_peer)
585 uint32_t num_mal_peers;
587 #ifdef ENABLE_MALICIOUS
588 GNUNET_assert (1 >= portion
590 num_mal_peers = round (portion * num_peers);
592 if (rps_peer->index >= num_mal_peers)
593 { /* It's useless to ask a malicious peer about a random sample -
595 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
596 seed_peers, rps_peer);
597 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
598 request_peers, rps_peer);
600 #endif /* ENABLE_MALICIOUS */
606 unsigned int num_mal_peers;
608 num_mal_peers = round (num_peers * portion);
609 return evaluate (&rps_peers[num_mal_peers],
610 num_peers - (num_mal_peers),
615 /***********************************
617 ***********************************/
619 single_req_cb (struct RPSPeer *rps_peer)
621 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
622 request_peers, rps_peer);
625 /***********************************
627 ***********************************/
629 delay_req_cb (struct RPSPeer *rps_peer)
631 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
632 request_peers, rps_peer);
633 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
634 request_peers, rps_peer);
637 /***********************************
639 ***********************************/
641 seed_cb (struct RPSPeer *rps_peer)
643 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
644 seed_peers, rps_peer);
647 /***********************************
649 ***********************************/
651 seed_big_cb (struct RPSPeer *rps_peer)
653 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
656 /***********************************
658 ***********************************/
660 single_peer_seed_cb (struct RPSPeer *rps_peer)
665 /***********************************
667 ***********************************/
669 seed_req_cb (struct RPSPeer *rps_peer)
671 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
672 seed_peers, rps_peer);
673 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15),
674 request_peers, rps_peer);
679 /***********************************
681 ***********************************/
683 req_cancel_cb (struct RPSPeer *rps_peer)
688 /***********************************
690 ***********************************/
692 churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
694 struct OpListEntry *entry;
697 double portion_online;
698 unsigned int *permut;
699 double prob_go_offline;
700 double portion_go_online;
701 double portion_go_offline;
704 /* Compute the probability for an online peer to go offline
706 portion_online = num_peers_online * 1.0 / num_peers;
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Portion online: %f\n",
710 portion_go_online = ((1 - portion_online) * .5 * .66);
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Portion that should go online: %f\n",
714 portion_go_offline = (portion_online + portion_go_online) - .75;
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "Portion that probably goes offline: %f\n",
718 prob_go_offline = portion_go_offline / (portion_online * .5);
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Probability of a selected online peer to go offline: %f\n",
723 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
724 (unsigned int) num_peers);
726 /* Go over 50% randomly chosen peers */
727 for (i = 0 ; i < .5 * num_peers ; i++)
731 /* If online, shut down with certain probability */
732 if (GNUNET_YES == rps_peers[j].online)
734 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "%u. selected peer (%u: %s) is online.\n",
740 GNUNET_i2s (rps_peers[j].peer_id));
741 if (prob < prob_go_offline * UINT32_MAX)
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 GNUNET_i2s (rps_peers[j].peer_id));
747 entry = make_oplist_entry ();
750 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
759 /* If offline, restart with certain probability */
760 else if (GNUNET_NO == rps_peers[j].online)
762 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "%u. selected peer (%u: %s) is offline.\n",
768 GNUNET_i2s (rps_peers[j].peer_id));
769 if (prob < .66 * UINT32_MAX)
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 GNUNET_i2s (rps_peers[j].peer_id));
775 entry = make_oplist_entry ();
778 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
788 churn_task = GNUNET_SCHEDULER_add_delayed (
789 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
796 profiler_pre (void *cls, struct GNUNET_RPS_Handle *h)
798 //churn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
803 if (NULL == churn_task)
805 churn_task = GNUNET_SCHEDULER_add_delayed (
806 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
813 profiler_cb (struct RPSPeer *rps_peer)
815 // We're not requesting peers
820 /***********************************************************************
821 * /Definition of tests
822 ***********************************************************************/
826 * Actual "main" function for the testcase.
829 * @param h the run handle
830 * @param num_peers number of peers in 'peers'
831 * @param peers handle to peers run in the testbed
832 * @param links_succeeded the number of overlay link connection attempts that
834 * @param links_failed the number of overlay link connection attempts that
839 struct GNUNET_TESTBED_RunHandle *h,
840 unsigned int num_peers,
841 struct GNUNET_TESTBED_Peer **peers,
842 unsigned int links_succeeded,
843 unsigned int links_failed)
848 testbed_peers = peers;
849 num_peers_online = 0;
851 for (i = 0 ; i < NUM_PEERS ; i++)
853 tmp_i = GNUNET_new (unsigned int);
856 (void) GNUNET_TESTBED_peer_get_information (peers[i],
857 GNUNET_TESTBED_PIT_IDENTITY,
862 GNUNET_assert (NUM_PEERS == num_peers);
863 for (i = 0 ; i < num_peers ; i++)
865 rps_peers[i].index = i;
867 GNUNET_TESTBED_service_connect (&rps_peers[i],
870 &rps_connect_complete_cb,
872 &rps_connect_adapter,
873 &rps_disconnect_adapter,
877 if (NULL != churn_task)
878 GNUNET_SCHEDULER_cancel (churn_task);
880 //GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
881 GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_task, NULL);
886 * Entry point for the testcase, sets up the testbed.
890 * @return 0 on success
893 main (int argc, char *argv[])
895 cur_test_run.pre_test = NULL;
896 cur_test_run.eval_cb = default_eval_cb;
899 if (strstr (argv[0], "malicious") != NULL)
901 cur_test_run.pre_test = mal_pre;
902 cur_test_run.main_test = mal_cb;
903 cur_test_run.eval_cb = mal_eval;
905 if (strstr (argv[0], "_1") != NULL)
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
910 else if (strstr (argv[0], "_2") != NULL)
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
915 else if (strstr (argv[0], "_3") != NULL)
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
922 else if (strstr (argv[0], "_single_req") != NULL)
924 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
925 cur_test_run.main_test = single_req_cb;
927 else if (strstr (argv[0], "_delayed_reqs") != NULL)
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
930 cur_test_run.main_test = delay_req_cb;
932 else if (strstr (argv[0], "_seed_big") != NULL)
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
935 cur_test_run.main_test = seed_big_cb;
937 else if (strstr (argv[0], "_single_peer_seed") != NULL)
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
940 cur_test_run.main_test = single_peer_seed_cb;
942 else if (strstr (argv[0], "_seed_request") != NULL)
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
945 cur_test_run.main_test = seed_req_cb;
947 else if (strstr (argv[0], "_seed") != NULL)
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
950 cur_test_run.main_test = seed_cb;
951 cur_test_run.eval_cb = seed_eval;
953 else if (strstr (argv[0], "_req_cancel") != NULL)
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
956 cur_test_run.main_test = req_cancel_cb;
958 else if (strstr (argv[0], "profiler") != NULL)
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
962 cur_test_run.pre_test = profiler_pre;
963 cur_test_run.main_test = profiler_cb;
964 churn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
970 (void) GNUNET_TESTBED_test_run ("test-rps-multipeer",
976 if (NULL != churn_task)
977 GNUNET_SCHEDULER_cancel (churn_task);
979 return cur_test_run.eval_cb();
982 /* end of test_rps_multipeer.c */