blob: 46453c84adf8e81ca9b56c7b2f9f44d2158d4b8b [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36#include "tests.h"
37
38#include <cnxctx.h>
39
40#ifndef TEST_PORT
41#define TEST_PORT 3868
42#endif /* TEST_PORT */
43
44#ifndef NB_STREAMS
45#define NB_STREAMS 10
46#endif /* NB_STREAMS */
47
48
49
50/* Main test routine */
51int main(int argc, char *argv[])
52{
53#ifdef DISABLE_SCTP
54 INIT_FD();
55 /* In this case, we don't perform this simple test */
56 PASSTEST();
57#else /* DISABLE_SCTP */
58 struct cnxctx cli, srv; /* we use only their cc_socket & cc_state */
59 int sock;
60 char buf1[]="abcdef";
61 char *buf2;
62 size_t sz;
63 struct iovec iov;
64 struct fd_list eps = FD_LIST_INITIALIZER(eps);
65 uint16_t str;
66 int ev;
67
68 /* Initialize the server addresses */
69 {
70 struct addrinfo hints, *ai, *aip;
71 memset(&hints, 0, sizeof(hints));
72 hints.ai_flags = AI_NUMERICSERV;
73 hints.ai_family = AF_INET;
74 CHECK( 0, getaddrinfo("localhost", _stringize(TEST_PORT), &hints, &ai) );
75 aip = ai;
76 while (aip) {
77 CHECK( 0, fd_ep_add_merge( &eps, aip->ai_addr, aip->ai_addrlen, EP_FL_DISC | EP_ACCEPTALL ));
78 aip = aip->ai_next;
79 };
80 freeaddrinfo(ai);
81
82 CHECK( 0, FD_IS_LIST_EMPTY(&eps) ? 1 : 0 );
83 }
84
85 memset(&cli, 0, sizeof(cli));
86 memset(&srv, 0, sizeof(srv));
87
88 /* First, initialize the daemon modules */
89 INIT_FD();
90
91 /* Restrain the # of streams */
92 fd_g_config->cnf_sctp_str = NB_STREAMS;
93
94 /* Create the server socket */
95 CHECK( 0, fd_sctp_create_bind_server( &sock, AF_INET6, &eps, TEST_PORT ));
96
97 /* Accept incoming clients */
98 CHECK( 0, fd_sctp_listen( sock ));
99
100 /* Now, create the client socket */
101 CHECK( 0, fd_sctp_client( &cli.cc_socket, 0, TEST_PORT, &eps ));
102
103 /* Accept this connection */
104 srv.cc_socket = accept(sock, NULL, NULL);
105
106 /* Send a first message */
107 iov.iov_base = buf1;
108 iov.iov_len = sizeof(buf1);
109 CHECK( sizeof(buf1), fd_sctp_sendstrv(&srv, 1, &iov, 1 ) );
110 CHECK( 0, srv.cc_state);
111
112 /* Receive this message */
113redo1:
114 CHECK( 0, fd_sctp_recvmeta(&cli, &str, (uint8_t **)&buf2, &sz, &ev) );
115 if (ev == FDEVP_CNX_EP_CHANGE)
116 goto redo1;
117 CHECK( FDEVP_CNX_MSG_RECV, ev);
118 CHECK( 0, cli.cc_state);
119 CHECK( 1, str);
120 CHECK( sizeof(buf1), sz );
121 CHECK( 0, memcmp(buf1, buf2, sz) );
122 free(buf2); buf2 = NULL;
123
124 /* Send in the other direction */
125 CHECK( sizeof(buf1), fd_sctp_sendstrv(&cli, 2, &iov, 1) );
126 CHECK( 0, cli.cc_state);
127
128 /* Receive this message */
129redo2:
130 CHECK( 0, fd_sctp_recvmeta(&srv, &str, (uint8_t **)&buf2, &sz, &ev) );
131 if (ev == FDEVP_CNX_EP_CHANGE)
132 goto redo2;
133 CHECK( FDEVP_CNX_MSG_RECV, ev);
134 CHECK( 0, srv.cc_state);
135 CHECK( 2, str);
136 CHECK( sizeof(buf1), sz );
137 CHECK( 0, memcmp(buf1, buf2, sz) );
138 free(buf2); buf2 = NULL;
139
140 /* That's all for the tests yet */
141 PASSTEST();
142#endif /* DISABLE_SCTP */
143}
144