Debian Codegraph @

btf/format_test.go

raw symbols SCIP
1 package btf
2
3 import (
4 "errors"
5 "fmt"
6 "go/format"
7 "math"
8 "strings"
9 "testing"
10 )
11
12 func TestGoTypeDeclaration(t *testing.T) {
13 tests := []struct {
14 typ Type
15 output string
16 }{
17 {&Int{Size: 1}, "type t uint8"},
18 {&Int{Size: 1, Encoding: Bool}, "type t bool"},
19 {&Int{Size: 1, Encoding: Char}, "type t uint8"},
20 {&Int{Size: 2, Encoding: Signed}, "type t int16"},
21 {&Int{Size: 4, Encoding: Signed}, "type t int32"},
22 {&Int{Size: 8}, "type t uint64"},
23 {&Typedef{Name: "frob", Type: &Int{Size: 8}}, "type t uint64"},
24 {&Int{Size: 16}, "type t [16]byte /* uint128 */"},
25 {&Enum{Values: []EnumValue{{"FOO", 32}}, Size: 4}, "type t uint32; const ( tFOO t = 32; )"},
26 {
27 &Enum{
28 Values: []EnumValue{
29 {"MINUS_ONE", math.MaxUint64},
30 {"MINUS_TWO", math.MaxUint64 - 1},
31 },
32 Size: 1,
33 Signed: true,
34 },
35 "type t int8; const ( tMINUS_ONE t = -1; tMINUS_TWO t = -2; )",
36 },
37 {
38 &Struct{
39 Name: "enum literals",
40 Size: 2,
41 Members: []Member{
42 {Name: "enum", Type: &Enum{Values: []EnumValue{{"BAR", 1}}, Size: 2}, Offset: 0},
43 },
44 },
45 "type t struct { _ structs.HostLayout; enum uint16; }",
46 },
47 {&Array{Nelems: 2, Type: &Int{Size: 1}}, "type t [2]uint8"},
48 {
49 &Union{
50 Size: 8,
51 Members: []Member{
52 {Name: "a", Type: &Int{Size: 4}},
53 {Name: "b", Type: &Int{Size: 8}},
54 },
55 },
56 "type t struct { _ structs.HostLayout; a uint32; _ [4]byte; }",
57 },
58 {
59 &Struct{
60 Name: "field padding",
61 Size: 16,
62 Members: []Member{
63 {Name: "frob", Type: &Int{Size: 4}, Offset: 0},
64 {Name: "foo", Type: &Int{Size: 8}, Offset: 8 * 8},
65 },
66 },
67 "type t struct { _ structs.HostLayout; frob uint32; _ [4]byte; foo uint64; }",
68 },
69 {
70 &Struct{
71 Name: "end padding",
72 Size: 16,
73 Members: []Member{
74 {Name: "foo", Type: &Int{Size: 8}, Offset: 0},
75 {Name: "frob", Type: &Int{Size: 4}, Offset: 8 * 8},
76 },
77 },
78 "type t struct { _ structs.HostLayout; foo uint64; frob uint32; _ [4]byte; }",
79 },
80 {
81 &Struct{
82 Name: "bitfield",
83 Size: 8,
84 Members: []Member{
85 {Name: "foo", Type: &Int{Size: 4}, Offset: 0, BitfieldSize: 1},
86 {Name: "frob", Type: &Int{Size: 4}, Offset: 4 * 8},
87 },
88 },
89 "type t struct { _ structs.HostLayout; _ [4]byte /* unsupported bitfield */; frob uint32; }",
90 },
91 {
92 &Struct{
93 Name: "nested",
94 Size: 8,
95 Members: []Member{
96 {
97 Name: "foo",
98 Type: &Struct{
99 Size: 4,
100 Members: []Member{
101 {Name: "bar", Type: &Int{Size: 4}, Offset: 0},
102 },
103 },
104 },
105 {Name: "frob", Type: &Int{Size: 4}, Offset: 4 * 8},
106 },
107 },
108 "type t struct { _ structs.HostLayout; foo struct { _ structs.HostLayout; bar uint32; }; frob uint32; }",
109 },
110 {
111 &Struct{
112 Name: "nested anon union",
113 Size: 8,
114 Members: []Member{
115 {
116 Name: "",
117 Type: &Union{
118 Size: 4,
119 Members: []Member{
120 {Name: "foo", Type: &Int{Size: 4}, Offset: 0},
121 {Name: "bar", Type: &Int{Size: 4}, Offset: 0},
122 },
123 },
124 },
125 },
126 },
127 "type t struct { _ structs.HostLayout; foo uint32; _ [4]byte; }",
128 },
129 {
130 &Datasec{
131 Size: 16,
132 Vars: []VarSecinfo{
133 {&Var{Name: "s", Type: &Int{Size: 2}, Linkage: StaticVar}, 0, 2},
134 {&Var{Name: "g", Type: &Int{Size: 4}, Linkage: GlobalVar}, 4, 4},
135 {&Var{Name: "e", Type: &Int{Size: 8}, Linkage: ExternVar}, 8, 8},
136 },
137 },
138 "type t struct { _ structs.HostLayout; _ [4]byte; g uint32; _ [8]byte; }",
139 },
140 {&Var{Type: &Int{Size: 4}}, "type t uint32"},
141 }
142
143 for _, test := range tests {
144 t.Run(fmt.Sprint(test.typ), func(t *testing.T) {
145 have := mustGoTypeDeclaration(t, test.typ, nil, nil)
146 if have != test.output {
147 t.Errorf("Unexpected output:\n\t-%s\n\t+%s", test.output, have)
148 }
149 })
150 }
151 }
152
153 func TestGoTypeDeclarationNamed(t *testing.T) {
154 e1 := &Enum{Name: "e1", Size: 4}
155 s1 := &Struct{
156 Name: "s1",
157 Size: 4,
158 Members: []Member{
159 {Name: "frob", Type: e1},
160 },
161 }
162 s2 := &Struct{
163 Name: "s2",
164 Size: 4,
165 Members: []Member{
166 {Name: "frood", Type: s1},
167 },
168 }
169 td := &Typedef{Name: "td", Type: e1}
170 arr := &Array{Nelems: 1, Type: td}
171
172 tests := []struct {
173 typ Type
174 named []Type
175 output string
176 }{
177 {e1, []Type{e1}, "type t uint32"},
178 {s1, []Type{e1, s1}, "type t struct { _ structs.HostLayout; frob E1; }"},
179 {s2, []Type{e1}, "type t struct { _ structs.HostLayout; frood struct { _ structs.HostLayout; frob E1; }; }"},
180 {s2, []Type{e1, s1}, "type t struct { _ structs.HostLayout; frood S1; }"},
181 {td, nil, "type t uint32"},
182 {td, []Type{td}, "type t uint32"},
183 {arr, []Type{td}, "type t [1]TD"},
184 }
185
186 for _, test := range tests {
187 t.Run(fmt.Sprint(test.typ), func(t *testing.T) {
188 names := make(map[Type]string)
189 for _, t := range test.named {
190 names[t] = strings.ToUpper(t.TypeName())
191 }
192
193 have := mustGoTypeDeclaration(t, test.typ, names, nil)
194 if have != test.output {
195 t.Errorf("Unexpected output:\n\t-%s\n\t+%s", test.output, have)
196 }
197 })
198 }
199 }
200
201 func TestGoTypeDeclarationQualifiers(t *testing.T) {
202 i := &Int{Size: 4}
203 want := mustGoTypeDeclaration(t, i, nil, nil)
204
205 tests := []struct {
206 typ Type
207 }{
208 {&Volatile{Type: i}},
209 {&Const{Type: i}},
210 {&Restrict{Type: i}},
211 }
212
213 for _, test := range tests {
214 t.Run(fmt.Sprint(test.typ), func(t *testing.T) {
215 have := mustGoTypeDeclaration(t, test.typ, nil, nil)
216 if have != want {
217 t.Errorf("Unexpected output:\n\t-%s\n\t+%s", want, have)
218 }
219 })
220 }
221 }
222
223 func TestGoTypeDeclarationCycle(t *testing.T) {
224 s := &Struct{Name: "cycle"}
225 s.Members = []Member{{Name: "f", Type: s}}
226
227 var gf GoFormatter
228 _, err := gf.TypeDeclaration("t", s)
229 if !errors.Is(err, errNestedTooDeep) {
230 t.Fatal("Expected errNestedTooDeep, got", err)
231 }
232 }
233
234 func TestRejectBogusTypes(t *testing.T) {
235 tests := []struct {
236 typ Type
237 }{
238 {&Struct{
239 Size: 1,
240 Members: []Member{
241 {Name: "foo", Type: &Int{Size: 2}, Offset: 0},
242 },
243 }},
244 {&Int{Size: 2, Encoding: Bool}},
245 {&Int{Size: 1, Encoding: Char | Signed}},
246 {&Int{Size: 2, Encoding: Char}},
247 }
248 for _, test := range tests {
249 t.Run(fmt.Sprint(test.typ), func(t *testing.T) {
250 var gf GoFormatter
251
252 _, err := gf.TypeDeclaration("t", test.typ)
253 if err == nil {
254 t.Fatal("TypeDeclaration does not reject bogus type")
255 }
256 })
257 }
258 }
259
260 func mustGoTypeDeclaration(tb testing.TB, typ Type, names map[Type]string, id func(string) string) string {
261 tb.Helper()
262
263 gf := GoFormatter{
264 Names: names,
265 Identifier: id,
266 }
267
268 have, err := gf.TypeDeclaration("t", typ)
269 if err != nil {
270 tb.Fatal(err)
271 }
272
273 _, err = format.Source([]byte(have))
274 if err != nil {
275 tb.Fatalf("Output can't be formatted: %s\n%s", err, have)
276 }
277
278 return have
279 }