-
Notifications
You must be signed in to change notification settings - Fork 48
/
common_test.go
61 lines (47 loc) · 1.27 KB
/
common_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
package tc
// Collection of functions that are used often in regular tests.
import (
"testing"
"github.com/mdlayher/netlink"
)
// injectTcft alters orig and adds a Tcft struct, that can only be returned by
// the kernel, and returns the new data bytes.
func injectTcft(t *testing.T, orig []byte, tcftAttribute uint16) ([]byte, *Tcft) {
t.Helper()
tcft := Tcft{
Install: 12,
LastUse: 34,
Expires: 56,
FirstUse: 78,
}
tcftBytes, err := marshalStruct(tcft)
if err != nil {
t.Fatalf("Failed to marshal tcft: %v", err)
}
newData := injectAttribute(t, orig, tcftBytes, tcftAttribute)
return newData, &tcft
}
// injectAttribute is a helper function for tests to inject new data for a tcaAttribute
func injectAttribute(t *testing.T, orig, new []byte, tcaAttribute uint16) []byte {
t.Helper()
var attrs []netlink.Attribute
ad, err := netlink.NewAttributeDecoder(orig)
if err != nil {
t.Fatalf("Failed to decode attributes: %v", err)
}
for ad.Next() {
attrs = append(attrs, netlink.Attribute{
Type: ad.Type(),
Data: ad.Bytes(),
})
}
attrs = append(attrs, netlink.Attribute{
Type: tcaAttribute,
Data: new,
})
newData, err := netlink.MarshalAttributes(attrs)
if err != nil {
t.Fatalf("Failed to marshal attributes: %v", err)
}
return newData
}