forked from postmanlabs/observability-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
223 lines (185 loc) · 5.61 KB
/
util_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package pcap
import (
"bytes"
"fmt"
"net"
"strconv"
"strings"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/gopacket"
"github.com/google/gopacket/reassembly"
"github.com/akitasoftware/akita-libs/akinet"
"github.com/akitasoftware/akita-libs/memview"
)
var (
ip1 = net.IP{1, 2, 3, 4}
port1 = 1234
ip2 = net.IP{8, 8, 8, 8}
port2 = 53
brokerIP = net.IP{172, 16, 12, 3}
)
type fakePcap []gopacket.Packet
func (f fakePcap) capturePackets(done <-chan struct{}, interfaceName, bpfFilter string) (<-chan gopacket.Packet, error) {
outChan := make(chan gopacket.Packet)
go func() {
defer close(outChan)
for _, p := range f {
select {
case <-done:
return
case outChan <- p:
}
}
}()
return outChan, nil
}
func (f fakePcap) getInterfaceAddrs(interfaceName string) ([]net.IP, error) {
return []net.IP{brokerIP}, nil
}
// Fake pcap implementation that only closes the output channel when explicitly
// cancelled.
type forceCancelPcap []gopacket.Packet
func (f forceCancelPcap) capturePackets(done <-chan struct{}, interfaceName, bpfFilter string) (<-chan gopacket.Packet, error) {
outChan := make(chan gopacket.Packet)
go func() {
defer close(outChan)
for _, p := range f {
select {
case <-done:
return
case outChan <- p:
}
}
// Wait for explicit cancellation
<-done
}()
return outChan, nil
}
func (f forceCancelPcap) getInterfaceAddrs(interfaceName string) ([]net.IP, error) {
return []net.IP{brokerIP}, nil
}
const (
princeProtoHeader = "prince|"
pineappleProtoHeader = "pineapple^"
)
// Quick and dirty way to see if t1 < t2. For test only.
func netTrafficLess(t1, t2 akinet.ParsedNetworkTraffic) bool {
return strings.Compare(fmt.Sprintf("%v", t1), fmt.Sprintf("%v", t2)) < 0
}
func princeLess(p1, p2 akinet.AkitaPrince) bool {
return strings.Compare(string(p1), string(p2)) < 0
}
func byteSliceLess(b1, b2 []byte) bool {
return bytes.Compare(b1, b2) < 0
}
func stringLess(s1, s2 string) bool {
return strings.Compare(s1, s2) < 0
}
func compareDroppedBytes(db1, db2 akinet.DroppedBytes) bool {
return db1 == db2
}
// Custom cmp.Diff function with common options for use in net_parse related
// tests.
func netParseCmp(e1, e2 interface{}) string {
return cmp.Diff(e1, e2,
cmpopts.SortSlices(netTrafficLess),
cmpopts.SortSlices(princeLess),
cmpopts.SortSlices(byteSliceLess),
cmpopts.SortSlices(stringLess),
cmp.Comparer(compareDroppedBytes),
)
}
// Prince protocol: prince|<payload>|
type princeParserFactory struct{}
func (princeParserFactory) Name() string {
return "PrinceParserFactory"
}
func (princeParserFactory) Accepts(input memview.MemView, isEnd bool) (decision akinet.AcceptDecision, df int64) {
defer func() {
if decision == akinet.NeedMoreData && isEnd {
decision = akinet.Reject
df = input.Len()
}
}()
if input.Len() < int64(len(princeProtoHeader)) {
return akinet.NeedMoreData, 0
}
i := input.Index(0, []byte(princeProtoHeader))
if i < 0 {
// The proto header could have leading bytes in front of it.
p := input.Index(0, []byte(princeProtoHeader[0:1]))
if p >= 0 {
return akinet.NeedMoreData, input.Len() - p
}
return akinet.NeedMoreData, input.Len()
}
return akinet.Accept, i
}
func (princeParserFactory) CreateParser(id akinet.TCPBidiID, seq, ack reassembly.Sequence) akinet.TCPParser {
return &princeParser{}
}
type princeParser struct {
all memview.MemView
}
func (*princeParser) Name() string {
return "prince!"
}
// Assumes input starts with the right header.
func (p *princeParser) Parse(input memview.MemView, isEnd bool) (akinet.ParsedNetworkContent, memview.MemView, int64, error) {
p.all.Append(input)
bytesConsumed := p.all.Len()
barOne := int64(len(princeProtoHeader) - 1)
if p.all.GetByte(barOne) != '|' {
return nil, memview.MemView{}, bytesConsumed, fmt.Errorf("prince parser got content that does start with prince proto header %s", strconv.Quote(p.all.String()))
}
barTwo := p.all.Index(barOne+1, []byte("|"))
if barTwo < 0 {
if isEnd {
return nil, memview.MemView{}, bytesConsumed, fmt.Errorf("EOF before parse done")
}
// Not done yet.
return nil, memview.MemView{}, bytesConsumed, nil
}
c := akinet.AkitaPrince(p.all.SubView(barOne+1, barTwo).String())
unused := p.all.SubView(barTwo+1, p.all.Len())
bytesConsumed -= unused.Len()
return c, unused, bytesConsumed, nil
}
// Prince protocol: pineapple^<payload>^
type pineappleParserFactory struct{}
func (pineappleParserFactory) Name() string {
return "PineappleParserFactory"
}
func (pineappleParserFactory) Accepts(input memview.MemView, isEnd bool) (decision akinet.AcceptDecision, df int64) {
defer func() {
if decision == akinet.NeedMoreData && isEnd {
decision = akinet.Reject
df = input.Len()
}
}()
if input.Len() < int64(len(pineappleProtoHeader)) {
return akinet.NeedMoreData, 0
}
i := input.Index(0, []byte(pineappleProtoHeader))
if i < 0 {
// The proto header could have leading bytes in front of it.
p := input.Index(0, []byte(pineappleProtoHeader[0:1]))
if p >= 0 {
return akinet.NeedMoreData, input.Len() - p
}
return akinet.NeedMoreData, input.Len()
}
return akinet.Accept, i
}
func (pineappleParserFactory) CreateParser(id akinet.TCPBidiID, seq, ack reassembly.Sequence) akinet.TCPParser {
return pineappleParser{}
}
type pineappleParser struct{}
func (pineappleParser) Name() string {
return "pineapple!"
}
// Assumes input starts with the right header.
func (pineappleParser) Parse(input memview.MemView, isEnd bool) (akinet.ParsedNetworkContent, memview.MemView, int64, error) {
return nil, memview.MemView{}, input.Len(), fmt.Errorf("should not get invoked")
}