4e4aeb4724f18a529c3cf83ed660994c8de893df
[tpg/acess2.git] / Tools / NetTest_Runner / test_tcp.c
1 /*
2  * Acess2 Network Stack Tester
3  * - By John Hodge (thePowersGang)
4  *
5  * test_tcp.c
6  * - Tests for the behavior of the "Transmission Control Protocol"
7  */
8 #include "test.h"
9 #include "tests.h"
10 #include "net.h"
11 #include "stack.h"
12 #include "arp.h"
13 #include "tcp.h"
14 #include <string.h>
15
16 #define RX_HEADER \
17         size_t  rxlen, ofs, len; \
18         char rxbuf[MTU]
19 #define TEST_HEADER \
20         TEST_SETNAME(__func__);\
21         RX_HEADER
22
23 #define TEST_ASSERT_rx()        TEST_ASSERT( rxlen = Net_Receive(0, sizeof(rxbuf), rxbuf, ERX_TIMEOUT) )
24 #define TEST_ASSERT_no_rx()     TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 )
25 const int       ERX_TIMEOUT = 1000;     // Expect RX timeout (timeout=failure)
26 const int       NRX_TIMEOUT = 250;      // Not expect RX timeout (timeout=success)
27 const int       RETX_TIMEOUT = 1000;    // OS PARAM - Retransmit timeout
28 const int       LOST_TIMEOUT = 1000;    // OS PARAM - Time before sending an ACK 
29 const int       DACK_TIMEOUT = 500;     // OS PARAM - Timeout for delayed ACKs
30 const size_t    DACK_BYTES = 4096;      // OS PARAM - Threshold for delayed ACKs
31
32 bool Test_TCP_Basic(void)
33 {
34         TEST_HEADER;
35
36         tTCPConn        testconn = {
37                 .IFNum = 0, .AF = 4,
38                 .RAddr = BLOB(TEST_IP),
39                 .LAddr = BLOB(HOST_IP),
40                 .RPort = 80,
41                 .LPort = 11200,
42                 .Window = 0x1000,
43                 .LSeq = 0x1000,
44                 .RSeq = 0,
45         };
46
47         const char testblob[] = "HelloWorld, this is some random testing data for TCP\xFF\x00\x66\x12\x12.";
48         const size_t    testblob_len = sizeof(testblob);
49
50         // TODO: Check checksum failures        
51
52         // 1. Test packets to closed port
53         // > RFC793 Pg.65
54         
55         // 1.1. Send SYN packet
56         TCP_SendC(&testconn, TCP_SYN, testblob_len, testblob);
57         testconn.RSeq = 0;
58         testconn.LSeq += testblob_len;
59         // Expect RST,ACK with SEQ=0
60         TEST_ASSERT_rx();
61         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST|TCP_ACK) );
62         TEST_ASSERT_REL(ofs, ==, rxlen);
63         
64         // 1.2. Send a SYN,ACK packet
65         testconn.RSeq = 12345;
66         TCP_SendC(&testconn, TCP_SYN|TCP_ACK, 0, NULL);
67         // Expect a TCP_RST with SEQ=ACK
68         TEST_ASSERT_rx();
69         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
70         TEST_ASSERT_REL(ofs, ==, rxlen);
71         testconn.LSeq ++;
72         
73         // 1.3. Send a RST packet
74         TCP_SendC(&testconn, TCP_RST, 0, NULL);
75         // Expect nothing
76         TEST_ASSERT_no_rx();
77         testconn.LSeq ++;
78         
79         // 1.3. Send a RST,ACK packet
80         TCP_SendC(&testconn, TCP_RST|TCP_ACK, 0, NULL);
81         // Expect nothing
82         TEST_ASSERT_no_rx();
83         testconn.LSeq ++;
84
85         
86         // 2. Establishing connection with a server
87         testconn.RPort = 7;
88         testconn.LPort = 11239;
89         Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
90
91         // >>> STATE: LISTEN
92
93         // 2.1. Send RST
94         TCP_SendC(&testconn, TCP_RST, 0, NULL);
95         // - Expect nothing
96         TEST_ASSERT_no_rx();
97         // 2.2. Send ACK
98         TCP_SendC(&testconn, TCP_ACK, 0, NULL);
99         // - Expect RST
100         TEST_ASSERT_rx();
101         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
102         TEST_ASSERT_REL(ofs, ==, rxlen);
103
104         // 2.3. Begin hanshake (SYN)
105         // TODO: Test "If the SYN bit is set, check the security."
106         TCP_SendC(&testconn, TCP_SYN, 0, NULL);
107         testconn.LSeq ++;
108         // - Expect SYN,ACK with ACK == SEQ+1
109         TEST_ASSERT_rx();
110         TCP_SkipCheck_Seq(true);
111         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
112         testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
113
114         // >>> STATE: SYN-RECEIVED
115         // TODO: Test other transitions from SYN-RECEIVED
116         
117         // 2.4. Complete handshake, TCP ACK
118         TCP_SendC(&testconn, TCP_ACK, 0, NULL);
119         // - Expect nothing
120         TEST_ASSERT_no_rx();
121
122         // >>> STATE: ESTABLISHED
123         
124         // 2.5. Send data
125         TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
126         testconn.LSeq += testblob_len;
127
128         // Expect echoed reponse with ACK
129         TEST_ASSERT_rx();
130         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
131         TEST_ASSERT_REL( len, ==, testblob_len );
132         TEST_ASSERT( memcmp(rxbuf + ofs, testblob, testblob_len) == 0 );
133         testconn.RSeq += testblob_len;
134
135         // Send something short
136         const char testblob2[] = "test blob two.";
137         const size_t    testblob2_len = sizeof(testblob2);
138         TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob2_len, testblob2);
139         testconn.LSeq += testblob2_len;
140         // Expect response with data and ACK
141         TEST_ASSERT_rx();
142         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
143         TEST_ASSERT_REL( len, ==, testblob2_len );
144         TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
145         
146         // Wait for just under retransmit time, expecting nothing
147         #if TEST_TIMERS
148         TEST_ASSERT( 0 == Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) );
149         // Now expect the previous message
150         TEST_ASSERT_rx();
151         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
152         TEST_ASSERT_REL( len, ==, testblob2_len );
153         TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
154         #else
155         TEST_WARN("Not testing retransmit timer");
156         #endif
157         testconn.RSeq += testblob2_len;
158         
159         // Send explicit acknowledgement
160         TCP_SendC(&testconn, TCP_ACK, 0, NULL);
161         TEST_ASSERT_no_rx();
162         
163         // TODO: Test delayed ACKs (Timeout and data)
164         // > Requires inhibiting the server's echo response?
165         
166         // === Test out-of-order packets ===
167         testconn.LSeq += testblob2_len; // raise sequence number
168         TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
169         // - previous data has not been sent, expect no response for ()
170         // TODO: Should this ACK be delayed?
171         //TEST_ASSERT_no_rx();
172         // - Expect an ACK of the highest received packet
173         testconn.LSeq -= testblob2_len;
174         TEST_ASSERT_rx();
175         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
176         TEST_ASSERT_REL( len, ==, 0 );
177         // - Send missing data
178         TCP_SendC(&testconn, TCP_ACK, testblob2_len, testblob2);
179         testconn.LSeq += testblob_len+testblob2_len;    // raise sequence number
180         // - Expect echo response with all sent data
181         TEST_ASSERT_rx();
182         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
183         TEST_ASSERT_REL( len, ==, testblob_len+testblob2_len );
184         TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
185         TEST_ASSERT( memcmp(rxbuf + ofs+testblob2_len, testblob, testblob_len) == 0 );
186         testconn.RSeq += len;
187         
188         // 2.6. Close connection (TCP FIN)
189         TCP_SendC(&testconn, TCP_ACK|TCP_FIN, 0, NULL);
190         testconn.LSeq ++;       // Empty = 1 byte
191         // Expect ACK? (Does acess do delayed ACKs here?)
192         TEST_ASSERT_rx();
193         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
194         TEST_ASSERT_REL( len, ==, 0 );
195         // >>> STATE: CLOSE WAIT
196         
197         // Expect FIN
198         TEST_ASSERT_rx();
199         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_FIN) );
200         TEST_ASSERT_REL( len, ==, 0 );
201         
202         // >>> STATE: LAST-ACK
203
204         // 2.7 Send ACK of FIN
205         TCP_SendC(&testconn, TCP_ACK, 0, NULL);
206         // Expect no response
207         TEST_ASSERT_no_rx();
208         
209         // >>> STATE: CLOSED
210         
211         return true;
212 }
213
214 bool Test_TCP_int_OpenConnection(tTCPConn *Conn)
215 {
216         RX_HEADER;
217         // >> SYN
218         TCP_SendC(Conn, TCP_SYN, 0, NULL);
219         Conn->LSeq ++;
220         TEST_ASSERT_rx();
221         // << SYN|ACK (save remote sequence number)
222         TCP_SkipCheck_Seq(true);
223         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, Conn, TCP_SYN|TCP_ACK) );
224         TEST_ASSERT_REL(len, ==, 0);
225         Conn->RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, Conn->AF) + 1;
226         // >> ACK
227         TCP_SendC(Conn, TCP_ACK, 0, NULL);
228         TEST_ASSERT_no_rx();
229         return true;
230 }
231
232 #if 0
233 bool Test_TCP_SYN_RECEIVED(void)
234 {
235         TEST_HEADER;
236         
237         // 1. Get into SYN-RECEIVED
238         TCP_SendC(&testconn, TCP_SYN, 0, NULL);
239         
240         // 2. Send various non-ACK packets
241         return false;
242 }
243 #endif
244
245 bool Test_TCP_Reset(void)
246 {
247         TEST_HEADER;
248         
249         tTCPConn        testconn = {
250                 .IFNum = 0,
251                 .AF = 4,
252                 .LAddr = BLOB(HOST_IP),
253                 .RAddr = BLOB(TEST_IP),
254                 .LPort = 44359,
255                 .RPort = 9,
256                 .LSeq = 0x600,
257                 .RSeq = 0x600,
258                 .Window = 128
259         };
260
261         Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
262
263         // 1. Response in listen-based SYN-RECEIVED
264         // >> SYN
265         TCP_SendC(&testconn, TCP_SYN, 0, NULL);
266         testconn.LSeq ++;
267         // << SYN|ACK :: Now in SYN-RECEIVED
268         TEST_ASSERT_rx();
269         TCP_SkipCheck_Seq(true);
270         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
271         TEST_ASSERT_REL(len, ==, 0);
272         testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
273         // >> RST (not ACK)
274         TCP_SendC(&testconn, TCP_RST, 0, NULL);
275         // << nothing (connection should now be dead)
276         TEST_ASSERT_no_rx();
277         // >> ACK (this should be to a closed conneciton, see LISTEN[ACK] above)
278         TCP_SendC(&testconn, TCP_ACK, 0, NULL);
279         // << RST
280         TEST_ASSERT_rx();
281         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
282         TEST_ASSERT_REL(len, ==, 0);
283         
284         // 2. Response in open-based SYN-RECEIVED? (What is that?)
285         TEST_WARN("TODO: RFC793 pg70 mentions OPEN-based SYN-RECEIVED");
286         
287         testconn.LPort += 1234;
288         // ESTABLISHED[RST] - RFC793:Pg70
289         // 2. Response in ESTABLISHED 
290         TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
291         // >> RST
292         TCP_SendC(&testconn, TCP_RST, 0, NULL);
293         // << no response, connection closed
294         TEST_ASSERT_no_rx();
295         // >> ACK (LISTEN[ACK])
296         TCP_SendC(&testconn, TCP_ACK, 0, NULL);
297         // << RST
298         TEST_ASSERT_rx();
299         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
300         TEST_ASSERT_REL(len, ==, 0);
301         
302         return true;
303 }
304
305 bool Test_TCP_WindowSizes(void)
306 {
307         TEST_HEADER;
308         tTCPConn        testconn = {
309                 .IFNum = 0,
310                 .AF = 4,
311                 .LAddr = BLOB(HOST_IP),
312                 .RAddr = BLOB(TEST_IP),
313                 .LPort = 44359,
314                 .RPort = 9,
315                 .LSeq = 0x600,
316                 .RSeq = 0x600,
317                 .Window = 128
318         };
319         char    testdata[152];
320         memset(testdata, 0xAB, sizeof(testdata));
321         
322         Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
323         // > Open Connection
324         TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
325         
326         // 1. Test remote respecting our transmission window (>=1 byte)
327         // > Send more data than our RX window
328         TCP_SendC(&testconn, TCP_PSH, sizeof(testdata), testdata);
329         testconn.LSeq += sizeof(testdata);
330         // Expect our RX window back
331         TEST_ASSERT_rx();
332         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
333         TEST_ASSERT_REL(len, ==, testconn.Window);
334         testconn.RSeq += len;
335         // > Send ACK and reduce window to 1 byte
336         testconn.Window = 1;
337         TCP_SendC(&testconn, TCP_ACK, 0, NULL);
338         testconn.LSeq += sizeof(testdata);
339         // > Expect 1 byte back
340         TEST_ASSERT_rx();
341         TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
342         TEST_ASSERT_REL(len, ==, 1);
343         testconn.RSeq += len;
344         // 2. Test remote handling our window being 0 (should only send ACKs)
345         // 3. Test that remote drops data outside of its RX window
346         // 3.1. Ensure that data that wraps the end of the RX window is handled correctly
347         return false;
348 }

UCC git Repository :: git.ucc.asn.au