2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file stream/test_stream_local.c
23 * @brief Stream API testing between local peers
24 * @author Sree Harsha Totakura
30 #include "gnunet_util_lib.h"
31 #include "gnunet_mesh_service.h"
32 #include "gnunet_stream_lib.h"
36 static struct GNUNET_OS_Process *arm_pid;
37 static struct GNUNET_STREAM_Socket *peer1_socket;
38 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
39 static struct GNUNET_STREAM_Socket *peer2_socket;
41 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
42 static GNUNET_SCHEDULER_TaskIdentifier test_task;
43 static GNUNET_SCHEDULER_TaskIdentifier read_task;
45 static GNUNET_STREAM_IOHandle *peer1_IOHandle;
46 static GNUNET_STREAM_IOHandle *peer2_IOHandle;
48 static char *data = "ABCD";
49 static unsigned int *data_pointer;
50 static unsigned int *read_pointer;
53 static int test1_success_counter;
60 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
62 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
65 GNUNET_SCHEDULER_cancel (abort_task);
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
68 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
70 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
72 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
73 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
74 GNUNET_OS_process_close (arm_pid);
79 * Something went wrong and timed out. Kill everything and set error flag
82 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
87 GNUNET_SCHEDULER_cancel (test_task);
91 GNUNET_SCHEDULER_cancel (read_task);
93 result = GNUNET_SYSERR;
95 do_shutdown (cls, tc);
100 * The write completion function; called upon writing some data to stream or
103 * @param cls the closure from GNUNET_STREAM_write/read
104 * @param status the status of the stream at the time this function is called
105 * @param size the number of bytes read or written
107 void write_completion (void *cls,
108 enum GNUNET_STREAM_Status status,
111 GNUNET_assert (GNUNET_STREAM_OK == status);
112 if (data_pointer + size != strlen(data)) /* Have more data to send */
114 data_pointer += size;
115 peer1_IOHandle = GNUNET_STREAM_write (peer1_socket,
116 (void *) data_pointer,
117 strlen(data) - data_pointer,
118 GNUNET_TIME_relative_multiply
119 (GNUNET_TIME_UNIT_SECONDS, 5),
122 GNUNET_assert (NULL != peer1_IOHandle);
124 else{ /* Close peer1 socket */
125 GNUNET_STREAM_close (peer1_socket);
126 test1_success_counter++;
127 if (2 == test1_success_counter)
129 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
136 * Function executed after stream has been established
138 * @param cls the closure from GNUNET_STREAM_open
139 * @param socket socket to use to communicate with the other side (read/write)
142 stream_open_cb (void *cls,
143 struct GNUNET_STREAM_Socket
147 GNUNET_assert (socket == peer1_socket);
148 peer1_IOHandle = GNUNET_STREAM_write (socket, /* socket */
149 (void *) data, /* data */
151 GNUNET_TIME_relative_multiply
152 (GNUNET_TIME_UNIT_SECONDS, 5),
155 GNUNET_assert (NULL != peer1_IOHandle);
163 * @param cls the closure from GNUNET_STREAM_write/read
164 * @param status the status of the stream at the time this function is called
165 * @param data traffic from the other side
166 * @param size the number of bytes available in data read
167 * @return number of bytes of processed from 'data' (any data remaining should be
168 * given to the next time the read processor is called).
171 input_processor (void *cls,
172 enum GNUNET_STREAM_Status status,
173 const void *input_data,
176 GNUNET_assert (size < strlen (data));
177 GNUNET_assert (strncmp ((const char *) data,
178 (const char *) input_data,
180 read_pointer += size;
182 if (read_pointer < strlen (data))
184 peer2_IOHandle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket) cls,
185 GNUNET_TIME_relative_multiply
186 (GNUNET_TIME_UNIT_SECONDS, 5),
189 GNUNET_assert (NULL != peer2_IOHandle);
191 else { /* Close peer2 socket */
192 GNUNET_STREAM_close (peer2_socket);
193 test1_success_counter ++;
194 if (2 == test1_success_counter)
196 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
205 * Scheduler call back; to be executed when a new stream is connected
208 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
210 read_task = GNUNET_SCHEDULER_NO_TASK;
211 GNUNET_assert (NULL != cls);
213 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
214 peer2_IOHandle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket) cls,
215 GNUNET_TIME_relative_multiply
216 (GNUNET_TIME_UNIT_SECONDS, 5),
219 GNUNET_assert (NULL != peer2_IOHandle);
224 * Functions of this type are called upon new stream connection from other peers
226 * @param cls the closure from GNUNET_STREAM_listen
227 * @param socket the socket representing the stream
228 * @param initiator the identity of the peer who wants to establish a stream
230 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
231 * stream (the socket will be invalid after the call)
234 stream_listen_cb (void *cls,
235 struct GNUNET_STREAM_Socket *socket,
236 const struct GNUNET_PeerIdentity *initiator)
238 GNUNET_assert (NULL != socket);
239 GNUNET_assert (NULL == initiator); /* Local peer=NULL? */
240 GNUNET_assert (socket != peer1_socket);
242 peer2_socket = socket;
243 read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
252 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
254 test_task = GNUNET_SCHEDULER_NO_TASK;
255 test1_success_counter = 0;
256 /* Connect to stream library */
257 peer1_socket = GNUNET_STREAM_open (NULL, /* Null for local peer? */
260 GNUNET_assert (NULL != peer1_socket);
261 peer2_listen_socket = GNUNET_STREAM_listen (10 /* App port */
264 GNUNET_assert (NULL != peer2_listen_socket);
269 * Initialize framework and start test
272 run (void *cls, char *const *args, const char *cfgfile,
273 const struct GNUNET_CONFIGURATION_Handle *cfg)
275 GNUNET_log_setup ("test_stream_local",
283 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
284 "gnunet-service-arm",
288 "-c", "test_stream_local.conf", NULL);
291 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
292 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
295 test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
302 int main (int argc, char **argv)
306 char *const argv2[] = { "test-stream-local",
307 "-c", "test_stream.conf",
314 struct GNUNET_GETOPT_CommandLineOption options[] = {
315 GNUNET_GETOPT_OPTION_END
319 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
320 "test-stream-local", "nohelp", options, &run, NULL);
322 if (GNUNET_OK != ret)
324 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
328 if (GNUNET_SYSERR == result)
330 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");