Unit Tests
Netcap has tests for its core functionality

Prerequisites

Some of the tests operate on a dump file that is not in the repository.
You can download it with:
1
$ zeus download-test-pcap
Copied!
which will basically just invoke:
1
wget https://weberblog.net/wp-content/uploads/2020/02/The-Ultimate-PCAP.7z
Copied!
Now unpack the file and move it to the tests folder in the project root.

Unit Tests

Unit tests have been implemented for parts of the core functionality. Currently there are basic tests for reading pcap data from files and traffic live from an interface, as well as tests and benchmarks for common utility functions, such progress displaying and time conversions.
The tests and benchmarks can be executed from the repository root by executing the following from the project root:
1
$ go test -v ./...
2
=== RUN TestCountRecords
3
--- PASS: TestCountRecords (0.18s)
4
=== RUN TestReader
5
--- PASS: TestReader (0.01s)
6
=== RUN TestWriter
7
--- PASS: TestWriter (0.06s)
8
PASS
9
ok github.com/dreadl0ck/netcap 0.862s
10
? github.com/dreadl0ck/netcap/cmd [no test files]
11
? github.com/dreadl0ck/netcap/cmd/agent [no test files]
12
? github.com/dreadl0ck/netcap/cmd/capture [no test files]
13
? github.com/dreadl0ck/netcap/cmd/collect [no test files]
14
? github.com/dreadl0ck/netcap/cmd/dump [no test files]
15
? github.com/dreadl0ck/netcap/cmd/export [no test files]
16
? github.com/dreadl0ck/netcap/cmd/label [no test files]
17
? github.com/dreadl0ck/netcap/cmd/proxy [no test files]
18
? github.com/dreadl0ck/netcap/cmd/split [no test files]
19
? github.com/dreadl0ck/netcap/cmd/transform [no test files]
20
? github.com/dreadl0ck/netcap/cmd/util [no test files]
21
=== RUN TestCollectPCA
22
done in 2.595847118s
23
--- PASS: TestCollectPCAP (2.60s)
24
PASS
25
ok github.com/dreadl0ck/netcap/collector 3.242s
26
=== RUN TestCorruptedWriter
27
TestCorruptedWriter: delimited_test.go:46: Put record returned expected error: BAD
28
--- PASS: TestCorruptedWriter (0.00s)
29
=== RUN TestGoodWriter
30
--- PASS: TestGoodWriter (0.00s)
31
=== RUN TestCorruptedReader
32
TestCorruptedReader: delimited_test.go:87: Next record returned expected error: unexpected EOF
33
--- PASS: TestCorruptedReader (0.00s)
34
=== RUN TestGoodReader
35
--- PASS: TestGoodReader (0.00s)
36
=== RUN TestRoundTrip
37
TestRoundTrip: delimited_test.go:136: After writing: buffer="\x04Some\x02of\x04what\x01a\x04fool\x06thinks\x05often\bremains." len=42
38
--- PASS: TestRoundTrip (0.00s)
39
PASS
40
ok github.com/dreadl0ck/netcap/delimited 0.526s
41
? github.com/dreadl0ck/netcap/dpi [no test files]
42
? github.com/dreadl0ck/netcap/encoder [no test files]
43
? github.com/dreadl0ck/netcap/io [no test files]
44
? github.com/dreadl0ck/netcap/label [no test files]
45
? github.com/dreadl0ck/netcap/maltego [no test files]
46
? github.com/dreadl0ck/netcap/metrics [no test files]
47
? github.com/dreadl0ck/netcap/resolvers [no test files]
48
=== RUN TestMarshal
49
--- PASS: TestMarshal (0.00s)
50
PASS
51
ok github.com/dreadl0ck/netcap/types 0.668s
52
=== RUN TestTimeToString
53
--- PASS: TestTimeToString (0.00s)
54
=== RUN TestStringToTime
55
--- PASS: TestStringToTime (0.00s)
56
PASS
57
ok github.com/dreadl0ck/netcap/utils 0.932s
Copied!

Benchmarks

Run the benchmarks using:
1
$ go test -bench=. ./... | grep -E "Bench|pkg"
2
pkg: github.com/dreadl0ck/netcap/collector
3
BenchmarkReadPcapNG-12 1265539 844 ns/op 1249 B/op 1 allocs/op
4
BenchmarkReadPcapNGZeroCopy-12 2028283 640 ns/op 0 B/op 0 allocs/op
5
BenchmarkReadPcap-12 7557667 137 ns/op 106 B/op 1 allocs/op
6
pkg: github.com/dreadl0ck/netcap/types
7
BenchmarkMarshal-12 9817819 110 ns/op 64 B/op 1 allocs/op
8
BenchmarkUnmarshal-12 8703766 134 ns/op 40 B/op 2 allocs/op
9
pkg: github.com/dreadl0ck/netcap/utils
10
BenchmarkTimeToStringOld-12 5283726 229 ns/op 64 B/op 4 allocs/op
11
BenchmarkTimeToString-12 8273997 136 ns/op 80 B/op 3 allocs/op
12
BenchmarkStringToTime-12 8842005 137 ns/op 32 B/op 1 allocs/op
13
BenchmarkStringToTimeFieldsFunc-12 6809409 185 ns/op 32 B/op 1 allocs/op
14
BenchmarkProgressOld-12 54425902 21.0 ns/op 0 B/op 0 allocs/op
15
BenchmarkProgress-12 23389420 45.9 ns/op 16 B/op 2 allocs/op
Copied!

Race Detection Tests

Run the tests with race detection enabled:
1
$ go test -race -v ./...
Copied!
Last modified 1yr ago