-
Notifications
You must be signed in to change notification settings - Fork 48
/
class_test.go
133 lines (114 loc) · 2.75 KB
/
class_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package tc
import (
"errors"
"testing"
"github.com/florianl/go-tc/core"
"github.com/florianl/go-tc/internal/unix"
)
func TestClass(t *testing.T) {
tcSocket, done := testConn(t)
defer done()
err := tcSocket.Class().Add(nil)
if err != ErrNoArg {
t.Fatalf("expected ErrNoArg, received: %v", err)
}
err = tcSocket.Class().Replace(nil)
if err != ErrNoArg {
t.Fatalf("expected ErrNoArg, received: %v", err)
}
err = tcSocket.Class().Replace(&Object{
Msg{
Ifindex: 0,
},
Attribute{},
})
if err != ErrInvalidDev {
t.Fatalf("expected ErrInvalidDev, received: %v", err)
}
err = tcSocket.Class().Delete(nil)
if err != ErrNoArg {
t.Fatalf("expected ErrNoArg, received: %v", err)
}
err = tcSocket.Class().Delete(&Object{
Msg{
Ifindex: 0,
},
Attribute{},
})
if err != ErrInvalidDev {
t.Fatalf("expected ErrInvalidDev, received: %v", err)
}
_, err = tcSocket.Class().Get(nil)
if err != ErrNoArg {
t.Fatalf("expected ErrNoArg, received: %v", err)
}
tcMsg := Msg{
Family: unix.AF_UNSPEC,
Ifindex: 1337,
Handle: core.BuildHandle(0x1, 0x0000),
Parent: 0xFFFFFFFF,
Info: 0,
}
testQdisc := Object{
tcMsg,
Attribute{
Kind: "htb",
Htb: &Htb{
Init: &HtbGlob{
Defcls: 0x30,
},
},
},
}
// tc qdisc add dev $INTERFACE root handle 1: htb default 30
if err := tcSocket.Qdisc().Add(&testQdisc); err != nil {
t.Fatalf("could not add new qdisc: %v", err)
}
tcMsg.Parent = 0x10000
tcMsg.Handle = core.BuildHandle(0x1, 0x1)
tests := map[string]struct {
kind string
err error
hfsc *Hfsc
htb *Htb
dsmark *Dsmark
}{
"hfsc": {kind: "hfsc", hfsc: &Hfsc{Rsc: &ServiceCurve{M1: 12, D: 34, M2: 56}}},
"htb": {kind: "htb", htb: &Htb{DirectQlen: uint32Ptr(4455)}},
"dsmark": {kind: "dsmark", dsmark: &Dsmark{DefaultIndex: uint16Ptr(42)}},
"unknown": {kind: "unknown", err: ErrNotImplemented},
}
for name, testcase := range tests {
t.Run(name, func(t *testing.T) {
testClass := Object{
tcMsg,
Attribute{
Kind: testcase.kind,
Hfsc: testcase.hfsc,
Htb: testcase.htb,
Dsmark: testcase.dsmark,
},
}
if err := tcSocket.Class().Add(&testClass); err != nil {
if errors.Is(err, testcase.err) {
t.Log("received expected error")
return
}
t.Fatalf("could not add new class: %v", err)
}
classes, err := tcSocket.Class().Get(&tcMsg)
if err != nil {
t.Fatalf("could not get classes: %v", err)
}
for _, class := range classes {
t.Logf("%#v\n", class)
}
if err := tcSocket.Class().Replace(&testClass); err != nil {
t.Fatalf("could not replace exisiting class: %v", err)
}
if err := tcSocket.Class().Delete(&testClass); err != nil {
t.Fatalf("could not delete class: %v", err)
}
})
}
}