Debian Codegraph @

cmd/bpf2go/gen/output_test.go

raw symbols SCIP
1 //go:build !windows
2
3 package gen
4
5 import (
6 "bytes"
7 "fmt"
8 "strings"
9 "testing"
10
11 "github.com/go-quicktest/qt"
12
13 "github.com/cilium/ebpf/btf"
14 "github.com/cilium/ebpf/cmd/bpf2go/internal"
15 )
16
17 func TestOrderTypes(t *testing.T) {
18 a := &btf.Int{}
19 b := &btf.Int{}
20 c := &btf.Int{}
21
22 for _, test := range []struct {
23 name string
24 in map[btf.Type]string
25 out []btf.Type
26 }{
27 {
28 "order",
29 map[btf.Type]string{
30 a: "foo",
31 b: "bar",
32 c: "baz",
33 },
34 []btf.Type{b, c, a},
35 },
36 } {
37 t.Run(test.name, func(t *testing.T) {
38 result, err := sortTypes(test.in)
39 qt.Assert(t, qt.IsNil(err))
40 qt.Assert(t, qt.Equals(len(result), len(test.out)))
41 for i, o := range test.out {
42 if result[i] != o {
43 t.Fatalf("Index %d: expected %p got %p", i, o, result[i])
44 }
45 }
46 })
47 }
48
49 for _, test := range []struct {
50 name string
51 in map[btf.Type]string
52 }{
53 {
54 "duplicate names",
55 map[btf.Type]string{
56 a: "foo",
57 b: "foo",
58 },
59 },
60 } {
61 t.Run(test.name, func(t *testing.T) {
62 result, err := sortTypes(test.in)
63 qt.Assert(t, qt.IsNotNil(err))
64 qt.Assert(t, qt.IsNil(result))
65 })
66 }
67 }
68
69 func TestPackageImport(t *testing.T) {
70 var buf bytes.Buffer
71 err := Generate(GenerateArgs{
72 Package: "foo",
73 Stem: "bar",
74 ObjectFile: "frob.o",
75 Output: &buf,
76 })
77 qt.Assert(t, qt.IsNil(err))
78 // NB: It'd be great to test that this is the case for callers outside of
79 // this module, but that is kind of tricky.
80 qt.Assert(t, qt.StringContains(buf.String(), fmt.Sprintf(`"%s"`, internal.CurrentModule)))
81 }
82
83 func TestCustomIdentifier(t *testing.T) {
84 var buf bytes.Buffer
85 args := GenerateArgs{
86 Package: "foo",
87 Stem: "bar",
88 ObjectFile: "frob.o",
89 Output: &buf,
90 Programs: []string{"do_thing"},
91 Identifier: strings.ToUpper,
92 }
93 err := Generate(args)
94 qt.Assert(t, qt.IsNil(err))
95 qt.Assert(t, qt.StringContains(buf.String(), "DO_THING"))
96 }
97
98 func TestObjects(t *testing.T) {
99 var buf bytes.Buffer
100 args := GenerateArgs{
101 Package: "foo",
102 Stem: "bar",
103 Maps: []string{"map1"},
104 Variables: []string{"var_1"},
105 Programs: []string{"prog_foo_1"},
106 Output: &buf,
107 }
108 err := Generate(args)
109 qt.Assert(t, qt.IsNil(err))
110
111 str := buf.String()
112
113 qt.Assert(t, qt.StringContains(str, "Map1 *ebpf.MapSpec `ebpf:\"map1\"`"))
114 qt.Assert(t, qt.StringContains(str, "Var1 *ebpf.VariableSpec `ebpf:\"var_1\"`"))
115 qt.Assert(t, qt.StringContains(str, "ProgFoo1 *ebpf.ProgramSpec `ebpf:\"prog_foo_1\"`"))
116
117 qt.Assert(t, qt.StringContains(str, "Map1 *ebpf.Map `ebpf:\"map1\"`"))
118 qt.Assert(t, qt.StringContains(str, "Var1 *ebpf.Variable `ebpf:\"var_1\"`"))
119 qt.Assert(t, qt.StringContains(str, "ProgFoo1 *ebpf.Program `ebpf:\"prog_foo_1\"`"))
120
121 qt.Assert(t, qt.StringContains(str, "barMapMap1 = \"map1\""))
122 qt.Assert(t, qt.StringContains(str, "barVarVar1 = \"var_1\""))
123 qt.Assert(t, qt.StringContains(str, "barProgProgFoo1 = \"prog_foo_1\""))
124 }
125
126 func TestGenerateStructTypes(t *testing.T) {
127 ts := &btf.Struct{
128 Name: "test_struct",
129 Size: 8,
130 Members: []btf.Member{
131 {
132 Name: "field1",
133 Type: &btf.Int{Size: 8, Encoding: btf.Unsigned},
134 Offset: 0,
135 },
136 },
137 }
138 td := &btf.Typedef{
139 Name: "test_typedef",
140 Type: ts,
141 }
142
143 tests := []struct {
144 name string
145 types []btf.Type
146 expected string
147 }{
148 {
149 name: "simple struct",
150 types: []btf.Type{ts},
151 expected: "type stemTestStruct struct {\n\t_ structs.HostLayout\n\tField1 uint64\n}",
152 },
153 {
154 name: "typedef struct",
155 types: []btf.Type{td},
156 expected: "type stemTestTypedef struct {\n\t_ structs.HostLayout\n\tField1 uint64\n}",
157 },
158 }
159
160 for _, tt := range tests {
161 t.Run(tt.name, func(t *testing.T) {
162 var buf bytes.Buffer
163 err := Generate(GenerateArgs{
164 Package: "test",
165 Stem: "stem",
166 Types: tt.types,
167 Output: &buf,
168 Constraints: nil,
169 })
170 qt.Assert(t, qt.IsNil(err))
171
172 str := buf.String()
173 qt.Assert(t, qt.StringContains(str, tt.expected))
174 qt.Assert(t, qt.StringContains(str, "\"structs\""))
175 })
176 }
177 }