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_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_IOWriteHandle *io_write_handle;
67 * Peer's io read handle
69 struct GNUNET_STREAM_IOReadHandle *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
109 * Get the identity of peer 1
114 * Get the identity of peer 2
119 * Connect to stream service of peer 1
121 PEER1_STREAM_CONNECT,
124 * Connect to stream service of peer 2
131 * Various states during test setup
133 static enum SetupState setup_state;
136 * Data context for peer 1
138 static struct PeerData peer1;
141 * Data context for peer 2
143 static struct PeerData peer2;
146 * Testbed operation handle
148 static struct GNUNET_TESTBED_Operation *op;
150 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
152 static char *data = "ABCD";
155 static int writing_success;
156 static int reading_success;
162 * @param cls the closure from GNUNET_STREAM_write/read
163 * @param status the status of the stream at the time this function is called
164 * @param data traffic from the other side
165 * @param size the number of bytes available in data read
166 * @return number of bytes of processed from 'data' (any data remaining should be
167 * given to the next time the read processor is called).
170 input_processor (void *cls,
171 enum GNUNET_STREAM_Status status,
172 const void *input_data,
176 * Task for calling STREAM_read
178 * @param cls the peer data entity
179 * @param tc the task context
182 stream_read_task (void *cls,
183 const struct GNUNET_SCHEDULER_TaskContext *tc)
185 struct PeerData *peer = cls;
187 peer->io_read_handle = GNUNET_STREAM_read (peer->socket,
188 GNUNET_TIME_relative_multiply
189 (GNUNET_TIME_UNIT_SECONDS, 5),
192 GNUNET_assert (NULL != peer->io_read_handle);
196 * The write completion function; called upon writing some data to stream or
199 * @param cls the closure from GNUNET_STREAM_write/read
200 * @param status the status of the stream at the time this function is called
201 * @param size the number of bytes read or written
204 write_completion (void *cls,
205 enum GNUNET_STREAM_Status status,
210 * Task for calling STREAM_write
212 * @param cls the peer data entity
213 * @param tc the task context
216 stream_write_task (void *cls,
217 const struct GNUNET_SCHEDULER_TaskContext *tc)
219 struct PeerData *peer = cls;
221 peer->io_write_handle =
222 GNUNET_STREAM_write (peer->socket,
224 strlen(data) - peer->bytes_wrote,
225 GNUNET_TIME_relative_multiply
226 (GNUNET_TIME_UNIT_SECONDS, 5),
230 GNUNET_assert (NULL != peer->io_write_handle);
235 * Close sockets and stop testing deamons nicely
238 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
240 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
241 GNUNET_SCHEDULER_cancel (abort_task);
242 if (NULL != peer1.socket)
243 GNUNET_STREAM_close (peer1.socket);
244 if (NULL != peer1.op)
245 GNUNET_TESTBED_operation_done (peer1.op);
247 GNUNET_SCHEDULER_shutdown (); /* For shutting down testbed */
252 * Completion callback for shutdown
254 * @param cls the closure from GNUNET_STREAM_shutdown call
255 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
259 shutdown_completion (void *cls,
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STREAM shutdown successful\n");
263 GNUNET_SCHEDULER_add_now (&do_close, cls);
269 * Shutdown sockets gracefully
272 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
275 peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket, SHUT_RDWR,
276 &shutdown_completion, cls);
281 * Something went wrong and timed out. Kill everything and set error flag
284 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
287 result = GNUNET_SYSERR;
294 * The write completion function; called upon writing some data to stream or
297 * @param cls the closure from GNUNET_STREAM_write/read
298 * @param status the status of the stream at the time this function is called
299 * @param size the number of bytes read or written
302 write_completion (void *cls,
303 enum GNUNET_STREAM_Status status,
306 struct PeerData *peer=cls;
308 GNUNET_assert (GNUNET_STREAM_OK == status);
309 GNUNET_assert (size <= strlen (data));
310 peer->bytes_wrote += size;
312 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
314 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "Writing completed\n");
321 if (&peer2 == peer) /* Peer1 has finished writing; should read now */
323 peer->bytes_read = 0;
324 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
328 writing_success = GNUNET_YES;
329 if (GNUNET_YES == reading_success)
330 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
337 * Function executed after stream has been established
339 * @param cls the closure from GNUNET_STREAM_open
340 * @param socket socket to use to communicate with the other side (read/write)
343 stream_open_cb (void *cls,
344 struct GNUNET_STREAM_Socket *socket)
346 struct PeerData *peer=cls;
348 GNUNET_assert (&peer2 == peer);
349 GNUNET_assert (socket == peer2.socket);
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Stream established from peer2\n",
351 GNUNET_i2s (&peer1.our_id));
352 peer->bytes_wrote = 0;
353 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
360 * @param cls the closure from GNUNET_STREAM_write/read
361 * @param status the status of the stream at the time this function is called
362 * @param data traffic from the other side
363 * @param size the number of bytes available in data read
364 * @return number of bytes of processed from 'data' (any data remaining should be
365 * given to the next time the read processor is called).
368 input_processor (void *cls,
369 enum GNUNET_STREAM_Status status,
370 const void *input_data,
373 struct PeerData *peer;
375 peer = (struct PeerData *) cls;
377 if (GNUNET_STREAM_TIMEOUT == status)
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Read operation timedout - reading again!\n");
381 GNUNET_assert (0 == size);
382 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
386 GNUNET_assert (GNUNET_STREAM_OK == status);
387 GNUNET_assert (size <= strlen (data));
388 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
389 (const char *) input_data,
391 peer->bytes_read += size;
393 if (peer->bytes_read < strlen (data))
395 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
399 if (&peer1 == peer) /* Peer2 has completed reading; should write */
401 peer->bytes_wrote = 0;
402 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
404 else /* Peer1 has completed reading. End of tests */
406 reading_success = GNUNET_YES;
407 if (GNUNET_YES == writing_success)
408 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
416 * Functions of this type are called upon new stream connection from other peers
418 * @param cls the closure from GNUNET_STREAM_listen
419 * @param socket the socket representing the stream
420 * @param initiator the identity of the peer who wants to establish a stream
422 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
423 * stream (the socket will be invalid after the call)
426 stream_listen_cb (void *cls,
427 struct GNUNET_STREAM_Socket *socket,
428 const struct GNUNET_PeerIdentity *initiator)
430 if ((NULL == socket) || (NULL == initiator))
432 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Binding error\n");
433 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
434 GNUNET_SCHEDULER_cancel (abort_task);
435 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
438 GNUNET_assert (NULL != initiator);
439 GNUNET_assert (socket != peer2.socket);
440 GNUNET_assert (0 == memcmp (initiator, &peer2.our_id,
441 sizeof (struct GNUNET_PeerIdentity)));
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Peer connected: %s\n",
443 GNUNET_i2s (&peer1.our_id), GNUNET_i2s (initiator));
444 peer1.socket = socket;
445 peer1.bytes_read = 0;
446 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer1);
452 * Listen success callback; connects a peer to stream as client
454 static void stream_connect (void);
458 * Adapter function called to destroy a connection to
462 * @param op_result service handle returned from the connect adapter
465 stream_da (void *cls, void *op_result)
467 struct GNUNET_STREAM_ListenSocket *lsocket;
468 struct GNUNET_STREAM_Socket *socket;
473 GNUNET_STREAM_listen_close (lsocket);
474 if (NULL != peer2.op)
475 GNUNET_TESTBED_operation_done (peer2.op);
477 GNUNET_SCHEDULER_shutdown ();
483 GNUNET_STREAM_close (socket);
484 GNUNET_SCHEDULER_shutdown (); /* Exit point of the test */
492 * Adapter function called to establish a connection to
496 * @param cfg configuration of the peer to connect to; will be available until
497 * GNUNET_TESTBED_operation_done() is called on the operation returned
498 * from GNUNET_TESTBED_service_connect()
499 * @return service handle to return in 'op_result', NULL on error
502 stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
504 struct GNUNET_STREAM_ListenSocket *lsocket;
508 case PEER1_STREAM_CONNECT:
509 lsocket = GNUNET_STREAM_listen (cfg, 10, &stream_listen_cb, NULL,
510 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
511 &stream_connect, GNUNET_STREAM_OPTION_END);
513 case PEER2_STREAM_CONNECT:
514 peer2.socket = GNUNET_STREAM_open (cfg, &peer1.our_id, 10, &stream_open_cb,
515 &peer2, GNUNET_STREAM_OPTION_END);
524 * Listen success callback; connects a peer to stream as client
527 stream_connect (void)
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream listen open successful\n");
530 peer2.op = GNUNET_TESTBED_service_connect (&peer2, peer2.peer, "stream",
532 stream_ca, stream_da, &peer2);
533 setup_state = PEER2_STREAM_CONNECT;
538 * Callback to be called when the requested peer information is available
540 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
541 * @param op the operation this callback corresponds to
542 * @param pinfo the result; will be NULL if the operation has failed
543 * @param emsg error message if the operation has failed; will be NULL if the
544 * operation is successfull
547 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
548 const struct GNUNET_TESTBED_PeerInformation *pinfo,
551 GNUNET_assert (NULL == emsg);
552 GNUNET_assert (op == op_);
555 case PEER1_GET_IDENTITY:
556 memcpy (&peer1.our_id, pinfo->result.id,
557 sizeof (struct GNUNET_PeerIdentity));
558 GNUNET_TESTBED_operation_done (op);
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n", GNUNET_i2s
561 op = GNUNET_TESTBED_peer_get_information (peer2.peer,
562 GNUNET_TESTBED_PIT_IDENTITY,
564 setup_state = PEER2_GET_IDENTITY;
566 case PEER2_GET_IDENTITY:
567 memcpy (&peer2.our_id, pinfo->result.id,
568 sizeof (struct GNUNET_PeerIdentity));
569 GNUNET_TESTBED_operation_done (op);
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n", GNUNET_i2s
572 peer1.op = GNUNET_TESTBED_service_connect (&peer1, peer1.peer, "stream",
573 NULL, NULL, stream_ca,
575 setup_state = PEER1_STREAM_CONNECT;
584 * Controller event callback
587 * @param event the controller event
590 controller_event_cb (void *cls,
591 const struct GNUNET_TESTBED_EventInformation *event)
595 case GNUNET_TESTBED_ET_CONNECT:
596 GNUNET_assert (INIT == setup_state);
597 GNUNET_TESTBED_operation_done (op);
598 /* Get the peer identity and configuration of peers */
599 op = GNUNET_TESTBED_peer_get_information (peer1.peer,
600 GNUNET_TESTBED_PIT_IDENTITY,
602 setup_state = PEER1_GET_IDENTITY;
604 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
607 case PEER1_STREAM_CONNECT:
608 case PEER2_STREAM_CONNECT:
609 GNUNET_assert (NULL == event->details.operation_finished.emsg);
622 * Signature of a main function for a testcase.
625 * @param num_peers number of peers in 'peers'
626 * @param peers handle to peers run in the testbed
629 test_master (void *cls, unsigned int num_peers,
630 struct GNUNET_TESTBED_Peer **peers)
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 op = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL, peer2.peer, peer1.peer);
640 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
641 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
649 int main (int argc, char **argv)
655 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
656 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
657 GNUNET_TESTBED_test_run ("test_stream_2peers", "test_stream_local.conf",
658 NUM_PEERS, event_mask, &controller_event_cb, NULL,
660 if (GNUNET_SYSERR == result)