]> git.feebdaed.xyz Git - 0xmirror/ebpf.git/commitdiff
info: add test for Map.Info()
authorTimo Beckers <timo@isovalent.com>
Wed, 28 May 2025 09:49:47 +0000 (11:49 +0200)
committerTimo Beckers <ti-mo@users.noreply.github.com>
Tue, 3 Jun 2025 15:17:00 +0000 (17:17 +0200)
Previously, we didn't really have a test that covers all of Map.Info(), only the
procfs-based fallback. This patch fixes that and requires fdinfo to be present
from now on. This was added to the kernel in 4.9.

Signed-off-by: Timo Beckers <timo@isovalent.com>
info_test.go
map_test.go

index 963b15998b3975b73e1df4090e92936b7327c172..6d835a15cbab058dbb283fceac8a0a537a4d7803 100644 (file)
@@ -27,6 +27,14 @@ var btfFn = &btf.Func{
        Linkage: btf.StaticFunc,
 }
 
+var hashMapSpec = &MapSpec{
+       Type:       Hash,
+       KeySize:    4,
+       ValueSize:  5,
+       MaxEntries: 2,
+       Flags:      sys.BPF_F_NO_PREALLOC,
+}
+
 var multiprogSpec = &ProgramSpec{
        Name: "test",
        Type: SocketFilter,
@@ -42,31 +50,44 @@ var multiprogSpec = &ProgramSpec{
        License: "MIT",
 }
 
+func validateMapInfo(t *testing.T, info *MapInfo, spec *MapSpec) {
+       t.Helper()
+
+       qt.Assert(t, qt.Equals(info.Type, spec.Type))
+       qt.Assert(t, qt.Equals(info.KeySize, spec.KeySize))
+       qt.Assert(t, qt.Equals(info.ValueSize, spec.ValueSize))
+       qt.Assert(t, qt.Equals(info.MaxEntries, spec.MaxEntries))
+       qt.Assert(t, qt.Equals(info.Flags, spec.Flags))
+
+       memlock, _ := info.Memlock()
+       qt.Assert(t, qt.Not(qt.Equals(memlock, 0)))
+}
+
+func TestMapInfo(t *testing.T) {
+       m := mustNewMap(t, hashMapSpec, nil)
+
+       info, err := m.Info()
+       qt.Assert(t, qt.IsNil(err))
+
+       validateMapInfo(t, info, hashMapSpec)
+}
+
 func TestMapInfoFromProc(t *testing.T) {
-       hash := mustNewMap(t, &MapSpec{
-               Type:       Hash,
-               KeySize:    4,
-               ValueSize:  5,
-               MaxEntries: 2,
-               Flags:      sys.BPF_F_NO_PREALLOC,
-       }, nil)
+       hash := mustNewMap(t, hashMapSpec, nil)
 
        var info MapInfo
        err := readMapInfoFromProc(hash.fd, &info)
        testutils.SkipIfNotSupported(t, err)
-
        qt.Assert(t, qt.IsNil(err))
-       qt.Assert(t, qt.Equals(info.Type, Hash))
-       qt.Assert(t, qt.Equals(info.KeySize, 4))
-       qt.Assert(t, qt.Equals(info.ValueSize, 5))
-       qt.Assert(t, qt.Equals(info.MaxEntries, 2))
-       qt.Assert(t, qt.Equals(info.Flags, sys.BPF_F_NO_PREALLOC))
+
+       validateMapInfo(t, &info, hashMapSpec)
 }
 
 func TestMapInfoFromProcOuterMap(t *testing.T) {
-       outer := mustNewMap(t, &MapSpec{
+       outer := &MapSpec{
                Type:       ArrayOfMaps,
                KeySize:    4,
+               ValueSize:  4,
                MaxEntries: 2,
                InnerMap: &MapSpec{
                        Type:       Array,
@@ -74,15 +95,15 @@ func TestMapInfoFromProcOuterMap(t *testing.T) {
                        ValueSize:  4,
                        MaxEntries: 2,
                },
-       }, nil)
+       }
+       m := mustNewMap(t, outer, nil)
 
        var info MapInfo
-       err := readMapInfoFromProc(outer.fd, &info)
+       err := readMapInfoFromProc(m.fd, &info)
        testutils.SkipIfNotSupported(t, err)
-
        qt.Assert(t, qt.IsNil(err))
-       qt.Assert(t, qt.Equals(info.KeySize, 4))
-       qt.Assert(t, qt.Equals(info.MaxEntries, 2))
+
+       validateMapInfo(t, &info, outer)
 }
 
 func validateProgInfo(t *testing.T, spec *ProgramSpec, info *ProgramInfo) {
index f48ab226a17c4fb29bbeb88f0a0ce8bd7b6946d0..a94325be8c7463b2d2eb203798ccc78d50c6c2fe 100644 (file)
@@ -1516,6 +1516,10 @@ func TestMapFreeze(t *testing.T) {
        if err := arr.Put(uint32(0), uint32(1)); err == nil {
                t.Error("Freeze doesn't prevent modification from user space")
        }
+
+       info, err := arr.Info()
+       qt.Assert(t, qt.IsNil(err))
+       qt.Assert(t, qt.IsTrue(info.Frozen()))
 }
 
 func TestMapGetNextID(t *testing.T) {