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