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