-new test case for multiple packet transmission using stream
[oweals/gnunet.git] / src / stream / test_stream_big.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_local.c
23  * @brief Stream API testing between local peers
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_stream_lib.h"
32 #include "gnunet_testing_lib.h"
33
34 #define LOG(kind, ...)                         \
35   GNUNET_log (kind, __VA_ARGS__);
36
37 /**
38  * Structure for holding peer's sockets and IO Handles
39  */
40 struct PeerData
41 {
42   /**
43    * Peer's stream socket
44    */
45   struct GNUNET_STREAM_Socket *socket;
46
47   /**
48    * Peer's io write handle
49    */
50   struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
51
52   /**
53    * Peer's io read handle
54    */
55   struct GNUNET_STREAM_IOReadHandle *io_read_handle;
56
57   /**
58    * Bytes the peer has written
59    */
60   unsigned int bytes_wrote;
61
62   /**
63    * Byte the peer has read
64    */
65   unsigned int bytes_read;
66 };
67
68 static struct GNUNET_OS_Process *arm_pid;
69 static struct PeerData peer1;
70 static struct PeerData peer2;
71 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
72 static struct GNUNET_CONFIGURATION_Handle *config;
73
74 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
75 static GNUNET_SCHEDULER_TaskIdentifier test_task;
76 static GNUNET_SCHEDULER_TaskIdentifier read_task;
77 static GNUNET_SCHEDULER_TaskIdentifier write_task;
78
79 #define DATA_SIZE 65536      /* 64KB */
80 static uint32_t data[DATA_SIZE / 4];     /* 64KB array */
81 static int result;
82
83 /**
84  * Shutdown nicely
85  */
86 static void
87 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89   GNUNET_STREAM_close (peer1.socket);
90   if (NULL != peer2.socket)
91     GNUNET_STREAM_close (peer2.socket);
92   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
93   if (0 != abort_task)
94   {
95     GNUNET_SCHEDULER_cancel (abort_task);
96   }
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
98   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
99   {
100     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
101   }
102   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
103   /* Free the duplicated configuration */
104   GNUNET_CONFIGURATION_destroy (config);
105   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
106   GNUNET_OS_process_destroy (arm_pid);
107 }
108
109
110 /**
111  * Something went wrong and timed out. Kill everything and set error flag
112  */
113 static void
114 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 {
116   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
117   if (0 != test_task)
118   {
119     GNUNET_SCHEDULER_cancel (test_task);
120   }
121   if (0 != read_task)
122     {
123       GNUNET_SCHEDULER_cancel (read_task);
124     }
125   result = GNUNET_SYSERR;
126   abort_task = 0;
127   do_shutdown (cls, tc);
128 }
129
130
131 /**
132  * The write completion function; called upon writing some data to stream or
133  * upon error
134  *
135  * @param cls the closure from GNUNET_STREAM_write/read
136  * @param status the status of the stream at the time this function is called
137  * @param size the number of bytes read or written
138  */
139 static void 
140 write_completion (void *cls,
141                   enum GNUNET_STREAM_Status status,
142                   size_t size)
143 {
144   struct PeerData *peer;
145
146   peer = (struct PeerData *) cls;
147   GNUNET_assert (GNUNET_STREAM_OK == status);
148   GNUNET_assert (size < DATA_SIZE);
149   peer->bytes_wrote += size;
150
151   if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
152     {
153       peer->io_write_handle =
154         GNUNET_STREAM_write (peer->socket,
155                              (void *) data,
156                              DATA_SIZE - peer->bytes_wrote,
157                              GNUNET_TIME_relative_multiply
158                              (GNUNET_TIME_UNIT_SECONDS, 5),
159                              &write_completion,
160                              cls);
161       GNUNET_assert (NULL != peer->io_write_handle);
162     }
163   else
164     {
165       LOG (GNUNET_ERROR_TYPE_DEBUG,
166            "Finished writing the data; waiting for the other peer to finish"
167            "reading");
168     }
169 }
170
171
172 /**
173  * Task for calling STREAM_write with a chunk of random data
174  *
175  * @param cls the peer data entity
176  * @param tc the task context
177  */
178 static void
179 stream_write_task (void *cls,
180                    const struct GNUNET_SCHEDULER_TaskContext *tc)
181 {
182   struct PeerData *peer=cls;
183   unsigned int count;
184
185   write_task = GNUNET_SCHEDULER_NO_TASK;
186   for (count=0; count < DATA_SIZE / 4; count++)
187     {
188       data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
189                                             UINT32_MAX);
190     }
191   LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
192   peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
193                                                data,
194                                                DATA_SIZE,
195                                                GNUNET_TIME_relative_multiply
196                                                (GNUNET_TIME_UNIT_SECONDS, 10),
197                                                &write_completion,
198                                                peer);
199   GNUNET_assert (NULL != peer->io_write_handle);
200 }
201
202
203 /**
204  * Function executed after stream has been established
205  *
206  * @param cls the closure from GNUNET_STREAM_open
207  * @param socket socket to use to communicate with the other side (read/write)
208  */
209 static void 
210 stream_open_cb (void *cls,
211                 struct GNUNET_STREAM_Socket *socket)
212 {
213   struct PeerData *peer;
214
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
216   peer = (struct PeerData *) cls;
217   peer->bytes_wrote = 0;
218   GNUNET_assert (socket == peer1.socket);
219   GNUNET_assert (socket == peer->socket);
220   write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
221 }
222
223
224 /**
225  * Input processor
226  *
227  * @param cls the closure from GNUNET_STREAM_write/read
228  * @param status the status of the stream at the time this function is called
229  * @param data traffic from the other side
230  * @param size the number of bytes available in data read 
231  * @return number of bytes of processed from 'data' (any data remaining should be
232  *         given to the next time the read processor is called).
233  */
234 static size_t
235 input_processor (void *cls,
236                  enum GNUNET_STREAM_Status status,
237                  const void *input_data,
238                  size_t size)
239 {
240   struct PeerData *peer = cls;
241
242   GNUNET_assert (GNUNET_STREAM_OK == status);
243   GNUNET_assert (size < DATA_SIZE);
244   GNUNET_assert (memcmp (data + peer->bytes_read, 
245                          input_data,
246                          size));
247   peer->bytes_read += size;
248   
249   if (peer->bytes_read < DATA_SIZE)
250   {
251     peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
252                                                peer->socket,
253                                                GNUNET_TIME_relative_multiply
254                                                (GNUNET_TIME_UNIT_SECONDS, 5),
255                                                &input_processor,
256                                                cls);
257     GNUNET_assert (NULL != peer->io_read_handle);
258   }
259   else 
260   {
261     /* Peer2 has completed reading*/
262     result = GNUNET_OK;
263     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
264   } 
265   return size;
266 }
267
268   
269 /**
270  * Scheduler call back; to be executed when a new stream is connected
271  * Called from listen connect for peer2
272  */
273 static void
274 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
275 {
276   struct GNUNET_STREAM_Socket *socket = cls;
277
278   read_task = GNUNET_SCHEDULER_NO_TASK;
279   GNUNET_assert (socket == peer2.socket);
280   peer2.bytes_read = 0;
281   GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
282   peer2.io_read_handle =
283     GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls,
284                         GNUNET_TIME_relative_multiply
285                         (GNUNET_TIME_UNIT_SECONDS, 10),
286                         &input_processor,
287                         cls);
288   GNUNET_assert (NULL != peer2.io_read_handle);
289 }
290
291
292 /**
293  * Functions of this type are called upon new stream connection from other peers
294  *
295  * @param cls the closure from GNUNET_STREAM_listen
296  * @param socket the socket representing the stream
297  * @param initiator the identity of the peer who wants to establish a stream
298  *            with us
299  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
300  *             stream (the socket will be invalid after the call)
301  */
302 static int
303 stream_listen_cb (void *cls,
304            struct GNUNET_STREAM_Socket *socket,
305            const struct GNUNET_PeerIdentity *initiator)
306 {
307   GNUNET_assert (NULL != socket);
308   GNUNET_assert (socket != peer1.socket);
309
310   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311               "Peer connected: %s\n", GNUNET_i2s(initiator));
312
313   peer2.socket = socket;
314   read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, socket);
315   return GNUNET_OK;
316 }
317
318
319 /**
320  * Testing function
321  *
322  * @param cls NULL
323  * @param tc the task context
324  */
325 static void
326 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
327 {
328   struct GNUNET_PeerIdentity self;
329
330   test_task = GNUNET_SCHEDULER_NO_TASK;
331   /* Get our identity */
332   GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
333                                                                 &self));
334
335   peer2_listen_socket = GNUNET_STREAM_listen (config,
336                                               10, /* App port */
337                                               &stream_listen_cb,
338                                               NULL);
339   GNUNET_assert (NULL != peer2_listen_socket);
340
341   /* Connect to stream library */
342   peer1.socket = GNUNET_STREAM_open (config,
343                                      &self,         /* Null for local peer? */
344                                      10,           /* App port */
345                                      &stream_open_cb,
346                                      (void *) &peer1);
347   GNUNET_assert (NULL != peer1.socket);                  
348 }
349
350 /**
351  * Initialize framework and start test
352  */
353 static void
354 run (void *cls, char *const *args, const char *cfgfile,
355      const struct GNUNET_CONFIGURATION_Handle *cfg)
356 {
357   /* Duplicate the configuration */
358   config = GNUNET_CONFIGURATION_dup (cfg);
359   arm_pid =
360     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
361                              "gnunet-service-arm",
362 #if VERBOSE_ARM
363                              "-L", "DEBUG",
364 #endif
365                              "-c", "test_stream_local.conf", NULL);
366   
367   abort_task =
368     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
369                                   (GNUNET_TIME_UNIT_SECONDS, 30), &do_abort,
370                                   NULL);
371   
372   test_task = GNUNET_SCHEDULER_add_now (&test, NULL);  
373 }
374
375 /**
376  * Main function
377  */
378 int main (int argc, char **argv)
379 {
380   int ret;
381   
382   char *const argv2[] = { "test-stream-big",
383                           "-c", "test_stream_local.conf",
384                           "-L", "DEBUG",
385                           NULL
386   };
387   
388   struct GNUNET_GETOPT_CommandLineOption options[] = {
389     GNUNET_GETOPT_OPTION_END
390   };
391   
392   ret =
393     GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
394                         "test-stream-big", "nohelp", options, &run, NULL);
395   
396   if (GNUNET_OK != ret)
397   {
398     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
399                 ret);
400     return 1;
401   }
402   if (GNUNET_SYSERR == result)
403   {
404     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
405     return 1;
406   }
407   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
408   return 0;
409 }