b8f7588e101f78d96026321d97984c1cfd9a62d8
[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 NSE service at the peer set this to 1
88    * or -1
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
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
224 /**
225  * Information for all the peers.
226  */
227 static struct RPSPeer *rps_peers;
228
229 /**
230  * Peermap to get the index of a given peer ID quick.
231  */
232 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
233
234 /**
235  * IDs of the peers.
236  */
237 static struct GNUNET_PeerIdentity *rps_peer_ids;
238
239 /**
240  * ID of the targeted peer.
241  */
242 static struct GNUNET_PeerIdentity *target_peer;
243
244 /**
245  * ID of the peer that requests for the evaluation.
246  */
247 static struct RPSPeer *eval_peer;
248
249 /**
250  * Number of online peers.
251  */
252 static unsigned int num_peers_online;
253
254 /**
255  * Return value from 'main'.
256  */
257 static int ok;
258
259 /**
260  * Identifier for the churn task that runs periodically
261  */
262 static struct GNUNET_SCHEDULER_Task *churn_task;
263
264 /**
265  * Called to initialise the given RPSPeer
266  */
267 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
268
269 /**
270  * Called directly after connecting to the service
271  */
272 typedef void (*PreTest) (void *cls, struct GNUNET_RPS_Handle *h);
273
274 /**
275  * Called from within #rps_connect_complete_cb ()
276  * Executes functions to test the api/service
277  */
278 typedef void (*MainTest) (struct RPSPeer *rps_peer);
279
280 /**
281  * Callback called once the requested random peers are available
282  */
283 typedef void (*ReplyHandle) (void *cls,
284                              uint64_t n,
285                              const struct GNUNET_PeerIdentity *recv_peers);
286
287 /**
288  * Called directly before disconnecting from the service
289  */
290 typedef void (*PostTest) (void *cls, struct GNUNET_RPS_Handle *h);
291
292 /**
293  * Function called after disconnect to evaluate test success
294  */
295 typedef int (*EvaluationCallback) (void);
296
297
298 /**
299  * Structure to define a single test
300  */
301 struct SingleTestRun
302 {
303   /**
304    * Name of the test
305    */
306   char *name;
307
308   /**
309    * Called to initialise peer
310    */
311   InitPeer init_peer;
312
313   /**
314    * Called directly after connecting to the service
315    */
316   PreTest pre_test;
317
318   /**
319    * Function to execute the functions to be tested
320    */
321   MainTest main_test;
322
323   /**
324    * Callback called once the requested peers are available
325    */
326   ReplyHandle reply_handle;
327
328   /**
329    * Called directly before disconnecting from the service
330    */
331   PostTest post_test;
332
333   /**
334    * Function to evaluate the test results
335    */
336   EvaluationCallback eval_cb;
337
338   /**
339    * Request interval
340    */
341   uint32_t request_interval;
342
343   /**
344    * Number of Requests to make.
345    */
346   uint32_t num_requests;
347
348   /**
349    * Run with churn
350    */
351   int have_churn;
352 } cur_test_run;
353
354 /**
355  * Are we shutting down?
356  */
357 static int in_shutdown;
358
359 /**
360  * Append arguments to file
361  */
362 static void
363 tofile_ (const char *file_name, const char *line)
364 {
365   struct GNUNET_DISK_FileHandle *f;
366   /* char output_buffer[512]; */
367   size_t size;
368   /* int size; */
369   size_t size2;
370
371   if (NULL == (f = GNUNET_DISK_file_open (file_name,
372                                           GNUNET_DISK_OPEN_APPEND |
373                                           GNUNET_DISK_OPEN_WRITE |
374                                           GNUNET_DISK_OPEN_CREATE,
375                                           GNUNET_DISK_PERM_USER_READ |
376                                           GNUNET_DISK_PERM_USER_WRITE |
377                                           GNUNET_DISK_PERM_GROUP_READ |
378                                           GNUNET_DISK_PERM_OTHER_READ)))
379   {
380     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
381                 "Not able to open file %s\n",
382                 file_name);
383     return;
384   }
385   /* size = GNUNET_snprintf (output_buffer,
386                           sizeof (output_buffer),
387                           "%llu %s\n",
388                           GNUNET_TIME_absolute_get ().abs_value_us,
389                           line);
390   if (0 > size)
391   {
392     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
393                 "Failed to write string to buffer (size: %i)\n",
394                 size);
395     return;
396   } */
397
398   size = strlen (line) * sizeof (char);
399
400   size2 = GNUNET_DISK_file_write (f, line, size);
401   if (size != size2)
402   {
403     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
404                 "Unable to write to file! (Size: %lu, size2: %lu)\n",
405                 size,
406                 size2);
407     if (GNUNET_YES != GNUNET_DISK_file_close (f))
408     {
409       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
410                   "Unable to close file\n");
411     }
412     return;
413   }
414
415   if (GNUNET_YES != GNUNET_DISK_file_close (f))
416   {
417     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
418                 "Unable to close file\n");
419   }
420 }
421
422 /**
423  * This function is used to facilitate writing important information to disk
424  */
425 #define tofile(file_name, ...) do {\
426   char tmp_buf[512];\
427     int size;\
428     size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
429     if (0 > size)\
430       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
431                      "Failed to create tmp_buf\n");\
432     else\
433       tofile_(file_name,tmp_buf);\
434   } while (0);
435
436
437 /**
438  * Write the ids and their according index in the given array to a file
439  * Unused
440  */
441 /* static void
442 ids_to_file (char *file_name,
443              struct GNUNET_PeerIdentity *peer_ids,
444              unsigned int num_peer_ids)
445 {
446   unsigned int i;
447
448   for (i=0 ; i < num_peer_ids ; i++)
449   {
450     to_file (file_name,
451              "%u\t%s",
452              i,
453              GNUNET_i2s_full (&peer_ids[i]));
454   }
455 } */
456
457 /**
458  * Test the success of a single test
459  */
460 static int
461 evaluate (void)
462 {
463   unsigned int i;
464   int tmp_ok;
465
466   tmp_ok = 1;
467
468   for (i = 0; i < num_peers; i++)
469   {
470     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471         "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
472         i,
473         GNUNET_i2s (rps_peers[i].peer_id),
474         rps_peers[i].num_recv_ids,
475         rps_peers[i].num_ids_to_request,
476         (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
477     tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
478   }
479   return tmp_ok? 0 : 1;
480 }
481
482
483 /**
484  * Creates an oplist entry and adds it to the oplist DLL
485  */
486 static struct OpListEntry *
487 make_oplist_entry ()
488 {
489   struct OpListEntry *entry;
490
491   entry = GNUNET_new (struct OpListEntry);
492   GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
493   return entry;
494 }
495
496
497 /**
498  * Task run on timeout to shut everything down.
499  */
500 static void
501 shutdown_op (void *cls)
502 {
503   unsigned int i;
504
505   in_shutdown = GNUNET_YES;
506   if (NULL != churn_task)
507   {
508     GNUNET_SCHEDULER_cancel (churn_task);
509     churn_task = NULL;
510   }
511   for (i = 0; i < num_peers; i++)
512     if (NULL != rps_peers[i].op)
513       GNUNET_TESTBED_operation_done (rps_peers[i].op);
514   GNUNET_SCHEDULER_shutdown ();
515 }
516
517
518 /**
519  * Seed peers.
520  */
521 static void
522 seed_peers (void *cls)
523 {
524   struct RPSPeer *peer = cls;
525   unsigned int amount;
526   unsigned int i;
527
528   // TODO if malicious don't seed mal peers
529   amount = round (.5 * num_peers);
530
531   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
532   for (i = 0 ; i < amount ; i++)
533     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
534                 i,
535                 GNUNET_i2s (&rps_peer_ids[i]));
536
537   GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
538 }
539
540
541 /**
542  * Seed peers.
543  */
544 static void
545 seed_peers_big (void *cls)
546 {
547   struct RPSPeer *peer = cls;
548   unsigned int seed_msg_size;
549   uint32_t num_peers_max;
550   unsigned int amount;
551   unsigned int i;
552
553   seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
554   num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - seed_msg_size) /
555     sizeof (struct GNUNET_PeerIdentity);
556   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557       "Peers that fit in one seed msg; %u\n",
558       num_peers_max);
559   amount = num_peers_max + (0.5 * num_peers_max);
560   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561       "Seeding many (%u) peers:\n",
562       amount);
563   struct GNUNET_PeerIdentity ids_to_seed[amount];
564   for (i = 0; i < amount; i++)
565   {
566     ids_to_seed[i] = *peer->peer_id;
567     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
568                 i,
569                 GNUNET_i2s (&ids_to_seed[i]));
570   }
571
572   GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
573 }
574
575 /**
576  * Get the id of peer i.
577  */
578   void
579 info_cb (void *cb_cls,
580          struct GNUNET_TESTBED_Operation *op,
581          const struct GNUNET_TESTBED_PeerInformation *pinfo,
582          const char *emsg)
583 {
584   struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
585
586   if (NULL == pinfo || NULL != emsg)
587   {
588     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
589     GNUNET_TESTBED_operation_done (entry->op);
590     return;
591   }
592
593   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
594               "Peer %u is %s\n",
595               entry->index,
596               GNUNET_i2s (pinfo->result.id));
597
598   rps_peer_ids[entry->index] = *(pinfo->result.id);
599   rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
600
601   GNUNET_assert (GNUNET_OK ==
602       GNUNET_CONTAINER_multipeermap_put (peer_map,
603         &rps_peer_ids[entry->index],
604         &rps_peers[entry->index],
605         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
606   tofile ("/tmp/rps/peer_ids",
607            "%u\t%s\n",
608            entry->index,
609            GNUNET_i2s_full (&rps_peer_ids[entry->index]));
610
611   GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
612   GNUNET_TESTBED_operation_done (entry->op);
613   GNUNET_free (entry);
614 }
615
616
617 /**
618  * Callback to be called when RPS service connect operation is completed
619  *
620  * @param cls the callback closure from functions generating an operation
621  * @param op the operation that has been finished
622  * @param ca_result the RPS service handle returned from rps_connect_adapter
623  * @param emsg error message in case the operation has failed; will be NULL if
624  *          operation has executed successfully.
625  */
626 static void
627 rps_connect_complete_cb (void *cls,
628                          struct GNUNET_TESTBED_Operation *op,
629                          void *ca_result,
630                          const char *emsg)
631 {
632   struct RPSPeer *rps_peer = cls;
633   struct GNUNET_RPS_Handle *rps = ca_result;
634
635   rps_peer->rps_handle = rps;
636   rps_peer->online = GNUNET_YES;
637   num_peers_online++;
638
639   GNUNET_assert (op == rps_peer->op);
640   if (NULL != emsg)
641   {
642     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
643                 "Failed to connect to RPS service: %s\n",
644                 emsg);
645     ok = 1;
646     GNUNET_SCHEDULER_shutdown ();
647     return;
648   }
649
650   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
651
652   cur_test_run.main_test (rps_peer);
653 }
654
655
656 /**
657  * Adapter function called to establish a connection to
658  * the RPS service.
659  *
660  * @param cls closure
661  * @param cfg configuration of the peer to connect to; will be available until
662  *          GNUNET_TESTBED_operation_done() is called on the operation returned
663  *          from GNUNET_TESTBED_service_connect()
664  * @return service handle to return in 'op_result', NULL on error
665  */
666 static void *
667 rps_connect_adapter (void *cls,
668                                  const struct GNUNET_CONFIGURATION_Handle *cfg)
669 {
670   struct GNUNET_RPS_Handle *h;
671
672   h = GNUNET_RPS_connect (cfg);
673
674   if (NULL != cur_test_run.pre_test)
675     cur_test_run.pre_test (cls, h);
676
677   return h;
678 }
679
680
681 /**
682  * Adapter function called to destroy connection to
683  * RPS service.
684  *
685  * @param cls closure
686  * @param op_result service handle returned from the connect adapter
687  */
688 static void
689 rps_disconnect_adapter (void *cls,
690                                           void *op_result)
691 {
692   struct RPSPeer *peer = cls;
693   struct GNUNET_RPS_Handle *h = op_result;
694   GNUNET_assert (NULL != peer);
695   GNUNET_RPS_disconnect (h);
696   peer->rps_handle = NULL;
697 }
698
699
700 /***********************************************************************
701  * Definition of tests
702 ***********************************************************************/
703
704 // TODO check whether tests can be stopped earlier
705 static int
706 default_eval_cb (void)
707 {
708   return evaluate ();
709 }
710
711 static int
712 no_eval (void)
713 {
714   return 0;
715 }
716
717 /**
718  * Initialise given RPSPeer
719  */
720 static void default_init_peer (struct RPSPeer *rps_peer)
721 {
722   rps_peer->num_ids_to_request = 1;
723 }
724
725 /**
726  * Callback to call on receipt of a reply
727  *
728  * @param cls closure
729  * @param n number of peers
730  * @param recv_peers the received peers
731  */
732 static void
733 default_reply_handle (void *cls,
734                       uint64_t n,
735                       const struct GNUNET_PeerIdentity *recv_peers)
736 {
737   struct RPSPeer *rps_peer;
738   struct PendingReply *pending_rep = (struct PendingReply *) cls;
739   unsigned int i;
740
741   rps_peer = pending_rep->rps_peer;
742   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
743                                rps_peer->pending_rep_tail,
744                                pending_rep);
745   rps_peer->num_pending_reps--;
746   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747               "[%s] got %" PRIu64 " peers:\n",
748               GNUNET_i2s (rps_peer->peer_id),
749               n);
750
751   for (i = 0; i < n; i++)
752   {
753     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754                 "%u: %s\n",
755                 i,
756                 GNUNET_i2s (&recv_peers[i]));
757
758     rps_peer->num_recv_ids++;
759   }
760
761   if (0 == evaluate ())
762   {
763     GNUNET_SCHEDULER_shutdown ();
764   }
765 }
766
767 /**
768  * Request random peers.
769  */
770 static void
771 request_peers (void *cls)
772 {
773   struct PendingRequest *pending_req = cls;
774   struct RPSPeer *rps_peer;
775   struct PendingReply *pending_rep;
776
777   if (GNUNET_YES == in_shutdown)
778     return;
779   rps_peer = pending_req->rps_peer;
780   GNUNET_assert (1 <= rps_peer->num_pending_reqs);
781   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
782                                rps_peer->pending_req_tail,
783                                pending_req);
784   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785               "Requesting one peer\n");
786   pending_rep = GNUNET_new (struct PendingReply);
787   pending_rep->rps_peer = rps_peer;
788   pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
789       1,
790       cur_test_run.reply_handle,
791       pending_rep);
792   GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
793                                     rps_peer->pending_rep_tail,
794                                     pending_rep);
795   rps_peer->num_pending_reps++;
796   rps_peer->num_pending_reqs--;
797 }
798
799 static void
800 cancel_pending_req (struct PendingRequest *pending_req)
801 {
802   struct RPSPeer *rps_peer;
803
804   rps_peer = pending_req->rps_peer;
805   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
806                                rps_peer->pending_req_tail,
807                                pending_req);
808   rps_peer->num_pending_reqs--;
809   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810               "Cancelling pending request\n");
811   GNUNET_SCHEDULER_cancel (pending_req->request_task);
812   GNUNET_free (pending_req);
813 }
814
815 static void
816 cancel_request (struct PendingReply *pending_rep)
817 {
818   struct RPSPeer *rps_peer;
819
820   rps_peer = pending_rep->rps_peer;
821   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
822                                rps_peer->pending_rep_tail,
823                                pending_rep);
824   rps_peer->num_pending_reps--;
825   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826               "Cancelling request\n");
827   GNUNET_RPS_request_cancel (pending_rep->req_handle);
828   GNUNET_free (pending_rep);
829 }
830
831 /**
832  * Cancel a request.
833  */
834 static void
835 cancel_request_cb (void *cls)
836 {
837   struct RPSPeer *rps_peer = cls;
838   struct PendingReply *pending_rep;
839
840   if (GNUNET_YES == in_shutdown)
841     return;
842   pending_rep = rps_peer->pending_rep_head;
843   GNUNET_assert (1 <= rps_peer->num_pending_reps);
844   cancel_request (pending_rep);
845 }
846
847
848 /**
849  * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
850  * issued, nor replied
851  */
852 void
853 schedule_missing_requests (struct RPSPeer *rps_peer)
854 {
855   unsigned int i;
856   struct PendingRequest *pending_req;
857
858   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859       "Scheduling %u - %u missing requests\n",
860       rps_peer->num_ids_to_request,
861       rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
862   GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
863       rps_peer->num_ids_to_request);
864   for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
865        i < rps_peer->num_ids_to_request; i++)
866   {
867     pending_req = GNUNET_new (struct PendingRequest);
868     pending_req->rps_peer = rps_peer;
869     pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
870         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
871           cur_test_run.request_interval * i),
872         request_peers,
873         pending_req);
874     GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
875                                       rps_peer->pending_req_tail,
876                                       pending_req);
877     rps_peer->num_pending_reqs++;
878   }
879 }
880
881 void
882 cancel_pending_req_rep (struct RPSPeer *rps_peer)
883 {
884   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885       "Cancelling all (pending) requests.\n");
886   while (NULL != rps_peer->pending_req_head)
887     cancel_pending_req (rps_peer->pending_req_head);
888   GNUNET_assert (0 == rps_peer->num_pending_reqs);
889   while (NULL != rps_peer->pending_rep_head)
890     cancel_request (rps_peer->pending_rep_head);
891   GNUNET_assert (0 == rps_peer->num_pending_reps);
892 }
893
894 /***********************************
895  * MALICIOUS
896 ***********************************/
897
898 /**
899  * Initialise only non-mal RPSPeers
900  */
901 static void mal_init_peer (struct RPSPeer *rps_peer)
902 {
903   if (rps_peer->index >= round (portion * num_peers))
904     rps_peer->num_ids_to_request = 1;
905 }
906
907 static void
908 mal_pre (void *cls, struct GNUNET_RPS_Handle *h)
909 {
910   #ifdef ENABLE_MALICIOUS
911   uint32_t num_mal_peers;
912   struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
913
914   GNUNET_assert ( (1 >= portion) &&
915                   (0 <  portion) );
916   num_mal_peers = round (portion * num_peers);
917
918   if (rps_peer->index < num_mal_peers)
919   {
920     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921                 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
922                 rps_peer->index,
923                 GNUNET_i2s (rps_peer->peer_id),
924                 num_mal_peers);
925
926     GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
927                               rps_peer_ids, target_peer);
928   }
929   #endif /* ENABLE_MALICIOUS */
930 }
931
932 static void
933 mal_cb (struct RPSPeer *rps_peer)
934 {
935   uint32_t num_mal_peers;
936
937   #ifdef ENABLE_MALICIOUS
938   GNUNET_assert ( (1 >= portion) &&
939                   (0 <  portion) );
940   num_mal_peers = round (portion * num_peers);
941
942   if (rps_peer->index >= num_mal_peers)
943   { /* It's useless to ask a malicious peer about a random sample -
944        it's not sampling */
945     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
946                                   seed_peers, rps_peer);
947     schedule_missing_requests (rps_peer);
948   }
949   #endif /* ENABLE_MALICIOUS */
950 }
951
952
953 /***********************************
954  * SINGLE_REQUEST
955 ***********************************/
956 static void
957 single_req_cb (struct RPSPeer *rps_peer)
958 {
959   schedule_missing_requests (rps_peer);
960 }
961
962 /***********************************
963  * DELAYED_REQUESTS
964 ***********************************/
965 static void
966 delay_req_cb (struct RPSPeer *rps_peer)
967 {
968   schedule_missing_requests (rps_peer);
969 }
970
971 /***********************************
972  * SEED
973 ***********************************/
974 static void
975 seed_cb (struct RPSPeer *rps_peer)
976 {
977   GNUNET_SCHEDULER_add_delayed (
978       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
979       seed_peers, rps_peer);
980 }
981
982 /***********************************
983  * SEED_BIG
984 ***********************************/
985 static void
986 seed_big_cb (struct RPSPeer *rps_peer)
987 {
988   // TODO test seeding > GNUNET_MAX_MESSAGE_SIZE peers
989   GNUNET_SCHEDULER_add_delayed (
990       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
991       seed_peers_big, rps_peer);
992 }
993
994 /***********************************
995  * SINGLE_PEER_SEED
996 ***********************************/
997 static void
998 single_peer_seed_cb (struct RPSPeer *rps_peer)
999 {
1000   // TODO
1001 }
1002
1003 /***********************************
1004  * SEED_REQUEST
1005 ***********************************/
1006 static void
1007 seed_req_cb (struct RPSPeer *rps_peer)
1008 {
1009   GNUNET_SCHEDULER_add_delayed (
1010       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1011       seed_peers, rps_peer);
1012   schedule_missing_requests (rps_peer);
1013 }
1014
1015 //TODO start big mal
1016
1017 /***********************************
1018  * REQUEST_CANCEL
1019 ***********************************/
1020 static void
1021 req_cancel_cb (struct RPSPeer *rps_peer)
1022 {
1023   schedule_missing_requests (rps_peer);
1024   GNUNET_SCHEDULER_add_delayed (
1025       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1026                                      (cur_test_run.request_interval + 1)),
1027       cancel_request_cb, rps_peer);
1028 }
1029
1030 /***********************************
1031  * PROFILER
1032 ***********************************/
1033
1034 /**
1035  * Callback to be called when RPS service is started or stopped at peers
1036  *
1037  * @param cls NULL
1038  * @param op the operation handle
1039  * @param emsg NULL on success; otherwise an error description
1040  */
1041 static void
1042 churn_cb (void *cls,
1043           struct GNUNET_TESTBED_Operation *op,
1044           const char *emsg)
1045 {
1046   // FIXME
1047   struct OpListEntry *entry = cls;
1048
1049   GNUNET_TESTBED_operation_done (entry->op);
1050   if (NULL != emsg)
1051   {
1052     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1053     GNUNET_SCHEDULER_shutdown ();
1054     return;
1055   }
1056   GNUNET_assert (0 != entry->delta);
1057
1058   num_peers_online += entry->delta;
1059
1060   if (0 > entry->delta)
1061   { /* Peer hopefully just went offline */
1062     if (GNUNET_YES != rps_peers[entry->index].online)
1063     {
1064       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1065                   "peer %s was expected to go offline but is still marked as online\n",
1066                   GNUNET_i2s (rps_peers[entry->index].peer_id));
1067       GNUNET_break (0);
1068     }
1069     else
1070     {
1071       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072                   "peer %s probably went offline as expected\n",
1073                   GNUNET_i2s (rps_peers[entry->index].peer_id));
1074     }
1075     rps_peers[entry->index].online = GNUNET_NO;
1076   }
1077
1078   else if (0 < entry->delta)
1079   { /* Peer hopefully just went online */
1080     if (GNUNET_NO != rps_peers[entry->index].online)
1081     {
1082       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1083                   "peer %s was expected to go online but is still marked as offline\n",
1084                   GNUNET_i2s (rps_peers[entry->index].peer_id));
1085       GNUNET_break (0);
1086     }
1087     else
1088     {
1089       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090                   "peer %s probably went online as expected\n",
1091                   GNUNET_i2s (rps_peers[entry->index].peer_id));
1092       if (NULL != cur_test_run.pre_test)
1093       {
1094         cur_test_run.pre_test (&rps_peers[entry->index],
1095             rps_peers[entry->index].rps_handle);
1096         schedule_missing_requests (&rps_peers[entry->index]);
1097       }
1098     }
1099     rps_peers[entry->index].online = GNUNET_YES;
1100   }
1101
1102   GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1103   GNUNET_free (entry);
1104   //if (num_peers_in_round[current_round] == peers_running)
1105   //  run_round ();
1106 }
1107
1108 static void
1109 manage_service_wrapper (unsigned int i, unsigned int j, int delta,
1110     double prob_go_on_off)
1111 {
1112   struct OpListEntry *entry;
1113   uint32_t prob;
1114
1115   prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1116                                    UINT32_MAX);
1117   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118               "%u. selected peer (%u: %s) is %s.\n",
1119               i,
1120               j,
1121               GNUNET_i2s (rps_peers[j].peer_id),
1122               (0 > delta) ? "online" : "offline");
1123   if (prob < prob_go_on_off * UINT32_MAX)
1124   {
1125     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126                 "%s goes %s\n",
1127                 GNUNET_i2s (rps_peers[j].peer_id),
1128                 (0 > delta) ? "offline" : "online");
1129
1130     if (0 > delta)
1131       cancel_pending_req_rep (&rps_peers[j]);
1132     entry = make_oplist_entry ();
1133     entry->delta = delta;
1134     entry->index = j;
1135     entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1136                                                     testbed_peers[j],
1137                                                     "rps",
1138                                                     &churn_cb,
1139                                                     entry,
1140                                                     (0 > delta) ? 0 : 1);
1141   }
1142 }
1143
1144
1145 static void
1146 churn (void *cls)
1147 {
1148   unsigned int i;
1149   unsigned int j;
1150   double portion_online;
1151   unsigned int *permut;
1152   double prob_go_offline;
1153   double portion_go_online;
1154   double portion_go_offline;
1155
1156   /* Compute the probability for an online peer to go offline
1157    * this round */
1158   portion_online = num_peers_online * 1.0 / num_peers;
1159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160               "Portion online: %f\n",
1161               portion_online);
1162   portion_go_online = ((1 - portion_online) * .5 * .66);
1163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164               "Portion that should go online: %f\n",
1165               portion_go_online);
1166   portion_go_offline = (portion_online + portion_go_online) - .75;
1167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168               "Portion that probably goes offline: %f\n",
1169               portion_go_offline);
1170   prob_go_offline = portion_go_offline / (portion_online * .5);
1171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1172               "Probability of a selected online peer to go offline: %f\n",
1173               prob_go_offline);
1174
1175   permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1176                                          (unsigned int) num_peers);
1177
1178   /* Go over 50% randomly chosen peers */
1179   for (i = 0; i < .5 * num_peers; i++)
1180   {
1181     j = permut[i];
1182
1183     /* If online, shut down with certain probability */
1184     if (GNUNET_YES == rps_peers[j].online)
1185     {
1186       manage_service_wrapper (i, j, -1, prob_go_offline);
1187     }
1188
1189     /* If offline, restart with certain probability */
1190     else if (GNUNET_NO == rps_peers[j].online)
1191     {
1192       manage_service_wrapper (i, j, 1, 0.66);
1193     }
1194   }
1195
1196   GNUNET_free (permut);
1197
1198   churn_task = GNUNET_SCHEDULER_add_delayed (
1199         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1200         churn,
1201         NULL);
1202 }
1203
1204
1205 /**
1206  * Initialise given RPSPeer
1207  */
1208 static void profiler_init_peer (struct RPSPeer *rps_peer)
1209 {
1210   if (num_peers - 1 == rps_peer->index)
1211     rps_peer->num_ids_to_request = cur_test_run.num_requests;
1212 }
1213
1214
1215 /**
1216  * Callback to call on receipt of a reply
1217  *
1218  * @param cls closure
1219  * @param n number of peers
1220  * @param recv_peers the received peers
1221  */
1222 static void
1223 profiler_reply_handle (void *cls,
1224                       uint64_t n,
1225                       const struct GNUNET_PeerIdentity *recv_peers)
1226 {
1227   struct RPSPeer *rps_peer;
1228   struct RPSPeer *rcv_rps_peer;
1229   char *file_name;
1230   char *file_name_dh;
1231   unsigned int i;
1232   struct PendingReply *pending_rep = (struct PendingReply *) cls;
1233
1234   rps_peer = pending_rep->rps_peer;
1235   file_name = "/tmp/rps/received_ids";
1236   file_name_dh = "/tmp/rps/diehard_input";
1237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238               "[%s] got %" PRIu64 " peers:\n",
1239               GNUNET_i2s (rps_peer->peer_id),
1240               n);
1241   for (i = 0; i < n; i++)
1242   {
1243     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1244                 "%u: %s\n",
1245                 i,
1246                 GNUNET_i2s (&recv_peers[i]));
1247     tofile (file_name,
1248              "%s\n",
1249              GNUNET_i2s_full (&recv_peers[i]));
1250     rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1251     GNUNET_assert (NULL != rcv_rps_peer);
1252     tofile (file_name_dh,
1253              "%" PRIu32 "\n",
1254              (uint32_t) rcv_rps_peer->index);
1255   }
1256   default_reply_handle (cls, n, recv_peers);
1257 }
1258
1259
1260 static void
1261 profiler_cb (struct RPSPeer *rps_peer)
1262 {
1263   /* Start churn */
1264   if (GNUNET_YES == cur_test_run.have_churn && NULL == churn_task)
1265   {
1266     churn_task = GNUNET_SCHEDULER_add_delayed (
1267           GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1268           churn,
1269           NULL);
1270   }
1271
1272   /* Only request peer ids at one peer.
1273    * (It's the before-last because last one is target of the focussed attack.)
1274    */
1275   if (eval_peer == rps_peer)
1276     schedule_missing_requests (rps_peer);
1277 }
1278
1279 /**
1280  * Function called from #profiler_eval with a filename.
1281  *
1282  * @param cls closure
1283  * @param filename complete filename (absolute path)
1284  * @return #GNUNET_OK to continue to iterate,
1285  *  #GNUNET_NO to stop iteration with no error,
1286  *  #GNUNET_SYSERR to abort iteration with error!
1287  */
1288 int
1289 file_name_cb (void *cls, const char *filename)
1290 {
1291   if (NULL != strstr (filename, "sampler_el"))
1292   {
1293     struct RPS_SamplerElement *s_elem;
1294     struct GNUNET_CRYPTO_AuthKey auth_key;
1295     const char *key_char;
1296     uint32_t i;
1297
1298     key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1299     tofile (filename, "--------------------------\n");
1300
1301     auth_key = string_to_auth_key (key_char);
1302     s_elem = RPS_sampler_elem_create ();
1303     RPS_sampler_elem_set (s_elem, auth_key);
1304
1305     for (i = 0; i < num_peers; i++)
1306     {
1307       RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1308     }
1309     RPS_sampler_elem_destroy (s_elem);
1310   }
1311   return GNUNET_OK;
1312 }
1313
1314 /**
1315  * This is run after the test finished.
1316  *
1317  * Compute all perfect samples.
1318  */
1319 int
1320 profiler_eval (void)
1321 {
1322   /* Compute perfect sample for each sampler element */
1323   if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1324   {
1325     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1326   }
1327
1328   return evaluate ();
1329 }
1330
1331
1332 /***********************************************************************
1333  * /Definition of tests
1334 ***********************************************************************/
1335
1336
1337 /**
1338  * Actual "main" function for the testcase.
1339  *
1340  * @param cls closure
1341  * @param h the run handle
1342  * @param n_peers number of peers in 'peers'
1343  * @param peers handle to peers run in the testbed
1344  * @param links_succeeded the number of overlay link connection attempts that
1345  *          succeeded
1346  * @param links_failed the number of overlay link connection attempts that
1347  *          failed
1348  */
1349 static void
1350 run (void *cls,
1351      struct GNUNET_TESTBED_RunHandle *h,
1352      unsigned int n_peers,
1353      struct GNUNET_TESTBED_Peer **peers,
1354      unsigned int links_succeeded,
1355      unsigned int links_failed)
1356 {
1357   unsigned int i;
1358   struct OpListEntry *entry;
1359   uint32_t num_mal_peers;
1360
1361   testbed_peers = peers;
1362   num_peers_online = 0;
1363   for (i = 0; i < num_peers; i++)
1364   {
1365     entry = make_oplist_entry ();
1366     entry->index = i;
1367     rps_peers[i].index = i;
1368     if (NULL != cur_test_run.init_peer)
1369       cur_test_run.init_peer (&rps_peers[i]);
1370     entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
1371                                                      GNUNET_TESTBED_PIT_IDENTITY,
1372                                                      &info_cb,
1373                                                      entry);
1374   }
1375
1376   num_mal_peers = round (portion * num_peers);
1377   GNUNET_assert (num_peers == n_peers);
1378   for (i = 0; i < n_peers; i++)
1379   {
1380     rps_peers[i].index = i;
1381     if ( (rps_peers[i].num_recv_ids < rps_peers[i].num_ids_to_request) ||
1382          (i < num_mal_peers) )
1383     {
1384       rps_peers[i].op =
1385         GNUNET_TESTBED_service_connect (&rps_peers[i],
1386                                         peers[i],
1387                                         "rps",
1388                                         &rps_connect_complete_cb,
1389                                         &rps_peers[i],
1390                                         &rps_connect_adapter,
1391                                         &rps_disconnect_adapter,
1392                                         &rps_peers[i]);
1393     }
1394   }
1395
1396   if (NULL != churn_task)
1397     GNUNET_SCHEDULER_cancel (churn_task);
1398   GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
1399 }
1400
1401
1402 /**
1403  * Entry point for the testcase, sets up the testbed.
1404  *
1405  * @param argc unused
1406  * @param argv unused
1407  * @return 0 on success
1408  */
1409 int
1410 main (int argc, char *argv[])
1411 {
1412   int ret_value;
1413
1414   num_peers = 5;
1415   cur_test_run.name = "test-rps-default";
1416   cur_test_run.init_peer = default_init_peer;
1417   cur_test_run.pre_test = NULL;
1418   cur_test_run.reply_handle = default_reply_handle;
1419   cur_test_run.eval_cb = default_eval_cb;
1420   cur_test_run.have_churn = GNUNET_YES;
1421   churn_task = NULL;
1422   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
1423
1424   if (strstr (argv[0], "malicious") != NULL)
1425   {
1426     cur_test_run.pre_test = mal_pre;
1427     cur_test_run.main_test = mal_cb;
1428     cur_test_run.init_peer = mal_init_peer;
1429
1430     if (strstr (argv[0], "_1") != NULL)
1431     {
1432       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
1433       cur_test_run.name = "test-rps-malicious_1";
1434       mal_type = 1;
1435     }
1436     else if (strstr (argv[0], "_2") != NULL)
1437     {
1438       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
1439       cur_test_run.name = "test-rps-malicious_2";
1440       mal_type = 2;
1441     }
1442     else if (strstr (argv[0], "_3") != NULL)
1443     {
1444       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
1445       cur_test_run.name = "test-rps-malicious_3";
1446       mal_type = 3;
1447     }
1448   }
1449
1450   else if (strstr (argv[0], "_single_req") != NULL)
1451   {
1452     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
1453     cur_test_run.name = "test-rps-single-req";
1454     cur_test_run.main_test = single_req_cb;
1455     cur_test_run.have_churn = GNUNET_NO;
1456   }
1457
1458   else if (strstr (argv[0], "_delayed_reqs") != NULL)
1459   {
1460     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
1461     cur_test_run.name = "test-rps-delayed-reqs";
1462     cur_test_run.main_test = delay_req_cb;
1463     cur_test_run.have_churn = GNUNET_NO;
1464   }
1465
1466   else if (strstr (argv[0], "_seed_big") != NULL)
1467   {
1468     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
1469     num_peers = 1;
1470     cur_test_run.name = "test-rps-seed-big";
1471     cur_test_run.main_test = seed_big_cb;
1472     cur_test_run.eval_cb = no_eval;
1473     cur_test_run.have_churn = GNUNET_NO;
1474     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1475   }
1476
1477   else if (strstr (argv[0], "_single_peer_seed") != NULL)
1478   {
1479     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
1480     cur_test_run.name = "test-rps-single-peer-seed";
1481     cur_test_run.main_test = single_peer_seed_cb;
1482     cur_test_run.have_churn = GNUNET_NO;
1483   }
1484
1485   else if (strstr (argv[0], "_seed_request") != NULL)
1486   {
1487     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
1488     cur_test_run.name = "test-rps-seed-request";
1489     cur_test_run.main_test = seed_req_cb;
1490     cur_test_run.have_churn = GNUNET_NO;
1491   }
1492
1493   else if (strstr (argv[0], "_seed") != NULL)
1494   {
1495     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
1496     cur_test_run.name = "test-rps-seed";
1497     cur_test_run.main_test = seed_cb;
1498     cur_test_run.eval_cb = no_eval;
1499     cur_test_run.have_churn = GNUNET_NO;
1500   }
1501
1502   else if (strstr (argv[0], "_req_cancel") != NULL)
1503   {
1504     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
1505     cur_test_run.name = "test-rps-req-cancel";
1506     num_peers = 1;
1507     cur_test_run.main_test = req_cancel_cb;
1508     cur_test_run.eval_cb = no_eval;
1509     cur_test_run.have_churn = GNUNET_NO;
1510     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1511   }
1512
1513   else if (strstr (argv[0], "_churn") != NULL)
1514   {
1515     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
1516     cur_test_run.name = "test-rps-churn";
1517     num_peers = 5;
1518     cur_test_run.main_test = single_req_cb;
1519     cur_test_run.have_churn = GNUNET_YES;
1520     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1521   }
1522
1523   else if (strstr (argv[0], "profiler") != NULL)
1524   {
1525     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
1526     cur_test_run.name = "test-rps-profiler";
1527     num_peers = 10;
1528     mal_type = 3;
1529     cur_test_run.init_peer = profiler_init_peer;
1530     cur_test_run.pre_test = mal_pre;
1531     cur_test_run.main_test = profiler_cb;
1532     cur_test_run.reply_handle = profiler_reply_handle;
1533     cur_test_run.eval_cb = profiler_eval;
1534     cur_test_run.request_interval = 2;
1535     cur_test_run.num_requests = 5;
1536     cur_test_run.have_churn = GNUNET_YES;
1537     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90);
1538
1539     /* 'Clean' directory */
1540     (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
1541     GNUNET_DISK_directory_create ("/tmp/rps/");
1542   }
1543
1544   rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
1545   peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
1546   rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1547   if ( (2 == mal_type) ||
1548        (3 == mal_type))
1549     target_peer = &rps_peer_ids[num_peers - 2];
1550   if (profiler_eval == cur_test_run.eval_cb)
1551     eval_peer = &rps_peers[num_peers - 1];    /* FIXME: eval_peer could be a
1552                                                  malicious peer if not careful
1553                                                  with the malicious portion */
1554
1555   ok = 1;
1556   (void) GNUNET_TESTBED_test_run (cur_test_run.name,
1557                                   "test_rps.conf",
1558                                   num_peers,
1559                                   0, NULL, NULL,
1560                                   &run, NULL);
1561
1562   ret_value = cur_test_run.eval_cb();
1563   GNUNET_free (rps_peers );
1564   GNUNET_free (rps_peer_ids);
1565   GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1566   return ret_value;
1567 }
1568
1569 /* end of test_rps.c */