-fix
[oweals/gnunet.git] / src / stream / test_stream_local_halfclose.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_halfclose.c
23  * @brief Stream API testing between local peers with half closed connections
24  * @author Sree Harsha Totakura
25  */
26
27 #include <string.h>
28 #include <sys/socket.h>         /* For SHUT_RD, SHUT_WR */
29
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_mesh_service.h"
33 #include "gnunet_stream_lib.h"
34
35 #define VERBOSE 1
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 handle
49    */
50   struct GNUNET_STREAM_IOHandle *io_handle;
51
52   /**
53    * Bytes the peer has written
54    */
55   unsigned int bytes_wrote;
56
57   /**
58    * Byte the peer has read
59    */
60   unsigned int bytes_read;
61 };
62
63 static struct GNUNET_OS_Process *arm_pid;
64 static struct PeerData peer1;
65 static struct PeerData peer2;
66 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
67
68 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
69 static GNUNET_SCHEDULER_TaskIdentifier test_task;
70 static GNUNET_SCHEDULER_TaskIdentifier read_task;
71
72 static char *data = "ABCD";
73 static int result;
74
75 /**
76  * Shutdown nicely
77  */
78 static void
79 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   GNUNET_STREAM_close (peer1.socket);
82   GNUNET_STREAM_close (peer2.socket);
83   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
84   if (0 != abort_task)
85   {
86     GNUNET_SCHEDULER_cancel (abort_task);
87   }
88   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
89   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
90   {
91     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
92   }
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
94   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
95   GNUNET_OS_process_close (arm_pid);
96 }
97
98
99 /**
100  * Something went wrong and timed out. Kill everything and set error flag
101  */
102 static void
103 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 {
105   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
106   if (0 != test_task)
107   {
108     GNUNET_SCHEDULER_cancel (test_task);
109   }
110   if (0 != read_task)
111     {
112       GNUNET_SCHEDULER_cancel (read_task);
113     }
114   result = GNUNET_SYSERR;
115   abort_task = 0;
116   do_shutdown (cls, tc);
117 }
118
119
120 /**
121  * The write completion function; called upon writing some data to stream or
122  * upon error
123  *
124  * @param cls the closure from GNUNET_STREAM_write/read
125  * @param status the status of the stream at the time this function is called
126  * @param size the number of bytes read or written
127  */
128 static void 
129 write_completion (void *cls,
130                   enum GNUNET_STREAM_Status status,
131                   size_t size)
132 {
133   struct PeerData *peer;
134
135   peer = (struct PeerData *) cls;
136   GNUNET_assert (GNUNET_STREAM_OK == status);
137   GNUNET_assert (size < strlen (data));
138   peer->bytes_wrote += size;
139
140   if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
141     {
142       peer->io_handle = GNUNET_STREAM_write (peer->socket,
143                                              (void *) data,
144                                              strlen(data) - peer->bytes_wrote,
145                                              GNUNET_TIME_relative_multiply
146                                              (GNUNET_TIME_UNIT_SECONDS, 5),
147                                              &write_completion,
148                                              cls);
149       GNUNET_assert (NULL != peer->io_handle);
150     }
151   else
152     {
153       if (&peer1 == peer)   /* Peer1 has finished writing; should read now */
154         {
155           peer->io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
156                                                 peer->socket,
157                                                 GNUNET_TIME_relative_multiply
158                                                 (GNUNET_TIME_UNIT_SECONDS, 5),
159                                                 &input_processor,
160                                                 cls);
161           GNUNET_assert (NULL!=peer->io_handle);
162         }
163     }
164 }
165
166
167 /**
168  * Function executed after stream has been established
169  *
170  * @param cls the closure from GNUNET_STREAM_open
171  * @param socket socket to use to communicate with the other side (read/write)
172  */
173 static void 
174 stream_open_cb (void *cls,
175                 struct GNUNET_STREAM_Socket *socket)
176 {
177   struct PeerData *peer;
178
179   peer = (struct PeerData *) cls;
180   peer->bytes_wrote = 0;
181   GNUNET_assert (socket == peer1.socket);
182   GNUNET_assert (socket == peer->socket);
183   peer->io_handle = GNUNET_STREAM_write (peer->socket, /* socket */
184                                          (void *) data, /* data */
185                                          strlen(data),
186                                          GNUNET_TIME_relative_multiply
187                                          (GNUNET_TIME_UNIT_SECONDS, 5),
188                                          &write_completion,
189                                          cls);
190   GNUNET_assert (NULL != peer->io_handle);
191 }
192
193
194 /**
195  * Input processor
196  *
197  * @param cls the closure from GNUNET_STREAM_write/read
198  * @param status the status of the stream at the time this function is called
199  * @param data traffic from the other side
200  * @param size the number of bytes available in data read 
201  * @return number of bytes of processed from 'data' (any data remaining should be
202  *         given to the next time the read processor is called).
203  */
204 static size_t
205 input_processor (void *cls,
206                  enum GNUNET_STREAM_Status status,
207                  const void *input_data,
208                  size_t size)
209 {
210   struct PeerData *peer;
211
212   peer = (struct PeerData *) cls;
213
214   /* Peer1 is expected to read when it first finishes writing */
215   if (peer == &peer1)
216     {
217       /* since p2 closed write */
218       GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
219       /* Test passed; shutdown now */
220       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
221       return 0;
222     }
223
224   GNUNET_assert (GNUNET_STERAM_OK == status);
225   GNUNET_assert (size < strlen (data));
226   GNUNET_assert (strncmp ((const char *) data + peer->bytes_read, 
227                           (const char *) input_data,
228                           size));
229   peer->bytes_read += size;
230   
231   if (peer->bytes_read < strlen (data))
232     {
233       peer->io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
234                                             peer->socket,
235                                             GNUNET_TIME_relative_multiply
236                                             (GNUNET_TIME_UNIT_SECONDS, 5),
237                                             &input_processor,
238                                             cls);
239       GNUNET_assert (NULL != peer->io_handle);
240     }
241  
242   return size;
243 }
244
245   
246 /**
247  * Scheduler call back; to be executed when a new stream is connected
248  * Called from listen connect for peer2
249  */
250 static void
251 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
252 {
253   read_task = GNUNET_SCHEDULER_NO_TASK;
254   GNUNET_assert (NULL != cls);
255   peer2.bytes_read = 0;
256   GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
257   /* Close the stream for writing */
258   GNUNET_STREAM_shutdown ((struct GNUNET_STREAM_Socket *) cls,
259                           SHUT_WR);
260   peer2.io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls,
261                                         GNUNET_TIME_relative_multiply
262                                         (GNUNET_TIME_UNIT_SECONDS, 5),
263                                         &input_processor,
264                                         (void *) &peer2);
265   GNUNET_assert (NULL != peer2.io_handle);
266 }
267
268
269 /**
270  * Functions of this type are called upon new stream connection from other peers
271  *
272  * @param cls the closure from GNUNET_STREAM_listen
273  * @param socket the socket representing the stream
274  * @param initiator the identity of the peer who wants to establish a stream
275  *            with us
276  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
277  *             stream (the socket will be invalid after the call)
278  */
279 static int
280 stream_listen_cb (void *cls,
281            struct GNUNET_STREAM_Socket *socket,
282            const struct GNUNET_PeerIdentity *initiator)
283 {
284   GNUNET_assert (NULL != socket);
285   GNUNET_assert (NULL == initiator);   /* Local peer=NULL? */
286   GNUNET_assert (socket != peer1.socket);
287
288   peer2.socket = socket;
289   read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
290   return GNUNET_OK;
291 }
292
293
294 /**
295  * Testing function
296  */
297 static void
298 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
299 {
300   test_task = GNUNET_SCHEDULER_NO_TASK;
301
302   /* Connect to stream library */
303   peer1.socket = GNUNET_STREAM_open (NULL,         /* Null for local peer? */
304                                      10,           /* App port */
305                                      &stream_open_cb,
306                                      (void *) &peer1);
307   GNUNET_assert (NULL != peer1.socket);
308   peer2_listen_socket = GNUNET_STREAM_listen (10 /* App port */
309                                               &stream_listen_cb,
310                                               NULL);
311   GNUNET_assert (NULL != peer2_listen_socket);
312                   
313 }
314
315 /**
316  * Initialize framework and start test
317  */
318 static void
319 run (void *cls, char *const *args, const char *cfgfile,
320      const struct GNUNET_CONFIGURATION_Handle *cfg)
321 {
322    GNUNET_log_setup ("test_stream_local",
323 #if VERBOSE
324                     "DEBUG",
325 #else
326                     "WARNING",
327 #endif
328                     NULL);
329    arm_pid =
330      GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
331                               "gnunet-service-arm",
332 #if VERBOSE_ARM
333                               "-L", "DEBUG",
334 #endif
335                               "-c", "test_stream_local.conf", NULL);
336
337    abort_task =
338      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
339                                    (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
340                                     NULL);
341
342    test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
343
344 }
345
346 /**
347  * Main function
348  */
349 int main (int argc, char **argv)
350 {
351   int ret;
352
353   char *const argv2[] = { "test-stream-local",
354                           "-c", "test_stream.conf",
355 #if VERBOSE
356                           "-L", "DEBUG",
357 #endif
358                           NULL
359   };
360   
361   struct GNUNET_GETOPT_CommandLineOption options[] = {
362     GNUNET_GETOPT_OPTION_END
363   };
364   
365   ret =
366       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
367                           "test-stream-local", "nohelp", options, &run, NULL);
368
369   if (GNUNET_OK != ret)
370   {
371     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
372                 ret);
373     return 1;
374   }
375   if (GNUNET_SYSERR == result)
376   {
377     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
378     return 1;
379   }
380   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
381   return 0;
382 }