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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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"
31 #include "gnunet_rps_service.h"
32 #include "rps-test_util.h"
33 #include "gnunet-service-rps_sampler_elem.h"
39 * How many peers do we start?
44 * How long do we run the test?
46 //#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 static struct GNUNET_TIME_Relative timeout;
51 * Portion of malicious peers
53 static double portion = .1;
56 * Type of malicious peer to test
58 static unsigned int mal_type = 0;
61 * Handles to all of the running peers
63 static struct GNUNET_TESTBED_Peer **testbed_peers;
74 struct OpListEntry *next;
79 struct OpListEntry *prev;
82 * The testbed operation
84 struct GNUNET_TESTBED_Operation *op;
87 * Depending on whether we start or stop NSE service at the peer set this to 1
93 * Index of the regarding peer
101 static struct OpListEntry *oplist_head;
106 static struct OpListEntry *oplist_tail;
110 * A pending reply: A request was sent and the reply is pending.
117 struct PendingReply *next;
118 struct PendingReply *prev;
121 * Handle to the request we are waiting for
123 struct GNUNET_RPS_Request_Handle *req_handle;
126 * The peer that requested
128 struct RPSPeer *rps_peer;
133 * A pending request: A request was not made yet but is scheduled for later.
135 struct PendingRequest
140 struct PendingRequest *next;
141 struct PendingRequest *prev;
144 * Handle to the request we are waiting for
146 struct GNUNET_SCHEDULER_Task *request_task;
149 * The peer that requested
151 struct RPSPeer *rps_peer;
156 * Information we track for each peer.
166 * Handle for RPS connect operation.
168 struct GNUNET_TESTBED_Operation *op;
171 * Handle to RPS service.
173 struct GNUNET_RPS_Handle *rps_handle;
178 struct GNUNET_PeerIdentity *peer_id;
181 * A request handle to check for an request
183 //struct GNUNET_RPS_Request_Handle *req_handle;
186 * Peer on- or offline?
191 * Number of Peer IDs to request
193 unsigned int num_ids_to_request;
196 * Pending requests DLL
198 struct PendingRequest *pending_req_head;
199 struct PendingRequest *pending_req_tail;
202 * Number of pending requests
204 unsigned int num_pending_reqs;
207 * Pending replies DLL
209 struct PendingReply *pending_rep_head;
210 struct PendingReply *pending_rep_tail;
213 * Number of pending replies
215 unsigned int num_pending_reps;
220 struct GNUNET_PeerIdentity *rec_ids;
223 * Number of received PeerIDs
225 unsigned int num_rec_ids;
230 * Information for all the peers.
232 static struct RPSPeer *rps_peers;
235 * Peermap to get the index of a given peer ID quick.
237 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
242 static struct GNUNET_PeerIdentity *rps_peer_ids;
245 * ID of the targeted peer.
247 static struct GNUNET_PeerIdentity *target_peer;
250 * ID of the peer that requests for the evaluation.
252 static struct RPSPeer *eval_peer;
255 * Number of online peers.
257 static unsigned int num_peers_online;
260 * Return value from 'main'.
266 * Identifier for the churn task that runs periodically
268 static struct GNUNET_SCHEDULER_Task *churn_task;
272 * Called to initialise the given RPSPeer
274 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
277 * Called directly after connecting to the service
279 typedef void (*PreTest) (void *cls, struct GNUNET_RPS_Handle *h);
282 * Called from within #rps_connect_complete_cb ()
283 * Executes functions to test the api/service
285 typedef void (*MainTest) (struct RPSPeer *rps_peer);
288 * Callback called once the requested random peers are available
290 typedef void (*ReplyHandle) (void *cls,
292 const struct GNUNET_PeerIdentity *recv_peers);
295 * Called directly before disconnecting from the service
297 typedef void (*PostTest) (void *cls, struct GNUNET_RPS_Handle *h);
300 * Function called after disconnect to evaluate test success
302 typedef int (*EvaluationCallback) (void);
306 * Structure to define a single test
316 * Called to initialise peer
321 * Called directly after connecting to the service
326 * Function to execute the functions to be tested
331 * Callback called once the requested peers are available
333 ReplyHandle reply_handle;
336 * Called directly before disconnecting from the service
341 * Function to evaluate the test results
343 EvaluationCallback eval_cb;
348 uint32_t request_interval;
351 * Number of Requests to make.
353 uint32_t num_requests;
357 * Are we shutting down?
359 static int in_shutdown;
362 * Append arguments to file
365 tofile_ (const char *file_name, char *line)
367 struct GNUNET_DISK_FileHandle *f;
368 /* char output_buffer[512]; */
373 if (NULL == (f = GNUNET_DISK_file_open (file_name,
374 GNUNET_DISK_OPEN_APPEND |
375 GNUNET_DISK_OPEN_WRITE |
376 GNUNET_DISK_OPEN_CREATE,
377 GNUNET_DISK_PERM_USER_READ |
378 GNUNET_DISK_PERM_USER_WRITE |
379 GNUNET_DISK_PERM_GROUP_READ |
380 GNUNET_DISK_PERM_OTHER_READ)))
382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
383 "Not able to open file %s\n",
387 /* size = GNUNET_snprintf (output_buffer,
388 sizeof (output_buffer),
390 GNUNET_TIME_absolute_get ().abs_value_us,
394 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
395 "Failed to write string to buffer (size: %i)\n",
400 size = strlen (line) * sizeof (char);
402 size2 = GNUNET_DISK_file_write (f, line, size);
405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
406 "Unable to write to file! (Size: %u, size2: %u)\n",
412 if (GNUNET_YES != GNUNET_DISK_file_close (f))
413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
414 "Unable to close file\n");
418 * This function is used to facilitate writing important information to disk
420 #define tofile(file_name, ...) do {\
423 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
426 "Failed to create tmp_buf\n");\
428 tofile_(file_name,tmp_buf);\
433 * Write the ids and their according index in the given array to a file
437 ids_to_file (char *file_name,
438 struct GNUNET_PeerIdentity *peer_ids,
439 unsigned int num_peer_ids)
443 for (i=0 ; i < num_peer_ids ; i++)
448 GNUNET_i2s_full (&peer_ids[i]));
453 * Test the success of a single test
456 evaluate (struct RPSPeer *loc_rps_peers,
457 unsigned int num_loc_rps_peers,
458 unsigned int expected_recv)
463 tmp_ok = (1 == loc_rps_peers[0].num_rec_ids);
465 for (i = 0 ; i < num_loc_rps_peers ; i++)
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
470 GNUNET_i2s (loc_rps_peers[i].peer_id),
471 loc_rps_peers[i].num_rec_ids,
473 (1 == loc_rps_peers[i].num_rec_ids));
474 tmp_ok &= (1 == loc_rps_peers[i].num_rec_ids);
476 return tmp_ok? 0 : 1;
481 * Creates an oplist entry and adds it to the oplist DLL
483 static struct OpListEntry *
486 struct OpListEntry *entry;
488 entry = GNUNET_new (struct OpListEntry);
489 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
495 * Task run on timeout to shut everything down.
498 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
502 in_shutdown = GNUNET_YES;
503 if (NULL != churn_task)
504 GNUNET_SCHEDULER_cancel (churn_task);
506 for (i = 0 ; i < num_peers ; i++)
507 GNUNET_TESTBED_operation_done (rps_peers[i].op);
508 GNUNET_SCHEDULER_shutdown ();
516 seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
519 struct RPSPeer *peer = (struct RPSPeer *) cls;
522 // TODO if malicious don't seed mal peers
523 amount = round (.5 * num_peers);
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
526 for (i = 0 ; i < amount ; i++)
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
529 GNUNET_i2s (&rps_peer_ids[i]));
531 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
536 * Get the id of peer i.
539 info_cb (void *cb_cls,
540 struct GNUNET_TESTBED_Operation *op,
541 const struct GNUNET_TESTBED_PeerInformation *pinfo,
544 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
546 if (NULL == pinfo || NULL != emsg)
548 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 GNUNET_i2s (pinfo->result.id));
557 rps_peer_ids[entry->index] = *(pinfo->result.id);
558 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
559 rps_peers[entry->index].rec_ids = NULL;
560 rps_peers[entry->index].num_rec_ids = 0;
562 GNUNET_CONTAINER_multipeermap_put (peer_map,
563 &rps_peer_ids[entry->index],
564 &rps_peers[entry->index],
565 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
566 tofile ("/tmp/rps/peer_ids",
569 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
571 if (NULL != cur_test_run.init_peer)
572 cur_test_run.init_peer (&rps_peers[entry->index]);
574 GNUNET_TESTBED_operation_done (entry->op);
575 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
581 * Callback to be called when RPS service connect operation is completed
583 * @param cls the callback closure from functions generating an operation
584 * @param op the operation that has been finished
585 * @param ca_result the RPS service handle returned from rps_connect_adapter
586 * @param emsg error message in case the operation has failed; will be NULL if
587 * operation has executed successfully.
590 rps_connect_complete_cb (void *cls,
591 struct GNUNET_TESTBED_Operation *op,
595 struct RPSPeer *rps_peer = cls;
596 struct GNUNET_RPS_Handle *rps = ca_result;
598 rps_peer->rps_handle = rps;
599 rps_peer->online = GNUNET_YES;
602 GNUNET_assert (op == rps_peer->op);
605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
606 "Failed to connect to RPS service: %s\n",
609 GNUNET_SCHEDULER_shutdown ();
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
615 cur_test_run.main_test (rps_peer);
620 * Adapter function called to establish a connection to
624 * @param cfg configuration of the peer to connect to; will be available until
625 * GNUNET_TESTBED_operation_done() is called on the operation returned
626 * from GNUNET_TESTBED_service_connect()
627 * @return service handle to return in 'op_result', NULL on error
630 rps_connect_adapter (void *cls,
631 const struct GNUNET_CONFIGURATION_Handle *cfg)
633 struct GNUNET_RPS_Handle *h;
635 h = GNUNET_RPS_connect (cfg);
637 if (NULL != cur_test_run.pre_test)
638 cur_test_run.pre_test (cls, h);
645 * Adapter function called to destroy connection to
649 * @param op_result service handle returned from the connect adapter
652 rps_disconnect_adapter (void *cls,
655 struct GNUNET_RPS_Handle *h = op_result;
656 GNUNET_RPS_disconnect (h);
660 /***********************************************************************
661 * Definition of tests
662 ***********************************************************************/
664 // TODO check whether tests can be stopped earlier
666 default_eval_cb (void)
668 return evaluate (rps_peers, num_peers, 1);
678 * Initialise given RPSPeer
680 static void default_init_peer (struct RPSPeer *rps_peer)
682 rps_peer->num_ids_to_request = 1;
686 * Callback to call on receipt of a reply
689 * @param n number of peers
690 * @param recv_peers the received peers
693 default_reply_handle (void *cls,
695 const struct GNUNET_PeerIdentity *recv_peers)
697 struct RPSPeer *rps_peer;
698 struct PendingReply *pending_rep = (struct PendingReply *) cls;
701 rps_peer = pending_rep->rps_peer;
702 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
703 rps_peer->pending_rep_tail,
705 rps_peer->num_pending_reps--;
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "[%s] got %" PRIu64 " peers:\n",
708 GNUNET_i2s (rps_peer->peer_id),
711 for (i = 0 ; i < n ; i++)
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 GNUNET_i2s (&recv_peers[i]));
718 GNUNET_array_append (rps_peer->rec_ids, rps_peer->num_rec_ids, recv_peers[i]);
723 * Request random peers.
726 request_peers (void *cls,
727 const struct GNUNET_SCHEDULER_TaskContext *tc)
729 struct RPSPeer *rps_peer;
730 struct PendingRequest *pending_req = (struct PendingRequest *) cls;
731 struct PendingReply *pending_rep;
733 if (GNUNET_YES == in_shutdown)
735 rps_peer = pending_req->rps_peer;
736 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
737 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
738 rps_peer->pending_req_tail,
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 "Requesting one peer\n");
742 pending_rep = GNUNET_new (struct PendingReply);
743 pending_rep->rps_peer = rps_peer;
744 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
746 cur_test_run.reply_handle,
748 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
749 rps_peer->pending_rep_tail,
751 rps_peer->num_pending_reps++;
752 rps_peer->num_pending_reqs--;
756 cancel_pending_req (struct PendingRequest *pending_req)
758 struct RPSPeer *rps_peer;
760 rps_peer = pending_req->rps_peer;
761 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
762 rps_peer->pending_req_tail,
764 rps_peer->num_pending_reqs--;
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Cancelling pending request\n");
767 GNUNET_SCHEDULER_cancel (pending_req->request_task);
768 GNUNET_free (pending_req);
772 cancel_request (struct PendingReply *pending_rep)
774 struct RPSPeer *rps_peer;
776 rps_peer = pending_rep->rps_peer;
777 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
778 rps_peer->pending_rep_tail,
780 rps_peer->num_pending_reps--;
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Cancelling request\n");
783 GNUNET_RPS_request_cancel (pending_rep->req_handle);
784 GNUNET_free (pending_rep);
791 cancel_request_cb (void *cls,
792 const struct GNUNET_SCHEDULER_TaskContext *tc)
794 struct PendingReply *pending_rep;
795 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
797 if (GNUNET_YES == in_shutdown)
799 pending_rep = rps_peer->pending_rep_head;
800 GNUNET_assert (1 <= rps_peer->num_pending_reps);
801 cancel_request (pending_rep);
806 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
807 * issued, nor replied
810 schedule_missing_requests (struct RPSPeer *rps_peer)
813 struct PendingRequest *pending_req;
815 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
816 i < rps_peer->num_ids_to_request; i++)
818 pending_req = GNUNET_new (struct PendingRequest);
819 pending_req->rps_peer = rps_peer;
820 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
821 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
822 cur_test_run.request_interval * i),
825 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
826 rps_peer->pending_req_tail,
828 rps_peer->num_pending_reqs++;
833 cancel_pending_req_rep (struct RPSPeer *rps_peer)
835 while (NULL != rps_peer->pending_req_head)
836 cancel_pending_req (rps_peer->pending_req_head);
837 GNUNET_assert (0 == rps_peer->num_pending_reqs);
838 while (NULL != rps_peer->pending_rep_head)
839 cancel_request (rps_peer->pending_rep_head);
840 GNUNET_assert (0 == rps_peer->num_pending_reps);
843 /***********************************
845 ***********************************/
847 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
849 #ifdef ENABLE_MALICIOUS
850 uint32_t num_mal_peers;
851 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
853 GNUNET_assert ( (1 >= portion) &&
855 num_mal_peers = round (portion * num_peers);
857 if (rps_peer->index < num_mal_peers)
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
862 GNUNET_i2s (rps_peer->peer_id),
865 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
866 rps_peer_ids, target_peer);
868 #endif /* ENABLE_MALICIOUS */
872 mal_cb (struct RPSPeer *rps_peer)
874 uint32_t num_mal_peers;
876 #ifdef ENABLE_MALICIOUS
877 GNUNET_assert ( (1 >= portion) &&
879 num_mal_peers = round (portion * num_peers);
881 if (rps_peer->index >= num_mal_peers)
882 { /* It's useless to ask a malicious peer about a random sample -
884 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
885 seed_peers, rps_peer);
886 schedule_missing_requests (rps_peer);
888 #endif /* ENABLE_MALICIOUS */
894 unsigned int num_mal_peers;
896 num_mal_peers = round (num_peers * portion);
897 return evaluate (&rps_peers[num_mal_peers],
898 num_peers - (num_mal_peers),
903 /***********************************
905 ***********************************/
907 single_req_cb (struct RPSPeer *rps_peer)
909 schedule_missing_requests (rps_peer);
912 /***********************************
914 ***********************************/
916 delay_req_cb (struct RPSPeer *rps_peer)
918 schedule_missing_requests (rps_peer);
921 /***********************************
923 ***********************************/
925 seed_cb (struct RPSPeer *rps_peer)
927 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
928 seed_peers, rps_peer);
931 /***********************************
933 ***********************************/
935 seed_big_cb (struct RPSPeer *rps_peer)
937 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
940 /***********************************
942 ***********************************/
944 single_peer_seed_cb (struct RPSPeer *rps_peer)
949 /***********************************
951 ***********************************/
953 seed_req_cb (struct RPSPeer *rps_peer)
955 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
956 seed_peers, rps_peer);
957 schedule_missing_requests (rps_peer);
962 /***********************************
964 ***********************************/
966 req_cancel_cb (struct RPSPeer *rps_peer)
968 schedule_missing_requests (rps_peer);
969 GNUNET_SCHEDULER_add_delayed (
970 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
971 (cur_test_run.request_interval + 1)),
972 cancel_request_cb, rps_peer);
975 /***********************************
977 ***********************************/
980 * Callback to be called when RPS service is started or stopped at peers
983 * @param op the operation handle
984 * @param emsg NULL on success; otherwise an error description
988 struct GNUNET_TESTBED_Operation *op,
992 struct OpListEntry *entry = cls;
994 GNUNET_TESTBED_operation_done (entry->op);
997 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
998 GNUNET_SCHEDULER_shutdown ();
1001 GNUNET_assert (0 != entry->delta);
1003 num_peers_online += entry->delta;
1005 if (0 > entry->delta)
1006 { /* Peer hopefully just went offline */
1007 if (GNUNET_YES != rps_peers[entry->index].online)
1009 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1010 "peer %s was expected to go offline but is still marked as online\n",
1011 GNUNET_i2s (rps_peers[entry->index].peer_id));
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "peer %s probably went offline as expected\n",
1018 GNUNET_i2s (rps_peers[entry->index].peer_id));
1020 rps_peers[entry->index].online = GNUNET_NO;
1023 else if (0 < entry->delta)
1024 { /* Peer hopefully just went online */
1025 if (GNUNET_NO != rps_peers[entry->index].online)
1027 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1028 "peer %s was expected to go online but is still marked as offline\n",
1029 GNUNET_i2s (rps_peers[entry->index].peer_id));
1034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "peer %s probably went online as expected\n",
1036 GNUNET_i2s (rps_peers[entry->index].peer_id));
1037 if (NULL != cur_test_run.pre_test)
1039 cur_test_run.pre_test (&rps_peers[entry->index],
1040 rps_peers[entry->index].rps_handle);
1041 schedule_missing_requests (&rps_peers[entry->index]);
1044 rps_peers[entry->index].online = GNUNET_YES;
1047 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1048 GNUNET_free (entry);
1049 //if (num_peers_in_round[current_round] == peers_running)
1054 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1055 double prob_go_on_off)
1057 struct OpListEntry *entry;
1060 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1063 "%u. selected peer (%u: %s) is %s.\n",
1066 GNUNET_i2s (rps_peers[j].peer_id),
1067 (delta < 0)? "online" : "offline");
1068 if (prob < prob_go_on_off * UINT32_MAX)
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 GNUNET_i2s (rps_peers[j].peer_id),
1073 (delta < 0) ? "offline" : "online");
1075 entry = make_oplist_entry ();
1076 entry->delta = delta;
1078 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1083 (delta < 0) ? 0 : 1);
1088 churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1092 double portion_online;
1093 unsigned int *permut;
1094 double prob_go_offline;
1095 double portion_go_online;
1096 double portion_go_offline;
1098 /* Compute the probability for an online peer to go offline
1100 portion_online = num_peers_online * 1.0 / num_peers;
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "Portion online: %f\n",
1104 portion_go_online = ((1 - portion_online) * .5 * .66);
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1106 "Portion that should go online: %f\n",
1108 portion_go_offline = (portion_online + portion_go_online) - .75;
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "Portion that probably goes offline: %f\n",
1111 portion_go_offline);
1112 prob_go_offline = portion_go_offline / (portion_online * .5);
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 "Probability of a selected online peer to go offline: %f\n",
1117 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1118 (unsigned int) num_peers);
1120 /* Go over 50% randomly chosen peers */
1121 for (i = 0; i < .5 * num_peers; i++)
1125 /* If online, shut down with certain probability */
1126 if (GNUNET_YES == rps_peers[j].online)
1128 cancel_pending_req_rep (&rps_peers[j]);
1129 manage_service_wrapper (i, j, -1, prob_go_offline);
1132 /* If offline, restart with certain probability */
1133 else if (GNUNET_NO == rps_peers[j].online)
1135 manage_service_wrapper (i, j, 1, 0.66);
1139 GNUNET_free (permut);
1141 churn_task = GNUNET_SCHEDULER_add_delayed (
1142 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1149 * Initialise given RPSPeer
1151 static void profiler_init_peer (struct RPSPeer *rps_peer)
1153 if (num_peers - 1 == rps_peer->index)
1154 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1159 * Callback to call on receipt of a reply
1161 * @param cls closure
1162 * @param n number of peers
1163 * @param recv_peers the received peers
1166 profiler_reply_handle (void *cls,
1168 const struct GNUNET_PeerIdentity *recv_peers)
1170 struct RPSPeer *rps_peer;
1171 struct RPSPeer *rcv_rps_peer;
1175 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1177 rps_peer = pending_rep->rps_peer;
1178 file_name = "/tmp/rps/received_ids";
1179 file_name_dh = "/tmp/rps/diehard_input";
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "[%s] got %" PRIu64 " peers:\n",
1182 GNUNET_i2s (rps_peer->peer_id),
1184 for (i = 0; i < n; i++)
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 GNUNET_i2s (&recv_peers[i]));
1192 GNUNET_i2s_full (&recv_peers[i]));
1193 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1194 tofile (file_name_dh,
1196 (uint32_t) rcv_rps_peer->index);
1198 /* Find #PendingReply holding the request handle */
1199 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1200 rps_peer->pending_rep_tail,
1202 rps_peer->num_pending_reps--;
1207 profiler_cb (struct RPSPeer *rps_peer)
1210 if (NULL == churn_task)
1212 churn_task = GNUNET_SCHEDULER_add_delayed (
1213 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1218 /* Only request peer ids at one peer.
1219 * (It's the before-last because last one is target of the focussed attack.)
1221 if (eval_peer == rps_peer)
1222 schedule_missing_requests (rps_peer);
1226 * Function called from #profiler_eval with a filename.
1228 * @param cls closure
1229 * @param filename complete filename (absolute path)
1230 * @return #GNUNET_OK to continue to iterate,
1231 * #GNUNET_NO to stop iteration with no error,
1232 * #GNUNET_SYSERR to abort iteration with error!
1235 file_name_cb (void *cls, const char *filename)
1237 if (NULL != strstr (filename, "sampler_el"))
1239 struct RPS_SamplerElement *s_elem;
1240 struct GNUNET_CRYPTO_AuthKey auth_key;
1241 const char *key_char;
1244 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1245 tofile (filename, "--------------------------\n");
1247 auth_key = string_to_auth_key (key_char);
1248 s_elem = RPS_sampler_elem_create ();
1249 RPS_sampler_elem_set (s_elem, auth_key);
1251 for (i = 0; i < num_peers; i++)
1253 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1260 * This is run after the test finished.
1262 * Compute all perfect samples.
1265 profiler_eval (void)
1267 /* Compute perfect sample for each sampler element */
1268 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1277 /***********************************************************************
1278 * /Definition of tests
1279 ***********************************************************************/
1283 * Actual "main" function for the testcase.
1285 * @param cls closure
1286 * @param h the run handle
1287 * @param n_peers number of peers in 'peers'
1288 * @param peers handle to peers run in the testbed
1289 * @param links_succeeded the number of overlay link connection attempts that
1291 * @param links_failed the number of overlay link connection attempts that
1296 struct GNUNET_TESTBED_RunHandle *h,
1297 unsigned int n_peers,
1298 struct GNUNET_TESTBED_Peer **peers,
1299 unsigned int links_succeeded,
1300 unsigned int links_failed)
1303 struct OpListEntry *entry;
1305 testbed_peers = peers;
1306 num_peers_online = 0;
1307 for (i = 0 ; i < num_peers ; i++)
1309 entry = make_oplist_entry ();
1311 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1312 GNUNET_TESTBED_PIT_IDENTITY,
1317 GNUNET_assert (num_peers == n_peers);
1318 for (i = 0 ; i < n_peers ; i++)
1320 rps_peers[i].index = i;
1322 GNUNET_TESTBED_service_connect (&rps_peers[i],
1325 &rps_connect_complete_cb,
1327 &rps_connect_adapter,
1328 &rps_disconnect_adapter,
1332 if (NULL != churn_task)
1333 GNUNET_SCHEDULER_cancel (churn_task);
1334 GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_task, NULL);
1339 * Entry point for the testcase, sets up the testbed.
1341 * @param argc unused
1342 * @param argv unused
1343 * @return 0 on success
1346 main (int argc, char *argv[])
1351 cur_test_run.name = "test-rps-default";
1352 cur_test_run.init_peer = default_init_peer;
1353 cur_test_run.pre_test = NULL;
1354 cur_test_run.reply_handle = default_reply_handle;
1355 cur_test_run.eval_cb = default_eval_cb;
1357 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1359 if (strstr (argv[0], "malicious") != NULL)
1361 cur_test_run.pre_test = mal_pre;
1362 cur_test_run.main_test = mal_cb;
1363 cur_test_run.eval_cb = mal_eval;
1365 if (strstr (argv[0], "_1") != NULL)
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1368 cur_test_run.name = "test-rps-malicious_1";
1371 else if (strstr (argv[0], "_2") != NULL)
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1374 cur_test_run.name = "test-rps-malicious_2";
1377 else if (strstr (argv[0], "_3") != NULL)
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1380 cur_test_run.name = "test-rps-malicious_3";
1385 else if (strstr (argv[0], "_single_req") != NULL)
1387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1388 cur_test_run.name = "test-rps-single-req";
1389 cur_test_run.main_test = single_req_cb;
1392 else if (strstr (argv[0], "_delayed_reqs") != NULL)
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1395 cur_test_run.name = "test-rps-delayed-reqs";
1396 cur_test_run.main_test = delay_req_cb;
1399 else if (strstr (argv[0], "_seed_big") != NULL)
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
1402 cur_test_run.name = "test-rps-seed-big";
1403 cur_test_run.main_test = seed_big_cb;
1406 else if (strstr (argv[0], "_single_peer_seed") != NULL)
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1409 cur_test_run.name = "test-rps-single-peer-seed";
1410 cur_test_run.main_test = single_peer_seed_cb;
1413 else if (strstr (argv[0], "_seed_request") != NULL)
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1416 cur_test_run.name = "test-rps-seed-request";
1417 cur_test_run.main_test = seed_req_cb;
1420 else if (strstr (argv[0], "_seed") != NULL)
1422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1423 cur_test_run.name = "test-rps-seed";
1424 cur_test_run.main_test = seed_cb;
1425 cur_test_run.eval_cb = no_eval;
1428 else if (strstr (argv[0], "_req_cancel") != NULL)
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1431 cur_test_run.name = "test-rps-req-cancel";
1433 cur_test_run.main_test = req_cancel_cb;
1434 cur_test_run.eval_cb = no_eval;
1437 else if (strstr (argv[0], "profiler") != NULL)
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1440 cur_test_run.name = "test-rps-profiler";
1443 cur_test_run.init_peer = profiler_init_peer;
1444 cur_test_run.pre_test = mal_pre;
1445 cur_test_run.main_test = profiler_cb;
1446 cur_test_run.reply_handle = profiler_reply_handle;
1447 cur_test_run.eval_cb = profiler_eval;
1448 cur_test_run.request_interval = 2;
1449 cur_test_run.num_requests = 5;
1450 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1452 /* 'Clean' directory */
1453 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1454 GNUNET_DISK_directory_create ("/tmp/rps/");
1457 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1458 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1459 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1460 if ( (2 == mal_type) ||
1462 target_peer = &rps_peer_ids[num_peers - 2];
1463 if (profiler_eval == cur_test_run.eval_cb)
1464 eval_peer = &rps_peers[num_peers - 1];
1467 (void) GNUNET_TESTBED_test_run (cur_test_run.name,
1473 ret_value = cur_test_run.eval_cb();
1474 GNUNET_free (rps_peers );
1475 GNUNET_free (rps_peer_ids);
1476 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1480 /* end of test_rps_multipeer.c */