2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 GNUnet e.V.
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;
218 * Number of received PeerIDs
220 unsigned int num_recv_ids;
225 * Information for all the peers.
227 static struct RPSPeer *rps_peers;
230 * Peermap to get the index of a given peer ID quick.
232 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
237 static struct GNUNET_PeerIdentity *rps_peer_ids;
240 * ID of the targeted peer.
242 static struct GNUNET_PeerIdentity *target_peer;
245 * ID of the peer that requests for the evaluation.
247 static struct RPSPeer *eval_peer;
250 * Number of online peers.
252 static unsigned int num_peers_online;
255 * Return value from 'main'.
261 * Identifier for the churn task that runs periodically
263 static struct GNUNET_SCHEDULER_Task *churn_task;
266 * Identifier for the churn task that runs periodically
268 static struct GNUNET_SCHEDULER_Task *shutdown_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
463 for (i = 0; i < num_peers; i++)
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
468 GNUNET_i2s (rps_peers[i].peer_id),
469 rps_peers[i].num_recv_ids,
470 rps_peers[i].num_ids_to_request,
471 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
472 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
474 return tmp_ok? 0 : 1;
479 * Creates an oplist entry and adds it to the oplist DLL
481 static struct OpListEntry *
484 struct OpListEntry *entry;
486 entry = GNUNET_new (struct OpListEntry);
487 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
493 * Task run on timeout to shut everything down.
496 shutdown_op (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
500 in_shutdown = GNUNET_YES;
501 if (NULL != churn_task)
502 GNUNET_SCHEDULER_cancel (churn_task);
504 for (i = 0; i < num_peers; i++)
505 if (NULL != rps_peers[i].op)
506 GNUNET_TESTBED_operation_done (rps_peers[i].op);
507 GNUNET_SCHEDULER_shutdown ();
515 seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
518 struct RPSPeer *peer = (struct RPSPeer *) cls;
521 // TODO if malicious don't seed mal peers
522 amount = round (.5 * num_peers);
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
525 for (i = 0 ; i < amount ; i++)
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
528 GNUNET_i2s (&rps_peer_ids[i]));
530 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
537 seed_peers_big (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
539 struct RPSPeer *peer = (struct RPSPeer *) cls;
540 unsigned int seed_msg_size;
541 uint32_t num_peers_max;
545 seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
546 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE - seed_msg_size) /
547 sizeof (struct GNUNET_PeerIdentity);
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Peers that fit in one seed msg; %u\n",
551 amount = num_peers_max + (0.5 * num_peers_max);
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Seeding many (%u) peers:\n",
555 struct GNUNET_PeerIdentity ids_to_seed[amount];
556 for (i = 0; i < amount; i++)
558 ids_to_seed[i] = *peer->peer_id;
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
561 GNUNET_i2s (&ids_to_seed[i]));
564 GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
568 * Get the id of peer i.
571 info_cb (void *cb_cls,
572 struct GNUNET_TESTBED_Operation *op,
573 const struct GNUNET_TESTBED_PeerInformation *pinfo,
576 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
578 if (NULL == pinfo || NULL != emsg)
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
581 GNUNET_TESTBED_operation_done (entry->op);
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 GNUNET_i2s (pinfo->result.id));
590 rps_peer_ids[entry->index] = *(pinfo->result.id);
591 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
593 GNUNET_CONTAINER_multipeermap_put (peer_map,
594 &rps_peer_ids[entry->index],
595 &rps_peers[entry->index],
596 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
597 tofile ("/tmp/rps/peer_ids",
600 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
602 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
603 GNUNET_TESTBED_operation_done (entry->op);
609 * Callback to be called when RPS service connect operation is completed
611 * @param cls the callback closure from functions generating an operation
612 * @param op the operation that has been finished
613 * @param ca_result the RPS service handle returned from rps_connect_adapter
614 * @param emsg error message in case the operation has failed; will be NULL if
615 * operation has executed successfully.
618 rps_connect_complete_cb (void *cls,
619 struct GNUNET_TESTBED_Operation *op,
623 struct RPSPeer *rps_peer = cls;
624 struct GNUNET_RPS_Handle *rps = ca_result;
626 rps_peer->rps_handle = rps;
627 rps_peer->online = GNUNET_YES;
630 GNUNET_assert (op == rps_peer->op);
633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
634 "Failed to connect to RPS service: %s\n",
637 GNUNET_SCHEDULER_shutdown ();
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
643 cur_test_run.main_test (rps_peer);
648 * Adapter function called to establish a connection to
652 * @param cfg configuration of the peer to connect to; will be available until
653 * GNUNET_TESTBED_operation_done() is called on the operation returned
654 * from GNUNET_TESTBED_service_connect()
655 * @return service handle to return in 'op_result', NULL on error
658 rps_connect_adapter (void *cls,
659 const struct GNUNET_CONFIGURATION_Handle *cfg)
661 struct GNUNET_RPS_Handle *h;
663 h = GNUNET_RPS_connect (cfg);
665 if (NULL != cur_test_run.pre_test)
666 cur_test_run.pre_test (cls, h);
673 * Adapter function called to destroy connection to
677 * @param op_result service handle returned from the connect adapter
680 rps_disconnect_adapter (void *cls,
683 struct RPSPeer *peer = cls;
684 struct GNUNET_RPS_Handle *h = op_result;
685 GNUNET_assert (NULL != peer);
686 GNUNET_RPS_disconnect (h);
687 peer->rps_handle = NULL;
691 /***********************************************************************
692 * Definition of tests
693 ***********************************************************************/
695 // TODO check whether tests can be stopped earlier
697 default_eval_cb (void)
709 * Initialise given RPSPeer
711 static void default_init_peer (struct RPSPeer *rps_peer)
713 rps_peer->num_ids_to_request = 1;
717 * Callback to call on receipt of a reply
720 * @param n number of peers
721 * @param recv_peers the received peers
724 default_reply_handle (void *cls,
726 const struct GNUNET_PeerIdentity *recv_peers)
728 struct RPSPeer *rps_peer;
729 struct PendingReply *pending_rep = (struct PendingReply *) cls;
732 rps_peer = pending_rep->rps_peer;
733 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
734 rps_peer->pending_rep_tail,
736 rps_peer->num_pending_reps--;
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "[%s] got %" PRIu64 " peers:\n",
739 GNUNET_i2s (rps_peer->peer_id),
742 for (i = 0; i < n; i++)
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747 GNUNET_i2s (&recv_peers[i]));
749 rps_peer->num_recv_ids++;
752 if (0 == evaluate ())
754 GNUNET_SCHEDULER_cancel (shutdown_task);
755 shutdown_task = GNUNET_SCHEDULER_add_now (&shutdown_op, NULL);
760 * Request random peers.
763 request_peers (void *cls,
764 const struct GNUNET_SCHEDULER_TaskContext *tc)
766 struct RPSPeer *rps_peer;
767 struct PendingRequest *pending_req = (struct PendingRequest *) cls;
768 struct PendingReply *pending_rep;
770 if (GNUNET_YES == in_shutdown)
772 rps_peer = pending_req->rps_peer;
773 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
774 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
775 rps_peer->pending_req_tail,
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778 "Requesting one peer\n");
779 pending_rep = GNUNET_new (struct PendingReply);
780 pending_rep->rps_peer = rps_peer;
781 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
783 cur_test_run.reply_handle,
785 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
786 rps_peer->pending_rep_tail,
788 rps_peer->num_pending_reps++;
789 rps_peer->num_pending_reqs--;
793 cancel_pending_req (struct PendingRequest *pending_req)
795 struct RPSPeer *rps_peer;
797 rps_peer = pending_req->rps_peer;
798 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
799 rps_peer->pending_req_tail,
801 rps_peer->num_pending_reqs--;
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "Cancelling pending request\n");
804 GNUNET_SCHEDULER_cancel (pending_req->request_task);
805 GNUNET_free (pending_req);
809 cancel_request (struct PendingReply *pending_rep)
811 struct RPSPeer *rps_peer;
813 rps_peer = pending_rep->rps_peer;
814 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
815 rps_peer->pending_rep_tail,
817 rps_peer->num_pending_reps--;
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Cancelling request\n");
820 GNUNET_RPS_request_cancel (pending_rep->req_handle);
821 GNUNET_free (pending_rep);
828 cancel_request_cb (void *cls,
829 const struct GNUNET_SCHEDULER_TaskContext *tc)
831 struct PendingReply *pending_rep;
832 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
834 if (GNUNET_YES == in_shutdown)
836 pending_rep = rps_peer->pending_rep_head;
837 GNUNET_assert (1 <= rps_peer->num_pending_reps);
838 cancel_request (pending_rep);
843 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
844 * issued, nor replied
847 schedule_missing_requests (struct RPSPeer *rps_peer)
850 struct PendingRequest *pending_req;
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "Scheduling %u - %u missing requests\n",
854 rps_peer->num_ids_to_request,
855 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
856 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
857 rps_peer->num_ids_to_request);
858 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
859 i < rps_peer->num_ids_to_request; i++)
861 pending_req = GNUNET_new (struct PendingRequest);
862 pending_req->rps_peer = rps_peer;
863 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
864 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
865 cur_test_run.request_interval * i),
868 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
869 rps_peer->pending_req_tail,
871 rps_peer->num_pending_reqs++;
876 cancel_pending_req_rep (struct RPSPeer *rps_peer)
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Cancelling all (pending) requests.\n");
880 while (NULL != rps_peer->pending_req_head)
881 cancel_pending_req (rps_peer->pending_req_head);
882 GNUNET_assert (0 == rps_peer->num_pending_reqs);
883 while (NULL != rps_peer->pending_rep_head)
884 cancel_request (rps_peer->pending_rep_head);
885 GNUNET_assert (0 == rps_peer->num_pending_reps);
888 /***********************************
890 ***********************************/
893 * Initialise only non-mal RPSPeers
895 static void mal_init_peer (struct RPSPeer *rps_peer)
897 if (rps_peer->index >= round (portion * num_peers))
898 rps_peer->num_ids_to_request = 1;
902 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
904 #ifdef ENABLE_MALICIOUS
905 uint32_t num_mal_peers;
906 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
908 GNUNET_assert ( (1 >= portion) &&
910 num_mal_peers = round (portion * num_peers);
912 if (rps_peer->index < num_mal_peers)
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
917 GNUNET_i2s (rps_peer->peer_id),
920 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
921 rps_peer_ids, target_peer);
923 #endif /* ENABLE_MALICIOUS */
927 mal_cb (struct RPSPeer *rps_peer)
929 uint32_t num_mal_peers;
931 #ifdef ENABLE_MALICIOUS
932 GNUNET_assert ( (1 >= portion) &&
934 num_mal_peers = round (portion * num_peers);
936 if (rps_peer->index >= num_mal_peers)
937 { /* It's useless to ask a malicious peer about a random sample -
939 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
940 seed_peers, rps_peer);
941 schedule_missing_requests (rps_peer);
943 #endif /* ENABLE_MALICIOUS */
947 /***********************************
949 ***********************************/
951 single_req_cb (struct RPSPeer *rps_peer)
953 schedule_missing_requests (rps_peer);
956 /***********************************
958 ***********************************/
960 delay_req_cb (struct RPSPeer *rps_peer)
962 schedule_missing_requests (rps_peer);
965 /***********************************
967 ***********************************/
969 seed_cb (struct RPSPeer *rps_peer)
971 GNUNET_SCHEDULER_add_delayed (
972 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
973 seed_peers, rps_peer);
976 /***********************************
978 ***********************************/
980 seed_big_cb (struct RPSPeer *rps_peer)
982 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
983 GNUNET_SCHEDULER_add_delayed (
984 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
985 seed_peers_big, rps_peer);
988 /***********************************
990 ***********************************/
992 single_peer_seed_cb (struct RPSPeer *rps_peer)
997 /***********************************
999 ***********************************/
1001 seed_req_cb (struct RPSPeer *rps_peer)
1003 GNUNET_SCHEDULER_add_delayed (
1004 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1005 seed_peers, rps_peer);
1006 schedule_missing_requests (rps_peer);
1009 //TODO start big mal
1011 /***********************************
1013 ***********************************/
1015 req_cancel_cb (struct RPSPeer *rps_peer)
1017 schedule_missing_requests (rps_peer);
1018 GNUNET_SCHEDULER_add_delayed (
1019 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1020 (cur_test_run.request_interval + 1)),
1021 cancel_request_cb, rps_peer);
1024 /***********************************
1026 ***********************************/
1029 * Callback to be called when RPS service is started or stopped at peers
1032 * @param op the operation handle
1033 * @param emsg NULL on success; otherwise an error description
1036 churn_cb (void *cls,
1037 struct GNUNET_TESTBED_Operation *op,
1041 struct OpListEntry *entry = cls;
1043 GNUNET_TESTBED_operation_done (entry->op);
1046 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1047 GNUNET_SCHEDULER_shutdown ();
1050 GNUNET_assert (0 != entry->delta);
1052 num_peers_online += entry->delta;
1054 if (0 > entry->delta)
1055 { /* Peer hopefully just went offline */
1056 if (GNUNET_YES != rps_peers[entry->index].online)
1058 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1059 "peer %s was expected to go offline but is still marked as online\n",
1060 GNUNET_i2s (rps_peers[entry->index].peer_id));
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "peer %s probably went offline as expected\n",
1067 GNUNET_i2s (rps_peers[entry->index].peer_id));
1069 rps_peers[entry->index].online = GNUNET_NO;
1072 else if (0 < entry->delta)
1073 { /* Peer hopefully just went online */
1074 if (GNUNET_NO != rps_peers[entry->index].online)
1076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1077 "peer %s was expected to go online but is still marked as offline\n",
1078 GNUNET_i2s (rps_peers[entry->index].peer_id));
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "peer %s probably went online as expected\n",
1085 GNUNET_i2s (rps_peers[entry->index].peer_id));
1086 if (NULL != cur_test_run.pre_test)
1088 cur_test_run.pre_test (&rps_peers[entry->index],
1089 rps_peers[entry->index].rps_handle);
1090 schedule_missing_requests (&rps_peers[entry->index]);
1093 rps_peers[entry->index].online = GNUNET_YES;
1096 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1097 GNUNET_free (entry);
1098 //if (num_peers_in_round[current_round] == peers_running)
1103 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1104 double prob_go_on_off)
1106 struct OpListEntry *entry;
1109 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "%u. selected peer (%u: %s) is %s.\n",
1115 GNUNET_i2s (rps_peers[j].peer_id),
1116 (0 > delta) ? "online" : "offline");
1117 if (prob < prob_go_on_off * UINT32_MAX)
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1121 GNUNET_i2s (rps_peers[j].peer_id),
1122 (0 > delta) ? "offline" : "online");
1125 cancel_pending_req_rep (&rps_peers[j]);
1126 entry = make_oplist_entry ();
1127 entry->delta = delta;
1129 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1134 (0 > delta) ? 0 : 1);
1139 churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1143 double portion_online;
1144 unsigned int *permut;
1145 double prob_go_offline;
1146 double portion_go_online;
1147 double portion_go_offline;
1149 /* Compute the probability for an online peer to go offline
1151 portion_online = num_peers_online * 1.0 / num_peers;
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Portion online: %f\n",
1155 portion_go_online = ((1 - portion_online) * .5 * .66);
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 "Portion that should go online: %f\n",
1159 portion_go_offline = (portion_online + portion_go_online) - .75;
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1161 "Portion that probably goes offline: %f\n",
1162 portion_go_offline);
1163 prob_go_offline = portion_go_offline / (portion_online * .5);
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 "Probability of a selected online peer to go offline: %f\n",
1168 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1169 (unsigned int) num_peers);
1171 /* Go over 50% randomly chosen peers */
1172 for (i = 0; i < .5 * num_peers; i++)
1176 /* If online, shut down with certain probability */
1177 if (GNUNET_YES == rps_peers[j].online)
1179 manage_service_wrapper (i, j, -1, prob_go_offline);
1182 /* If offline, restart with certain probability */
1183 else if (GNUNET_NO == rps_peers[j].online)
1185 manage_service_wrapper (i, j, 1, 0.66);
1189 GNUNET_free (permut);
1191 churn_task = GNUNET_SCHEDULER_add_delayed (
1192 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1199 * Initialise given RPSPeer
1201 static void profiler_init_peer (struct RPSPeer *rps_peer)
1203 if (num_peers - 1 == rps_peer->index)
1204 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1209 * Callback to call on receipt of a reply
1211 * @param cls closure
1212 * @param n number of peers
1213 * @param recv_peers the received peers
1216 profiler_reply_handle (void *cls,
1218 const struct GNUNET_PeerIdentity *recv_peers)
1220 struct RPSPeer *rps_peer;
1221 struct RPSPeer *rcv_rps_peer;
1225 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1227 rps_peer = pending_rep->rps_peer;
1228 file_name = "/tmp/rps/received_ids";
1229 file_name_dh = "/tmp/rps/diehard_input";
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "[%s] got %" PRIu64 " peers:\n",
1232 GNUNET_i2s (rps_peer->peer_id),
1234 for (i = 0; i < n; i++)
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 GNUNET_i2s (&recv_peers[i]));
1242 GNUNET_i2s_full (&recv_peers[i]));
1243 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1244 tofile (file_name_dh,
1246 (uint32_t) rcv_rps_peer->index);
1248 default_reply_handle (cls, n, recv_peers);
1253 profiler_cb (struct RPSPeer *rps_peer)
1256 if (NULL == churn_task)
1258 churn_task = GNUNET_SCHEDULER_add_delayed (
1259 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1264 /* Only request peer ids at one peer.
1265 * (It's the before-last because last one is target of the focussed attack.)
1267 if (eval_peer == rps_peer)
1268 schedule_missing_requests (rps_peer);
1272 * Function called from #profiler_eval with a filename.
1274 * @param cls closure
1275 * @param filename complete filename (absolute path)
1276 * @return #GNUNET_OK to continue to iterate,
1277 * #GNUNET_NO to stop iteration with no error,
1278 * #GNUNET_SYSERR to abort iteration with error!
1281 file_name_cb (void *cls, const char *filename)
1283 if (NULL != strstr (filename, "sampler_el"))
1285 struct RPS_SamplerElement *s_elem;
1286 struct GNUNET_CRYPTO_AuthKey auth_key;
1287 const char *key_char;
1290 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1291 tofile (filename, "--------------------------\n");
1293 auth_key = string_to_auth_key (key_char);
1294 s_elem = RPS_sampler_elem_create ();
1295 RPS_sampler_elem_set (s_elem, auth_key);
1297 for (i = 0; i < num_peers; i++)
1299 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1306 * This is run after the test finished.
1308 * Compute all perfect samples.
1311 profiler_eval (void)
1313 /* Compute perfect sample for each sampler element */
1314 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1323 /***********************************************************************
1324 * /Definition of tests
1325 ***********************************************************************/
1329 * Actual "main" function for the testcase.
1331 * @param cls closure
1332 * @param h the run handle
1333 * @param n_peers number of peers in 'peers'
1334 * @param peers handle to peers run in the testbed
1335 * @param links_succeeded the number of overlay link connection attempts that
1337 * @param links_failed the number of overlay link connection attempts that
1342 struct GNUNET_TESTBED_RunHandle *h,
1343 unsigned int n_peers,
1344 struct GNUNET_TESTBED_Peer **peers,
1345 unsigned int links_succeeded,
1346 unsigned int links_failed)
1349 struct OpListEntry *entry;
1350 uint32_t num_mal_peers;
1352 testbed_peers = peers;
1353 num_peers_online = 0;
1354 for (i = 0; i < num_peers; i++)
1356 entry = make_oplist_entry ();
1358 rps_peers[i].index = i;
1359 if (NULL != cur_test_run.init_peer)
1360 cur_test_run.init_peer (&rps_peers[i]);
1361 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1362 GNUNET_TESTBED_PIT_IDENTITY,
1367 num_mal_peers = round (portion * num_peers);
1368 GNUNET_assert (num_peers == n_peers);
1369 for (i = 0; i < n_peers; i++)
1371 rps_peers[i].index = i;
1372 if ( (rps_peers[i].num_recv_ids < rps_peers[i].num_ids_to_request) ||
1373 (i < num_mal_peers) )
1376 GNUNET_TESTBED_service_connect (&rps_peers[i],
1379 &rps_connect_complete_cb,
1381 &rps_connect_adapter,
1382 &rps_disconnect_adapter,
1387 if (NULL != churn_task)
1388 GNUNET_SCHEDULER_cancel (churn_task);
1389 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1394 * Entry point for the testcase, sets up the testbed.
1396 * @param argc unused
1397 * @param argv unused
1398 * @return 0 on success
1401 main (int argc, char *argv[])
1406 cur_test_run.name = "test-rps-default";
1407 cur_test_run.init_peer = default_init_peer;
1408 cur_test_run.pre_test = NULL;
1409 cur_test_run.reply_handle = default_reply_handle;
1410 cur_test_run.eval_cb = default_eval_cb;
1412 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1414 if (strstr (argv[0], "malicious") != NULL)
1416 cur_test_run.pre_test = mal_pre;
1417 cur_test_run.main_test = mal_cb;
1418 cur_test_run.init_peer = mal_init_peer;
1420 if (strstr (argv[0], "_1") != NULL)
1422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1423 cur_test_run.name = "test-rps-malicious_1";
1426 else if (strstr (argv[0], "_2") != NULL)
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1429 cur_test_run.name = "test-rps-malicious_2";
1432 else if (strstr (argv[0], "_3") != NULL)
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1435 cur_test_run.name = "test-rps-malicious_3";
1440 else if (strstr (argv[0], "_single_req") != NULL)
1442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1443 cur_test_run.name = "test-rps-single-req";
1444 cur_test_run.main_test = single_req_cb;
1447 else if (strstr (argv[0], "_delayed_reqs") != NULL)
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1450 cur_test_run.name = "test-rps-delayed-reqs";
1451 cur_test_run.main_test = delay_req_cb;
1454 else if (strstr (argv[0], "_seed_big") != NULL)
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
1458 cur_test_run.name = "test-rps-seed-big";
1459 cur_test_run.main_test = seed_big_cb;
1460 cur_test_run.eval_cb = no_eval;
1461 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1464 else if (strstr (argv[0], "_single_peer_seed") != NULL)
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1467 cur_test_run.name = "test-rps-single-peer-seed";
1468 cur_test_run.main_test = single_peer_seed_cb;
1471 else if (strstr (argv[0], "_seed_request") != NULL)
1473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1474 cur_test_run.name = "test-rps-seed-request";
1475 cur_test_run.main_test = seed_req_cb;
1478 else if (strstr (argv[0], "_seed") != NULL)
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1481 cur_test_run.name = "test-rps-seed";
1482 cur_test_run.main_test = seed_cb;
1483 cur_test_run.eval_cb = no_eval;
1486 else if (strstr (argv[0], "_req_cancel") != NULL)
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1489 cur_test_run.name = "test-rps-req-cancel";
1491 cur_test_run.main_test = req_cancel_cb;
1492 cur_test_run.eval_cb = no_eval;
1493 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1496 else if (strstr (argv[0], "profiler") != NULL)
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1499 cur_test_run.name = "test-rps-profiler";
1502 cur_test_run.init_peer = profiler_init_peer;
1503 cur_test_run.pre_test = mal_pre;
1504 cur_test_run.main_test = profiler_cb;
1505 cur_test_run.reply_handle = profiler_reply_handle;
1506 cur_test_run.eval_cb = profiler_eval;
1507 cur_test_run.request_interval = 2;
1508 cur_test_run.num_requests = 5;
1509 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1511 /* 'Clean' directory */
1512 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1513 GNUNET_DISK_directory_create ("/tmp/rps/");
1516 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1517 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1518 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1519 if ( (2 == mal_type) ||
1521 target_peer = &rps_peer_ids[num_peers - 2];
1522 if (profiler_eval == cur_test_run.eval_cb)
1523 eval_peer = &rps_peers[num_peers - 1];
1526 (void) GNUNET_TESTBED_test_run (cur_test_run.name,
1532 ret_value = cur_test_run.eval_cb();
1533 GNUNET_free (rps_peers );
1534 GNUNET_free (rps_peer_ids);
1535 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1539 /* end of test_rps_multipeer.c */