-test cases renaming
[oweals/gnunet.git] / src / stream / test_stream_2peers.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file stream/test_stream_2peers.c
23  * @brief Stream API testing between 2 peers using testing API
24  * @author Sree Harsha Totakura
25  */
26
27 #include <string.h>
28
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_mesh_service.h"
32 #include "gnunet_stream_lib.h"
33 #include "gnunet_testing_lib.h"
34
35 #define VERBOSE 1
36
37 /**
38  * Number of peers
39  */
40 #define NUM_PEERS 2
41
42 /**
43  * Structure for holding peer's sockets and IO Handles
44  */
45 struct PeerData
46 {
47   /**
48    * Peer's stream socket
49    */
50   struct GNUNET_STREAM_Socket *socket;
51
52   /**
53    * Peer's io write handle
54    */
55   struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
56
57   /**
58    * Peer's io read handle
59    */
60   struct GNUNET_STREAM_IOReadHandle *io_read_handle;
61
62   /**
63    * Peer's shutdown handle
64    */
65   struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
66
67   /**
68    * Our Peer id
69    */
70   struct GNUNET_PeerIdentity our_id;
71
72   /**
73    * Bytes the peer has written
74    */
75   unsigned int bytes_wrote;
76
77   /**
78    * Byte the peer has read
79    */
80   unsigned int bytes_read;
81 };
82
83 /**
84  * The current peer group
85  */
86 static struct GNUNET_TESTING_PeerGroup *pg;
87
88 /**
89  * Peer 1 daemon
90  */
91 static struct GNUNET_TESTING_Daemon *d1;
92
93 /**
94  * Peer 2 daemon
95  */
96 static struct GNUNET_TESTING_Daemon *d2;
97
98 static struct PeerData peer1;
99 static struct PeerData peer2;
100 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
101 static struct GNUNET_CONFIGURATION_Handle *config;
102
103 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
104 static GNUNET_SCHEDULER_TaskIdentifier read_task;
105
106 static char *data = "ABCD";
107 static int result;
108
109 static int writing_success;
110 static int reading_success;
111
112
113 /**
114  * Check whether peers successfully shut down.
115  */
116 static void
117 peergroup_shutdown_callback (void *cls, const char *emsg)
118 {
119   if (emsg != NULL)
120   {
121     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
122                 "Shutdown of peers failed!\n");
123   }
124   else
125   {
126     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
127                 "All peers successfully shut down!\n");
128   }
129   GNUNET_CONFIGURATION_destroy (config);
130 }
131
132
133 /**
134  * Close sockets and stop testing deamons nicely
135  */
136 static void
137 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
138 {
139   if (NULL != peer1.socket)
140     GNUNET_STREAM_close (peer1.socket);
141   if (NULL != peer2.socket)
142     GNUNET_STREAM_close (peer2.socket);
143   if (NULL != peer2_listen_socket)
144     GNUNET_STREAM_listen_close (peer2_listen_socket);
145
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
147   if (0 != abort_task)
148   {
149     GNUNET_SCHEDULER_cancel (abort_task);
150   }
151
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
153
154   GNUNET_TESTING_daemons_stop (pg,
155                                GNUNET_TIME_relative_multiply
156                                (GNUNET_TIME_UNIT_SECONDS, 5),
157                                &peergroup_shutdown_callback,
158                                NULL);
159 }
160
161
162 /**
163  * Completion callback for shutdown
164  *
165  * @param cls the closure from GNUNET_STREAM_shutdown call
166  * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
167  *          SHUT_RDWR) 
168  */
169 static void 
170 shutdown_completion (void *cls,
171                      int operation)
172 {
173   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174               "STREAM shutdown successful\n");
175   GNUNET_SCHEDULER_add_now (&do_close,
176                             cls);
177 }
178
179
180
181 /**
182  * Shutdown sockets gracefully
183  */
184 static void
185 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
186 {
187   peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket, 
188                                                   SHUT_RDWR,
189                                                   &shutdown_completion,
190                                                   cls);
191 }
192
193
194 /**
195  * Something went wrong and timed out. Kill everything and set error flag
196  */
197 static void
198 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
199 {
200   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
201   if (0 != read_task)
202     {
203       GNUNET_SCHEDULER_cancel (read_task);
204     }
205   result = GNUNET_SYSERR;
206   abort_task = 0;
207   do_close (cls, tc);  
208 }
209
210 /**
211  * Signature for input processor 
212  *
213  * @param cls the closure from GNUNET_STREAM_write/read
214  * @param status the status of the stream at the time this function is called
215  * @param data traffic from the other side
216  * @param size the number of bytes available in data read 
217  * @return number of bytes of processed from 'data' (any data remaining should be
218  *         given to the next time the read processor is called).
219  */
220 static size_t
221 input_processor (void *cls,
222                  enum GNUNET_STREAM_Status status,
223                  const void *input_data,
224                  size_t size);
225
226
227 /**
228  * The write completion function; called upon writing some data to stream or
229  * upon error
230  *
231  * @param cls the closure from GNUNET_STREAM_write/read
232  * @param status the status of the stream at the time this function is called
233  * @param size the number of bytes read or written
234  */
235 static void 
236 write_completion (void *cls,
237                   enum GNUNET_STREAM_Status status,
238                   size_t size)
239 {
240   struct PeerData *peer = cls;
241
242   GNUNET_assert (GNUNET_STREAM_OK == status);
243   GNUNET_assert (size <= strlen (data));
244   peer->bytes_wrote += size;
245
246   if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
247     {
248       peer->io_write_handle =
249         GNUNET_STREAM_write (peer->socket,
250                              (void *) data,
251                              strlen(data) - peer->bytes_wrote,
252                              GNUNET_TIME_relative_multiply
253                              (GNUNET_TIME_UNIT_SECONDS, 5),
254                              &write_completion,
255                              cls);
256       GNUNET_assert (NULL != peer->io_write_handle);
257     }
258   else
259     {
260       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261                   "Writing completed\n");
262
263       if (&peer1 == peer)   /* Peer1 has finished writing; should read now */
264         {
265           peer->io_read_handle =
266             GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
267                                 peer->socket,
268                                 GNUNET_TIME_relative_multiply
269                                 (GNUNET_TIME_UNIT_SECONDS, 5),
270                                 &input_processor,
271                                 cls);
272           GNUNET_assert (NULL!=peer->io_read_handle);
273         }
274       else
275         {
276           writing_success = GNUNET_YES;
277           if (GNUNET_YES == reading_success)
278             GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
279         }
280     }
281 }
282
283
284 /**
285  * Function executed after stream has been established
286  *
287  * @param cls the closure from GNUNET_STREAM_open
288  * @param socket socket to use to communicate with the other side (read/write)
289  */
290 static void 
291 stream_open_cb (void *cls,
292                 struct GNUNET_STREAM_Socket *socket)
293 {
294   struct PeerData *peer;
295
296   GNUNET_assert (socket == peer1.socket);
297   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298               "%s: Stream established from peer1\n",
299               GNUNET_i2s (&peer1.our_id));
300   peer = (struct PeerData *) cls;
301   peer->bytes_wrote = 0;
302   GNUNET_assert (socket == peer1.socket);
303   GNUNET_assert (socket == peer->socket);
304   peer->io_write_handle = GNUNET_STREAM_write (peer->socket, /* socket */
305                                                (void *) data, /* data */
306                                                strlen(data),
307                                                GNUNET_TIME_relative_multiply
308                                                (GNUNET_TIME_UNIT_SECONDS, 5),
309                                                &write_completion,
310                                          cls);
311   GNUNET_assert (NULL != peer->io_write_handle);
312 }
313
314
315 /**
316  * Input processor
317  *
318  * @param cls the closure from GNUNET_STREAM_write/read
319  * @param status the status of the stream at the time this function is called
320  * @param data traffic from the other side
321  * @param size the number of bytes available in data read 
322  * @return number of bytes of processed from 'data' (any data remaining should be
323  *         given to the next time the read processor is called).
324  */
325 static size_t
326 input_processor (void *cls,
327                  enum GNUNET_STREAM_Status status,
328                  const void *input_data,
329                  size_t size)
330 {
331   struct PeerData *peer;
332
333   peer = (struct PeerData *) cls;
334
335   if (GNUNET_STREAM_TIMEOUT == status)
336     {
337       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338                   "Read operation timedout - reading again!\n");
339       GNUNET_assert (0 == size);
340       peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
341                                                  peer->socket,
342                                                  GNUNET_TIME_relative_multiply
343                                                  (GNUNET_TIME_UNIT_SECONDS, 5),
344                                                  &input_processor,
345                                                  cls);
346       GNUNET_assert (NULL != peer->io_read_handle);
347       return 0;
348     }
349
350   GNUNET_assert (GNUNET_STREAM_OK == status);
351   GNUNET_assert (size <= strlen (data));
352   GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read, 
353                                (const char *) input_data,
354                                size));
355   peer->bytes_read += size;
356   
357   if (peer->bytes_read < strlen (data))
358     {
359       peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
360                                                  peer->socket,
361                                                  GNUNET_TIME_relative_multiply
362                                                  (GNUNET_TIME_UNIT_SECONDS, 5),
363                                                  &input_processor,
364                                                  cls);
365       GNUNET_assert (NULL != peer->io_read_handle);
366     }
367   else 
368     {
369       if (&peer2 == peer)    /* Peer2 has completed reading; should write */
370         {
371           peer->bytes_wrote = 0;
372           peer->io_write_handle = 
373             GNUNET_STREAM_write (peer->socket,
374                                  data,
375                                  strlen(data),
376                                  GNUNET_TIME_relative_multiply
377                                  (GNUNET_TIME_UNIT_SECONDS, 5),
378                                  &write_completion,
379                                  cls);
380         }
381       else                      /* Peer1 has completed reading. End of tests */
382         {
383           reading_success = GNUNET_YES;
384           if (GNUNET_YES == writing_success)
385             GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
386         }
387     }
388   return size;
389 }
390
391   
392 /**
393  * Scheduler call back; to be executed when a new stream is connected
394  * Called from listen connect for peer2
395  */
396 static void
397 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
398 {
399   read_task = GNUNET_SCHEDULER_NO_TASK;
400   GNUNET_assert (NULL != cls);
401   peer2.bytes_read = 0;
402   peer2.io_read_handle =
403     GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls,
404                         GNUNET_TIME_relative_multiply
405                         (GNUNET_TIME_UNIT_SECONDS, 5),
406                         &input_processor,
407                         (void *) &peer2);
408   GNUNET_assert (NULL != peer2.io_read_handle);
409 }
410
411
412 /**
413  * Functions of this type are called upon new stream connection from other peers
414  *
415  * @param cls the closure from GNUNET_STREAM_listen
416  * @param socket the socket representing the stream
417  * @param initiator the identity of the peer who wants to establish a stream
418  *            with us
419  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
420  *             stream (the socket will be invalid after the call)
421  */
422 static int
423 stream_listen_cb (void *cls,
424                   struct GNUNET_STREAM_Socket *socket,
425                   const struct GNUNET_PeerIdentity *initiator)
426 {
427   GNUNET_assert (NULL != socket);
428   GNUNET_assert (NULL != initiator);
429   GNUNET_assert (socket != peer1.socket);
430
431   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432               "%s: Peer connected: %s\n",
433               GNUNET_i2s (&peer2.our_id),
434               GNUNET_i2s(initiator));
435
436   peer2.socket = socket;
437   /* FIXME: reading should be done right now instead of a scheduled call */
438   read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
439   return GNUNET_OK;
440 }
441
442
443 /**
444  * Callback to be called when testing peer group is ready
445  *
446  * @param cls NULL
447  * @param emsg NULL on success
448  */
449 void
450 peergroup_ready (void *cls, const char *emsg)
451 {
452   if (NULL != emsg)
453     {
454       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
455                   "Starting peer group failed: %s\n", emsg);
456       return;
457     }
458   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459               "Peer group is now ready\n");
460   
461   GNUNET_assert (2 == GNUNET_TESTING_daemons_running (pg));
462   
463   d1 = GNUNET_TESTING_daemon_get (pg, 0);
464   GNUNET_assert (NULL != d1);
465   
466   d2 = GNUNET_TESTING_daemon_get (pg, 1);
467   GNUNET_assert (NULL != d2);
468
469   GNUNET_TESTING_get_peer_identity (d1->cfg,
470                                     &peer1.our_id);
471   GNUNET_TESTING_get_peer_identity (d2->cfg,
472                                     &peer2.our_id);
473   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474               "%s : %s\n",
475               GNUNET_i2s (&peer1.our_id),
476               GNUNET_i2s (&d1->id));
477   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478               "%s : %s\n",
479               GNUNET_i2s (&peer2.our_id),
480               GNUNET_i2s (&d2->id));
481
482   peer2_listen_socket = GNUNET_STREAM_listen (d2->cfg,
483                                               10, /* App port */
484                                               &stream_listen_cb,
485                                               NULL);
486   GNUNET_assert (NULL != peer2_listen_socket);
487
488   /* Connect to stream library */
489   peer1.socket = GNUNET_STREAM_open (d1->cfg,
490                                      &d2->id,         /* Null for local peer? */
491                                      10,           /* App port */
492                                      &stream_open_cb,
493                                      &peer1);
494   GNUNET_assert (NULL != peer1.socket);
495 }
496
497
498 /**
499  * Initialize framework and start test
500  */
501 static void
502 run (void *cls, char *const *args, const char *cfgfile,
503      const struct GNUNET_CONFIGURATION_Handle *cfg)
504 {
505   struct GNUNET_TESTING_Host *hosts; /* FIXME: free hosts (DLL) */
506
507   /* GNUNET_log_setup ("test_stream_local", */
508   /*                   "DEBUG", */
509   /*                   NULL); */
510
511   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
512               "Starting test\n");
513   /* Duplicate the configuration */
514   config = GNUNET_CONFIGURATION_dup (cfg);
515
516   hosts = GNUNET_TESTING_hosts_load (config);
517   
518   pg = GNUNET_TESTING_peergroup_start (config,
519                                        2,
520                                        GNUNET_TIME_relative_multiply
521                                        (GNUNET_TIME_UNIT_SECONDS, 3),
522                                        NULL,
523                                        &peergroup_ready,
524                                        NULL,
525                                        hosts);
526   GNUNET_assert (NULL != pg);
527                                        
528   abort_task =
529     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
530                                   (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
531                                   NULL);
532 }
533
534 /**
535  * Main function
536  */
537 int main (int argc, char **argv)
538 {
539   int ret;
540
541   char *argv2[] = { "test-stream-local",
542                     "-L", "DEBUG",
543                     "-c", "test_stream_local.conf",
544                     NULL};
545   
546   struct GNUNET_GETOPT_CommandLineOption options[] = {
547     GNUNET_GETOPT_OPTION_END
548   };
549
550   ret =
551       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
552                           "test-stream-local", "nohelp", options, &run, NULL);
553
554   if (GNUNET_OK != ret)
555   {
556     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
557                 ret);
558     return 1;
559   }
560   if (GNUNET_SYSERR == result)
561   {
562     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
563     return 1;
564   }
565   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test ok\n");
566   return 0;
567 }