2 * Acess2 Network Stack Tester
3 * - By John Hodge (thePowersGang)
6 * - Tests for the behavior of the "Transmission Control Protocol"
18 static const int ERX_TIMEOUT = 1000; // Expect RX timeout (timeout=failure)
19 static const int NRX_TIMEOUT = 250; // Not expect RX timeout (timeout=success)
20 static const int RETX_TIMEOUT = 1000; // OS PARAM - Retransmit timeout
21 static const int LOST_TIMEOUT = 1000; // OS PARAM - Time before sending an ACK
22 static const int DACK_TIMEOUT = 500; // OS PARAM - Timeout for delayed ACKs
23 static const size_t DACK_BYTES = 4096; // OS PARAM - Threshold for delayed ACKs
25 bool Test_TCP_Basic(void)
31 .RAddr = BLOB(TEST_IP),
32 .LAddr = BLOB(HOST_IP),
40 const char testblob[] = "HelloWorld, this is some random testing data for TCP\xFF\x00\x66\x12\x12.";
41 const size_t testblob_len = sizeof(testblob);
43 // TODO: Check checksum failures
45 // 1. Test packets to closed port
48 // 1.1. Send SYN packet
49 TEST_STEP("1.1. Send SYN packet to CLOSED");
50 TCP_SendC(&testconn, TCP_SYN, testblob_len, testblob);
52 testconn.LSeq += testblob_len;
53 // Expect RST,ACK with SEQ=0
55 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST|TCP_ACK) );
56 TEST_ASSERT_REL(ofs, ==, rxlen);
58 // 1.2. Send a SYN,ACK packet
59 TEST_STEP("1.2. Send SYN,ACK packet to CLOSED");
60 testconn.RSeq = 12345;
61 TCP_SendC(&testconn, TCP_SYN|TCP_ACK, 0, NULL);
62 // Expect a TCP_RST with SEQ=ACK
64 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
65 TEST_ASSERT_REL(ofs, ==, rxlen);
68 // 1.3. Send a RST packet
69 TEST_STEP("1.2. Send RST packet to CLOSED");
70 TCP_SendC(&testconn, TCP_RST, 0, NULL);
75 // 1.3. Send a RST,ACK packet
76 TCP_SendC(&testconn, TCP_RST|TCP_ACK, 0, NULL);
82 // 2. Establishing connection with a server
84 testconn.LPort = 11239;
85 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
90 TCP_SendC(&testconn, TCP_RST, 0, NULL);
94 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
97 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
98 TEST_ASSERT_REL(ofs, ==, rxlen);
100 // 2.3. Begin hanshake (SYN)
101 // TODO: Test "If the SYN bit is set, check the security."
102 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
104 // - Expect SYN,ACK with ACK == SEQ+1
106 TCP_SkipCheck_Seq(true);
107 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
108 testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
110 // >>> STATE: SYN-RECEIVED
111 // TODO: Test other transitions from SYN-RECEIVED
113 // 2.4. Complete handshake, TCP ACK
114 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
118 // >>> STATE: ESTABLISHED
121 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
122 testconn.LSeq += testblob_len;
124 // Expect echoed reponse with ACK
126 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
127 TEST_ASSERT_REL( len, ==, testblob_len );
128 TEST_ASSERT( memcmp(rxbuf + ofs, testblob, testblob_len) == 0 );
129 testconn.RSeq += testblob_len;
131 // Send something short
132 const char testblob2[] = "test blob two.";
133 const size_t testblob2_len = sizeof(testblob2);
134 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob2_len, testblob2);
135 testconn.LSeq += testblob2_len;
136 // Expect response with data and ACK
138 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
139 TEST_ASSERT_REL( len, ==, testblob2_len );
140 TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
142 // Wait for just under retransmit time, expecting nothing
144 TEST_ASSERT( 0 == Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) );
145 // Now expect the previous message
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 TEST_WARN("Not testing retransmit timer");
153 testconn.RSeq += testblob2_len;
155 // Send explicit acknowledgement
156 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
159 // TODO: Test delayed ACKs (Timeout and data)
160 // > Requires inhibiting the server's echo response?
162 // === Test out-of-order packets ===
163 testconn.LSeq += testblob2_len; // raise sequence number
164 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
165 // - previous data has not been sent, expect no response for ()
166 // TODO: Should this ACK be delayed?
167 //TEST_ASSERT_no_rx();
168 // - Expect an ACK of the highest received packet
169 testconn.LSeq -= testblob2_len;
171 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
172 TEST_ASSERT_REL( len, ==, 0 );
173 // - Send missing data
174 TCP_SendC(&testconn, TCP_ACK, testblob2_len, testblob2);
175 testconn.LSeq += testblob_len+testblob2_len; // raise sequence number
176 // - Expect echo response with all sent data
178 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
179 TEST_ASSERT_REL( len, ==, testblob_len+testblob2_len );
180 TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
181 TEST_ASSERT( memcmp(rxbuf + ofs+testblob2_len, testblob, testblob_len) == 0 );
182 testconn.RSeq += len;
184 // 2.6. Close connection (TCP FIN)
185 TCP_SendC(&testconn, TCP_ACK|TCP_FIN, 0, NULL);
186 testconn.LSeq ++; // Empty = 1 byte
187 // Expect ACK? (Does Acess do delayed ACKs here?)
189 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
190 TEST_ASSERT_REL( len, ==, 0 );
191 // >>> STATE: CLOSE WAIT
195 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_FIN) );
196 TEST_ASSERT_REL( len, ==, 0 );
198 // >>> STATE: LAST-ACK
200 // 2.7 Send ACK of FIN
201 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
202 // Expect no response
210 bool Test_TCP_int_OpenConnection(tTCPConn *Conn)
214 TCP_SendC(Conn, TCP_SYN, 0, NULL);
217 // << SYN|ACK (save remote sequence number)
218 TCP_SkipCheck_Seq(true);
219 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, Conn, TCP_SYN|TCP_ACK) );
220 TEST_ASSERT_REL(len, ==, 0);
221 Conn->RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, Conn->AF) + 1;
223 TCP_SendC(Conn, TCP_ACK, 0, NULL);
229 bool Test_TCP_SYN_RECEIVED(void)
233 // 1. Get into SYN-RECEIVED
234 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
236 // 2. Send various non-ACK packets
241 bool Test_TCP_Reset(void)
245 tTCPConn testconn = {
248 .LAddr = BLOB(HOST_IP),
249 .RAddr = BLOB(TEST_IP),
257 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
259 // 1. Response in listen-based SYN-RECEIVED
261 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
263 // << SYN|ACK :: Now in SYN-RECEIVED
265 TCP_SkipCheck_Seq(true);
266 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
267 TEST_ASSERT_REL(len, ==, 0);
268 testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
270 TCP_SendC(&testconn, TCP_RST, 0, NULL);
271 // << nothing (connection should now be dead)
273 // >> ACK (this should be to a closed conneciton, see LISTEN[ACK] above)
274 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
277 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
278 TEST_ASSERT_REL(len, ==, 0);
280 // 2. Response in open-based SYN-RECEIVED? (What is that?)
281 TEST_WARN("TODO: RFC793 pg70 mentions OPEN-based SYN-RECEIVED");
283 testconn.LPort += 1234;
284 // ESTABLISHED[RST] - RFC793:Pg70
285 // 2. Response in ESTABLISHED
286 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
288 TCP_SendC(&testconn, TCP_RST, 0, NULL);
289 // << no response, connection closed
291 // >> ACK (LISTEN[ACK])
292 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
295 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
296 TEST_ASSERT_REL(len, ==, 0);
301 bool Test_TCP_WindowSizes(void)
304 tTCPConn testconn = {
307 .LAddr = BLOB(HOST_IP),
308 .RAddr = BLOB(TEST_IP),
316 memset(testdata, 0xAB, sizeof(testdata));
318 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
320 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
322 // 1. Test remote respecting our transmission window (>=1 byte)
323 // > Send more data than our RX window
324 TCP_SendC(&testconn, TCP_PSH, sizeof(testdata), testdata);
325 testconn.LSeq += sizeof(testdata);
326 // Expect our RX window back
328 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
329 TEST_ASSERT_REL(len, ==, testconn.Window);
330 testconn.RSeq += len;
331 // > Send ACK and reduce window to 1 byte
333 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
334 testconn.LSeq += sizeof(testdata);
335 // > Expect 1 byte back
337 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
338 TEST_ASSERT_REL(len, ==, 1);
339 testconn.RSeq += len;
340 // 2. Test remote handling our window being 0 (should only send ACKs)
341 // 3. Test that remote drops data outside of its RX window
342 // 3.1. Ensure that data that wraps the end of the RX window is handled correctly