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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
package btf
import (
"errors"
"fmt"
"go/format"
"math"
"strings"
"testing"
)
func TestGoTypeDeclaration(t *testing.T) {
tests := []struct {
typ Type
output string
}{
{&Int{Size: 1}, "type t uint8"},
{&Int{Size: 1, Encoding: Bool}, "type t bool"},
{&Int{Size: 1, Encoding: Char}, "type t uint8"},
{&Int{Size: 2, Encoding: Signed}, "type t int16"},
{&Int{Size: 4, Encoding: Signed}, "type t int32"},
{&Int{Size: 8}, "type t uint64"},
{&Typedef{Name: "frob", Type: &Int{Size: 8}}, "type t uint64"},
{&Int{Size: 16}, "type t [16]byte /* uint128 */"},
{&Enum{Values: []EnumValue{{"FOO", 32}}, Size: 4}, "type t uint32; const ( tFOO t = 32; )"},
{
&Enum{
Values: []EnumValue{
{"MINUS_ONE", math.MaxUint64},
{"MINUS_TWO", math.MaxUint64 - 1},
},
Size: 1,
Signed: true,
},
"type t int8; const ( tMINUS_ONE t = -1; tMINUS_TWO t = -2; )",
},
{
&Struct{
Name: "enum literals",
Size: 2,
Members: []Member{
{Name: "enum", Type: &Enum{Values: []EnumValue{{"BAR", 1}}, Size: 2}, Offset: 0},
},
},
"type t struct { enum uint16; }",
},
{&Array{Nelems: 2, Type: &Int{Size: 1}}, "type t [2]uint8"},
{
&Union{
Size: 8,
Members: []Member{
{Name: "a", Type: &Int{Size: 4}},
{Name: "b", Type: &Int{Size: 8}},
},
},
"type t struct { a uint32; _ [4]byte; }",
},
{
&Struct{
Name: "field padding",
Size: 16,
Members: []Member{
{Name: "frob", Type: &Int{Size: 4}, Offset: 0},
{Name: "foo", Type: &Int{Size: 8}, Offset: 8 * 8},
},
},
"type t struct { frob uint32; _ [4]byte; foo uint64; }",
},
{
&Struct{
Name: "end padding",
Size: 16,
Members: []Member{
{Name: "foo", Type: &Int{Size: 8}, Offset: 0},
{Name: "frob", Type: &Int{Size: 4}, Offset: 8 * 8},
},
},
"type t struct { foo uint64; frob uint32; _ [4]byte; }",
},
{
&Struct{
Name: "bitfield",
Size: 8,
Members: []Member{
{Name: "foo", Type: &Int{Size: 4}, Offset: 0, BitfieldSize: 1},
{Name: "frob", Type: &Int{Size: 4}, Offset: 4 * 8},
},
},
"type t struct { _ [4]byte /* unsupported bitfield */; frob uint32; }",
},
{
&Struct{
Name: "nested",
Size: 8,
Members: []Member{
{
Name: "foo",
Type: &Struct{
Size: 4,
Members: []Member{
{Name: "bar", Type: &Int{Size: 4}, Offset: 0},
},
},
},
{Name: "frob", Type: &Int{Size: 4}, Offset: 4 * 8},
},
},
"type t struct { foo struct { bar uint32; }; frob uint32; }",
},
{
&Struct{
Name: "nested anon union",
Size: 8,
Members: []Member{
{
Name: "",
Type: &Union{
Size: 4,
Members: []Member{
{Name: "foo", Type: &Int{Size: 4}, Offset: 0},
{Name: "bar", Type: &Int{Size: 4}, Offset: 0},
},
},
},
},
},
"type t struct { foo uint32; _ [4]byte; }",
},
{
&Datasec{
Size: 16,
Vars: []VarSecinfo{
{&Var{Name: "s", Type: &Int{Size: 2}, Linkage: StaticVar}, 0, 2},
{&Var{Name: "g", Type: &Int{Size: 4}, Linkage: GlobalVar}, 4, 4},
{&Var{Name: "e", Type: &Int{Size: 8}, Linkage: ExternVar}, 8, 8},
},
},
"type t struct { _ [4]byte; g uint32; _ [8]byte; }",
},
{&Var{Type: &Int{Size: 4}}, "type t uint32"},
}
for _, test := range tests {
t.Run(fmt.Sprint(test.typ), func(t *testing.T) {
have := mustGoTypeDeclaration(t, test.typ, nil, nil)
if have != test.output {
t.Errorf("Unexpected output:\n\t-%s\n\t+%s", test.output, have)
}
})
}
}
func TestGoTypeDeclarationNamed(t *testing.T) {
e1 := &Enum{Name: "e1", Size: 4}
s1 := &Struct{
Name: "s1",
Size: 4,
Members: []Member{
{Name: "frob", Type: e1},
},
}
s2 := &Struct{
Name: "s2",
Size: 4,
Members: []Member{
{Name: "frood", Type: s1},
},
}
td := &Typedef{Name: "td", Type: e1}
arr := &Array{Nelems: 1, Type: td}
tests := []struct {
typ Type
named []Type
output string
}{
{e1, []Type{e1}, "type t uint32"},
{s1, []Type{e1, s1}, "type t struct { frob E1; }"},
{s2, []Type{e1}, "type t struct { frood struct { frob E1; }; }"},
{s2, []Type{e1, s1}, "type t struct { frood S1; }"},
{td, nil, "type t uint32"},
{td, []Type{td}, "type t uint32"},
{arr, []Type{td}, "type t [1]TD"},
}
for _, test := range tests {
t.Run(fmt.Sprint(test.typ), func(t *testing.T) {
names := make(map[Type]string)
for _, t := range test.named {
names[t] = strings.ToUpper(t.TypeName())
}
have := mustGoTypeDeclaration(t, test.typ, names, nil)
if have != test.output {
t.Errorf("Unexpected output:\n\t-%s\n\t+%s", test.output, have)
}
})
}
}
func TestGoTypeDeclarationQualifiers(t *testing.T) {
i := &Int{Size: 4}
want := mustGoTypeDeclaration(t, i, nil, nil)
tests := []struct {
typ Type
}{
{&Volatile{Type: i}},
{&Const{Type: i}},
{&Restrict{Type: i}},
}
for _, test := range tests {
t.Run(fmt.Sprint(test.typ), func(t *testing.T) {
have := mustGoTypeDeclaration(t, test.typ, nil, nil)
if have != want {
t.Errorf("Unexpected output:\n\t-%s\n\t+%s", want, have)
}
})
}
}
func TestGoTypeDeclarationCycle(t *testing.T) {
s := &Struct{Name: "cycle"}
s.Members = []Member{{Name: "f", Type: s}}
var gf GoFormatter
_, err := gf.TypeDeclaration("t", s)
if !errors.Is(err, errNestedTooDeep) {
t.Fatal("Expected errNestedTooDeep, got", err)
}
}
func TestRejectBogusTypes(t *testing.T) {
tests := []struct {
typ Type
}{
{&Struct{
Size: 1,
Members: []Member{
{Name: "foo", Type: &Int{Size: 2}, Offset: 0},
},
}},
{&Int{Size: 2, Encoding: Bool}},
{&Int{Size: 1, Encoding: Char | Signed}},
{&Int{Size: 2, Encoding: Char}},
}
for _, test := range tests {
t.Run(fmt.Sprint(test.typ), func(t *testing.T) {
var gf GoFormatter
_, err := gf.TypeDeclaration("t", test.typ)
if err == nil {
t.Fatal("TypeDeclaration does not reject bogus type")
}
})
}
}
func mustGoTypeDeclaration(tb testing.TB, typ Type, names map[Type]string, id func(string) string) string {
tb.Helper()
gf := GoFormatter{
Names: names,
Identifier: id,
}
have, err := gf.TypeDeclaration("t", typ)
if err != nil {
tb.Fatal(err)
}
_, err = format.Source([]byte(have))
if err != nil {
tb.Fatalf("Output can't be formatted: %s\n%s", err, have)
}
return have
}
|