-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdecode_test.go
58 lines (49 loc) · 1.32 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
package nf9packet
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDecodeValidHeader(t *testing.T) {
data := []byte{
0x00, 0x09, // Version
0x00, 0x00, // Records count
0x00, 0x00, 0x01, 0x00, // System uptime in milliseconds (256)
0x00, 0x00, 0x02, 0x00, // Timestamp (512)
0x00, 0x00, 0x04, 0x00, // Sequence number (1024)
0x00, 0x00, 0x08, 0x00, // Source ID (2048)
}
expected := Packet{
Version: 9,
Count: 0,
SysUpTime: 256,
UnixSecs: 512,
SequenceNumber: 1024,
SourceId: 2048,
FlowSets: []interface{}{},
}
actual, err := Decode(data)
require.NoError(t, err)
assert.Equal(t, &expected, actual)
}
func TestDecodeIncompleteHeader(t *testing.T) {
data := []byte{
0x00, 0x09, // Version
0x00, 0x00, // Records count
0x00, 0x00, 0x01, 0x00, // System uptime in milliseconds (256)
0x00, 0x00, 0x02, 0x00, // Timestamp (512)
0x00, 0x00, 0x04, 0x00, // Sequence number (1024)
0x00, 0x00, 0x08, // Source ID, missing single byte
}
actual, err := Decode(data)
assert.Error(t, err)
assert.Nil(t, actual)
}
func TestDecodeEmptyPacket(t *testing.T) {
actual, err := Decode(nil)
assert.Error(t, err)
assert.Nil(t, actual)
actual, err = Decode([]byte{})
assert.Error(t, err)
assert.Nil(t, actual)
}