584d26c265ae555d3dbcb37f26e37c2c1c636adc
[oweals/gnunet.git] / src / rps / test_rps.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2012 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20 /**
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.
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30
31 #include "gnunet_rps_service.h"
32 #include "rps-test_util.h"
33 #include "gnunet-service-rps_sampler_elem.h"
34
35 #include <inttypes.h>
36
37
38 /**
39  * How many peers do we start?
40  */
41 static uint32_t num_peers;
42
43 /**
44  * How long do we run the test?
45  */
46 //#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 static struct GNUNET_TIME_Relative timeout;
48
49
50 /**
51  * Portion of malicious peers
52  */
53 static double portion = .1;
54
55 /**
56  * Type of malicious peer to test
57  */
58 static unsigned int mal_type = 0;
59
60 /**
61  * Handles to all of the running peers
62  */
63 static struct GNUNET_TESTBED_Peer **testbed_peers;
64
65
66 /**
67  * Operation map entry
68  */
69 struct OpListEntry
70 {
71   /**
72    * DLL next ptr
73    */
74   struct OpListEntry *next;
75
76   /**
77    * DLL prev ptr
78    */
79   struct OpListEntry *prev;
80
81   /**
82    * The testbed operation
83    */
84   struct GNUNET_TESTBED_Operation *op;
85
86   /**
87    * Depending on whether we start or stop RPS service at the peer set this to 1 (start)
88    * or -1 (stop)
89    */
90   int delta;
91
92   /**
93    * Index of the regarding peer
94    */
95   unsigned int index;
96 };
97
98 /**
99  * OpList DLL head
100  */
101 static struct OpListEntry *oplist_head;
102
103 /**
104  * OpList DLL tail
105  */
106 static struct OpListEntry *oplist_tail;
107
108
109 /**
110  * A pending reply: A request was sent and the reply is pending.
111  */
112 struct PendingReply
113 {
114   /**
115    * DLL next,prev ptr
116    */
117   struct PendingReply *next;
118   struct PendingReply *prev;
119
120   /**
121    * Handle to the request we are waiting for
122    */
123   struct GNUNET_RPS_Request_Handle *req_handle;
124
125   /**
126    * The peer that requested
127    */
128   struct RPSPeer *rps_peer;
129 };
130
131
132 /**
133  * A pending request: A request was not made yet but is scheduled for later.
134  */
135 struct PendingRequest
136 {
137   /**
138    * DLL next,prev ptr
139    */
140   struct PendingRequest *next;
141   struct PendingRequest *prev;
142
143   /**
144    * Handle to the request we are waiting for
145    */
146   struct GNUNET_SCHEDULER_Task *request_task;
147
148   /**
149    * The peer that requested
150    */
151   struct RPSPeer *rps_peer;
152 };
153
154
155 /**
156  * Information we track for each peer.
157  */
158 struct RPSPeer
159 {
160   /**
161    * Index of the peer.
162    */
163   unsigned int index;
164
165   /**
166    * Handle for RPS connect operation.
167    */
168   struct GNUNET_TESTBED_Operation *op;
169
170   /**
171    * Handle to RPS service.
172    */
173   struct GNUNET_RPS_Handle *rps_handle;
174
175   /**
176    * ID of the peer.
177    */
178   struct GNUNET_PeerIdentity *peer_id;
179
180   /**
181    * A request handle to check for an request
182    */
183   //struct GNUNET_RPS_Request_Handle *req_handle;
184
185   /**
186    * Peer on- or offline?
187    */
188   int online;
189
190   /**
191    * Number of Peer IDs to request during the whole test
192    */
193   unsigned int num_ids_to_request;
194
195   /**
196    * Pending requests DLL
197    */
198   struct PendingRequest *pending_req_head;
199   struct PendingRequest *pending_req_tail;
200
201   /**
202    * Number of pending requests
203    */
204   unsigned int num_pending_reqs;
205
206   /**
207    * Pending replies DLL
208    */
209   struct PendingReply *pending_rep_head;
210   struct PendingReply *pending_rep_tail;
211
212   /**
213    * Number of pending replies
214    */
215   unsigned int num_pending_reps;
216
217   /**
218    * Number of received PeerIDs
219    */
220   unsigned int num_recv_ids;
221
222   /**
223    * Pending operation on that peer
224    */
225   const struct OpListEntry *entry_op_manage;
226 };
227
228
229 /**
230  * Information for all the peers.
231  */
232 static struct RPSPeer *rps_peers;
233
234 /**
235  * Peermap to get the index of a given peer ID quick.
236  */
237 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
238
239 /**
240  * IDs of the peers.
241  */
242 static struct GNUNET_PeerIdentity *rps_peer_ids;
243
244 /**
245  * ID of the targeted peer.
246  */
247 static struct GNUNET_PeerIdentity *target_peer;
248
249 /**
250  * ID of the peer that requests for the evaluation.
251  */
252 static struct RPSPeer *eval_peer;
253
254 /**
255  * Number of online peers.
256  */
257 static unsigned int num_peers_online;
258
259 /**
260  * Return value from 'main'.
261  */
262 static int ok;
263
264 /**
265  * Identifier for the churn task that runs periodically
266  */
267 static struct GNUNET_SCHEDULER_Task *shutdown_task;
268
269 /**
270  * Identifier for the churn task that runs periodically
271  */
272 static struct GNUNET_SCHEDULER_Task *churn_task;
273
274 /**
275  * Called to initialise the given RPSPeer
276  */
277 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
278
279 /**
280  * @brief Called directly after connecting to the service
281  *
282  * @param rps_peer Specific peer the function is called on
283  * @param h the handle to the rps service
284  */
285 typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
286
287 /**
288  * @brief Executes functions to test the api/service for a given peer
289  *
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
293  *
294  * @param rps_peer the peer the task runs on
295  */
296 typedef void (*MainTest) (struct RPSPeer *rps_peer);
297
298 /**
299  * Callback called once the requested random peers are available
300  */
301 typedef void (*ReplyHandle) (void *cls,
302                              uint64_t n,
303                              const struct GNUNET_PeerIdentity *recv_peers);
304
305 /**
306  * Called directly before disconnecting from the service
307  */
308 typedef void (*PostTest) (void *cls, struct GNUNET_RPS_Handle *h);
309
310 /**
311  * Function called after disconnect to evaluate test success
312  */
313 typedef int (*EvaluationCallback) (void);
314
315
316 /**
317  * Structure to define a single test
318  */
319 struct SingleTestRun
320 {
321   /**
322    * Name of the test
323    */
324   char *name;
325
326   /**
327    * Called with a single peer in order to initialise that peer
328    */
329   InitPeer init_peer;
330
331   /**
332    * Called directly after connecting to the service
333    */
334   PreTest pre_test;
335
336   /**
337    * Main function for each peer
338    */
339   MainTest main_test;
340
341   /**
342    * Callback called once the requested peers are available
343    */
344   ReplyHandle reply_handle;
345
346   /**
347    * Called directly before disconnecting from the service
348    */
349   PostTest post_test;
350
351   /**
352    * Function to evaluate the test results
353    */
354   EvaluationCallback eval_cb;
355
356   /**
357    * Request interval
358    */
359   uint32_t request_interval;
360
361   /**
362    * Number of Requests to make.
363    */
364   uint32_t num_requests;
365
366   /**
367    * Run with churn
368    */
369   int have_churn;
370 } cur_test_run;
371
372 /**
373  * Are we shutting down?
374  */
375 static int in_shutdown;
376
377 /**
378  * Append arguments to file
379  */
380 static void
381 tofile_ (const char *file_name, const char *line)
382 {
383   struct GNUNET_DISK_FileHandle *f;
384   /* char output_buffer[512]; */
385   size_t size;
386   /* int size; */
387   size_t size2;
388
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)))
397   {
398     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
399                 "Not able to open file %s\n",
400                 file_name);
401     return;
402   }
403   /* size = GNUNET_snprintf (output_buffer,
404                           sizeof (output_buffer),
405                           "%llu %s\n",
406                           GNUNET_TIME_absolute_get ().abs_value_us,
407                           line);
408   if (0 > size)
409   {
410     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
411                 "Failed to write string to buffer (size: %i)\n",
412                 size);
413     return;
414   } */
415
416   size = strlen (line) * sizeof (char);
417
418   size2 = GNUNET_DISK_file_write (f, line, size);
419   if (size != size2)
420   {
421     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
422                 "Unable to write to file! (Size: %lu, size2: %lu)\n",
423                 size,
424                 size2);
425     if (GNUNET_YES != GNUNET_DISK_file_close (f))
426     {
427       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
428                   "Unable to close file\n");
429     }
430     return;
431   }
432
433   if (GNUNET_YES != GNUNET_DISK_file_close (f))
434   {
435     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
436                 "Unable to close file\n");
437   }
438 }
439
440 /**
441  * This function is used to facilitate writing important information to disk
442  */
443 #define tofile(file_name, ...) do {\
444   char tmp_buf[512];\
445     int size;\
446     size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
447     if (0 > size)\
448       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
449                      "Failed to create tmp_buf\n");\
450     else\
451       tofile_(file_name,tmp_buf);\
452   } while (0);
453
454
455 /**
456  * Write the ids and their according index in the given array to a file
457  * Unused
458  */
459 /* static void
460 ids_to_file (char *file_name,
461              struct GNUNET_PeerIdentity *peer_ids,
462              unsigned int num_peer_ids)
463 {
464   unsigned int i;
465
466   for (i=0 ; i < num_peer_ids ; i++)
467   {
468     to_file (file_name,
469              "%u\t%s",
470              i,
471              GNUNET_i2s_full (&peer_ids[i]));
472   }
473 } */
474
475 /**
476  * Test the success of a single test
477  */
478 static int
479 evaluate (void)
480 {
481   unsigned int i;
482   int tmp_ok;
483
484   tmp_ok = 1;
485
486   for (i = 0; i < num_peers; i++)
487   {
488     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489         "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
490         i,
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);
496   }
497   return tmp_ok? 0 : 1;
498 }
499
500
501 /**
502  * Creates an oplist entry and adds it to the oplist DLL
503  */
504 static struct OpListEntry *
505 make_oplist_entry ()
506 {
507   struct OpListEntry *entry;
508
509   entry = GNUNET_new (struct OpListEntry);
510   GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
511   return entry;
512 }
513
514
515 /**
516  * Task run on timeout to shut everything down.
517  */
518 static void
519 shutdown_op (void *cls)
520 {
521   unsigned int i;
522
523   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
524               "Shutdown task scheduled, going down.\n");
525   in_shutdown = GNUNET_YES;
526   if (NULL != churn_task)
527   {
528     GNUNET_SCHEDULER_cancel (churn_task);
529     churn_task = NULL;
530   }
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 ();
535 }
536
537
538 /**
539  * Seed peers.
540  */
541 static void
542 seed_peers (void *cls)
543 {
544   struct RPSPeer *peer = cls;
545   unsigned int amount;
546   unsigned int i;
547
548   // TODO if malicious don't seed mal peers
549   amount = round (.5 * num_peers);
550
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",
554                 i,
555                 GNUNET_i2s (&rps_peer_ids[i]));
556
557   GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
558 }
559
560
561 /**
562  * Seed peers.
563  */
564 static void
565 seed_peers_big (void *cls)
566 {
567   struct RPSPeer *peer = cls;
568   unsigned int seed_msg_size;
569   uint32_t num_peers_max;
570   unsigned int amount;
571   unsigned int i;
572
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",
578       num_peers_max);
579   amount = num_peers_max + (0.5 * num_peers_max);
580   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581       "Seeding many (%u) peers:\n",
582       amount);
583   struct GNUNET_PeerIdentity ids_to_seed[amount];
584   for (i = 0; i < amount; i++)
585   {
586     ids_to_seed[i] = *peer->peer_id;
587     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
588                 i,
589                 GNUNET_i2s (&ids_to_seed[i]));
590   }
591
592   GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
593 }
594
595 /**
596  * Get the id of peer i.
597  */
598   void
599 info_cb (void *cb_cls,
600          struct GNUNET_TESTBED_Operation *op,
601          const struct GNUNET_TESTBED_PeerInformation *pinfo,
602          const char *emsg)
603 {
604   struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
605
606   if (GNUNET_YES == in_shutdown)
607   {
608     return;
609   }
610
611   if (NULL == pinfo || NULL != emsg)
612   {
613     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
614     GNUNET_TESTBED_operation_done (entry->op);
615     return;
616   }
617
618   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619               "Peer %u is %s\n",
620               entry->index,
621               GNUNET_i2s (pinfo->result.id));
622
623   rps_peer_ids[entry->index] = *(pinfo->result.id);
624   rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
625
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",
632            "%u\t%s\n",
633            entry->index,
634            GNUNET_i2s_full (&rps_peer_ids[entry->index]));
635
636   GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
637   GNUNET_TESTBED_operation_done (entry->op);
638   GNUNET_free (entry);
639 }
640
641
642 /**
643  * Callback to be called when RPS service connect operation is completed
644  *
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.
650  */
651 static void
652 rps_connect_complete_cb (void *cls,
653                          struct GNUNET_TESTBED_Operation *op,
654                          void *ca_result,
655                          const char *emsg)
656 {
657   struct RPSPeer *rps_peer = cls;
658   struct GNUNET_RPS_Handle *rps = ca_result;
659
660   if (GNUNET_YES == in_shutdown)
661   {
662     return;
663   }
664
665   rps_peer->rps_handle = rps;
666   rps_peer->online = GNUNET_YES;
667   num_peers_online++;
668
669   GNUNET_assert (op == rps_peer->op);
670   if (NULL != emsg)
671   {
672     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673                 "Failed to connect to RPS service: %s\n",
674                 emsg);
675     ok = 1;
676     GNUNET_SCHEDULER_shutdown ();
677     return;
678   }
679
680   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
681
682   cur_test_run.main_test (rps_peer);
683 }
684
685
686 /**
687  * Adapter function called to establish a connection to
688  * the RPS service.
689  *
690  * @param cls closure
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
695  */
696 static void *
697 rps_connect_adapter (void *cls,
698                                  const struct GNUNET_CONFIGURATION_Handle *cfg)
699 {
700   struct GNUNET_RPS_Handle *h;
701
702   h = GNUNET_RPS_connect (cfg);
703
704   if (NULL != cur_test_run.pre_test)
705     cur_test_run.pre_test (cls, h);
706
707   return h;
708 }
709
710
711 /**
712  * Adapter function called to destroy connection to
713  * RPS service.
714  *
715  * @param cls closure
716  * @param op_result service handle returned from the connect adapter
717  */
718 static void
719 rps_disconnect_adapter (void *cls,
720                                           void *op_result)
721 {
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;
727 }
728
729
730 /***********************************************************************
731  * Definition of tests
732 ***********************************************************************/
733
734 // TODO check whether tests can be stopped earlier
735 static int
736 default_eval_cb (void)
737 {
738   return evaluate ();
739 }
740
741 static int
742 no_eval (void)
743 {
744   return 0;
745 }
746
747 /**
748  * Initialise given RPSPeer
749  */
750 static void default_init_peer (struct RPSPeer *rps_peer)
751 {
752   rps_peer->num_ids_to_request = 1;
753 }
754
755 /**
756  * Callback to call on receipt of a reply
757  *
758  * @param cls closure
759  * @param n number of peers
760  * @param recv_peers the received peers
761  */
762 static void
763 default_reply_handle (void *cls,
764                       uint64_t n,
765                       const struct GNUNET_PeerIdentity *recv_peers)
766 {
767   struct RPSPeer *rps_peer;
768   struct PendingReply *pending_rep = (struct PendingReply *) cls;
769   unsigned int i;
770
771   rps_peer = pending_rep->rps_peer;
772   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
773                                rps_peer->pending_rep_tail,
774                                pending_rep);
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),
779               n);
780
781   for (i = 0; i < n; i++)
782   {
783     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784                 "%u: %s\n",
785                 i,
786                 GNUNET_i2s (&recv_peers[i]));
787
788     rps_peer->num_recv_ids++;
789   }
790
791   if (0 == evaluate ())
792   {
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);
797   }
798 }
799
800 /**
801  * Request random peers.
802  */
803 static void
804 request_peers (void *cls)
805 {
806   struct PendingRequest *pending_req = cls;
807   struct RPSPeer *rps_peer;
808   struct PendingReply *pending_rep;
809
810   if (GNUNET_YES == in_shutdown)
811     return;
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,
816                                pending_req);
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,
822       1,
823       cur_test_run.reply_handle,
824       pending_rep);
825   GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
826                                     rps_peer->pending_rep_tail,
827                                     pending_rep);
828   rps_peer->num_pending_reps++;
829   rps_peer->num_pending_reqs--;
830 }
831
832 static void
833 cancel_pending_req (struct PendingRequest *pending_req)
834 {
835   struct RPSPeer *rps_peer;
836
837   rps_peer = pending_req->rps_peer;
838   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
839                                rps_peer->pending_req_tail,
840                                pending_req);
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);
846 }
847
848 static void
849 cancel_request (struct PendingReply *pending_rep)
850 {
851   struct RPSPeer *rps_peer;
852
853   rps_peer = pending_rep->rps_peer;
854   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
855                                rps_peer->pending_rep_tail,
856                                pending_rep);
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);
862 }
863
864 /**
865  * Cancel a request.
866  */
867 static void
868 cancel_request_cb (void *cls)
869 {
870   struct RPSPeer *rps_peer = cls;
871   struct PendingReply *pending_rep;
872
873   if (GNUNET_YES == in_shutdown)
874     return;
875   pending_rep = rps_peer->pending_rep_head;
876   GNUNET_assert (1 <= rps_peer->num_pending_reps);
877   cancel_request (pending_rep);
878 }
879
880
881 /**
882  * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
883  * issued, nor replied
884  */
885 void
886 schedule_missing_requests (struct RPSPeer *rps_peer)
887 {
888   unsigned int i;
889   struct PendingRequest *pending_req;
890
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++)
899   {
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),
905         request_peers,
906         pending_req);
907     GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
908                                       rps_peer->pending_req_tail,
909                                       pending_req);
910     rps_peer->num_pending_reqs++;
911   }
912 }
913
914 void
915 cancel_pending_req_rep (struct RPSPeer *rps_peer)
916 {
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);
925 }
926
927 /***********************************
928  * MALICIOUS
929 ***********************************/
930
931 /**
932  * Initialise only non-mal RPSPeers
933  */
934 static void mal_init_peer (struct RPSPeer *rps_peer)
935 {
936   if (rps_peer->index >= round (portion * num_peers))
937     rps_peer->num_ids_to_request = 1;
938 }
939
940
941 /**
942  * @brief Set peers to (non-)malicious before execution
943  *
944  * Of signature #PreTest
945  *
946  * @param rps_peer the peer to set (non-) malicious
947  * @param h the handle to the service
948  */
949 static void
950 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
951 {
952   #ifdef ENABLE_MALICIOUS
953   uint32_t num_mal_peers;
954
955   GNUNET_assert ( (1 >= portion) &&
956                   (0 <  portion) );
957   num_mal_peers = round (portion * num_peers);
958
959   if (rps_peer->index < num_mal_peers)
960   {
961     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962                 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
963                 rps_peer->index,
964                 GNUNET_i2s (rps_peer->peer_id),
965                 num_mal_peers);
966
967     GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
968                               rps_peer_ids, target_peer);
969   }
970   #endif /* ENABLE_MALICIOUS */
971 }
972
973 static void
974 mal_cb (struct RPSPeer *rps_peer)
975 {
976   uint32_t num_mal_peers;
977
978   if (GNUNET_YES == in_shutdown)
979   {
980     return;
981   }
982
983   #ifdef ENABLE_MALICIOUS
984   GNUNET_assert ( (1 >= portion) &&
985                   (0 <  portion) );
986   num_mal_peers = round (portion * num_peers);
987
988   if (rps_peer->index >= num_mal_peers)
989   { /* It's useless to ask a malicious peer about a random sample -
990        it's not sampling */
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);
994   }
995   #endif /* ENABLE_MALICIOUS */
996 }
997
998
999 /***********************************
1000  * SINGLE_REQUEST
1001 ***********************************/
1002 static void
1003 single_req_cb (struct RPSPeer *rps_peer)
1004 {
1005   if (GNUNET_YES == in_shutdown)
1006   {
1007     return;
1008   }
1009
1010   schedule_missing_requests (rps_peer);
1011 }
1012
1013 /***********************************
1014  * DELAYED_REQUESTS
1015 ***********************************/
1016 static void
1017 delay_req_cb (struct RPSPeer *rps_peer)
1018 {
1019   if (GNUNET_YES == in_shutdown)
1020   {
1021     return;
1022   }
1023
1024   schedule_missing_requests (rps_peer);
1025 }
1026
1027 /***********************************
1028  * SEED
1029 ***********************************/
1030 static void
1031 seed_cb (struct RPSPeer *rps_peer)
1032 {
1033   if (GNUNET_YES == in_shutdown)
1034   {
1035     return;
1036   }
1037
1038   GNUNET_SCHEDULER_add_delayed (
1039       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
1040       seed_peers, rps_peer);
1041 }
1042
1043 /***********************************
1044  * SEED_BIG
1045 ***********************************/
1046 static void
1047 seed_big_cb (struct RPSPeer *rps_peer)
1048 {
1049   if (GNUNET_YES == in_shutdown)
1050   {
1051     return;
1052   }
1053
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);
1058 }
1059
1060 /***********************************
1061  * SINGLE_PEER_SEED
1062 ***********************************/
1063 static void
1064 single_peer_seed_cb (struct RPSPeer *rps_peer)
1065 {
1066   // TODO
1067 }
1068
1069 /***********************************
1070  * SEED_REQUEST
1071 ***********************************/
1072 static void
1073 seed_req_cb (struct RPSPeer *rps_peer)
1074 {
1075   if (GNUNET_YES == in_shutdown)
1076   {
1077     return;
1078   }
1079
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);
1084 }
1085
1086 //TODO start big mal
1087
1088 /***********************************
1089  * REQUEST_CANCEL
1090 ***********************************/
1091 static void
1092 req_cancel_cb (struct RPSPeer *rps_peer)
1093 {
1094   if (GNUNET_YES == in_shutdown)
1095   {
1096     return;
1097   }
1098
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);
1104 }
1105
1106 /***********************************
1107  * CHURN
1108 ***********************************/
1109
1110 static void
1111 churn (void *cls);
1112
1113 /**
1114  * @brief Starts churn
1115  *
1116  * Has signature of #MainTest
1117  *
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
1120  * once.)
1121  *
1122  * @param rps_peer The peer it's called for
1123  */
1124 static void
1125 churn_test_cb (struct RPSPeer *rps_peer)
1126 {
1127   if (GNUNET_YES == in_shutdown)
1128   {
1129     return;
1130   }
1131
1132   /* Start churn */
1133   if (GNUNET_YES == cur_test_run.have_churn && NULL == churn_task)
1134   {
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),
1139           churn,
1140           NULL);
1141   } else {
1142     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143                 "Not starting churn task\n");
1144   }
1145
1146   schedule_missing_requests (rps_peer);
1147 }
1148
1149 /***********************************
1150  * PROFILER
1151 ***********************************/
1152
1153 /**
1154  * Callback to be called when RPS service is started or stopped at peers
1155  *
1156  * @param cls NULL
1157  * @param op the operation handle
1158  * @param emsg NULL on success; otherwise an error description
1159  */
1160 static void
1161 churn_cb (void *cls,
1162           struct GNUNET_TESTBED_Operation *op,
1163           const char *emsg)
1164 {
1165   // FIXME
1166   struct OpListEntry *entry = cls;
1167
1168   if (GNUNET_YES == in_shutdown)
1169   {
1170     return;
1171   }
1172
1173   GNUNET_TESTBED_operation_done (entry->op);
1174   if (NULL != emsg)
1175   {
1176     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1177     GNUNET_SCHEDULER_shutdown ();
1178     return;
1179   }
1180   GNUNET_assert (0 != entry->delta);
1181
1182   num_peers_online += entry->delta;
1183
1184   if (0 > entry->delta)
1185   { /* Peer hopefully just went offline */
1186     if (GNUNET_YES != rps_peers[entry->index].online)
1187     {
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));
1191       GNUNET_break (0);
1192     }
1193     else
1194     {
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));
1198     }
1199     rps_peers[entry->index].online = GNUNET_NO;
1200   }
1201
1202   else if (0 < entry->delta)
1203   { /* Peer hopefully just went online */
1204     if (GNUNET_NO != rps_peers[entry->index].online)
1205     {
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));
1209       GNUNET_break (0);
1210     }
1211     else
1212     {
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)
1217       {
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]);
1221       }
1222     }
1223     rps_peers[entry->index].online = GNUNET_YES;
1224   }
1225
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)
1230   //  run_round ();
1231 }
1232
1233 /**
1234  * @brief Set the rps-service up or down for a specific peer
1235  *
1236  * TODO use enum instead of 1/-1 for delta
1237  *
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
1242  */
1243 static void
1244 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1245     double prob_go_on_off)
1246 {
1247   struct OpListEntry *entry;
1248   uint32_t prob;
1249
1250   GNUNET_assert (GNUNET_YES == rps_peers[j].online);
1251
1252   /* make sure that management operation is not already scheduled */
1253   if (NULL != rps_peers[j].entry_op_manage)
1254   {
1255     return;
1256   }
1257
1258   prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1259                                    UINT32_MAX);
1260   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261               "%u. selected peer (%u: %s) is %s.\n",
1262               i,
1263               j,
1264               GNUNET_i2s (rps_peers[j].peer_id),
1265               (0 > delta) ? "online" : "offline");
1266   if (prob < prob_go_on_off * UINT32_MAX)
1267   {
1268     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269                 "%s goes %s\n",
1270                 GNUNET_i2s (rps_peers[j].peer_id),
1271                 (0 > delta) ? "offline" : "online");
1272
1273     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274                 "testbed_peers points to %p, peer 0 to %p\n",
1275                 testbed_peers, testbed_peers[0]);
1276
1277     if (0 > delta)
1278       cancel_pending_req_rep (&rps_peers[j]);
1279     entry = make_oplist_entry ();
1280     entry->delta = delta;
1281     entry->index = j;
1282     entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1283                                                     testbed_peers[j],
1284                                                     "rps",
1285                                                     &churn_cb,
1286                                                     entry,
1287                                                     (0 > delta) ? 0 : 1);
1288   }
1289   rps_peers[j].entry_op_manage = entry;
1290 }
1291
1292
1293 static void
1294 churn (void *cls)
1295 {
1296   unsigned int i;
1297   unsigned int j;
1298   double portion_online;
1299   unsigned int *permut;
1300   double prob_go_offline;
1301   double portion_go_online;
1302   double portion_go_offline;
1303
1304   if (GNUNET_YES == in_shutdown)
1305   {
1306     return;
1307   }
1308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309               "Churn function executing\n");
1310
1311   churn_task = NULL; /* Should be invalid by now */
1312
1313   /* Compute the probability for an online peer to go offline
1314    * this round */
1315   portion_online = num_peers_online * 1.0 / num_peers;
1316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317               "Portion online: %f\n",
1318               portion_online);
1319   portion_go_online = ((1 - portion_online) * .5 * .66);
1320   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1321               "Portion that should go online: %f\n",
1322               portion_go_online);
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",
1330               prob_go_offline);
1331
1332   permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1333                                          (unsigned int) num_peers);
1334
1335   /* Go over 50% randomly chosen peers */
1336   for (i = 0; i < .5 * num_peers; i++)
1337   {
1338     j = permut[i];
1339
1340     /* If online, shut down with certain probability */
1341     if (GNUNET_YES == rps_peers[j].online)
1342     {
1343       manage_service_wrapper (i, j, -1, prob_go_offline);
1344     }
1345
1346     /* If offline, restart with certain probability */
1347     else if (GNUNET_NO == rps_peers[j].online)
1348     {
1349       manage_service_wrapper (i, j, 1, 0.66);
1350     }
1351   }
1352
1353   GNUNET_free (permut);
1354
1355   churn_task = GNUNET_SCHEDULER_add_delayed (
1356         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1357         churn,
1358         NULL);
1359 }
1360
1361
1362 /**
1363  * Initialise given RPSPeer
1364  */
1365 static void profiler_init_peer (struct RPSPeer *rps_peer)
1366 {
1367   if (num_peers - 1 == rps_peer->index)
1368     rps_peer->num_ids_to_request = cur_test_run.num_requests;
1369 }
1370
1371
1372 /**
1373  * Callback to call on receipt of a reply
1374  *
1375  * @param cls closure
1376  * @param n number of peers
1377  * @param recv_peers the received peers
1378  */
1379 static void
1380 profiler_reply_handle (void *cls,
1381                       uint64_t n,
1382                       const struct GNUNET_PeerIdentity *recv_peers)
1383 {
1384   struct RPSPeer *rps_peer;
1385   struct RPSPeer *rcv_rps_peer;
1386   char *file_name;
1387   char *file_name_dh;
1388   unsigned int i;
1389   struct PendingReply *pending_rep = (struct PendingReply *) cls;
1390
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),
1397               n);
1398   for (i = 0; i < n; i++)
1399   {
1400     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401                 "%u: %s\n",
1402                 i,
1403                 GNUNET_i2s (&recv_peers[i]));
1404     tofile (file_name,
1405              "%s\n",
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,
1410              "%" PRIu32 "\n",
1411              (uint32_t) rcv_rps_peer->index);
1412   }
1413   default_reply_handle (cls, n, recv_peers);
1414 }
1415
1416
1417 static void
1418 profiler_cb (struct RPSPeer *rps_peer)
1419 {
1420   if (GNUNET_YES == in_shutdown)
1421   {
1422     return;
1423   }
1424
1425   /* Start churn */
1426   if (GNUNET_YES == cur_test_run.have_churn && NULL == churn_task)
1427   {
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),
1432           churn,
1433           NULL);
1434   } else {
1435     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1436                 "Not starting churn task\n");
1437   }
1438
1439   /* Only request peer ids at one peer.
1440    * (It's the before-last because last one is target of the focussed attack.)
1441    */
1442   if (eval_peer == rps_peer)
1443     schedule_missing_requests (rps_peer);
1444 }
1445
1446 /**
1447  * Function called from #profiler_eval with a filename.
1448  *
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!
1454  */
1455 int
1456 file_name_cb (void *cls, const char *filename)
1457 {
1458   if (NULL != strstr (filename, "sampler_el"))
1459   {
1460     struct RPS_SamplerElement *s_elem;
1461     struct GNUNET_CRYPTO_AuthKey auth_key;
1462     const char *key_char;
1463     uint32_t i;
1464
1465     key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1466     tofile (filename, "--------------------------\n");
1467
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);
1471
1472     for (i = 0; i < num_peers; i++)
1473     {
1474       RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1475     }
1476     RPS_sampler_elem_destroy (s_elem);
1477   }
1478   return GNUNET_OK;
1479 }
1480
1481 /**
1482  * This is run after the test finished.
1483  *
1484  * Compute all perfect samples.
1485  */
1486 int
1487 profiler_eval (void)
1488 {
1489   /* Compute perfect sample for each sampler element */
1490   if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1491   {
1492     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1493   }
1494
1495   return evaluate ();
1496 }
1497
1498
1499 /***********************************************************************
1500  * /Definition of tests
1501 ***********************************************************************/
1502
1503
1504 /**
1505  * Actual "main" function for the testcase.
1506  *
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
1512  *          succeeded
1513  * @param links_failed the number of overlay link connection attempts that
1514  *          failed
1515  */
1516 static void
1517 run (void *cls,
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)
1523 {
1524   unsigned int i;
1525   struct OpListEntry *entry;
1526   uint32_t num_mal_peers;
1527
1528   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
1529
1530   /* Check whether we timed out */
1531   if (n_peers != num_peers ||
1532       NULL == peers ||
1533       0 == links_succeeded)
1534   {
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 ();
1541     return;
1542   }
1543
1544
1545   /* Initialize peers */
1546   testbed_peers = peers;
1547   num_peers_online = 0;
1548   for (i = 0; i < num_peers; i++)
1549   {
1550     entry = make_oplist_entry ();
1551     entry->index = i;
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,
1557                                                      &info_cb,
1558                                                      entry);
1559   }
1560
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++)
1565   {
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) )
1569     {
1570       rps_peers[i].op =
1571         GNUNET_TESTBED_service_connect (&rps_peers[i],
1572                                         peers[i],
1573                                         "rps",
1574                                         &rps_connect_complete_cb,
1575                                         &rps_peers[i],
1576                                         &rps_connect_adapter,
1577                                         &rps_disconnect_adapter,
1578                                         &rps_peers[i]);
1579     }
1580   }
1581
1582   if (NULL != churn_task)
1583     GNUNET_SCHEDULER_cancel (churn_task);
1584   shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1585 }
1586
1587
1588 /**
1589  * Entry point for the testcase, sets up the testbed.
1590  *
1591  * @param argc unused
1592  * @param argv unused
1593  * @return 0 on success
1594  */
1595 int
1596 main (int argc, char *argv[])
1597 {
1598   int ret_value;
1599
1600   num_peers = 5;
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;
1607   churn_task = NULL;
1608   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1609
1610   if (strstr (argv[0], "malicious") != NULL)
1611   {
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;
1615
1616     if (strstr (argv[0], "_1") != NULL)
1617     {
1618       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1619       cur_test_run.name = "test-rps-malicious_1";
1620       mal_type = 1;
1621     }
1622     else if (strstr (argv[0], "_2") != NULL)
1623     {
1624       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1625       cur_test_run.name = "test-rps-malicious_2";
1626       mal_type = 2;
1627     }
1628     else if (strstr (argv[0], "_3") != NULL)
1629     {
1630       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1631       cur_test_run.name = "test-rps-malicious_3";
1632       mal_type = 3;
1633     }
1634   }
1635
1636   else if (strstr (argv[0], "_single_req") != NULL)
1637   {
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;
1642   }
1643
1644   else if (strstr (argv[0], "_delayed_reqs") != NULL)
1645   {
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;
1650   }
1651
1652   else if (strstr (argv[0], "_seed_big") != NULL)
1653   {
1654     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
1655     num_peers = 1;
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);
1661   }
1662
1663   else if (strstr (argv[0], "_single_peer_seed") != NULL)
1664   {
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;
1669   }
1670
1671   else if (strstr (argv[0], "_seed_request") != NULL)
1672   {
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;
1677   }
1678
1679   else if (strstr (argv[0], "_seed") != NULL)
1680   {
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;
1686   }
1687
1688   else if (strstr (argv[0], "_req_cancel") != NULL)
1689   {
1690     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1691     cur_test_run.name = "test-rps-req-cancel";
1692     num_peers = 1;
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);
1697   }
1698
1699   else if (strstr (argv[0], "_churn") != NULL)
1700   {
1701     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
1702     cur_test_run.name = "test-rps-churn";
1703     num_peers = 5;
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);
1710   }
1711
1712   else if (strstr (argv[0], "profiler") != NULL)
1713   {
1714     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1715     cur_test_run.name = "test-rps-profiler";
1716     num_peers = 10;
1717     mal_type = 3;
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);
1727
1728     /* 'Clean' directory */
1729     (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1730     GNUNET_DISK_directory_create ("/tmp/rps/");
1731   }
1732
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) ||
1737        (3 == 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 */
1743
1744   ok = 1;
1745   ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
1746                                   "test_rps.conf",
1747                                   num_peers,
1748                                   0, NULL, NULL,
1749                                   &run, NULL);
1750   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1751               "_test_run returned.\n");
1752   if (GNUNET_OK != ret_value)
1753   {
1754     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1755                 "Test did not run successfully!\n");
1756   }
1757
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);
1762   return ret_value;
1763 }
1764
1765 /* end of test_rps.c */