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
19 static const int ERX_TIMEOUT = 1000; // Expect RX timeout (timeout=failure)
20 static const int NRX_TIMEOUT = 250; // Not expect RX timeout (timeout=success)
21 static const int RETX_TIMEOUT = 1000; // OS PARAM - Retransmit timeout
22 static const int LOST_TIMEOUT = 1000; // OS PARAM - Time before sending an ACK
23 static const int DACK_TIMEOUT = 500; // OS PARAM - Timeout for delayed ACKs
24 static const size_t DACK_BYTES = 4096; // OS PARAM - Threshold for delayed ACKs
26 bool Test_TCP_Basic(void)
32 .RAddr = BLOB(TEST_IP),
33 .LAddr = BLOB(HOST_IP),
41 const char testblob[] = "HelloWorld, this is some random testing data for TCP\xFF\x00\x66\x12\x12.";
42 const size_t testblob_len = sizeof(testblob);
44 // TODO: Check checksum failures
46 // 1. Test packets to closed port
49 // 1.1. Send SYN packet
50 TEST_STEP("1.1. Send SYN packet to CLOSED");
51 TCP_SendC(&testconn, TCP_SYN, testblob_len, testblob);
53 testconn.LSeq += testblob_len;
54 // Expect RST,ACK with SEQ=0
56 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST|TCP_ACK) );
57 TEST_ASSERT_REL(ofs, ==, rxlen);
59 // 1.2. Send a SYN,ACK packet
60 TEST_STEP("1.2. Send SYN,ACK packet to CLOSED");
61 testconn.RSeq = 12345;
62 TCP_SendC(&testconn, TCP_SYN|TCP_ACK, 0, NULL);
63 // Expect a TCP_RST with SEQ=ACK
65 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
66 TEST_ASSERT_REL(ofs, ==, rxlen);
69 // 1.3. Send a RST packet
70 TEST_STEP("1.2. Send RST packet to CLOSED");
71 TCP_SendC(&testconn, TCP_RST, 0, NULL);
76 // 1.3. Send a RST,ACK packet
77 TCP_SendC(&testconn, TCP_RST|TCP_ACK, 0, NULL);
83 // 2. Establishing connection with a server
85 testconn.LPort = 11239;
86 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
91 TCP_SendC(&testconn, TCP_RST, 0, NULL);
95 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
98 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
99 TEST_ASSERT_REL(ofs, ==, rxlen);
101 // 2.3. Begin hanshake (SYN)
102 // TODO: Test "If the SYN bit is set, check the security."
103 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
105 // - Expect SYN,ACK with ACK == SEQ+1
107 TCP_SkipCheck_Seq(true);
108 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
109 testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
111 // >>> STATE: SYN-RECEIVED
112 // TODO: Test other transitions from SYN-RECEIVED
114 // 2.4. Complete handshake, TCP ACK
115 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
119 // >>> STATE: ESTABLISHED
122 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
123 testconn.LSeq += testblob_len;
125 // Expect echoed reponse with ACK
127 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
128 TEST_ASSERT_REL( len, ==, testblob_len );
129 TEST_ASSERT( memcmp(rxbuf + ofs, testblob, testblob_len) == 0 );
130 testconn.RSeq += testblob_len;
132 // Send something short
133 const char testblob2[] = "test blob two.";
134 const size_t testblob2_len = sizeof(testblob2);
135 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob2_len, testblob2);
136 testconn.LSeq += testblob2_len;
137 // Expect response with data and ACK
139 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
140 TEST_ASSERT_REL( len, ==, testblob2_len );
141 TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
143 // Wait for just under retransmit time, expecting nothing
145 TEST_ASSERT( 0 == Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) );
146 // Now expect the previous message
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 TEST_WARN("Not testing retransmit timer");
154 testconn.RSeq += testblob2_len;
156 // Send explicit acknowledgement
157 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
160 // TODO: Test delayed ACKs (Timeout and data)
161 // > Requires inhibiting the server's echo response?
163 // === Test out-of-order packets ===
164 testconn.LSeq += testblob2_len; // raise sequence number
165 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
166 // - previous data has not been sent, expect no response for ()
167 // TODO: Should this ACK be delayed?
168 //TEST_ASSERT_no_rx();
169 // - Expect an ACK of the highest received packet
170 testconn.LSeq -= testblob2_len;
172 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
173 TEST_ASSERT_REL( len, ==, 0 );
174 // - Send missing data
175 TCP_SendC(&testconn, TCP_ACK, testblob2_len, testblob2);
176 testconn.LSeq += testblob_len+testblob2_len; // raise sequence number
177 // - Expect echo response with all sent data
179 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
180 TEST_ASSERT_REL( len, ==, testblob_len+testblob2_len );
181 TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
182 TEST_ASSERT( memcmp(rxbuf + ofs+testblob2_len, testblob, testblob_len) == 0 );
183 testconn.RSeq += len;
185 // 2.6. Close connection (TCP FIN)
186 TCP_SendC(&testconn, TCP_ACK|TCP_FIN, 0, NULL);
187 testconn.LSeq ++; // Empty = 1 byte
188 // Expect ACK? (Does Acess do delayed ACKs here?)
190 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
191 TEST_ASSERT_REL( len, ==, 0 );
192 // >>> STATE: CLOSE WAIT
196 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_FIN) );
197 TEST_ASSERT_REL( len, ==, 0 );
199 // >>> STATE: LAST-ACK
201 // 2.7 Send ACK of FIN
202 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
203 // Expect no response
211 bool Test_TCP_int_OpenConnection(tTCPConn *Conn)
215 TCP_SendC(Conn, TCP_SYN, 0, NULL);
218 // << SYN|ACK (save remote sequence number)
219 TCP_SkipCheck_Seq(true);
220 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, Conn, TCP_SYN|TCP_ACK) );
221 TEST_ASSERT_REL(len, ==, 0);
222 Conn->RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, Conn->AF) + 1;
224 TCP_SendC(Conn, TCP_ACK, 0, NULL);
230 bool Test_TCP_SYN_RECEIVED(void)
234 // 1. Get into SYN-RECEIVED
235 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
237 // 2. Send various non-ACK packets
242 bool Test_TCP_Reset(void)
246 tTCPConn testconn = {
249 .LAddr = BLOB(HOST_IP),
250 .RAddr = BLOB(TEST_IP),
258 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
260 // 1. Response in listen-based SYN-RECEIVED
262 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
264 // << SYN|ACK :: Now in SYN-RECEIVED
266 TCP_SkipCheck_Seq(true);
267 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
268 TEST_ASSERT_REL(len, ==, 0);
269 testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
271 TCP_SendC(&testconn, TCP_RST, 0, NULL);
272 // << nothing (connection should now be dead)
274 // >> ACK (this should be to a closed conneciton, see LISTEN[ACK] above)
275 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
278 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
279 TEST_ASSERT_REL(len, ==, 0);
281 // 2. Response in open-based SYN-RECEIVED? (What is that?)
282 TEST_WARN("TODO: RFC793 pg70 mentions OPEN-based SYN-RECEIVED");
284 testconn.LPort += 1234;
285 // ESTABLISHED[RST] - RFC793:Pg70
286 // 2. Response in ESTABLISHED
287 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
289 TCP_SendC(&testconn, TCP_RST, 0, NULL);
290 // << no response, connection closed
292 // >> ACK (LISTEN[ACK])
293 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
296 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
297 TEST_ASSERT_REL(len, ==, 0);
302 bool Test_TCP_WindowSizes(void)
305 tTCPConn testconn = {
308 .LAddr = BLOB(HOST_IP),
309 .RAddr = BLOB(TEST_IP),
317 memset(testdata, 0xAB, sizeof(testdata));
319 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
321 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
323 // 1. Test remote respecting our transmission window (>=1 byte)
324 // > Send more data than our RX window
325 TCP_SendC(&testconn, TCP_PSH, sizeof(testdata), testdata);
326 testconn.LSeq += sizeof(testdata);
327 // Expect our RX window back
329 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
330 TEST_ASSERT_REL(len, ==, testconn.Window);
331 testconn.RSeq += len;
332 // > Send ACK and reduce window to 1 byte
334 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
335 testconn.LSeq += sizeof(testdata);
336 // > Expect 1 byte back
338 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
339 TEST_ASSERT_REL(len, ==, 1);
340 testconn.RSeq += len;
341 // 2. Test remote handling our window being 0 (should only send ACKs)
343 // 3. Test that remote drops data outside of its RX window
344 // 3.1. Ensure that data that wraps the end of the RX window is handled correctly
350 bool Test_TCP_Retransmit(void)
353 tTCPConn testconn = {
356 .LAddr = BLOB(HOST_IP),
357 .RAddr = BLOB(TEST_IP),
365 memset(testdata, 0xAB, sizeof(testdata));
367 TEST_STEP("1. Open and connect to TCP server");
368 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
369 TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
372 TEST_STEP("2. Send data and expect it to be echoed");
373 TCP_SendC(&testconn, TCP_PSH, sizeof(testdata), testdata);
374 testconn.LSeq += sizeof(testdata);
376 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
378 TEST_STEP("3. Expect nothing for TCP_RTX_TIMEOUT_1");
379 TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) == 0 );
381 TEST_STEP("4. Expect a retransmit");