Tools/NetTest - Fixed ip checksum, pcap packet trace, cleanup
[tpg/acess2.git] / Tools / NetTest_Runner / test_tcp.c
1 /*
2  * Acess2 Network Stack Tester
3  * - By John Hodge (thePowersGang)
4  *
5  * test_tcp.c
6  * - Tests for the behavior of the "Transmission Control Protocol"
7  */
8 #include "test.h"
9 #include "tests.h"
10 #include "net.h"
11 #include "stack.h"
12 #include "arp.h"
13 #include "tcp.h"
14
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 )
17
18 bool Test_TCP_Basic(void)
19 {
20         TEST_SETNAME(__func__);
21         size_t  rxlen, ofs;
22         char rxbuf[MTU];
23         const int       ERX_TIMEOUT = 1000;     // Expect RX timeout (timeout=failure)
24         const int       NRX_TIMEOUT = 250;      // Not expect RX timeout (timeout=success)
25         
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);
28         
29         // 1. Test packets to closed port
30         // > RFC793 Pg.65
31         const uint16_t  our_window = 0x1000;
32         uint32_t        seq_tx = 0x1000;
33         uint32_t        seq_exp = 0x33456;
34         
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
38         TEST_ASSERT_rx();
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);
42         
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
46         TEST_ASSERT_rx();
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);
49         
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);
52         // Expect nothing
53         TEST_ASSERT_no_rx();
54         
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);
57         // Expect nothing
58         TEST_ASSERT_no_rx();
59
60         
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);
65
66         // >>> STATE: LISTEN
67
68         // 2.1. Send RST
69         TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_RST, our_window, 0, NULL);
70         // - Expect nothing
71         TEST_ASSERT_no_rx();
72         // 2.2. Send ACK
73         TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_ACK, our_window, 0, NULL);
74         // - Expect RST
75         TEST_ASSERT_rx();
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) );
78
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
83         TEST_ASSERT_rx();
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);
88
89         // >>> STATE: SYN-RECEIVED
90         // TODO: Test other transitions from SYN-RECEIVED
91                 
92         // 2.4. Complete handshake, TCP ACK
93         seq_exp ++;
94         seq_tx ++;
95         TCP_Send(0,4,BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_ACK, our_window, 0, NULL);
96         // - Expect nothing
97         TEST_ASSERT_no_rx();
98
99         // >>> STATE: ESTABLISHED
100         
101         // 2.5. Send data
102         
103         return true;
104 }
105
106 bool Test_TCP_SYN_RECEIVED(void)
107 {
108         // 1. Get into SYN-RECEIVED
109         
110         // 2. Send various non-ACK packets
111         return false;
112 }

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