*
* test_tcp.c
* - Tests for the behavior of the "Transmission Control Protocol"
+ * - These tests are written off RFC793
*/
#include "test.h"
#include "tests.h"
#include "tcp.h"
#include <string.h>
-#define TEST_ASSERT_rx() TEST_ASSERT( rxlen = Net_Receive(0, sizeof(rxbuf), rxbuf, ERX_TIMEOUT) )
-#define TEST_ASSERT_no_rx() TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 )
-const int ERX_TIMEOUT = 1000; // Expect RX timeout (timeout=failure)
-const int NRX_TIMEOUT = 250; // Not expect RX timeout (timeout=success)
-const int RETX_TIMEOUT = 1000; // OS PARAM - Retransmit timeout
-const int LOST_TIMEOUT = 1000; // OS PARAM - Time before sending an ACK
-const int DACK_TIMEOUT = 500; // OS PARAM - Timeout for delayed ACKs
-const size_t DACK_BYTES = 4096; // OS PARAM - Threshold for delayed ACKs
+#define TEST_TIMERS 0
+
+static const int ERX_TIMEOUT = 1000; // Expect RX timeout (timeout=failure)
+static const int NRX_TIMEOUT = 250; // Not expect RX timeout (timeout=success)
+static const int RETX_TIMEOUT = 1000; // OS PARAM - Retransmit timeout
+static const int LOST_TIMEOUT = 1000; // OS PARAM - Time before sending an ACK
+static const int DACK_TIMEOUT = 500; // OS PARAM - Timeout for delayed ACKs
+static const size_t DACK_BYTES = 4096; // OS PARAM - Threshold for delayed ACKs
bool Test_TCP_Basic(void)
{
- TEST_SETNAME(__func__);
- size_t rxlen, ofs, len;
- char rxbuf[MTU];
+ TEST_HEADER;
tTCPConn testconn = {
.IFNum = 0, .AF = 4,
// > RFC793 Pg.65
// 1.1. Send SYN packet
+ TEST_STEP("1.1. Send SYN packet to CLOSED");
TCP_SendC(&testconn, TCP_SYN, testblob_len, testblob);
testconn.RSeq = 0;
testconn.LSeq += testblob_len;
TEST_ASSERT_REL(ofs, ==, rxlen);
// 1.2. Send a SYN,ACK packet
+ TEST_STEP("1.2. Send SYN,ACK packet to CLOSED");
testconn.RSeq = 12345;
TCP_SendC(&testconn, TCP_SYN|TCP_ACK, 0, NULL);
// Expect a TCP_RST with SEQ=ACK
testconn.LSeq ++;
// 1.3. Send a RST packet
+ TEST_STEP("1.2. Send RST packet to CLOSED");
TCP_SendC(&testconn, TCP_RST, 0, NULL);
// Expect nothing
TEST_ASSERT_no_rx();
// 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?)
+ // 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 );
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_SETNAME(__func__);
+ 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_WindowSizes(void)
+bool Test_TCP_Reset(void)
{
- TEST_SETNAME(__func__);
- size_t rxlen, ofs, len;
- char rxbuf[MTU];
+ TEST_HEADER;
+
tTCPConn testconn = {
.IFNum = 0,
.AF = 4,
.RSeq = 0x600,
.Window = 128
};
- char testdata[152];
- memset(testdata, 0xAB, sizeof(testdata));
-
+
Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
- // > Open Connection
+
+ // 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);
+
+ return true;
+}
+
+bool Test_TCP_WindowSizes(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
+ };
+ char testdata[152];
+ memset(testdata, 0xAB, sizeof(testdata));
+
+ Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
+ // > Open Connection
+ TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
// 1. Test remote respecting our transmission window (>=1 byte)
// > Send more data than our RX window
TEST_ASSERT_REL(len, ==, 1);
testconn.RSeq += len;
// 2. Test remote handling our window being 0 (should only send ACKs)
+ // TODO:
// 3. Test that remote drops data outside of its RX window
// 3.1. Ensure that data that wraps the end of the RX window is handled correctly
+ // TODO:
+ return false;
+}
+
+// RFC793 pg41
+bool Test_TCP_Retransmit(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
+ };
+ char testdata[128];
+ memset(testdata, 0xAB, sizeof(testdata));
+
+ TEST_STEP("1. Open and connect to TCP server");
+ Stack_SendCommand("tcp_echo_server %i", testconn.RPort);
+ TEST_ASSERT( Test_TCP_int_OpenConnection(&testconn) );
+
+
+ TEST_STEP("2. Send data and expect it to be echoed");
+ TCP_SendC(&testconn, TCP_PSH, sizeof(testdata), testdata);
+ testconn.LSeq += sizeof(testdata);
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_CheckC(rxlen, rxbuf, &ofs, &len, &testconn, TCP_ACK|TCP_PSH) );
+
+ TEST_STEP("3. Expect nothing for TCP_RTX_TIMEOUT_1");
+ TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, RETX_TIMEOUT-100) == 0 );
+
+ TEST_STEP("4. Expect a retransmit");
+ TEST_ASSERT_rx();
+
return false;
}