2 * Acess2 Network Stack Tester
3 * - By John Hodge (thePowersGang)
6 * - Tests for the behavior of the "Transmission Control Protocol"
17 size_t rxlen, ofs, len; \
20 TEST_SETNAME(__func__);\
23 #define TEST_ASSERT_rx() TEST_ASSERT( rxlen = Net_Receive(0, sizeof(rxbuf), rxbuf, ERX_TIMEOUT) )
24 #define TEST_ASSERT_no_rx() TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 )
25 const int ERX_TIMEOUT = 1000; // Expect RX timeout (timeout=failure)
26 const int NRX_TIMEOUT = 250; // Not expect RX timeout (timeout=success)
27 const int RETX_TIMEOUT = 1000; // OS PARAM - Retransmit timeout
28 const int LOST_TIMEOUT = 1000; // OS PARAM - Time before sending an ACK
29 const int DACK_TIMEOUT = 500; // OS PARAM - Timeout for delayed ACKs
30 const size_t DACK_BYTES = 4096; // OS PARAM - Threshold for delayed ACKs
32 bool Test_TCP_Basic(void)
38 .RAddr = BLOB(TEST_IP),
39 .LAddr = BLOB(HOST_IP),
47 const char testblob[] = "HelloWorld, this is some random testing data for TCP\xFF\x00\x66\x12\x12.";
48 const size_t testblob_len = sizeof(testblob);
50 // TODO: Check checksum failures
52 // 1. Test packets to closed port
55 // 1.1. Send SYN packet
56 TCP_SendC(&testconn, TCP_SYN, testblob_len, testblob);
58 testconn.LSeq += testblob_len;
59 // Expect RST,ACK with SEQ=0
61 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST|TCP_ACK) );
62 TEST_ASSERT_REL(ofs, ==, rxlen);
64 // 1.2. Send a SYN,ACK packet
65 testconn.RSeq = 12345;
66 TCP_SendC(&testconn, TCP_SYN|TCP_ACK, 0, NULL);
67 // Expect a TCP_RST with SEQ=ACK
69 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
70 TEST_ASSERT_REL(ofs, ==, rxlen);
73 // 1.3. Send a RST packet
74 TCP_SendC(&testconn, TCP_RST, 0, NULL);
79 // 1.3. Send a RST,ACK packet
80 TCP_SendC(&testconn, TCP_RST|TCP_ACK, 0, NULL);
86 // 2. Establishing connection with a server
88 testconn.LPort = 11239;
89 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
94 TCP_SendC(&testconn, TCP_RST, 0, NULL);
98 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
101 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
102 TEST_ASSERT_REL(ofs, ==, rxlen);
104 // 2.3. Begin hanshake (SYN)
105 // TODO: Test "If the SYN bit is set, check the security."
106 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
108 // - Expect SYN,ACK with ACK == SEQ+1
110 TCP_SkipCheck_Seq(true);
111 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
112 testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
114 // >>> STATE: SYN-RECEIVED
115 // TODO: Test other transitions from SYN-RECEIVED
117 // 2.4. Complete handshake, TCP ACK
118 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
122 // >>> STATE: ESTABLISHED
125 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
126 testconn.LSeq += testblob_len;
128 // Expect echoed reponse with ACK
130 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
131 TEST_ASSERT_REL( len, ==, testblob_len );
132 TEST_ASSERT( memcmp(rxbuf + ofs, testblob, testblob_len) == 0 );
133 testconn.RSeq += testblob_len;
135 // Send something short
136 const char testblob2[] = "test blob two.";
137 const size_t testblob2_len = sizeof(testblob2);
138 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob2_len, testblob2);
139 testconn.LSeq += testblob2_len;
140 // Expect response with data and ACK
142 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
143 TEST_ASSERT_REL( len, ==, testblob2_len );
144 TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
146 // Wait for just under retransmit time, expecting nothing
148 TEST_ASSERT( 0 == Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) );
149 // Now expect the previous message
151 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
152 TEST_ASSERT_REL( len, ==, testblob2_len );
153 TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
155 TEST_WARN("Not testing retransmit timer");
157 testconn.RSeq += testblob2_len;
159 // Send explicit acknowledgement
160 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
163 // TODO: Test delayed ACKs (Timeout and data)
164 // > Requires inhibiting the server's echo response?
166 // === Test out-of-order packets ===
167 testconn.LSeq += testblob2_len; // raise sequence number
168 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
169 // - previous data has not been sent, expect no response for ()
170 // TODO: Should this ACK be delayed?
171 //TEST_ASSERT_no_rx();
172 // - Expect an ACK of the highest received packet
173 testconn.LSeq -= testblob2_len;
175 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
176 TEST_ASSERT_REL( len, ==, 0 );
177 // - Send missing data
178 TCP_SendC(&testconn, TCP_ACK, testblob2_len, testblob2);
179 testconn.LSeq += testblob_len+testblob2_len; // raise sequence number
180 // - Expect echo response with all sent data
182 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
183 TEST_ASSERT_REL( len, ==, testblob_len+testblob2_len );
184 TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
185 TEST_ASSERT( memcmp(rxbuf + ofs+testblob2_len, testblob, testblob_len) == 0 );
186 testconn.RSeq += len;
188 // 2.6. Close connection (TCP FIN)
189 TCP_SendC(&testconn, TCP_ACK|TCP_FIN, 0, NULL);
190 testconn.LSeq ++; // Empty = 1 byte
191 // Expect ACK? (Does acess do delayed ACKs here?)
193 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
194 TEST_ASSERT_REL( len, ==, 0 );
195 // >>> STATE: CLOSE WAIT
199 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_FIN) );
200 TEST_ASSERT_REL( len, ==, 0 );
202 // >>> STATE: LAST-ACK
204 // 2.7 Send ACK of FIN
205 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
206 // Expect no response
214 bool Test_TCP_int_OpenConnection(tTCPConn *Conn)
218 TCP_SendC(Conn, TCP_SYN, 0, NULL);
221 // << SYN|ACK (save remote sequence number)
222 TCP_SkipCheck_Seq(true);
223 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, Conn, TCP_SYN|TCP_ACK) );
224 TEST_ASSERT_REL(len, ==, 0);
225 Conn->RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, Conn->AF) + 1;
227 TCP_SendC(Conn, TCP_ACK, 0, NULL);
233 bool Test_TCP_SYN_RECEIVED(void)
237 // 1. Get into SYN-RECEIVED
238 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
240 // 2. Send various non-ACK packets
245 bool Test_TCP_Reset(void)
249 tTCPConn testconn = {
252 .LAddr = BLOB(HOST_IP),
253 .RAddr = BLOB(TEST_IP),
261 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
263 // 1. Response in listen-based SYN-RECEIVED
265 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
267 // << SYN|ACK :: Now in SYN-RECEIVED
269 TCP_SkipCheck_Seq(true);
270 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
271 TEST_ASSERT_REL(len, ==, 0);
272 testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
274 TCP_SendC(&testconn, TCP_RST, 0, NULL);
275 // << nothing (connection should now be dead)
277 // >> ACK (this should be to a closed conneciton, see LISTEN[ACK] above)
278 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
281 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
282 TEST_ASSERT_REL(len, ==, 0);
284 // 2. Response in open-based SYN-RECEIVED? (What is that?)
285 TEST_WARN("TODO: RFC793 pg70 mentions OPEN-based SYN-RECEIVED");
287 testconn.LPort += 1234;
288 // ESTABLISHED[RST] - RFC793:Pg70
289 // 2. Response in ESTABLISHED
290 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
292 TCP_SendC(&testconn, TCP_RST, 0, NULL);
293 // << no response, connection closed
295 // >> ACK (LISTEN[ACK])
296 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
299 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
300 TEST_ASSERT_REL(len, ==, 0);
305 bool Test_TCP_WindowSizes(void)
308 tTCPConn testconn = {
311 .LAddr = BLOB(HOST_IP),
312 .RAddr = BLOB(TEST_IP),
320 memset(testdata, 0xAB, sizeof(testdata));
322 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
324 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
326 // 1. Test remote respecting our transmission window (>=1 byte)
327 // > Send more data than our RX window
328 TCP_SendC(&testconn, TCP_PSH, sizeof(testdata), testdata);
329 testconn.LSeq += sizeof(testdata);
330 // Expect our RX window back
332 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
333 TEST_ASSERT_REL(len, ==, testconn.Window);
334 testconn.RSeq += len;
335 // > Send ACK and reduce window to 1 byte
337 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
338 testconn.LSeq += sizeof(testdata);
339 // > Expect 1 byte back
341 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
342 TEST_ASSERT_REL(len, ==, 1);
343 testconn.RSeq += len;
344 // 2. Test remote handling our window being 0 (should only send ACKs)
345 // 3. Test that remote drops data outside of its RX window
346 // 3.1. Ensure that data that wraps the end of the RX window is handled correctly