2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file rps/test_rps.c
22 * @brief Testcase for the random peer sampling service. Starts
23 * a peergroup with a given number of peers, then waits to
24 * receive size pushes/pulls from each peer. Expects to wait
25 * for one message from each peer.
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
31 #include "gnunet_rps_service.h"
32 #include "rps-test_util.h"
33 #include "gnunet-service-rps_sampler_elem.h"
39 * How many peers do we start?
41 static uint32_t num_peers;
44 * How long do we run the test?
46 //#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 static struct GNUNET_TIME_Relative timeout;
51 * Portion of malicious peers
53 static double portion = .1;
56 * Type of malicious peer to test
58 static unsigned int mal_type = 0;
61 * Handles to all of the running peers
63 static struct GNUNET_TESTBED_Peer **testbed_peers;
74 struct OpListEntry *next;
79 struct OpListEntry *prev;
82 * The testbed operation
84 struct GNUNET_TESTBED_Operation *op;
87 * Depending on whether we start or stop NSE service at the peer set this to 1
93 * Index of the regarding peer
101 static struct OpListEntry *oplist_head;
106 static struct OpListEntry *oplist_tail;
110 * A pending reply: A request was sent and the reply is pending.
117 struct PendingReply *next;
118 struct PendingReply *prev;
121 * Handle to the request we are waiting for
123 struct GNUNET_RPS_Request_Handle *req_handle;
126 * The peer that requested
128 struct RPSPeer *rps_peer;
133 * A pending request: A request was not made yet but is scheduled for later.
135 struct PendingRequest
140 struct PendingRequest *next;
141 struct PendingRequest *prev;
144 * Handle to the request we are waiting for
146 struct GNUNET_SCHEDULER_Task *request_task;
149 * The peer that requested
151 struct RPSPeer *rps_peer;
156 * Information we track for each peer.
166 * Handle for RPS connect operation.
168 struct GNUNET_TESTBED_Operation *op;
171 * Handle to RPS service.
173 struct GNUNET_RPS_Handle *rps_handle;
178 struct GNUNET_PeerIdentity *peer_id;
181 * A request handle to check for an request
183 //struct GNUNET_RPS_Request_Handle *req_handle;
186 * Peer on- or offline?
191 * Number of Peer IDs to request
193 unsigned int num_ids_to_request;
196 * Pending requests DLL
198 struct PendingRequest *pending_req_head;
199 struct PendingRequest *pending_req_tail;
202 * Number of pending requests
204 unsigned int num_pending_reqs;
207 * Pending replies DLL
209 struct PendingReply *pending_rep_head;
210 struct PendingReply *pending_rep_tail;
213 * Number of pending replies
215 unsigned int num_pending_reps;
218 * Number of received PeerIDs
220 unsigned int num_recv_ids;
225 * Information for all the peers.
227 static struct RPSPeer *rps_peers;
230 * Peermap to get the index of a given peer ID quick.
232 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
237 static struct GNUNET_PeerIdentity *rps_peer_ids;
240 * ID of the targeted peer.
242 static struct GNUNET_PeerIdentity *target_peer;
245 * ID of the peer that requests for the evaluation.
247 static struct RPSPeer *eval_peer;
250 * Number of online peers.
252 static unsigned int num_peers_online;
255 * Return value from 'main'.
260 * Identifier for the churn task that runs periodically
262 static struct GNUNET_SCHEDULER_Task *churn_task;
265 * Called to initialise the given RPSPeer
267 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
270 * Called directly after connecting to the service
272 typedef void (*PreTest) (void *cls, struct GNUNET_RPS_Handle *h);
275 * Called from within #rps_connect_complete_cb ()
276 * Executes functions to test the api/service
278 typedef void (*MainTest) (struct RPSPeer *rps_peer);
281 * Callback called once the requested random peers are available
283 typedef void (*ReplyHandle) (void *cls,
285 const struct GNUNET_PeerIdentity *recv_peers);
288 * Called directly before disconnecting from the service
290 typedef void (*PostTest) (void *cls, struct GNUNET_RPS_Handle *h);
293 * Function called after disconnect to evaluate test success
295 typedef int (*EvaluationCallback) (void);
299 * Structure to define a single test
309 * Called to initialise peer
314 * Called directly after connecting to the service
319 * Function to execute the functions to be tested
324 * Callback called once the requested peers are available
326 ReplyHandle reply_handle;
329 * Called directly before disconnecting from the service
334 * Function to evaluate the test results
336 EvaluationCallback eval_cb;
341 uint32_t request_interval;
344 * Number of Requests to make.
346 uint32_t num_requests;
355 * Are we shutting down?
357 static int in_shutdown;
360 * Append arguments to file
363 tofile_ (const char *file_name, const char *line)
365 struct GNUNET_DISK_FileHandle *f;
366 /* char output_buffer[512]; */
371 if (NULL == (f = GNUNET_DISK_file_open (file_name,
372 GNUNET_DISK_OPEN_APPEND |
373 GNUNET_DISK_OPEN_WRITE |
374 GNUNET_DISK_OPEN_CREATE,
375 GNUNET_DISK_PERM_USER_READ |
376 GNUNET_DISK_PERM_USER_WRITE |
377 GNUNET_DISK_PERM_GROUP_READ |
378 GNUNET_DISK_PERM_OTHER_READ)))
380 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
381 "Not able to open file %s\n",
385 /* size = GNUNET_snprintf (output_buffer,
386 sizeof (output_buffer),
388 GNUNET_TIME_absolute_get ().abs_value_us,
392 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
393 "Failed to write string to buffer (size: %i)\n",
398 size = strlen (line) * sizeof (char);
400 size2 = GNUNET_DISK_file_write (f, line, size);
403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
404 "Unable to write to file! (Size: %lu, size2: %lu)\n",
407 if (GNUNET_YES != GNUNET_DISK_file_close (f))
409 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
410 "Unable to close file\n");
415 if (GNUNET_YES != GNUNET_DISK_file_close (f))
417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
418 "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_recv_ids,
475 rps_peers[i].num_ids_to_request,
476 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
477 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_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)
505 in_shutdown = GNUNET_YES;
506 if (NULL != churn_task)
508 GNUNET_SCHEDULER_cancel (churn_task);
511 for (i = 0; i < num_peers; i++)
512 if (NULL != rps_peers[i].op)
513 GNUNET_TESTBED_operation_done (rps_peers[i].op);
514 GNUNET_SCHEDULER_shutdown ();
522 seed_peers (void *cls)
524 struct RPSPeer *peer = cls;
528 // TODO if malicious don't seed mal peers
529 amount = round (.5 * num_peers);
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
532 for (i = 0 ; i < amount ; i++)
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
535 GNUNET_i2s (&rps_peer_ids[i]));
537 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
545 seed_peers_big (void *cls)
547 struct RPSPeer *peer = cls;
548 unsigned int seed_msg_size;
549 uint32_t num_peers_max;
553 seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
554 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - seed_msg_size) /
555 sizeof (struct GNUNET_PeerIdentity);
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Peers that fit in one seed msg; %u\n",
559 amount = num_peers_max + (0.5 * num_peers_max);
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "Seeding many (%u) peers:\n",
563 struct GNUNET_PeerIdentity ids_to_seed[amount];
564 for (i = 0; i < amount; i++)
566 ids_to_seed[i] = *peer->peer_id;
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
569 GNUNET_i2s (&ids_to_seed[i]));
572 GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
576 * Get the id of peer i.
579 info_cb (void *cb_cls,
580 struct GNUNET_TESTBED_Operation *op,
581 const struct GNUNET_TESTBED_PeerInformation *pinfo,
584 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
586 if (NULL == pinfo || NULL != emsg)
588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
589 GNUNET_TESTBED_operation_done (entry->op);
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
596 GNUNET_i2s (pinfo->result.id));
598 rps_peer_ids[entry->index] = *(pinfo->result.id);
599 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
601 GNUNET_assert (GNUNET_OK ==
602 GNUNET_CONTAINER_multipeermap_put (peer_map,
603 &rps_peer_ids[entry->index],
604 &rps_peers[entry->index],
605 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
606 tofile ("/tmp/rps/peer_ids",
609 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
611 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
612 GNUNET_TESTBED_operation_done (entry->op);
618 * Callback to be called when RPS service connect operation is completed
620 * @param cls the callback closure from functions generating an operation
621 * @param op the operation that has been finished
622 * @param ca_result the RPS service handle returned from rps_connect_adapter
623 * @param emsg error message in case the operation has failed; will be NULL if
624 * operation has executed successfully.
627 rps_connect_complete_cb (void *cls,
628 struct GNUNET_TESTBED_Operation *op,
632 struct RPSPeer *rps_peer = cls;
633 struct GNUNET_RPS_Handle *rps = ca_result;
635 rps_peer->rps_handle = rps;
636 rps_peer->online = GNUNET_YES;
639 GNUNET_assert (op == rps_peer->op);
642 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
643 "Failed to connect to RPS service: %s\n",
646 GNUNET_SCHEDULER_shutdown ();
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
652 cur_test_run.main_test (rps_peer);
657 * Adapter function called to establish a connection to
661 * @param cfg configuration of the peer to connect to; will be available until
662 * GNUNET_TESTBED_operation_done() is called on the operation returned
663 * from GNUNET_TESTBED_service_connect()
664 * @return service handle to return in 'op_result', NULL on error
667 rps_connect_adapter (void *cls,
668 const struct GNUNET_CONFIGURATION_Handle *cfg)
670 struct GNUNET_RPS_Handle *h;
672 h = GNUNET_RPS_connect (cfg);
674 if (NULL != cur_test_run.pre_test)
675 cur_test_run.pre_test (cls, h);
682 * Adapter function called to destroy connection to
686 * @param op_result service handle returned from the connect adapter
689 rps_disconnect_adapter (void *cls,
692 struct RPSPeer *peer = cls;
693 struct GNUNET_RPS_Handle *h = op_result;
694 GNUNET_assert (NULL != peer);
695 GNUNET_RPS_disconnect (h);
696 peer->rps_handle = NULL;
700 /***********************************************************************
701 * Definition of tests
702 ***********************************************************************/
704 // TODO check whether tests can be stopped earlier
706 default_eval_cb (void)
718 * Initialise given RPSPeer
720 static void default_init_peer (struct RPSPeer *rps_peer)
722 rps_peer->num_ids_to_request = 1;
726 * Callback to call on receipt of a reply
729 * @param n number of peers
730 * @param recv_peers the received peers
733 default_reply_handle (void *cls,
735 const struct GNUNET_PeerIdentity *recv_peers)
737 struct RPSPeer *rps_peer;
738 struct PendingReply *pending_rep = (struct PendingReply *) cls;
741 rps_peer = pending_rep->rps_peer;
742 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
743 rps_peer->pending_rep_tail,
745 rps_peer->num_pending_reps--;
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747 "[%s] got %" PRIu64 " peers:\n",
748 GNUNET_i2s (rps_peer->peer_id),
751 for (i = 0; i < n; i++)
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 GNUNET_i2s (&recv_peers[i]));
758 rps_peer->num_recv_ids++;
761 if (0 == evaluate ())
763 GNUNET_SCHEDULER_shutdown ();
768 * Request random peers.
771 request_peers (void *cls)
773 struct PendingRequest *pending_req = cls;
774 struct RPSPeer *rps_peer;
775 struct PendingReply *pending_rep;
777 if (GNUNET_YES == in_shutdown)
779 rps_peer = pending_req->rps_peer;
780 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
781 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
782 rps_peer->pending_req_tail,
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "Requesting one peer\n");
786 pending_rep = GNUNET_new (struct PendingReply);
787 pending_rep->rps_peer = rps_peer;
788 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
790 cur_test_run.reply_handle,
792 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
793 rps_peer->pending_rep_tail,
795 rps_peer->num_pending_reps++;
796 rps_peer->num_pending_reqs--;
800 cancel_pending_req (struct PendingRequest *pending_req)
802 struct RPSPeer *rps_peer;
804 rps_peer = pending_req->rps_peer;
805 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
806 rps_peer->pending_req_tail,
808 rps_peer->num_pending_reqs--;
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Cancelling pending request\n");
811 GNUNET_SCHEDULER_cancel (pending_req->request_task);
812 GNUNET_free (pending_req);
816 cancel_request (struct PendingReply *pending_rep)
818 struct RPSPeer *rps_peer;
820 rps_peer = pending_rep->rps_peer;
821 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
822 rps_peer->pending_rep_tail,
824 rps_peer->num_pending_reps--;
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826 "Cancelling request\n");
827 GNUNET_RPS_request_cancel (pending_rep->req_handle);
828 GNUNET_free (pending_rep);
835 cancel_request_cb (void *cls)
837 struct RPSPeer *rps_peer = cls;
838 struct PendingReply *pending_rep;
840 if (GNUNET_YES == in_shutdown)
842 pending_rep = rps_peer->pending_rep_head;
843 GNUNET_assert (1 <= rps_peer->num_pending_reps);
844 cancel_request (pending_rep);
849 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
850 * issued, nor replied
853 schedule_missing_requests (struct RPSPeer *rps_peer)
856 struct PendingRequest *pending_req;
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Scheduling %u - %u missing requests\n",
860 rps_peer->num_ids_to_request,
861 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
862 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
863 rps_peer->num_ids_to_request);
864 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
865 i < rps_peer->num_ids_to_request; i++)
867 pending_req = GNUNET_new (struct PendingRequest);
868 pending_req->rps_peer = rps_peer;
869 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
870 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
871 cur_test_run.request_interval * i),
874 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
875 rps_peer->pending_req_tail,
877 rps_peer->num_pending_reqs++;
882 cancel_pending_req_rep (struct RPSPeer *rps_peer)
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 "Cancelling all (pending) requests.\n");
886 while (NULL != rps_peer->pending_req_head)
887 cancel_pending_req (rps_peer->pending_req_head);
888 GNUNET_assert (0 == rps_peer->num_pending_reqs);
889 while (NULL != rps_peer->pending_rep_head)
890 cancel_request (rps_peer->pending_rep_head);
891 GNUNET_assert (0 == rps_peer->num_pending_reps);
894 /***********************************
896 ***********************************/
899 * Initialise only non-mal RPSPeers
901 static void mal_init_peer (struct RPSPeer *rps_peer)
903 if (rps_peer->index >= round (portion * num_peers))
904 rps_peer->num_ids_to_request = 1;
908 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
910 #ifdef ENABLE_MALICIOUS
911 uint32_t num_mal_peers;
912 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
914 GNUNET_assert ( (1 >= portion) &&
916 num_mal_peers = round (portion * num_peers);
918 if (rps_peer->index < num_mal_peers)
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
923 GNUNET_i2s (rps_peer->peer_id),
926 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
927 rps_peer_ids, target_peer);
929 #endif /* ENABLE_MALICIOUS */
933 mal_cb (struct RPSPeer *rps_peer)
935 uint32_t num_mal_peers;
937 #ifdef ENABLE_MALICIOUS
938 GNUNET_assert ( (1 >= portion) &&
940 num_mal_peers = round (portion * num_peers);
942 if (rps_peer->index >= num_mal_peers)
943 { /* It's useless to ask a malicious peer about a random sample -
945 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
946 seed_peers, rps_peer);
947 schedule_missing_requests (rps_peer);
949 #endif /* ENABLE_MALICIOUS */
953 /***********************************
955 ***********************************/
957 single_req_cb (struct RPSPeer *rps_peer)
959 schedule_missing_requests (rps_peer);
962 /***********************************
964 ***********************************/
966 delay_req_cb (struct RPSPeer *rps_peer)
968 schedule_missing_requests (rps_peer);
971 /***********************************
973 ***********************************/
975 seed_cb (struct RPSPeer *rps_peer)
977 GNUNET_SCHEDULER_add_delayed (
978 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
979 seed_peers, rps_peer);
982 /***********************************
984 ***********************************/
986 seed_big_cb (struct RPSPeer *rps_peer)
988 // TODO test seeding > GNUNET_MAX_MESSAGE_SIZE peers
989 GNUNET_SCHEDULER_add_delayed (
990 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
991 seed_peers_big, rps_peer);
994 /***********************************
996 ***********************************/
998 single_peer_seed_cb (struct RPSPeer *rps_peer)
1003 /***********************************
1005 ***********************************/
1007 seed_req_cb (struct RPSPeer *rps_peer)
1009 GNUNET_SCHEDULER_add_delayed (
1010 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1011 seed_peers, rps_peer);
1012 schedule_missing_requests (rps_peer);
1015 //TODO start big mal
1017 /***********************************
1019 ***********************************/
1021 req_cancel_cb (struct RPSPeer *rps_peer)
1023 schedule_missing_requests (rps_peer);
1024 GNUNET_SCHEDULER_add_delayed (
1025 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1026 (cur_test_run.request_interval + 1)),
1027 cancel_request_cb, rps_peer);
1030 /***********************************
1032 ***********************************/
1038 churn_test_cb (struct RPSPeer *rps_peer)
1041 if (GNUNET_YES == cur_test_run.have_churn && NULL == churn_task)
1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1044 "Starting churn task\n");
1045 churn_task = GNUNET_SCHEDULER_add_delayed (
1046 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "Not starting churn task\n");
1054 schedule_missing_requests (rps_peer);
1057 /***********************************
1059 ***********************************/
1062 * Callback to be called when RPS service is started or stopped at peers
1065 * @param op the operation handle
1066 * @param emsg NULL on success; otherwise an error description
1069 churn_cb (void *cls,
1070 struct GNUNET_TESTBED_Operation *op,
1074 struct OpListEntry *entry = cls;
1076 GNUNET_TESTBED_operation_done (entry->op);
1079 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1080 GNUNET_SCHEDULER_shutdown ();
1083 GNUNET_assert (0 != entry->delta);
1085 num_peers_online += entry->delta;
1087 if (0 > entry->delta)
1088 { /* Peer hopefully just went offline */
1089 if (GNUNET_YES != rps_peers[entry->index].online)
1091 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1092 "peer %s was expected to go offline but is still marked as online\n",
1093 GNUNET_i2s (rps_peers[entry->index].peer_id));
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1099 "peer %s probably went offline as expected\n",
1100 GNUNET_i2s (rps_peers[entry->index].peer_id));
1102 rps_peers[entry->index].online = GNUNET_NO;
1105 else if (0 < entry->delta)
1106 { /* Peer hopefully just went online */
1107 if (GNUNET_NO != rps_peers[entry->index].online)
1109 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1110 "peer %s was expected to go online but is still marked as offline\n",
1111 GNUNET_i2s (rps_peers[entry->index].peer_id));
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "peer %s probably went online as expected\n",
1118 GNUNET_i2s (rps_peers[entry->index].peer_id));
1119 if (NULL != cur_test_run.pre_test)
1121 cur_test_run.pre_test (&rps_peers[entry->index],
1122 rps_peers[entry->index].rps_handle);
1123 schedule_missing_requests (&rps_peers[entry->index]);
1126 rps_peers[entry->index].online = GNUNET_YES;
1129 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1130 GNUNET_free (entry);
1131 //if (num_peers_in_round[current_round] == peers_running)
1136 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1137 double prob_go_on_off)
1139 struct OpListEntry *entry;
1142 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "%u. selected peer (%u: %s) is %s.\n",
1148 GNUNET_i2s (rps_peers[j].peer_id),
1149 (0 > delta) ? "online" : "offline");
1150 if (prob < prob_go_on_off * UINT32_MAX)
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 GNUNET_i2s (rps_peers[j].peer_id),
1155 (0 > delta) ? "offline" : "online");
1158 cancel_pending_req_rep (&rps_peers[j]);
1159 entry = make_oplist_entry ();
1160 entry->delta = delta;
1162 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1167 (0 > delta) ? 0 : 1);
1177 double portion_online;
1178 unsigned int *permut;
1179 double prob_go_offline;
1180 double portion_go_online;
1181 double portion_go_offline;
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "Churn function executing\n");
1186 /* Compute the probability for an online peer to go offline
1188 portion_online = num_peers_online * 1.0 / num_peers;
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Portion online: %f\n",
1192 portion_go_online = ((1 - portion_online) * .5 * .66);
1193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1194 "Portion that should go online: %f\n",
1196 portion_go_offline = (portion_online + portion_go_online) - .75;
1197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198 "Portion that probably goes offline: %f\n",
1199 portion_go_offline);
1200 prob_go_offline = portion_go_offline / (portion_online * .5);
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202 "Probability of a selected online peer to go offline: %f\n",
1205 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1206 (unsigned int) num_peers);
1208 /* Go over 50% randomly chosen peers */
1209 for (i = 0; i < .5 * num_peers; i++)
1213 /* If online, shut down with certain probability */
1214 if (GNUNET_YES == rps_peers[j].online)
1216 manage_service_wrapper (i, j, -1, prob_go_offline);
1219 /* If offline, restart with certain probability */
1220 else if (GNUNET_NO == rps_peers[j].online)
1222 manage_service_wrapper (i, j, 1, 0.66);
1226 GNUNET_free (permut);
1228 churn_task = GNUNET_SCHEDULER_add_delayed (
1229 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1236 * Initialise given RPSPeer
1238 static void profiler_init_peer (struct RPSPeer *rps_peer)
1240 if (num_peers - 1 == rps_peer->index)
1241 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1246 * Callback to call on receipt of a reply
1248 * @param cls closure
1249 * @param n number of peers
1250 * @param recv_peers the received peers
1253 profiler_reply_handle (void *cls,
1255 const struct GNUNET_PeerIdentity *recv_peers)
1257 struct RPSPeer *rps_peer;
1258 struct RPSPeer *rcv_rps_peer;
1262 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1264 rps_peer = pending_rep->rps_peer;
1265 file_name = "/tmp/rps/received_ids";
1266 file_name_dh = "/tmp/rps/diehard_input";
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1268 "[%s] got %" PRIu64 " peers:\n",
1269 GNUNET_i2s (rps_peer->peer_id),
1271 for (i = 0; i < n; i++)
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 GNUNET_i2s (&recv_peers[i]));
1279 GNUNET_i2s_full (&recv_peers[i]));
1280 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1281 GNUNET_assert (NULL != rcv_rps_peer);
1282 tofile (file_name_dh,
1284 (uint32_t) rcv_rps_peer->index);
1286 default_reply_handle (cls, n, recv_peers);
1291 profiler_cb (struct RPSPeer *rps_peer)
1294 if (GNUNET_YES == cur_test_run.have_churn && NULL == churn_task)
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "Starting churn task\n");
1298 churn_task = GNUNET_SCHEDULER_add_delayed (
1299 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1304 "Not starting churn task\n");
1307 /* Only request peer ids at one peer.
1308 * (It's the before-last because last one is target of the focussed attack.)
1310 if (eval_peer == rps_peer)
1311 schedule_missing_requests (rps_peer);
1315 * Function called from #profiler_eval with a filename.
1317 * @param cls closure
1318 * @param filename complete filename (absolute path)
1319 * @return #GNUNET_OK to continue to iterate,
1320 * #GNUNET_NO to stop iteration with no error,
1321 * #GNUNET_SYSERR to abort iteration with error!
1324 file_name_cb (void *cls, const char *filename)
1326 if (NULL != strstr (filename, "sampler_el"))
1328 struct RPS_SamplerElement *s_elem;
1329 struct GNUNET_CRYPTO_AuthKey auth_key;
1330 const char *key_char;
1333 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1334 tofile (filename, "--------------------------\n");
1336 auth_key = string_to_auth_key (key_char);
1337 s_elem = RPS_sampler_elem_create ();
1338 RPS_sampler_elem_set (s_elem, auth_key);
1340 for (i = 0; i < num_peers; i++)
1342 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1344 RPS_sampler_elem_destroy (s_elem);
1350 * This is run after the test finished.
1352 * Compute all perfect samples.
1355 profiler_eval (void)
1357 /* Compute perfect sample for each sampler element */
1358 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1367 /***********************************************************************
1368 * /Definition of tests
1369 ***********************************************************************/
1373 * Actual "main" function for the testcase.
1375 * @param cls closure
1376 * @param h the run handle
1377 * @param n_peers number of peers in 'peers'
1378 * @param peers handle to peers run in the testbed
1379 * @param links_succeeded the number of overlay link connection attempts that
1381 * @param links_failed the number of overlay link connection attempts that
1386 struct GNUNET_TESTBED_RunHandle *h,
1387 unsigned int n_peers,
1388 struct GNUNET_TESTBED_Peer **peers,
1389 unsigned int links_succeeded,
1390 unsigned int links_failed)
1393 struct OpListEntry *entry;
1394 uint32_t num_mal_peers;
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
1398 /* Check whether we timed out */
1399 if (n_peers != num_peers ||
1401 0 == links_succeeded)
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
1408 GNUNET_SCHEDULER_shutdown ();
1413 /* Initialize peers */
1414 testbed_peers = peers;
1415 num_peers_online = 0;
1416 for (i = 0; i < num_peers; i++)
1418 entry = make_oplist_entry ();
1420 rps_peers[i].index = i;
1421 if (NULL != cur_test_run.init_peer)
1422 cur_test_run.init_peer (&rps_peers[i]);
1423 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1424 GNUNET_TESTBED_PIT_IDENTITY,
1429 num_mal_peers = round (portion * num_peers);
1430 GNUNET_assert (num_peers == n_peers);
1431 for (i = 0; i < n_peers; i++)
1433 rps_peers[i].index = i;
1434 if ( (rps_peers[i].num_recv_ids < rps_peers[i].num_ids_to_request) ||
1435 (i < num_mal_peers) )
1438 GNUNET_TESTBED_service_connect (&rps_peers[i],
1441 &rps_connect_complete_cb,
1443 &rps_connect_adapter,
1444 &rps_disconnect_adapter,
1449 if (NULL != churn_task)
1450 GNUNET_SCHEDULER_cancel (churn_task);
1451 GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1456 * Entry point for the testcase, sets up the testbed.
1458 * @param argc unused
1459 * @param argv unused
1460 * @return 0 on success
1463 main (int argc, char *argv[])
1468 cur_test_run.name = "test-rps-default";
1469 cur_test_run.init_peer = default_init_peer;
1470 cur_test_run.pre_test = NULL;
1471 cur_test_run.reply_handle = default_reply_handle;
1472 cur_test_run.eval_cb = default_eval_cb;
1473 cur_test_run.have_churn = GNUNET_YES;
1475 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1477 if (strstr (argv[0], "malicious") != NULL)
1479 cur_test_run.pre_test = mal_pre;
1480 cur_test_run.main_test = mal_cb;
1481 cur_test_run.init_peer = mal_init_peer;
1483 if (strstr (argv[0], "_1") != NULL)
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1486 cur_test_run.name = "test-rps-malicious_1";
1489 else if (strstr (argv[0], "_2") != NULL)
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1492 cur_test_run.name = "test-rps-malicious_2";
1495 else if (strstr (argv[0], "_3") != NULL)
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1498 cur_test_run.name = "test-rps-malicious_3";
1503 else if (strstr (argv[0], "_single_req") != NULL)
1505 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1506 cur_test_run.name = "test-rps-single-req";
1507 cur_test_run.main_test = single_req_cb;
1508 cur_test_run.have_churn = GNUNET_NO;
1511 else if (strstr (argv[0], "_delayed_reqs") != NULL)
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1514 cur_test_run.name = "test-rps-delayed-reqs";
1515 cur_test_run.main_test = delay_req_cb;
1516 cur_test_run.have_churn = GNUNET_NO;
1519 else if (strstr (argv[0], "_seed_big") != NULL)
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
1523 cur_test_run.name = "test-rps-seed-big";
1524 cur_test_run.main_test = seed_big_cb;
1525 cur_test_run.eval_cb = no_eval;
1526 cur_test_run.have_churn = GNUNET_NO;
1527 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1530 else if (strstr (argv[0], "_single_peer_seed") != NULL)
1532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1533 cur_test_run.name = "test-rps-single-peer-seed";
1534 cur_test_run.main_test = single_peer_seed_cb;
1535 cur_test_run.have_churn = GNUNET_NO;
1538 else if (strstr (argv[0], "_seed_request") != NULL)
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1541 cur_test_run.name = "test-rps-seed-request";
1542 cur_test_run.main_test = seed_req_cb;
1543 cur_test_run.have_churn = GNUNET_NO;
1546 else if (strstr (argv[0], "_seed") != NULL)
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1549 cur_test_run.name = "test-rps-seed";
1550 cur_test_run.main_test = seed_cb;
1551 cur_test_run.eval_cb = no_eval;
1552 cur_test_run.have_churn = GNUNET_NO;
1555 else if (strstr (argv[0], "_req_cancel") != NULL)
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1558 cur_test_run.name = "test-rps-req-cancel";
1560 cur_test_run.main_test = req_cancel_cb;
1561 cur_test_run.eval_cb = no_eval;
1562 cur_test_run.have_churn = GNUNET_NO;
1563 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1566 else if (strstr (argv[0], "_churn") != NULL)
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
1569 cur_test_run.name = "test-rps-churn";
1571 cur_test_run.init_peer = default_init_peer;
1572 cur_test_run.main_test = churn_test_cb;
1573 cur_test_run.reply_handle = default_reply_handle;
1574 cur_test_run.eval_cb = default_eval_cb;
1575 cur_test_run.have_churn = GNUNET_YES;
1576 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1579 else if (strstr (argv[0], "profiler") != NULL)
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1582 cur_test_run.name = "test-rps-profiler";
1585 cur_test_run.init_peer = profiler_init_peer;
1586 cur_test_run.pre_test = mal_pre;
1587 cur_test_run.main_test = profiler_cb;
1588 cur_test_run.reply_handle = profiler_reply_handle;
1589 cur_test_run.eval_cb = profiler_eval;
1590 cur_test_run.request_interval = 2;
1591 cur_test_run.num_requests = 5;
1592 cur_test_run.have_churn = GNUNET_YES;
1593 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1595 /* 'Clean' directory */
1596 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1597 GNUNET_DISK_directory_create ("/tmp/rps/");
1600 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1601 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1602 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1603 if ( (2 == mal_type) ||
1605 target_peer = &rps_peer_ids[num_peers - 2];
1606 if (profiler_eval == cur_test_run.eval_cb)
1607 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
1608 malicious peer if not careful
1609 with the malicious portion */
1612 (void) GNUNET_TESTBED_test_run (cur_test_run.name,
1618 ret_value = cur_test_run.eval_cb();
1619 GNUNET_free (rps_peers );
1620 GNUNET_free (rps_peer_ids);
1621 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1625 /* end of test_rps.c */