2 * Acess2 Network Stack Tester
3 * - By John Hodge (thePowersGang)
6 * - Tests for the behavior of the "Transmission Control Protocol"
16 #define TEST_ASSERT_rx() TEST_ASSERT( rxlen = Net_Receive(0, sizeof(rxbuf), rxbuf, ERX_TIMEOUT) )
17 #define TEST_ASSERT_no_rx() TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 )
18 const int ERX_TIMEOUT = 1000; // Expect RX timeout (timeout=failure)
19 const int NRX_TIMEOUT = 250; // Not expect RX timeout (timeout=success)
20 const int RETX_TIMEOUT = 1000; // OS PARAM - Retransmit timeout
21 const int LOST_TIMEOUT = 1000; // OS PARAM - Time before sending an ACK
22 const int DACK_TIMEOUT = 500; // OS PARAM - Timeout for delayed ACKs
23 const size_t DACK_BYTES = 4096; // OS PARAM - Threshold for delayed ACKs
25 bool Test_TCP_Basic(void)
27 TEST_SETNAME(__func__);
28 size_t rxlen, ofs, len;
33 .RAddr = BLOB(TEST_IP),
34 .LAddr = BLOB(HOST_IP),
42 const char testblob[] = "HelloWorld, this is some random testing data for TCP\xFF\x00\x66\x12\x12.";
43 const size_t testblob_len = sizeof(testblob);
45 // TODO: Check checksum failures
47 // 1. Test packets to closed port
50 // 1.1. Send SYN packet
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 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 TCP_SendC(&testconn, TCP_RST, 0, NULL);
74 // 1.3. Send a RST,ACK packet
75 TCP_SendC(&testconn, TCP_RST|TCP_ACK, 0, NULL);
81 // 2. Establishing connection with a server
83 testconn.LPort = 11239;
84 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
89 TCP_SendC(&testconn, TCP_RST, 0, NULL);
93 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
96 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
97 TEST_ASSERT_REL(ofs, ==, rxlen);
99 // 2.3. Begin hanshake (SYN)
100 // TODO: Test "If the SYN bit is set, check the security."
101 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
103 // - Expect SYN,ACK with ACK == SEQ+1
105 TCP_SkipCheck_Seq(true);
106 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
107 testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
109 // >>> STATE: SYN-RECEIVED
110 // TODO: Test other transitions from SYN-RECEIVED
112 // 2.4. Complete handshake, TCP ACK
113 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
117 // >>> STATE: ESTABLISHED
120 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
121 testconn.LSeq += testblob_len;
123 // Expect echoed reponse with ACK
125 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
126 TEST_ASSERT_REL( len, ==, testblob_len );
127 TEST_ASSERT( memcmp(rxbuf + ofs, testblob, testblob_len) == 0 );
128 testconn.RSeq += testblob_len;
130 // Send something short
131 const char testblob2[] = "test blob two.";
132 const size_t testblob2_len = sizeof(testblob2);
133 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob2_len, testblob2);
134 testconn.LSeq += testblob2_len;
135 // Expect response with data and ACK
137 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
138 TEST_ASSERT_REL( len, ==, testblob2_len );
139 TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
141 // Wait for just under retransmit time, expecting nothing
143 TEST_ASSERT( 0 == Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) );
144 // Now expect the previous message
146 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
147 TEST_ASSERT_REL( len, ==, testblob2_len );
148 TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
150 TEST_WARN("Not testing retransmit timer");
152 testconn.RSeq += testblob2_len;
154 // Send explicit acknowledgement
155 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
158 // TODO: Test delayed ACKs (Timeout and data)
159 // > Requires inhibiting the server's echo response?
161 // === Test out-of-order packets ===
162 testconn.LSeq += testblob2_len; // raise sequence number
163 TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
164 // - previous data has not been sent, expect no response for ()
165 // TODO: Should this ACK be delayed?
166 //TEST_ASSERT_no_rx();
167 // - Expect an ACK of the highest received packet
168 testconn.LSeq -= testblob2_len;
170 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
171 TEST_ASSERT_REL( len, ==, 0 );
172 // - Send missing data
173 TCP_SendC(&testconn, TCP_ACK, testblob2_len, testblob2);
174 testconn.LSeq += testblob_len+testblob2_len; // raise sequence number
175 // - Expect echo response with all sent data
177 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
178 TEST_ASSERT_REL( len, ==, testblob_len+testblob2_len );
179 TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
180 TEST_ASSERT( memcmp(rxbuf + ofs+testblob2_len, testblob, testblob_len) == 0 );
181 testconn.RSeq += len;
183 // 2.6. Close connection (TCP FIN)
184 TCP_SendC(&testconn, TCP_ACK|TCP_FIN, 0, NULL);
185 testconn.LSeq ++; // Empty = 1 byte
186 // Expect ACK? (Does acess do delayed ACKs here?)
188 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
189 TEST_ASSERT_REL( len, ==, 0 );
190 // >>> STATE: CLOSE WAIT
194 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_FIN) );
195 TEST_ASSERT_REL( len, ==, 0 );
197 // >>> STATE: LAST-ACK
199 // 2.7 Send ACK of FIN
200 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
201 // Expect no response
209 bool Test_TCP_SYN_RECEIVED(void)
211 TEST_SETNAME(__func__);
212 // 1. Get into SYN-RECEIVED
214 // 2. Send various non-ACK packets
218 bool Test_TCP_WindowSizes(void)
220 TEST_SETNAME(__func__);
221 size_t rxlen, ofs, len;
223 tTCPConn testconn = {
226 .LAddr = BLOB(HOST_IP),
227 .RAddr = BLOB(TEST_IP),
235 memset(testdata, 0xAB, sizeof(testdata));
237 Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
239 TCP_SendC(&testconn, TCP_SYN, 0, NULL);
242 TCP_SkipCheck_Seq(true);
243 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
244 TEST_ASSERT_REL(len, ==, 0);
245 testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
246 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
249 // 1. Test remote respecting our transmission window (>=1 byte)
250 // > Send more data than our RX window
251 TCP_SendC(&testconn, TCP_PSH, sizeof(testdata), testdata);
252 testconn.LSeq += sizeof(testdata);
253 // Expect our RX window back
255 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
256 TEST_ASSERT_REL(len, ==, testconn.Window);
257 testconn.RSeq += len;
258 // > Send ACK and reduce window to 1 byte
260 TCP_SendC(&testconn, TCP_ACK, 0, NULL);
261 testconn.LSeq += sizeof(testdata);
262 // > Expect 1 byte back
264 TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
265 TEST_ASSERT_REL(len, ==, 1);
266 testconn.RSeq += len;
267 // 2. Test remote handling our window being 0 (should only send ACKs)
268 // 3. Test that remote drops data outside of its RX window
269 // 3.1. Ensure that data that wraps the end of the RX window is handled correctly