aboutsummaryrefslogtreecommitdiff
path: root/src/stream
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-09-07 16:53:35 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-09-07 16:53:35 +0000
commitc8f5c4b7c29f33186afde05c88a6b2922284a1cb (patch)
tree5981578b6a849ba3074bd75a46b2958690079211 /src/stream
parent88638fd30b3c7898e1209b8128adf42b1e4bd315 (diff)
downloadgnunet-c8f5c4b7c29f33186afde05c88a6b2922284a1cb.tar.gz
gnunet-c8f5c4b7c29f33186afde05c88a6b2922284a1cb.zip
migrate stream 2peers test to testbed
Diffstat (limited to 'src/stream')
-rw-r--r--src/stream/Makefile.am12
-rw-r--r--src/stream/test_stream_2peers.c422
-rw-r--r--src/stream/test_stream_2peers_new.c650
3 files changed, 251 insertions, 833 deletions
diff --git a/src/stream/Makefile.am b/src/stream/Makefile.am
index 8132b0207..472b5949f 100644
--- a/src/stream/Makefile.am
+++ b/src/stream/Makefile.am
@@ -22,7 +22,6 @@ libgnunetstream_la_LDFLAGS = \
22 22
23check_PROGRAMS = \ 23check_PROGRAMS = \
24 test_stream_2peers \ 24 test_stream_2peers \
25 test_stream_2peers_new \
26 test_stream_2peers_halfclose \ 25 test_stream_2peers_halfclose \
27 test_stream_local \ 26 test_stream_local \
28 test_stream_big \ 27 test_stream_big \
@@ -36,8 +35,7 @@ TESTS = \
36 test_stream_2peers_halfclose \ 35 test_stream_2peers_halfclose \
37 test_stream_local \ 36 test_stream_local \
38 test_stream_big \ 37 test_stream_big \
39 test_stream_sequence_wraparound \ 38 test_stream_sequence_wraparound
40 test_stream_2peers_new
41endif 39endif
42 40
43test_stream_2peers_SOURCES = \ 41test_stream_2peers_SOURCES = \
@@ -45,15 +43,9 @@ test_stream_2peers_SOURCES = \
45test_stream_2peers_LDADD = \ 43test_stream_2peers_LDADD = \
46 $(top_builddir)/src/stream/libgnunetstream.la \ 44 $(top_builddir)/src/stream/libgnunetstream.la \
47 $(top_builddir)/src/util/libgnunetutil.la \ 45 $(top_builddir)/src/util/libgnunetutil.la \
48 $(top_builddir)/src/testing_old/libgnunettesting_old.la
49
50test_stream_2peers_new_SOURCES = \
51 test_stream_2peers_new.c
52test_stream_2peers_new_LDADD = \
53 $(top_builddir)/src/stream/libgnunetstream.la \
54 $(top_builddir)/src/util/libgnunetutil.la \
55 $(top_builddir)/src/testbed/libgnunettestbed.la 46 $(top_builddir)/src/testbed/libgnunettestbed.la
56 47
48
57test_stream_2peers_halfclose_SOURCES = \ 49test_stream_2peers_halfclose_SOURCES = \
58 test_stream_2peers_halfclose.c 50 test_stream_2peers_halfclose.c
59test_stream_2peers_halfclose_LDADD = \ 51test_stream_2peers_halfclose_LDADD = \
diff --git a/src/stream/test_stream_2peers.c b/src/stream/test_stream_2peers.c
index 4313f646b..d597ac5c3 100644
--- a/src/stream/test_stream_2peers.c
+++ b/src/stream/test_stream_2peers.c
@@ -30,15 +30,16 @@
30#include "gnunet_util_lib.h" 30#include "gnunet_util_lib.h"
31#include "gnunet_mesh_service.h" 31#include "gnunet_mesh_service.h"
32#include "gnunet_stream_lib.h" 32#include "gnunet_stream_lib.h"
33#include "gnunet_testing_lib.h" 33#include "gnunet_testbed_service.h"
34
35#define VERBOSE 1
36 34
37/** 35/**
38 * Number of peers 36 * Number of peers; Do NOT change this
39 */ 37 */
40#define NUM_PEERS 2 38#define NUM_PEERS 2
41 39
40/**
41 * Shorthand for Relative time in seconds
42 */
42#define TIME_REL_SECS(sec) \ 43#define TIME_REL_SECS(sec) \
43 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec) 44 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
44 45
@@ -48,6 +49,11 @@
48struct PeerData 49struct PeerData
49{ 50{
50 /** 51 /**
52 * Handle to testbed peer
53 */
54 struct GNUNET_TESTBED_Peer *peer;
55
56 /**
51 * Peer's stream socket 57 * Peer's stream socket
52 */ 58 */
53 struct GNUNET_STREAM_Socket *socket; 59 struct GNUNET_STREAM_Socket *socket;
@@ -68,6 +74,11 @@ struct PeerData
68 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle; 74 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
69 75
70 /** 76 /**
77 * The service connect operation to stream
78 */
79 struct GNUNET_TESTBED_Operation *op;
80
81 /**
71 * Our Peer id 82 * Our Peer id
72 */ 83 */
73 struct GNUNET_PeerIdentity our_id; 84 struct GNUNET_PeerIdentity our_id;
@@ -83,25 +94,58 @@ struct PeerData
83 unsigned int bytes_read; 94 unsigned int bytes_read;
84}; 95};
85 96
97
86/** 98/**
87 * The current peer group 99 * Different states in test setup
88 */ 100 */
89static struct GNUNET_TESTING_PeerGroup *pg; 101enum SetupState
102{
103 /**
104 * The initial state
105 */
106 INIT,
107
108 /**
109 * Get the identity of peer 1
110 */
111 PEER1_GET_IDENTITY,
112
113 /**
114 * Get the identity of peer 2
115 */
116 PEER2_GET_IDENTITY,
117
118 /**
119 * Connect to stream service of peer 1
120 */
121 PEER1_STREAM_CONNECT,
122
123 /**
124 * Connect to stream service of peer 2
125 */
126 PEER2_STREAM_CONNECT
127
128};
90 129
91/** 130/**
92 * Peer 1 daemon 131 * Various states during test setup
93 */ 132 */
94static struct GNUNET_TESTING_Daemon *d1; 133static enum SetupState setup_state;
95 134
96/** 135/**
97 * Peer 2 daemon 136 * Data context for peer 1
98 */ 137 */
99static struct GNUNET_TESTING_Daemon *d2;
100
101static struct PeerData peer1; 138static struct PeerData peer1;
139
140/**
141 * Data context for peer 2
142 */
102static struct PeerData peer2; 143static struct PeerData peer2;
103static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket; 144
104static struct GNUNET_CONFIGURATION_Handle *config; 145/**
146 * Testbed operation handle
147 */
148static struct GNUNET_TESTBED_Operation *op;
105 149
106static GNUNET_SCHEDULER_TaskIdentifier abort_task; 150static GNUNET_SCHEDULER_TaskIdentifier abort_task;
107 151
@@ -188,51 +232,16 @@ stream_write_task (void *cls,
188 232
189 233
190/** 234/**
191 * Check whether peers successfully shut down.
192 */
193static void
194peergroup_shutdown_callback (void *cls, const char *emsg)
195{
196 if (emsg != NULL)
197 {
198 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
199 "Shutdown of peers failed!\n");
200 }
201 else
202 {
203 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
204 "All peers successfully shut down!\n");
205 }
206 GNUNET_CONFIGURATION_destroy (config);
207}
208
209
210/**
211 * Close sockets and stop testing deamons nicely 235 * Close sockets and stop testing deamons nicely
212 */ 236 */
213static void 237static void
214do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 238do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
215{ 239{
240 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
241 GNUNET_SCHEDULER_cancel (abort_task);
216 if (NULL != peer1.socket) 242 if (NULL != peer1.socket)
217 GNUNET_STREAM_close (peer1.socket); 243 GNUNET_STREAM_close (peer1.socket);
218 if (NULL != peer2.socket) 244 GNUNET_TESTBED_operation_done (peer1.op);
219 GNUNET_STREAM_close (peer2.socket);
220 if (NULL != peer2_listen_socket)
221 GNUNET_STREAM_listen_close (peer2_listen_socket);
222
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
224 if (0 != abort_task)
225 {
226 GNUNET_SCHEDULER_cancel (abort_task);
227 }
228
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
230
231 GNUNET_TESTING_daemons_stop (pg,
232 GNUNET_TIME_relative_multiply
233 (GNUNET_TIME_UNIT_SECONDS, 5),
234 &peergroup_shutdown_callback,
235 NULL);
236} 245}
237 246
238 247
@@ -247,10 +256,8 @@ static void
247shutdown_completion (void *cls, 256shutdown_completion (void *cls,
248 int operation) 257 int operation)
249{ 258{
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STREAM shutdown successful\n");
251 "STREAM shutdown successful\n"); 260 GNUNET_SCHEDULER_add_now (&do_close, cls);
252 GNUNET_SCHEDULER_add_now (&do_close,
253 cls);
254} 261}
255 262
256 263
@@ -261,10 +268,9 @@ shutdown_completion (void *cls,
261static void 268static void
262do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 269do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
263{ 270{
264 peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket, 271 result = GNUNET_OK;
265 SHUT_RDWR, 272 peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket, SHUT_RDWR,
266 &shutdown_completion, 273 &shutdown_completion, cls);
267 cls);
268} 274}
269 275
270 276
@@ -309,7 +315,7 @@ write_completion (void *cls,
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Writing completed\n"); 316 "Writing completed\n");
311 317
312 if (&peer1 == peer) /* Peer1 has finished writing; should read now */ 318 if (&peer2 == peer) /* Peer1 has finished writing; should read now */
313 { 319 {
314 peer->bytes_read = 0; 320 peer->bytes_read = 0;
315 GNUNET_SCHEDULER_add_now (&stream_read_task, peer); 321 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
@@ -336,10 +342,9 @@ stream_open_cb (void *cls,
336{ 342{
337 struct PeerData *peer=cls; 343 struct PeerData *peer=cls;
338 344
339 GNUNET_assert (&peer1 == peer); 345 GNUNET_assert (&peer2 == peer);
340 GNUNET_assert (socket == peer1.socket); 346 GNUNET_assert (socket == peer2.socket);
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Stream established from peer2\n",
342 "%s: Stream established from peer1\n",
343 GNUNET_i2s (&peer1.our_id)); 348 GNUNET_i2s (&peer1.our_id));
344 peer->bytes_wrote = 0; 349 peer->bytes_wrote = 0;
345 GNUNET_SCHEDULER_add_now (&stream_write_task, peer); 350 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
@@ -388,7 +393,7 @@ input_processor (void *cls,
388 } 393 }
389 else 394 else
390 { 395 {
391 if (&peer2 == peer) /* Peer2 has completed reading; should write */ 396 if (&peer1 == peer) /* Peer2 has completed reading; should write */
392 { 397 {
393 peer->bytes_wrote = 0; 398 peer->bytes_wrote = 0;
394 GNUNET_SCHEDULER_add_now (&stream_write_task, peer); 399 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
@@ -421,17 +426,84 @@ stream_listen_cb (void *cls,
421{ 426{
422 GNUNET_assert (NULL != socket); 427 GNUNET_assert (NULL != socket);
423 GNUNET_assert (NULL != initiator); 428 GNUNET_assert (NULL != initiator);
424 GNUNET_assert (socket != peer1.socket); 429 GNUNET_assert (socket != peer2.socket);
430 GNUNET_assert (0 == memcmp (initiator, &peer2.our_id,
431 sizeof (struct GNUNET_PeerIdentity)));
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Peer connected: %s\n",
433 GNUNET_i2s (&peer1.our_id), GNUNET_i2s (initiator));
434 peer1.socket = socket;
435 peer1.bytes_read = 0;
436 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer1);
437 return GNUNET_OK;
438}
425 439
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "%s: Peer connected: %s\n",
428 GNUNET_i2s (&peer2.our_id),
429 GNUNET_i2s(initiator));
430 440
431 peer2.socket = socket; 441/**
432 peer2.bytes_read = 0; 442 * Listen success callback; connects a peer to stream as client
433 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2); 443 */
434 return GNUNET_OK; 444static void stream_connect (void);
445
446
447/**
448 * Adapter function called to destroy a connection to
449 * a service.
450 *
451 * @param cls closure
452 * @param op_result service handle returned from the connect adapter
453 */
454static void
455stream_da (void *cls, void *op_result)
456{
457 struct GNUNET_STREAM_ListenSocket *lsocket;
458 struct GNUNET_STREAM_Socket *socket;
459
460 if (&peer1 == cls)
461 {
462 lsocket = op_result;
463 GNUNET_STREAM_listen_close (lsocket);
464 GNUNET_TESTBED_operation_done (peer2.op);
465 return;
466 }
467 if (&peer2 == cls)
468 {
469 socket = op_result;
470 GNUNET_STREAM_close (socket);
471 GNUNET_SCHEDULER_shutdown (); /* Exit point of the test */
472 return;
473 }
474 GNUNET_assert (0);
475}
476
477
478/**
479 * Adapter function called to establish a connection to
480 * a service.
481 *
482 * @param cls closure
483 * @param cfg configuration of the peer to connect to; will be available until
484 * GNUNET_TESTBED_operation_done() is called on the operation returned
485 * from GNUNET_TESTBED_service_connect()
486 * @return service handle to return in 'op_result', NULL on error
487 */
488static void *
489stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
490{
491 struct GNUNET_STREAM_ListenSocket *lsocket;
492
493 switch (setup_state)
494 {
495 case PEER1_STREAM_CONNECT:
496 lsocket = GNUNET_STREAM_listen (cfg, 10, &stream_listen_cb, NULL,
497 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
498 &stream_connect, GNUNET_STREAM_OPTION_END);
499 return lsocket;
500 case PEER2_STREAM_CONNECT:
501 peer2.socket = GNUNET_STREAM_open (cfg, &peer1.our_id, 10, &stream_open_cb,
502 &peer2, GNUNET_STREAM_OPTION_END);
503 return peer2.socket;
504 default:
505 GNUNET_assert (0);
506 }
435} 507}
436 508
437 509
@@ -440,135 +512,139 @@ stream_listen_cb (void *cls,
440 */ 512 */
441static void 513static void
442stream_connect (void) 514stream_connect (void)
443{ 515{
444 peer1.socket = GNUNET_STREAM_open (d1->cfg, 516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream listen open successful\n");
445 &d2->id, /* Null for local peer? */ 517 peer2.op = GNUNET_TESTBED_service_connect (&peer2, peer2.peer, "stream",
446 10, /* App port */ 518 NULL, NULL,
447 &stream_open_cb, 519 stream_ca, stream_da, &peer2);
448 &peer1, 520 setup_state = PEER2_STREAM_CONNECT;
449 GNUNET_STREAM_OPTION_END);
450 GNUNET_assert (NULL != peer1.socket);
451} 521}
452 522
453 523
454/** 524/**
455 * Callback to be called when testing peer group is ready 525 * Callback to be called when the requested peer information is available
456 * 526 *
457 * @param cls NULL 527 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
458 * @param emsg NULL on success 528 * @param op the operation this callback corresponds to
529 * @param pinfo the result; will be NULL if the operation has failed
530 * @param emsg error message if the operation has failed; will be NULL if the
531 * operation is successfull
459 */ 532 */
460void 533static void
461peergroup_ready (void *cls, const char *emsg) 534peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
535 const struct GNUNET_TESTBED_PeerInformation *pinfo,
536 const char *emsg)
462{ 537{
463 if (NULL != emsg) 538 GNUNET_assert (NULL == emsg);
539 GNUNET_assert (op == op_);
540 switch (setup_state)
464 { 541 {
465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 542 case PEER1_GET_IDENTITY:
466 "Starting peer group failed: %s\n", emsg); 543 memcpy (&peer1.our_id, pinfo->result.id,
467 return; 544 sizeof (struct GNUNET_PeerIdentity));
545 GNUNET_TESTBED_operation_done (op);
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n", GNUNET_i2s
547 (&peer1.our_id));
548 op = GNUNET_TESTBED_peer_get_information (peer2.peer,
549 GNUNET_TESTBED_PIT_IDENTITY,
550 &peerinfo_cb, NULL);
551 setup_state = PEER2_GET_IDENTITY;
552 break;
553 case PEER2_GET_IDENTITY:
554 memcpy (&peer2.our_id, pinfo->result.id,
555 sizeof (struct GNUNET_PeerIdentity));
556 GNUNET_TESTBED_operation_done (op);
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n", GNUNET_i2s
558 (&peer2.our_id));
559 peer1.op = GNUNET_TESTBED_service_connect (&peer1, peer1.peer, "stream",
560 NULL, NULL, stream_ca,
561 stream_da, &peer1);
562 setup_state = PEER1_STREAM_CONNECT;
563 break;
564 default:
565 GNUNET_assert (0);
468 } 566 }
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "Peer group is now ready\n");
471
472 GNUNET_assert (2 == GNUNET_TESTING_daemons_running (pg));
473
474 d1 = GNUNET_TESTING_daemon_get (pg, 0);
475 GNUNET_assert (NULL != d1);
476
477 d2 = GNUNET_TESTING_daemon_get (pg, 1);
478 GNUNET_assert (NULL != d2);
479
480 GNUNET_TESTING_get_peer_identity (d1->cfg,
481 &peer1.our_id);
482 GNUNET_TESTING_get_peer_identity (d2->cfg,
483 &peer2.our_id);
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "%s : %s\n",
486 GNUNET_i2s (&peer1.our_id),
487 GNUNET_i2s (&d1->id));
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "%s : %s\n",
490 GNUNET_i2s (&peer2.our_id),
491 GNUNET_i2s (&d2->id));
492
493 peer2_listen_socket =
494 GNUNET_STREAM_listen (d2->cfg,
495 10, /* App port */
496 &stream_listen_cb,
497 NULL,
498 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
499 &stream_connect,
500 GNUNET_STREAM_OPTION_END);
501 GNUNET_assert (NULL != peer2_listen_socket);
502} 567}
503 568
504 569
505/** 570/**
506 * Initialize framework and start test 571 * Controller event callback
572 *
573 * @param cls NULL
574 * @param event the controller event
507 */ 575 */
508static void 576static void
509run (void *cls, char *const *args, const char *cfgfile, 577controller_event_cb (void *cls,
510 const struct GNUNET_CONFIGURATION_Handle *cfg) 578 const struct GNUNET_TESTBED_EventInformation *event)
511{ 579{
512 struct GNUNET_TESTING_Host *hosts; /* FIXME: free hosts (DLL) */ 580 switch (event->type)
513 581 {
514 GNUNET_log_setup ("test_stream_2peers", 582 case GNUNET_TESTBED_ET_CONNECT:
515 "DEBUG", 583 GNUNET_assert (INIT == setup_state);
516 NULL); 584 GNUNET_TESTBED_operation_done (op);
585 /* Get the peer identity and configuration of peers */
586 op = GNUNET_TESTBED_peer_get_information (peer1.peer,
587 GNUNET_TESTBED_PIT_IDENTITY,
588 &peerinfo_cb, NULL);
589 setup_state = PEER1_GET_IDENTITY;
590 break;
591 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
592 switch (setup_state)
593 {
594 case PEER1_STREAM_CONNECT:
595 case PEER2_STREAM_CONNECT:
596 GNUNET_assert (NULL == event->details.operation_finished.emsg);
597 break;
598 default:
599 GNUNET_assert (0);
600 }
601 break;
602 default:
603 GNUNET_assert (0);
604 }
605}
517 606
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Starting test\n");
520 /* Duplicate the configuration */
521 config = GNUNET_CONFIGURATION_dup (cfg);
522 607
523 hosts = GNUNET_TESTING_hosts_load (config); 608/**
524 609 * Signature of a main function for a testcase.
525 pg = GNUNET_TESTING_peergroup_start (config, 610 *
526 2, 611 * @param cls closure
527 GNUNET_TIME_relative_multiply 612 * @param num_peers number of peers in 'peers'
528 (GNUNET_TIME_UNIT_SECONDS, 3), 613 * @param peers handle to peers run in the testbed
529 NULL, 614 */
530 &peergroup_ready, 615static void
531 NULL, 616test_master (void *cls, unsigned int num_peers,
532 hosts); 617 struct GNUNET_TESTBED_Peer **peers)
533 GNUNET_assert (NULL != pg); 618{
534 619 GNUNET_assert (NULL != peers);
620 GNUNET_assert (NULL != peers[0]);
621 GNUNET_assert (NULL != peers[1]);
622 peer1.peer = peers[0];
623 peer2.peer = peers[1];
624 op = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL, peer2.peer, peer1.peer);
625 setup_state = INIT;
535 abort_task = 626 abort_task =
536 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 627 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
537 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort, 628 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
538 NULL); 629 NULL);
539} 630}
540 631
632
541/** 633/**
542 * Main function 634 * Main function
543 */ 635 */
544int main (int argc, char **argv) 636int main (int argc, char **argv)
545{ 637{
546 int ret; 638 uint64_t event_mask;
547 639
548 char *argv2[] = { "test-stream-2peers", 640 result = GNUNET_NO;
549 "-L", "DEBUG", 641 event_mask = 0;
550 "-c", "test_stream_local.conf", 642 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
551 NULL}; 643 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
552 644 GNUNET_TESTBED_test_run ("test_stream_2peers", "test_stream_local.conf",
553 struct GNUNET_GETOPT_CommandLineOption options[] = { 645 NUM_PEERS, event_mask, &controller_event_cb, NULL,
554 GNUNET_GETOPT_OPTION_END 646 &test_master, NULL);
555 };
556
557 ret =
558 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
559 "test-stream-2peers", "nohelp", options, &run, NULL);
560
561 if (GNUNET_OK != ret)
562 {
563 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
564 ret);
565 return 1;
566 }
567 if (GNUNET_SYSERR == result) 647 if (GNUNET_SYSERR == result)
568 {
569 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
570 return 1; 648 return 1;
571 }
572 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test ok\n");
573 return 0; 649 return 0;
574} 650}
diff --git a/src/stream/test_stream_2peers_new.c b/src/stream/test_stream_2peers_new.c
deleted file mode 100644
index d597ac5c3..000000000
--- a/src/stream/test_stream_2peers_new.c
+++ /dev/null
@@ -1,650 +0,0 @@
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_2peers.c
23 * @brief Stream API testing between 2 peers using testing API
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_mesh_service.h"
32#include "gnunet_stream_lib.h"
33#include "gnunet_testbed_service.h"
34
35/**
36 * Number of peers; Do NOT change this
37 */
38#define NUM_PEERS 2
39
40/**
41 * Shorthand for Relative time in seconds
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 */
49struct PeerData
50{
51 /**
52 * Handle to testbed peer
53 */
54 struct GNUNET_TESTBED_Peer *peer;
55
56 /**
57 * Peer's stream socket
58 */
59 struct GNUNET_STREAM_Socket *socket;
60
61 /**
62 * Peer's io write handle
63 */
64 struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
65
66 /**
67 * Peer's io read handle
68 */
69 struct GNUNET_STREAM_IOReadHandle *io_read_handle;
70
71 /**
72 * Peer's shutdown handle
73 */
74 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
75
76 /**
77 * The service connect operation to stream
78 */
79 struct GNUNET_TESTBED_Operation *op;
80
81 /**
82 * Our Peer id
83 */
84 struct GNUNET_PeerIdentity our_id;
85
86 /**
87 * Bytes the peer has written
88 */
89 unsigned int bytes_wrote;
90
91 /**
92 * Byte the peer has read
93 */
94 unsigned int bytes_read;
95};
96
97
98/**
99 * Different states in test setup
100 */
101enum SetupState
102{
103 /**
104 * The initial state
105 */
106 INIT,
107
108 /**
109 * Get the identity of peer 1
110 */
111 PEER1_GET_IDENTITY,
112
113 /**
114 * Get the identity of peer 2
115 */
116 PEER2_GET_IDENTITY,
117
118 /**
119 * Connect to stream service of peer 1
120 */
121 PEER1_STREAM_CONNECT,
122
123 /**
124 * Connect to stream service of peer 2
125 */
126 PEER2_STREAM_CONNECT
127
128};
129
130/**
131 * Various states during test setup
132 */
133static enum SetupState setup_state;
134
135/**
136 * Data context for peer 1
137 */
138static struct PeerData peer1;
139
140/**
141 * Data context for peer 2
142 */
143static struct PeerData peer2;
144
145/**
146 * Testbed operation handle
147 */
148static struct GNUNET_TESTBED_Operation *op;
149
150static GNUNET_SCHEDULER_TaskIdentifier abort_task;
151
152static char *data = "ABCD";
153static int result;
154
155static int writing_success;
156static int reading_success;
157
158
159/**
160 * Input processor
161 *
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).
168 */
169static size_t
170input_processor (void *cls,
171 enum GNUNET_STREAM_Status status,
172 const void *input_data,
173 size_t size);
174
175/**
176 * Task for calling STREAM_read
177 *
178 * @param cls the peer data entity
179 * @param tc the task context
180 */
181static void
182stream_read_task (void *cls,
183 const struct GNUNET_SCHEDULER_TaskContext *tc)
184{
185 struct PeerData *peer = cls;
186
187 peer->io_read_handle = GNUNET_STREAM_read (peer->socket,
188 GNUNET_TIME_relative_multiply
189 (GNUNET_TIME_UNIT_SECONDS, 5),
190 &input_processor,
191 peer);
192 GNUNET_assert (NULL != peer->io_read_handle);
193}
194
195/**
196 * The write completion function; called upon writing some data to stream or
197 * upon error
198 *
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
202 */
203static void
204write_completion (void *cls,
205 enum GNUNET_STREAM_Status status,
206 size_t size);
207
208
209/**
210 * Task for calling STREAM_write
211 *
212 * @param cls the peer data entity
213 * @param tc the task context
214 */
215static void
216stream_write_task (void *cls,
217 const struct GNUNET_SCHEDULER_TaskContext *tc)
218{
219 struct PeerData *peer = cls;
220
221 peer->io_write_handle =
222 GNUNET_STREAM_write (peer->socket,
223 (void *) data,
224 strlen(data) - peer->bytes_wrote,
225 GNUNET_TIME_relative_multiply
226 (GNUNET_TIME_UNIT_SECONDS, 5),
227 &write_completion,
228 peer);
229
230 GNUNET_assert (NULL != peer->io_write_handle);
231 }
232
233
234/**
235 * Close sockets and stop testing deamons nicely
236 */
237static void
238do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239{
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 GNUNET_TESTBED_operation_done (peer1.op);
245}
246
247
248/**
249 * Completion callback for shutdown
250 *
251 * @param cls the closure from GNUNET_STREAM_shutdown call
252 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
253 * SHUT_RDWR)
254 */
255static void
256shutdown_completion (void *cls,
257 int operation)
258{
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STREAM shutdown successful\n");
260 GNUNET_SCHEDULER_add_now (&do_close, cls);
261}
262
263
264
265/**
266 * Shutdown sockets gracefully
267 */
268static void
269do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
270{
271 result = GNUNET_OK;
272 peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket, SHUT_RDWR,
273 &shutdown_completion, cls);
274}
275
276
277/**
278 * Something went wrong and timed out. Kill everything and set error flag
279 */
280static void
281do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
282{
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
284 result = GNUNET_SYSERR;
285 abort_task = 0;
286 do_close (cls, tc);
287}
288
289
290/**
291 * The write completion function; called upon writing some data to stream or
292 * upon error
293 *
294 * @param cls the closure from GNUNET_STREAM_write/read
295 * @param status the status of the stream at the time this function is called
296 * @param size the number of bytes read or written
297 */
298static void
299write_completion (void *cls,
300 enum GNUNET_STREAM_Status status,
301 size_t size)
302{
303 struct PeerData *peer=cls;
304
305 GNUNET_assert (GNUNET_STREAM_OK == status);
306 GNUNET_assert (size <= strlen (data));
307 peer->bytes_wrote += size;
308
309 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
310 {
311 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
312 }
313 else
314 {
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Writing completed\n");
317
318 if (&peer2 == peer) /* Peer1 has finished writing; should read now */
319 {
320 peer->bytes_read = 0;
321 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
322 }
323 else
324 {
325 writing_success = GNUNET_YES;
326 if (GNUNET_YES == reading_success)
327 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
328 }
329 }
330}
331
332
333/**
334 * Function executed after stream has been established
335 *
336 * @param cls the closure from GNUNET_STREAM_open
337 * @param socket socket to use to communicate with the other side (read/write)
338 */
339static void
340stream_open_cb (void *cls,
341 struct GNUNET_STREAM_Socket *socket)
342{
343 struct PeerData *peer=cls;
344
345 GNUNET_assert (&peer2 == peer);
346 GNUNET_assert (socket == peer2.socket);
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Stream established from peer2\n",
348 GNUNET_i2s (&peer1.our_id));
349 peer->bytes_wrote = 0;
350 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
351}
352
353
354/**
355 * Input processor
356 *
357 * @param cls the closure from GNUNET_STREAM_write/read
358 * @param status the status of the stream at the time this function is called
359 * @param data traffic from the other side
360 * @param size the number of bytes available in data read
361 * @return number of bytes of processed from 'data' (any data remaining should be
362 * given to the next time the read processor is called).
363 */
364static size_t
365input_processor (void *cls,
366 enum GNUNET_STREAM_Status status,
367 const void *input_data,
368 size_t size)
369{
370 struct PeerData *peer;
371
372 peer = (struct PeerData *) cls;
373
374 if (GNUNET_STREAM_TIMEOUT == status)
375 {
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Read operation timedout - reading again!\n");
378 GNUNET_assert (0 == size);
379 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
380 return 0;
381 }
382
383 GNUNET_assert (GNUNET_STREAM_OK == status);
384 GNUNET_assert (size <= strlen (data));
385 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
386 (const char *) input_data,
387 size));
388 peer->bytes_read += size;
389
390 if (peer->bytes_read < strlen (data))
391 {
392 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
393 }
394 else
395 {
396 if (&peer1 == peer) /* Peer2 has completed reading; should write */
397 {
398 peer->bytes_wrote = 0;
399 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
400 }
401 else /* Peer1 has completed reading. End of tests */
402 {
403 reading_success = GNUNET_YES;
404 if (GNUNET_YES == writing_success)
405 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
406 }
407 }
408 return size;
409}
410
411
412/**
413 * Functions of this type are called upon new stream connection from other peers
414 *
415 * @param cls the closure from GNUNET_STREAM_listen
416 * @param socket the socket representing the stream
417 * @param initiator the identity of the peer who wants to establish a stream
418 * with us
419 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
420 * stream (the socket will be invalid after the call)
421 */
422static int
423stream_listen_cb (void *cls,
424 struct GNUNET_STREAM_Socket *socket,
425 const struct GNUNET_PeerIdentity *initiator)
426{
427 GNUNET_assert (NULL != socket);
428 GNUNET_assert (NULL != initiator);
429 GNUNET_assert (socket != peer2.socket);
430 GNUNET_assert (0 == memcmp (initiator, &peer2.our_id,
431 sizeof (struct GNUNET_PeerIdentity)));
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Peer connected: %s\n",
433 GNUNET_i2s (&peer1.our_id), GNUNET_i2s (initiator));
434 peer1.socket = socket;
435 peer1.bytes_read = 0;
436 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer1);
437 return GNUNET_OK;
438}
439
440
441/**
442 * Listen success callback; connects a peer to stream as client
443 */
444static void stream_connect (void);
445
446
447/**
448 * Adapter function called to destroy a connection to
449 * a service.
450 *
451 * @param cls closure
452 * @param op_result service handle returned from the connect adapter
453 */
454static void
455stream_da (void *cls, void *op_result)
456{
457 struct GNUNET_STREAM_ListenSocket *lsocket;
458 struct GNUNET_STREAM_Socket *socket;
459
460 if (&peer1 == cls)
461 {
462 lsocket = op_result;
463 GNUNET_STREAM_listen_close (lsocket);
464 GNUNET_TESTBED_operation_done (peer2.op);
465 return;
466 }
467 if (&peer2 == cls)
468 {
469 socket = op_result;
470 GNUNET_STREAM_close (socket);
471 GNUNET_SCHEDULER_shutdown (); /* Exit point of the test */
472 return;
473 }
474 GNUNET_assert (0);
475}
476
477
478/**
479 * Adapter function called to establish a connection to
480 * a service.
481 *
482 * @param cls closure
483 * @param cfg configuration of the peer to connect to; will be available until
484 * GNUNET_TESTBED_operation_done() is called on the operation returned
485 * from GNUNET_TESTBED_service_connect()
486 * @return service handle to return in 'op_result', NULL on error
487 */
488static void *
489stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
490{
491 struct GNUNET_STREAM_ListenSocket *lsocket;
492
493 switch (setup_state)
494 {
495 case PEER1_STREAM_CONNECT:
496 lsocket = GNUNET_STREAM_listen (cfg, 10, &stream_listen_cb, NULL,
497 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
498 &stream_connect, GNUNET_STREAM_OPTION_END);
499 return lsocket;
500 case PEER2_STREAM_CONNECT:
501 peer2.socket = GNUNET_STREAM_open (cfg, &peer1.our_id, 10, &stream_open_cb,
502 &peer2, GNUNET_STREAM_OPTION_END);
503 return peer2.socket;
504 default:
505 GNUNET_assert (0);
506 }
507}
508
509
510/**
511 * Listen success callback; connects a peer to stream as client
512 */
513static void
514stream_connect (void)
515{
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream listen open successful\n");
517 peer2.op = GNUNET_TESTBED_service_connect (&peer2, peer2.peer, "stream",
518 NULL, NULL,
519 stream_ca, stream_da, &peer2);
520 setup_state = PEER2_STREAM_CONNECT;
521}
522
523
524/**
525 * Callback to be called when the requested peer information is available
526 *
527 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
528 * @param op the operation this callback corresponds to
529 * @param pinfo the result; will be NULL if the operation has failed
530 * @param emsg error message if the operation has failed; will be NULL if the
531 * operation is successfull
532 */
533static void
534peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
535 const struct GNUNET_TESTBED_PeerInformation *pinfo,
536 const char *emsg)
537{
538 GNUNET_assert (NULL == emsg);
539 GNUNET_assert (op == op_);
540 switch (setup_state)
541 {
542 case PEER1_GET_IDENTITY:
543 memcpy (&peer1.our_id, pinfo->result.id,
544 sizeof (struct GNUNET_PeerIdentity));
545 GNUNET_TESTBED_operation_done (op);
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n", GNUNET_i2s
547 (&peer1.our_id));
548 op = GNUNET_TESTBED_peer_get_information (peer2.peer,
549 GNUNET_TESTBED_PIT_IDENTITY,
550 &peerinfo_cb, NULL);
551 setup_state = PEER2_GET_IDENTITY;
552 break;
553 case PEER2_GET_IDENTITY:
554 memcpy (&peer2.our_id, pinfo->result.id,
555 sizeof (struct GNUNET_PeerIdentity));
556 GNUNET_TESTBED_operation_done (op);
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n", GNUNET_i2s
558 (&peer2.our_id));
559 peer1.op = GNUNET_TESTBED_service_connect (&peer1, peer1.peer, "stream",
560 NULL, NULL, stream_ca,
561 stream_da, &peer1);
562 setup_state = PEER1_STREAM_CONNECT;
563 break;
564 default:
565 GNUNET_assert (0);
566 }
567}
568
569
570/**
571 * Controller event callback
572 *
573 * @param cls NULL
574 * @param event the controller event
575 */
576static void
577controller_event_cb (void *cls,
578 const struct GNUNET_TESTBED_EventInformation *event)
579{
580 switch (event->type)
581 {
582 case GNUNET_TESTBED_ET_CONNECT:
583 GNUNET_assert (INIT == setup_state);
584 GNUNET_TESTBED_operation_done (op);
585 /* Get the peer identity and configuration of peers */
586 op = GNUNET_TESTBED_peer_get_information (peer1.peer,
587 GNUNET_TESTBED_PIT_IDENTITY,
588 &peerinfo_cb, NULL);
589 setup_state = PEER1_GET_IDENTITY;
590 break;
591 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
592 switch (setup_state)
593 {
594 case PEER1_STREAM_CONNECT:
595 case PEER2_STREAM_CONNECT:
596 GNUNET_assert (NULL == event->details.operation_finished.emsg);
597 break;
598 default:
599 GNUNET_assert (0);
600 }
601 break;
602 default:
603 GNUNET_assert (0);
604 }
605}
606
607
608/**
609 * Signature of a main function for a testcase.
610 *
611 * @param cls closure
612 * @param num_peers number of peers in 'peers'
613 * @param peers handle to peers run in the testbed
614 */
615static void
616test_master (void *cls, unsigned int num_peers,
617 struct GNUNET_TESTBED_Peer **peers)
618{
619 GNUNET_assert (NULL != peers);
620 GNUNET_assert (NULL != peers[0]);
621 GNUNET_assert (NULL != peers[1]);
622 peer1.peer = peers[0];
623 peer2.peer = peers[1];
624 op = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL, peer2.peer, peer1.peer);
625 setup_state = INIT;
626 abort_task =
627 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
628 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
629 NULL);
630}
631
632
633/**
634 * Main function
635 */
636int main (int argc, char **argv)
637{
638 uint64_t event_mask;
639
640 result = GNUNET_NO;
641 event_mask = 0;
642 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
643 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
644 GNUNET_TESTBED_test_run ("test_stream_2peers", "test_stream_local.conf",
645 NUM_PEERS, event_mask, &controller_event_cb, NULL,
646 &test_master, NULL);
647 if (GNUNET_SYSERR == result)
648 return 1;
649 return 0;
650}