2 * Acess2 Network Stack Tester
3 * - By John Hodge (thePowersGang)
6 * - Tests for the behavior of the "Transmission Control Protocol"
19 size_t rxlen, ofs, len; \
22 TEST_SETNAME(__func__);\
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
34 bool Test_TCP_Basic(void)
40 .RAddr = BLOB(TEST_IP),
41 .LAddr = BLOB(HOST_IP),
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);
52 // TODO: Check checksum failures
54 // 1. Test packets to closed port
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);
61 testconn.LSeq += testblob_len;
62 // Expect RST,ACK with SEQ=0
64 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST|TCP_ACK) );
65 TEST_ASSERT_REL(ofs, ==, rxlen);
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
73 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
74 TEST_ASSERT_REL(ofs, ==, rxlen);
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);
84 // 1.3. Send a RST,ACK packet
85 TCP_SendC(&testconn, TCP_RST|TCP_ACK, 0, NULL);
91 // 2. Establishing connection with a server
93 testconn.LPort = 11239;
94 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
99 TCP_SendC(&testconn, TCP_RST, 0, NULL);
103 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
106 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
107 TEST_ASSERT_REL(ofs, ==, rxlen);
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);
113 // - Expect SYN,ACK with ACK == SEQ+1
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;
119 // >>> STATE: SYN-RECEIVED
120 // TODO: Test other transitions from SYN-RECEIVED
122 // 2.4. Complete handshake, TCP ACK
123 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
127 // >>> STATE: ESTABLISHED
130 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
131 testconn.LSeq += testblob_len;
133 // Expect echoed reponse with ACK
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;
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
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 );
151 // Wait for just under retransmit time, expecting nothing
153 TEST_ASSERT( 0 == Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) );
154 // Now expect the previous message
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 );
160 TEST_WARN("Not testing retransmit timer");
162 testconn.RSeq += testblob2_len;
164 // Send explicit acknowledgement
165 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
168 // TODO: Test delayed ACKs (Timeout and data)
169 // > Requires inhibiting the server's echo response?
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;
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
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;
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?)
198 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
199 TEST_ASSERT_REL( len, ==, 0 );
200 // >>> STATE: CLOSE WAIT
204 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_FIN) );
205 TEST_ASSERT_REL( len, ==, 0 );
207 // >>> STATE: LAST-ACK
209 // 2.7 Send ACK of FIN
210 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
211 // Expect no response
219 bool Test_TCP_int_OpenConnection(tTCPConn *Conn)
223 TCP_SendC(Conn, TCP_SYN, 0, NULL);
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;
232 TCP_SendC(Conn, TCP_ACK, 0, NULL);
238 bool Test_TCP_SYN_RECEIVED(void)
242 // 1. Get into SYN-RECEIVED
243 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
245 // 2. Send various non-ACK packets
250 bool Test_TCP_Reset(void)
254 tTCPConn testconn = {
257 .LAddr = BLOB(HOST_IP),
258 .RAddr = BLOB(TEST_IP),
266 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
268 // 1. Response in listen-based SYN-RECEIVED
270 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
272 // << SYN|ACK :: Now in SYN-RECEIVED
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;
279 TCP_SendC(&testconn, TCP_RST, 0, NULL);
280 // << nothing (connection should now be dead)
282 // >> ACK (this should be to a closed conneciton, see LISTEN[ACK] above)
283 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
286 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
287 TEST_ASSERT_REL(len, ==, 0);
289 // 2. Response in open-based SYN-RECEIVED? (What is that?)
290 TEST_WARN("TODO: RFC793 pg70 mentions OPEN-based SYN-RECEIVED");
292 testconn.LPort += 1234;
293 // ESTABLISHED[RST] - RFC793:Pg70
294 // 2. Response in ESTABLISHED
295 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
297 TCP_SendC(&testconn, TCP_RST, 0, NULL);
298 // << no response, connection closed
300 // >> ACK (LISTEN[ACK])
301 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
304 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
305 TEST_ASSERT_REL(len, ==, 0);
310 bool Test_TCP_WindowSizes(void)
313 tTCPConn testconn = {
316 .LAddr = BLOB(HOST_IP),
317 .RAddr = BLOB(TEST_IP),
325 memset(testdata, 0xAB, sizeof(testdata));
327 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
329 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
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
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
342 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
343 testconn.LSeq += sizeof(testdata);
344 // > Expect 1 byte back
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