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