10 #define TEST_ASSERT_rx() TEST_ASSERT( rxlen = Net_Receive(0, sizeof(rxbuf), rxbuf, ERX_TIMEOUT) )
11 #define TEST_ASSERT_no_rx() TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 )
13 bool Test_TCP_Basic(void)
15 TEST_SETNAME(__func__);
18 const int ERX_TIMEOUT = 1000; // Expect RX timeout (timeout=failure)
19 const int NRX_TIMEOUT = 1000; // Not expect RX timeout (timeout=success)
21 const char testblob[] = "HelloWorld, this is some random testing data for TCP\xFF\x00\x66\x12\x12";
22 const size_t testblob_len = sizeof(testblob);
24 // 1. Test packets to closed port
26 const uint16_t our_window = 0x1000;
27 uint32_t seq_tx = 0x1000;
28 uint32_t seq_exp = 0x33456;
30 // 1.1. Send SYN packet
31 TCP_Send(0, 4, BLOB(TEST_IP), 1234, 80, seq_tx, seq_exp, TCP_SYN, 0x1000, testblob_len, testblob);
32 // Expect a TCP_RST|TCP_ACK with SEQ=0,ACK=SEQ+LEN
33 TEST_ASSERT( rxlen = Net_Receive(0, sizeof(rxbuf), rxbuf, ERX_TIMEOUT) );
34 TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP), 80, 1234,
35 0, seq_tx+testblob_len, TCP_RST|TCP_ACK) );
36 TEST_ASSERT_REL(ofs, ==, rxlen);
38 // 1.2. Send a SYN,ACK packet
39 TCP_Send(0, 4, BLOB(TEST_IP), 1234, 80, seq_tx, seq_exp, TCP_SYN|TCP_ACK, 0x1000, 0, NULL);
40 // Expect a TCP_RST with SEQ=ACK
41 TEST_ASSERT( rxlen = Net_Receive(0, sizeof(rxbuf), rxbuf, ERX_TIMEOUT) );
42 TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP), 80, 1234, seq_exp, seq_tx+0, TCP_RST) );
43 TEST_ASSERT_REL(ofs, ==, rxlen);
45 // 1.3. Send a RST packet
46 TCP_Send(0, 4, BLOB(TEST_IP), 1234, 80, seq_tx, seq_exp, TCP_RST, 0x1000, 0, NULL);
48 TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 );
50 // 1.3. Send a RST,ACK packet
51 TCP_Send(0, 4, BLOB(TEST_IP), 1234, 80, seq_tx, seq_exp, TCP_RST|TCP_ACK, 0x1000, 0, NULL);
56 // 2. Establishing connection with a server
57 const int server_port = 1024;
58 const int local_port = 11234;
59 Stack_SendCommand("tcp_echo_server %i", server_port);
64 TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_RST, our_window, 0, NULL);
68 TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_ACK, our_window, 0, NULL);
71 TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP), 80, 1234, seq_exp, seq_tx+0, TCP_RST) );
73 // 2.3. Begin hanshake (SYN)
74 // TODO: "If the SYN bit is set, check the security."
75 TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, 0, TCP_SYN, our_window, 0, NULL);
76 // - Expect SYN,ACK with ACK == SEQ+1
78 TCP_SkipCheck_Seq(true);
79 TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP), server_port, local_port,
80 0, seq_tx+1, TCP_SYN|TCP_ACK) );
81 seq_exp = TCP_Pkt_GetSeq(rxlen, rxbuf, 4);
83 // >>> STATE: SYN-RECEIVED
84 // TODO: Test other transitions from SYN-RECEIVED
86 // 2.4. Complete handshake, TCP ACK
87 TCP_Send(0,4,BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_ACK, our_window, 0, NULL);
89 TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 );
91 // >>> STATE: ESTABLISHED