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?
41 static uint32_t num_peers;
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'.
260 * Identifier for the churn task that runs periodically
262 static struct GNUNET_SCHEDULER_Task *churn_task;
265 * Called to initialise the given RPSPeer
267 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
270 * Called directly after connecting to the service
272 typedef void (*PreTest) (void *cls, struct GNUNET_RPS_Handle *h);
275 * Called from within #rps_connect_complete_cb ()
276 * Executes functions to test the api/service
278 typedef void (*MainTest) (struct RPSPeer *rps_peer);
281 * Callback called once the requested random peers are available
283 typedef void (*ReplyHandle) (void *cls,
285 const struct GNUNET_PeerIdentity *recv_peers);
288 * Called directly before disconnecting from the service
290 typedef void (*PostTest) (void *cls, struct GNUNET_RPS_Handle *h);
293 * Function called after disconnect to evaluate test success
295 typedef int (*EvaluationCallback) (void);
299 * Structure to define a single test
309 * Called to initialise peer
314 * Called directly after connecting to the service
319 * Function to execute the functions to be tested
324 * Callback called once the requested peers are available
326 ReplyHandle reply_handle;
329 * Called directly before disconnecting from the service
334 * Function to evaluate the test results
336 EvaluationCallback eval_cb;
341 uint32_t request_interval;
344 * Number of Requests to make.
346 uint32_t num_requests;
350 * Are we shutting down?
352 static int in_shutdown;
355 * Append arguments to file
358 tofile_ (const char *file_name, const char *line)
360 struct GNUNET_DISK_FileHandle *f;
361 /* char output_buffer[512]; */
366 if (NULL == (f = GNUNET_DISK_file_open (file_name,
367 GNUNET_DISK_OPEN_APPEND |
368 GNUNET_DISK_OPEN_WRITE |
369 GNUNET_DISK_OPEN_CREATE,
370 GNUNET_DISK_PERM_USER_READ |
371 GNUNET_DISK_PERM_USER_WRITE |
372 GNUNET_DISK_PERM_GROUP_READ |
373 GNUNET_DISK_PERM_OTHER_READ)))
375 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
376 "Not able to open file %s\n",
380 /* size = GNUNET_snprintf (output_buffer,
381 sizeof (output_buffer),
383 GNUNET_TIME_absolute_get ().abs_value_us,
387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
388 "Failed to write string to buffer (size: %i)\n",
393 size = strlen (line) * sizeof (char);
395 size2 = GNUNET_DISK_file_write (f, line, size);
398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
399 "Unable to write to file! (Size: %lu, size2: %lu)\n",
405 if (GNUNET_YES != GNUNET_DISK_file_close (f))
406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
407 "Unable to close file\n");
411 * This function is used to facilitate writing important information to disk
413 #define tofile(file_name, ...) do {\
416 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
419 "Failed to create tmp_buf\n");\
421 tofile_(file_name,tmp_buf);\
426 * Write the ids and their according index in the given array to a file
430 ids_to_file (char *file_name,
431 struct GNUNET_PeerIdentity *peer_ids,
432 unsigned int num_peer_ids)
436 for (i=0 ; i < num_peer_ids ; i++)
441 GNUNET_i2s_full (&peer_ids[i]));
446 * Test the success of a single test
456 for (i = 0; i < num_peers; i++)
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
461 GNUNET_i2s (rps_peers[i].peer_id),
462 rps_peers[i].num_recv_ids,
463 rps_peers[i].num_ids_to_request,
464 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
465 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
467 return tmp_ok? 0 : 1;
472 * Creates an oplist entry and adds it to the oplist DLL
474 static struct OpListEntry *
477 struct OpListEntry *entry;
479 entry = GNUNET_new (struct OpListEntry);
480 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
486 * Task run on timeout to shut everything down.
489 shutdown_op (void *cls)
493 in_shutdown = GNUNET_YES;
494 if (NULL != churn_task)
496 GNUNET_SCHEDULER_cancel (churn_task);
499 for (i = 0; i < num_peers; i++)
500 if (NULL != rps_peers[i].op)
501 GNUNET_TESTBED_operation_done (rps_peers[i].op);
502 GNUNET_SCHEDULER_shutdown ();
510 seed_peers (void *cls)
512 struct RPSPeer *peer = cls;
516 // TODO if malicious don't seed mal peers
517 amount = round (.5 * num_peers);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
520 for (i = 0 ; i < amount ; i++)
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
523 GNUNET_i2s (&rps_peer_ids[i]));
525 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
533 seed_peers_big (void *cls)
535 struct RPSPeer *peer = cls;
536 unsigned int seed_msg_size;
537 uint32_t num_peers_max;
541 seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
542 num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE - seed_msg_size) /
543 sizeof (struct GNUNET_PeerIdentity);
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Peers that fit in one seed msg; %u\n",
547 amount = num_peers_max + (0.5 * num_peers_max);
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Seeding many (%u) peers:\n",
551 struct GNUNET_PeerIdentity ids_to_seed[amount];
552 for (i = 0; i < amount; i++)
554 ids_to_seed[i] = *peer->peer_id;
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
557 GNUNET_i2s (&ids_to_seed[i]));
560 GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
564 * Get the id of peer i.
567 info_cb (void *cb_cls,
568 struct GNUNET_TESTBED_Operation *op,
569 const struct GNUNET_TESTBED_PeerInformation *pinfo,
572 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
574 if (NULL == pinfo || NULL != emsg)
576 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
577 GNUNET_TESTBED_operation_done (entry->op);
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 GNUNET_i2s (pinfo->result.id));
586 rps_peer_ids[entry->index] = *(pinfo->result.id);
587 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
589 GNUNET_CONTAINER_multipeermap_put (peer_map,
590 &rps_peer_ids[entry->index],
591 &rps_peers[entry->index],
592 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
593 tofile ("/tmp/rps/peer_ids",
596 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
598 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
599 GNUNET_TESTBED_operation_done (entry->op);
605 * Callback to be called when RPS service connect operation is completed
607 * @param cls the callback closure from functions generating an operation
608 * @param op the operation that has been finished
609 * @param ca_result the RPS service handle returned from rps_connect_adapter
610 * @param emsg error message in case the operation has failed; will be NULL if
611 * operation has executed successfully.
614 rps_connect_complete_cb (void *cls,
615 struct GNUNET_TESTBED_Operation *op,
619 struct RPSPeer *rps_peer = cls;
620 struct GNUNET_RPS_Handle *rps = ca_result;
622 rps_peer->rps_handle = rps;
623 rps_peer->online = GNUNET_YES;
626 GNUNET_assert (op == rps_peer->op);
629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
630 "Failed to connect to RPS service: %s\n",
633 GNUNET_SCHEDULER_shutdown ();
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
639 cur_test_run.main_test (rps_peer);
644 * Adapter function called to establish a connection to
648 * @param cfg configuration of the peer to connect to; will be available until
649 * GNUNET_TESTBED_operation_done() is called on the operation returned
650 * from GNUNET_TESTBED_service_connect()
651 * @return service handle to return in 'op_result', NULL on error
654 rps_connect_adapter (void *cls,
655 const struct GNUNET_CONFIGURATION_Handle *cfg)
657 struct GNUNET_RPS_Handle *h;
659 h = GNUNET_RPS_connect (cfg);
661 if (NULL != cur_test_run.pre_test)
662 cur_test_run.pre_test (cls, h);
669 * Adapter function called to destroy connection to
673 * @param op_result service handle returned from the connect adapter
676 rps_disconnect_adapter (void *cls,
679 struct RPSPeer *peer = cls;
680 struct GNUNET_RPS_Handle *h = op_result;
681 GNUNET_assert (NULL != peer);
682 GNUNET_RPS_disconnect (h);
683 peer->rps_handle = NULL;
687 /***********************************************************************
688 * Definition of tests
689 ***********************************************************************/
691 // TODO check whether tests can be stopped earlier
693 default_eval_cb (void)
705 * Initialise given RPSPeer
707 static void default_init_peer (struct RPSPeer *rps_peer)
709 rps_peer->num_ids_to_request = 1;
713 * Callback to call on receipt of a reply
716 * @param n number of peers
717 * @param recv_peers the received peers
720 default_reply_handle (void *cls,
722 const struct GNUNET_PeerIdentity *recv_peers)
724 struct RPSPeer *rps_peer;
725 struct PendingReply *pending_rep = (struct PendingReply *) cls;
728 rps_peer = pending_rep->rps_peer;
729 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
730 rps_peer->pending_rep_tail,
732 rps_peer->num_pending_reps--;
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
734 "[%s] got %" PRIu64 " peers:\n",
735 GNUNET_i2s (rps_peer->peer_id),
738 for (i = 0; i < n; i++)
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 GNUNET_i2s (&recv_peers[i]));
745 rps_peer->num_recv_ids++;
748 if (0 == evaluate ())
750 GNUNET_SCHEDULER_shutdown ();
755 * Request random peers.
758 request_peers (void *cls)
760 struct PendingRequest *pending_req = cls;
761 struct RPSPeer *rps_peer;
762 struct PendingReply *pending_rep;
764 if (GNUNET_YES == in_shutdown)
766 rps_peer = pending_req->rps_peer;
767 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
768 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
769 rps_peer->pending_req_tail,
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
772 "Requesting one peer\n");
773 pending_rep = GNUNET_new (struct PendingReply);
774 pending_rep->rps_peer = rps_peer;
775 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
777 cur_test_run.reply_handle,
779 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
780 rps_peer->pending_rep_tail,
782 rps_peer->num_pending_reps++;
783 rps_peer->num_pending_reqs--;
787 cancel_pending_req (struct PendingRequest *pending_req)
789 struct RPSPeer *rps_peer;
791 rps_peer = pending_req->rps_peer;
792 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
793 rps_peer->pending_req_tail,
795 rps_peer->num_pending_reqs--;
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "Cancelling pending request\n");
798 GNUNET_SCHEDULER_cancel (pending_req->request_task);
799 GNUNET_free (pending_req);
803 cancel_request (struct PendingReply *pending_rep)
805 struct RPSPeer *rps_peer;
807 rps_peer = pending_rep->rps_peer;
808 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
809 rps_peer->pending_rep_tail,
811 rps_peer->num_pending_reps--;
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
813 "Cancelling request\n");
814 GNUNET_RPS_request_cancel (pending_rep->req_handle);
815 GNUNET_free (pending_rep);
822 cancel_request_cb (void *cls)
824 struct RPSPeer *rps_peer = cls;
825 struct PendingReply *pending_rep;
827 if (GNUNET_YES == in_shutdown)
829 pending_rep = rps_peer->pending_rep_head;
830 GNUNET_assert (1 <= rps_peer->num_pending_reps);
831 cancel_request (pending_rep);
836 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
837 * issued, nor replied
840 schedule_missing_requests (struct RPSPeer *rps_peer)
843 struct PendingRequest *pending_req;
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 "Scheduling %u - %u missing requests\n",
847 rps_peer->num_ids_to_request,
848 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
849 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
850 rps_peer->num_ids_to_request);
851 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
852 i < rps_peer->num_ids_to_request; i++)
854 pending_req = GNUNET_new (struct PendingRequest);
855 pending_req->rps_peer = rps_peer;
856 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
857 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
858 cur_test_run.request_interval * i),
861 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
862 rps_peer->pending_req_tail,
864 rps_peer->num_pending_reqs++;
869 cancel_pending_req_rep (struct RPSPeer *rps_peer)
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "Cancelling all (pending) requests.\n");
873 while (NULL != rps_peer->pending_req_head)
874 cancel_pending_req (rps_peer->pending_req_head);
875 GNUNET_assert (0 == rps_peer->num_pending_reqs);
876 while (NULL != rps_peer->pending_rep_head)
877 cancel_request (rps_peer->pending_rep_head);
878 GNUNET_assert (0 == rps_peer->num_pending_reps);
881 /***********************************
883 ***********************************/
886 * Initialise only non-mal RPSPeers
888 static void mal_init_peer (struct RPSPeer *rps_peer)
890 if (rps_peer->index >= round (portion * num_peers))
891 rps_peer->num_ids_to_request = 1;
895 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
897 #ifdef ENABLE_MALICIOUS
898 uint32_t num_mal_peers;
899 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
901 GNUNET_assert ( (1 >= portion) &&
903 num_mal_peers = round (portion * num_peers);
905 if (rps_peer->index < num_mal_peers)
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
910 GNUNET_i2s (rps_peer->peer_id),
913 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
914 rps_peer_ids, target_peer);
916 #endif /* ENABLE_MALICIOUS */
920 mal_cb (struct RPSPeer *rps_peer)
922 uint32_t num_mal_peers;
924 #ifdef ENABLE_MALICIOUS
925 GNUNET_assert ( (1 >= portion) &&
927 num_mal_peers = round (portion * num_peers);
929 if (rps_peer->index >= num_mal_peers)
930 { /* It's useless to ask a malicious peer about a random sample -
932 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
933 seed_peers, rps_peer);
934 schedule_missing_requests (rps_peer);
936 #endif /* ENABLE_MALICIOUS */
940 /***********************************
942 ***********************************/
944 single_req_cb (struct RPSPeer *rps_peer)
946 schedule_missing_requests (rps_peer);
949 /***********************************
951 ***********************************/
953 delay_req_cb (struct RPSPeer *rps_peer)
955 schedule_missing_requests (rps_peer);
958 /***********************************
960 ***********************************/
962 seed_cb (struct RPSPeer *rps_peer)
964 GNUNET_SCHEDULER_add_delayed (
965 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
966 seed_peers, rps_peer);
969 /***********************************
971 ***********************************/
973 seed_big_cb (struct RPSPeer *rps_peer)
975 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
976 GNUNET_SCHEDULER_add_delayed (
977 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
978 seed_peers_big, rps_peer);
981 /***********************************
983 ***********************************/
985 single_peer_seed_cb (struct RPSPeer *rps_peer)
990 /***********************************
992 ***********************************/
994 seed_req_cb (struct RPSPeer *rps_peer)
996 GNUNET_SCHEDULER_add_delayed (
997 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
998 seed_peers, rps_peer);
999 schedule_missing_requests (rps_peer);
1002 //TODO start big mal
1004 /***********************************
1006 ***********************************/
1008 req_cancel_cb (struct RPSPeer *rps_peer)
1010 schedule_missing_requests (rps_peer);
1011 GNUNET_SCHEDULER_add_delayed (
1012 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1013 (cur_test_run.request_interval + 1)),
1014 cancel_request_cb, rps_peer);
1017 /***********************************
1019 ***********************************/
1022 * Callback to be called when RPS service is started or stopped at peers
1025 * @param op the operation handle
1026 * @param emsg NULL on success; otherwise an error description
1029 churn_cb (void *cls,
1030 struct GNUNET_TESTBED_Operation *op,
1034 struct OpListEntry *entry = cls;
1036 GNUNET_TESTBED_operation_done (entry->op);
1039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1040 GNUNET_SCHEDULER_shutdown ();
1043 GNUNET_assert (0 != entry->delta);
1045 num_peers_online += entry->delta;
1047 if (0 > entry->delta)
1048 { /* Peer hopefully just went offline */
1049 if (GNUNET_YES != rps_peers[entry->index].online)
1051 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1052 "peer %s was expected to go offline but is still marked as online\n",
1053 GNUNET_i2s (rps_peers[entry->index].peer_id));
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "peer %s probably went offline as expected\n",
1060 GNUNET_i2s (rps_peers[entry->index].peer_id));
1062 rps_peers[entry->index].online = GNUNET_NO;
1065 else if (0 < entry->delta)
1066 { /* Peer hopefully just went online */
1067 if (GNUNET_NO != rps_peers[entry->index].online)
1069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1070 "peer %s was expected to go online but is still marked as offline\n",
1071 GNUNET_i2s (rps_peers[entry->index].peer_id));
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1077 "peer %s probably went online as expected\n",
1078 GNUNET_i2s (rps_peers[entry->index].peer_id));
1079 if (NULL != cur_test_run.pre_test)
1081 cur_test_run.pre_test (&rps_peers[entry->index],
1082 rps_peers[entry->index].rps_handle);
1083 schedule_missing_requests (&rps_peers[entry->index]);
1086 rps_peers[entry->index].online = GNUNET_YES;
1089 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1090 GNUNET_free (entry);
1091 //if (num_peers_in_round[current_round] == peers_running)
1096 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1097 double prob_go_on_off)
1099 struct OpListEntry *entry;
1102 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1105 "%u. selected peer (%u: %s) is %s.\n",
1108 GNUNET_i2s (rps_peers[j].peer_id),
1109 (0 > delta) ? "online" : "offline");
1110 if (prob < prob_go_on_off * UINT32_MAX)
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 GNUNET_i2s (rps_peers[j].peer_id),
1115 (0 > delta) ? "offline" : "online");
1118 cancel_pending_req_rep (&rps_peers[j]);
1119 entry = make_oplist_entry ();
1120 entry->delta = delta;
1122 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1127 (0 > delta) ? 0 : 1);
1137 double portion_online;
1138 unsigned int *permut;
1139 double prob_go_offline;
1140 double portion_go_online;
1141 double portion_go_offline;
1143 /* Compute the probability for an online peer to go offline
1145 portion_online = num_peers_online * 1.0 / num_peers;
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Portion online: %f\n",
1149 portion_go_online = ((1 - portion_online) * .5 * .66);
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1151 "Portion that should go online: %f\n",
1153 portion_go_offline = (portion_online + portion_go_online) - .75;
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1155 "Portion that probably goes offline: %f\n",
1156 portion_go_offline);
1157 prob_go_offline = portion_go_offline / (portion_online * .5);
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 "Probability of a selected online peer to go offline: %f\n",
1162 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1163 (unsigned int) num_peers);
1165 /* Go over 50% randomly chosen peers */
1166 for (i = 0; i < .5 * num_peers; i++)
1170 /* If online, shut down with certain probability */
1171 if (GNUNET_YES == rps_peers[j].online)
1173 manage_service_wrapper (i, j, -1, prob_go_offline);
1176 /* If offline, restart with certain probability */
1177 else if (GNUNET_NO == rps_peers[j].online)
1179 manage_service_wrapper (i, j, 1, 0.66);
1183 GNUNET_free (permut);
1185 churn_task = GNUNET_SCHEDULER_add_delayed (
1186 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1193 * Initialise given RPSPeer
1195 static void profiler_init_peer (struct RPSPeer *rps_peer)
1197 if (num_peers - 1 == rps_peer->index)
1198 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1203 * Callback to call on receipt of a reply
1205 * @param cls closure
1206 * @param n number of peers
1207 * @param recv_peers the received peers
1210 profiler_reply_handle (void *cls,
1212 const struct GNUNET_PeerIdentity *recv_peers)
1214 struct RPSPeer *rps_peer;
1215 struct RPSPeer *rcv_rps_peer;
1219 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1221 rps_peer = pending_rep->rps_peer;
1222 file_name = "/tmp/rps/received_ids";
1223 file_name_dh = "/tmp/rps/diehard_input";
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "[%s] got %" PRIu64 " peers:\n",
1226 GNUNET_i2s (rps_peer->peer_id),
1228 for (i = 0; i < n; i++)
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 GNUNET_i2s (&recv_peers[i]));
1236 GNUNET_i2s_full (&recv_peers[i]));
1237 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1238 tofile (file_name_dh,
1240 (uint32_t) rcv_rps_peer->index);
1242 default_reply_handle (cls, n, recv_peers);
1247 profiler_cb (struct RPSPeer *rps_peer)
1250 if (NULL == churn_task)
1252 churn_task = GNUNET_SCHEDULER_add_delayed (
1253 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1258 /* Only request peer ids at one peer.
1259 * (It's the before-last because last one is target of the focussed attack.)
1261 if (eval_peer == rps_peer)
1262 schedule_missing_requests (rps_peer);
1266 * Function called from #profiler_eval with a filename.
1268 * @param cls closure
1269 * @param filename complete filename (absolute path)
1270 * @return #GNUNET_OK to continue to iterate,
1271 * #GNUNET_NO to stop iteration with no error,
1272 * #GNUNET_SYSERR to abort iteration with error!
1275 file_name_cb (void *cls, const char *filename)
1277 if (NULL != strstr (filename, "sampler_el"))
1279 struct RPS_SamplerElement *s_elem;
1280 struct GNUNET_CRYPTO_AuthKey auth_key;
1281 const char *key_char;
1284 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1285 tofile (filename, "--------------------------\n");
1287 auth_key = string_to_auth_key (key_char);
1288 s_elem = RPS_sampler_elem_create ();
1289 RPS_sampler_elem_set (s_elem, auth_key);
1291 for (i = 0; i < num_peers; i++)
1293 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1295 RPS_sampler_elem_destroy (s_elem);
1301 * This is run after the test finished.
1303 * Compute all perfect samples.
1306 profiler_eval (void)
1308 /* Compute perfect sample for each sampler element */
1309 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1318 /***********************************************************************
1319 * /Definition of tests
1320 ***********************************************************************/
1324 * Actual "main" function for the testcase.
1326 * @param cls closure
1327 * @param h the run handle
1328 * @param n_peers number of peers in 'peers'
1329 * @param peers handle to peers run in the testbed
1330 * @param links_succeeded the number of overlay link connection attempts that
1332 * @param links_failed the number of overlay link connection attempts that
1337 struct GNUNET_TESTBED_RunHandle *h,
1338 unsigned int n_peers,
1339 struct GNUNET_TESTBED_Peer **peers,
1340 unsigned int links_succeeded,
1341 unsigned int links_failed)
1344 struct OpListEntry *entry;
1345 uint32_t num_mal_peers;
1347 testbed_peers = peers;
1348 num_peers_online = 0;
1349 for (i = 0; i < num_peers; i++)
1351 entry = make_oplist_entry ();
1353 rps_peers[i].index = i;
1354 if (NULL != cur_test_run.init_peer)
1355 cur_test_run.init_peer (&rps_peers[i]);
1356 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1357 GNUNET_TESTBED_PIT_IDENTITY,
1362 num_mal_peers = round (portion * num_peers);
1363 GNUNET_assert (num_peers == n_peers);
1364 for (i = 0; i < n_peers; i++)
1366 rps_peers[i].index = i;
1367 if ( (rps_peers[i].num_recv_ids < rps_peers[i].num_ids_to_request) ||
1368 (i < num_mal_peers) )
1371 GNUNET_TESTBED_service_connect (&rps_peers[i],
1374 &rps_connect_complete_cb,
1376 &rps_connect_adapter,
1377 &rps_disconnect_adapter,
1382 if (NULL != churn_task)
1383 GNUNET_SCHEDULER_cancel (churn_task);
1384 GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1389 * Entry point for the testcase, sets up the testbed.
1391 * @param argc unused
1392 * @param argv unused
1393 * @return 0 on success
1396 main (int argc, char *argv[])
1401 cur_test_run.name = "test-rps-default";
1402 cur_test_run.init_peer = default_init_peer;
1403 cur_test_run.pre_test = NULL;
1404 cur_test_run.reply_handle = default_reply_handle;
1405 cur_test_run.eval_cb = default_eval_cb;
1407 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1409 if (strstr (argv[0], "malicious") != NULL)
1411 cur_test_run.pre_test = mal_pre;
1412 cur_test_run.main_test = mal_cb;
1413 cur_test_run.init_peer = mal_init_peer;
1415 if (strstr (argv[0], "_1") != NULL)
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1418 cur_test_run.name = "test-rps-malicious_1";
1421 else if (strstr (argv[0], "_2") != NULL)
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1424 cur_test_run.name = "test-rps-malicious_2";
1427 else if (strstr (argv[0], "_3") != NULL)
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1430 cur_test_run.name = "test-rps-malicious_3";
1435 else if (strstr (argv[0], "_single_req") != NULL)
1437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1438 cur_test_run.name = "test-rps-single-req";
1439 cur_test_run.main_test = single_req_cb;
1442 else if (strstr (argv[0], "_delayed_reqs") != NULL)
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1445 cur_test_run.name = "test-rps-delayed-reqs";
1446 cur_test_run.main_test = delay_req_cb;
1449 else if (strstr (argv[0], "_seed_big") != NULL)
1451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
1453 cur_test_run.name = "test-rps-seed-big";
1454 cur_test_run.main_test = seed_big_cb;
1455 cur_test_run.eval_cb = no_eval;
1456 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1459 else if (strstr (argv[0], "_single_peer_seed") != NULL)
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1462 cur_test_run.name = "test-rps-single-peer-seed";
1463 cur_test_run.main_test = single_peer_seed_cb;
1466 else if (strstr (argv[0], "_seed_request") != NULL)
1468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1469 cur_test_run.name = "test-rps-seed-request";
1470 cur_test_run.main_test = seed_req_cb;
1473 else if (strstr (argv[0], "_seed") != NULL)
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1476 cur_test_run.name = "test-rps-seed";
1477 cur_test_run.main_test = seed_cb;
1478 cur_test_run.eval_cb = no_eval;
1481 else if (strstr (argv[0], "_req_cancel") != NULL)
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1484 cur_test_run.name = "test-rps-req-cancel";
1486 cur_test_run.main_test = req_cancel_cb;
1487 cur_test_run.eval_cb = no_eval;
1488 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1491 else if (strstr (argv[0], "profiler") != NULL)
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1494 cur_test_run.name = "test-rps-profiler";
1497 cur_test_run.init_peer = profiler_init_peer;
1498 cur_test_run.pre_test = mal_pre;
1499 cur_test_run.main_test = profiler_cb;
1500 cur_test_run.reply_handle = profiler_reply_handle;
1501 cur_test_run.eval_cb = profiler_eval;
1502 cur_test_run.request_interval = 2;
1503 cur_test_run.num_requests = 5;
1504 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1506 /* 'Clean' directory */
1507 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1508 GNUNET_DISK_directory_create ("/tmp/rps/");
1511 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1512 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1513 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1514 if ( (2 == mal_type) ||
1516 target_peer = &rps_peer_ids[num_peers - 2];
1517 if (profiler_eval == cur_test_run.eval_cb)
1518 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
1519 malicious peer if not careful
1520 with the malicious portion */
1523 (void) GNUNET_TESTBED_test_run (cur_test_run.name,
1529 ret_value = cur_test_run.eval_cb();
1530 GNUNET_free (rps_peers );
1531 GNUNET_free (rps_peer_ids);
1532 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1536 /* end of test_rps_multipeer.c */