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

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