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 <pcut/pcut.h>
|
---|
31 |
|
---|
32 | #include "../conn.h"
|
---|
33 | #include "../iqueue.h"
|
---|
34 | #include "../segment.h"
|
---|
35 |
|
---|
36 | PCUT_INIT;
|
---|
37 |
|
---|
38 | PCUT_TEST_SUITE(iqueue);
|
---|
39 |
|
---|
40 | /** Test empty queue */
|
---|
41 | PCUT_TEST(empty_queue)
|
---|
42 | {
|
---|
43 | tcp_conn_t *conn;
|
---|
44 | tcp_iqueue_t iqueue;
|
---|
45 | inet_ep2_t epp;
|
---|
46 | tcp_segment_t *rseg;
|
---|
47 | errno_t rc;
|
---|
48 |
|
---|
49 | inet_ep2_init(&epp);
|
---|
50 | conn = tcp_conn_new(&epp);
|
---|
51 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
52 |
|
---|
53 | conn->rcv_nxt = 10;
|
---|
54 | conn->rcv_wnd = 20;
|
---|
55 |
|
---|
56 | tcp_iqueue_init(&iqueue, conn);
|
---|
57 | rc = tcp_iqueue_get_ready_seg(&iqueue, &rseg);
|
---|
58 | PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
|
---|
59 |
|
---|
60 | tcp_conn_delete(conn);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** Test iqeue with one segment */
|
---|
64 | PCUT_TEST(one_segment)
|
---|
65 | {
|
---|
66 | tcp_conn_t *conn;
|
---|
67 | tcp_iqueue_t iqueue;
|
---|
68 | inet_ep2_t epp;
|
---|
69 | tcp_segment_t *rseg;
|
---|
70 | tcp_segment_t *seg;
|
---|
71 | void *data;
|
---|
72 | size_t dsize;
|
---|
73 | errno_t rc;
|
---|
74 |
|
---|
75 | inet_ep2_init(&epp);
|
---|
76 | conn = tcp_conn_new(&epp);
|
---|
77 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
78 |
|
---|
79 | conn->rcv_nxt = 10;
|
---|
80 | conn->rcv_wnd = 20;
|
---|
81 |
|
---|
82 | dsize = 15;
|
---|
83 | data = calloc(dsize, 1);
|
---|
84 | PCUT_ASSERT_NOT_NULL(data);
|
---|
85 |
|
---|
86 | seg = tcp_segment_make_data(0, data, dsize);
|
---|
87 | PCUT_ASSERT_NOT_NULL(seg);
|
---|
88 |
|
---|
89 | tcp_iqueue_init(&iqueue, conn);
|
---|
90 | rc = tcp_iqueue_get_ready_seg(&iqueue, &rseg);
|
---|
91 | PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
|
---|
92 |
|
---|
93 | seg->seq = 10;
|
---|
94 | tcp_iqueue_insert_seg(&iqueue, seg);
|
---|
95 | rc = tcp_iqueue_get_ready_seg(&iqueue, &rseg);
|
---|
96 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
97 |
|
---|
98 | seg->seq = 15;
|
---|
99 | tcp_iqueue_insert_seg(&iqueue, seg);
|
---|
100 | rc = tcp_iqueue_get_ready_seg(&iqueue, &rseg);
|
---|
101 | PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
|
---|
102 | tcp_iqueue_remove_seg(&iqueue, seg);
|
---|
103 |
|
---|
104 | tcp_segment_delete(seg);
|
---|
105 | free(data);
|
---|
106 | tcp_conn_delete(conn);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Test iqeue with two segments */
|
---|
110 | PCUT_TEST(two_segments)
|
---|
111 | {
|
---|
112 | tcp_conn_t *conn;
|
---|
113 | tcp_iqueue_t iqueue;
|
---|
114 | inet_ep2_t epp;
|
---|
115 | tcp_segment_t *rseg;
|
---|
116 | tcp_segment_t *seg1, *seg2;
|
---|
117 | void *data;
|
---|
118 | size_t dsize;
|
---|
119 | errno_t rc;
|
---|
120 |
|
---|
121 | inet_ep2_init(&epp);
|
---|
122 | conn = tcp_conn_new(&epp);
|
---|
123 | PCUT_ASSERT_NOT_NULL(conn);
|
---|
124 |
|
---|
125 | conn->rcv_nxt = 10;
|
---|
126 | conn->rcv_wnd = 20;
|
---|
127 |
|
---|
128 | dsize = 15;
|
---|
129 | data = calloc(dsize, 1);
|
---|
130 | PCUT_ASSERT_NOT_NULL(data);
|
---|
131 |
|
---|
132 | seg1 = tcp_segment_make_data(0, data, dsize);
|
---|
133 | PCUT_ASSERT_NOT_NULL(seg1);
|
---|
134 | seg2 = tcp_segment_make_data(0, data, dsize);
|
---|
135 | PCUT_ASSERT_NOT_NULL(seg2);
|
---|
136 |
|
---|
137 | tcp_iqueue_init(&iqueue, conn);
|
---|
138 | rc = tcp_iqueue_get_ready_seg(&iqueue, &rseg);
|
---|
139 | PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
|
---|
140 |
|
---|
141 | /* Test reception in ascending order */
|
---|
142 | seg1->seq = 5;
|
---|
143 | tcp_iqueue_insert_seg(&iqueue, seg1);
|
---|
144 | seg2->seq = 10;
|
---|
145 | tcp_iqueue_insert_seg(&iqueue, seg2);
|
---|
146 |
|
---|
147 | rc = tcp_iqueue_get_ready_seg(&iqueue, &rseg);
|
---|
148 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
149 | PCUT_ASSERT_TRUE(rseg == seg1);
|
---|
150 |
|
---|
151 | rc = tcp_iqueue_get_ready_seg(&iqueue, &rseg);
|
---|
152 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
153 | PCUT_ASSERT_TRUE(rseg == seg2);
|
---|
154 |
|
---|
155 | /* Test reception in descending order */
|
---|
156 | seg1->seq = 10;
|
---|
157 | tcp_iqueue_insert_seg(&iqueue, seg1);
|
---|
158 | seg2->seq = 5;
|
---|
159 | tcp_iqueue_insert_seg(&iqueue, seg2);
|
---|
160 |
|
---|
161 | rc = tcp_iqueue_get_ready_seg(&iqueue, &rseg);
|
---|
162 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
163 | PCUT_ASSERT_EQUALS(seg2, rseg);
|
---|
164 |
|
---|
165 | rc = tcp_iqueue_get_ready_seg(&iqueue, &rseg);
|
---|
166 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
167 | PCUT_ASSERT_EQUALS(seg1, rseg);
|
---|
168 |
|
---|
169 | rc = tcp_iqueue_get_ready_seg(&iqueue, &rseg);
|
---|
170 | PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
|
---|
171 |
|
---|
172 | tcp_segment_delete(seg1);
|
---|
173 | tcp_segment_delete(seg2);
|
---|
174 | free(data);
|
---|
175 | tcp_conn_delete(conn);
|
---|
176 | }
|
---|
177 |
|
---|
178 | PCUT_EXPORT(iqueue);
|
---|