+ TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
+ testconn.LSeq += testblob_len;
+
+ // Expect echoed reponse with ACK
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
+ TEST_ASSERT_REL( len, ==, testblob_len );
+ TEST_ASSERT( memcmp(rxbuf + ofs, testblob, testblob_len) == 0 );
+ testconn.RSeq += testblob_len;
+
+ // Send something short
+ const char testblob2[] = "test blob two.";
+ const size_t testblob2_len = sizeof(testblob2);
+ TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob2_len, testblob2);
+ testconn.LSeq += testblob2_len;
+ // Expect response with data and ACK
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
+ TEST_ASSERT_REL( len, ==, testblob2_len );
+ TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
+
+ // Wait for just under retransmit time, expecting nothing
+ #if TEST_TIMERS
+ TEST_ASSERT( 0 == Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) );
+ // Now expect the previous message
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
+ TEST_ASSERT_REL( len, ==, testblob2_len );
+ TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
+ #else
+ TEST_WARN("Not testing retransmit timer");
+ #endif
+ testconn.RSeq += testblob2_len;
+
+ // Send explicit acknowledgement
+ TCP_SendC(&testconn, TCP_ACK, 0, NULL);
+ TEST_ASSERT_no_rx();
+
+ // TODO: Test delayed ACKs (Timeout and data)
+ // > Requires inhibiting the server's echo response?
+
+ // === Test out-of-order packets ===
+ testconn.LSeq += testblob2_len; // raise sequence number
+ TCP_SendC(&testconn, TCP_ACK|TCP_PSH, testblob_len, testblob);
+ // - previous data has not been sent, expect no response for ()
+ // TODO: Should this ACK be delayed?
+ //TEST_ASSERT_no_rx();
+ // - Expect an ACK of the highest received packet
+ testconn.LSeq -= testblob2_len;
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
+ TEST_ASSERT_REL( len, ==, 0 );
+ // - Send missing data
+ TCP_SendC(&testconn, TCP_ACK, testblob2_len, testblob2);
+ testconn.LSeq += testblob_len+testblob2_len; // raise sequence number
+ // - Expect echo response with all sent data
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
+ TEST_ASSERT_REL( len, ==, testblob_len+testblob2_len );
+ TEST_ASSERT( memcmp(rxbuf + ofs, testblob2, testblob2_len) == 0 );
+ TEST_ASSERT( memcmp(rxbuf + ofs+testblob2_len, testblob, testblob_len) == 0 );
+ testconn.RSeq += len;
+
+ // 2.6. Close connection (TCP FIN)
+ TCP_SendC(&testconn, TCP_ACK|TCP_FIN, 0, NULL);
+ testconn.LSeq ++; // Empty = 1 byte
+ // Expect ACK? (Does acess do delayed ACKs here?)
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK) );
+ TEST_ASSERT_REL( len, ==, 0 );
+ // >>> STATE: CLOSE WAIT
+
+ // Expect FIN
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_FIN) );
+ TEST_ASSERT_REL( len, ==, 0 );
+
+ // >>> STATE: LAST-ACK
+
+ // 2.7 Send ACK of FIN
+ TCP_SendC(&testconn, TCP_ACK, 0, NULL);
+ // Expect no response
+ TEST_ASSERT_no_rx();
+
+ // >>> STATE: CLOSED
+
+ return true;
+}
+
+bool Test_TCP_int_OpenConnection(tTCPConn *Conn)
+{
+ RX_HEADER;
+ // >> SYN
+ TCP_SendC(Conn, TCP_SYN, 0, NULL);
+ Conn->LSeq ++;
+ TEST_ASSERT_rx();
+ // << SYN|ACK (save remote sequence number)
+ TCP_SkipCheck_Seq(true);
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, Conn, TCP_SYN|TCP_ACK) );
+ TEST_ASSERT_REL(len, ==, 0);
+ Conn->RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, Conn->AF) + 1;
+ // >> ACK
+ TCP_SendC(Conn, TCP_ACK, 0, NULL);
+ TEST_ASSERT_no_rx();
+ return true;
+}
+
+#if 0
+bool Test_TCP_SYN_RECEIVED(void)
+{
+ TEST_HEADER;
+
+ // 1. Get into SYN-RECEIVED
+ TCP_SendC(&testconn, TCP_SYN, 0, NULL);
+
+ // 2. Send various non-ACK packets
+ return false;
+}
+#endif
+
+bool Test_TCP_Reset(void)
+{
+ TEST_HEADER;
+
+ tTCPConn testconn = {
+ .IFNum = 0,
+ .AF = 4,
+ .LAddr = BLOB(HOST_IP),
+ .RAddr = BLOB(TEST_IP),
+ .LPort = 44359,
+ .RPort = 9,
+ .LSeq = 0x600,
+ .RSeq = 0x600,
+ .Window = 128
+ };
+
+ Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
+
+ // 1. Response in listen-based SYN-RECEIVED
+ // >> SYN
+ TCP_SendC(&testconn, TCP_SYN, 0, NULL);
+ testconn.LSeq ++;
+ // << SYN|ACK :: Now in SYN-RECEIVED
+ TEST_ASSERT_rx();
+ TCP_SkipCheck_Seq(true);
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_SYN|TCP_ACK) );
+ TEST_ASSERT_REL(len, ==, 0);
+ testconn.RSeq = TCP_Pkt_GetSeq(rxlen, rxbuf, testconn.AF) + 1;
+ // >> RST (not ACK)
+ TCP_SendC(&testconn, TCP_RST, 0, NULL);
+ // << nothing (connection should now be dead)
+ TEST_ASSERT_no_rx();
+ // >> ACK (this should be to a closed conneciton, see LISTEN[ACK] above)
+ TCP_SendC(&testconn, TCP_ACK, 0, NULL);
+ // << RST
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
+ TEST_ASSERT_REL(len, ==, 0);
+
+ // 2. Response in open-based SYN-RECEIVED? (What is that?)
+ TEST_WARN("TODO: RFC793 pg70 mentions OPEN-based SYN-RECEIVED");
+
+ testconn.LPort += 1234;
+ // ESTABLISHED[RST] - RFC793:Pg70
+ // 2. Response in ESTABLISHED
+ TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
+ // >> RST
+ TCP_SendC(&testconn, TCP_RST, 0, NULL);
+ // << no response, connection closed
+ TEST_ASSERT_no_rx();
+ // >> ACK (LISTEN[ACK])
+ TCP_SendC(&testconn, TCP_ACK, 0, NULL);
+ // << RST
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_RST) );
+ TEST_ASSERT_REL(len, ==, 0);