| 1 |
package ebpf |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"reflect" |
| 7 |
"runtime" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/go-quicktest/qt" |
| 12 |
|
| 13 |
"github.com/cilium/ebpf/asm" |
| 14 |
"github.com/cilium/ebpf/btf" |
| 15 |
"github.com/cilium/ebpf/internal" |
| 16 |
"github.com/cilium/ebpf/internal/sys" |
| 17 |
"github.com/cilium/ebpf/internal/testutils" |
| 18 |
) |
| 19 |
|
| 20 |
var btfFn = &btf.Func{ |
| 21 |
Name: "_", |
| 22 |
Type: &btf.FuncProto{ |
| 23 |
Return: &btf.Int{Size: 16}, |
| 24 |
Params: []btf.FuncParam{}, |
| 25 |
}, |
| 26 |
Linkage: btf.StaticFunc, |
| 27 |
} |
| 28 |
|
| 29 |
var hashMapSpec = &MapSpec{ |
| 30 |
Type: Hash, |
| 31 |
KeySize: 4, |
| 32 |
ValueSize: 5, |
| 33 |
MaxEntries: 2, |
| 34 |
Flags: sys.BPF_F_NO_PREALLOC, |
| 35 |
} |
| 36 |
|
| 37 |
var multiprogSpec = &ProgramSpec{ |
| 38 |
Name: "test", |
| 39 |
Type: SocketFilter, |
| 40 |
Instructions: asm.Instructions{ |
| 41 |
btf.WithFuncMetadata(asm.LoadImm(asm.R0, 0, asm.DWord), btfFn). |
| 42 |
WithSource(asm.Comment("line info")), |
| 43 |
asm.Call.Label("fn"), |
| 44 |
asm.Return(), |
| 45 |
btf.WithFuncMetadata(asm.LoadImm(asm.R0, 0, asm.DWord), btfFn). |
| 46 |
WithSource(asm.Comment("line info")).WithSymbol("fn"), |
| 47 |
asm.Return(), |
| 48 |
}, |
| 49 |
License: "MIT", |
| 50 |
} |
| 51 |
|
| 52 |
func validateMapInfo(t *testing.T, info *MapInfo, spec *MapSpec) { |
| 53 |
t.Helper() |
| 54 |
|
| 55 |
qt.Assert(t, qt.Equals(info.Type, spec.Type)) |
| 56 |
qt.Assert(t, qt.Equals(info.KeySize, spec.KeySize)) |
| 57 |
qt.Assert(t, qt.Equals(info.ValueSize, spec.ValueSize)) |
| 58 |
qt.Assert(t, qt.Equals(info.MaxEntries, spec.MaxEntries)) |
| 59 |
qt.Assert(t, qt.Equals(info.Flags, spec.Flags)) |
| 60 |
|
| 61 |
memlock, _ := info.Memlock() |
| 62 |
qt.Assert(t, qt.Not(qt.Equals(memlock, 0))) |
| 63 |
} |
| 64 |
|
| 65 |
func TestMapInfo(t *testing.T) { |
| 66 |
m := mustNewMap(t, hashMapSpec, nil) |
| 67 |
|
| 68 |
info, err := m.Info() |
| 69 |
qt.Assert(t, qt.IsNil(err)) |
| 70 |
|
| 71 |
validateMapInfo(t, info, hashMapSpec) |
| 72 |
} |
| 73 |
|
| 74 |
func TestMapInfoFromProc(t *testing.T) { |
| 75 |
hash := mustNewMap(t, hashMapSpec, nil) |
| 76 |
|
| 77 |
var info MapInfo |
| 78 |
err := readMapInfoFromProc(hash.fd, &info) |
| 79 |
testutils.SkipIfNotSupported(t, err) |
| 80 |
qt.Assert(t, qt.IsNil(err)) |
| 81 |
|
| 82 |
validateMapInfo(t, &info, hashMapSpec) |
| 83 |
} |
| 84 |
|
| 85 |
func TestMapInfoFromProcOuterMap(t *testing.T) { |
| 86 |
outer := &MapSpec{ |
| 87 |
Type: ArrayOfMaps, |
| 88 |
KeySize: 4, |
| 89 |
ValueSize: 4, |
| 90 |
MaxEntries: 2, |
| 91 |
InnerMap: &MapSpec{ |
| 92 |
Type: Array, |
| 93 |
KeySize: 4, |
| 94 |
ValueSize: 4, |
| 95 |
MaxEntries: 2, |
| 96 |
}, |
| 97 |
} |
| 98 |
m := mustNewMap(t, outer, nil) |
| 99 |
|
| 100 |
var info MapInfo |
| 101 |
err := readMapInfoFromProc(m.fd, &info) |
| 102 |
testutils.SkipIfNotSupported(t, err) |
| 103 |
qt.Assert(t, qt.IsNil(err)) |
| 104 |
|
| 105 |
validateMapInfo(t, &info, outer) |
| 106 |
} |
| 107 |
|
| 108 |
func BenchmarkNewMapFromFD(b *testing.B) { |
| 109 |
b.ReportAllocs() |
| 110 |
|
| 111 |
m := mustNewMap(b, hashMapSpec, nil) |
| 112 |
|
| 113 |
for b.Loop() { |
| 114 |
if _, err := newMapFromFD(m.fd); err != nil { |
| 115 |
b.Fatal(err) |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
func BenchmarkMapInfo(b *testing.B) { |
| 121 |
b.ReportAllocs() |
| 122 |
|
| 123 |
m := mustNewMap(b, hashMapSpec, nil) |
| 124 |
|
| 125 |
for b.Loop() { |
| 126 |
if _, err := newMapInfoFromFd(m.fd); err != nil { |
| 127 |
b.Fatal(err) |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func validateProgInfo(t *testing.T, spec *ProgramSpec, info *ProgramInfo) { |
| 133 |
t.Helper() |
| 134 |
|
| 135 |
qt.Assert(t, qt.Equals(info.Type, spec.Type)) |
| 136 |
if info.Tag != "" { |
| 137 |
qt.Assert(t, qt.SliceAny( |
| 138 |
[]string{ |
| 139 |
"d7edec644f05498d", |
| 140 |
"01e57aadad14352b", |
| 141 |
}, |
| 142 |
qt.F2(qt.Equals, info.Tag), |
| 143 |
)) |
| 144 |
} |
| 145 |
memlock, ok := info.Memlock() |
| 146 |
if ok { |
| 147 |
qt.Assert(t, qt.Equals(memlock, uint64(os.Getpagesize()))) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func TestProgramInfo(t *testing.T) { |
| 152 |
spec := fixupProgramSpec(basicProgramSpec) |
| 153 |
prog := mustNewProgram(t, spec, nil) |
| 154 |
|
| 155 |
info, err := newProgramInfoFromFd(prog.fd) |
| 156 |
testutils.SkipIfNotSupported(t, err) |
| 157 |
qt.Assert(t, qt.IsNil(err)) |
| 158 |
|
| 159 |
validateProgInfo(t, spec, info) |
| 160 |
|
| 161 |
id, ok := info.ID() |
| 162 |
qt.Assert(t, qt.IsTrue(ok)) |
| 163 |
qt.Assert(t, qt.Not(qt.Equals(id, 0))) |
| 164 |
|
| 165 |
if testutils.IsVersionLessThan(t, "4.15", "windows:0.20") { |
| 166 |
qt.Assert(t, qt.Equals(info.Name, "")) |
| 167 |
} else { |
| 168 |
qt.Assert(t, qt.Equals(info.Name, "test")) |
| 169 |
} |
| 170 |
|
| 171 |
if jitedSize, err := info.JitedSize(); testutils.IsVersionLessThan(t, "4.13") { |
| 172 |
qt.Assert(t, qt.IsNotNil(err)) |
| 173 |
} else { |
| 174 |
qt.Assert(t, qt.IsNil(err)) |
| 175 |
qt.Assert(t, qt.IsTrue(jitedSize > 0)) |
| 176 |
} |
| 177 |
|
| 178 |
if xlatedSize, err := info.TranslatedSize(); testutils.IsVersionLessThan(t, "4.13") { |
| 179 |
qt.Assert(t, qt.IsNotNil(err)) |
| 180 |
} else { |
| 181 |
qt.Assert(t, qt.IsNil(err)) |
| 182 |
qt.Assert(t, qt.IsTrue(xlatedSize > 0)) |
| 183 |
} |
| 184 |
|
| 185 |
if uid, ok := info.CreatedByUID(); testutils.IsVersionLessThan(t, "4.15") { |
| 186 |
qt.Assert(t, qt.IsFalse(ok)) |
| 187 |
} else { |
| 188 |
qt.Assert(t, qt.IsTrue(ok)) |
| 189 |
qt.Assert(t, qt.Equals(uid, uint32(os.Getuid()))) |
| 190 |
} |
| 191 |
|
| 192 |
if loadTime, ok := info.LoadTime(); testutils.IsVersionLessThan(t, "4.15") { |
| 193 |
qt.Assert(t, qt.IsFalse(ok)) |
| 194 |
} else { |
| 195 |
qt.Assert(t, qt.IsTrue(ok)) |
| 196 |
qt.Assert(t, qt.IsTrue(loadTime > 0)) |
| 197 |
} |
| 198 |
|
| 199 |
if verifiedInsns, ok := info.VerifiedInstructions(); testutils.IsVersionLessThan(t, "5.16") { |
| 200 |
qt.Assert(t, qt.IsFalse(ok)) |
| 201 |
} else { |
| 202 |
qt.Assert(t, qt.IsTrue(ok)) |
| 203 |
qt.Assert(t, qt.IsTrue(verifiedInsns > 0)) |
| 204 |
} |
| 205 |
|
| 206 |
if insns, ok := info.JitedInsns(); testutils.IsVersionLessThan(t, "4.13") { |
| 207 |
qt.Assert(t, qt.IsFalse(ok)) |
| 208 |
} else { |
| 209 |
qt.Assert(t, qt.IsTrue(ok)) |
| 210 |
qt.Assert(t, qt.IsTrue(len(insns) > 0)) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
func BenchmarkNewProgramFromFD(b *testing.B) { |
| 215 |
b.ReportAllocs() |
| 216 |
|
| 217 |
spec := fixupProgramSpec(basicProgramSpec) |
| 218 |
prog := mustNewProgram(b, spec, nil) |
| 219 |
|
| 220 |
for b.Loop() { |
| 221 |
if _, err := newProgramFromFD(prog.fd); err != nil { |
| 222 |
b.Fatal(err) |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func BenchmarkProgramInfo(b *testing.B) { |
| 228 |
b.ReportAllocs() |
| 229 |
|
| 230 |
spec := fixupProgramSpec(basicProgramSpec) |
| 231 |
prog := mustNewProgram(b, spec, nil) |
| 232 |
|
| 233 |
for b.Loop() { |
| 234 |
if _, err := newProgramInfoFromFd(prog.fd); err != nil { |
| 235 |
b.Fatal(err) |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func TestProgramInfoProc(t *testing.T) { |
| 241 |
spec := fixupProgramSpec(basicProgramSpec) |
| 242 |
prog := mustNewProgram(t, spec, nil) |
| 243 |
|
| 244 |
var info ProgramInfo |
| 245 |
err := readProgramInfoFromProc(prog.fd, &info) |
| 246 |
testutils.SkipIfNotSupported(t, err) |
| 247 |
qt.Assert(t, qt.IsNil(err)) |
| 248 |
|
| 249 |
validateProgInfo(t, spec, &info) |
| 250 |
} |
| 251 |
|
| 252 |
func TestProgramInfoBTF(t *testing.T) { |
| 253 |
prog, err := newProgram(t, multiprogSpec, nil) |
| 254 |
testutils.SkipIfNotSupported(t, err) |
| 255 |
qt.Assert(t, qt.IsNil(err)) |
| 256 |
|
| 257 |
info, err := prog.Info() |
| 258 |
testutils.SkipIfNotSupported(t, err) |
| 259 |
qt.Assert(t, qt.IsNil(err)) |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
if addrs, ok := info.JitedKsymAddrs(); testutils.IsVersionLessThan(t, "4.18") { |
| 264 |
qt.Assert(t, qt.IsFalse(ok)) |
| 265 |
} else { |
| 266 |
qt.Assert(t, qt.IsTrue(ok)) |
| 267 |
qt.Assert(t, qt.IsTrue(len(addrs) > 0)) |
| 268 |
} |
| 269 |
|
| 270 |
if lens, ok := info.JitedFuncLens(); testutils.IsVersionLessThan(t, "4.18") { |
| 271 |
qt.Assert(t, qt.IsFalse(ok)) |
| 272 |
} else { |
| 273 |
qt.Assert(t, qt.IsTrue(ok)) |
| 274 |
qt.Assert(t, qt.IsTrue(len(lens) > 0)) |
| 275 |
} |
| 276 |
|
| 277 |
if infos, ok := info.JitedLineInfos(); testutils.IsVersionLessThan(t, "5.0") { |
| 278 |
qt.Assert(t, qt.IsFalse(ok)) |
| 279 |
} else { |
| 280 |
qt.Assert(t, qt.IsTrue(ok)) |
| 281 |
qt.Assert(t, qt.IsTrue(len(infos) > 0)) |
| 282 |
} |
| 283 |
|
| 284 |
if funcs, err := info.FuncInfos(); testutils.IsVersionLessThan(t, "5.0") { |
| 285 |
qt.Assert(t, qt.IsNotNil(err)) |
| 286 |
} else { |
| 287 |
qt.Assert(t, qt.IsNil(err)) |
| 288 |
qt.Assert(t, qt.HasLen(funcs, 2)) |
| 289 |
qt.Assert(t, qt.ContentEquals(funcs[0].Func, btfFn)) |
| 290 |
qt.Assert(t, qt.ContentEquals(funcs[1].Func, btfFn)) |
| 291 |
} |
| 292 |
|
| 293 |
if lines, err := info.LineInfos(); testutils.IsVersionLessThan(t, "5.0") { |
| 294 |
qt.Assert(t, qt.IsNotNil(err)) |
| 295 |
} else { |
| 296 |
qt.Assert(t, qt.IsNil(err)) |
| 297 |
qt.Assert(t, qt.HasLen(lines, 2)) |
| 298 |
qt.Assert(t, qt.Equals(lines[0].Line.Line(), "line info")) |
| 299 |
qt.Assert(t, qt.Equals(lines[1].Line.Line(), "line info")) |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
func TestProgramInfoMapIDs(t *testing.T) { |
| 304 |
arr := createMap(t, Array, 1) |
| 305 |
|
| 306 |
prog := mustNewProgram(t, &ProgramSpec{ |
| 307 |
Type: SocketFilter, |
| 308 |
Instructions: asm.Instructions{ |
| 309 |
asm.LoadMapPtr(asm.R0, arr.FD()), |
| 310 |
asm.LoadImm(asm.R0, 2, asm.DWord), |
| 311 |
asm.Return(), |
| 312 |
}, |
| 313 |
License: "MIT", |
| 314 |
}, nil) |
| 315 |
|
| 316 |
info, err := prog.Info() |
| 317 |
testutils.SkipIfNotSupported(t, err) |
| 318 |
qt.Assert(t, qt.IsNil(err)) |
| 319 |
|
| 320 |
ids, ok := info.MapIDs() |
| 321 |
switch { |
| 322 |
case testutils.IsVersionLessThan(t, "4.15", "windows:0.20"): |
| 323 |
qt.Assert(t, qt.IsFalse(ok)) |
| 324 |
qt.Assert(t, qt.HasLen(ids, 0)) |
| 325 |
|
| 326 |
default: |
| 327 |
qt.Assert(t, qt.IsTrue(ok)) |
| 328 |
|
| 329 |
mapInfo, err := arr.Info() |
| 330 |
qt.Assert(t, qt.IsNil(err)) |
| 331 |
|
| 332 |
mapID, ok := mapInfo.ID() |
| 333 |
qt.Assert(t, qt.IsTrue(ok)) |
| 334 |
qt.Assert(t, qt.ContentEquals(ids, []MapID{mapID})) |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
func TestProgramInfoMapIDsNoMaps(t *testing.T) { |
| 339 |
prog := createBasicProgram(t) |
| 340 |
|
| 341 |
info, err := prog.Info() |
| 342 |
testutils.SkipIfNotSupported(t, err) |
| 343 |
qt.Assert(t, qt.IsNil(err)) |
| 344 |
|
| 345 |
ids, ok := info.MapIDs() |
| 346 |
switch { |
| 347 |
case testutils.IsVersionLessThan(t, "4.15", "windows:0.20"): |
| 348 |
qt.Assert(t, qt.IsFalse(ok)) |
| 349 |
qt.Assert(t, qt.HasLen(ids, 0)) |
| 350 |
|
| 351 |
default: |
| 352 |
qt.Assert(t, qt.IsTrue(ok)) |
| 353 |
qt.Assert(t, qt.HasLen(ids, 0)) |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
func TestScanFdInfoReader(t *testing.T) { |
| 358 |
tests := []struct { |
| 359 |
fields map[string]any |
| 360 |
valid bool |
| 361 |
}{ |
| 362 |
{nil, true}, |
| 363 |
{map[string]any{"foo": new(string)}, true}, |
| 364 |
{map[string]any{"zap": new(string)}, false}, |
| 365 |
{map[string]any{"foo": new(int)}, false}, |
| 366 |
} |
| 367 |
|
| 368 |
for _, test := range tests { |
| 369 |
err := scanFdInfoReader(strings.NewReader("foo:\tbar\n"), test.fields) |
| 370 |
if test.valid { |
| 371 |
if err != nil { |
| 372 |
t.Errorf("fields %v returns an error: %s", test.fields, err) |
| 373 |
} |
| 374 |
} else { |
| 375 |
if err == nil { |
| 376 |
t.Errorf("fields %v doesn't return an error", test.fields) |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
func BenchmarkScanFdInfoReader(b *testing.B) { |
| 383 |
b.ReportAllocs() |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
input := strings.Repeat("ignore:\tthis\n", 9) |
| 388 |
input += "foo:\tbar\n" |
| 389 |
r := strings.NewReader(input) |
| 390 |
|
| 391 |
var val string |
| 392 |
fields := map[string]any{"foo": &val} |
| 393 |
|
| 394 |
for b.Loop() { |
| 395 |
val = "" |
| 396 |
r.Reset(input) |
| 397 |
|
| 398 |
if err := scanFdInfoReader(r, fields); err != nil { |
| 399 |
b.Fatal(err) |
| 400 |
} |
| 401 |
if val != "bar" { |
| 402 |
b.Fatal("unexpected value:", val) |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
func TestProgramStats(t *testing.T) { |
| 410 |
testutils.SkipOnOldKernel(t, "5.8", "BPF_ENABLE_STATS") |
| 411 |
|
| 412 |
prog := createBasicProgram(t) |
| 413 |
|
| 414 |
s, err := prog.Stats() |
| 415 |
qt.Assert(t, qt.IsNil(err)) |
| 416 |
|
| 417 |
qt.Assert(t, qt.Equals(s.RunCount, 0)) |
| 418 |
qt.Assert(t, qt.Equals(s.RecursionMisses, 0)) |
| 419 |
|
| 420 |
if runtime.GOARCH != "arm64" { |
| 421 |
|
| 422 |
qt.Assert(t, qt.Equals(s.Runtime, 0)) |
| 423 |
} |
| 424 |
|
| 425 |
if err := testStats(t, prog); err != nil { |
| 426 |
testutils.SkipIfNotSupportedOnOS(t, err) |
| 427 |
t.Error(err) |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
func BenchmarkStats(b *testing.B) { |
| 433 |
b.ReportAllocs() |
| 434 |
|
| 435 |
testutils.SkipOnOldKernel(b, "5.8", "BPF_ENABLE_STATS") |
| 436 |
|
| 437 |
prog := createBasicProgram(b) |
| 438 |
|
| 439 |
for b.Loop() { |
| 440 |
if err := testStats(b, prog); err != nil { |
| 441 |
testutils.SkipIfNotSupportedOnOS(b, err) |
| 442 |
b.Fatal(err) |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
func testStats(tb testing.TB, prog *Program) error { |
| 458 |
tb.Helper() |
| 459 |
|
| 460 |
in := internal.EmptyBPFContext |
| 461 |
|
| 462 |
stats, err := EnableStats(uint32(sys.BPF_STATS_RUN_TIME)) |
| 463 |
if err != nil { |
| 464 |
return fmt.Errorf("failed to enable stats: %w", err) |
| 465 |
} |
| 466 |
defer stats.Close() |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
mustRun(tb, prog, &RunOptions{Data: in}) |
| 471 |
|
| 472 |
s1, err := prog.Stats() |
| 473 |
qt.Assert(tb, qt.IsNil(err)) |
| 474 |
|
| 475 |
qt.Assert(tb, qt.Not(qt.Equals(s1.RunCount, 0)), qt.Commentf("expected run count to be at least 1 after first invocation")) |
| 476 |
qt.Assert(tb, qt.Not(qt.Equals(s1.Runtime, 0)), qt.Commentf("expected runtime to be at least 1ns after first invocation")) |
| 477 |
|
| 478 |
qt.Assert(tb, qt.IsNil(stats.Close())) |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
mustRun(tb, prog, &RunOptions{Data: in}) |
| 483 |
|
| 484 |
s2, err := prog.Stats() |
| 485 |
qt.Assert(tb, qt.IsNil(err)) |
| 486 |
|
| 487 |
qt.Assert(tb, qt.Equals(s2.RunCount, s1.RunCount), qt.Commentf("run count (%d) increased after first invocation (%d)", s2.RunCount, s1.RunCount)) |
| 488 |
qt.Assert(tb, qt.Equals(s2.Runtime, s1.Runtime), qt.Commentf("runtime (%d) increased after first invocation (%d)", s2.Runtime, s1.Runtime)) |
| 489 |
|
| 490 |
return nil |
| 491 |
} |
| 492 |
|
| 493 |
func TestHaveProgramInfoMapIDs(t *testing.T) { |
| 494 |
testutils.CheckFeatureTest(t, haveProgramInfoMapIDs) |
| 495 |
} |
| 496 |
|
| 497 |
func TestProgInfoExtBTF(t *testing.T) { |
| 498 |
testutils.SkipOnOldKernel(t, "5.0", "Program BTF (func/line_info)") |
| 499 |
|
| 500 |
spec, err := LoadCollectionSpec(testutils.NativeFile(t, "testdata/loader-%s.elf")) |
| 501 |
if err != nil { |
| 502 |
t.Fatal(err) |
| 503 |
} |
| 504 |
|
| 505 |
var obj struct { |
| 506 |
Main *Program `ebpf:"xdp_prog"` |
| 507 |
} |
| 508 |
|
| 509 |
err = loadAndAssign(t, spec, &obj, nil) |
| 510 |
testutils.SkipIfNotSupported(t, err) |
| 511 |
if err != nil { |
| 512 |
t.Fatal(err) |
| 513 |
} |
| 514 |
defer obj.Main.Close() |
| 515 |
|
| 516 |
info, err := obj.Main.Info() |
| 517 |
if err != nil { |
| 518 |
t.Fatal(err) |
| 519 |
} |
| 520 |
|
| 521 |
inst, err := info.Instructions() |
| 522 |
if err != nil { |
| 523 |
t.Fatal(err) |
| 524 |
} |
| 525 |
|
| 526 |
expectedLineInfoCount := 28 |
| 527 |
expectedFuncInfo := map[string]bool{ |
| 528 |
"xdp_prog": false, |
| 529 |
"static_fn": false, |
| 530 |
"global_fn2": false, |
| 531 |
"global_fn3": false, |
| 532 |
} |
| 533 |
|
| 534 |
lineInfoCount := 0 |
| 535 |
|
| 536 |
for _, ins := range inst { |
| 537 |
if ins.Source() != nil { |
| 538 |
lineInfoCount++ |
| 539 |
} |
| 540 |
|
| 541 |
fn := btf.FuncMetadata(&ins) |
| 542 |
if fn != nil { |
| 543 |
expectedFuncInfo[fn.Name] = true |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
if lineInfoCount != expectedLineInfoCount { |
| 548 |
t.Errorf("expected %d line info entries, got %d", expectedLineInfoCount, lineInfoCount) |
| 549 |
} |
| 550 |
|
| 551 |
for fn, found := range expectedFuncInfo { |
| 552 |
if !found { |
| 553 |
t.Errorf("func %q not found", fn) |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
func TestInfoExportedFields(t *testing.T) { |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
var names []string |
| 563 |
for _, field := range reflect.VisibleFields(reflect.TypeFor[MapInfo]()) { |
| 564 |
if field.IsExported() { |
| 565 |
names = append(names, field.Name) |
| 566 |
} |
| 567 |
} |
| 568 |
qt.Assert(t, qt.ContentEquals(names, []string{ |
| 569 |
"Type", |
| 570 |
"KeySize", |
| 571 |
"ValueSize", |
| 572 |
"MaxEntries", |
| 573 |
"Flags", |
| 574 |
"Name", |
| 575 |
})) |
| 576 |
|
| 577 |
names = nil |
| 578 |
for _, field := range reflect.VisibleFields(reflect.TypeFor[ProgramInfo]()) { |
| 579 |
if field.IsExported() { |
| 580 |
names = append(names, field.Name) |
| 581 |
} |
| 582 |
} |
| 583 |
qt.Assert(t, qt.ContentEquals(names, []string{ |
| 584 |
"Type", |
| 585 |
"Tag", |
| 586 |
"Name", |
| 587 |
})) |
| 588 |
} |
| 589 |
|
| 590 |
func TestZero(t *testing.T) { |
| 591 |
var ( |
| 592 |
empty = "" |
| 593 |
nul uint32 = 0 |
| 594 |
one uint32 = 1 |
| 595 |
|
| 596 |
iempty any = "" |
| 597 |
inul any = uint32(0) |
| 598 |
ione any = uint32(1) |
| 599 |
) |
| 600 |
|
| 601 |
qt.Assert(t, qt.IsTrue(zero(empty))) |
| 602 |
qt.Assert(t, qt.IsTrue(zero(nul))) |
| 603 |
qt.Assert(t, qt.IsFalse(zero(one))) |
| 604 |
|
| 605 |
qt.Assert(t, qt.IsTrue(zero(&empty))) |
| 606 |
qt.Assert(t, qt.IsTrue(zero(&nul))) |
| 607 |
qt.Assert(t, qt.IsFalse(zero(&one))) |
| 608 |
|
| 609 |
qt.Assert(t, qt.IsTrue(zero(iempty))) |
| 610 |
qt.Assert(t, qt.IsTrue(zero(inul))) |
| 611 |
qt.Assert(t, qt.IsFalse(zero(ione))) |
| 612 |
|
| 613 |
qt.Assert(t, qt.IsTrue(zero(&iempty))) |
| 614 |
qt.Assert(t, qt.IsTrue(zero(&inul))) |
| 615 |
qt.Assert(t, qt.IsFalse(zero(&ione))) |
| 616 |
} |