2 * Acess2 Network Stack Tester
3 * - By John Hodge (thePowersGang)
6 * - Tests for the behavior of the "Transmission Control Protocol"
7 * - These tests are written off RFC793
20 size_t rxlen, ofs, len; \
23 TEST_SETNAME(__func__);\
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
35 bool Test_TCP_Basic(void)
41 .RAddr = BLOB(TEST_IP),
42 .LAddr = BLOB(HOST_IP),
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);
53 // TODO: Check checksum failures
55 // 1. Test packets to closed port
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);
62 testconn.LSeq += testblob_len;
63 // Expect RST,ACK with SEQ=0
65 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST|TCP_ACK) );
66 TEST_ASSERT_REL(ofs, ==, rxlen);
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
74 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
75 TEST_ASSERT_REL(ofs, ==, rxlen);
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);
85 // 1.3. Send a RST,ACK packet
86 TCP_SendC(&testconn, TCP_RST|TCP_ACK, 0, NULL);
92 // 2. Establishing connection with a server
94 testconn.LPort = 11239;
95 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
100 TCP_SendC(&testconn, TCP_RST, 0, NULL);
104 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
107 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
108 TEST_ASSERT_REL(ofs, ==, rxlen);
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);
114 // - Expect SYN,ACK with ACK == SEQ+1
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;
120 // >>> STATE: SYN-RECEIVED
121 // TODO: Test other transitions from SYN-RECEIVED
123 // 2.4. Complete handshake, TCP ACK
124 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
128 // >>> STATE: ESTABLISHED
131 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
132 testconn.LSeq += testblob_len;
134 // Expect echoed reponse with ACK
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;
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
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 );
152 // Wait for just under retransmit time, expecting nothing
154 TEST_ASSERT( 0 == Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) );
155 // Now expect the previous message
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 );
161 TEST_WARN("Not testing retransmit timer");
163 testconn.RSeq += testblob2_len;
165 // Send explicit acknowledgement
166 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
169 // TODO: Test delayed ACKs (Timeout and data)
170 // > Requires inhibiting the server's echo response?
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;
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
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;
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?)
199 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
200 TEST_ASSERT_REL( len, ==, 0 );
201 // >>> STATE: CLOSE WAIT
205 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_FIN) );
206 TEST_ASSERT_REL( len, ==, 0 );
208 // >>> STATE: LAST-ACK
210 // 2.7 Send ACK of FIN
211 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
212 // Expect no response
220 bool Test_TCP_int_OpenConnection(tTCPConn *Conn)
224 TCP_SendC(Conn, TCP_SYN, 0, NULL);
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;
233 TCP_SendC(Conn, TCP_ACK, 0, NULL);
239 bool Test_TCP_SYN_RECEIVED(void)
243 // 1. Get into SYN-RECEIVED
244 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
246 // 2. Send various non-ACK packets
251 bool Test_TCP_Reset(void)
255 tTCPConn testconn = {
258 .LAddr = BLOB(HOST_IP),
259 .RAddr = BLOB(TEST_IP),
267 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
269 // 1. Response in listen-based SYN-RECEIVED
271 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
273 // << SYN|ACK :: Now in SYN-RECEIVED
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;
280 TCP_SendC(&testconn, TCP_RST, 0, NULL);
281 // << nothing (connection should now be dead)
283 // >> ACK (this should be to a closed conneciton, see LISTEN[ACK] above)
284 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
287 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
288 TEST_ASSERT_REL(len, ==, 0);
290 // 2. Response in open-based SYN-RECEIVED? (What is that?)
291 TEST_WARN("TODO: RFC793 pg70 mentions OPEN-based SYN-RECEIVED");
293 testconn.LPort += 1234;
294 // ESTABLISHED[RST] - RFC793:Pg70
295 // 2. Response in ESTABLISHED
296 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
298 TCP_SendC(&testconn, TCP_RST, 0, NULL);
299 // << no response, connection closed
301 // >> ACK (LISTEN[ACK])
302 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
305 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
306 TEST_ASSERT_REL(len, ==, 0);
311 bool Test_TCP_WindowSizes(void)
314 tTCPConn testconn = {
317 .LAddr = BLOB(HOST_IP),
318 .RAddr = BLOB(TEST_IP),
326 memset(testdata, 0xAB, sizeof(testdata));
328 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
330 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
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
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
343 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
344 testconn.LSeq += sizeof(testdata);
345 // > Expect 1 byte back
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)
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
359 bool Test_TCP_Retransmit(void)
362 tTCPConn testconn = {
365 .LAddr = BLOB(HOST_IP),
366 .RAddr = BLOB(TEST_IP),
374 memset(testdata, 0xAB, sizeof(testdata));
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) );
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);
385 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
387 TEST_STEP("3. Expect nothing for TCP_RTX_TIMEOUT_1");
388 TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) == 0 )
390 TEST_STEP("4. Expect a retransmit");