2 This file is part of GNUnet.
3 (C) 2008--2013 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_2peers.c
23 * @brief Stream API testing between 2 peers using testing API
24 * @author Sree Harsha Totakura
30 #include "gnunet_util_lib.h"
31 #include "gnunet_mesh_service.h"
32 #include "gnunet_stream_lib.h"
33 #include "gnunet_testbed_service.h"
36 * Number of peers; Do NOT change this
41 * Shorthand for Relative time in seconds
43 #define TIME_REL_SECS(sec) \
44 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
47 * Structure for holding peer's sockets and IO Handles
52 * Handle to testbed peer
54 struct GNUNET_TESTBED_Peer *peer;
57 * Peer's stream socket
59 struct GNUNET_STREAM_Socket *socket;
62 * Peer's io write handle
64 struct GNUNET_STREAM_WriteHandle *io_write_handle;
67 * Peer's io read handle
69 struct GNUNET_STREAM_ReadHandle *io_read_handle;
72 * Peer's shutdown handle
74 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
77 * The service connect operation to stream
79 struct GNUNET_TESTBED_Operation *op;
84 struct GNUNET_PeerIdentity our_id;
87 * Bytes the peer has written
89 unsigned int bytes_wrote;
92 * Byte the peer has read
94 unsigned int bytes_read;
99 * Different states in test setup
104 * Get the identity of peer 1
109 * Get the identity of peer 2
114 * Connect to stream service of peer 1
116 PEER1_STREAM_CONNECT,
119 * Connect to stream service of peer 2
126 * Various states during test setup
128 static enum SetupState setup_state;
131 * Data context for peer 1
133 static struct PeerData peer1;
136 * Data context for peer 2
138 static struct PeerData peer2;
141 * Testbed operation handle
143 static struct GNUNET_TESTBED_Operation *op;
145 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
147 static char *data = "ABCD";
150 static int writing_success;
151 static int reading_success;
157 * @param cls the closure from GNUNET_STREAM_write/read
158 * @param status the status of the stream at the time this function is called
159 * @param data traffic from the other side
160 * @param size the number of bytes available in data read
161 * @return number of bytes of processed from 'data' (any data remaining should be
162 * given to the next time the read processor is called).
165 input_processor (void *cls,
166 enum GNUNET_STREAM_Status status,
167 const void *input_data,
171 * Task for calling STREAM_read
173 * @param cls the peer data entity
174 * @param tc the task context
177 stream_read_task (void *cls,
178 const struct GNUNET_SCHEDULER_TaskContext *tc)
180 struct PeerData *peer = cls;
182 peer->io_read_handle = GNUNET_STREAM_read (peer->socket,
183 GNUNET_TIME_relative_multiply
184 (GNUNET_TIME_UNIT_SECONDS, 5),
187 GNUNET_assert (NULL != peer->io_read_handle);
191 * The write completion function; called upon writing some data to stream or
194 * @param cls the closure from GNUNET_STREAM_write/read
195 * @param status the status of the stream at the time this function is called
196 * @param size the number of bytes read or written
199 write_completion (void *cls,
200 enum GNUNET_STREAM_Status status,
205 * Task for calling STREAM_write
207 * @param cls the peer data entity
208 * @param tc the task context
211 stream_write_task (void *cls,
212 const struct GNUNET_SCHEDULER_TaskContext *tc)
214 struct PeerData *peer = cls;
216 peer->io_write_handle =
217 GNUNET_STREAM_write (peer->socket,
219 strlen(data) - peer->bytes_wrote,
220 GNUNET_TIME_relative_multiply
221 (GNUNET_TIME_UNIT_SECONDS, 5),
225 GNUNET_assert (NULL != peer->io_write_handle);
230 * Close sockets and stop testing deamons nicely
233 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
236 GNUNET_SCHEDULER_cancel (abort_task);
237 if (NULL != peer1.socket)
238 GNUNET_STREAM_close (peer1.socket);
239 if (NULL != peer1.op)
240 GNUNET_TESTBED_operation_done (peer1.op);
242 GNUNET_SCHEDULER_shutdown (); /* For shutting down testbed */
247 * Completion callback for shutdown
249 * @param cls the closure from GNUNET_STREAM_shutdown call
250 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
254 shutdown_completion (void *cls,
257 static int shutdowns;
259 if (++shutdowns == 1)
261 peer1.shutdown_handle = NULL;
262 peer2.shutdown_handle = GNUNET_STREAM_shutdown (peer2.socket, SHUT_RDWR,
263 &shutdown_completion, cls);
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STREAM shutdown successful\n");
267 GNUNET_SCHEDULER_add_now (&do_close, cls);
272 * Shutdown sockets gracefully
275 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
278 peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket, SHUT_RDWR,
279 &shutdown_completion, cls);
284 * Something went wrong and timed out. Kill everything and set error flag
287 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
290 result = GNUNET_SYSERR;
297 * The write completion function; called upon writing some data to stream or
300 * @param cls the closure from GNUNET_STREAM_write/read
301 * @param status the status of the stream at the time this function is called
302 * @param size the number of bytes read or written
305 write_completion (void *cls,
306 enum GNUNET_STREAM_Status status,
309 struct PeerData *peer=cls;
311 GNUNET_assert (GNUNET_STREAM_OK == status);
312 GNUNET_assert (size <= strlen (data));
313 peer->bytes_wrote += size;
315 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
317 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Writing completed\n");
324 if (&peer2 == peer) /* Peer1 has finished writing; should read now */
326 peer->bytes_read = 0;
327 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
331 writing_success = GNUNET_YES;
332 if (GNUNET_YES == reading_success)
333 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
340 * Function executed after stream has been established
342 * @param cls the closure from GNUNET_STREAM_open
343 * @param socket socket to use to communicate with the other side (read/write)
346 stream_open_cb (void *cls,
347 struct GNUNET_STREAM_Socket *socket)
349 struct PeerData *peer=cls;
351 GNUNET_assert (&peer2 == peer);
352 GNUNET_assert (socket == peer2.socket);
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Stream established from peer2\n",
354 GNUNET_i2s (&peer1.our_id));
355 peer->bytes_wrote = 0;
356 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
363 * @param cls the closure from GNUNET_STREAM_write/read
364 * @param status the status of the stream at the time this function is called
365 * @param data traffic from the other side
366 * @param size the number of bytes available in data read
367 * @return number of bytes of processed from 'data' (any data remaining should be
368 * given to the next time the read processor is called).
371 input_processor (void *cls,
372 enum GNUNET_STREAM_Status status,
373 const void *input_data,
376 struct PeerData *peer;
378 peer = (struct PeerData *) cls;
380 if (GNUNET_STREAM_TIMEOUT == status)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Read operation timedout - reading again!\n");
384 GNUNET_assert (0 == size);
385 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
389 GNUNET_assert (GNUNET_STREAM_OK == status);
390 GNUNET_assert (size <= strlen (data));
391 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
392 (const char *) input_data,
394 peer->bytes_read += size;
396 if (peer->bytes_read < strlen (data))
398 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
402 if (&peer1 == peer) /* Peer2 has completed reading; should write */
404 peer->bytes_wrote = 0;
405 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
407 else /* Peer1 has completed reading. End of tests */
409 reading_success = GNUNET_YES;
410 if (GNUNET_YES == writing_success)
411 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
419 * Functions of this type are called upon new stream connection from other peers
421 * @param cls the closure from GNUNET_STREAM_listen
422 * @param socket the socket representing the stream
423 * @param initiator the identity of the peer who wants to establish a stream
425 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
426 * stream (the socket will be invalid after the call)
429 stream_listen_cb (void *cls,
430 struct GNUNET_STREAM_Socket *socket,
431 const struct GNUNET_PeerIdentity *initiator)
433 if ((NULL == socket) || (NULL == initiator))
435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Binding error\n");
436 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
437 GNUNET_SCHEDULER_cancel (abort_task);
438 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
441 GNUNET_assert (NULL != initiator);
442 GNUNET_assert (socket != peer2.socket);
443 GNUNET_assert (0 == memcmp (initiator, &peer2.our_id,
444 sizeof (struct GNUNET_PeerIdentity)));
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Peer connected: %s\n",
446 GNUNET_i2s (&peer1.our_id), GNUNET_i2s (initiator));
447 peer1.socket = socket;
448 peer1.bytes_read = 0;
449 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer1);
455 * Listen success callback; connects a peer to stream as client
457 static void stream_connect (void);
461 * Adapter function called to destroy a connection to
465 * @param op_result service handle returned from the connect adapter
468 stream_da (void *cls, void *op_result)
470 struct GNUNET_STREAM_ListenSocket *lsocket;
471 struct GNUNET_STREAM_Socket *socket;
476 GNUNET_STREAM_listen_close (lsocket);
477 if (NULL != peer2.op)
478 GNUNET_TESTBED_operation_done (peer2.op);
480 GNUNET_SCHEDULER_shutdown ();
486 GNUNET_STREAM_close (socket);
487 GNUNET_SCHEDULER_shutdown (); /* Exit point of the test */
495 * Adapter function called to establish a connection to
499 * @param cfg configuration of the peer to connect to; will be available until
500 * GNUNET_TESTBED_operation_done() is called on the operation returned
501 * from GNUNET_TESTBED_service_connect()
502 * @return service handle to return in 'op_result', NULL on error
505 stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
507 struct GNUNET_STREAM_ListenSocket *lsocket;
511 case PEER1_STREAM_CONNECT:
512 lsocket = GNUNET_STREAM_listen (cfg, 10, &stream_listen_cb, NULL,
513 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
514 &stream_connect, GNUNET_STREAM_OPTION_END);
516 case PEER2_STREAM_CONNECT:
517 peer2.socket = GNUNET_STREAM_open (cfg, &peer1.our_id, 10, &stream_open_cb,
518 &peer2, GNUNET_STREAM_OPTION_END);
527 * Listen success callback; connects a peer to stream as client
530 stream_connect (void)
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream listen open successful\n");
533 peer2.op = GNUNET_TESTBED_service_connect (&peer2, peer2.peer, "stream",
535 stream_ca, stream_da, &peer2);
536 setup_state = PEER2_STREAM_CONNECT;
541 * Callback to be called when the requested peer information is available
543 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
544 * @param op the operation this callback corresponds to
545 * @param pinfo the result; will be NULL if the operation has failed
546 * @param emsg error message if the operation has failed; will be NULL if the
547 * operation is successfull
550 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
551 const struct GNUNET_TESTBED_PeerInformation *pinfo,
554 GNUNET_assert (NULL == emsg);
555 GNUNET_assert (op == op_);
558 case PEER1_GET_IDENTITY:
559 memcpy (&peer1.our_id, pinfo->result.id,
560 sizeof (struct GNUNET_PeerIdentity));
561 GNUNET_TESTBED_operation_done (op);
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n", GNUNET_i2s
564 op = GNUNET_TESTBED_peer_get_information (peer2.peer,
565 GNUNET_TESTBED_PIT_IDENTITY,
567 setup_state = PEER2_GET_IDENTITY;
569 case PEER2_GET_IDENTITY:
570 memcpy (&peer2.our_id, pinfo->result.id,
571 sizeof (struct GNUNET_PeerIdentity));
572 GNUNET_TESTBED_operation_done (op);
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n", GNUNET_i2s
575 peer1.op = GNUNET_TESTBED_service_connect (&peer1, peer1.peer, "stream",
576 NULL, NULL, stream_ca,
578 setup_state = PEER1_STREAM_CONNECT;
587 * Controller event callback
590 * @param event the controller event
593 controller_event_cb (void *cls,
594 const struct GNUNET_TESTBED_EventInformation *event)
598 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
601 case PEER1_STREAM_CONNECT:
602 case PEER2_STREAM_CONNECT:
603 GNUNET_assert (NULL == event->details.operation_finished.emsg);
616 * Signature of a main function for a testcase.
619 * @param num_peers number of peers in 'peers'
620 * @param peers handle to peers run in the testbed
621 * @param links_succeeded the number of overlay link connection attempts that
623 * @param links_failed the number of overlay link connection attempts that
627 test_master (void *cls, unsigned int num_peers,
628 struct GNUNET_TESTBED_Peer **peers,
629 unsigned int links_succeeded,
630 unsigned int links_failed)
632 GNUNET_assert (NULL != peers);
633 GNUNET_assert (NULL != peers[0]);
634 GNUNET_assert (NULL != peers[1]);
635 peer1.peer = peers[0];
636 peer2.peer = peers[1];
637 /* Get the peer identity and configuration of peers */
638 op = GNUNET_TESTBED_peer_get_information (peer1.peer,
639 GNUNET_TESTBED_PIT_IDENTITY,
641 setup_state = PEER1_GET_IDENTITY;
643 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
644 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
652 int main (int argc, char **argv)
658 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
659 (void) GNUNET_TESTBED_test_run ("test_stream_2peers",
660 "test_stream_local.conf",
661 NUM_PEERS, event_mask, &controller_event_cb,
664 if (GNUNET_SYSERR == result)