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 (0 > 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;
1226 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1227 rps_peers[entry->index].entry_op_manage = NULL;
1228 GNUNET_free (entry);
1229 //if (num_peers_in_round[current_round] == peers_running)
1234 * @brief Set the rps-service up or down for a specific peer
1236 * TODO use enum instead of 1/-1 for delta
1238 * @param i index of action
1239 * @param j index of peer
1240 * @param delta down (-1) or up (1)
1241 * @param prob_go_on_off the probability of the action
1244 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1245 double prob_go_on_off)
1247 struct OpListEntry *entry;
1250 GNUNET_assert (GNUNET_YES == rps_peers[j].online);
1252 /* make sure that management operation is not already scheduled */
1253 if (NULL != rps_peers[j].entry_op_manage)
1258 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "%u. selected peer (%u: %s) is %s.\n",
1264 GNUNET_i2s (rps_peers[j].peer_id),
1265 (0 > delta) ? "online" : "offline");
1266 if (prob < prob_go_on_off * UINT32_MAX)
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 GNUNET_i2s (rps_peers[j].peer_id),
1271 (0 > delta) ? "offline" : "online");
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274 "testbed_peers points to %p, peer 0 to %p\n",
1275 testbed_peers, testbed_peers[0]);
1278 cancel_pending_req_rep (&rps_peers[j]);
1279 entry = make_oplist_entry ();
1280 entry->delta = delta;
1282 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1287 (0 > delta) ? 0 : 1);
1289 rps_peers[j].entry_op_manage = entry;
1298 double portion_online;
1299 unsigned int *permut;
1300 double prob_go_offline;
1301 double portion_go_online;
1302 double portion_go_offline;
1304 if (GNUNET_YES == in_shutdown)
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309 "Churn function executing\n");
1311 churn_task = NULL; /* Should be invalid by now */
1313 /* Compute the probability for an online peer to go offline
1315 portion_online = num_peers_online * 1.0 / num_peers;
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 "Portion online: %f\n",
1319 portion_go_online = ((1 - portion_online) * .5 * .66);
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1321 "Portion that should go online: %f\n",
1323 portion_go_offline = (portion_online + portion_go_online) - .75;
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Portion that probably goes offline: %f\n",
1326 portion_go_offline);
1327 prob_go_offline = portion_go_offline / (portion_online * .5);
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329 "Probability of a selected online peer to go offline: %f\n",
1332 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1333 (unsigned int) num_peers);
1335 /* Go over 50% randomly chosen peers */
1336 for (i = 0; i < .5 * num_peers; i++)
1340 /* If online, shut down with certain probability */
1341 if (GNUNET_YES == rps_peers[j].online)
1343 manage_service_wrapper (i, j, -1, prob_go_offline);
1346 /* If offline, restart with certain probability */
1347 else if (GNUNET_NO == rps_peers[j].online)
1349 manage_service_wrapper (i, j, 1, 0.66);
1353 GNUNET_free (permut);
1355 churn_task = GNUNET_SCHEDULER_add_delayed (
1356 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1363 * Initialise given RPSPeer
1365 static void profiler_init_peer (struct RPSPeer *rps_peer)
1367 if (num_peers - 1 == rps_peer->index)
1368 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1373 * Callback to call on receipt of a reply
1375 * @param cls closure
1376 * @param n number of peers
1377 * @param recv_peers the received peers
1380 profiler_reply_handle (void *cls,
1382 const struct GNUNET_PeerIdentity *recv_peers)
1384 struct RPSPeer *rps_peer;
1385 struct RPSPeer *rcv_rps_peer;
1389 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1391 rps_peer = pending_rep->rps_peer;
1392 file_name = "/tmp/rps/received_ids";
1393 file_name_dh = "/tmp/rps/diehard_input";
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "[%s] got %" PRIu64 " peers:\n",
1396 GNUNET_i2s (rps_peer->peer_id),
1398 for (i = 0; i < n; i++)
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 GNUNET_i2s (&recv_peers[i]));
1406 GNUNET_i2s_full (&recv_peers[i]));
1407 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1408 GNUNET_assert (NULL != rcv_rps_peer);
1409 tofile (file_name_dh,
1411 (uint32_t) rcv_rps_peer->index);
1413 default_reply_handle (cls, n, recv_peers);
1418 profiler_cb (struct RPSPeer *rps_peer)
1420 if (GNUNET_YES == in_shutdown)
1426 if (GNUNET_YES == cur_test_run.have_churn && NULL == churn_task)
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Starting churn task\n");
1430 churn_task = GNUNET_SCHEDULER_add_delayed (
1431 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1436 "Not starting churn task\n");
1439 /* Only request peer ids at one peer.
1440 * (It's the before-last because last one is target of the focussed attack.)
1442 if (eval_peer == rps_peer)
1443 schedule_missing_requests (rps_peer);
1447 * Function called from #profiler_eval with a filename.
1449 * @param cls closure
1450 * @param filename complete filename (absolute path)
1451 * @return #GNUNET_OK to continue to iterate,
1452 * #GNUNET_NO to stop iteration with no error,
1453 * #GNUNET_SYSERR to abort iteration with error!
1456 file_name_cb (void *cls, const char *filename)
1458 if (NULL != strstr (filename, "sampler_el"))
1460 struct RPS_SamplerElement *s_elem;
1461 struct GNUNET_CRYPTO_AuthKey auth_key;
1462 const char *key_char;
1465 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1466 tofile (filename, "--------------------------\n");
1468 auth_key = string_to_auth_key (key_char);
1469 s_elem = RPS_sampler_elem_create ();
1470 RPS_sampler_elem_set (s_elem, auth_key);
1472 for (i = 0; i < num_peers; i++)
1474 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1476 RPS_sampler_elem_destroy (s_elem);
1482 * This is run after the test finished.
1484 * Compute all perfect samples.
1487 profiler_eval (void)
1489 /* Compute perfect sample for each sampler element */
1490 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1499 /***********************************************************************
1500 * /Definition of tests
1501 ***********************************************************************/
1505 * Actual "main" function for the testcase.
1507 * @param cls closure
1508 * @param h the run handle
1509 * @param n_peers number of peers in 'peers'
1510 * @param peers handle to peers run in the testbed
1511 * @param links_succeeded the number of overlay link connection attempts that
1513 * @param links_failed the number of overlay link connection attempts that
1518 struct GNUNET_TESTBED_RunHandle *h,
1519 unsigned int n_peers,
1520 struct GNUNET_TESTBED_Peer **peers,
1521 unsigned int links_succeeded,
1522 unsigned int links_failed)
1525 struct OpListEntry *entry;
1526 uint32_t num_mal_peers;
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
1530 /* Check whether we timed out */
1531 if (n_peers != num_peers ||
1533 0 == links_succeeded)
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
1537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
1540 GNUNET_SCHEDULER_shutdown ();
1545 /* Initialize peers */
1546 testbed_peers = peers;
1547 num_peers_online = 0;
1548 for (i = 0; i < num_peers; i++)
1550 entry = make_oplist_entry ();
1552 rps_peers[i].index = i;
1553 if (NULL != cur_test_run.init_peer)
1554 cur_test_run.init_peer (&rps_peers[i]);
1555 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1556 GNUNET_TESTBED_PIT_IDENTITY,
1561 /* Bring peers up */
1562 num_mal_peers = round (portion * num_peers);
1563 GNUNET_assert (num_peers == n_peers);
1564 for (i = 0; i < n_peers; i++)
1566 rps_peers[i].index = i;
1567 if ( (rps_peers[i].num_recv_ids < rps_peers[i].num_ids_to_request) ||
1568 (i < num_mal_peers) )
1571 GNUNET_TESTBED_service_connect (&rps_peers[i],
1574 &rps_connect_complete_cb,
1576 &rps_connect_adapter,
1577 &rps_disconnect_adapter,
1582 if (NULL != churn_task)
1583 GNUNET_SCHEDULER_cancel (churn_task);
1584 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1589 * Entry point for the testcase, sets up the testbed.
1591 * @param argc unused
1592 * @param argv unused
1593 * @return 0 on success
1596 main (int argc, char *argv[])
1601 cur_test_run.name = "test-rps-default";
1602 cur_test_run.init_peer = default_init_peer;
1603 cur_test_run.pre_test = NULL;
1604 cur_test_run.reply_handle = default_reply_handle;
1605 cur_test_run.eval_cb = default_eval_cb;
1606 cur_test_run.have_churn = GNUNET_YES;
1608 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1610 if (strstr (argv[0], "malicious") != NULL)
1612 cur_test_run.pre_test = mal_pre;
1613 cur_test_run.main_test = mal_cb;
1614 cur_test_run.init_peer = mal_init_peer;
1616 if (strstr (argv[0], "_1") != NULL)
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1619 cur_test_run.name = "test-rps-malicious_1";
1622 else if (strstr (argv[0], "_2") != NULL)
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1625 cur_test_run.name = "test-rps-malicious_2";
1628 else if (strstr (argv[0], "_3") != NULL)
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1631 cur_test_run.name = "test-rps-malicious_3";
1636 else if (strstr (argv[0], "_single_req") != NULL)
1638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1639 cur_test_run.name = "test-rps-single-req";
1640 cur_test_run.main_test = single_req_cb;
1641 cur_test_run.have_churn = GNUNET_NO;
1644 else if (strstr (argv[0], "_delayed_reqs") != NULL)
1646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1647 cur_test_run.name = "test-rps-delayed-reqs";
1648 cur_test_run.main_test = delay_req_cb;
1649 cur_test_run.have_churn = GNUNET_NO;
1652 else if (strstr (argv[0], "_seed_big") != NULL)
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
1656 cur_test_run.name = "test-rps-seed-big";
1657 cur_test_run.main_test = seed_big_cb;
1658 cur_test_run.eval_cb = no_eval;
1659 cur_test_run.have_churn = GNUNET_NO;
1660 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1663 else if (strstr (argv[0], "_single_peer_seed") != NULL)
1665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1666 cur_test_run.name = "test-rps-single-peer-seed";
1667 cur_test_run.main_test = single_peer_seed_cb;
1668 cur_test_run.have_churn = GNUNET_NO;
1671 else if (strstr (argv[0], "_seed_request") != NULL)
1673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1674 cur_test_run.name = "test-rps-seed-request";
1675 cur_test_run.main_test = seed_req_cb;
1676 cur_test_run.have_churn = GNUNET_NO;
1679 else if (strstr (argv[0], "_seed") != NULL)
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1682 cur_test_run.name = "test-rps-seed";
1683 cur_test_run.main_test = seed_cb;
1684 cur_test_run.eval_cb = no_eval;
1685 cur_test_run.have_churn = GNUNET_NO;
1688 else if (strstr (argv[0], "_req_cancel") != NULL)
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1691 cur_test_run.name = "test-rps-req-cancel";
1693 cur_test_run.main_test = req_cancel_cb;
1694 cur_test_run.eval_cb = no_eval;
1695 cur_test_run.have_churn = GNUNET_NO;
1696 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1699 else if (strstr (argv[0], "_churn") != NULL)
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
1702 cur_test_run.name = "test-rps-churn";
1704 cur_test_run.init_peer = default_init_peer;
1705 cur_test_run.main_test = churn_test_cb;
1706 cur_test_run.reply_handle = default_reply_handle;
1707 cur_test_run.eval_cb = default_eval_cb;
1708 cur_test_run.have_churn = GNUNET_YES;
1709 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1712 else if (strstr (argv[0], "profiler") != NULL)
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1715 cur_test_run.name = "test-rps-profiler";
1718 cur_test_run.init_peer = profiler_init_peer;
1719 cur_test_run.pre_test = mal_pre;
1720 cur_test_run.main_test = profiler_cb;
1721 cur_test_run.reply_handle = profiler_reply_handle;
1722 cur_test_run.eval_cb = profiler_eval;
1723 cur_test_run.request_interval = 2;
1724 cur_test_run.num_requests = 5;
1725 cur_test_run.have_churn = GNUNET_YES;
1726 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1728 /* 'Clean' directory */
1729 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1730 GNUNET_DISK_directory_create ("/tmp/rps/");
1733 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1734 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1735 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1736 if ( (2 == mal_type) ||
1738 target_peer = &rps_peer_ids[num_peers - 2];
1739 if (profiler_eval == cur_test_run.eval_cb)
1740 eval_peer = &rps_peers[num_peers - 1]; /* FIXME: eval_peer could be a
1741 malicious peer if not careful
1742 with the malicious portion */
1745 ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
1750 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1751 "_test_run returned.\n");
1752 if (GNUNET_OK != ret_value)
1754 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1755 "Test did not run successfully!\n");
1758 ret_value = cur_test_run.eval_cb();
1759 GNUNET_free (rps_peers);
1760 GNUNET_free (rps_peer_ids);
1761 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1765 /* end of test_rps.c */