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