2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file rps/test_rps_multipeer.c
22 * @brief Testcase for the random peer sampling service. Starts
23 * a peergroup with a given number of peers, then waits to
24 * receive size pushes/pulls from each peer. Expects to wait
25 * for one message from each peer.
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
31 #include "gnunet_rps_service.h"
32 #include "rps-test_util.h"
33 #include "gnunet-service-rps_sampler_elem.h"
39 * How many peers do we start?
44 * How long do we run the test?
46 //#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 static struct GNUNET_TIME_Relative timeout;
51 * Portion of malicious peers
53 static double portion = .1;
56 * Type of malicious peer to test
58 static unsigned int mal_type = 0;
61 * Handles to all of the running peers
63 static struct GNUNET_TESTBED_Peer **testbed_peers;
74 struct OpListEntry *next;
79 struct OpListEntry *prev;
82 * The testbed operation
84 struct GNUNET_TESTBED_Operation *op;
87 * Depending on whether we start or stop NSE service at the peer set this to 1
93 * Index of the regarding peer
101 static struct OpListEntry *oplist_head;
106 static struct OpListEntry *oplist_tail;
110 * A pending reply: A request was sent and the reply is pending.
117 struct PendingReply *next;
118 struct PendingReply *prev;
121 * Handle to the request we are waiting for
123 struct GNUNET_RPS_Request_Handle *req_handle;
126 * The peer that requested
128 struct RPSPeer *rps_peer;
133 * A pending request: A request was not made yet but is scheduled for later.
135 struct PendingRequest
140 struct PendingRequest *next;
141 struct PendingRequest *prev;
144 * Handle to the request we are waiting for
146 struct GNUNET_SCHEDULER_Task *request_task;
149 * The peer that requested
151 struct RPSPeer *rps_peer;
156 * Information we track for each peer.
166 * Handle for RPS connect operation.
168 struct GNUNET_TESTBED_Operation *op;
171 * Handle to RPS service.
173 struct GNUNET_RPS_Handle *rps_handle;
178 struct GNUNET_PeerIdentity *peer_id;
181 * A request handle to check for an request
183 //struct GNUNET_RPS_Request_Handle *req_handle;
186 * Peer on- or offline?
191 * Number of Peer IDs to request
193 unsigned int num_ids_to_request;
196 * Pending requests DLL
198 struct PendingRequest *pending_req_head;
199 struct PendingRequest *pending_req_tail;
202 * Number of pending requests
204 unsigned int num_pending_reqs;
207 * Pending replies DLL
209 struct PendingReply *pending_rep_head;
210 struct PendingReply *pending_rep_tail;
213 * Number of pending replies
215 unsigned int num_pending_reps;
220 struct GNUNET_PeerIdentity *rec_ids;
223 * Number of received PeerIDs
225 unsigned int num_rec_ids;
230 * Information for all the peers.
232 static struct RPSPeer *rps_peers;
235 * Peermap to get the index of a given peer ID quick.
237 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
242 static struct GNUNET_PeerIdentity *rps_peer_ids;
245 * ID of the targeted peer.
247 static struct GNUNET_PeerIdentity *target_peer;
250 * ID of the peer that requests for the evaluation.
252 static struct RPSPeer *eval_peer;
255 * Number of online peers.
257 static unsigned int num_peers_online;
260 * Return value from 'main'.
266 * Identifier for the churn task that runs periodically
268 static struct GNUNET_SCHEDULER_Task *churn_task;
271 * Identifier for the churn task that runs periodically
273 static struct GNUNET_SCHEDULER_Task *shutdown_task;
277 * Called to initialise the given RPSPeer
279 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
282 * Called directly after connecting to the service
284 typedef void (*PreTest) (void *cls, struct GNUNET_RPS_Handle *h);
287 * Called from within #rps_connect_complete_cb ()
288 * Executes functions to test the api/service
290 typedef void (*MainTest) (struct RPSPeer *rps_peer);
293 * Callback called once the requested random peers are available
295 typedef void (*ReplyHandle) (void *cls,
297 const struct GNUNET_PeerIdentity *recv_peers);
300 * Called directly before disconnecting from the service
302 typedef void (*PostTest) (void *cls, struct GNUNET_RPS_Handle *h);
305 * Function called after disconnect to evaluate test success
307 typedef int (*EvaluationCallback) (void);
311 * Structure to define a single test
321 * Called to initialise peer
326 * Called directly after connecting to the service
331 * Function to execute the functions to be tested
336 * Callback called once the requested peers are available
338 ReplyHandle reply_handle;
341 * Called directly before disconnecting from the service
346 * Function to evaluate the test results
348 EvaluationCallback eval_cb;
353 uint32_t request_interval;
356 * Number of Requests to make.
358 uint32_t num_requests;
362 * Are we shutting down?
364 static int in_shutdown;
367 * Append arguments to file
370 tofile_ (const char *file_name, char *line)
372 struct GNUNET_DISK_FileHandle *f;
373 /* char output_buffer[512]; */
378 if (NULL == (f = GNUNET_DISK_file_open (file_name,
379 GNUNET_DISK_OPEN_APPEND |
380 GNUNET_DISK_OPEN_WRITE |
381 GNUNET_DISK_OPEN_CREATE,
382 GNUNET_DISK_PERM_USER_READ |
383 GNUNET_DISK_PERM_USER_WRITE |
384 GNUNET_DISK_PERM_GROUP_READ |
385 GNUNET_DISK_PERM_OTHER_READ)))
387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
388 "Not able to open file %s\n",
392 /* size = GNUNET_snprintf (output_buffer,
393 sizeof (output_buffer),
395 GNUNET_TIME_absolute_get ().abs_value_us,
399 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
400 "Failed to write string to buffer (size: %i)\n",
405 size = strlen (line) * sizeof (char);
407 size2 = GNUNET_DISK_file_write (f, line, size);
410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
411 "Unable to write to file! (Size: %u, size2: %u)\n",
417 if (GNUNET_YES != GNUNET_DISK_file_close (f))
418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
419 "Unable to close file\n");
423 * This function is used to facilitate writing important information to disk
425 #define tofile(file_name, ...) do {\
428 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
431 "Failed to create tmp_buf\n");\
433 tofile_(file_name,tmp_buf);\
438 * Write the ids and their according index in the given array to a file
442 ids_to_file (char *file_name,
443 struct GNUNET_PeerIdentity *peer_ids,
444 unsigned int num_peer_ids)
448 for (i=0 ; i < num_peer_ids ; i++)
453 GNUNET_i2s_full (&peer_ids[i]));
458 * Test the success of a single test
468 for (i = 0; i < num_peers; i++)
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
473 GNUNET_i2s (rps_peers[i].peer_id),
474 rps_peers[i].num_rec_ids,
475 rps_peers[i].num_ids_to_request,
476 (rps_peers[i].num_ids_to_request == rps_peers[i].num_rec_ids));
477 tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_rec_ids);
479 return tmp_ok? 0 : 1;
484 * Creates an oplist entry and adds it to the oplist DLL
486 static struct OpListEntry *
489 struct OpListEntry *entry;
491 entry = GNUNET_new (struct OpListEntry);
492 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
498 * Task run on timeout to shut everything down.
501 shutdown_op (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
505 in_shutdown = GNUNET_YES;
506 if (NULL != churn_task)
507 GNUNET_SCHEDULER_cancel (churn_task);
509 for (i = 0 ; i < num_peers ; i++)
510 GNUNET_TESTBED_operation_done (rps_peers[i].op);
511 GNUNET_SCHEDULER_shutdown ();
519 seed_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
522 struct RPSPeer *peer = (struct RPSPeer *) cls;
525 // TODO if malicious don't seed mal peers
526 amount = round (.5 * num_peers);
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
529 for (i = 0 ; i < amount ; i++)
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
532 GNUNET_i2s (&rps_peer_ids[i]));
534 GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
539 * Get the id of peer i.
542 info_cb (void *cb_cls,
543 struct GNUNET_TESTBED_Operation *op,
544 const struct GNUNET_TESTBED_PeerInformation *pinfo,
547 struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
549 if (NULL == pinfo || NULL != emsg)
551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558 GNUNET_i2s (pinfo->result.id));
560 rps_peer_ids[entry->index] = *(pinfo->result.id);
561 rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
562 rps_peers[entry->index].rec_ids = NULL;
563 rps_peers[entry->index].num_rec_ids = 0;
565 GNUNET_CONTAINER_multipeermap_put (peer_map,
566 &rps_peer_ids[entry->index],
567 &rps_peers[entry->index],
568 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
569 tofile ("/tmp/rps/peer_ids",
572 GNUNET_i2s_full (&rps_peer_ids[entry->index]));
574 if (NULL != cur_test_run.init_peer)
575 cur_test_run.init_peer (&rps_peers[entry->index]);
577 GNUNET_TESTBED_operation_done (entry->op);
578 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
584 * Callback to be called when RPS service connect operation is completed
586 * @param cls the callback closure from functions generating an operation
587 * @param op the operation that has been finished
588 * @param ca_result the RPS service handle returned from rps_connect_adapter
589 * @param emsg error message in case the operation has failed; will be NULL if
590 * operation has executed successfully.
593 rps_connect_complete_cb (void *cls,
594 struct GNUNET_TESTBED_Operation *op,
598 struct RPSPeer *rps_peer = cls;
599 struct GNUNET_RPS_Handle *rps = ca_result;
601 rps_peer->rps_handle = rps;
602 rps_peer->online = GNUNET_YES;
605 GNUNET_assert (op == rps_peer->op);
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 "Failed to connect to RPS service: %s\n",
612 GNUNET_SCHEDULER_shutdown ();
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
618 cur_test_run.main_test (rps_peer);
623 * Adapter function called to establish a connection to
627 * @param cfg configuration of the peer to connect to; will be available until
628 * GNUNET_TESTBED_operation_done() is called on the operation returned
629 * from GNUNET_TESTBED_service_connect()
630 * @return service handle to return in 'op_result', NULL on error
633 rps_connect_adapter (void *cls,
634 const struct GNUNET_CONFIGURATION_Handle *cfg)
636 struct GNUNET_RPS_Handle *h;
638 h = GNUNET_RPS_connect (cfg);
640 if (NULL != cur_test_run.pre_test)
641 cur_test_run.pre_test (cls, h);
648 * Adapter function called to destroy connection to
652 * @param op_result service handle returned from the connect adapter
655 rps_disconnect_adapter (void *cls,
658 struct RPSPeer *peer = cls;
659 struct GNUNET_RPS_Handle *h = op_result;
660 GNUNET_assert (NULL != peer);
661 GNUNET_RPS_disconnect (h);
662 peer->rps_handle = NULL;
666 /***********************************************************************
667 * Definition of tests
668 ***********************************************************************/
670 // TODO check whether tests can be stopped earlier
672 default_eval_cb (void)
684 * Initialise given RPSPeer
686 static void default_init_peer (struct RPSPeer *rps_peer)
688 rps_peer->num_ids_to_request = 1;
692 * Callback to call on receipt of a reply
695 * @param n number of peers
696 * @param recv_peers the received peers
699 default_reply_handle (void *cls,
701 const struct GNUNET_PeerIdentity *recv_peers)
703 struct RPSPeer *rps_peer;
704 struct PendingReply *pending_rep = (struct PendingReply *) cls;
707 rps_peer = pending_rep->rps_peer;
708 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
709 rps_peer->pending_rep_tail,
711 rps_peer->num_pending_reps--;
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
713 "[%s] got %" PRIu64 " peers:\n",
714 GNUNET_i2s (rps_peer->peer_id),
717 for (i = 0; i < n; i++)
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 GNUNET_i2s (&recv_peers[i]));
724 /* GNUNET_array_append (rps_peer->rec_ids, rps_peer->num_rec_ids, recv_peers[i]); */
725 rps_peer->num_rec_ids++;
728 if (0 == evaluate ())
730 GNUNET_SCHEDULER_cancel (shutdown_task);
731 shutdown_task = GNUNET_SCHEDULER_add_now (&shutdown_op, NULL);
736 * Request random peers.
739 request_peers (void *cls,
740 const struct GNUNET_SCHEDULER_TaskContext *tc)
742 struct RPSPeer *rps_peer;
743 struct PendingRequest *pending_req = (struct PendingRequest *) cls;
744 struct PendingReply *pending_rep;
746 if (GNUNET_YES == in_shutdown)
748 rps_peer = pending_req->rps_peer;
749 GNUNET_assert (1 <= rps_peer->num_pending_reqs);
750 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
751 rps_peer->pending_req_tail,
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "Requesting one peer\n");
755 pending_rep = GNUNET_new (struct PendingReply);
756 pending_rep->rps_peer = rps_peer;
757 pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
759 cur_test_run.reply_handle,
761 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
762 rps_peer->pending_rep_tail,
764 rps_peer->num_pending_reps++;
765 rps_peer->num_pending_reqs--;
769 cancel_pending_req (struct PendingRequest *pending_req)
771 struct RPSPeer *rps_peer;
773 rps_peer = pending_req->rps_peer;
774 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
775 rps_peer->pending_req_tail,
777 rps_peer->num_pending_reqs--;
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Cancelling pending request\n");
780 GNUNET_SCHEDULER_cancel (pending_req->request_task);
781 GNUNET_free (pending_req);
785 cancel_request (struct PendingReply *pending_rep)
787 struct RPSPeer *rps_peer;
789 rps_peer = pending_rep->rps_peer;
790 GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
791 rps_peer->pending_rep_tail,
793 rps_peer->num_pending_reps--;
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
795 "Cancelling request\n");
796 GNUNET_RPS_request_cancel (pending_rep->req_handle);
797 GNUNET_free (pending_rep);
804 cancel_request_cb (void *cls,
805 const struct GNUNET_SCHEDULER_TaskContext *tc)
807 struct PendingReply *pending_rep;
808 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
810 if (GNUNET_YES == in_shutdown)
812 pending_rep = rps_peer->pending_rep_head;
813 GNUNET_assert (1 <= rps_peer->num_pending_reps);
814 cancel_request (pending_rep);
819 * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
820 * issued, nor replied
823 schedule_missing_requests (struct RPSPeer *rps_peer)
826 struct PendingRequest *pending_req;
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Scheduling %u - %u missing requests\n",
830 rps_peer->num_ids_to_request,
831 rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
832 GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
833 rps_peer->num_ids_to_request);
834 for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
835 i < rps_peer->num_ids_to_request; i++)
837 pending_req = GNUNET_new (struct PendingRequest);
838 pending_req->rps_peer = rps_peer;
839 pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
840 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
841 cur_test_run.request_interval * i),
844 GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
845 rps_peer->pending_req_tail,
847 rps_peer->num_pending_reqs++;
852 cancel_pending_req_rep (struct RPSPeer *rps_peer)
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "Cancelling all (pending) requests.\n");
856 while (NULL != rps_peer->pending_req_head)
857 cancel_pending_req (rps_peer->pending_req_head);
858 GNUNET_assert (0 == rps_peer->num_pending_reqs);
859 while (NULL != rps_peer->pending_rep_head)
860 cancel_request (rps_peer->pending_rep_head);
861 GNUNET_assert (0 == rps_peer->num_pending_reps);
864 /***********************************
866 ***********************************/
869 * Initialise only non-mal RPSPeers
871 static void mal_init_peer (struct RPSPeer *rps_peer)
873 if (rps_peer->index >= round (portion * num_peers))
874 rps_peer->num_ids_to_request = 1;
878 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
880 #ifdef ENABLE_MALICIOUS
881 uint32_t num_mal_peers;
882 struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
884 GNUNET_assert ( (1 >= portion) &&
886 num_mal_peers = round (portion * num_peers);
888 if (rps_peer->index < num_mal_peers)
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
893 GNUNET_i2s (rps_peer->peer_id),
896 GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
897 rps_peer_ids, target_peer);
899 #endif /* ENABLE_MALICIOUS */
903 mal_cb (struct RPSPeer *rps_peer)
905 uint32_t num_mal_peers;
907 #ifdef ENABLE_MALICIOUS
908 GNUNET_assert ( (1 >= portion) &&
910 num_mal_peers = round (portion * num_peers);
912 if (rps_peer->index >= num_mal_peers)
913 { /* It's useless to ask a malicious peer about a random sample -
915 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
916 seed_peers, rps_peer);
917 schedule_missing_requests (rps_peer);
919 #endif /* ENABLE_MALICIOUS */
923 /***********************************
925 ***********************************/
927 single_req_cb (struct RPSPeer *rps_peer)
929 schedule_missing_requests (rps_peer);
932 /***********************************
934 ***********************************/
936 delay_req_cb (struct RPSPeer *rps_peer)
938 schedule_missing_requests (rps_peer);
941 /***********************************
943 ***********************************/
945 seed_cb (struct RPSPeer *rps_peer)
947 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
948 seed_peers, rps_peer);
951 /***********************************
953 ***********************************/
955 seed_big_cb (struct RPSPeer *rps_peer)
957 // TODO test seeding > GNUNET_SERVER_MAX_MESSAGE_SIZE peers
960 /***********************************
962 ***********************************/
964 single_peer_seed_cb (struct RPSPeer *rps_peer)
969 /***********************************
971 ***********************************/
973 seed_req_cb (struct RPSPeer *rps_peer)
975 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
976 seed_peers, rps_peer);
977 schedule_missing_requests (rps_peer);
982 /***********************************
984 ***********************************/
986 req_cancel_cb (struct RPSPeer *rps_peer)
988 schedule_missing_requests (rps_peer);
989 GNUNET_SCHEDULER_add_delayed (
990 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
991 (cur_test_run.request_interval + 1)),
992 cancel_request_cb, rps_peer);
995 /***********************************
997 ***********************************/
1000 * Callback to be called when RPS service is started or stopped at peers
1003 * @param op the operation handle
1004 * @param emsg NULL on success; otherwise an error description
1007 churn_cb (void *cls,
1008 struct GNUNET_TESTBED_Operation *op,
1012 struct OpListEntry *entry = cls;
1014 GNUNET_TESTBED_operation_done (entry->op);
1017 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1018 GNUNET_SCHEDULER_shutdown ();
1021 GNUNET_assert (0 != entry->delta);
1023 num_peers_online += entry->delta;
1025 if (0 > entry->delta)
1026 { /* Peer hopefully just went offline */
1027 if (GNUNET_YES != rps_peers[entry->index].online)
1029 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1030 "peer %s was expected to go offline but is still marked as online\n",
1031 GNUNET_i2s (rps_peers[entry->index].peer_id));
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "peer %s probably went offline as expected\n",
1038 GNUNET_i2s (rps_peers[entry->index].peer_id));
1040 rps_peers[entry->index].online = GNUNET_NO;
1043 else if (0 < entry->delta)
1044 { /* Peer hopefully just went online */
1045 if (GNUNET_NO != rps_peers[entry->index].online)
1047 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1048 "peer %s was expected to go online but is still marked as offline\n",
1049 GNUNET_i2s (rps_peers[entry->index].peer_id));
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "peer %s probably went online as expected\n",
1056 GNUNET_i2s (rps_peers[entry->index].peer_id));
1057 if (NULL != cur_test_run.pre_test)
1059 cur_test_run.pre_test (&rps_peers[entry->index],
1060 rps_peers[entry->index].rps_handle);
1061 schedule_missing_requests (&rps_peers[entry->index]);
1064 rps_peers[entry->index].online = GNUNET_YES;
1067 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1068 GNUNET_free (entry);
1069 //if (num_peers_in_round[current_round] == peers_running)
1074 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1075 double prob_go_on_off)
1077 struct OpListEntry *entry;
1080 prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 "%u. selected peer (%u: %s) is %s.\n",
1086 GNUNET_i2s (rps_peers[j].peer_id),
1087 (0 > delta) ? "online" : "offline");
1088 if (prob < prob_go_on_off * UINT32_MAX)
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 GNUNET_i2s (rps_peers[j].peer_id),
1093 (0 > delta) ? "offline" : "online");
1096 cancel_pending_req_rep (&rps_peers[j]);
1097 entry = make_oplist_entry ();
1098 entry->delta = delta;
1100 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1105 (0 > delta) ? 0 : 1);
1110 churn (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1114 double portion_online;
1115 unsigned int *permut;
1116 double prob_go_offline;
1117 double portion_go_online;
1118 double portion_go_offline;
1120 /* Compute the probability for an online peer to go offline
1122 portion_online = num_peers_online * 1.0 / num_peers;
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "Portion online: %f\n",
1126 portion_go_online = ((1 - portion_online) * .5 * .66);
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1128 "Portion that should go online: %f\n",
1130 portion_go_offline = (portion_online + portion_go_online) - .75;
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Portion that probably goes offline: %f\n",
1133 portion_go_offline);
1134 prob_go_offline = portion_go_offline / (portion_online * .5);
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 "Probability of a selected online peer to go offline: %f\n",
1139 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1140 (unsigned int) num_peers);
1142 /* Go over 50% randomly chosen peers */
1143 for (i = 0; i < .5 * num_peers; i++)
1147 /* If online, shut down with certain probability */
1148 if (GNUNET_YES == rps_peers[j].online)
1150 manage_service_wrapper (i, j, -1, prob_go_offline);
1153 /* If offline, restart with certain probability */
1154 else if (GNUNET_NO == rps_peers[j].online)
1156 manage_service_wrapper (i, j, 1, 0.66);
1160 GNUNET_free (permut);
1162 churn_task = GNUNET_SCHEDULER_add_delayed (
1163 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1170 * Initialise given RPSPeer
1172 static void profiler_init_peer (struct RPSPeer *rps_peer)
1174 if (num_peers - 1 == rps_peer->index)
1175 rps_peer->num_ids_to_request = cur_test_run.num_requests;
1180 * Callback to call on receipt of a reply
1182 * @param cls closure
1183 * @param n number of peers
1184 * @param recv_peers the received peers
1187 profiler_reply_handle (void *cls,
1189 const struct GNUNET_PeerIdentity *recv_peers)
1191 struct RPSPeer *rps_peer;
1192 struct RPSPeer *rcv_rps_peer;
1196 struct PendingReply *pending_rep = (struct PendingReply *) cls;
1198 rps_peer = pending_rep->rps_peer;
1199 file_name = "/tmp/rps/received_ids";
1200 file_name_dh = "/tmp/rps/diehard_input";
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202 "[%s] got %" PRIu64 " peers:\n",
1203 GNUNET_i2s (rps_peer->peer_id),
1205 for (i = 0; i < n; i++)
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210 GNUNET_i2s (&recv_peers[i]));
1213 GNUNET_i2s_full (&recv_peers[i]));
1214 rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1215 tofile (file_name_dh,
1217 (uint32_t) rcv_rps_peer->index);
1219 default_reply_handle (cls, n, recv_peers);
1224 profiler_cb (struct RPSPeer *rps_peer)
1227 if (NULL == churn_task)
1229 churn_task = GNUNET_SCHEDULER_add_delayed (
1230 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1235 /* Only request peer ids at one peer.
1236 * (It's the before-last because last one is target of the focussed attack.)
1238 if (eval_peer == rps_peer)
1239 schedule_missing_requests (rps_peer);
1243 * Function called from #profiler_eval with a filename.
1245 * @param cls closure
1246 * @param filename complete filename (absolute path)
1247 * @return #GNUNET_OK to continue to iterate,
1248 * #GNUNET_NO to stop iteration with no error,
1249 * #GNUNET_SYSERR to abort iteration with error!
1252 file_name_cb (void *cls, const char *filename)
1254 if (NULL != strstr (filename, "sampler_el"))
1256 struct RPS_SamplerElement *s_elem;
1257 struct GNUNET_CRYPTO_AuthKey auth_key;
1258 const char *key_char;
1261 key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1262 tofile (filename, "--------------------------\n");
1264 auth_key = string_to_auth_key (key_char);
1265 s_elem = RPS_sampler_elem_create ();
1266 RPS_sampler_elem_set (s_elem, auth_key);
1268 for (i = 0; i < num_peers; i++)
1270 RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1277 * This is run after the test finished.
1279 * Compute all perfect samples.
1282 profiler_eval (void)
1284 /* Compute perfect sample for each sampler element */
1285 if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1294 /***********************************************************************
1295 * /Definition of tests
1296 ***********************************************************************/
1300 * Actual "main" function for the testcase.
1302 * @param cls closure
1303 * @param h the run handle
1304 * @param n_peers number of peers in 'peers'
1305 * @param peers handle to peers run in the testbed
1306 * @param links_succeeded the number of overlay link connection attempts that
1308 * @param links_failed the number of overlay link connection attempts that
1313 struct GNUNET_TESTBED_RunHandle *h,
1314 unsigned int n_peers,
1315 struct GNUNET_TESTBED_Peer **peers,
1316 unsigned int links_succeeded,
1317 unsigned int links_failed)
1320 struct OpListEntry *entry;
1322 testbed_peers = peers;
1323 num_peers_online = 0;
1324 for (i = 0 ; i < num_peers ; i++)
1326 entry = make_oplist_entry ();
1328 entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1329 GNUNET_TESTBED_PIT_IDENTITY,
1334 GNUNET_assert (num_peers == n_peers);
1335 for (i = 0 ; i < n_peers ; i++)
1337 rps_peers[i].index = i;
1339 GNUNET_TESTBED_service_connect (&rps_peers[i],
1342 &rps_connect_complete_cb,
1344 &rps_connect_adapter,
1345 &rps_disconnect_adapter,
1349 if (NULL != churn_task)
1350 GNUNET_SCHEDULER_cancel (churn_task);
1351 shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1356 * Entry point for the testcase, sets up the testbed.
1358 * @param argc unused
1359 * @param argv unused
1360 * @return 0 on success
1363 main (int argc, char *argv[])
1368 cur_test_run.name = "test-rps-default";
1369 cur_test_run.init_peer = default_init_peer;
1370 cur_test_run.pre_test = NULL;
1371 cur_test_run.reply_handle = default_reply_handle;
1372 cur_test_run.eval_cb = default_eval_cb;
1374 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1376 if (strstr (argv[0], "malicious") != NULL)
1378 cur_test_run.pre_test = mal_pre;
1379 cur_test_run.main_test = mal_cb;
1380 cur_test_run.init_peer = mal_init_peer;
1382 if (strstr (argv[0], "_1") != NULL)
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1385 cur_test_run.name = "test-rps-malicious_1";
1388 else if (strstr (argv[0], "_2") != NULL)
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1391 cur_test_run.name = "test-rps-malicious_2";
1394 else if (strstr (argv[0], "_3") != NULL)
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1397 cur_test_run.name = "test-rps-malicious_3";
1402 else if (strstr (argv[0], "_single_req") != NULL)
1404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1405 cur_test_run.name = "test-rps-single-req";
1406 cur_test_run.main_test = single_req_cb;
1409 else if (strstr (argv[0], "_delayed_reqs") != NULL)
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1412 cur_test_run.name = "test-rps-delayed-reqs";
1413 cur_test_run.main_test = delay_req_cb;
1416 else if (strstr (argv[0], "_seed_big") != NULL)
1418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_SERVER_MAX_MESSAGE_SIZE)\n");
1419 cur_test_run.name = "test-rps-seed-big";
1420 cur_test_run.main_test = seed_big_cb;
1423 else if (strstr (argv[0], "_single_peer_seed") != NULL)
1425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1426 cur_test_run.name = "test-rps-single-peer-seed";
1427 cur_test_run.main_test = single_peer_seed_cb;
1430 else if (strstr (argv[0], "_seed_request") != NULL)
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1433 cur_test_run.name = "test-rps-seed-request";
1434 cur_test_run.main_test = seed_req_cb;
1437 else if (strstr (argv[0], "_seed") != NULL)
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1440 cur_test_run.name = "test-rps-seed";
1441 cur_test_run.main_test = seed_cb;
1442 cur_test_run.eval_cb = no_eval;
1445 else if (strstr (argv[0], "_req_cancel") != NULL)
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1448 cur_test_run.name = "test-rps-req-cancel";
1450 cur_test_run.main_test = req_cancel_cb;
1451 cur_test_run.eval_cb = no_eval;
1452 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1455 else if (strstr (argv[0], "profiler") != NULL)
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1458 cur_test_run.name = "test-rps-profiler";
1461 cur_test_run.init_peer = profiler_init_peer;
1462 cur_test_run.pre_test = mal_pre;
1463 cur_test_run.main_test = profiler_cb;
1464 cur_test_run.reply_handle = profiler_reply_handle;
1465 cur_test_run.eval_cb = profiler_eval;
1466 cur_test_run.request_interval = 2;
1467 cur_test_run.num_requests = 5;
1468 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1470 /* 'Clean' directory */
1471 (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1472 GNUNET_DISK_directory_create ("/tmp/rps/");
1475 rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1476 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1477 rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1478 if ( (2 == mal_type) ||
1480 target_peer = &rps_peer_ids[num_peers - 2];
1481 if (profiler_eval == cur_test_run.eval_cb)
1482 eval_peer = &rps_peers[num_peers - 1];
1485 (void) GNUNET_TESTBED_test_run (cur_test_run.name,
1491 ret_value = cur_test_run.eval_cb();
1492 GNUNET_free (rps_peers );
1493 GNUNET_free (rps_peer_ids);
1494 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1498 /* end of test_rps_multipeer.c */