-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnfdump_test.go
92 lines (77 loc) · 2.96 KB
/
nfdump_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
package nfdump
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"testing"
)
var testData = []NFRecord{
{Flags: 0x86, MsecFirst: 0x3be, MsecLast: 0x3be, First: 0x5d51b507, Last: 0x5d51b507, FwdStatus: 0x0, TCPFlags: 0x10, Proto: 0x6, Tos: 0x0, SrcPort: 0x1bb, DstPort: 0xa16a, ExporterSysID: 0x4c8, Reserved: 0x0, SrcIP: net.IP{0xd8, 0xce, 0x91, 0x83}, DstIP: net.IP{0xd1, 0x94, 0xcd, 0x37}, PacketCount: 0xbb8, ByteCount: 0x44aa20, Input: 0x492, Output: 0x4f0, SrcAS: 0xd1, DstAS: 0x32c, DstTos: 0x0, Dir: 0x0, SrcMask: 0xf, DstMask: 0x14, NextHopIP: net.IP{0x40, 0x56, 0x4f, 0x7f}, BGPNextIP: net.IP(nil), SrcVlan: 0x2, DstVLan: 0x0, OutPkts: 0x0, OutBytes: 0x0, AggeFlows: 0x0, RouterIP: net.IP{0x42, 0x6e, 0x1, 0x11}, Received: 0x16c872c34c8},
{Flags: 0x86, MsecFirst: 0x2a, MsecLast: 0x2a, First: 0x5d51b508, Last: 0x5d51b508, FwdStatus: 0x0, TCPFlags: 0x10, Proto: 0x6, Tos: 0x0, SrcPort: 0x291d, DstPort: 0x1bb, ExporterSysID: 0x4c8, Reserved: 0x0, SrcIP: net.IP{0xc8, 0x44, 0x96, 0x56}, DstIP: net.IP{0x63, 0x56, 0x3d, 0xaa}, PacketCount: 0xbb8, ByteCount: 0x26160, Input: 0x492, Output: 0x3e7, SrcAS: 0x6ef3, DstAS: 0x407d, DstTos: 0x0, Dir: 0x0, SrcMask: 0x18, DstMask: 0x16, NextHopIP: net.IP{0x40, 0x56, 0x4f, 0x7b}, BGPNextIP: net.IP(nil), SrcVlan: 0x2, DstVLan: 0x0, OutPkts: 0x0, OutBytes: 0x0, AggeFlows: 0x0, RouterIP: net.IP{0x42, 0x6e, 0x1, 0x11}, Received: 0x16c872c34c8},
}
var testFileRecordLength = 100000
var testFiles = []struct {
fileName string
expectedRecords int
}{
{
fileName: "testdata/nfcapd-large-none",
expectedRecords: 100000,
},
{
fileName: "testdata/nfcapd-large-lzo",
expectedRecords: 100000,
},
{
fileName: "testdata/nfcapd-empty",
expectedRecords: 0,
},
}
func TestReader(t *testing.T) {
for _, tc := range testFiles {
tc := tc
t.Run(tc.fileName, func(t *testing.T) {
var data []byte
var err error
if data, err = ioutil.ReadFile(tc.fileName); err != nil {
t.Error(err)
}
var reader = bytes.NewReader(data)
var nff *NFFile
if nff, err = ParseReader(reader); err != nil {
t.Error(err)
}
for x, record := range nff.Records {
// Only test first 2 records
if x > 1 {
break
}
if fmt.Sprintf("%#v", record) != fmt.Sprintf("%#v", testData[x]) {
t.Errorf("test record:%d does not match", x)
}
}
if len(nff.Records) != tc.expectedRecords {
t.Errorf("Unexpected record count:%d in test file, expected %d", len(nff.Records), tc.expectedRecords)
}
})
}
}
// BenchmarkReadFile read all test files to allow benchmarking how fast files can be read.
func BenchmarkReadFile(b *testing.B) {
for n := 0; n < b.N; n++ {
var err error
var data []byte
var nff *NFFile
if data, err = ioutil.ReadFile(testFiles[0].fileName); err != nil {
b.Error(err)
}
var reader = bytes.NewReader(data)
if nff, err = ParseReader(reader); err != nil {
b.Error(err)
}
if len(nff.Records) != testFileRecordLength {
b.Error("Unexpected Record count")
}
}
}