-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes_test.go
More file actions
372 lines (339 loc) · 9.7 KB
/
types_test.go
File metadata and controls
372 lines (339 loc) · 9.7 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package kfeatures
import (
"errors"
"fmt"
"testing"
)
func TestConfigValue_IsEnabled(t *testing.T) {
tests := []struct {
value ConfigValue
want bool
}{
{ConfigNotSet, false},
{ConfigModule, true},
{ConfigBuiltin, true},
}
for _, tt := range tests {
if got := tt.value.IsEnabled(); got != tt.want {
t.Errorf("ConfigValue(%d).IsEnabled() = %v, want %v", tt.value, got, tt.want)
}
}
}
func TestConfigValue_IsBuiltin(t *testing.T) {
tests := []struct {
value ConfigValue
want bool
}{
{ConfigNotSet, false},
{ConfigModule, false},
{ConfigBuiltin, true},
}
for _, tt := range tests {
if got := tt.value.IsBuiltin(); got != tt.want {
t.Errorf("ConfigValue(%d).IsBuiltin() = %v, want %v", tt.value, got, tt.want)
}
}
}
func TestConfigValue_String(t *testing.T) {
tests := []struct {
value ConfigValue
want string
}{
{ConfigNotSet, "not set"},
{ConfigModule, "m"},
{ConfigBuiltin, "y"},
{ConfigValue(99), "ConfigValue(99)"},
}
for _, tt := range tests {
if got := tt.value.String(); got != tt.want {
t.Errorf("ConfigValue(%d).String() = %q, want %q", tt.value, got, tt.want)
}
}
}
func TestKernelConfig_Get(t *testing.T) {
kc := NewKernelConfig(map[string]ConfigValue{
"BPF_LSM": ConfigBuiltin,
"IMA": ConfigBuiltin,
"DEBUG_INFO_BTF": ConfigBuiltin,
"FPROBE": ConfigBuiltin,
"XDP_SOCKETS": ConfigBuiltin,
})
if got := kc.Get("BPF_LSM"); got != ConfigBuiltin {
t.Errorf("Get(BPF_LSM) = %v, want ConfigBuiltin", got)
}
if got := kc.Get("NONEXISTENT"); got != ConfigNotSet {
t.Errorf("Get(NONEXISTENT) = %v, want ConfigNotSet", got)
}
// Convenience fields.
if kc.BPFLSM != ConfigBuiltin {
t.Errorf("BPFLSM = %v, want ConfigBuiltin", kc.BPFLSM)
}
if kc.IMA != ConfigBuiltin {
t.Errorf("IMA = %v, want ConfigBuiltin", kc.IMA)
}
if kc.BTF != ConfigBuiltin {
t.Errorf("BTF = %v, want ConfigBuiltin", kc.BTF)
}
if kc.KprobeMulti != ConfigBuiltin {
t.Errorf("KprobeMulti = %v, want ConfigBuiltin", kc.KprobeMulti)
}
}
func TestKernelConfig_IsSet(t *testing.T) {
kc := NewKernelConfig(map[string]ConfigValue{
"BPF_LSM": ConfigBuiltin,
"NET": ConfigModule,
})
if !kc.IsSet("BPF_LSM") {
t.Error("IsSet(BPF_LSM) = false, want true")
}
if !kc.IsSet("NET") {
t.Error("IsSet(NET) = false, want true")
}
if kc.IsSet("MISSING") {
t.Error("IsSet(MISSING) = true, want false")
}
}
func TestKernelConfig_Nil(t *testing.T) {
var kc *KernelConfig
if got := kc.Get("anything"); got != ConfigNotSet {
t.Errorf("nil KernelConfig.Get() = %v, want ConfigNotSet", got)
}
if kc.IsSet("anything") {
t.Error("nil KernelConfig.IsSet() = true, want false")
}
}
func TestKernelConfig_Immutability(t *testing.T) {
raw := map[string]ConfigValue{
"BPF_LSM": ConfigBuiltin,
}
kc := NewKernelConfig(raw)
// Mutate the original map.
raw["BPF_LSM"] = ConfigNotSet
raw["NEW_KEY"] = ConfigModule
// KernelConfig should not be affected.
if kc.Get("BPF_LSM") != ConfigBuiltin {
t.Error("KernelConfig was affected by mutation of original map")
}
if kc.Get("NEW_KEY") != ConfigNotSet {
t.Error("KernelConfig was affected by addition to original map")
}
}
func TestFeature_String(t *testing.T) {
tests := []struct {
f Feature
want string
}{
{FeatureBPFLSM, "bpf-lsm"},
{FeatureBTF, "btf"},
{FeatureIMA, "ima"},
{FeatureKprobe, "kprobe"},
{FeatureKprobeMulti, "kprobe-multi"},
{FeatureFentry, "fentry"},
{FeatureTracepoint, "tracepoint"},
{FeatureCapBPF, "cap-bpf"},
{FeatureCapSysAdmin, "cap-sys-admin"},
{FeatureCapPerfmon, "cap-perfmon"},
{FeatureJITEnabled, "jit-enabled"},
{FeatureJITHardened, "jit-hardened"},
{FeatureBPFSyscall, "bpf-syscall"},
{FeaturePerfEventOpen, "perf-event-open"},
{FeatureSleepableBPF, "sleepable-bpf"},
{FeatureTraceFS, "trace-fs"},
{FeatureBPFFS, "bpf-fs"},
{FeatureInitUserNS, "init-user-ns"},
{FeatureUnprivilegedBPFDisabled, "unprivileged-bpf-disabled"},
{FeatureBPFStatsEnabled, "bpf-stats-enabled"},
{Feature(999), "Feature(999)"},
}
for _, tt := range tests {
if got := tt.f.String(); got != tt.want {
t.Errorf("Feature(%d).String() = %q, want %q", tt.f, got, tt.want)
}
}
}
func TestFeature_ParseAndHelpers(t *testing.T) {
names := FeatureNames()
values := FeatureValues()
if len(names) != len(values) {
t.Fatalf("FeatureNames() and FeatureValues() length mismatch: %d != %d", len(names), len(values))
}
for i, name := range names {
got, err := ParseFeature(name)
if err != nil {
t.Fatalf("ParseFeature(%q) error = %v", name, err)
}
if got != values[i] {
t.Fatalf("ParseFeature(%q) = %v, want %v", name, got, values[i])
}
if !got.IsValid() {
t.Fatalf("Feature(%v).IsValid() = false, want true", got)
}
}
if _, err := ParseFeature("not-a-feature"); err == nil {
t.Fatal("ParseFeature(not-a-feature) expected error")
}
if Feature(999).IsValid() {
t.Fatal("Feature(999).IsValid() = true, want false")
}
}
func TestFeature_ParseFeature_CaseInsensitive(t *testing.T) {
tests := []struct {
input string
want Feature
}{
{input: "IMA", want: FeatureIMA},
{input: "iMa", want: FeatureIMA},
{input: "BPF-SYSCALL", want: FeatureBPFSyscall},
{input: "Trace-FS", want: FeatureTraceFS},
}
for _, tt := range tests {
got, err := ParseFeature(tt.input)
if err != nil {
t.Fatalf("ParseFeature(%q) error = %v", tt.input, err)
}
if got != tt.want {
t.Fatalf("ParseFeature(%q) = %v, want %v", tt.input, got, tt.want)
}
}
}
func TestFeature_TextCodecs(t *testing.T) {
t.Run("marshal text", func(t *testing.T) {
text, err := FeatureCapBPF.MarshalText()
if err != nil {
t.Fatalf("MarshalText() error = %v", err)
}
if got, want := string(text), "cap-bpf"; got != want {
t.Fatalf("MarshalText() = %q, want %q", got, want)
}
})
t.Run("unmarshal text case insensitive", func(t *testing.T) {
var got Feature
if err := got.UnmarshalText([]byte("CAP-PERFMON")); err != nil {
t.Fatalf("UnmarshalText() error = %v", err)
}
if got != FeatureCapPerfmon {
t.Fatalf("UnmarshalText() = %v, want %v", got, FeatureCapPerfmon)
}
})
t.Run("unmarshal text invalid value", func(t *testing.T) {
var got Feature
if err := got.UnmarshalText([]byte("not-a-feature")); err == nil {
t.Fatal("UnmarshalText(not-a-feature) expected error")
}
})
t.Run("append text", func(t *testing.T) {
f := FeatureBPFLSM
out, err := (&f).AppendText([]byte("prefix:"))
if err != nil {
t.Fatalf("AppendText() error = %v", err)
}
if got, want := string(out), "prefix:bpf-lsm"; got != want {
t.Fatalf("AppendText() = %q, want %q", got, want)
}
})
}
func TestPreemptMode(t *testing.T) {
tests := []struct {
mode PreemptMode
wantStr string
wantSleepable bool
}{
{PreemptUnknown, "unknown", false},
{PreemptNone, "none", false},
{PreemptVoluntary, "voluntary", false},
{PreemptFull, "full", true},
{PreemptDynamic, "dynamic", true},
}
for _, tt := range tests {
t.Run(tt.wantStr, func(t *testing.T) {
if got := tt.mode.String(); got != tt.wantStr {
t.Errorf("String() = %q, want %q", got, tt.wantStr)
}
if got := tt.mode.SupportsSleepable(); got != tt.wantSleepable {
t.Errorf("SupportsSleepable() = %v, want %v", got, tt.wantSleepable)
}
})
}
}
func TestDerivePreemptMode(t *testing.T) {
tests := []struct {
name string
raw map[string]ConfigValue
want PreemptMode
}{
{"dynamic", map[string]ConfigValue{"PREEMPT_DYNAMIC": ConfigBuiltin}, PreemptDynamic},
{"full", map[string]ConfigValue{"PREEMPT": ConfigBuiltin}, PreemptFull},
{"voluntary", map[string]ConfigValue{"PREEMPT_VOLUNTARY": ConfigBuiltin}, PreemptVoluntary},
{"none", map[string]ConfigValue{"PREEMPT_NONE": ConfigBuiltin}, PreemptNone},
{"empty", map[string]ConfigValue{}, PreemptUnknown},
{"dynamic wins", map[string]ConfigValue{"PREEMPT_DYNAMIC": ConfigBuiltin, "PREEMPT": ConfigBuiltin}, PreemptDynamic},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
kc := NewKernelConfig(tt.raw)
if kc.Preempt != tt.want {
t.Errorf("Preempt = %v, want %v", kc.Preempt, tt.want)
}
})
}
}
func TestFeatureError(t *testing.T) {
t.Run("without underlying error", func(t *testing.T) {
fe := &FeatureError{Feature: "BPF LSM", Reason: "not set"}
want := "feature BPF LSM: not set"
if got := fe.Error(); got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
if fe.Unwrap() != nil {
t.Error("Unwrap() should be nil")
}
})
t.Run("with underlying error", func(t *testing.T) {
inner := errors.New("permission denied")
fe := &FeatureError{Feature: "BTF", Reason: "probe failed", Err: inner}
want := "feature BTF: probe failed: permission denied"
if got := fe.Error(); got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
if !errors.Is(fe, inner) {
t.Error("errors.Is should match underlying error")
}
})
t.Run("errors.As", func(t *testing.T) {
fe := &FeatureError{Feature: "IMA", Reason: "not supported"}
err := fmt.Errorf("check failed: %w", fe)
var target *FeatureError
if !errors.As(err, &target) {
t.Fatal("errors.As should match FeatureError")
}
if target.Feature != "IMA" {
t.Errorf("Feature = %q, want %q", target.Feature, "IMA")
}
})
}
func TestProbeResult(t *testing.T) {
t.Run("supported", func(t *testing.T) {
r := ProbeResult{Supported: true}
if !r.Supported {
t.Error("Supported should be true")
}
if r.Error != nil {
t.Error("Error should be nil")
}
})
t.Run("unsupported", func(t *testing.T) {
r := ProbeResult{Supported: false}
if r.Supported {
t.Error("Supported should be false")
}
})
t.Run("error", func(t *testing.T) {
r := ProbeResult{Supported: false, Error: errors.New("oops")}
if r.Supported {
t.Error("Supported should be false")
}
if r.Error == nil {
t.Error("Error should not be nil")
}
})
}