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

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