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 RPS service at the peer set this to 1 (start)
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 during the whole test
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;
223 * Pending operation on that peer
225 const struct OpListEntry *entry_op_manage;
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'.
265 * Identifier for the churn task that runs periodically
267 static struct GNUNET_SCHEDULER_Task *shutdown_task;
270 * Identifier for the churn task that runs periodically
272 static struct GNUNET_SCHEDULER_Task *churn_task;
275 * Called to initialise the given RPSPeer
277 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
280 * @brief Called directly after connecting to the service
282 * @param rps_peer Specific peer the function is called on
283 * @param h the handle to the rps service
285 typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
288 * @brief Executes functions to test the api/service for a given peer
290 * Called from within #rps_connect_complete_cb ()
291 * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
292 * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
294 * @param rps_peer the peer the task runs on
296 typedef void (*MainTest) (struct RPSPeer *rps_peer);
299 * Callback called once the requested random peers are available
301 typedef void (*ReplyHandle) (void *cls,
303 const struct GNUNET_PeerIdentity *recv_peers);
306 * Called directly before disconnecting from the service
308 typedef void (*PostTest) (void *cls, struct GNUNET_RPS_Handle *h);
311 * Function called after disconnect to evaluate test success
313 typedef int (*EvaluationCallback) (void);
317 * Structure to define a single test
327 * Called with a single peer in order to initialise that peer
332 * Called directly after connecting to the service
337 * Main function for each peer
342 * Callback called once the requested peers are available
344 ReplyHandle reply_handle;
347 * Called directly before disconnecting from the service
352 * Function to evaluate the test results
354 EvaluationCallback eval_cb;
359 uint32_t request_interval;
362 * Number of Requests to make.
364 uint32_t num_requests;
373 * Are we shutting down?
375 static int in_shutdown;
378 * Append arguments to file
381 tofile_ (const char *file_name, const char *line)
383 struct GNUNET_DISK_FileHandle *f;
384 /* char output_buffer[512]; */
389 if (NULL == (f = GNUNET_DISK_file_open (file_name,
390 GNUNET_DISK_OPEN_APPEND |
391 GNUNET_DISK_OPEN_WRITE |
392 GNUNET_DISK_OPEN_CREATE,
393 GNUNET_DISK_PERM_USER_READ |
394 GNUNET_DISK_PERM_USER_WRITE |
395 GNUNET_DISK_PERM_GROUP_READ |
396 GNUNET_DISK_PERM_OTHER_READ)))
398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
399 "Not able to open file %s\n",
403 /* size = GNUNET_snprintf (output_buffer,
404 sizeof (output_buffer),
406 GNUNET_TIME_absolute_get ().abs_value_us,
410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
411 "Failed to write string to buffer (size: %i)\n",
416 size = strlen (line) * sizeof (char);
418 size2 = GNUNET_DISK_file_write (f, line, size);
421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
422 "Unable to write to file! (Size: %lu, size2: %lu)\n",
425 if (GNUNET_YES != GNUNET_DISK_file_close (f))
427 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
428 "Unable to close file\n");
433 if (GNUNET_YES != GNUNET_DISK_file_close (f))
435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
436 "Unable to close file\n");
441 * This function is used to facilitate writing important information to disk
443 #define tofile(file_name, ...) do {\
446 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
448 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
449 "Failed to create tmp_buf\n");\
451 tofile_(file_name,tmp_buf);\
456 * Write the ids and their according index in the given array to a file
460 ids_to_file (char *file_name,
461 struct GNUNET_PeerIdentity *peer_ids,
462 unsigned int num_peer_ids)
466 for (i=0 ; i < num_peer_ids ; i++)
471 GNUNET_i2s_full (&peer_ids[i]));
476 * Test the success of a single test
486 for (i = 0; i < num_peers; i++)
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
491 GNUNET_i2s (rps_peers[i].peer_id),
492 rps_peers[i].num_recv_ids,
493 rps_peers[i].num_ids_to_request,
494 (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
495 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
497 return tmp_ok? 0 : 1;
502 * Creates an oplist entry and adds it to the oplist DLL
504 static struct OpListEntry *
507 struct OpListEntry *entry;
509 entry = GNUNET_new (struct OpListEntry);
510 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
516 * Task run on timeout to shut everything down.
519 shutdown_op (void *cls)
523 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
524 "Shutdown task scheduled, going down.\n");
525 in_shutdown = GNUNET_YES;
526 if (NULL != churn_task)
528 GNUNET_SCHEDULER_cancel (churn_task);
531 for (i = 0; i < num_peers; i++)
532 if (NULL != rps_peers[i].op)
533 GNUNET_TESTBED_operation_done (rps_peers[i].op);
534 GNUNET_SCHEDULER_shutdown ();
542 seed_peers (void *cls)
544 struct RPSPeer *peer = cls;
548 // TODO if malicious don't seed mal peers
549 amount = round (.5 * num_peers);
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
552 for (i = 0 ; i < amount ; i++)
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
555 GNUNET_i2s (&rps_peer_ids[i]));
557 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
565 seed_peers_big (void *cls)
567 struct RPSPeer *peer = cls;
568 unsigned int seed_msg_size;
569 uint32_t num_peers_max;
573 seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
574 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - seed_msg_size) /
575 sizeof (struct GNUNET_PeerIdentity);
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "Peers that fit in one seed msg; %u\n",
579 amount = num_peers_max + (0.5 * num_peers_max);
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581 "Seeding many (%u) peers:\n",
583 struct GNUNET_PeerIdentity ids_to_seed[amount];
584 for (i = 0; i < amount; i++)
586 ids_to_seed[i] = *peer->peer_id;
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
589 GNUNET_i2s (&ids_to_seed[i]));
592 GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
596 * Get the id of peer i.
599 info_cb (void *cb_cls,
600 struct GNUNET_TESTBED_Operation *op,
601 const struct GNUNET_TESTBED_PeerInformation *pinfo,
604 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
606 if (GNUNET_YES == in_shutdown)
611 if (NULL == pinfo || NULL != emsg)
613 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
614 GNUNET_TESTBED_operation_done (entry->op);
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 GNUNET_i2s (pinfo->result.id));
623 rps_peer_ids[entry->index] = *(pinfo->result.id);
624 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
626 GNUNET_assert (GNUNET_OK ==
627 GNUNET_CONTAINER_multipeermap_put (peer_map,
628 &rps_peer_ids[entry->index],
629 &rps_peers[entry->index],
630 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
631 tofile ("/tmp/rps/peer_ids",
634 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
636 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
637 GNUNET_TESTBED_operation_done (entry->op);
643 * Callback to be called when RPS service connect operation is completed
645 * @param cls the callback closure from functions generating an operation
646 * @param op the operation that has been finished
647 * @param ca_result the RPS service handle returned from rps_connect_adapter
648 * @param emsg error message in case the operation has failed; will be NULL if
649 * operation has executed successfully.
652 rps_connect_complete_cb (void *cls,
653 struct GNUNET_TESTBED_Operation *op,
657 struct RPSPeer *rps_peer = cls;
658 struct GNUNET_RPS_Handle *rps = ca_result;
660 if (GNUNET_YES == in_shutdown)
665 rps_peer->rps_handle = rps;
666 rps_peer->online = GNUNET_YES;
669 GNUNET_assert (op == rps_peer->op);
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673 "Failed to connect to RPS service: %s\n",
676 GNUNET_SCHEDULER_shutdown ();
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
682 cur_test_run.main_test (rps_peer);
687 * Adapter function called to establish a connection to
691 * @param cfg configuration of the peer to connect to; will be available until
692 * GNUNET_TESTBED_operation_done() is called on the operation returned
693 * from GNUNET_TESTBED_service_connect()
694 * @return service handle to return in 'op_result', NULL on error
697 rps_connect_adapter (void *cls,
698 const struct GNUNET_CONFIGURATION_Handle *cfg)
700 struct GNUNET_RPS_Handle *h;
702 h = GNUNET_RPS_connect (cfg);
704 if (NULL != cur_test_run.pre_test)
705 cur_test_run.pre_test (cls, h);
712 * Adapter function called to destroy connection to
716 * @param op_result service handle returned from the connect adapter
719 rps_disconnect_adapter (void *cls,
722 struct RPSPeer *peer = cls;
723 struct GNUNET_RPS_Handle *h = op_result;
724 GNUNET_assert (NULL != peer);
725 GNUNET_RPS_disconnect (h);
726 peer->rps_handle = NULL;
730 /***********************************************************************
731 * Definition of tests
732 ***********************************************************************/
734 // TODO check whether tests can be stopped earlier
736 default_eval_cb (void)
748 * Initialise given RPSPeer
750 static void default_init_peer (struct RPSPeer *rps_peer)
752 rps_peer->num_ids_to_request = 1;
756 * Callback to call on receipt of a reply
759 * @param n number of peers
760 * @param recv_peers the received peers
763 default_reply_handle (void *cls,
765 const struct GNUNET_PeerIdentity *recv_peers)
767 struct RPSPeer *rps_peer;
768 struct PendingReply *pending_rep = (struct PendingReply *) cls;
771 rps_peer = pending_rep->rps_peer;
772 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
773 rps_peer->pending_rep_tail,
775 rps_peer->num_pending_reps--;
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "[%s] got %" PRIu64 " peers:\n",
778 GNUNET_i2s (rps_peer->peer_id),
781 for (i = 0; i < n; i++)
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 GNUNET_i2s (&recv_peers[i]));
788 rps_peer->num_recv_ids++;
791 if (0 == evaluate ())
793 GNUNET_assert (NULL != shutdown_task);
794 GNUNET_SCHEDULER_cancel (shutdown_task);
795 shutdown_task = GNUNET_SCHEDULER_add_now (&shutdown_op, NULL);
796 GNUNET_assert (NULL!= shutdown_task);
801 * Request random peers.
804 request_peers (void *cls)
806 struct PendingRequest *pending_req = cls;
807 struct RPSPeer *rps_peer;
808 struct PendingReply *pending_rep;
810 if (GNUNET_YES == in_shutdown)
812 rps_peer = pending_req->rps_peer;
813 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
814 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
815 rps_peer->pending_req_tail,
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "Requesting one peer\n");
819 pending_rep = GNUNET_new (struct PendingReply);
820 pending_rep->rps_peer = rps_peer;
821 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
823 cur_test_run.reply_handle,
825 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
826 rps_peer->pending_rep_tail,
828 rps_peer->num_pending_reps++;
829 rps_peer->num_pending_reqs--;
833 cancel_pending_req (struct PendingRequest *pending_req)
835 struct RPSPeer *rps_peer;
837 rps_peer = pending_req->rps_peer;
838 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
839 rps_peer->pending_req_tail,
841 rps_peer->num_pending_reqs--;
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 "Cancelling pending request\n");
844 GNUNET_SCHEDULER_cancel (pending_req->request_task);
845 GNUNET_free (pending_req);
849 cancel_request (struct PendingReply *pending_rep)
851 struct RPSPeer *rps_peer;
853 rps_peer = pending_rep->rps_peer;
854 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
855 rps_peer->pending_rep_tail,
857 rps_peer->num_pending_reps--;
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Cancelling request\n");
860 GNUNET_RPS_request_cancel (pending_rep->req_handle);
861 GNUNET_free (pending_rep);
868 cancel_request_cb (void *cls)
870 struct RPSPeer *rps_peer = cls;
871 struct PendingReply *pending_rep;
873 if (GNUNET_YES == in_shutdown)
875 pending_rep = rps_peer->pending_rep_head;
876 GNUNET_assert (1 <= rps_peer->num_pending_reps);
877 cancel_request (pending_rep);
882 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
883 * issued, nor replied
886 schedule_missing_requests (struct RPSPeer *rps_peer)
889 struct PendingRequest *pending_req;
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Scheduling %u - %u missing requests\n",
893 rps_peer->num_ids_to_request,
894 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
895 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
896 rps_peer->num_ids_to_request);
897 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
898 i < rps_peer->num_ids_to_request; i++)
900 pending_req = GNUNET_new (struct PendingRequest);
901 pending_req->rps_peer = rps_peer;
902 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
903 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
904 cur_test_run.request_interval * i),
907 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
908 rps_peer->pending_req_tail,
910 rps_peer->num_pending_reqs++;
915 cancel_pending_req_rep (struct RPSPeer *rps_peer)
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "Cancelling all (pending) requests.\n");
919 while (NULL != rps_peer->pending_req_head)
920 cancel_pending_req (rps_peer->pending_req_head);
921 GNUNET_assert (0 == rps_peer->num_pending_reqs);
922 while (NULL != rps_peer->pending_rep_head)
923 cancel_request (rps_peer->pending_rep_head);
924 GNUNET_assert (0 == rps_peer->num_pending_reps);
927 /***********************************
929 ***********************************/
932 * Initialise only non-mal RPSPeers
934 static void mal_init_peer (struct RPSPeer *rps_peer)
936 if (rps_peer->index >= round (portion * num_peers))
937 rps_peer->num_ids_to_request = 1;
942 * @brief Set peers to (non-)malicious before execution
944 * Of signature #PreTest
946 * @param rps_peer the peer to set (non-) malicious
947 * @param h the handle to the service
950 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
952 #ifdef ENABLE_MALICIOUS
953 uint32_t num_mal_peers;
955 GNUNET_assert ( (1 >= portion) &&
957 num_mal_peers = round (portion * num_peers);
959 if (rps_peer->index < num_mal_peers)
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
964 GNUNET_i2s (rps_peer->peer_id),
967 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
968 rps_peer_ids, target_peer);
970 #endif /* ENABLE_MALICIOUS */
974 mal_cb (struct RPSPeer *rps_peer)
976 uint32_t num_mal_peers;
978 if (GNUNET_YES == in_shutdown)
983 #ifdef ENABLE_MALICIOUS
984 GNUNET_assert ( (1 >= portion) &&
986 num_mal_peers = round (portion * num_peers);
988 if (rps_peer->index >= num_mal_peers)
989 { /* It's useless to ask a malicious peer about a random sample -
991 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
992 seed_peers, rps_peer);
993 schedule_missing_requests (rps_peer);
995 #endif /* ENABLE_MALICIOUS */
999 /***********************************
1001 ***********************************/
1003 single_req_cb (struct RPSPeer *rps_peer)
1005 if (GNUNET_YES == in_shutdown)
1010 schedule_missing_requests (rps_peer);
1013 /***********************************
1015 ***********************************/
1017 delay_req_cb (struct RPSPeer *rps_peer)
1019 if (GNUNET_YES == in_shutdown)
1024 schedule_missing_requests (rps_peer);
1027 /***********************************
1029 ***********************************/
1031 seed_cb (struct RPSPeer *rps_peer)
1033 if (GNUNET_YES == in_shutdown)
1038 GNUNET_SCHEDULER_add_delayed (
1039 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
1040 seed_peers, rps_peer);
1043 /***********************************
1045 ***********************************/
1047 seed_big_cb (struct RPSPeer *rps_peer)
1049 if (GNUNET_YES == in_shutdown)
1054 // TODO test seeding > GNUNET_MAX_MESSAGE_SIZE peers
1055 GNUNET_SCHEDULER_add_delayed (
1056 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1057 seed_peers_big, rps_peer);
1060 /***********************************
1062 ***********************************/
1064 single_peer_seed_cb (struct RPSPeer *rps_peer)
1069 /***********************************
1071 ***********************************/
1073 seed_req_cb (struct RPSPeer *rps_peer)
1075 if (GNUNET_YES == in_shutdown)
1080 GNUNET_SCHEDULER_add_delayed (
1081 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1082 seed_peers, rps_peer);
1083 schedule_missing_requests (rps_peer);
1086 //TODO start big mal
1088 /***********************************
1090 ***********************************/
1092 req_cancel_cb (struct RPSPeer *rps_peer)
1094 if (GNUNET_YES == in_shutdown)
1099 schedule_missing_requests (rps_peer);
1100 GNUNET_SCHEDULER_add_delayed (
1101 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1102 (cur_test_run.request_interval + 1)),
1103 cancel_request_cb, rps_peer);
1106 /***********************************
1108 ***********************************/
1114 * @brief Starts churn
1116 * Has signature of #MainTest
1118 * This is not implemented too nicely as this is called for each peer, but we
1119 * only need to call it once. (Yes we check that we only schedule the task
1122 * @param rps_peer The peer it's called for
1125 churn_test_cb (struct RPSPeer *rps_peer)
1127 if (GNUNET_YES == in_shutdown)
1133 if (GNUNET_YES == cur_test_run.have_churn && NULL == churn_task)
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 "Starting churn task\n");
1137 churn_task = GNUNET_SCHEDULER_add_delayed (
1138 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "Not starting churn task\n");
1146 schedule_missing_requests (rps_peer);
1149 /***********************************
1151 ***********************************/
1154 * Callback to be called when RPS service is started or stopped at peers
1157 * @param op the operation handle
1158 * @param emsg NULL on success; otherwise an error description
1161 churn_cb (void *cls,
1162 struct GNUNET_TESTBED_Operation *op,
1166 struct OpListEntry *entry = cls;
1168 if (GNUNET_YES == in_shutdown)
1173 GNUNET_TESTBED_operation_done (entry->op);
1176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1177 GNUNET_SCHEDULER_shutdown ();
1180 GNUNET_assert (0 != entry->delta);
1182 num_peers_online += entry->delta;
1184 if (-1 == entry->delta)
1185 { /* Peer hopefully just went offline */
1186 if (GNUNET_YES != rps_peers[entry->index].online)
1188 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1189 "peer %s was expected to go offline but is still marked as online\n",
1190 GNUNET_i2s (rps_peers[entry->index].peer_id));
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "peer %s probably went offline as expected\n",
1197 GNUNET_i2s (rps_peers[entry->index].peer_id));
1199 rps_peers[entry->index].online = GNUNET_NO;
1202 else if (0 < entry->delta)
1203 { /* Peer hopefully just went online */
1204 if (GNUNET_NO != rps_peers[entry->index].online)
1206 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1207 "peer %s was expected to go online but is still marked as offline\n",
1208 GNUNET_i2s (rps_peers[entry->index].peer_id));
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214 "peer %s probably went online as expected\n",
1215 GNUNET_i2s (rps_peers[entry->index].peer_id));
1216 if (NULL != cur_test_run.pre_test)
1218 cur_test_run.pre_test (&rps_peers[entry->index],
1219 rps_peers[entry->index].rps_handle);
1220 schedule_missing_requests (&rps_peers[entry->index]);
1223 rps_peers[entry->index].online = GNUNET_YES;
1227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1228 "Invalid value for delta: %i\n", entry->delta);
1232 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1233 rps_peers[entry->index].entry_op_manage = NULL;
1234 GNUNET_free (entry);
1235 //if (num_peers_in_round[current_round] == peers_running)
1240 * @brief Set the rps-service up or down for a specific peer
1242 * TODO use enum instead of 1/-1 for delta
1244 * @param i index of action
1245 * @param j index of peer
1246 * @param delta down (-1) or up (1)
1247 * @param prob_go_on_off the probability of the action
1250 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1251 double prob_go_on_off)
1253 struct OpListEntry *entry;
1256 GNUNET_assert (GNUNET_YES == rps_peers[j].online);
1258 /* make sure that management operation is not already scheduled */
1259 if (NULL != rps_peers[j].entry_op_manage)
1264 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 "%u. selected peer (%u: %s) is %s.\n",
1270 GNUNET_i2s (rps_peers[j].peer_id),
1271 (0 > delta) ? "online" : "offline");
1272 if (prob < prob_go_on_off * UINT32_MAX)
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 GNUNET_i2s (rps_peers[j].peer_id),
1277 (0 > delta) ? "offline" : "online");
1279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1280 "testbed_peers points to %p, peer 0 to %p\n",
1281 testbed_peers, testbed_peers[0]);
1284 cancel_pending_req_rep (&rps_peers[j]);
1285 entry = make_oplist_entry ();
1286 entry->delta = delta;
1288 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1293 (0 > delta) ? 0 : 1);
1295 rps_peers[j].entry_op_manage = entry;
1304 double portion_online;
1305 unsigned int *permut;
1306 double prob_go_offline;
1307 double portion_go_online;
1308 double portion_go_offline;
1310 if (GNUNET_YES == in_shutdown)
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "Churn function executing\n");
1317 churn_task = NULL; /* Should be invalid by now */
1319 /* Compute the probability for an online peer to go offline
1321 portion_online = num_peers_online * 1.0 / num_peers;
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "Portion online: %f\n",
1325 portion_go_online = ((1 - portion_online) * .5 * .66);
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "Portion that should go online: %f\n",
1329 portion_go_offline = (portion_online + portion_go_online) - .75;
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 "Portion that probably goes offline: %f\n",
1332 portion_go_offline);
1333 prob_go_offline = portion_go_offline / (portion_online * .5);
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "Probability of a selected online peer to go offline: %f\n",
1338 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1339 (unsigned int) num_peers);
1341 /* Go over 50% randomly chosen peers */
1342 for (i = 0; i < .5 * num_peers; i++)
1346 /* If online, shut down with certain probability */
1347 if (GNUNET_YES == rps_peers[j].online)
1349 manage_service_wrapper (i, j, -1, prob_go_offline);
1352 /* If offline, restart with certain probability */
1353 else if (GNUNET_NO == rps_peers[j].online)
1355 manage_service_wrapper (i, j, 1, 0.66);
1359 GNUNET_free (permut);
1361 churn_task = GNUNET_SCHEDULER_add_delayed (
1362 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1369 * Initialise given RPSPeer
1371 static void profiler_init_peer (struct RPSPeer *rps_peer)
1373 if (num_peers - 1 == rps_peer->index)
1374 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1379 * Callback to call on receipt of a reply
1381 * @param cls closure
1382 * @param n number of peers
1383 * @param recv_peers the received peers
1386 profiler_reply_handle (void *cls,
1388 const struct GNUNET_PeerIdentity *recv_peers)
1390 struct RPSPeer *rps_peer;
1391 struct RPSPeer *rcv_rps_peer;
1395 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1397 rps_peer = pending_rep->rps_peer;
1398 file_name = "/tmp/rps/received_ids";
1399 file_name_dh = "/tmp/rps/diehard_input";
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401 "[%s] got %" PRIu64 " peers:\n",
1402 GNUNET_i2s (rps_peer->peer_id),
1404 for (i = 0; i < n; i++)
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1409 GNUNET_i2s (&recv_peers[i]));
1412 GNUNET_i2s_full (&recv_peers[i]));
1413 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1414 GNUNET_assert (NULL != rcv_rps_peer);
1415 tofile (file_name_dh,
1417 (uint32_t) rcv_rps_peer->index);
1419 default_reply_handle (cls, n, recv_peers);
1424 profiler_cb (struct RPSPeer *rps_peer)
1426 if (GNUNET_YES == in_shutdown)
1432 if (GNUNET_YES == cur_test_run.have_churn && NULL == churn_task)
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1435 "Starting churn task\n");
1436 churn_task = GNUNET_SCHEDULER_add_delayed (
1437 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Not starting churn task\n");
1445 /* Only request peer ids at one peer.
1446 * (It's the before-last because last one is target of the focussed attack.)
1448 if (eval_peer == rps_peer)
1449 schedule_missing_requests (rps_peer);
1453 * Function called from #profiler_eval with a filename.
1455 * @param cls closure
1456 * @param filename complete filename (absolute path)
1457 * @return #GNUNET_OK to continue to iterate,
1458 * #GNUNET_NO to stop iteration with no error,
1459 * #GNUNET_SYSERR to abort iteration with error!
1462 file_name_cb (void *cls, const char *filename)
1464 if (NULL != strstr (filename, "sampler_el"))
1466 struct RPS_SamplerElement *s_elem;
1467 struct GNUNET_CRYPTO_AuthKey auth_key;
1468 const char *key_char;
1471 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1472 tofile (filename, "--------------------------\n");
1474 auth_key = string_to_auth_key (key_char);
1475 s_elem = RPS_sampler_elem_create ();
1476 RPS_sampler_elem_set (s_elem, auth_key);
1478 for (i = 0; i < num_peers; i++)
1480 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1482 RPS_sampler_elem_destroy (s_elem);
1488 * This is run after the test finished.
1490 * Compute all perfect samples.
1493 profiler_eval (void)
1495 /* Compute perfect sample for each sampler element */
1496 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1505 /***********************************************************************
1506 * /Definition of tests
1507 ***********************************************************************/
1511 * Actual "main" function for the testcase.
1513 * @param cls closure
1514 * @param h the run handle
1515 * @param n_peers number of peers in 'peers'
1516 * @param peers handle to peers run in the testbed
1517 * @param links_succeeded the number of overlay link connection attempts that
1519 * @param links_failed the number of overlay link connection attempts that
1524 struct GNUNET_TESTBED_RunHandle *h,
1525 unsigned int n_peers,
1526 struct GNUNET_TESTBED_Peer **peers,
1527 unsigned int links_succeeded,
1528 unsigned int links_failed)
1531 struct OpListEntry *entry;
1532 uint32_t num_mal_peers;
1534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
1536 /* Check whether we timed out */
1537 if (n_peers != num_peers ||
1539 0 == links_succeeded)
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
1546 GNUNET_SCHEDULER_shutdown ();
1551 /* Initialize peers */
1552 testbed_peers = peers;
1553 num_peers_online = 0;
1554 for (i = 0; i < num_peers; i++)
1556 entry = make_oplist_entry ();
1558 rps_peers[i].index = i;
1559 if (NULL != cur_test_run.init_peer)
1560 cur_test_run.init_peer (&rps_peers[i]);
1561 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1562 GNUNET_TESTBED_PIT_IDENTITY,
1567 /* Bring peers up */
1568 num_mal_peers = round (portion * num_peers);
1569 GNUNET_assert (num_peers == n_peers);
1570 for (i = 0; i < n_peers; i++)
1572 rps_peers[i].index = i;
1573 if ( (rps_peers[i].num_recv_ids < rps_peers[i].num_ids_to_request) ||
1574 (i < num_mal_peers) )
1577 GNUNET_TESTBED_service_connect (&rps_peers[i],
1580 &rps_connect_complete_cb,
1582 &rps_connect_adapter,
1583 &rps_disconnect_adapter,
1588 if (NULL != churn_task)
1589 GNUNET_SCHEDULER_cancel (churn_task);
1590 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1595 * Entry point for the testcase, sets up the testbed.
1597 * @param argc unused
1598 * @param argv unused
1599 * @return 0 on success
1602 main (int argc, char *argv[])
1607 cur_test_run.name = "test-rps-default";
1608 cur_test_run.init_peer = default_init_peer;
1609 cur_test_run.pre_test = NULL;
1610 cur_test_run.reply_handle = default_reply_handle;
1611 cur_test_run.eval_cb = default_eval_cb;
1612 cur_test_run.have_churn = GNUNET_YES;
1614 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1616 if (strstr (argv[0], "malicious") != NULL)
1618 cur_test_run.pre_test = mal_pre;
1619 cur_test_run.main_test = mal_cb;
1620 cur_test_run.init_peer = mal_init_peer;
1622 if (strstr (argv[0], "_1") != NULL)
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1625 cur_test_run.name = "test-rps-malicious_1";
1628 else if (strstr (argv[0], "_2") != NULL)
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1631 cur_test_run.name = "test-rps-malicious_2";
1634 else if (strstr (argv[0], "_3") != NULL)
1636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1637 cur_test_run.name = "test-rps-malicious_3";
1642 else if (strstr (argv[0], "_single_req") != NULL)
1644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1645 cur_test_run.name = "test-rps-single-req";
1646 cur_test_run.main_test = single_req_cb;
1647 cur_test_run.have_churn = GNUNET_NO;
1650 else if (strstr (argv[0], "_delayed_reqs") != NULL)
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1653 cur_test_run.name = "test-rps-delayed-reqs";
1654 cur_test_run.main_test = delay_req_cb;
1655 cur_test_run.have_churn = GNUNET_NO;
1658 else if (strstr (argv[0], "_seed_big") != NULL)
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
1662 cur_test_run.name = "test-rps-seed-big";
1663 cur_test_run.main_test = seed_big_cb;
1664 cur_test_run.eval_cb = no_eval;
1665 cur_test_run.have_churn = GNUNET_NO;
1666 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1669 else if (strstr (argv[0], "_single_peer_seed") != NULL)
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1672 cur_test_run.name = "test-rps-single-peer-seed";
1673 cur_test_run.main_test = single_peer_seed_cb;
1674 cur_test_run.have_churn = GNUNET_NO;
1677 else if (strstr (argv[0], "_seed_request") != NULL)
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1680 cur_test_run.name = "test-rps-seed-request";
1681 cur_test_run.main_test = seed_req_cb;
1682 cur_test_run.have_churn = GNUNET_NO;
1685 else if (strstr (argv[0], "_seed") != NULL)
1687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1688 cur_test_run.name = "test-rps-seed";
1689 cur_test_run.main_test = seed_cb;
1690 cur_test_run.eval_cb = no_eval;
1691 cur_test_run.have_churn = GNUNET_NO;
1694 else if (strstr (argv[0], "_req_cancel") != NULL)
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1697 cur_test_run.name = "test-rps-req-cancel";
1699 cur_test_run.main_test = req_cancel_cb;
1700 cur_test_run.eval_cb = no_eval;
1701 cur_test_run.have_churn = GNUNET_NO;
1702 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1705 else if (strstr (argv[0], "_churn") != NULL)
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
1708 cur_test_run.name = "test-rps-churn";
1710 cur_test_run.init_peer = default_init_peer;
1711 cur_test_run.main_test = churn_test_cb;
1712 cur_test_run.reply_handle = default_reply_handle;
1713 cur_test_run.eval_cb = default_eval_cb;
1714 cur_test_run.have_churn = GNUNET_YES;
1715 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1718 else if (strstr (argv[0], "profiler") != NULL)
1720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1721 cur_test_run.name = "test-rps-profiler";
1724 cur_test_run.init_peer = profiler_init_peer;
1725 cur_test_run.pre_test = mal_pre;
1726 cur_test_run.main_test = profiler_cb;
1727 cur_test_run.reply_handle = profiler_reply_handle;
1728 cur_test_run.eval_cb = profiler_eval;
1729 cur_test_run.request_interval = 2;
1730 cur_test_run.num_requests = 5;
1731 cur_test_run.have_churn = GNUNET_YES;
1732 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1734 /* 'Clean' directory */
1735 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1736 GNUNET_DISK_directory_create ("/tmp/rps/");
1739 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1740 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1741 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1742 if ( (2 == mal_type) ||
1744 target_peer = &rps_peer_ids[num_peers - 2];
1745 if (profiler_eval == cur_test_run.eval_cb)
1746 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
1747 malicious peer if not careful
1748 with the malicious portion */
1751 ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
1756 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1757 "_test_run returned.\n");
1758 if (GNUNET_OK != ret_value)
1760 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1761 "Test did not run successfully!\n");
1764 ret_value = cur_test_run.eval_cb();
1765 GNUNET_free (rps_peers);
1766 GNUNET_free (rps_peer_ids);
1767 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1771 /* end of test_rps.c */