-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_test.go
More file actions
623 lines (566 loc) · 20.7 KB
/
check_test.go
File metadata and controls
623 lines (566 loc) · 20.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//go:build linux
package kfeatures
import (
"errors"
"reflect"
"testing"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"golang.org/x/sys/unix"
)
func TestSystemFeatures_Result(t *testing.T) {
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: true},
BPFLSMEnabled: ProbeResult{Supported: true},
BTF: ProbeResult{Supported: true},
IMAEnabled: ProbeResult{Supported: false},
Kprobe: ProbeResult{Supported: true},
KprobeMulti: ProbeResult{Supported: true},
Fentry: ProbeResult{Supported: true},
Tracepoint: ProbeResult{Supported: true},
HasCapBPF: ProbeResult{Supported: true},
HasCapSysAdmin: ProbeResult{Supported: true},
HasCapPerfmon: ProbeResult{Supported: false},
UnprivilegedBPFDisabled: ProbeResult{Supported: true},
JITEnabled: ProbeResult{Supported: true},
JITHardened: ProbeResult{Supported: false},
BPFSyscall: ProbeResult{Supported: true},
PerfEventOpen: ProbeResult{Supported: true},
TraceFS: ProbeResult{Supported: true},
BPFFS: ProbeResult{Supported: false},
InInitUserNS: ProbeResult{Supported: false},
BPFStatsEnabled: ProbeResult{Supported: false},
PreemptMode: PreemptDynamic,
}
tests := []struct {
feature Feature
wantOK bool
wantValue bool
}{
{FeatureBPFLSM, true, true},
{FeatureBTF, true, true},
{FeatureIMA, true, false},
{FeatureKprobe, true, true},
{FeatureKprobeMulti, true, true},
{FeatureFentry, true, true},
{FeatureTracepoint, true, true},
{FeatureCapBPF, true, true},
{FeatureCapSysAdmin, true, true},
{FeatureCapPerfmon, true, false},
{FeatureJITEnabled, true, true},
{FeatureJITHardened, true, false},
{FeatureBPFSyscall, true, true},
{FeaturePerfEventOpen, true, true},
{FeatureSleepableBPF, true, true},
{FeatureTraceFS, true, true},
{FeatureBPFFS, true, false},
{FeatureInitUserNS, true, false},
{FeatureUnprivilegedBPFDisabled, true, true},
{FeatureBPFStatsEnabled, true, false},
{Feature(999), false, false},
}
for _, tt := range tests {
t.Run(tt.feature.String(), func(t *testing.T) {
result, ok := sf.Result(tt.feature)
if ok != tt.wantOK {
t.Errorf("Result() ok = %v, want %v", ok, tt.wantOK)
}
if result.Supported != tt.wantValue {
t.Errorf("Result() Supported = %v, want %v", result.Supported, tt.wantValue)
}
})
}
}
func TestResult_BPFLSMComposite(t *testing.T) {
t.Run("both supported", func(t *testing.T) {
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: true},
BPFLSMEnabled: ProbeResult{Supported: true},
}
result, ok := sf.Result(FeatureBPFLSM)
if !ok || !result.Supported {
t.Error("expected Supported=true when both LSM program type and sysfs pass")
}
})
t.Run("program type fails with error", func(t *testing.T) {
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: false, Error: unix.EPERM},
BPFLSMEnabled: ProbeResult{Supported: true},
}
result, ok := sf.Result(FeatureBPFLSM)
if !ok {
t.Fatal("expected known=true")
}
if result.Supported {
t.Error("expected Supported=false when LSM program type probe fails")
}
if result.Error != unix.EPERM {
t.Errorf("expected Error=EPERM, got %v", result.Error)
}
})
t.Run("program type not supported", func(t *testing.T) {
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: false},
BPFLSMEnabled: ProbeResult{Supported: true},
}
result, ok := sf.Result(FeatureBPFLSM)
if !ok {
t.Fatal("expected known=true")
}
if result.Supported {
t.Error("expected Supported=false when kernel doesn't support LSM programs")
}
})
t.Run("program type ok but sysfs fails", func(t *testing.T) {
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: true},
BPFLSMEnabled: ProbeResult{Supported: false},
}
result, ok := sf.Result(FeatureBPFLSM)
if !ok {
t.Fatal("expected known=true")
}
if result.Supported {
t.Error("expected Supported=false when 'bpf' not in LSM list")
}
})
}
func TestSystemFeatures_Diagnose(t *testing.T) {
t.Run("BPF LSM not in config", func(t *testing.T) {
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: true},
KernelConfig: NewKernelConfig(map[string]ConfigValue{}),
}
got := sf.Diagnose(FeatureBPFLSM)
if got != "CONFIG_BPF_LSM not set; rebuild kernel with CONFIG_BPF_LSM=y" {
t.Errorf("Diagnose(FeatureBPFLSM) = %q", got)
}
})
t.Run("BPF LSM probe error has deterministic remediation", func(t *testing.T) {
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: true},
BPFLSMEnabled: ProbeResult{Error: errors.New("permission denied")},
}
got := sf.Diagnose(FeatureBPFLSM)
if got != "unable to read active LSM list (/sys/kernel/security/lsm); ensure securityfs is mounted and readable" {
t.Errorf("Diagnose(FeatureBPFLSM) = %q", got)
}
})
t.Run("BPF LSM compiled but not in boot params", func(t *testing.T) {
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: true},
BPFLSMEnabled: ProbeResult{Supported: false},
KernelConfig: NewKernelConfig(map[string]ConfigValue{"BPF_LSM": ConfigBuiltin}),
}
got := sf.Diagnose(FeatureBPFLSM)
if got != "CONFIG_BPF_LSM=y but 'bpf' not in active LSM list; add lsm=...,bpf to kernel boot params" {
t.Errorf("Diagnose(FeatureBPFLSM) = %q", got)
}
})
t.Run("BPF LSM program type probe error (EPERM)", func(t *testing.T) {
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: false, Error: unix.EPERM},
}
got := sf.Diagnose(FeatureBPFLSM)
want := "cannot probe LSM program type: operation not permitted; run as root or add CAP_BPF/CAP_SYS_ADMIN"
if got != want {
t.Errorf("Diagnose(FeatureBPFLSM) = %q, want %q", got, want)
}
})
t.Run("BPF LSM program type not supported", func(t *testing.T) {
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: false},
BPFLSMEnabled: ProbeResult{Supported: true}, // sysfs says yes, but kernel can't load LSM
}
got := sf.Diagnose(FeatureBPFLSM)
want := "LSM program type not supported; use a kernel with BPF LSM support (CONFIG_BPF_LSM=y)"
if got != want {
t.Errorf("Diagnose(FeatureBPFLSM) = %q, want %q", got, want)
}
})
t.Run("BTF not in config", func(t *testing.T) {
sf := &SystemFeatures{
KernelConfig: NewKernelConfig(map[string]ConfigValue{}),
}
got := sf.Diagnose(FeatureBTF)
if got != "CONFIG_DEBUG_INFO_BTF not set; rebuild kernel with CONFIG_DEBUG_INFO_BTF=y" {
t.Errorf("Diagnose(FeatureBTF) = %q", got)
}
})
t.Run("kprobe.multi not in config", func(t *testing.T) {
sf := &SystemFeatures{
KernelConfig: NewKernelConfig(map[string]ConfigValue{}),
}
got := sf.Diagnose(FeatureKprobeMulti)
if got != "CONFIG_FPROBE not set; requires kernel 5.18+ with CONFIG_FPROBE=y" {
t.Errorf("Diagnose(FeatureKprobeMulti) = %q", got)
}
})
t.Run("program-type diagnostics", func(t *testing.T) {
sf := &SystemFeatures{}
if got := sf.Diagnose(FeatureKprobe); got != "kprobe program type not supported; use a kernel with BPF kprobe support or switch to a supported attach type" {
t.Errorf("Diagnose(FeatureKprobe) = %q", got)
}
if got := sf.Diagnose(FeatureFentry); got != "fentry/fexit program type not supported; use a kernel with BPF trampoline support (and BTF) or switch attach strategy" {
t.Errorf("Diagnose(FeatureFentry) = %q", got)
}
if got := sf.Diagnose(FeatureTracepoint); got != "tracepoint program type not supported; ensure perf events are enabled and use a kernel with tracepoint BPF support" {
t.Errorf("Diagnose(FeatureTracepoint) = %q", got)
}
})
t.Run("IMA not in config", func(t *testing.T) {
sf := &SystemFeatures{
KernelConfig: NewKernelConfig(map[string]ConfigValue{}),
}
got := sf.Diagnose(FeatureIMA)
if got != "CONFIG_IMA not set; rebuild kernel with CONFIG_IMA=y" {
t.Errorf("Diagnose(FeatureIMA) = %q", got)
}
})
t.Run("IMA probe error has deterministic remediation", func(t *testing.T) {
sf := &SystemFeatures{
IMAEnabled: ProbeResult{Error: errors.New("permission denied")},
}
got := sf.Diagnose(FeatureIMA)
if got != "unable to read active LSM list (/sys/kernel/security/lsm); ensure securityfs is mounted and readable to verify IMA state" {
t.Errorf("Diagnose(FeatureIMA) = %q", got)
}
})
t.Run("capability diagnostics", func(t *testing.T) {
sf := &SystemFeatures{}
if got := sf.Diagnose(FeatureCapBPF); got != "missing CAP_BPF; run with CAP_BPF or as root" {
t.Errorf("Diagnose(FeatureCapBPF) = %q", got)
}
if got := sf.Diagnose(FeatureCapSysAdmin); got != "missing CAP_SYS_ADMIN; run as root or add CAP_SYS_ADMIN" {
t.Errorf("Diagnose(FeatureCapSysAdmin) = %q", got)
}
if got := sf.Diagnose(FeatureCapPerfmon); got != "missing CAP_PERFMON; run with CAP_PERFMON or as root" {
t.Errorf("Diagnose(FeatureCapPerfmon) = %q", got)
}
})
t.Run("JIT diagnostics", func(t *testing.T) {
sf := &SystemFeatures{}
if got := sf.Diagnose(FeatureJITEnabled); got != "BPF JIT disabled; set /proc/sys/net/core/bpf_jit_enable to 1" {
t.Errorf("Diagnose(FeatureJITEnabled) = %q", got)
}
if got := sf.Diagnose(FeatureJITHardened); got != "BPF JIT hardening disabled; set /proc/sys/net/core/bpf_jit_harden to 1 or 2" {
t.Errorf("Diagnose(FeatureJITHardened) = %q", got)
}
})
t.Run("syscall diagnostics", func(t *testing.T) {
sf := &SystemFeatures{}
if got := sf.Diagnose(FeatureBPFSyscall); got != "bpf() syscall not available; kernel too old or CONFIG_BPF not enabled" {
t.Errorf("Diagnose(FeatureBPFSyscall) = %q", got)
}
if got := sf.Diagnose(FeaturePerfEventOpen); got != "perf_event_open() syscall not available; kernel too old or CONFIG_PERF_EVENTS not enabled" {
t.Errorf("Diagnose(FeaturePerfEventOpen) = %q", got)
}
})
t.Run("sleepable BPF with non-preemptible kernel", func(t *testing.T) {
sf := &SystemFeatures{
KernelConfig: NewKernelConfig(map[string]ConfigValue{"PREEMPT_NONE": ConfigBuiltin}),
}
got := sf.Diagnose(FeatureSleepableBPF)
if got != "kernel preemption model is none; sleepable BPF (BPF_F_SLEEPABLE) requires CONFIG_PREEMPT or CONFIG_PREEMPT_DYNAMIC" {
t.Errorf("Diagnose(FeatureSleepableBPF) = %q", got)
}
})
t.Run("sleepable BPF without kernel config", func(t *testing.T) {
sf := &SystemFeatures{}
got := sf.Diagnose(FeatureSleepableBPF)
if got != "cannot determine preemption model; kernel config not available" {
t.Errorf("Diagnose(FeatureSleepableBPF) = %q", got)
}
})
t.Run("filesystem diagnostics", func(t *testing.T) {
sf := &SystemFeatures{}
if got := sf.Diagnose(FeatureTraceFS); got != "tracefs not mounted; mount tracefs at /sys/kernel/tracing (or /sys/kernel/debug/tracing on older kernels)" {
t.Errorf("Diagnose(FeatureTraceFS) = %q", got)
}
if got := sf.Diagnose(FeatureBPFFS); got != "bpffs not mounted; mount bpffs at /sys/fs/bpf" {
t.Errorf("Diagnose(FeatureBPFFS) = %q", got)
}
})
t.Run("namespace diagnostics", func(t *testing.T) {
sf := &SystemFeatures{}
if got := sf.Diagnose(FeatureInitUserNS); got != "process not in initial user namespace; run in host user namespace or adjust container runtime settings" {
t.Errorf("Diagnose(FeatureInitUserNS) = %q", got)
}
})
t.Run("unprivileged bpf diagnostics", func(t *testing.T) {
sf := &SystemFeatures{}
if got := sf.Diagnose(FeatureUnprivilegedBPFDisabled); got != "unprivileged BPF is enabled; set /proc/sys/kernel/unprivileged_bpf_disabled to 1 or 2" {
t.Errorf("Diagnose(FeatureUnprivilegedBPFDisabled) = %q", got)
}
})
t.Run("bpf stats diagnostics", func(t *testing.T) {
sf := &SystemFeatures{}
if got := sf.Diagnose(FeatureBPFStatsEnabled); got != "BPF runtime stats are disabled; set /proc/sys/kernel/bpf_stats_enabled to 1" {
t.Errorf("Diagnose(FeatureBPFStatsEnabled) = %q", got)
}
})
t.Run("no kernel config fallback", func(t *testing.T) {
// With zero-value SystemFeatures, LSMProgramType is {Supported: false},
// so Diagnose reports the program type as the blocker.
sf := &SystemFeatures{}
got := sf.Diagnose(FeatureBPFLSM)
if got != "LSM program type not supported; use a kernel with BPF LSM support (CONFIG_BPF_LSM=y)" {
t.Errorf("Diagnose(FeatureBPFLSM) without config = %q", got)
}
})
t.Run("no kernel config fallback with LSM program type supported", func(t *testing.T) {
// LSM program type works but no kernel config and sysfs check failed.
sf := &SystemFeatures{
LSMProgramType: ProbeResult{Supported: true},
}
got := sf.Diagnose(FeatureBPFLSM)
if got != "not supported" {
t.Errorf("Diagnose(FeatureBPFLSM) = %q, want 'not supported'", got)
}
})
}
func TestProbeOptionsFor(t *testing.T) {
t.Run("security features", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureBPFLSM, FeatureIMA})
// Should include security subsystems.
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.securitySubsystems {
t.Error("expected securitySubsystems=true for BPF LSM + IMA")
}
})
t.Run("kprobe.multi needs kernel config", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureKprobeMulti})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.kernelConfig {
t.Error("expected kernelConfig=true for kprobe.multi")
}
if len(cfg.programTypes) == 0 {
t.Error("expected programTypes to include Kprobe")
}
})
t.Run("capabilities", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureCapBPF})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.capabilities {
t.Error("expected capabilities=true for CAP_BPF")
}
})
t.Run("JIT features", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureJITEnabled, FeatureJITHardened})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.jit {
t.Error("expected jit=true for JIT features")
}
})
t.Run("syscall features", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureBPFSyscall, FeaturePerfEventOpen})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.syscalls {
t.Error("expected syscalls=true for syscall features")
}
})
t.Run("sleepable BPF needs kernel config", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureSleepableBPF})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.kernelConfig {
t.Error("expected kernelConfig=true for sleepable BPF")
}
})
t.Run("filesystem features", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureTraceFS, FeatureBPFFS})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.filesystems {
t.Error("expected filesystems=true for filesystem features")
}
})
t.Run("namespace features", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureInitUserNS})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.namespaces {
t.Error("expected namespaces=true for namespace features")
}
})
t.Run("unprivileged bpf disabled needs capabilities", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureUnprivilegedBPFDisabled})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.capabilities {
t.Error("expected capabilities=true for unprivileged bpf feature")
}
})
t.Run("bpf stats enabled needs capabilities", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureBPFStatsEnabled})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.capabilities {
t.Error("expected capabilities=true for bpf stats feature")
}
})
t.Run("BPF LSM includes LSM program type", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureBPFLSM})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.securitySubsystems {
t.Error("expected securitySubsystems=true for FeatureBPFLSM")
}
hasLSM := false
for _, pt := range cfg.programTypes {
if pt == ebpf.LSM {
hasLSM = true
break
}
}
if !hasLSM {
t.Error("expected programTypes to include ebpf.LSM for FeatureBPFLSM")
}
})
t.Run("IMA alone sets security but no program types", func(t *testing.T) {
opts := probeOptionsFor([]Feature{FeatureIMA})
cfg := &probeConfig{}
for _, opt := range opts {
opt(cfg)
}
if !cfg.securitySubsystems {
t.Error("expected securitySubsystems=true for FeatureIMA")
}
if len(cfg.programTypes) != 0 {
t.Errorf("expected no programTypes for FeatureIMA, got %v", cfg.programTypes)
}
})
t.Run("empty", func(t *testing.T) {
opts := probeOptionsFor(nil)
if len(opts) != 0 {
t.Errorf("expected 0 options for nil, got %d", len(opts))
}
})
}
func TestNormalizeRequirements(t *testing.T) {
rs := normalizeRequirements([]Requirement{
FeatureBTF,
FeatureGroup{
FeatureKprobe,
RequireProgramType(ebpf.XDP),
RequireMapType(ebpf.Hash),
RequireProgramHelper(ebpf.Kprobe, asm.FnGetCurrentPidTgid),
},
FeatureGroup{
FeatureBTF, // duplicate
FeatureCapBPF,
},
RequireProgramType(ebpf.XDP), // duplicate
RequireMapType(ebpf.Hash), // duplicate
RequireProgramHelper(ebpf.Kprobe, asm.FnGetCurrentPidTgid), // duplicate
})
if !reflect.DeepEqual(rs.features, []Feature{
FeatureBTF,
FeatureKprobe,
FeatureCapBPF,
}) {
t.Fatalf("features = %#v", rs.features)
}
if !reflect.DeepEqual(rs.programTypes, []ebpf.ProgramType{
ebpf.XDP,
}) {
t.Fatalf("programTypes = %#v", rs.programTypes)
}
if !reflect.DeepEqual(rs.mapTypes, []ebpf.MapType{
ebpf.Hash,
}) {
t.Fatalf("mapTypes = %#v", rs.mapTypes)
}
if !reflect.DeepEqual(rs.programHelpers, []ProgramHelperRequirement{
{
ProgramType: ebpf.Kprobe,
Helper: asm.FnGetCurrentPidTgid,
},
}) {
t.Fatalf("programHelpers = %#v", rs.programHelpers)
}
}
func TestParameterizedRequirementReasonHelpers(t *testing.T) {
t.Run("program type reason classification", func(t *testing.T) {
pt := ebpf.XDP
if got := reasonForProgramTypeError(pt, ebpf.ErrNotSupported); got != "program type XDP is unavailable on this kernel; use a kernel/workload combination that supports it" {
t.Fatalf("reasonForProgramTypeError(not-supported) = %q", got)
}
if got := reasonForProgramTypeError(pt, unix.EPERM); got != "cannot probe program type XDP due to insufficient privileges; run as root or add CAP_BPF/CAP_SYS_ADMIN" {
t.Fatalf("reasonForProgramTypeError(EPERM) = %q", got)
}
if got := reasonForProgramTypeError(pt, errors.New("boom")); got != "unable to validate program type XDP support; verify kernel BPF support and required privileges (CAP_BPF/CAP_SYS_ADMIN)" {
t.Fatalf("reasonForProgramTypeError(default) = %q", got)
}
})
t.Run("map type reason classification", func(t *testing.T) {
mt := ebpf.Hash
if got := reasonForMapTypeError(mt, ebpf.ErrNotSupported); got != "map type Hash is unavailable on this kernel; use a supported map type or a newer kernel" {
t.Fatalf("reasonForMapTypeError(not-supported) = %q", got)
}
if got := reasonForMapTypeError(mt, unix.EACCES); got != "cannot probe map type Hash due to insufficient privileges; run as root or add CAP_BPF/CAP_SYS_ADMIN" {
t.Fatalf("reasonForMapTypeError(EACCES) = %q", got)
}
if got := reasonForMapTypeError(mt, errors.New("boom")); got != "unable to validate map type Hash support; verify kernel BPF support and required privileges (CAP_BPF/CAP_SYS_ADMIN)" {
t.Fatalf("reasonForMapTypeError(default) = %q", got)
}
})
t.Run("program helper reason classification", func(t *testing.T) {
req := ProgramHelperRequirement{
ProgramType: ebpf.XDP,
Helper: asm.FnMapLookupElem,
}
if got := reasonForProgramHelperError(req, ebpf.ErrNotSupported); got != "helper FnMapLookupElem is unavailable for program type XDP; choose a compatible helper/program combination or newer kernel" {
t.Fatalf("reasonForProgramHelperError(not-supported) = %q", got)
}
if got := reasonForProgramHelperError(req, unix.EPERM); got != "cannot probe helper FnMapLookupElem for program type XDP due to insufficient privileges; run as root or add CAP_BPF/CAP_SYS_ADMIN" {
t.Fatalf("reasonForProgramHelperError(EPERM) = %q", got)
}
if got := reasonForProgramHelperError(req, errors.New("boom")); got != "unable to validate helper FnMapLookupElem for program type XDP; verify kernel support and required privileges (CAP_BPF/CAP_SYS_ADMIN)" {
t.Fatalf("reasonForProgramHelperError(default) = %q", got)
}
})
}
func TestCheck_WithFeatureGroup(t *testing.T) {
err := Check(FeatureGroup{Feature(999)})
if err == nil {
t.Fatal("expected error")
}
var fe *FeatureError
if !errors.As(err, &fe) {
t.Fatalf("expected FeatureError, got %T", err)
}
if fe.Feature != "Feature(999)" {
t.Fatalf("FeatureError.Feature = %q", fe.Feature)
}
if fe.Reason != "unknown feature" {
t.Fatalf("FeatureError.Reason = %q", fe.Reason)
}
}