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;
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);
535 * Get the id of peer i.
538 info_cb (void *cb_cls,
539 struct GNUNET_TESTBED_Operation *op,
540 const struct GNUNET_TESTBED_PeerInformation *pinfo,
543 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
545 if (NULL == pinfo || NULL != emsg)
547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
548 GNUNET_TESTBED_operation_done (entry->op);
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];
560 GNUNET_CONTAINER_multipeermap_put (peer_map,
561 &rps_peer_ids[entry->index],
562 &rps_peers[entry->index],
563 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
564 tofile ("/tmp/rps/peer_ids",
567 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
569 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
570 GNUNET_TESTBED_operation_done (entry->op);
576 * Callback to be called when RPS service connect operation is completed
578 * @param cls the callback closure from functions generating an operation
579 * @param op the operation that has been finished
580 * @param ca_result the RPS service handle returned from rps_connect_adapter
581 * @param emsg error message in case the operation has failed; will be NULL if
582 * operation has executed successfully.
585 rps_connect_complete_cb (void *cls,
586 struct GNUNET_TESTBED_Operation *op,
590 struct RPSPeer *rps_peer = cls;
591 struct GNUNET_RPS_Handle *rps = ca_result;
593 rps_peer->rps_handle = rps;
594 rps_peer->online = GNUNET_YES;
597 GNUNET_assert (op == rps_peer->op);
600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
601 "Failed to connect to RPS service: %s\n",
604 GNUNET_SCHEDULER_shutdown ();
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
610 cur_test_run.main_test (rps_peer);
615 * Adapter function called to establish a connection to
619 * @param cfg configuration of the peer to connect to; will be available until
620 * GNUNET_TESTBED_operation_done() is called on the operation returned
621 * from GNUNET_TESTBED_service_connect()
622 * @return service handle to return in 'op_result', NULL on error
625 rps_connect_adapter (void *cls,
626 const struct GNUNET_CONFIGURATION_Handle *cfg)
628 struct GNUNET_RPS_Handle *h;
630 h = GNUNET_RPS_connect (cfg);
632 if (NULL != cur_test_run.pre_test)
633 cur_test_run.pre_test (cls, h);
640 * Adapter function called to destroy connection to
644 * @param op_result service handle returned from the connect adapter
647 rps_disconnect_adapter (void *cls,
650 struct RPSPeer *peer = cls;
651 struct GNUNET_RPS_Handle *h = op_result;
652 GNUNET_assert (NULL != peer);
653 GNUNET_RPS_disconnect (h);
654 peer->rps_handle = NULL;
658 /***********************************************************************
659 * Definition of tests
660 ***********************************************************************/
662 // TODO check whether tests can be stopped earlier
664 default_eval_cb (void)
676 * Initialise given RPSPeer
678 static void default_init_peer (struct RPSPeer *rps_peer)
680 rps_peer->num_ids_to_request = 1;
684 * Callback to call on receipt of a reply
687 * @param n number of peers
688 * @param recv_peers the received peers
691 default_reply_handle (void *cls,
693 const struct GNUNET_PeerIdentity *recv_peers)
695 struct RPSPeer *rps_peer;
696 struct PendingReply *pending_rep = (struct PendingReply *) cls;
699 rps_peer = pending_rep->rps_peer;
700 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
701 rps_peer->pending_rep_tail,
703 rps_peer->num_pending_reps--;
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "[%s] got %" PRIu64 " peers:\n",
706 GNUNET_i2s (rps_peer->peer_id),
709 for (i = 0; i < n; i++)
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 GNUNET_i2s (&recv_peers[i]));
716 rps_peer->num_recv_ids++;
719 if (0 == evaluate ())
721 GNUNET_SCHEDULER_cancel (shutdown_task);
722 shutdown_task = GNUNET_SCHEDULER_add_now (&shutdown_op, NULL);
727 * Request random peers.
730 request_peers (void *cls,
731 const struct GNUNET_SCHEDULER_TaskContext *tc)
733 struct RPSPeer *rps_peer;
734 struct PendingRequest *pending_req = (struct PendingRequest *) cls;
735 struct PendingReply *pending_rep;
737 if (GNUNET_YES == in_shutdown)
739 rps_peer = pending_req->rps_peer;
740 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
741 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
742 rps_peer->pending_req_tail,
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Requesting one peer\n");
746 pending_rep = GNUNET_new (struct PendingReply);
747 pending_rep->rps_peer = rps_peer;
748 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
750 cur_test_run.reply_handle,
752 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
753 rps_peer->pending_rep_tail,
755 rps_peer->num_pending_reps++;
756 rps_peer->num_pending_reqs--;
760 cancel_pending_req (struct PendingRequest *pending_req)
762 struct RPSPeer *rps_peer;
764 rps_peer = pending_req->rps_peer;
765 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
766 rps_peer->pending_req_tail,
768 rps_peer->num_pending_reqs--;
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
770 "Cancelling pending request\n");
771 GNUNET_SCHEDULER_cancel (pending_req->request_task);
772 GNUNET_free (pending_req);
776 cancel_request (struct PendingReply *pending_rep)
778 struct RPSPeer *rps_peer;
780 rps_peer = pending_rep->rps_peer;
781 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
782 rps_peer->pending_rep_tail,
784 rps_peer->num_pending_reps--;
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Cancelling request\n");
787 GNUNET_RPS_request_cancel (pending_rep->req_handle);
788 GNUNET_free (pending_rep);
795 cancel_request_cb (void *cls,
796 const struct GNUNET_SCHEDULER_TaskContext *tc)
798 struct PendingReply *pending_rep;
799 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
801 if (GNUNET_YES == in_shutdown)
803 pending_rep = rps_peer->pending_rep_head;
804 GNUNET_assert (1 <= rps_peer->num_pending_reps);
805 cancel_request (pending_rep);
810 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
811 * issued, nor replied
814 schedule_missing_requests (struct RPSPeer *rps_peer)
817 struct PendingRequest *pending_req;
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820 "Scheduling %u - %u missing requests\n",
821 rps_peer->num_ids_to_request,
822 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
823 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
824 rps_peer->num_ids_to_request);
825 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
826 i < rps_peer->num_ids_to_request; i++)
828 pending_req = GNUNET_new (struct PendingRequest);
829 pending_req->rps_peer = rps_peer;
830 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
831 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
832 cur_test_run.request_interval * i),
835 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
836 rps_peer->pending_req_tail,
838 rps_peer->num_pending_reqs++;
843 cancel_pending_req_rep (struct RPSPeer *rps_peer)
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 "Cancelling all (pending) requests.\n");
847 while (NULL != rps_peer->pending_req_head)
848 cancel_pending_req (rps_peer->pending_req_head);
849 GNUNET_assert (0 == rps_peer->num_pending_reqs);
850 while (NULL != rps_peer->pending_rep_head)
851 cancel_request (rps_peer->pending_rep_head);
852 GNUNET_assert (0 == rps_peer->num_pending_reps);
855 /***********************************
857 ***********************************/
860 * Initialise only non-mal RPSPeers
862 static void mal_init_peer (struct RPSPeer *rps_peer)
864 if (rps_peer->index >= round (portion * num_peers))
865 rps_peer->num_ids_to_request = 1;
869 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
871 #ifdef ENABLE_MALICIOUS
872 uint32_t num_mal_peers;
873 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
875 GNUNET_assert ( (1 >= portion) &&
877 num_mal_peers = round (portion * num_peers);
879 if (rps_peer->index < num_mal_peers)
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
884 GNUNET_i2s (rps_peer->peer_id),
887 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
888 rps_peer_ids, target_peer);
890 #endif /* ENABLE_MALICIOUS */
894 mal_cb (struct RPSPeer *rps_peer)
896 uint32_t num_mal_peers;
898 #ifdef ENABLE_MALICIOUS
899 GNUNET_assert ( (1 >= portion) &&
901 num_mal_peers = round (portion * num_peers);
903 if (rps_peer->index >= num_mal_peers)
904 { /* It's useless to ask a malicious peer about a random sample -
906 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
907 seed_peers, rps_peer);
908 schedule_missing_requests (rps_peer);
910 #endif /* ENABLE_MALICIOUS */
914 /***********************************
916 ***********************************/
918 single_req_cb (struct RPSPeer *rps_peer)
920 schedule_missing_requests (rps_peer);
923 /***********************************
925 ***********************************/
927 delay_req_cb (struct RPSPeer *rps_peer)
929 schedule_missing_requests (rps_peer);
932 /***********************************
934 ***********************************/
936 seed_cb (struct RPSPeer *rps_peer)
938 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
939 seed_peers, rps_peer);
942 /***********************************
944 ***********************************/
946 seed_big_cb (struct RPSPeer *rps_peer)
948 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
951 /***********************************
953 ***********************************/
955 single_peer_seed_cb (struct RPSPeer *rps_peer)
960 /***********************************
962 ***********************************/
964 seed_req_cb (struct RPSPeer *rps_peer)
966 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
967 seed_peers, rps_peer);
968 schedule_missing_requests (rps_peer);
973 /***********************************
975 ***********************************/
977 req_cancel_cb (struct RPSPeer *rps_peer)
979 schedule_missing_requests (rps_peer);
980 GNUNET_SCHEDULER_add_delayed (
981 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
982 (cur_test_run.request_interval + 1)),
983 cancel_request_cb, rps_peer);
986 /***********************************
988 ***********************************/
991 * Callback to be called when RPS service is started or stopped at peers
994 * @param op the operation handle
995 * @param emsg NULL on success; otherwise an error description
999 struct GNUNET_TESTBED_Operation *op,
1003 struct OpListEntry *entry = cls;
1005 GNUNET_TESTBED_operation_done (entry->op);
1008 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1009 GNUNET_SCHEDULER_shutdown ();
1012 GNUNET_assert (0 != entry->delta);
1014 num_peers_online += entry->delta;
1016 if (0 > entry->delta)
1017 { /* Peer hopefully just went offline */
1018 if (GNUNET_YES != rps_peers[entry->index].online)
1020 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1021 "peer %s was expected to go offline but is still marked as online\n",
1022 GNUNET_i2s (rps_peers[entry->index].peer_id));
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "peer %s probably went offline as expected\n",
1029 GNUNET_i2s (rps_peers[entry->index].peer_id));
1031 rps_peers[entry->index].online = GNUNET_NO;
1034 else if (0 < entry->delta)
1035 { /* Peer hopefully just went online */
1036 if (GNUNET_NO != rps_peers[entry->index].online)
1038 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1039 "peer %s was expected to go online but is still marked as offline\n",
1040 GNUNET_i2s (rps_peers[entry->index].peer_id));
1045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1046 "peer %s probably went online as expected\n",
1047 GNUNET_i2s (rps_peers[entry->index].peer_id));
1048 if (NULL != cur_test_run.pre_test)
1050 cur_test_run.pre_test (&rps_peers[entry->index],
1051 rps_peers[entry->index].rps_handle);
1052 schedule_missing_requests (&rps_peers[entry->index]);
1055 rps_peers[entry->index].online = GNUNET_YES;
1058 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1059 GNUNET_free (entry);
1060 //if (num_peers_in_round[current_round] == peers_running)
1065 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1066 double prob_go_on_off)
1068 struct OpListEntry *entry;
1071 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 "%u. selected peer (%u: %s) is %s.\n",
1077 GNUNET_i2s (rps_peers[j].peer_id),
1078 (0 > delta) ? "online" : "offline");
1079 if (prob < prob_go_on_off * UINT32_MAX)
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 GNUNET_i2s (rps_peers[j].peer_id),
1084 (0 > delta) ? "offline" : "online");
1087 cancel_pending_req_rep (&rps_peers[j]);
1088 entry = make_oplist_entry ();
1089 entry->delta = delta;
1091 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1096 (0 > delta) ? 0 : 1);
1101 churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1105 double portion_online;
1106 unsigned int *permut;
1107 double prob_go_offline;
1108 double portion_go_online;
1109 double portion_go_offline;
1111 /* Compute the probability for an online peer to go offline
1113 portion_online = num_peers_online * 1.0 / num_peers;
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1115 "Portion online: %f\n",
1117 portion_go_online = ((1 - portion_online) * .5 * .66);
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Portion that should go online: %f\n",
1121 portion_go_offline = (portion_online + portion_go_online) - .75;
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1123 "Portion that probably goes offline: %f\n",
1124 portion_go_offline);
1125 prob_go_offline = portion_go_offline / (portion_online * .5);
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1127 "Probability of a selected online peer to go offline: %f\n",
1130 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1131 (unsigned int) num_peers);
1133 /* Go over 50% randomly chosen peers */
1134 for (i = 0; i < .5 * num_peers; i++)
1138 /* If online, shut down with certain probability */
1139 if (GNUNET_YES == rps_peers[j].online)
1141 manage_service_wrapper (i, j, -1, prob_go_offline);
1144 /* If offline, restart with certain probability */
1145 else if (GNUNET_NO == rps_peers[j].online)
1147 manage_service_wrapper (i, j, 1, 0.66);
1151 GNUNET_free (permut);
1153 churn_task = GNUNET_SCHEDULER_add_delayed (
1154 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1161 * Initialise given RPSPeer
1163 static void profiler_init_peer (struct RPSPeer *rps_peer)
1165 if (num_peers - 1 == rps_peer->index)
1166 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1171 * Callback to call on receipt of a reply
1173 * @param cls closure
1174 * @param n number of peers
1175 * @param recv_peers the received peers
1178 profiler_reply_handle (void *cls,
1180 const struct GNUNET_PeerIdentity *recv_peers)
1182 struct RPSPeer *rps_peer;
1183 struct RPSPeer *rcv_rps_peer;
1187 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1189 rps_peer = pending_rep->rps_peer;
1190 file_name = "/tmp/rps/received_ids";
1191 file_name_dh = "/tmp/rps/diehard_input";
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193 "[%s] got %" PRIu64 " peers:\n",
1194 GNUNET_i2s (rps_peer->peer_id),
1196 for (i = 0; i < n; i++)
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 GNUNET_i2s (&recv_peers[i]));
1204 GNUNET_i2s_full (&recv_peers[i]));
1205 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1206 tofile (file_name_dh,
1208 (uint32_t) rcv_rps_peer->index);
1210 default_reply_handle (cls, n, recv_peers);
1215 profiler_cb (struct RPSPeer *rps_peer)
1218 if (NULL == churn_task)
1220 churn_task = GNUNET_SCHEDULER_add_delayed (
1221 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1226 /* Only request peer ids at one peer.
1227 * (It's the before-last because last one is target of the focussed attack.)
1229 if (eval_peer == rps_peer)
1230 schedule_missing_requests (rps_peer);
1234 * Function called from #profiler_eval with a filename.
1236 * @param cls closure
1237 * @param filename complete filename (absolute path)
1238 * @return #GNUNET_OK to continue to iterate,
1239 * #GNUNET_NO to stop iteration with no error,
1240 * #GNUNET_SYSERR to abort iteration with error!
1243 file_name_cb (void *cls, const char *filename)
1245 if (NULL != strstr (filename, "sampler_el"))
1247 struct RPS_SamplerElement *s_elem;
1248 struct GNUNET_CRYPTO_AuthKey auth_key;
1249 const char *key_char;
1252 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1253 tofile (filename, "--------------------------\n");
1255 auth_key = string_to_auth_key (key_char);
1256 s_elem = RPS_sampler_elem_create ();
1257 RPS_sampler_elem_set (s_elem, auth_key);
1259 for (i = 0; i < num_peers; i++)
1261 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1268 * This is run after the test finished.
1270 * Compute all perfect samples.
1273 profiler_eval (void)
1275 /* Compute perfect sample for each sampler element */
1276 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1285 /***********************************************************************
1286 * /Definition of tests
1287 ***********************************************************************/
1291 * Actual "main" function for the testcase.
1293 * @param cls closure
1294 * @param h the run handle
1295 * @param n_peers number of peers in 'peers'
1296 * @param peers handle to peers run in the testbed
1297 * @param links_succeeded the number of overlay link connection attempts that
1299 * @param links_failed the number of overlay link connection attempts that
1304 struct GNUNET_TESTBED_RunHandle *h,
1305 unsigned int n_peers,
1306 struct GNUNET_TESTBED_Peer **peers,
1307 unsigned int links_succeeded,
1308 unsigned int links_failed)
1311 struct OpListEntry *entry;
1312 uint32_t num_mal_peers;
1314 testbed_peers = peers;
1315 num_peers_online = 0;
1316 for (i = 0; i < num_peers; i++)
1318 entry = make_oplist_entry ();
1320 rps_peers[i].index = i;
1321 if (NULL != cur_test_run.init_peer)
1322 cur_test_run.init_peer (&rps_peers[i]);
1323 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1324 GNUNET_TESTBED_PIT_IDENTITY,
1329 num_mal_peers = round (portion * num_peers);
1330 GNUNET_assert (num_peers == n_peers);
1331 for (i = 0; i < n_peers; i++)
1333 rps_peers[i].index = i;
1334 if ( (rps_peers[i].num_recv_ids < rps_peers[i].num_ids_to_request) ||
1335 (i < num_mal_peers) )
1338 GNUNET_TESTBED_service_connect (&rps_peers[i],
1341 &rps_connect_complete_cb,
1343 &rps_connect_adapter,
1344 &rps_disconnect_adapter,
1349 if (NULL != churn_task)
1350 GNUNET_SCHEDULER_cancel (churn_task);
1351 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1356 * Entry point for the testcase, sets up the testbed.
1358 * @param argc unused
1359 * @param argv unused
1360 * @return 0 on success
1363 main (int argc, char *argv[])
1368 cur_test_run.name = "test-rps-default";
1369 cur_test_run.init_peer = default_init_peer;
1370 cur_test_run.pre_test = NULL;
1371 cur_test_run.reply_handle = default_reply_handle;
1372 cur_test_run.eval_cb = default_eval_cb;
1374 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1376 if (strstr (argv[0], "malicious") != NULL)
1378 cur_test_run.pre_test = mal_pre;
1379 cur_test_run.main_test = mal_cb;
1380 cur_test_run.init_peer = mal_init_peer;
1382 if (strstr (argv[0], "_1") != NULL)
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1385 cur_test_run.name = "test-rps-malicious_1";
1388 else if (strstr (argv[0], "_2") != NULL)
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1391 cur_test_run.name = "test-rps-malicious_2";
1394 else if (strstr (argv[0], "_3") != NULL)
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1397 cur_test_run.name = "test-rps-malicious_3";
1402 else if (strstr (argv[0], "_single_req") != NULL)
1404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1405 cur_test_run.name = "test-rps-single-req";
1406 cur_test_run.main_test = single_req_cb;
1409 else if (strstr (argv[0], "_delayed_reqs") != NULL)
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1412 cur_test_run.name = "test-rps-delayed-reqs";
1413 cur_test_run.main_test = delay_req_cb;
1416 else if (strstr (argv[0], "_seed_big") != NULL)
1418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
1419 cur_test_run.name = "test-rps-seed-big";
1420 cur_test_run.main_test = seed_big_cb;
1423 else if (strstr (argv[0], "_single_peer_seed") != NULL)
1425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1426 cur_test_run.name = "test-rps-single-peer-seed";
1427 cur_test_run.main_test = single_peer_seed_cb;
1430 else if (strstr (argv[0], "_seed_request") != NULL)
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1433 cur_test_run.name = "test-rps-seed-request";
1434 cur_test_run.main_test = seed_req_cb;
1437 else if (strstr (argv[0], "_seed") != NULL)
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1440 cur_test_run.name = "test-rps-seed";
1441 cur_test_run.main_test = seed_cb;
1442 cur_test_run.eval_cb = no_eval;
1445 else if (strstr (argv[0], "_req_cancel") != NULL)
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1448 cur_test_run.name = "test-rps-req-cancel";
1450 cur_test_run.main_test = req_cancel_cb;
1451 cur_test_run.eval_cb = no_eval;
1452 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1455 else if (strstr (argv[0], "profiler") != NULL)
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1458 cur_test_run.name = "test-rps-profiler";
1461 cur_test_run.init_peer = profiler_init_peer;
1462 cur_test_run.pre_test = mal_pre;
1463 cur_test_run.main_test = profiler_cb;
1464 cur_test_run.reply_handle = profiler_reply_handle;
1465 cur_test_run.eval_cb = profiler_eval;
1466 cur_test_run.request_interval = 2;
1467 cur_test_run.num_requests = 5;
1468 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1470 /* 'Clean' directory */
1471 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1472 GNUNET_DISK_directory_create ("/tmp/rps/");
1475 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1476 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1477 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1478 if ( (2 == mal_type) ||
1480 target_peer = &rps_peer_ids[num_peers - 2];
1481 if (profiler_eval == cur_test_run.eval_cb)
1482 eval_peer = &rps_peers[num_peers - 1];
1485 (void) GNUNET_TESTBED_test_run (cur_test_run.name,
1491 ret_value = cur_test_run.eval_cb();
1492 GNUNET_free (rps_peers );
1493 GNUNET_free (rps_peer_ids);
1494 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1498 /* end of test_rps_multipeer.c */