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 GNUNET_TESTBED_operation_done (rps_peers[i].op);
506 GNUNET_SCHEDULER_shutdown ();
514 seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
517 struct RPSPeer *peer = (struct RPSPeer *) cls;
520 // TODO if malicious don't seed mal peers
521 amount = round (.5 * num_peers);
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
524 for (i = 0 ; i < amount ; i++)
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
527 GNUNET_i2s (&rps_peer_ids[i]));
529 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
534 * Get the id of peer i.
537 info_cb (void *cb_cls,
538 struct GNUNET_TESTBED_Operation *op,
539 const struct GNUNET_TESTBED_PeerInformation *pinfo,
542 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
544 if (NULL == pinfo || NULL != emsg)
546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 GNUNET_i2s (pinfo->result.id));
555 rps_peer_ids[entry->index] = *(pinfo->result.id);
556 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
557 rps_peers[entry->index].rec_ids = NULL;
558 rps_peers[entry->index].num_rec_ids = 0;
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 if (NULL != cur_test_run.init_peer)
570 cur_test_run.init_peer (&rps_peers[entry->index]);
572 GNUNET_TESTBED_operation_done (entry->op);
573 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
579 * Callback to be called when RPS service connect operation is completed
581 * @param cls the callback closure from functions generating an operation
582 * @param op the operation that has been finished
583 * @param ca_result the RPS service handle returned from rps_connect_adapter
584 * @param emsg error message in case the operation has failed; will be NULL if
585 * operation has executed successfully.
588 rps_connect_complete_cb (void *cls,
589 struct GNUNET_TESTBED_Operation *op,
593 struct RPSPeer *rps_peer = cls;
594 struct GNUNET_RPS_Handle *rps = ca_result;
596 rps_peer->rps_handle = rps;
597 rps_peer->online = GNUNET_YES;
600 GNUNET_assert (op == rps_peer->op);
603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
604 "Failed to connect to RPS service: %s\n",
607 GNUNET_SCHEDULER_shutdown ();
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
613 cur_test_run.main_test (rps_peer);
618 * Adapter function called to establish a connection to
622 * @param cfg configuration of the peer to connect to; will be available until
623 * GNUNET_TESTBED_operation_done() is called on the operation returned
624 * from GNUNET_TESTBED_service_connect()
625 * @return service handle to return in 'op_result', NULL on error
628 rps_connect_adapter (void *cls,
629 const struct GNUNET_CONFIGURATION_Handle *cfg)
631 struct GNUNET_RPS_Handle *h;
633 h = GNUNET_RPS_connect (cfg);
635 if (NULL != cur_test_run.pre_test)
636 cur_test_run.pre_test (cls, h);
643 * Adapter function called to destroy connection to
647 * @param op_result service handle returned from the connect adapter
650 rps_disconnect_adapter (void *cls,
653 struct RPSPeer *peer = cls;
654 struct GNUNET_RPS_Handle *h = op_result;
655 GNUNET_assert (NULL != peer);
656 GNUNET_RPS_disconnect (h);
657 peer->rps_handle = NULL;
661 /***********************************************************************
662 * Definition of tests
663 ***********************************************************************/
665 // TODO check whether tests can be stopped earlier
667 default_eval_cb (void)
679 * Initialise given RPSPeer
681 static void default_init_peer (struct RPSPeer *rps_peer)
683 rps_peer->num_ids_to_request = 1;
687 * Callback to call on receipt of a reply
690 * @param n number of peers
691 * @param recv_peers the received peers
694 default_reply_handle (void *cls,
696 const struct GNUNET_PeerIdentity *recv_peers)
698 struct RPSPeer *rps_peer;
699 struct PendingReply *pending_rep = (struct PendingReply *) cls;
702 rps_peer = pending_rep->rps_peer;
703 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
704 rps_peer->pending_rep_tail,
706 rps_peer->num_pending_reps--;
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "[%s] got %" PRIu64 " peers:\n",
709 GNUNET_i2s (rps_peer->peer_id),
712 for (i = 0; i < n; i++)
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
717 GNUNET_i2s (&recv_peers[i]));
719 rps_peer->num_recv_ids++;
722 if (0 == evaluate ())
724 GNUNET_SCHEDULER_cancel (shutdown_task);
725 shutdown_task = GNUNET_SCHEDULER_add_now (&shutdown_op, NULL);
730 * Request random peers.
733 request_peers (void *cls,
734 const struct GNUNET_SCHEDULER_TaskContext *tc)
736 struct RPSPeer *rps_peer;
737 struct PendingRequest *pending_req = (struct PendingRequest *) cls;
738 struct PendingReply *pending_rep;
740 if (GNUNET_YES == in_shutdown)
742 rps_peer = pending_req->rps_peer;
743 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
744 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
745 rps_peer->pending_req_tail,
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "Requesting one peer\n");
749 pending_rep = GNUNET_new (struct PendingReply);
750 pending_rep->rps_peer = rps_peer;
751 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
753 cur_test_run.reply_handle,
755 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
756 rps_peer->pending_rep_tail,
758 rps_peer->num_pending_reps++;
759 rps_peer->num_pending_reqs--;
763 cancel_pending_req (struct PendingRequest *pending_req)
765 struct RPSPeer *rps_peer;
767 rps_peer = pending_req->rps_peer;
768 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
769 rps_peer->pending_req_tail,
771 rps_peer->num_pending_reqs--;
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 "Cancelling pending request\n");
774 GNUNET_SCHEDULER_cancel (pending_req->request_task);
775 GNUNET_free (pending_req);
779 cancel_request (struct PendingReply *pending_rep)
781 struct RPSPeer *rps_peer;
783 rps_peer = pending_rep->rps_peer;
784 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
785 rps_peer->pending_rep_tail,
787 rps_peer->num_pending_reps--;
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Cancelling request\n");
790 GNUNET_RPS_request_cancel (pending_rep->req_handle);
791 GNUNET_free (pending_rep);
798 cancel_request_cb (void *cls,
799 const struct GNUNET_SCHEDULER_TaskContext *tc)
801 struct PendingReply *pending_rep;
802 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
804 if (GNUNET_YES == in_shutdown)
806 pending_rep = rps_peer->pending_rep_head;
807 GNUNET_assert (1 <= rps_peer->num_pending_reps);
808 cancel_request (pending_rep);
813 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
814 * issued, nor replied
817 schedule_missing_requests (struct RPSPeer *rps_peer)
820 struct PendingRequest *pending_req;
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
823 "Scheduling %u - %u missing requests\n",
824 rps_peer->num_ids_to_request,
825 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
826 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
827 rps_peer->num_ids_to_request);
828 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
829 i < rps_peer->num_ids_to_request; i++)
831 pending_req = GNUNET_new (struct PendingRequest);
832 pending_req->rps_peer = rps_peer;
833 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
834 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
835 cur_test_run.request_interval * i),
838 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
839 rps_peer->pending_req_tail,
841 rps_peer->num_pending_reqs++;
846 cancel_pending_req_rep (struct RPSPeer *rps_peer)
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Cancelling all (pending) requests.\n");
850 while (NULL != rps_peer->pending_req_head)
851 cancel_pending_req (rps_peer->pending_req_head);
852 GNUNET_assert (0 == rps_peer->num_pending_reqs);
853 while (NULL != rps_peer->pending_rep_head)
854 cancel_request (rps_peer->pending_rep_head);
855 GNUNET_assert (0 == rps_peer->num_pending_reps);
858 /***********************************
860 ***********************************/
863 * Initialise only non-mal RPSPeers
865 static void mal_init_peer (struct RPSPeer *rps_peer)
867 if (rps_peer->index >= round (portion * num_peers))
868 rps_peer->num_ids_to_request = 1;
872 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
874 #ifdef ENABLE_MALICIOUS
875 uint32_t num_mal_peers;
876 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
878 GNUNET_assert ( (1 >= portion) &&
880 num_mal_peers = round (portion * num_peers);
882 if (rps_peer->index < num_mal_peers)
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
887 GNUNET_i2s (rps_peer->peer_id),
890 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
891 rps_peer_ids, target_peer);
893 #endif /* ENABLE_MALICIOUS */
897 mal_cb (struct RPSPeer *rps_peer)
899 uint32_t num_mal_peers;
901 #ifdef ENABLE_MALICIOUS
902 GNUNET_assert ( (1 >= portion) &&
904 num_mal_peers = round (portion * num_peers);
906 if (rps_peer->index >= num_mal_peers)
907 { /* It's useless to ask a malicious peer about a random sample -
909 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
910 seed_peers, rps_peer);
911 schedule_missing_requests (rps_peer);
913 #endif /* ENABLE_MALICIOUS */
917 /***********************************
919 ***********************************/
921 single_req_cb (struct RPSPeer *rps_peer)
923 schedule_missing_requests (rps_peer);
926 /***********************************
928 ***********************************/
930 delay_req_cb (struct RPSPeer *rps_peer)
932 schedule_missing_requests (rps_peer);
935 /***********************************
937 ***********************************/
939 seed_cb (struct RPSPeer *rps_peer)
941 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
942 seed_peers, rps_peer);
945 /***********************************
947 ***********************************/
949 seed_big_cb (struct RPSPeer *rps_peer)
951 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
954 /***********************************
956 ***********************************/
958 single_peer_seed_cb (struct RPSPeer *rps_peer)
963 /***********************************
965 ***********************************/
967 seed_req_cb (struct RPSPeer *rps_peer)
969 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
970 seed_peers, rps_peer);
971 schedule_missing_requests (rps_peer);
976 /***********************************
978 ***********************************/
980 req_cancel_cb (struct RPSPeer *rps_peer)
982 schedule_missing_requests (rps_peer);
983 GNUNET_SCHEDULER_add_delayed (
984 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
985 (cur_test_run.request_interval + 1)),
986 cancel_request_cb, rps_peer);
989 /***********************************
991 ***********************************/
994 * Callback to be called when RPS service is started or stopped at peers
997 * @param op the operation handle
998 * @param emsg NULL on success; otherwise an error description
1001 churn_cb (void *cls,
1002 struct GNUNET_TESTBED_Operation *op,
1006 struct OpListEntry *entry = cls;
1008 GNUNET_TESTBED_operation_done (entry->op);
1011 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1012 GNUNET_SCHEDULER_shutdown ();
1015 GNUNET_assert (0 != entry->delta);
1017 num_peers_online += entry->delta;
1019 if (0 > entry->delta)
1020 { /* Peer hopefully just went offline */
1021 if (GNUNET_YES != rps_peers[entry->index].online)
1023 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1024 "peer %s was expected to go offline but is still marked as online\n",
1025 GNUNET_i2s (rps_peers[entry->index].peer_id));
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "peer %s probably went offline as expected\n",
1032 GNUNET_i2s (rps_peers[entry->index].peer_id));
1034 rps_peers[entry->index].online = GNUNET_NO;
1037 else if (0 < entry->delta)
1038 { /* Peer hopefully just went online */
1039 if (GNUNET_NO != rps_peers[entry->index].online)
1041 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1042 "peer %s was expected to go online but is still marked as offline\n",
1043 GNUNET_i2s (rps_peers[entry->index].peer_id));
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1049 "peer %s probably went online as expected\n",
1050 GNUNET_i2s (rps_peers[entry->index].peer_id));
1051 if (NULL != cur_test_run.pre_test)
1053 cur_test_run.pre_test (&rps_peers[entry->index],
1054 rps_peers[entry->index].rps_handle);
1055 schedule_missing_requests (&rps_peers[entry->index]);
1058 rps_peers[entry->index].online = GNUNET_YES;
1061 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1062 GNUNET_free (entry);
1063 //if (num_peers_in_round[current_round] == peers_running)
1068 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1069 double prob_go_on_off)
1071 struct OpListEntry *entry;
1074 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1077 "%u. selected peer (%u: %s) is %s.\n",
1080 GNUNET_i2s (rps_peers[j].peer_id),
1081 (0 > delta) ? "online" : "offline");
1082 if (prob < prob_go_on_off * UINT32_MAX)
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 GNUNET_i2s (rps_peers[j].peer_id),
1087 (0 > delta) ? "offline" : "online");
1090 cancel_pending_req_rep (&rps_peers[j]);
1091 entry = make_oplist_entry ();
1092 entry->delta = delta;
1094 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1099 (0 > delta) ? 0 : 1);
1104 churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1108 double portion_online;
1109 unsigned int *permut;
1110 double prob_go_offline;
1111 double portion_go_online;
1112 double portion_go_offline;
1114 /* Compute the probability for an online peer to go offline
1116 portion_online = num_peers_online * 1.0 / num_peers;
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118 "Portion online: %f\n",
1120 portion_go_online = ((1 - portion_online) * .5 * .66);
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "Portion that should go online: %f\n",
1124 portion_go_offline = (portion_online + portion_go_online) - .75;
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126 "Portion that probably goes offline: %f\n",
1127 portion_go_offline);
1128 prob_go_offline = portion_go_offline / (portion_online * .5);
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 "Probability of a selected online peer to go offline: %f\n",
1133 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1134 (unsigned int) num_peers);
1136 /* Go over 50% randomly chosen peers */
1137 for (i = 0; i < .5 * num_peers; i++)
1141 /* If online, shut down with certain probability */
1142 if (GNUNET_YES == rps_peers[j].online)
1144 manage_service_wrapper (i, j, -1, prob_go_offline);
1147 /* If offline, restart with certain probability */
1148 else if (GNUNET_NO == rps_peers[j].online)
1150 manage_service_wrapper (i, j, 1, 0.66);
1154 GNUNET_free (permut);
1156 churn_task = GNUNET_SCHEDULER_add_delayed (
1157 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1164 * Initialise given RPSPeer
1166 static void profiler_init_peer (struct RPSPeer *rps_peer)
1168 if (num_peers - 1 == rps_peer->index)
1169 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1174 * Callback to call on receipt of a reply
1176 * @param cls closure
1177 * @param n number of peers
1178 * @param recv_peers the received peers
1181 profiler_reply_handle (void *cls,
1183 const struct GNUNET_PeerIdentity *recv_peers)
1185 struct RPSPeer *rps_peer;
1186 struct RPSPeer *rcv_rps_peer;
1190 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1192 rps_peer = pending_rep->rps_peer;
1193 file_name = "/tmp/rps/received_ids";
1194 file_name_dh = "/tmp/rps/diehard_input";
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "[%s] got %" PRIu64 " peers:\n",
1197 GNUNET_i2s (rps_peer->peer_id),
1199 for (i = 0; i < n; i++)
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 GNUNET_i2s (&recv_peers[i]));
1207 GNUNET_i2s_full (&recv_peers[i]));
1208 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1209 tofile (file_name_dh,
1211 (uint32_t) rcv_rps_peer->index);
1213 default_reply_handle (cls, n, recv_peers);
1218 profiler_cb (struct RPSPeer *rps_peer)
1221 if (NULL == churn_task)
1223 churn_task = GNUNET_SCHEDULER_add_delayed (
1224 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1229 /* Only request peer ids at one peer.
1230 * (It's the before-last because last one is target of the focussed attack.)
1232 if (eval_peer == rps_peer)
1233 schedule_missing_requests (rps_peer);
1237 * Function called from #profiler_eval with a filename.
1239 * @param cls closure
1240 * @param filename complete filename (absolute path)
1241 * @return #GNUNET_OK to continue to iterate,
1242 * #GNUNET_NO to stop iteration with no error,
1243 * #GNUNET_SYSERR to abort iteration with error!
1246 file_name_cb (void *cls, const char *filename)
1248 if (NULL != strstr (filename, "sampler_el"))
1250 struct RPS_SamplerElement *s_elem;
1251 struct GNUNET_CRYPTO_AuthKey auth_key;
1252 const char *key_char;
1255 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1256 tofile (filename, "--------------------------\n");
1258 auth_key = string_to_auth_key (key_char);
1259 s_elem = RPS_sampler_elem_create ();
1260 RPS_sampler_elem_set (s_elem, auth_key);
1262 for (i = 0; i < num_peers; i++)
1264 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1271 * This is run after the test finished.
1273 * Compute all perfect samples.
1276 profiler_eval (void)
1278 /* Compute perfect sample for each sampler element */
1279 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1288 /***********************************************************************
1289 * /Definition of tests
1290 ***********************************************************************/
1294 * Actual "main" function for the testcase.
1296 * @param cls closure
1297 * @param h the run handle
1298 * @param n_peers number of peers in 'peers'
1299 * @param peers handle to peers run in the testbed
1300 * @param links_succeeded the number of overlay link connection attempts that
1302 * @param links_failed the number of overlay link connection attempts that
1307 struct GNUNET_TESTBED_RunHandle *h,
1308 unsigned int n_peers,
1309 struct GNUNET_TESTBED_Peer **peers,
1310 unsigned int links_succeeded,
1311 unsigned int links_failed)
1314 struct OpListEntry *entry;
1316 testbed_peers = peers;
1317 num_peers_online = 0;
1318 for (i = 0 ; i < num_peers ; i++)
1320 entry = make_oplist_entry ();
1322 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1323 GNUNET_TESTBED_PIT_IDENTITY,
1328 GNUNET_assert (num_peers == n_peers);
1329 for (i = 0 ; i < n_peers ; i++)
1331 rps_peers[i].index = i;
1333 GNUNET_TESTBED_service_connect (&rps_peers[i],
1336 &rps_connect_complete_cb,
1338 &rps_connect_adapter,
1339 &rps_disconnect_adapter,
1343 if (NULL != churn_task)
1344 GNUNET_SCHEDULER_cancel (churn_task);
1345 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1350 * Entry point for the testcase, sets up the testbed.
1352 * @param argc unused
1353 * @param argv unused
1354 * @return 0 on success
1357 main (int argc, char *argv[])
1362 cur_test_run.name = "test-rps-default";
1363 cur_test_run.init_peer = default_init_peer;
1364 cur_test_run.pre_test = NULL;
1365 cur_test_run.reply_handle = default_reply_handle;
1366 cur_test_run.eval_cb = default_eval_cb;
1368 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1370 if (strstr (argv[0], "malicious") != NULL)
1372 cur_test_run.pre_test = mal_pre;
1373 cur_test_run.main_test = mal_cb;
1374 cur_test_run.init_peer = mal_init_peer;
1376 if (strstr (argv[0], "_1") != NULL)
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1379 cur_test_run.name = "test-rps-malicious_1";
1382 else if (strstr (argv[0], "_2") != NULL)
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1385 cur_test_run.name = "test-rps-malicious_2";
1388 else if (strstr (argv[0], "_3") != NULL)
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1391 cur_test_run.name = "test-rps-malicious_3";
1396 else if (strstr (argv[0], "_single_req") != NULL)
1398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1399 cur_test_run.name = "test-rps-single-req";
1400 cur_test_run.main_test = single_req_cb;
1403 else if (strstr (argv[0], "_delayed_reqs") != NULL)
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1406 cur_test_run.name = "test-rps-delayed-reqs";
1407 cur_test_run.main_test = delay_req_cb;
1410 else if (strstr (argv[0], "_seed_big") != NULL)
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
1413 cur_test_run.name = "test-rps-seed-big";
1414 cur_test_run.main_test = seed_big_cb;
1417 else if (strstr (argv[0], "_single_peer_seed") != NULL)
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1420 cur_test_run.name = "test-rps-single-peer-seed";
1421 cur_test_run.main_test = single_peer_seed_cb;
1424 else if (strstr (argv[0], "_seed_request") != NULL)
1426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1427 cur_test_run.name = "test-rps-seed-request";
1428 cur_test_run.main_test = seed_req_cb;
1431 else if (strstr (argv[0], "_seed") != NULL)
1433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1434 cur_test_run.name = "test-rps-seed";
1435 cur_test_run.main_test = seed_cb;
1436 cur_test_run.eval_cb = no_eval;
1439 else if (strstr (argv[0], "_req_cancel") != NULL)
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1442 cur_test_run.name = "test-rps-req-cancel";
1444 cur_test_run.main_test = req_cancel_cb;
1445 cur_test_run.eval_cb = no_eval;
1446 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1449 else if (strstr (argv[0], "profiler") != NULL)
1451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1452 cur_test_run.name = "test-rps-profiler";
1455 cur_test_run.init_peer = profiler_init_peer;
1456 cur_test_run.pre_test = mal_pre;
1457 cur_test_run.main_test = profiler_cb;
1458 cur_test_run.reply_handle = profiler_reply_handle;
1459 cur_test_run.eval_cb = profiler_eval;
1460 cur_test_run.request_interval = 2;
1461 cur_test_run.num_requests = 5;
1462 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1464 /* 'Clean' directory */
1465 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1466 GNUNET_DISK_directory_create ("/tmp/rps/");
1469 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1470 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1471 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1472 if ( (2 == mal_type) ||
1474 target_peer = &rps_peer_ids[num_peers - 2];
1475 if (profiler_eval == cur_test_run.eval_cb)
1476 eval_peer = &rps_peers[num_peers - 1];
1479 (void) GNUNET_TESTBED_test_run (cur_test_run.name,
1485 ret_value = cur_test_run.eval_cb();
1486 GNUNET_free (rps_peers );
1487 GNUNET_free (rps_peer_ids);
1488 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1492 /* end of test_rps_multipeer.c */