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

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