-
Notifications
You must be signed in to change notification settings - Fork 48
/
ematch_nbyte_test.go
79 lines (73 loc) · 1.59 KB
/
ematch_nbyte_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
package tc
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestNByteMatch(t *testing.T) {
tests := map[string]struct {
val NByteMatch
err1 error
err2 error
}{
"simple": {
val: NByteMatch{Needle: []byte("helloWorld"),
Offset: 42,
Layer: 7},
},
}
for name, testcase := range tests {
t.Run(name, func(t *testing.T) {
data, err1 := marshalNByteMatch(&testcase.val)
if !errors.Is(err1, testcase.err1) {
t.Fatalf("Unexpected error: %v", err1)
}
val := NByteMatch{}
err2 := unmarshalNByteMatch(data, &val)
if !errors.Is(err2, testcase.err2) {
t.Fatalf("Unexpected error: %v", err2)
}
if diff := cmp.Diff(val, testcase.val); diff != "" {
t.Fatalf("NByteMatch missmatch (want +got):\n%s", diff)
}
})
}
t.Run("nil", func(t *testing.T) {
_, err := marshalNByteMatch(nil)
if !errors.Is(err, ErrNoArg) {
t.Fatalf("unexpected error: %v", err)
}
})
}
func TestUnmarshalNByteMatch(t *testing.T) {
tests := map[string]struct {
data []byte
needleLen uint16
err error
}{
"invalid lenght": {
data: []byte{0x0, 0x1, 0x2, 0x3},
err: ErrInvalidArg,
},
"invalid needle": {
data: []byte{0x00, 0x00,
0xaa, 0xaa,
0x01,
0x00, 0x00, 0x00,
0x0a, 0x0b, 0x0c},
err: ErrInvalidArg,
},
}
for name, testcase := range tests {
name := name
testcase := testcase
t.Run(name, func(t *testing.T) {
info := NByteMatch{}
if err := unmarshalNByteMatch(testcase.data, &info); err != nil {
if !errors.Is(err, ErrInvalidArg) {
t.Fatalf("Unexpected error: %v", err)
}
}
})
}
}