This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_marshal_test.go
101 lines (96 loc) · 2.71 KB
/
tcp_marshal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package swtch
import (
"testing"
"time"
"net"
"github.com/soypat/ether-swtch/grams"
"github.com/soypat/ether-swtch/hex"
"github.com/soypat/ether-swtch/lax"
)
func TestTCPStabilityMarshalUnmarshal(t *testing.T) {
var connRx, connTx TCPConn
var (
defaultMAC = net.HardwareAddr(hex.Decode([]byte(`de ad be ef fe ff`)))
defaultIP = net.IP{192, 168, 1, 5}
)
var tests = []struct {
name string
mac net.HardwareAddr
tcpSubframe grams.Frame
data []byte
}{
{
name: "TCP [SYN,ACK]",
mac: defaultMAC,
data: hex.Decode([]byte(`de ad be ef fe ff 28 d2 44 9a 2f f3 08 00 45 00
00 3c 2c da 40 00 40 06 8a 1c c0 a8 01 70 c0 a8
01 05 e6 28 00 50 3e ab 64 f7 00 00 00 00 a0 02
fa f0 bf 4c 00 00 02 04 05 b4 04 02 08 0a 08 a2
77 3f 00 00 00 00 01 03 03 07`)),
},
{
name: "TCP [ACK]",
mac: defaultMAC,
data: hex.Decode([]byte(`de ad be ef fe ff 28 d2 44 9a 2f f3 08 00 45 00
00 28 2c dd 40 00 40 06 8a 2d c0 a8 01 70 c0 a8
01 05 e6 28 00 50 3e ab 66 5c 00 00 0c f7 50 10
f8 64 83 e0 00 00`)),
},
{
name: "TCP [FIN,ACK]",
mac: defaultMAC,
data: hex.Decode([]byte(` de ad be ef fe ff 28 d2 44 9a 2f f3 08 00 45 00
00 28 2c de 40 00 40 06 8a 2c c0 a8 01 70 c0 a8
01 05 e6 28 00 50 3e ab 66 5c 00 00 0c f7 50 11
f8 64 83 e0 00 00`)),
},
}
for _, test := range tests {
name := test.name
rwconn := &readbacktest{
packet: packet{dataOnWire: test.data},
}
err := connTx.Init(rwconn, nil, time.Second, defaultMAC, defaultIP, 80)
if err != nil {
t.Fatal(err)
}
err = connTx.Decode()
if !lax.IsEOF(err) && err != nil {
t.Fatal(err) // cannot procede without unmarshalling contents
}
// Prevent modification of frame by skipping default tcpSetCtl routine.
connTx.start = etherCtl
err = connTx.SendResponse()
if err != nil {
t.Error(err)
}
// Data sent will be decoded.
var readbackconn = &readbacktest{
packet: packet{
dataOnWire: rwconn.sent(),
},
}
err = connRx.Init(readbackconn, nil, time.Second, defaultMAC, defaultIP, 80)
if err != nil {
t.Fatal(err)
}
err = connRx.Decode()
if !lax.IsEOF(err) && err != nil {
t.Fatal(name, err) // cannot procede without unmarshalling contents
}
// swap ACK and SEQ back.
localSeq := connRx.TCP.Ack()
set := connRx.TCP.Set()
set.Ack(connRx.TCP.Seq())
set.Seq(localSeq)
if errs := assertEqualEthernet(&connTx.Ethernet, &connRx.Ethernet); errs != nil {
t.Errorf("%s: Ethernet Tx!=Rx: %v", name, errs)
}
if errs := assertEqualIPv4(&connTx.IPv4, &connRx.IPv4); errs != nil {
t.Errorf("%s: IPv4 Tx!=Rx: %v", name, errs)
}
if errs := assertEqualTCP(&connTx.TCP, &connRx.TCP); errs != nil {
t.Errorf("%s: TCP Tx!=Rx: %v", name, errs)
}
}
}