2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file rps/test_rps_multipeer.c
22 * @brief Testcase for the random peer sampling service. Starts
23 * a peergroup with a given number of peers, then waits to
24 * receive size pushes/pulls from each peer. Expects to wait
25 * for one message from each peer.
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
31 #include "gnunet_rps_service.h"
32 #include "rps-test_util.h"
33 #include "gnunet-service-rps_sampler_elem.h"
39 * How many peers do we start?
44 * How long do we run the test?
46 //#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 static struct GNUNET_TIME_Relative timeout;
51 * Portion of malicious peers
53 static double portion = .1;
56 * Type of malicious peer to test
58 static unsigned int mal_type = 0;
61 * Handles to all of the running peers
63 static struct GNUNET_TESTBED_Peer **testbed_peers;
74 struct OpListEntry *next;
79 struct OpListEntry *prev;
82 * The testbed operation
84 struct GNUNET_TESTBED_Operation *op;
87 * Depending on whether we start or stop NSE service at the peer set this to 1
93 * Index of the regarding peer
101 static struct OpListEntry *oplist_head;
106 static struct OpListEntry *oplist_tail;
110 * A pending reply: A request was sent and the reply is pending.
117 struct PendingReply *next;
118 struct PendingReply *prev;
121 * Handle to the request we are waiting for
123 struct GNUNET_RPS_Request_Handle *req_handle;
126 * The peer that requested
128 struct RPSPeer *rps_peer;
133 * A pending request: A request was not made yet but is scheduled for later.
135 struct PendingRequest
140 struct PendingRequest *next;
141 struct PendingRequest *prev;
144 * Handle to the request we are waiting for
146 struct GNUNET_SCHEDULER_Task *request_task;
149 * The peer that requested
151 struct RPSPeer *rps_peer;
156 * Information we track for each peer.
166 * Handle for RPS connect operation.
168 struct GNUNET_TESTBED_Operation *op;
171 * Handle to RPS service.
173 struct GNUNET_RPS_Handle *rps_handle;
178 struct GNUNET_PeerIdentity *peer_id;
181 * A request handle to check for an request
183 //struct GNUNET_RPS_Request_Handle *req_handle;
186 * Peer on- or offline?
191 * Number of Peer IDs to request
193 unsigned int num_ids_to_request;
196 * Pending requests DLL
198 struct PendingRequest *pending_req_head;
199 struct PendingRequest *pending_req_tail;
202 * Number of pending requests
204 unsigned int num_pending_reqs;
207 * Pending replies DLL
209 struct PendingReply *pending_rep_head;
210 struct PendingReply *pending_rep_tail;
213 * Number of pending replies
215 unsigned int num_pending_reps;
220 struct GNUNET_PeerIdentity *rec_ids;
223 * Number of received PeerIDs
225 unsigned int num_rec_ids;
230 * Information for all the peers.
232 static struct RPSPeer *rps_peers;
235 * Peermap to get the index of a given peer ID quick.
237 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
242 static struct GNUNET_PeerIdentity *rps_peer_ids;
245 * ID of the targeted peer.
247 static struct GNUNET_PeerIdentity *target_peer;
250 * ID of the peer that requests for the evaluation.
252 static struct RPSPeer *eval_peer;
255 * Number of online peers.
257 static unsigned int num_peers_online;
260 * Return value from 'main'.
266 * Identifier for the churn task that runs periodically
268 static struct GNUNET_SCHEDULER_Task *churn_task;
271 * Identifier for the churn task that runs periodically
273 static struct GNUNET_SCHEDULER_Task *shutdown_task;
277 * Called to initialise the given RPSPeer
279 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
282 * Called directly after connecting to the service
284 typedef void (*PreTest) (void *cls, struct GNUNET_RPS_Handle *h);
287 * Called from within #rps_connect_complete_cb ()
288 * Executes functions to test the api/service
290 typedef void (*MainTest) (struct RPSPeer *rps_peer);
293 * Callback called once the requested random peers are available
295 typedef void (*ReplyHandle) (void *cls,
297 const struct GNUNET_PeerIdentity *recv_peers);
300 * Called directly before disconnecting from the service
302 typedef void (*PostTest) (void *cls, struct GNUNET_RPS_Handle *h);
305 * Function called after disconnect to evaluate test success
307 typedef int (*EvaluationCallback) (void);
311 * Structure to define a single test
321 * Called to initialise peer
326 * Called directly after connecting to the service
331 * Function to execute the functions to be tested
336 * Callback called once the requested peers are available
338 ReplyHandle reply_handle;
341 * Called directly before disconnecting from the service
346 * Function to evaluate the test results
348 EvaluationCallback eval_cb;
353 uint32_t request_interval;
356 * Number of Requests to make.
358 uint32_t num_requests;
362 * Are we shutting down?
364 static int in_shutdown;
367 * Append arguments to file
370 tofile_ (const char *file_name, char *line)
372 struct GNUNET_DISK_FileHandle *f;
373 /* char output_buffer[512]; */
378 if (NULL == (f = GNUNET_DISK_file_open (file_name,
379 GNUNET_DISK_OPEN_APPEND |
380 GNUNET_DISK_OPEN_WRITE |
381 GNUNET_DISK_OPEN_CREATE,
382 GNUNET_DISK_PERM_USER_READ |
383 GNUNET_DISK_PERM_USER_WRITE |
384 GNUNET_DISK_PERM_GROUP_READ |
385 GNUNET_DISK_PERM_OTHER_READ)))
387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
388 "Not able to open file %s\n",
392 /* size = GNUNET_snprintf (output_buffer,
393 sizeof (output_buffer),
395 GNUNET_TIME_absolute_get ().abs_value_us,
399 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
400 "Failed to write string to buffer (size: %i)\n",
405 size = strlen (line) * sizeof (char);
407 size2 = GNUNET_DISK_file_write (f, line, size);
410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
411 "Unable to write to file! (Size: %u, size2: %u)\n",
417 if (GNUNET_YES != GNUNET_DISK_file_close (f))
418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
419 "Unable to close file\n");
423 * This function is used to facilitate writing important information to disk
425 #define tofile(file_name, ...) do {\
428 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
431 "Failed to create tmp_buf\n");\
433 tofile_(file_name,tmp_buf);\
438 * Write the ids and their according index in the given array to a file
442 ids_to_file (char *file_name,
443 struct GNUNET_PeerIdentity *peer_ids,
444 unsigned int num_peer_ids)
448 for (i=0 ; i < num_peer_ids ; i++)
453 GNUNET_i2s_full (&peer_ids[i]));
458 * Test the success of a single test
468 for (i = 0; i < num_peers; i++)
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
473 GNUNET_i2s (rps_peers[i].peer_id),
474 rps_peers[i].num_rec_ids,
475 rps_peers[i].num_ids_to_request,
476 (rps_peers[i].num_ids_to_request == rps_peers[i].num_rec_ids));
477 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_rec_ids);
479 return tmp_ok? 0 : 1;
484 * Creates an oplist entry and adds it to the oplist DLL
486 static struct OpListEntry *
489 struct OpListEntry *entry;
491 entry = GNUNET_new (struct OpListEntry);
492 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
498 * Task run on timeout to shut everything down.
501 shutdown_op (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
505 in_shutdown = GNUNET_YES;
506 if (NULL != churn_task)
507 GNUNET_SCHEDULER_cancel (churn_task);
509 for (i = 0 ; i < num_peers ; i++)
510 GNUNET_TESTBED_operation_done (rps_peers[i].op);
511 GNUNET_SCHEDULER_shutdown ();
519 seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
522 struct RPSPeer *peer = (struct RPSPeer *) cls;
525 // TODO if malicious don't seed mal peers
526 amount = round (.5 * num_peers);
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
529 for (i = 0 ; i < amount ; i++)
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
532 GNUNET_i2s (&rps_peer_ids[i]));
534 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
539 * Get the id of peer i.
542 info_cb (void *cb_cls,
543 struct GNUNET_TESTBED_Operation *op,
544 const struct GNUNET_TESTBED_PeerInformation *pinfo,
547 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
549 if (NULL == pinfo || NULL != emsg)
551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558 GNUNET_i2s (pinfo->result.id));
560 rps_peer_ids[entry->index] = *(pinfo->result.id);
561 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
562 rps_peers[entry->index].rec_ids = NULL;
563 rps_peers[entry->index].num_rec_ids = 0;
565 GNUNET_CONTAINER_multipeermap_put (peer_map,
566 &rps_peer_ids[entry->index],
567 &rps_peers[entry->index],
568 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
569 tofile ("/tmp/rps/peer_ids",
572 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
574 if (NULL != cur_test_run.init_peer)
575 cur_test_run.init_peer (&rps_peers[entry->index]);
577 GNUNET_TESTBED_operation_done (entry->op);
578 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
584 * Callback to be called when RPS service connect operation is completed
586 * @param cls the callback closure from functions generating an operation
587 * @param op the operation that has been finished
588 * @param ca_result the RPS service handle returned from rps_connect_adapter
589 * @param emsg error message in case the operation has failed; will be NULL if
590 * operation has executed successfully.
593 rps_connect_complete_cb (void *cls,
594 struct GNUNET_TESTBED_Operation *op,
598 struct RPSPeer *rps_peer = cls;
599 struct GNUNET_RPS_Handle *rps = ca_result;
601 rps_peer->rps_handle = rps;
602 rps_peer->online = GNUNET_YES;
605 GNUNET_assert (op == rps_peer->op);
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 "Failed to connect to RPS service: %s\n",
612 GNUNET_SCHEDULER_shutdown ();
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
618 cur_test_run.main_test (rps_peer);
623 * Adapter function called to establish a connection to
627 * @param cfg configuration of the peer to connect to; will be available until
628 * GNUNET_TESTBED_operation_done() is called on the operation returned
629 * from GNUNET_TESTBED_service_connect()
630 * @return service handle to return in 'op_result', NULL on error
633 rps_connect_adapter (void *cls,
634 const struct GNUNET_CONFIGURATION_Handle *cfg)
636 struct GNUNET_RPS_Handle *h;
638 h = GNUNET_RPS_connect (cfg);
640 if (NULL != cur_test_run.pre_test)
641 cur_test_run.pre_test (cls, h);
648 * Adapter function called to destroy connection to
652 * @param op_result service handle returned from the connect adapter
655 rps_disconnect_adapter (void *cls,
658 struct GNUNET_RPS_Handle *h = op_result;
659 GNUNET_RPS_disconnect (h);
663 /***********************************************************************
664 * Definition of tests
665 ***********************************************************************/
667 // TODO check whether tests can be stopped earlier
669 default_eval_cb (void)
681 * Initialise given RPSPeer
683 static void default_init_peer (struct RPSPeer *rps_peer)
685 rps_peer->num_ids_to_request = 1;
689 * Callback to call on receipt of a reply
692 * @param n number of peers
693 * @param recv_peers the received peers
696 default_reply_handle (void *cls,
698 const struct GNUNET_PeerIdentity *recv_peers)
700 struct RPSPeer *rps_peer;
701 struct PendingReply *pending_rep = (struct PendingReply *) cls;
704 rps_peer = pending_rep->rps_peer;
705 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
706 rps_peer->pending_rep_tail,
708 rps_peer->num_pending_reps--;
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
710 "[%s] got %" PRIu64 " peers:\n",
711 GNUNET_i2s (rps_peer->peer_id),
714 for (i = 0; i < n; i++)
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 GNUNET_i2s (&recv_peers[i]));
721 GNUNET_array_append (rps_peer->rec_ids, rps_peer->num_rec_ids, recv_peers[i]);
724 if (0 == evaluate ())
726 GNUNET_SCHEDULER_cancel (shutdown_task);
727 shutdown_task = GNUNET_SCHEDULER_add_now (&shutdown_op, NULL);
732 * Request random peers.
735 request_peers (void *cls,
736 const struct GNUNET_SCHEDULER_TaskContext *tc)
738 struct RPSPeer *rps_peer;
739 struct PendingRequest *pending_req = (struct PendingRequest *) cls;
740 struct PendingReply *pending_rep;
742 if (GNUNET_YES == in_shutdown)
744 rps_peer = pending_req->rps_peer;
745 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
746 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
747 rps_peer->pending_req_tail,
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750 "Requesting one peer\n");
751 pending_rep = GNUNET_new (struct PendingReply);
752 pending_rep->rps_peer = rps_peer;
753 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
755 cur_test_run.reply_handle,
757 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
758 rps_peer->pending_rep_tail,
760 rps_peer->num_pending_reps++;
761 rps_peer->num_pending_reqs--;
765 cancel_pending_req (struct PendingRequest *pending_req)
767 struct RPSPeer *rps_peer;
769 rps_peer = pending_req->rps_peer;
770 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
771 rps_peer->pending_req_tail,
773 rps_peer->num_pending_reqs--;
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775 "Cancelling pending request\n");
776 GNUNET_SCHEDULER_cancel (pending_req->request_task);
777 GNUNET_free (pending_req);
781 cancel_request (struct PendingReply *pending_rep)
783 struct RPSPeer *rps_peer;
785 rps_peer = pending_rep->rps_peer;
786 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
787 rps_peer->pending_rep_tail,
789 rps_peer->num_pending_reps--;
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Cancelling request\n");
792 GNUNET_RPS_request_cancel (pending_rep->req_handle);
793 GNUNET_free (pending_rep);
800 cancel_request_cb (void *cls,
801 const struct GNUNET_SCHEDULER_TaskContext *tc)
803 struct PendingReply *pending_rep;
804 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
806 if (GNUNET_YES == in_shutdown)
808 pending_rep = rps_peer->pending_rep_head;
809 GNUNET_assert (1 <= rps_peer->num_pending_reps);
810 cancel_request (pending_rep);
815 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
816 * issued, nor replied
819 schedule_missing_requests (struct RPSPeer *rps_peer)
822 struct PendingRequest *pending_req;
824 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
825 i < rps_peer->num_ids_to_request; i++)
827 pending_req = GNUNET_new (struct PendingRequest);
828 pending_req->rps_peer = rps_peer;
829 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
830 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
831 cur_test_run.request_interval * i),
834 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
835 rps_peer->pending_req_tail,
837 rps_peer->num_pending_reqs++;
842 cancel_pending_req_rep (struct RPSPeer *rps_peer)
844 while (NULL != rps_peer->pending_req_head)
845 cancel_pending_req (rps_peer->pending_req_head);
846 GNUNET_assert (0 == rps_peer->num_pending_reqs);
847 while (NULL != rps_peer->pending_rep_head)
848 cancel_request (rps_peer->pending_rep_head);
849 GNUNET_assert (0 == rps_peer->num_pending_reps);
852 /***********************************
854 ***********************************/
857 * Initialise only non-mal RPSPeers
859 static void mal_init_peer (struct RPSPeer *rps_peer)
861 if (rps_peer->index >= round (portion * num_peers))
862 rps_peer->num_ids_to_request = 1;
866 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
868 #ifdef ENABLE_MALICIOUS
869 uint32_t num_mal_peers;
870 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
872 GNUNET_assert ( (1 >= portion) &&
874 num_mal_peers = round (portion * num_peers);
876 if (rps_peer->index < num_mal_peers)
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
881 GNUNET_i2s (rps_peer->peer_id),
884 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
885 rps_peer_ids, target_peer);
887 #endif /* ENABLE_MALICIOUS */
891 mal_cb (struct RPSPeer *rps_peer)
893 uint32_t num_mal_peers;
895 #ifdef ENABLE_MALICIOUS
896 GNUNET_assert ( (1 >= portion) &&
898 num_mal_peers = round (portion * num_peers);
900 if (rps_peer->index >= num_mal_peers)
901 { /* It's useless to ask a malicious peer about a random sample -
903 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
904 seed_peers, rps_peer);
905 schedule_missing_requests (rps_peer);
907 #endif /* ENABLE_MALICIOUS */
911 /***********************************
913 ***********************************/
915 single_req_cb (struct RPSPeer *rps_peer)
917 schedule_missing_requests (rps_peer);
920 /***********************************
922 ***********************************/
924 delay_req_cb (struct RPSPeer *rps_peer)
926 schedule_missing_requests (rps_peer);
929 /***********************************
931 ***********************************/
933 seed_cb (struct RPSPeer *rps_peer)
935 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
936 seed_peers, rps_peer);
939 /***********************************
941 ***********************************/
943 seed_big_cb (struct RPSPeer *rps_peer)
945 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
948 /***********************************
950 ***********************************/
952 single_peer_seed_cb (struct RPSPeer *rps_peer)
957 /***********************************
959 ***********************************/
961 seed_req_cb (struct RPSPeer *rps_peer)
963 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
964 seed_peers, rps_peer);
965 schedule_missing_requests (rps_peer);
970 /***********************************
972 ***********************************/
974 req_cancel_cb (struct RPSPeer *rps_peer)
976 schedule_missing_requests (rps_peer);
977 GNUNET_SCHEDULER_add_delayed (
978 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
979 (cur_test_run.request_interval + 1)),
980 cancel_request_cb, rps_peer);
983 /***********************************
985 ***********************************/
988 * Callback to be called when RPS service is started or stopped at peers
991 * @param op the operation handle
992 * @param emsg NULL on success; otherwise an error description
996 struct GNUNET_TESTBED_Operation *op,
1000 struct OpListEntry *entry = cls;
1002 GNUNET_TESTBED_operation_done (entry->op);
1005 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1006 GNUNET_SCHEDULER_shutdown ();
1009 GNUNET_assert (0 != entry->delta);
1011 num_peers_online += entry->delta;
1013 if (0 > entry->delta)
1014 { /* Peer hopefully just went offline */
1015 if (GNUNET_YES != rps_peers[entry->index].online)
1017 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1018 "peer %s was expected to go offline but is still marked as online\n",
1019 GNUNET_i2s (rps_peers[entry->index].peer_id));
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "peer %s probably went offline as expected\n",
1026 GNUNET_i2s (rps_peers[entry->index].peer_id));
1028 rps_peers[entry->index].online = GNUNET_NO;
1031 else if (0 < entry->delta)
1032 { /* Peer hopefully just went online */
1033 if (GNUNET_NO != rps_peers[entry->index].online)
1035 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1036 "peer %s was expected to go online but is still marked as offline\n",
1037 GNUNET_i2s (rps_peers[entry->index].peer_id));
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "peer %s probably went online as expected\n",
1044 GNUNET_i2s (rps_peers[entry->index].peer_id));
1045 if (NULL != cur_test_run.pre_test)
1047 cur_test_run.pre_test (&rps_peers[entry->index],
1048 rps_peers[entry->index].rps_handle);
1049 schedule_missing_requests (&rps_peers[entry->index]);
1052 rps_peers[entry->index].online = GNUNET_YES;
1055 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1056 GNUNET_free (entry);
1057 //if (num_peers_in_round[current_round] == peers_running)
1062 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1063 double prob_go_on_off)
1065 struct OpListEntry *entry;
1068 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "%u. selected peer (%u: %s) is %s.\n",
1074 GNUNET_i2s (rps_peers[j].peer_id),
1075 (delta < 0)? "online" : "offline");
1076 if (prob < prob_go_on_off * UINT32_MAX)
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 GNUNET_i2s (rps_peers[j].peer_id),
1081 (delta < 0) ? "offline" : "online");
1083 entry = make_oplist_entry ();
1084 entry->delta = delta;
1086 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1091 (delta < 0) ? 0 : 1);
1096 churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1100 double portion_online;
1101 unsigned int *permut;
1102 double prob_go_offline;
1103 double portion_go_online;
1104 double portion_go_offline;
1106 /* Compute the probability for an online peer to go offline
1108 portion_online = num_peers_online * 1.0 / num_peers;
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "Portion online: %f\n",
1112 portion_go_online = ((1 - portion_online) * .5 * .66);
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 "Portion that should go online: %f\n",
1116 portion_go_offline = (portion_online + portion_go_online) - .75;
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118 "Portion that probably goes offline: %f\n",
1119 portion_go_offline);
1120 prob_go_offline = portion_go_offline / (portion_online * .5);
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "Probability of a selected online peer to go offline: %f\n",
1125 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1126 (unsigned int) num_peers);
1128 /* Go over 50% randomly chosen peers */
1129 for (i = 0; i < .5 * num_peers; i++)
1133 /* If online, shut down with certain probability */
1134 if (GNUNET_YES == rps_peers[j].online)
1136 cancel_pending_req_rep (&rps_peers[j]);
1137 manage_service_wrapper (i, j, -1, prob_go_offline);
1140 /* If offline, restart with certain probability */
1141 else if (GNUNET_NO == rps_peers[j].online)
1143 manage_service_wrapper (i, j, 1, 0.66);
1147 GNUNET_free (permut);
1149 churn_task = GNUNET_SCHEDULER_add_delayed (
1150 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1157 * Initialise given RPSPeer
1159 static void profiler_init_peer (struct RPSPeer *rps_peer)
1161 if (num_peers - 1 == rps_peer->index)
1162 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1167 * Callback to call on receipt of a reply
1169 * @param cls closure
1170 * @param n number of peers
1171 * @param recv_peers the received peers
1174 profiler_reply_handle (void *cls,
1176 const struct GNUNET_PeerIdentity *recv_peers)
1178 struct RPSPeer *rps_peer;
1179 struct RPSPeer *rcv_rps_peer;
1183 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1185 rps_peer = pending_rep->rps_peer;
1186 file_name = "/tmp/rps/received_ids";
1187 file_name_dh = "/tmp/rps/diehard_input";
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "[%s] got %" PRIu64 " peers:\n",
1190 GNUNET_i2s (rps_peer->peer_id),
1192 for (i = 0; i < n; i++)
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 GNUNET_i2s (&recv_peers[i]));
1200 GNUNET_i2s_full (&recv_peers[i]));
1201 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1202 tofile (file_name_dh,
1204 (uint32_t) rcv_rps_peer->index);
1206 /* Find #PendingReply holding the request handle */
1207 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1208 rps_peer->pending_rep_tail,
1210 rps_peer->num_pending_reps--;
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;
1313 testbed_peers = peers;
1314 num_peers_online = 0;
1315 for (i = 0 ; i < num_peers ; i++)
1317 entry = make_oplist_entry ();
1319 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1320 GNUNET_TESTBED_PIT_IDENTITY,
1325 GNUNET_assert (num_peers == n_peers);
1326 for (i = 0 ; i < n_peers ; i++)
1328 rps_peers[i].index = i;
1330 GNUNET_TESTBED_service_connect (&rps_peers[i],
1333 &rps_connect_complete_cb,
1335 &rps_connect_adapter,
1336 &rps_disconnect_adapter,
1340 if (NULL != churn_task)
1341 GNUNET_SCHEDULER_cancel (churn_task);
1342 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1347 * Entry point for the testcase, sets up the testbed.
1349 * @param argc unused
1350 * @param argv unused
1351 * @return 0 on success
1354 main (int argc, char *argv[])
1359 cur_test_run.name = "test-rps-default";
1360 cur_test_run.init_peer = default_init_peer;
1361 cur_test_run.pre_test = NULL;
1362 cur_test_run.reply_handle = default_reply_handle;
1363 cur_test_run.eval_cb = default_eval_cb;
1365 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1367 if (strstr (argv[0], "malicious") != NULL)
1369 cur_test_run.pre_test = mal_pre;
1370 cur_test_run.main_test = mal_cb;
1371 cur_test_run.init_peer = mal_init_peer;
1373 if (strstr (argv[0], "_1") != NULL)
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1376 cur_test_run.name = "test-rps-malicious_1";
1379 else if (strstr (argv[0], "_2") != NULL)
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1382 cur_test_run.name = "test-rps-malicious_2";
1385 else if (strstr (argv[0], "_3") != NULL)
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1388 cur_test_run.name = "test-rps-malicious_3";
1393 else if (strstr (argv[0], "_single_req") != NULL)
1395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1396 cur_test_run.name = "test-rps-single-req";
1397 cur_test_run.main_test = single_req_cb;
1400 else if (strstr (argv[0], "_delayed_reqs") != NULL)
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1403 cur_test_run.name = "test-rps-delayed-reqs";
1404 cur_test_run.main_test = delay_req_cb;
1407 else if (strstr (argv[0], "_seed_big") != NULL)
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
1410 cur_test_run.name = "test-rps-seed-big";
1411 cur_test_run.main_test = seed_big_cb;
1414 else if (strstr (argv[0], "_single_peer_seed") != NULL)
1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1417 cur_test_run.name = "test-rps-single-peer-seed";
1418 cur_test_run.main_test = single_peer_seed_cb;
1421 else if (strstr (argv[0], "_seed_request") != NULL)
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1424 cur_test_run.name = "test-rps-seed-request";
1425 cur_test_run.main_test = seed_req_cb;
1428 else if (strstr (argv[0], "_seed") != NULL)
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1431 cur_test_run.name = "test-rps-seed";
1432 cur_test_run.main_test = seed_cb;
1433 cur_test_run.eval_cb = no_eval;
1436 else if (strstr (argv[0], "_req_cancel") != NULL)
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1439 cur_test_run.name = "test-rps-req-cancel";
1441 cur_test_run.main_test = req_cancel_cb;
1442 cur_test_run.eval_cb = no_eval;
1443 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1446 else if (strstr (argv[0], "profiler") != NULL)
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1449 cur_test_run.name = "test-rps-profiler";
1452 cur_test_run.init_peer = profiler_init_peer;
1453 cur_test_run.pre_test = mal_pre;
1454 cur_test_run.main_test = profiler_cb;
1455 cur_test_run.reply_handle = profiler_reply_handle;
1456 cur_test_run.eval_cb = profiler_eval;
1457 cur_test_run.request_interval = 2;
1458 cur_test_run.num_requests = 5;
1459 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1461 /* 'Clean' directory */
1462 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1463 GNUNET_DISK_directory_create ("/tmp/rps/");
1466 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1467 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1468 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1469 if ( (2 == mal_type) ||
1471 target_peer = &rps_peer_ids[num_peers - 2];
1472 if (profiler_eval == cur_test_run.eval_cb)
1473 eval_peer = &rps_peers[num_peers - 1];
1476 (void) GNUNET_TESTBED_test_run (cur_test_run.name,
1482 ret_value = cur_test_run.eval_cb();
1483 GNUNET_free (rps_peers );
1484 GNUNET_free (rps_peer_ids);
1485 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1489 /* end of test_rps_multipeer.c */