-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode_test.go
160 lines (136 loc) · 4.91 KB
/
decode_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package packet
import (
"fmt"
"os"
"testing"
"github.com/nickchen/packet/fixture"
"github.com/nickchen/packet/fixture/bgp"
"github.com/stretchr/testify/assert"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
var frame = []byte{
0xfa, 0x16, 0x3e, 0x85, 0x92, 0x77, 0xfa, 0x16, /* ..>..w.. */
0x3e, 0x1a, 0x43, 0xcb, 0x81, 0x00, 0x0f, 0xfe, /* >.C..... */
0x08, 0x00, 0x45, 0x00, 0x00, 0x6b, 0x9a, 0xaf, /* ..E..k.. */
0x40, 0x00, 0x01, 0x06, 0xca, 0xa2, 0x0a, 0x14, /* @....... */
0x00, 0x0a, 0x0a, 0x0a, 0x00, 0x14, 0x89, 0xce, /* ........ */
0x00, 0xb3, 0x48, 0x0c, 0x55, 0x19, 0x8b, 0xd2, /* ..H.U... */
0x47, 0x96, 0x80, 0x18, 0x00, 0x73, 0xfc, 0x5c, /* G....s.\ */
0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x80, 0x02, /* ........ */
0x3c, 0xbe, 0x00, 0x0a, 0xf2, 0x19, 0xff, 0xff, /* <....... */
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* ........ */
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x37, /* .......7 */
0x01, 0x04, 0xfd, 0xea, 0x00, 0x5a, 0x0a, 0x28, /* .....Z.( */
0x00, 0x0a, 0x1a, 0x02, 0x06, 0x01, 0x04, 0x00, /* ........ */
0x01, 0x00, 0x01, 0x02, 0x02, 0x80, 0x00, 0x02, /* ........ */
0x02, 0x02, 0x00, 0x02, 0x08, 0x40, 0x06, 0x00, /* .....@.. */
0x78, 0x00, 0x01, 0x01, 0x00, 0xf5, 0xde, 0xb0, /* x....... */
0xf5, 0x00, 0x14, 0x00, 0x01, 0x00, 0x01, 0x00, /* ........ */
0x01, 0x00, 0x0c, 0x00, 0x02, 0x01, 0x00, 0x00, /* ........ */
0x00, /* . */
}
func TestFixture(t *testing.T) {
ether := &fixture.EthernetII{}
err := Unmarshal(frame, ether)
assert.NoError(t, err, "failed to decode etherframe")
fmt.Printf("ether %+v\n", ether)
vlan, ok := ether.Body.(*fixture.VLAN)
assert.True(t, ok, "failed to find vlan")
fmt.Printf("vlan %+v\n", vlan)
ipv4, ok := vlan.Body.(*fixture.IPv4)
assert.True(t, ok, "failed to find ipv4")
fmt.Printf("ipv4 %+v\n", ipv4)
tcp, ok := ipv4.Body.(*fixture.TCP)
assert.True(t, ok, "failed to find TCP")
fmt.Printf("tcp %+v\n", tcp)
bgp, ok := tcp.Body.(*bgp.Message)
fmt.Printf("bgp %+v\n", bgp)
assert.True(t, ok, "failed to find BGP")
assert.NotNil(t, bgp, "bgp is null")
}
func TestCompare(t *testing.T) {
// Decode a packet
gp := gopacket.NewPacket(frame, layers.LayerTypeEthernet, gopacket.Default)
ip := &fixture.IPv4{}
err := Unmarshal(frame[18:], ip)
assert.NoError(t, err, "failed to unmarshal packet")
// expectedIP := fixture.IPv4{Version: 4, IHL: 5, Protocol: 6}
// if !assert.ObjectsAreEqual(expectedIP, ip) {
// fmt.Printf("object: *(%v)* *(%v)*", expectedIP, ip)
// }
gpIPLayer := gp.Layer(layers.LayerTypeIPv4)
assert.NotNil(t, gpIPLayer, "tcp layer decoded")
gpIP, _ := gpIPLayer.(*layers.IPv4)
assert.True(t, IPPacketEqual(t, ip, gpIP))
assert.NotNil(t, ip.Body, "body should be populated with TCP")
tcp, ok := ip.Body.(*fixture.TCP)
assert.True(t, ok, "tcp message from ip")
assert.NotNil(t, tcp, "tcp body not found")
fmt.Printf("TCP: %+v\n", tcp)
gpTCPLayer := gp.Layer(layers.LayerTypeTCP)
assert.NotNil(t, gpTCPLayer, "tcp layer decoded")
gpTCP, _ := gpTCPLayer.(*layers.TCP)
fmt.Printf("GP TCP: %+v\n", gpTCP)
}
func IPPacketEqual(t *testing.T, ip *fixture.IPv4, gp *layers.IPv4) bool {
assert.Equal(t, gp.Version, ip.Version)
assert.Equal(t, gp.IHL, ip.IHL)
assert.Equal(t, gp.Length, ip.Length)
assert.Equal(t, gp.Checksum, uint16(ip.Checksum))
fmt.Printf("object: *(%v)* *(%v)*\n", ip, gp)
return true
}
/*
Running tool: /usr/local/go/bin/go test -benchmem -run=^$ github.com/nickchen/packet -bench ^(BenchmarkGoPacket)$
for Etherframe -> VLAN -> IP -> TCP
goos: darwin
goarch: amd64
pkg: github.com/nickchen/packet
BenchmarkGoPacket-8 1000000 1035 ns/op 1240 B/op 12 allocs/op
BenchmarkGoPacket-12 2000000 877 ns/op 1240 B/op 12 allocs/op
PASS
ok github.com/nickchen/packet 1.396s
Success: Benchmarks passed.
*/
func BenchmarkGoPacket(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
_ = gopacket.NewPacket(frame, layers.LayerTypeEthernet, gopacket.Default)
}
}
/* for Etherframe -> VLAN -> IP -> TCP -> BGP -> Open ... 23 allocs */
func BenchmarkPacket(b *testing.B) {
b.ReportAllocs()
ether := &fixture.EthernetII{}
for n := 0; n < b.N; n++ {
_ = Unmarshal(frame, ether)
}
}
func TestReadPCAP(t *testing.T) {
pcapFile := "fixture/NTLM-wenchao.pcap"
if _, err := os.Stat(pcapFile); !os.IsNotExist(err) {
pcap, err := fixture.OpenPCAP(pcapFile)
assert.NoError(t, err, "failed to open pcap")
if err == nil {
count := 0
for p := range pcap.PacketData() {
fmt.Printf("=====\n")
ether := &fixture.EthernetII{}
err = Unmarshal(p, ether)
assert.NoError(t, err, "failed to decode")
fmt.Printf("Packet: %+v\n", ether)
ip, _ := ether.Body.(*fixture.IPv4)
assert.NotNil(t, ip, "ether->ip")
fmt.Printf("IP: %+v\n", ip)
tcp, _ := ip.Body.(*fixture.TCP)
assert.NotNil(t, tcp, "ip->tcp")
fmt.Printf("TCP: %s\n", string(tcp.Body.([]byte)))
count++
if count >= 5 {
break
}
}
}
}
}