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 <inet/endpoint.h>
|
---|
30 | #include <io/log.h>
|
---|
31 | #include <pcut/pcut.h>
|
---|
32 |
|
---|
33 | #include "../conn.h"
|
---|
34 | #include "../tqueue.h"
|
---|
35 |
|
---|
36 | PCUT_INIT;
|
---|
37 |
|
---|
38 | PCUT_TEST_SUITE(tqueue);
|
---|
39 |
|
---|
40 | enum {
|
---|
41 | test_seg_max = 10
|
---|
42 | };
|
---|
43 |
|
---|
44 | static int seg_cnt;
|
---|
45 | static tcp_segment_t *trans_seg[test_seg_max];
|
---|
46 |
|
---|
47 | static void tqueue_test_transmit_seg(inet_ep2_t *, tcp_segment_t *);
|
---|
48 |
|
---|
49 | static tcp_tqueue_cb_t tqueue_test_cb = {
|
---|
50 | .transmit_seg = tqueue_test_transmit_seg
|
---|
51 | };
|
---|
52 |
|
---|
53 | PCUT_TEST_BEFORE
|
---|
54 | {
|
---|
55 | errno_t rc;
|
---|
56 |
|
---|
57 | /* We will be calling functions that perform logging */
|
---|
58 | rc = log_init("test-tcp");
|
---|
59 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
60 |
|
---|
61 | rc = tcp_conns_init();
|
---|
62 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
63 | }
|
---|
64 |
|
---|
65 | PCUT_TEST_AFTER
|
---|
66 | {
|
---|
67 | tcp_conns_fini();
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Test */
|
---|
71 | PCUT_TEST(init_fini)
|
---|
72 | {
|
---|
73 | tcp_conn_t *conn;
|
---|
74 | inet_ep2_t epp;
|
---|
75 |
|
---|
76 | /* XXX tqueue can only be created via tcp_conn_new */
|
---|
77 | inet_ep2_init(&epp);
|
---|
78 | conn = tcp_conn_new(&epp);
|
---|
79 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
80 |
|
---|
81 | /* Redirect segment transmission */
|
---|
82 | conn->retransmit.cb = &tqueue_test_cb;
|
---|
83 | seg_cnt = 0;
|
---|
84 |
|
---|
85 | tcp_conn_lock(conn);
|
---|
86 | tcp_conn_reset(conn);
|
---|
87 | tcp_conn_unlock(conn);
|
---|
88 | tcp_conn_delete(conn);
|
---|
89 | PCUT_ASSERT_EQUALS(0, seg_cnt);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Test sending control segment and tearing down a non-empty queue */
|
---|
93 | PCUT_TEST(ctrl_seg_teardown)
|
---|
94 | {
|
---|
95 | tcp_conn_t *conn;
|
---|
96 | inet_ep2_t epp;
|
---|
97 |
|
---|
98 | /* XXX tqueue can only be created via tcp_conn_new */
|
---|
99 | inet_ep2_init(&epp);
|
---|
100 | conn = tcp_conn_new(&epp);
|
---|
101 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
102 |
|
---|
103 | conn->snd_nxt = 10;
|
---|
104 |
|
---|
105 | /* Redirect segment transmission */
|
---|
106 | conn->retransmit.cb = &tqueue_test_cb;
|
---|
107 | seg_cnt = 0;
|
---|
108 |
|
---|
109 | tcp_conn_lock(conn);
|
---|
110 | tcp_tqueue_ctrl_seg(conn, CTL_SYN);
|
---|
111 | tcp_conn_reset(conn);
|
---|
112 | tcp_conn_unlock(conn);
|
---|
113 | PCUT_ASSERT_EQUALS(11, conn->snd_nxt);
|
---|
114 |
|
---|
115 | tcp_conn_delete(conn);
|
---|
116 | PCUT_ASSERT_EQUALS(1, seg_cnt);
|
---|
117 | PCUT_ASSERT_EQUALS(CTL_SYN, trans_seg[0]->ctrl);
|
---|
118 | PCUT_ASSERT_EQUALS(10, trans_seg[0]->seq);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Test sending data and FIN */
|
---|
122 | PCUT_TEST(new_data_fin)
|
---|
123 | {
|
---|
124 | tcp_conn_t *conn;
|
---|
125 | inet_ep2_t epp;
|
---|
126 | int i;
|
---|
127 |
|
---|
128 | /* XXX tqueue can only be created via tcp_conn_new */
|
---|
129 | inet_ep2_init(&epp);
|
---|
130 | conn = tcp_conn_new(&epp);
|
---|
131 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
132 |
|
---|
133 | conn->cstate = st_established;
|
---|
134 | conn->snd_una = 10;
|
---|
135 | conn->snd_nxt = 10;
|
---|
136 | conn->snd_wnd = 1024;
|
---|
137 | conn->snd_buf_used = 20;
|
---|
138 | conn->snd_buf_fin = true;
|
---|
139 | for (i = 0; i < 20; i++)
|
---|
140 | conn->snd_buf[i] = i;
|
---|
141 |
|
---|
142 | /* Redirect segment transmission */
|
---|
143 | conn->retransmit.cb = &tqueue_test_cb;
|
---|
144 | seg_cnt = 0;
|
---|
145 |
|
---|
146 | tcp_conn_lock(conn);
|
---|
147 | tcp_tqueue_new_data(conn);
|
---|
148 | tcp_conn_reset(conn);
|
---|
149 | tcp_conn_unlock(conn);
|
---|
150 | PCUT_ASSERT_EQUALS(31, conn->snd_nxt);
|
---|
151 | PCUT_ASSERT_EQUALS(0, conn->snd_buf_used);
|
---|
152 | PCUT_ASSERT_FALSE(conn->snd_buf_fin);
|
---|
153 |
|
---|
154 | tcp_conn_delete(conn);
|
---|
155 | PCUT_ASSERT_EQUALS(1, seg_cnt);
|
---|
156 | PCUT_ASSERT_EQUALS(CTL_FIN | CTL_ACK, trans_seg[0]->ctrl);
|
---|
157 | PCUT_ASSERT_EQUALS(10, trans_seg[0]->seq);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** Test sending data when send window is smaller */
|
---|
161 | PCUT_TEST(new_data_small_win)
|
---|
162 | {
|
---|
163 | tcp_conn_t *conn;
|
---|
164 | inet_ep2_t epp;
|
---|
165 | int i;
|
---|
166 |
|
---|
167 | /* XXX tqueue can only be created via tcp_conn_new */
|
---|
168 | inet_ep2_init(&epp);
|
---|
169 | conn = tcp_conn_new(&epp);
|
---|
170 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
171 |
|
---|
172 | conn->cstate = st_established;
|
---|
173 | conn->snd_una = 10;
|
---|
174 | conn->snd_nxt = 10;
|
---|
175 | conn->snd_wnd = 5;
|
---|
176 | conn->snd_buf_used = 30;
|
---|
177 | conn->snd_buf_fin = false;
|
---|
178 | for (i = 0; i < 30; i++)
|
---|
179 | conn->snd_buf[i] = i;
|
---|
180 |
|
---|
181 | /* Redirect segment transmission */
|
---|
182 | conn->retransmit.cb = &tqueue_test_cb;
|
---|
183 | seg_cnt = 0;
|
---|
184 |
|
---|
185 | tcp_conn_lock(conn);
|
---|
186 | tcp_tqueue_new_data(conn);
|
---|
187 | tcp_conn_reset(conn);
|
---|
188 | tcp_conn_unlock(conn);
|
---|
189 |
|
---|
190 | PCUT_ASSERT_EQUALS(15, conn->snd_nxt);
|
---|
191 | PCUT_ASSERT_EQUALS(25, conn->snd_buf_used);
|
---|
192 | PCUT_ASSERT_FALSE(conn->snd_buf_fin);
|
---|
193 | for (i = 0; i < 25; i++)
|
---|
194 | PCUT_ASSERT_INT_EQUALS(5 + i, conn->snd_buf[i]);
|
---|
195 |
|
---|
196 | tcp_conn_delete(conn);
|
---|
197 | PCUT_ASSERT_EQUALS(1, seg_cnt);
|
---|
198 | PCUT_ASSERT_EQUALS(CTL_ACK, trans_seg[0]->ctrl);
|
---|
199 | PCUT_ASSERT_EQUALS(10, trans_seg[0]->seq);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /** Test flushing tqueue due to receiving an ACK */
|
---|
203 | PCUT_TEST(ack_received)
|
---|
204 | {
|
---|
205 | tcp_conn_t *conn;
|
---|
206 | inet_ep2_t epp;
|
---|
207 | int i;
|
---|
208 |
|
---|
209 | /* XXX tqueue can only be created via tcp_conn_new */
|
---|
210 | inet_ep2_init(&epp);
|
---|
211 | conn = tcp_conn_new(&epp);
|
---|
212 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
213 |
|
---|
214 | conn->cstate = st_established;
|
---|
215 | conn->snd_una = 10;
|
---|
216 | conn->snd_nxt = 10;
|
---|
217 | conn->snd_wnd = 1024;
|
---|
218 |
|
---|
219 | /* Redirect segment transmission */
|
---|
220 | conn->retransmit.cb = &tqueue_test_cb;
|
---|
221 | seg_cnt = 0;
|
---|
222 |
|
---|
223 | tcp_conn_lock(conn);
|
---|
224 |
|
---|
225 | /* Queue first data segment */
|
---|
226 | conn->snd_buf_used = 10;
|
---|
227 | conn->snd_buf_fin = false;
|
---|
228 | for (i = 0; i < 10; i++)
|
---|
229 | conn->snd_buf[i] = i;
|
---|
230 | tcp_tqueue_new_data(conn);
|
---|
231 |
|
---|
232 | PCUT_ASSERT_EQUALS(20, conn->snd_nxt);
|
---|
233 |
|
---|
234 | /* Queue second data segment */
|
---|
235 | conn->snd_buf_used = 20;
|
---|
236 | conn->snd_buf_fin = false;
|
---|
237 | for (i = 0; i < 20; i++)
|
---|
238 | conn->snd_buf[i] = i;
|
---|
239 | tcp_tqueue_new_data(conn);
|
---|
240 |
|
---|
241 | PCUT_ASSERT_EQUALS(40, conn->snd_nxt);
|
---|
242 |
|
---|
243 | PCUT_ASSERT_INT_EQUALS(2, list_count(&conn->retransmit.list));
|
---|
244 |
|
---|
245 | /* One of the two segments is acked */
|
---|
246 | conn->snd_una = 20;
|
---|
247 | tcp_tqueue_ack_received(conn);
|
---|
248 |
|
---|
249 | PCUT_ASSERT_INT_EQUALS(1, list_count(&conn->retransmit.list));
|
---|
250 |
|
---|
251 | tcp_conn_reset(conn);
|
---|
252 | tcp_conn_unlock(conn);
|
---|
253 | tcp_conn_delete(conn);
|
---|
254 | }
|
---|
255 |
|
---|
256 | static void tqueue_test_transmit_seg(inet_ep2_t *epp, tcp_segment_t *seg)
|
---|
257 | {
|
---|
258 | trans_seg[seg_cnt++] = seg;
|
---|
259 | }
|
---|
260 |
|
---|
261 | PCUT_EXPORT(tqueue);
|
---|