-
Notifications
You must be signed in to change notification settings - Fork 48
/
m_ct_test.go
54 lines (46 loc) · 1.31 KB
/
m_ct_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
package tc
import (
"errors"
"net"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestCt(t *testing.T) {
tests := map[string]struct {
val Ct
err1 error
err2 error
}{
"simple": {val: Ct{Parms: &CtParms{Index: 3}}},
"all arguments": {val: Ct{Parms: &CtParms{Capab: 4}, Action: uint16Ptr(5),
Zone: uint16Ptr(5), Mark: uint32Ptr(0xAA55AA55), MarkMask: uint32Ptr(0x55AA55AA),
NatIPv4Min: netIPPtr(net.ParseIP("1.2.3.4")), NatIPv4Max: netIPPtr(net.ParseIP("8.8.4.4")),
NatPortMin: uint16Ptr(42), NatPortMax: uint16Ptr(73)}},
}
for name, testcase := range tests {
t.Run(name, func(t *testing.T) {
data, err1 := marshalCt(&testcase.val)
if !errors.Is(err1, testcase.err1) {
t.Fatalf("Unexpected error: %v", err1)
}
newData, tm := injectTcft(t, data, tcaCtTm)
newData = injectAttribute(t, newData, []byte{}, tcaCtPad)
val := Ct{}
err2 := unmarshalCt(newData, &val)
if !errors.Is(err2, testcase.err2) {
t.Fatalf("Unexpected error: %v", err2)
}
// Reinject value to expected values
testcase.val.Tm = tm
if diff := cmp.Diff(val, testcase.val); diff != "" {
t.Fatalf("Ct missmatch (want +got):\n%s", diff)
}
})
}
t.Run("nil", func(t *testing.T) {
_, err := marshalCt(nil)
if !errors.Is(err, ErrNoArg) {
t.Fatalf("unexpected error: %v", err)
}
})
}