[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"
|
---|
| 34 | #include "../tqueue.h"
|
---|
| 35 |
|
---|
[3f932a7e] | 36 | PCUT_INIT;
|
---|
[975d528] | 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 | {
|
---|
[b7fd2a0] | 55 | errno_t rc;
|
---|
[975d528] | 56 |
|
---|
| 57 | /* We will be calling functions that perform logging */
|
---|
| 58 | rc = log_init("test-tcp");
|
---|
[e1b4ae0] | 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();
|
---|
[975d528] | 68 | }
|
---|
| 69 |
|
---|
[e1b4ae0] | 70 |
|
---|
[975d528] | 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);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /** Test sending data and FIN */
|
---|
| 123 | PCUT_TEST(new_data_fin)
|
---|
| 124 | {
|
---|
| 125 | tcp_conn_t *conn;
|
---|
| 126 | inet_ep2_t epp;
|
---|
| 127 | int i;
|
---|
| 128 |
|
---|
| 129 | /* XXX tqueue can only be created via tcp_conn_new */
|
---|
| 130 | inet_ep2_init(&epp);
|
---|
| 131 | conn = tcp_conn_new(&epp);
|
---|
| 132 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
| 133 |
|
---|
| 134 | conn->cstate = st_established;
|
---|
| 135 | conn->snd_una = 10;
|
---|
| 136 | conn->snd_nxt = 10;
|
---|
| 137 | conn->snd_wnd = 1024;
|
---|
| 138 | conn->snd_buf_used = 20;
|
---|
| 139 | conn->snd_buf_fin = true;
|
---|
| 140 | for (i = 0; i < 20; i++)
|
---|
| 141 | conn->snd_buf[i] = i;
|
---|
| 142 |
|
---|
| 143 | /* Redirect segment transmission */
|
---|
| 144 | conn->retransmit.cb = &tqueue_test_cb;
|
---|
| 145 | seg_cnt = 0;
|
---|
| 146 |
|
---|
| 147 | tcp_conn_lock(conn);
|
---|
| 148 | tcp_tqueue_new_data(conn);
|
---|
| 149 | tcp_conn_reset(conn);
|
---|
| 150 | tcp_conn_unlock(conn);
|
---|
| 151 | PCUT_ASSERT_EQUALS(31, conn->snd_nxt);
|
---|
| 152 | PCUT_ASSERT_EQUALS(0, conn->snd_buf_used);
|
---|
| 153 | PCUT_ASSERT_FALSE(conn->snd_buf_fin);
|
---|
| 154 |
|
---|
| 155 | tcp_conn_delete(conn);
|
---|
| 156 | PCUT_ASSERT_EQUALS(1, seg_cnt);
|
---|
| 157 | PCUT_ASSERT_EQUALS(CTL_FIN | CTL_ACK, trans_seg[0]->ctrl);
|
---|
| 158 | PCUT_ASSERT_EQUALS(10, trans_seg[0]->seq);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /** Test sending data when send window is smaller */
|
---|
| 162 | PCUT_TEST(new_data_small_win)
|
---|
| 163 | {
|
---|
| 164 | tcp_conn_t *conn;
|
---|
| 165 | inet_ep2_t epp;
|
---|
| 166 | int i;
|
---|
| 167 |
|
---|
| 168 | /* XXX tqueue can only be created via tcp_conn_new */
|
---|
| 169 | inet_ep2_init(&epp);
|
---|
| 170 | conn = tcp_conn_new(&epp);
|
---|
| 171 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
| 172 |
|
---|
| 173 | conn->cstate = st_established;
|
---|
| 174 | conn->snd_una = 10;
|
---|
| 175 | conn->snd_nxt = 10;
|
---|
| 176 | conn->snd_wnd = 5;
|
---|
| 177 | conn->snd_buf_used = 30;
|
---|
| 178 | conn->snd_buf_fin = false;
|
---|
| 179 | for (i = 0; i < 30; i++)
|
---|
| 180 | conn->snd_buf[i] = i;
|
---|
| 181 |
|
---|
| 182 | /* Redirect segment transmission */
|
---|
| 183 | conn->retransmit.cb = &tqueue_test_cb;
|
---|
| 184 | seg_cnt = 0;
|
---|
| 185 |
|
---|
| 186 | tcp_conn_lock(conn);
|
---|
| 187 | tcp_tqueue_new_data(conn);
|
---|
| 188 | tcp_conn_reset(conn);
|
---|
| 189 | tcp_conn_unlock(conn);
|
---|
| 190 |
|
---|
| 191 | PCUT_ASSERT_EQUALS(15, conn->snd_nxt);
|
---|
| 192 | PCUT_ASSERT_EQUALS(25, conn->snd_buf_used);
|
---|
| 193 | PCUT_ASSERT_FALSE(conn->snd_buf_fin);
|
---|
| 194 | for (i = 0; i < 25; i++)
|
---|
| 195 | PCUT_ASSERT_INT_EQUALS(5 + i, conn->snd_buf[i]);
|
---|
| 196 |
|
---|
| 197 | tcp_conn_delete(conn);
|
---|
| 198 | PCUT_ASSERT_EQUALS(1, seg_cnt);
|
---|
| 199 | PCUT_ASSERT_EQUALS(CTL_ACK, trans_seg[0]->ctrl);
|
---|
| 200 | PCUT_ASSERT_EQUALS(10, trans_seg[0]->seq);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | /** Test flushing tqueue due to receiving an ACK */
|
---|
| 204 | PCUT_TEST(ack_received)
|
---|
| 205 | {
|
---|
| 206 | tcp_conn_t *conn;
|
---|
| 207 | inet_ep2_t epp;
|
---|
| 208 | int i;
|
---|
| 209 |
|
---|
| 210 | /* XXX tqueue can only be created via tcp_conn_new */
|
---|
| 211 | inet_ep2_init(&epp);
|
---|
| 212 | conn = tcp_conn_new(&epp);
|
---|
| 213 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
| 214 |
|
---|
| 215 | conn->cstate = st_established;
|
---|
| 216 | conn->snd_una = 10;
|
---|
| 217 | conn->snd_nxt = 10;
|
---|
| 218 | conn->snd_wnd = 1024;
|
---|
| 219 |
|
---|
| 220 | /* Redirect segment transmission */
|
---|
| 221 | conn->retransmit.cb = &tqueue_test_cb;
|
---|
| 222 | seg_cnt = 0;
|
---|
| 223 |
|
---|
| 224 | tcp_conn_lock(conn);
|
---|
| 225 |
|
---|
| 226 | /* Queue first data segment */
|
---|
| 227 | conn->snd_buf_used = 10;
|
---|
| 228 | conn->snd_buf_fin = false;
|
---|
| 229 | for (i = 0; i < 10; i++)
|
---|
| 230 | conn->snd_buf[i] = i;
|
---|
| 231 | tcp_tqueue_new_data(conn);
|
---|
| 232 |
|
---|
| 233 | PCUT_ASSERT_EQUALS(20, conn->snd_nxt);
|
---|
| 234 |
|
---|
| 235 | /* Queue second data segment */
|
---|
| 236 | conn->snd_buf_used = 20;
|
---|
| 237 | conn->snd_buf_fin = false;
|
---|
| 238 | for (i = 0; i < 20; i++)
|
---|
| 239 | conn->snd_buf[i] = i;
|
---|
| 240 | tcp_tqueue_new_data(conn);
|
---|
| 241 |
|
---|
| 242 | PCUT_ASSERT_EQUALS(40, conn->snd_nxt);
|
---|
| 243 |
|
---|
| 244 | PCUT_ASSERT_INT_EQUALS(2, list_count(&conn->retransmit.list));
|
---|
| 245 |
|
---|
| 246 | /* One of the two segments is acked */
|
---|
| 247 | conn->snd_una = 20;
|
---|
| 248 | tcp_tqueue_ack_received(conn);
|
---|
| 249 |
|
---|
| 250 | PCUT_ASSERT_INT_EQUALS(1, list_count(&conn->retransmit.list));
|
---|
| 251 |
|
---|
| 252 | tcp_conn_reset(conn);
|
---|
| 253 | tcp_conn_unlock(conn);
|
---|
| 254 | tcp_conn_delete(conn);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | static void tqueue_test_transmit_seg(inet_ep2_t *epp, tcp_segment_t *seg)
|
---|
| 258 | {
|
---|
| 259 | trans_seg[seg_cnt++] = seg;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | PCUT_EXPORT(tqueue);
|
---|