File: abi_test.go

package info (click to toggle)
golang-github-cilium-ebpf 0.0~git20200413.48fb86d-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, experimental, sid
  • size: 1,436 kB
  • sloc: ansic: 127; sh: 51; makefile: 20
file content (98 lines) | stat: -rw-r--r-- 1,813 bytes parent folder | download
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
package ebpf

import (
	"testing"

	"github.com/cilium/ebpf/internal/testutils"
)

func TestMapABIEqual(t *testing.T) {
	abi := &MapABI{
		Type:       Array,
		KeySize:    4,
		ValueSize:  2,
		MaxEntries: 3,
		Flags:      1,
	}

	if !abi.Equal(abi) {
		t.Error("Equal returns true when comparing an ABI to itself")
	}

	if abi.Equal(&MapABI{}) {
		t.Error("Equal returns true for different ABIs")
	}
}

func TestMapABIFromProc(t *testing.T) {
	hash, err := NewMap(&MapSpec{
		Type:       Hash,
		KeySize:    4,
		ValueSize:  5,
		MaxEntries: 2,
		Flags:      0x1, // BPF_F_NO_PREALLOC
	})
	if err != nil {
		t.Fatal(err)
	}
	defer hash.Close()

	abi, err := newMapABIFromProc(hash.fd)
	if err != nil {
		t.Fatal("Can't get map ABI:", err)
	}

	if abi.Type != Hash {
		t.Error("Expected Hash, got", abi.Type)
	}

	if abi.KeySize != 4 {
		t.Error("Expected KeySize of 4, got", abi.KeySize)
	}

	if abi.ValueSize != 5 {
		t.Error("Expected ValueSize of 5, got", abi.ValueSize)
	}

	if abi.MaxEntries != 2 {
		t.Error("Expected MaxEntries of 2, got", abi.MaxEntries)
	}

	if abi.Flags != 1 {
		t.Error("Expected Flags to be 1, got", abi.Flags)
	}

	nested, err := NewMap(&MapSpec{
		Type:       ArrayOfMaps,
		KeySize:    4,
		MaxEntries: 2,
		InnerMap: &MapSpec{
			Type:       Array,
			KeySize:    4,
			ValueSize:  4,
			MaxEntries: 2,
		},
	})
	testutils.SkipIfNotSupported(t, err)
	if err != nil {
		t.Fatal(err)
	}
	defer nested.Close()

	_, err = newMapABIFromProc(nested.fd)
	if err != nil {
		t.Fatal("Can't get nested map ABI from /proc:", err)
	}
}

func TestProgramABI(t *testing.T) {
	abi := &ProgramABI{Type: SocketFilter}

	if !abi.Equal(abi) {
		t.Error("Equal returns true when comparing an ABI to itself")
	}

	if abi.Equal(&ProgramABI{}) {
		t.Error("Equal returns true for different ABIs")
	}
}