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

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