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 online */
304 GNUNET_break (GNUNET_NO == rps_peers[entry->index].online);
305 rps_peers[entry->index].online = GNUNET_YES;
307 else if (0 > entry->delta)
308 { /* Peer hopefully just went offline */
309 GNUNET_break (GNUNET_YES == rps_peers[entry->index].online);
310 rps_peers[entry->index].online = GNUNET_NO;
313 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
315 //if (num_peers_in_round[current_round] == peers_running)
321 * Task run on timeout to shut everything down.
324 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
328 if (NULL != churn_task)
329 GNUNET_SCHEDULER_cancel (churn_task);
331 for (i = 0 ; i < num_peers ; i++)
332 GNUNET_TESTBED_operation_done (rps_peers[i].op);
333 GNUNET_SCHEDULER_shutdown ();
338 * Callback to call on receipt of a reply
341 * @param n number of peers
342 * @param recv_peers the received peers
345 handle_reply (void *cls, uint64_t n, const struct GNUNET_PeerIdentity *recv_peers)
347 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "[%s] got %" PRIu64 " peers:\n",
352 GNUNET_i2s (rps_peer->peer_id),
355 for (i = 0 ; i < n ; i++)
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 GNUNET_i2s (&recv_peers[i]));
362 GNUNET_array_append (rps_peer->rec_ids, rps_peer->num_rec_ids, recv_peers[i]);
368 * Request random peers.
371 request_peers (void *cls,
372 const struct GNUNET_SCHEDULER_TaskContext *tc)
374 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Requesting one peer\n");
379 (void) GNUNET_RPS_request_peers (rps_peer->rps_handle, 1, handle_reply, rps_peer);
380 //rps_peer->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle, 1, handle_reply, rps_peer);
388 seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
391 struct RPSPeer *peer = (struct RPSPeer *) cls;
394 // TODO if malicious don't seed mal peers
395 amount = round (.5 * num_peers);
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
398 for (i = 0 ; i < amount ; i++)
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
401 GNUNET_i2s (&rps_peer_ids[i]));
403 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
408 * Get the id of peer i.
411 info_cb (void *cb_cls,
412 struct GNUNET_TESTBED_Operation *op,
413 const struct GNUNET_TESTBED_PeerInformation *pinfo,
416 unsigned int i = *((unsigned int *) cb_cls);
418 if (NULL == pinfo || NULL != emsg)
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
424 GNUNET_free (cb_cls);
426 rps_peer_ids[i] = *(pinfo->result.id);
427 rps_peers[i].peer_id = &rps_peer_ids[i];
428 rps_peers[i].rec_ids = NULL;
429 rps_peers[i].num_rec_ids = 0;
435 * Callback to be called when RPS service connect operation is completed
437 * @param cls the callback closure from functions generating an operation
438 * @param op the operation that has been finished
439 * @param ca_result the RPS service handle returned from rps_connect_adapter
440 * @param emsg error message in case the operation has failed; will be NULL if
441 * operation has executed successfully.
444 rps_connect_complete_cb (void *cls,
445 struct GNUNET_TESTBED_Operation *op,
449 struct RPSPeer *rps_peer = cls;
450 struct GNUNET_RPS_Handle *rps = ca_result;
452 rps_peer->rps_handle = rps;
453 rps_peer->online = GNUNET_YES;
456 GNUNET_assert (op == rps_peer->op);
459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
460 "Failed to connect to RPS service: %s\n",
463 GNUNET_SCHEDULER_shutdown ();
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
469 cur_test_run.main_test (rps_peer);
474 * Adapter function called to establish a connection to
478 * @param cfg configuration of the peer to connect to; will be available until
479 * GNUNET_TESTBED_operation_done() is called on the operation returned
480 * from GNUNET_TESTBED_service_connect()
481 * @return service handle to return in 'op_result', NULL on error
484 rps_connect_adapter (void *cls,
485 const struct GNUNET_CONFIGURATION_Handle *cfg)
487 struct GNUNET_RPS_Handle *h;
489 h = GNUNET_RPS_connect (cfg);
491 if (NULL != cur_test_run.pre_test)
492 cur_test_run.pre_test (cls, h);
499 * Adapter function called to destroy connection to
503 * @param op_result service handle returned from the connect adapter
506 rps_disconnect_adapter (void *cls,
509 struct GNUNET_RPS_Handle *h = op_result;
510 GNUNET_RPS_disconnect (h);
514 /***********************************************************************
515 * Definition of tests
516 ***********************************************************************/
519 default_eval_cb (void)
521 return evaluate (rps_peers, num_peers, 1);
530 /***********************************
532 ***********************************/
534 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
536 #ifdef ENABLE_MALICIOUS
537 uint32_t num_mal_peers;
538 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
540 GNUNET_assert (1 >= portion
542 num_mal_peers = round (portion * num_peers);
544 if (rps_peer->index < num_mal_peers)
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
549 GNUNET_i2s (rps_peer->peer_id),
552 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers, rps_peer_ids);
554 #endif /* ENABLE_MALICIOUS */
558 mal_cb (struct RPSPeer *rps_peer)
560 uint32_t num_mal_peers;
562 #ifdef ENABLE_MALICIOUS
563 GNUNET_assert (1 >= portion
565 num_mal_peers = round (portion * num_peers);
567 if (rps_peer->index >= num_mal_peers)
568 { /* It's useless to ask a malicious peer about a random sample -
570 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
571 seed_peers, rps_peer);
572 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
573 request_peers, rps_peer);
575 #endif /* ENABLE_MALICIOUS */
581 unsigned int num_mal_peers;
583 num_mal_peers = round (num_peers * portion);
584 return evaluate (&rps_peers[num_mal_peers],
585 num_peers - (num_mal_peers),
590 /***********************************
592 ***********************************/
594 single_req_cb (struct RPSPeer *rps_peer)
596 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
597 request_peers, rps_peer);
600 /***********************************
602 ***********************************/
604 delay_req_cb (struct RPSPeer *rps_peer)
606 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
607 request_peers, rps_peer);
608 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
609 request_peers, rps_peer);
612 /***********************************
614 ***********************************/
616 seed_cb (struct RPSPeer *rps_peer)
618 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
619 seed_peers, rps_peer);
622 /***********************************
624 ***********************************/
626 seed_big_cb (struct RPSPeer *rps_peer)
628 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
631 /***********************************
633 ***********************************/
635 single_peer_seed_cb (struct RPSPeer *rps_peer)
640 /***********************************
642 ***********************************/
644 seed_req_cb (struct RPSPeer *rps_peer)
646 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
647 seed_peers, rps_peer);
648 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15),
649 request_peers, rps_peer);
654 /***********************************
656 ***********************************/
658 req_cancel_cb (struct RPSPeer *rps_peer)
663 /***********************************
665 ***********************************/
667 churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
669 struct OpListEntry *entry;
672 double portion_online;
673 unsigned int *permut;
674 double prob_go_offline;
675 double portion_go_online;
676 double portion_go_offline;
679 portion_online = num_peers_online / NUM_PEERS;
680 portion_go_online = ((1 - portion_online) * .5 * .66);
681 portion_go_offline = (portion_online + portion_go_online) - .75;
682 prob_go_offline = portion_go_offline / (portion_online * .5);
684 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
685 (unsigned int) NUM_PEERS);
687 for (i = 0 ; i < .5 * NUM_PEERS ; i++)
691 if (GNUNET_YES == rps_peers[j].online)
693 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
695 if (prob < prob_go_offline * UINT32_MAX)
697 entry = make_oplist_entry ();
700 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
709 else if (GNUNET_NO == rps_peers[j].online)
711 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
713 if (prob < .66 * UINT32_MAX)
715 entry = make_oplist_entry ();
718 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
728 churn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
734 profiler_pre (void *cls, struct GNUNET_RPS_Handle *h)
736 churn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
743 profiler_cb (struct RPSPeer *rps_peer)
745 // We're not requesting peers
750 /***********************************************************************
751 * /Definition of tests
752 ***********************************************************************/
756 * Actual "main" function for the testcase.
759 * @param h the run handle
760 * @param num_peers number of peers in 'peers'
761 * @param peers handle to peers run in the testbed
762 * @param links_succeeded the number of overlay link connection attempts that
764 * @param links_failed the number of overlay link connection attempts that
769 struct GNUNET_TESTBED_RunHandle *h,
770 unsigned int num_peers,
771 struct GNUNET_TESTBED_Peer **peers,
772 unsigned int links_succeeded,
773 unsigned int links_failed)
778 testbed_peers = peers;
779 num_peers_online = 0;
781 for (i = 0 ; i < NUM_PEERS ; i++)
783 tmp_i = GNUNET_new (unsigned int);
786 (void) GNUNET_TESTBED_peer_get_information (peers[i],
787 GNUNET_TESTBED_PIT_IDENTITY,
792 GNUNET_assert (NUM_PEERS == num_peers);
793 for (i = 0 ; i < num_peers ; i++)
795 rps_peers[i].index = i;
797 GNUNET_TESTBED_service_connect (&rps_peers[i],
800 &rps_connect_complete_cb,
802 &rps_connect_adapter,
803 &rps_disconnect_adapter,
807 if (NULL != churn_task)
808 GNUNET_SCHEDULER_cancel (churn_task);
810 //GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
811 GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_task, NULL);
816 * Entry point for the testcase, sets up the testbed.
820 * @return 0 on success
823 main (int argc, char *argv[])
825 cur_test_run.pre_test = NULL;
826 cur_test_run.eval_cb = default_eval_cb;
829 if (strstr (argv[0], "malicious") != NULL)
831 cur_test_run.pre_test = mal_pre;
832 cur_test_run.main_test = mal_cb;
833 cur_test_run.eval_cb = mal_eval;
835 if (strstr (argv[0], "_1") != NULL)
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
840 else if (strstr (argv[0], "_2") != NULL)
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
845 else if (strstr (argv[0], "_3") != NULL)
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
852 else if (strstr (argv[0], "_single_req") != NULL)
854 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
855 cur_test_run.main_test = single_req_cb;
857 else if (strstr (argv[0], "_delayed_reqs") != NULL)
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
860 cur_test_run.main_test = delay_req_cb;
862 else if (strstr (argv[0], "_seed_big") != NULL)
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
865 cur_test_run.main_test = seed_big_cb;
867 else if (strstr (argv[0], "_single_peer_seed") != NULL)
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
870 cur_test_run.main_test = single_peer_seed_cb;
872 else if (strstr (argv[0], "_seed_request") != NULL)
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
875 cur_test_run.main_test = seed_req_cb;
877 else if (strstr (argv[0], "_seed") != NULL)
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
880 cur_test_run.main_test = seed_cb;
881 cur_test_run.eval_cb = seed_eval;
883 else if (strstr (argv[0], "_req_cancel") != NULL)
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
886 cur_test_run.main_test = req_cancel_cb;
888 else if (strstr (argv[0], "profiler") != NULL)
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
892 cur_test_run.pre_test = profiler_pre;
893 cur_test_run.main_test = profiler_cb;
894 churn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
900 (void) GNUNET_TESTBED_test_run ("test-rps-multipeer",
906 if (NULL != churn_task)
907 GNUNET_SCHEDULER_cancel (churn_task);
909 return cur_test_run.eval_cb();
912 /* end of test_rps_multipeer.c */