1 | /*
|
---|
2 | * Copyright (c) 2017 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <errno.h>
|
---|
30 | #include <inet/endpoint.h>
|
---|
31 | #include <io/log.h>
|
---|
32 | #include <pcut/pcut.h>
|
---|
33 |
|
---|
34 | #include "../conn.h"
|
---|
35 | #include "../rqueue.h"
|
---|
36 | #include "../ucall.h"
|
---|
37 |
|
---|
38 | PCUT_INIT;
|
---|
39 |
|
---|
40 | PCUT_TEST_SUITE(conn);
|
---|
41 |
|
---|
42 | static tcp_rqueue_cb_t test_rqueue_cb = {
|
---|
43 | .seg_received = tcp_as_segment_arrived
|
---|
44 | };
|
---|
45 |
|
---|
46 | PCUT_TEST_BEFORE
|
---|
47 | {
|
---|
48 | errno_t rc;
|
---|
49 |
|
---|
50 | /* We will be calling functions that perform logging */
|
---|
51 | rc = log_init("test-tcp");
|
---|
52 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
53 |
|
---|
54 | rc = tcp_conns_init();
|
---|
55 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
56 |
|
---|
57 | tcp_rqueue_init(&test_rqueue_cb);
|
---|
58 | tcp_rqueue_fibril_start();
|
---|
59 |
|
---|
60 | /* Enable internal loopback */
|
---|
61 | tcp_conn_lb = tcp_lb_segment;
|
---|
62 | }
|
---|
63 |
|
---|
64 | PCUT_TEST_AFTER
|
---|
65 | {
|
---|
66 | tcp_rqueue_fini();
|
---|
67 | tcp_conns_fini();
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Test creating and deleting connection */
|
---|
71 | PCUT_TEST(new_delete)
|
---|
72 | {
|
---|
73 | tcp_conn_t *conn;
|
---|
74 | inet_ep2_t epp;
|
---|
75 |
|
---|
76 | inet_ep2_init(&epp);
|
---|
77 | conn = tcp_conn_new(&epp);
|
---|
78 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
79 |
|
---|
80 | tcp_conn_lock(conn);
|
---|
81 | tcp_conn_reset(conn);
|
---|
82 | tcp_conn_unlock(conn);
|
---|
83 | tcp_conn_delete(conn);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Test adding, finding and deleting a connection */
|
---|
87 | PCUT_TEST(add_find_delete)
|
---|
88 | {
|
---|
89 | tcp_conn_t *conn, *cfound;
|
---|
90 | inet_ep2_t epp;
|
---|
91 | errno_t rc;
|
---|
92 |
|
---|
93 | inet_ep2_init(&epp);
|
---|
94 |
|
---|
95 | conn = tcp_conn_new(&epp);
|
---|
96 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
97 |
|
---|
98 | rc = tcp_conn_add(conn);
|
---|
99 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
100 |
|
---|
101 | /* Find the connection */
|
---|
102 | cfound = tcp_conn_find_ref(&conn->ident);
|
---|
103 | PCUT_ASSERT_EQUALS(conn, cfound);
|
---|
104 | tcp_conn_delref(cfound);
|
---|
105 |
|
---|
106 | /* We should have been assigned a port address, should not match */
|
---|
107 | cfound = tcp_conn_find_ref(&epp);
|
---|
108 | PCUT_ASSERT_EQUALS(NULL, cfound);
|
---|
109 |
|
---|
110 | tcp_conn_lock(conn);
|
---|
111 | tcp_conn_reset(conn);
|
---|
112 | tcp_conn_unlock(conn);
|
---|
113 | tcp_conn_delete(conn);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Test trying to connect to endpoint that sends RST back */
|
---|
117 | PCUT_TEST(connect_rst)
|
---|
118 | {
|
---|
119 | tcp_conn_t *conn;
|
---|
120 | inet_ep2_t epp;
|
---|
121 | errno_t rc;
|
---|
122 |
|
---|
123 | inet_ep2_init(&epp);
|
---|
124 | inet_addr(&epp.local.addr, 127, 0, 0, 1);
|
---|
125 | inet_addr(&epp.remote.addr, 127, 0, 0, 1);
|
---|
126 | epp.remote.port = inet_port_user_lo;
|
---|
127 |
|
---|
128 | conn = tcp_conn_new(&epp);
|
---|
129 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
130 |
|
---|
131 | rc = tcp_conn_add(conn);
|
---|
132 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
133 |
|
---|
134 | PCUT_ASSERT_INT_EQUALS(st_listen, conn->cstate);
|
---|
135 |
|
---|
136 | tcp_conn_lock(conn);
|
---|
137 | tcp_conn_sync(conn);
|
---|
138 | PCUT_ASSERT_INT_EQUALS(st_syn_sent, conn->cstate);
|
---|
139 |
|
---|
140 | while (conn->cstate == st_syn_sent)
|
---|
141 | fibril_condvar_wait(&conn->cstate_cv, &conn->lock);
|
---|
142 |
|
---|
143 | PCUT_ASSERT_INT_EQUALS(st_closed, conn->cstate);
|
---|
144 |
|
---|
145 | tcp_conn_unlock(conn);
|
---|
146 | tcp_conn_delete(conn);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** Test establishing a connection */
|
---|
150 | PCUT_TEST(conn_establish)
|
---|
151 | {
|
---|
152 | tcp_conn_t *cconn, *sconn;
|
---|
153 | inet_ep2_t cepp, sepp;
|
---|
154 | errno_t rc;
|
---|
155 |
|
---|
156 | /* Client EPP */
|
---|
157 | inet_ep2_init(&cepp);
|
---|
158 | inet_addr(&cepp.local.addr, 127, 0, 0, 1);
|
---|
159 | inet_addr(&cepp.remote.addr, 127, 0, 0, 1);
|
---|
160 | cepp.remote.port = inet_port_user_lo;
|
---|
161 |
|
---|
162 | /* Server EPP */
|
---|
163 | inet_ep2_init(&sepp);
|
---|
164 | inet_addr(&sepp.local.addr, 127, 0, 0, 1);
|
---|
165 | sepp.local.port = inet_port_user_lo;
|
---|
166 |
|
---|
167 | /* Client side of the connection */
|
---|
168 | cconn = tcp_conn_new(&cepp);
|
---|
169 | PCUT_ASSERT_NOT_NULL(cconn);
|
---|
170 |
|
---|
171 | rc = tcp_conn_add(cconn);
|
---|
172 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
173 |
|
---|
174 | PCUT_ASSERT_INT_EQUALS(st_listen, cconn->cstate);
|
---|
175 | PCUT_ASSERT_FALSE(tcp_conn_got_syn(cconn));
|
---|
176 |
|
---|
177 | /* Server side of the connection */
|
---|
178 | sconn = tcp_conn_new(&sepp);
|
---|
179 | PCUT_ASSERT_NOT_NULL(sconn);
|
---|
180 |
|
---|
181 | rc = tcp_conn_add(sconn);
|
---|
182 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
183 |
|
---|
184 | PCUT_ASSERT_INT_EQUALS(st_listen, sconn->cstate);
|
---|
185 | PCUT_ASSERT_FALSE(tcp_conn_got_syn(sconn));
|
---|
186 |
|
---|
187 | /* Start establishing the connection */
|
---|
188 |
|
---|
189 | tcp_conn_lock(cconn);
|
---|
190 | tcp_conn_sync(cconn);
|
---|
191 | PCUT_ASSERT_INT_EQUALS(st_syn_sent, cconn->cstate);
|
---|
192 | PCUT_ASSERT_FALSE(tcp_conn_got_syn(cconn));
|
---|
193 |
|
---|
194 | /* Wait for client-side state to transition */
|
---|
195 | while (cconn->cstate == st_syn_sent)
|
---|
196 | fibril_condvar_wait(&cconn->cstate_cv, &cconn->lock);
|
---|
197 |
|
---|
198 | PCUT_ASSERT_INT_EQUALS(st_established, cconn->cstate);
|
---|
199 | PCUT_ASSERT_TRUE(tcp_conn_got_syn(cconn));
|
---|
200 | tcp_conn_unlock(cconn);
|
---|
201 |
|
---|
202 | /* Wait for server-side state to transition */
|
---|
203 | tcp_conn_lock(sconn);
|
---|
204 | while (sconn->cstate == st_listen || sconn->cstate == st_syn_received)
|
---|
205 | fibril_condvar_wait(&sconn->cstate_cv, &sconn->lock);
|
---|
206 |
|
---|
207 | PCUT_ASSERT_INT_EQUALS(st_established, sconn->cstate);
|
---|
208 | PCUT_ASSERT_TRUE(tcp_conn_got_syn(sconn));
|
---|
209 |
|
---|
210 | /* Verify counters */
|
---|
211 | PCUT_ASSERT_EQUALS(cconn->iss + 1, cconn->snd_nxt);
|
---|
212 | PCUT_ASSERT_EQUALS(cconn->iss + 1, cconn->snd_una);
|
---|
213 | PCUT_ASSERT_EQUALS(sconn->iss + 1, sconn->snd_nxt);
|
---|
214 | PCUT_ASSERT_EQUALS(sconn->iss + 1, sconn->snd_una);
|
---|
215 |
|
---|
216 | tcp_conn_unlock(sconn);
|
---|
217 |
|
---|
218 | tcp_conn_lock(cconn);
|
---|
219 | tcp_conn_reset(cconn);
|
---|
220 | tcp_conn_unlock(cconn);
|
---|
221 | tcp_conn_delete(cconn);
|
---|
222 |
|
---|
223 | tcp_conn_lock(sconn);
|
---|
224 | tcp_conn_reset(sconn);
|
---|
225 | tcp_conn_unlock(sconn);
|
---|
226 | tcp_conn_delete(sconn);
|
---|
227 | }
|
---|
228 |
|
---|
229 | PCUT_TEST(ep2_flipped)
|
---|
230 | {
|
---|
231 | inet_ep2_t a, fa;
|
---|
232 |
|
---|
233 | inet_addr(&a.local.addr, 1, 2, 3, 4);
|
---|
234 | a.local.port = 1234;
|
---|
235 | inet_addr(&a.remote.addr, 5, 6, 7, 8);
|
---|
236 | a.remote.port = 5678;
|
---|
237 |
|
---|
238 | tcp_ep2_flipped(&a, &fa);
|
---|
239 |
|
---|
240 | PCUT_ASSERT_INT_EQUALS(a.local.port, fa.remote.port);
|
---|
241 | PCUT_ASSERT_INT_EQUALS(a.remote.port, fa.local.port);
|
---|
242 |
|
---|
243 | PCUT_ASSERT_TRUE(inet_addr_compare(&a.local.addr, &fa.remote.addr));
|
---|
244 | PCUT_ASSERT_TRUE(inet_addr_compare(&a.remote.addr, &fa.local.addr));
|
---|
245 | }
|
---|
246 |
|
---|
247 | PCUT_EXPORT(conn);
|
---|