2 * Acess2 Network Stack Tester
3 * - By John Hodge (thePowersGang)
6 * - Tests for the behavior of the "Transmission Control Protocol"
15 #define TEST_ASSERT_rx() TEST_ASSERT( rxlen = Net_Receive(0, sizeof(rxbuf), rxbuf, ERX_TIMEOUT) )
16 #define TEST_ASSERT_no_rx() TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 )
18 bool Test_TCP_Basic(void)
20 TEST_SETNAME(__func__);
23 const int ERX_TIMEOUT = 1000; // Expect RX timeout (timeout=failure)
24 const int NRX_TIMEOUT = 250; // Not expect RX timeout (timeout=success)
26 const char testblob[] = "HelloWorld, this is some random testing data for TCP\xFF\x00\x66\x12\x12";
27 const size_t testblob_len = sizeof(testblob);
29 // 1. Test packets to closed port
31 const uint16_t our_window = 0x1000;
32 uint32_t seq_tx = 0x1000;
33 uint32_t seq_exp = 0x33456;
35 // 1.1. Send SYN packet
36 TCP_Send(0, 4, BLOB(TEST_IP), 1234, 80, seq_tx, seq_exp, TCP_SYN, 0x1000, testblob_len, testblob);
37 // Expect a TCP_RST|TCP_ACK with SEQ=0,ACK=SEQ+LEN
39 TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP), 80, 1234,
40 0, seq_tx+testblob_len, TCP_RST|TCP_ACK) );
41 TEST_ASSERT_REL(ofs, ==, rxlen);
43 // 1.2. Send a SYN,ACK packet
44 TCP_Send(0, 4, BLOB(TEST_IP), 1234, 80, seq_tx, seq_exp, TCP_SYN|TCP_ACK, 0x1000, 0, NULL);
45 // Expect a TCP_RST with SEQ=ACK
47 TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP), 80, 1234, seq_exp, seq_tx+0, TCP_RST) );
48 TEST_ASSERT_REL(ofs, ==, rxlen);
50 // 1.3. Send a RST packet
51 TCP_Send(0, 4, BLOB(TEST_IP), 1234, 80, seq_tx, seq_exp, TCP_RST, 0x1000, 0, NULL);
55 // 1.3. Send a RST,ACK packet
56 TCP_Send(0, 4, BLOB(TEST_IP), 1234, 80, seq_tx, seq_exp, TCP_RST|TCP_ACK, 0x1000, 0, NULL);
61 // 2. Establishing connection with a server
62 const int server_port = 1024;
63 const int local_port = 11234;
64 Stack_SendCommand("tcp_echo_server %i", server_port);
69 TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_RST, our_window, 0, NULL);
73 TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_ACK, our_window, 0, NULL);
76 TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP),
77 server_port, local_port, seq_exp, seq_tx+0, TCP_RST) );
79 // 2.3. Begin hanshake (SYN)
80 // TODO: "If the SYN bit is set, check the security."
81 TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, 0, TCP_SYN, our_window, 0, NULL);
82 // - Expect SYN,ACK with ACK == SEQ+1
84 TCP_SkipCheck_Seq(true);
85 TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP),
86 server_port, local_port, 0, seq_tx+1, TCP_SYN|TCP_ACK) );
87 seq_exp = TCP_Pkt_GetSeq(rxlen, rxbuf, 4);
89 // >>> STATE: SYN-RECEIVED
90 // TODO: Test other transitions from SYN-RECEIVED
92 // 2.4. Complete handshake, TCP ACK
95 TCP_Send(0,4,BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_ACK, our_window, 0, NULL);
99 // >>> STATE: ESTABLISHED
106 bool Test_TCP_SYN_RECEIVED(void)
108 // 1. Get into SYN-RECEIVED
110 // 2. Send various non-ACK packets