9f3044dccbd36b054f73fd8dd48528a3021c4db2
[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 (-1 == 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   else
1226   {
1227     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1228         "Invalid value for delta: %i\n", entry->delta);
1229     GNUNET_break (0);
1230   }
1231
1232   GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1233   rps_peers[entry->index].entry_op_manage = NULL;
1234   GNUNET_free (entry);
1235   //if (num_peers_in_round[current_round] == peers_running)
1236   //  run_round ();
1237 }
1238
1239 /**
1240  * @brief Set the rps-service up or down for a specific peer
1241  *
1242  * TODO use enum instead of 1/-1 for delta
1243  *
1244  * @param i index of action
1245  * @param j index of peer
1246  * @param delta down (-1) or up (1)
1247  * @param prob_go_on_off the probability of the action
1248  */
1249 static void
1250 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1251     double prob_go_on_off)
1252 {
1253   struct OpListEntry *entry;
1254   uint32_t prob;
1255
1256   GNUNET_assert (GNUNET_YES == rps_peers[j].online);
1257
1258   /* make sure that management operation is not already scheduled */
1259   if (NULL != rps_peers[j].entry_op_manage)
1260   {
1261     return;
1262   }
1263
1264   prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1265                                    UINT32_MAX);
1266   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267               "%u. selected peer (%u: %s) is %s.\n",
1268               i,
1269               j,
1270               GNUNET_i2s (rps_peers[j].peer_id),
1271               (0 > delta) ? "online" : "offline");
1272   if (prob < prob_go_on_off * UINT32_MAX)
1273   {
1274     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1275                 "%s goes %s\n",
1276                 GNUNET_i2s (rps_peers[j].peer_id),
1277                 (0 > delta) ? "offline" : "online");
1278
1279     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1280                 "testbed_peers points to %p, peer 0 to %p\n",
1281                 testbed_peers, testbed_peers[0]);
1282
1283     if (0 > delta)
1284       cancel_pending_req_rep (&rps_peers[j]);
1285     entry = make_oplist_entry ();
1286     entry->delta = delta;
1287     entry->index = j;
1288     entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1289                                                     testbed_peers[j],
1290                                                     "rps",
1291                                                     &churn_cb,
1292                                                     entry,
1293                                                     (0 > delta) ? 0 : 1);
1294   }
1295   rps_peers[j].entry_op_manage = entry;
1296 }
1297
1298
1299 static void
1300 churn (void *cls)
1301 {
1302   unsigned int i;
1303   unsigned int j;
1304   double portion_online;
1305   unsigned int *permut;
1306   double prob_go_offline;
1307   double portion_go_online;
1308   double portion_go_offline;
1309
1310   if (GNUNET_YES == in_shutdown)
1311   {
1312     return;
1313   }
1314   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315               "Churn function executing\n");
1316
1317   churn_task = NULL; /* Should be invalid by now */
1318
1319   /* Compute the probability for an online peer to go offline
1320    * this round */
1321   portion_online = num_peers_online * 1.0 / num_peers;
1322   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323               "Portion online: %f\n",
1324               portion_online);
1325   portion_go_online = ((1 - portion_online) * .5 * .66);
1326   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327               "Portion that should go online: %f\n",
1328               portion_go_online);
1329   portion_go_offline = (portion_online + portion_go_online) - .75;
1330   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331               "Portion that probably goes offline: %f\n",
1332               portion_go_offline);
1333   prob_go_offline = portion_go_offline / (portion_online * .5);
1334   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335               "Probability of a selected online peer to go offline: %f\n",
1336               prob_go_offline);
1337
1338   permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1339                                          (unsigned int) num_peers);
1340
1341   /* Go over 50% randomly chosen peers */
1342   for (i = 0; i < .5 * num_peers; i++)
1343   {
1344     j = permut[i];
1345
1346     /* If online, shut down with certain probability */
1347     if (GNUNET_YES == rps_peers[j].online)
1348     {
1349       manage_service_wrapper (i, j, -1, prob_go_offline);
1350     }
1351
1352     /* If offline, restart with certain probability */
1353     else if (GNUNET_NO == rps_peers[j].online)
1354     {
1355       manage_service_wrapper (i, j, 1, 0.66);
1356     }
1357   }
1358
1359   GNUNET_free (permut);
1360
1361   churn_task = GNUNET_SCHEDULER_add_delayed (
1362         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1363         churn,
1364         NULL);
1365 }
1366
1367
1368 /**
1369  * Initialise given RPSPeer
1370  */
1371 static void profiler_init_peer (struct RPSPeer *rps_peer)
1372 {
1373   if (num_peers - 1 == rps_peer->index)
1374     rps_peer->num_ids_to_request = cur_test_run.num_requests;
1375 }
1376
1377
1378 /**
1379  * Callback to call on receipt of a reply
1380  *
1381  * @param cls closure
1382  * @param n number of peers
1383  * @param recv_peers the received peers
1384  */
1385 static void
1386 profiler_reply_handle (void *cls,
1387                       uint64_t n,
1388                       const struct GNUNET_PeerIdentity *recv_peers)
1389 {
1390   struct RPSPeer *rps_peer;
1391   struct RPSPeer *rcv_rps_peer;
1392   char *file_name;
1393   char *file_name_dh;
1394   unsigned int i;
1395   struct PendingReply *pending_rep = (struct PendingReply *) cls;
1396
1397   rps_peer = pending_rep->rps_peer;
1398   file_name = "/tmp/rps/received_ids";
1399   file_name_dh = "/tmp/rps/diehard_input";
1400   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401               "[%s] got %" PRIu64 " peers:\n",
1402               GNUNET_i2s (rps_peer->peer_id),
1403               n);
1404   for (i = 0; i < n; i++)
1405   {
1406     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407                 "%u: %s\n",
1408                 i,
1409                 GNUNET_i2s (&recv_peers[i]));
1410     tofile (file_name,
1411              "%s\n",
1412              GNUNET_i2s_full (&recv_peers[i]));
1413     rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1414     GNUNET_assert (NULL != rcv_rps_peer);
1415     tofile (file_name_dh,
1416              "%" PRIu32 "\n",
1417              (uint32_t) rcv_rps_peer->index);
1418   }
1419   default_reply_handle (cls, n, recv_peers);
1420 }
1421
1422
1423 static void
1424 profiler_cb (struct RPSPeer *rps_peer)
1425 {
1426   if (GNUNET_YES == in_shutdown)
1427   {
1428     return;
1429   }
1430
1431   /* Start churn */
1432   if (GNUNET_YES == cur_test_run.have_churn && NULL == churn_task)
1433   {
1434     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1435                 "Starting churn task\n");
1436     churn_task = GNUNET_SCHEDULER_add_delayed (
1437           GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1438           churn,
1439           NULL);
1440   } else {
1441     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442                 "Not starting churn task\n");
1443   }
1444
1445   /* Only request peer ids at one peer.
1446    * (It's the before-last because last one is target of the focussed attack.)
1447    */
1448   if (eval_peer == rps_peer)
1449     schedule_missing_requests (rps_peer);
1450 }
1451
1452 /**
1453  * Function called from #profiler_eval with a filename.
1454  *
1455  * @param cls closure
1456  * @param filename complete filename (absolute path)
1457  * @return #GNUNET_OK to continue to iterate,
1458  *  #GNUNET_NO to stop iteration with no error,
1459  *  #GNUNET_SYSERR to abort iteration with error!
1460  */
1461 int
1462 file_name_cb (void *cls, const char *filename)
1463 {
1464   if (NULL != strstr (filename, "sampler_el"))
1465   {
1466     struct RPS_SamplerElement *s_elem;
1467     struct GNUNET_CRYPTO_AuthKey auth_key;
1468     const char *key_char;
1469     uint32_t i;
1470
1471     key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1472     tofile (filename, "--------------------------\n");
1473
1474     auth_key = string_to_auth_key (key_char);
1475     s_elem = RPS_sampler_elem_create ();
1476     RPS_sampler_elem_set (s_elem, auth_key);
1477
1478     for (i = 0; i < num_peers; i++)
1479     {
1480       RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1481     }
1482     RPS_sampler_elem_destroy (s_elem);
1483   }
1484   return GNUNET_OK;
1485 }
1486
1487 /**
1488  * This is run after the test finished.
1489  *
1490  * Compute all perfect samples.
1491  */
1492 int
1493 profiler_eval (void)
1494 {
1495   /* Compute perfect sample for each sampler element */
1496   if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1497   {
1498     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1499   }
1500
1501   return evaluate ();
1502 }
1503
1504
1505 /***********************************************************************
1506  * /Definition of tests
1507 ***********************************************************************/
1508
1509
1510 /**
1511  * Actual "main" function for the testcase.
1512  *
1513  * @param cls closure
1514  * @param h the run handle
1515  * @param n_peers number of peers in 'peers'
1516  * @param peers handle to peers run in the testbed
1517  * @param links_succeeded the number of overlay link connection attempts that
1518  *          succeeded
1519  * @param links_failed the number of overlay link connection attempts that
1520  *          failed
1521  */
1522 static void
1523 run (void *cls,
1524      struct GNUNET_TESTBED_RunHandle *h,
1525      unsigned int n_peers,
1526      struct GNUNET_TESTBED_Peer **peers,
1527      unsigned int links_succeeded,
1528      unsigned int links_failed)
1529 {
1530   unsigned int i;
1531   struct OpListEntry *entry;
1532   uint32_t num_mal_peers;
1533
1534   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
1535
1536   /* Check whether we timed out */
1537   if (n_peers != num_peers ||
1538       NULL == peers ||
1539       0 == links_succeeded)
1540   {
1541     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
1542     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
1543     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
1544     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
1545     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
1546     GNUNET_SCHEDULER_shutdown ();
1547     return;
1548   }
1549
1550
1551   /* Initialize peers */
1552   testbed_peers = peers;
1553   num_peers_online = 0;
1554   for (i = 0; i < num_peers; i++)
1555   {
1556     entry = make_oplist_entry ();
1557     entry->index = i;
1558     rps_peers[i].index = i;
1559     if (NULL != cur_test_run.init_peer)
1560       cur_test_run.init_peer (&rps_peers[i]);
1561     entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1562                                                      GNUNET_TESTBED_PIT_IDENTITY,
1563                                                      &info_cb,
1564                                                      entry);
1565   }
1566
1567   /* Bring peers up */
1568   num_mal_peers = round (portion * num_peers);
1569   GNUNET_assert (num_peers == n_peers);
1570   for (i = 0; i < n_peers; i++)
1571   {
1572     rps_peers[i].index = i;
1573     if ( (rps_peers[i].num_recv_ids < rps_peers[i].num_ids_to_request) ||
1574          (i < num_mal_peers) )
1575     {
1576       rps_peers[i].op =
1577         GNUNET_TESTBED_service_connect (&rps_peers[i],
1578                                         peers[i],
1579                                         "rps",
1580                                         &rps_connect_complete_cb,
1581                                         &rps_peers[i],
1582                                         &rps_connect_adapter,
1583                                         &rps_disconnect_adapter,
1584                                         &rps_peers[i]);
1585     }
1586   }
1587
1588   if (NULL != churn_task)
1589     GNUNET_SCHEDULER_cancel (churn_task);
1590   shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1591 }
1592
1593
1594 /**
1595  * Entry point for the testcase, sets up the testbed.
1596  *
1597  * @param argc unused
1598  * @param argv unused
1599  * @return 0 on success
1600  */
1601 int
1602 main (int argc, char *argv[])
1603 {
1604   int ret_value;
1605
1606   num_peers = 5;
1607   cur_test_run.name = "test-rps-default";
1608   cur_test_run.init_peer = default_init_peer;
1609   cur_test_run.pre_test = NULL;
1610   cur_test_run.reply_handle = default_reply_handle;
1611   cur_test_run.eval_cb = default_eval_cb;
1612   cur_test_run.have_churn = GNUNET_YES;
1613   churn_task = NULL;
1614   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1615
1616   if (strstr (argv[0], "malicious") != NULL)
1617   {
1618     cur_test_run.pre_test = mal_pre;
1619     cur_test_run.main_test = mal_cb;
1620     cur_test_run.init_peer = mal_init_peer;
1621
1622     if (strstr (argv[0], "_1") != NULL)
1623     {
1624       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1625       cur_test_run.name = "test-rps-malicious_1";
1626       mal_type = 1;
1627     }
1628     else if (strstr (argv[0], "_2") != NULL)
1629     {
1630       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1631       cur_test_run.name = "test-rps-malicious_2";
1632       mal_type = 2;
1633     }
1634     else if (strstr (argv[0], "_3") != NULL)
1635     {
1636       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1637       cur_test_run.name = "test-rps-malicious_3";
1638       mal_type = 3;
1639     }
1640   }
1641
1642   else if (strstr (argv[0], "_single_req") != NULL)
1643   {
1644     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1645     cur_test_run.name = "test-rps-single-req";
1646     cur_test_run.main_test = single_req_cb;
1647     cur_test_run.have_churn = GNUNET_NO;
1648   }
1649
1650   else if (strstr (argv[0], "_delayed_reqs") != NULL)
1651   {
1652     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1653     cur_test_run.name = "test-rps-delayed-reqs";
1654     cur_test_run.main_test = delay_req_cb;
1655     cur_test_run.have_churn = GNUNET_NO;
1656   }
1657
1658   else if (strstr (argv[0], "_seed_big") != NULL)
1659   {
1660     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
1661     num_peers = 1;
1662     cur_test_run.name = "test-rps-seed-big";
1663     cur_test_run.main_test = seed_big_cb;
1664     cur_test_run.eval_cb = no_eval;
1665     cur_test_run.have_churn = GNUNET_NO;
1666     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1667   }
1668
1669   else if (strstr (argv[0], "_single_peer_seed") != NULL)
1670   {
1671     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1672     cur_test_run.name = "test-rps-single-peer-seed";
1673     cur_test_run.main_test = single_peer_seed_cb;
1674     cur_test_run.have_churn = GNUNET_NO;
1675   }
1676
1677   else if (strstr (argv[0], "_seed_request") != NULL)
1678   {
1679     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1680     cur_test_run.name = "test-rps-seed-request";
1681     cur_test_run.main_test = seed_req_cb;
1682     cur_test_run.have_churn = GNUNET_NO;
1683   }
1684
1685   else if (strstr (argv[0], "_seed") != NULL)
1686   {
1687     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1688     cur_test_run.name = "test-rps-seed";
1689     cur_test_run.main_test = seed_cb;
1690     cur_test_run.eval_cb = no_eval;
1691     cur_test_run.have_churn = GNUNET_NO;
1692   }
1693
1694   else if (strstr (argv[0], "_req_cancel") != NULL)
1695   {
1696     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1697     cur_test_run.name = "test-rps-req-cancel";
1698     num_peers = 1;
1699     cur_test_run.main_test = req_cancel_cb;
1700     cur_test_run.eval_cb = no_eval;
1701     cur_test_run.have_churn = GNUNET_NO;
1702     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1703   }
1704
1705   else if (strstr (argv[0], "_churn") != NULL)
1706   {
1707     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
1708     cur_test_run.name = "test-rps-churn";
1709     num_peers = 5;
1710     cur_test_run.init_peer = default_init_peer;
1711     cur_test_run.main_test = churn_test_cb;
1712     cur_test_run.reply_handle = default_reply_handle;
1713     cur_test_run.eval_cb = default_eval_cb;
1714     cur_test_run.have_churn = GNUNET_YES;
1715     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1716   }
1717
1718   else if (strstr (argv[0], "profiler") != NULL)
1719   {
1720     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1721     cur_test_run.name = "test-rps-profiler";
1722     num_peers = 10;
1723     mal_type = 3;
1724     cur_test_run.init_peer = profiler_init_peer;
1725     cur_test_run.pre_test = mal_pre;
1726     cur_test_run.main_test = profiler_cb;
1727     cur_test_run.reply_handle = profiler_reply_handle;
1728     cur_test_run.eval_cb = profiler_eval;
1729     cur_test_run.request_interval = 2;
1730     cur_test_run.num_requests = 5;
1731     cur_test_run.have_churn = GNUNET_YES;
1732     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1733
1734     /* 'Clean' directory */
1735     (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1736     GNUNET_DISK_directory_create ("/tmp/rps/");
1737   }
1738
1739   rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1740   peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1741   rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1742   if ( (2 == mal_type) ||
1743        (3 == mal_type))
1744     target_peer = &rps_peer_ids[num_peers - 2];
1745   if (profiler_eval == cur_test_run.eval_cb)
1746     eval_peer = &rps_peers[num_peers - 1];    /* FIXME: eval_peer could be a
1747                                                  malicious peer if not careful
1748                                                  with the malicious portion */
1749
1750   ok = 1;
1751   ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
1752                                        "test_rps.conf",
1753                                        num_peers,
1754                                        0, NULL, NULL,
1755                                        &run, NULL);
1756   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1757               "_test_run returned.\n");
1758   if (GNUNET_OK != ret_value)
1759   {
1760     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1761                 "Test did not run successfully!\n");
1762   }
1763
1764   ret_value = cur_test_run.eval_cb();
1765   GNUNET_free (rps_peers);
1766   GNUNET_free (rps_peer_ids);
1767   GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1768   return ret_value;
1769 }
1770
1771 /* end of test_rps.c */