Tools/NetTest - TCP stack testing, going well
[tpg/acess2.git] / Tools / NetTest_Runner / test_tcp.c
1 /*
2  */
3 #include "test.h"
4 #include "tests.h"
5 #include "net.h"
6 #include "stack.h"
7 #include "arp.h"
8 #include "tcp.h"
9
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 )
12
13 bool Test_TCP_Basic(void)
14 {
15         TEST_SETNAME(__func__);
16         size_t  rxlen, ofs;
17         char rxbuf[MTU];
18         const int       ERX_TIMEOUT = 1000;     // Expect RX timeout (timeout=failure)
19         const int       NRX_TIMEOUT = 1000;     // Not expect RX timeout (timeout=success)
20         
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);
23         
24         // 1. Test packets to closed port
25         // > RFC793 Pg.65
26         const uint16_t  our_window = 0x1000;
27         uint32_t        seq_tx = 0x1000;
28         uint32_t        seq_exp = 0x33456;
29         
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);
37         
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);
44         
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);
47         // Expect nothing
48         TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 );
49         
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);
52         // Expect nothing
53         TEST_ASSERT_no_rx();
54
55         
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);
60
61         // >>> STATE: LISTEN
62
63         // 2.1. Send RST
64         TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_RST, our_window, 0, NULL);
65         // - Expect nothing
66         TEST_ASSERT_no_rx();
67         // 2.2. Send ACK
68         TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_ACK, our_window, 0, NULL);
69         // - Expect RST
70         TEST_ASSERT_rx();
71         TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP), 80, 1234, seq_exp, seq_tx+0, TCP_RST) );
72
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
77         TEST_ASSERT_rx();
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);
82
83         // >>> STATE: SYN-RECEIVED
84         // TODO: Test other transitions from SYN-RECEIVED
85                 
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);
88         // - Expect nothing
89         TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 );
90
91         // >>> STATE: ESTABLISHED
92         
93         // 2.5. Send data
94         
95         return true;
96 }

UCC git Repository :: git.ucc.asn.au