| 1 |
package kconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"os" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/cilium/ebpf/btf" |
| 9 |
"github.com/cilium/ebpf/internal" |
| 10 |
|
| 11 |
"github.com/go-quicktest/qt" |
| 12 |
) |
| 13 |
|
| 14 |
func BenchmarkParse(b *testing.B) { |
| 15 |
f, err := os.Open("testdata/config-6.2.15-300.fc38.x86_64.gz") |
| 16 |
if err != nil { |
| 17 |
b.Fatal(err) |
| 18 |
} |
| 19 |
defer f.Close() |
| 20 |
|
| 21 |
b.ReportAllocs() |
| 22 |
|
| 23 |
for b.Loop() { |
| 24 |
_, err := Parse(f, nil) |
| 25 |
if err != nil { |
| 26 |
b.Fatal(err) |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func BenchmarkParseFiltered(b *testing.B) { |
| 32 |
f, err := os.Open("testdata/config-6.2.15-300.fc38.x86_64.gz") |
| 33 |
if err != nil { |
| 34 |
b.Fatal(err) |
| 35 |
} |
| 36 |
defer f.Close() |
| 37 |
|
| 38 |
b.ReportAllocs() |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
filter := map[string]struct{}{"CONFIG_ARCH_USE_MEMTEST": {}} |
| 44 |
|
| 45 |
for b.Loop() { |
| 46 |
_, err := Parse(f, filter) |
| 47 |
if err != nil { |
| 48 |
b.Fatal(err) |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestParse(t *testing.T) { |
| 54 |
t.Parallel() |
| 55 |
|
| 56 |
f, err := os.Open("testdata/test.kconfig") |
| 57 |
if err != nil { |
| 58 |
t.Fatal("Error reading /testdata/test.kconfig: ", err) |
| 59 |
} |
| 60 |
defer f.Close() |
| 61 |
|
| 62 |
config, err := Parse(f, nil) |
| 63 |
if err != nil { |
| 64 |
t.Fatal("Error parsing kconfig: ", err) |
| 65 |
} |
| 66 |
|
| 67 |
expected := map[string]string{ |
| 68 |
"CONFIG_TRISTATE": "m", |
| 69 |
"CONFIG_BOOL": "y", |
| 70 |
"CONFIG_CHAR": "100", |
| 71 |
"CONFIG_USHORT": "30000", |
| 72 |
"CONFIG_INT": "123456", |
| 73 |
"CONFIG_ULONG": "0xDEADBEEFC0DE", |
| 74 |
"CONFIG_STR": `"abracad"`, |
| 75 |
"CONFIG_FOO": `"foo"`, |
| 76 |
} |
| 77 |
qt.Assert(t, qt.DeepEquals(config, expected)) |
| 78 |
} |
| 79 |
|
| 80 |
func TestParseFiltered(t *testing.T) { |
| 81 |
t.Parallel() |
| 82 |
|
| 83 |
f, err := os.Open("testdata/test.kconfig") |
| 84 |
if err != nil { |
| 85 |
t.Fatal("Error reading /testdata/test.kconfig: ", err) |
| 86 |
} |
| 87 |
defer f.Close() |
| 88 |
|
| 89 |
filter := map[string]struct{}{"CONFIG_FOO": {}} |
| 90 |
|
| 91 |
config, err := Parse(f, filter) |
| 92 |
if err != nil { |
| 93 |
t.Fatal("Error parsing gzipped kconfig: ", err) |
| 94 |
} |
| 95 |
|
| 96 |
expected := map[string]string{"CONFIG_FOO": `"foo"`} |
| 97 |
qt.Assert(t, qt.DeepEquals(config, expected)) |
| 98 |
} |
| 99 |
|
| 100 |
func TestParseGzipped(t *testing.T) { |
| 101 |
t.Parallel() |
| 102 |
|
| 103 |
f, err := os.Open("testdata/config-6.2.15-300.fc38.x86_64.gz") |
| 104 |
if err != nil { |
| 105 |
t.Fatal("Error reading /testdata/config-6.2.15-300.fc38.x86_64.gz: ", err) |
| 106 |
} |
| 107 |
defer f.Close() |
| 108 |
|
| 109 |
_, err = Parse(f, nil) |
| 110 |
if err != nil { |
| 111 |
t.Fatal("Error parsing gzipped kconfig: ", err) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func TestParseGzippedFiltered(t *testing.T) { |
| 116 |
t.Parallel() |
| 117 |
|
| 118 |
f, err := os.Open("testdata/config-6.2.15-300.fc38.x86_64.gz") |
| 119 |
if err != nil { |
| 120 |
t.Fatal("Error reading /testdata/config-6.2.15-300.fc38.x86_64.gz: ", err) |
| 121 |
} |
| 122 |
defer f.Close() |
| 123 |
|
| 124 |
filter := map[string]struct{}{"CONFIG_HZ": {}} |
| 125 |
|
| 126 |
config, err := Parse(f, filter) |
| 127 |
if err != nil { |
| 128 |
t.Fatal("Error parsing gzipped kconfig: ", err) |
| 129 |
} |
| 130 |
|
| 131 |
expected := map[string]string{"CONFIG_HZ": "1000"} |
| 132 |
qt.Assert(t, qt.DeepEquals(config, expected)) |
| 133 |
} |
| 134 |
|
| 135 |
func TestProcessKconfigBadLine(t *testing.T) { |
| 136 |
t.Parallel() |
| 137 |
|
| 138 |
m := make(map[string]string) |
| 139 |
|
| 140 |
err := processKconfigLine([]byte("CONFIG_FOO"), m, nil) |
| 141 |
qt.Assert(t, qt.IsNotNil(err), qt.Commentf("line has no '='")) |
| 142 |
|
| 143 |
err = processKconfigLine([]byte("CONFIG_FOO="), m, nil) |
| 144 |
qt.Assert(t, qt.IsNotNil(err), qt.Commentf("line has no value")) |
| 145 |
} |
| 146 |
|
| 147 |
func TestPutValue(t *testing.T) { |
| 148 |
t.Parallel() |
| 149 |
|
| 150 |
type testCase struct { |
| 151 |
typ btf.Type |
| 152 |
value string |
| 153 |
expected any |
| 154 |
comment string |
| 155 |
} |
| 156 |
|
| 157 |
cases := []testCase{ |
| 158 |
{ |
| 159 |
typ: &btf.Int{ |
| 160 |
Size: 1, |
| 161 |
Encoding: btf.Bool, |
| 162 |
}, |
| 163 |
value: "n", |
| 164 |
expected: int8(0), |
| 165 |
}, |
| 166 |
{ |
| 167 |
typ: &btf.Int{ |
| 168 |
Size: 1, |
| 169 |
Encoding: btf.Bool, |
| 170 |
}, |
| 171 |
value: "y", |
| 172 |
expected: int8(1), |
| 173 |
}, |
| 174 |
{ |
| 175 |
typ: &btf.Int{ |
| 176 |
Size: 1, |
| 177 |
Encoding: btf.Bool, |
| 178 |
}, |
| 179 |
value: "foo", |
| 180 |
comment: "Bad value", |
| 181 |
}, |
| 182 |
{ |
| 183 |
typ: &btf.Int{}, |
| 184 |
comment: "Encoding is not Bool", |
| 185 |
}, |
| 186 |
{ |
| 187 |
typ: &btf.Int{ |
| 188 |
Encoding: btf.Bool, |
| 189 |
}, |
| 190 |
comment: "Size is not 1", |
| 191 |
}, |
| 192 |
{ |
| 193 |
typ: &btf.Enum{ |
| 194 |
Name: "libbpf_tristate", |
| 195 |
}, |
| 196 |
value: "y", |
| 197 |
expected: int32(TriYes), |
| 198 |
}, |
| 199 |
{ |
| 200 |
typ: &btf.Enum{ |
| 201 |
Name: "libbpf_tristate", |
| 202 |
}, |
| 203 |
value: "n", |
| 204 |
expected: int32(TriNo), |
| 205 |
}, |
| 206 |
{ |
| 207 |
typ: &btf.Enum{ |
| 208 |
Name: "libbpf_tristate", |
| 209 |
}, |
| 210 |
value: "m", |
| 211 |
expected: int32(TriModule), |
| 212 |
}, |
| 213 |
{ |
| 214 |
typ: &btf.Enum{ |
| 215 |
Name: "libbpf_tristate", |
| 216 |
}, |
| 217 |
value: "foo", |
| 218 |
comment: "Bad value", |
| 219 |
}, |
| 220 |
{ |
| 221 |
typ: &btf.Enum{ |
| 222 |
Name: "error", |
| 223 |
}, |
| 224 |
comment: "Enum name is wrong", |
| 225 |
}, |
| 226 |
{ |
| 227 |
typ: &btf.Array{}, |
| 228 |
value: "y", |
| 229 |
comment: "Type is not btf.Int", |
| 230 |
}, |
| 231 |
{ |
| 232 |
typ: &btf.Int{ |
| 233 |
Size: 1, |
| 234 |
}, |
| 235 |
value: "255", |
| 236 |
expected: uint8(255), |
| 237 |
}, |
| 238 |
{ |
| 239 |
typ: &btf.Int{ |
| 240 |
Size: 2, |
| 241 |
}, |
| 242 |
value: "0xcafe", |
| 243 |
expected: uint16(0xcafe), |
| 244 |
}, |
| 245 |
{ |
| 246 |
typ: &btf.Int{ |
| 247 |
Size: 2, |
| 248 |
}, |
| 249 |
value: "0755", |
| 250 |
expected: uint16(0755), |
| 251 |
}, |
| 252 |
{ |
| 253 |
typ: &btf.Int{ |
| 254 |
Size: 4, |
| 255 |
Encoding: btf.Signed, |
| 256 |
}, |
| 257 |
value: "-2147483648", |
| 258 |
expected: int32(-2147483648), |
| 259 |
}, |
| 260 |
{ |
| 261 |
typ: &btf.Int{ |
| 262 |
Size: 4, |
| 263 |
Encoding: btf.Signed, |
| 264 |
}, |
| 265 |
value: "+2147483647", |
| 266 |
expected: int32(+2147483647), |
| 267 |
}, |
| 268 |
{ |
| 269 |
typ: &btf.Int{ |
| 270 |
Size: 4, |
| 271 |
}, |
| 272 |
value: "0xcafec0de", |
| 273 |
expected: uint32(0xcafec0de), |
| 274 |
}, |
| 275 |
{ |
| 276 |
typ: &btf.Int{ |
| 277 |
Size: 8, |
| 278 |
Encoding: btf.Signed, |
| 279 |
}, |
| 280 |
value: "+1000000000000", |
| 281 |
expected: int64(1000000000000), |
| 282 |
}, |
| 283 |
{ |
| 284 |
typ: &btf.Int{ |
| 285 |
Size: 8, |
| 286 |
}, |
| 287 |
value: "1000000000000", |
| 288 |
expected: uint64(1000000000000), |
| 289 |
}, |
| 290 |
{ |
| 291 |
typ: &btf.Int{ |
| 292 |
Size: 1, |
| 293 |
}, |
| 294 |
value: "foo", |
| 295 |
comment: "Value is not an int", |
| 296 |
}, |
| 297 |
{ |
| 298 |
typ: &btf.Array{}, |
| 299 |
value: "1", |
| 300 |
comment: "Type is not btf.Int", |
| 301 |
}, |
| 302 |
{ |
| 303 |
typ: &btf.Int{ |
| 304 |
Size: 16, |
| 305 |
}, |
| 306 |
value: "1", |
| 307 |
comment: "Size is wrong", |
| 308 |
}, |
| 309 |
{ |
| 310 |
typ: &btf.Typedef{ |
| 311 |
Type: &btf.Int{ |
| 312 |
Size: 1, |
| 313 |
}, |
| 314 |
}, |
| 315 |
value: "1", |
| 316 |
expected: uint8(1), |
| 317 |
}, |
| 318 |
{ |
| 319 |
typ: &btf.Array{ |
| 320 |
Type: &btf.Int{ |
| 321 |
Size: 1, |
| 322 |
Encoding: btf.Char, |
| 323 |
}, |
| 324 |
Nelems: 6, |
| 325 |
}, |
| 326 |
value: `"foobar"`, |
| 327 |
expected: []byte("foobar"), |
| 328 |
}, |
| 329 |
{ |
| 330 |
typ: &btf.Array{ |
| 331 |
Type: &btf.Int{ |
| 332 |
Size: 1, |
| 333 |
Encoding: btf.Unsigned, |
| 334 |
}, |
| 335 |
Nelems: 3, |
| 336 |
}, |
| 337 |
value: `"foobar"`, |
| 338 |
expected: []byte("foo"), |
| 339 |
}, |
| 340 |
{ |
| 341 |
typ: &btf.Array{ |
| 342 |
Type: &btf.Int{ |
| 343 |
Size: 1, |
| 344 |
Encoding: btf.Signed, |
| 345 |
}, |
| 346 |
Nelems: 2, |
| 347 |
}, |
| 348 |
value: `"42"`, |
| 349 |
expected: []byte("42"), |
| 350 |
}, |
| 351 |
{ |
| 352 |
typ: &btf.Int{}, |
| 353 |
value: `"foo"`, |
| 354 |
comment: "Type is not btf.Array", |
| 355 |
}, |
| 356 |
{ |
| 357 |
typ: &btf.Array{}, |
| 358 |
value: `"foo"`, |
| 359 |
comment: "Type is not btf.Array of btf.Int", |
| 360 |
}, |
| 361 |
{ |
| 362 |
typ: &btf.Array{ |
| 363 |
Type: &btf.Int{ |
| 364 |
Size: 1, |
| 365 |
Encoding: btf.Bool, |
| 366 |
}, |
| 367 |
}, |
| 368 |
value: `"foo"`, |
| 369 |
comment: "Type is not btf.Array of btf.Int of size 1 which is not btf.Bool", |
| 370 |
}, |
| 371 |
{ |
| 372 |
typ: &btf.Array{ |
| 373 |
Type: &btf.Int{ |
| 374 |
Size: 4, |
| 375 |
Encoding: btf.Char, |
| 376 |
}, |
| 377 |
}, |
| 378 |
value: `"foo"`, |
| 379 |
comment: "Type is not btf.Array of btf.Char of size 1", |
| 380 |
}, |
| 381 |
{ |
| 382 |
typ: &btf.Array{ |
| 383 |
Type: &btf.Int{ |
| 384 |
Size: 1, |
| 385 |
Encoding: btf.Char, |
| 386 |
}, |
| 387 |
}, |
| 388 |
value: `"foo`, |
| 389 |
comment: `Value does not start and end with '"'`, |
| 390 |
}, |
| 391 |
} |
| 392 |
|
| 393 |
for _, c := range cases { |
| 394 |
if len(c.comment) > 0 { |
| 395 |
err := PutValue(make([]byte, 0), c.typ, c.value) |
| 396 |
|
| 397 |
qt.Assert(t, qt.IsNotNil(err), qt.Commentf(c.comment)) |
| 398 |
|
| 399 |
continue |
| 400 |
} |
| 401 |
|
| 402 |
expected, err := binary.Append(nil, internal.NativeEndian, c.expected) |
| 403 |
qt.Assert(t, qt.IsNil(err)) |
| 404 |
|
| 405 |
data := make([]byte, len(expected)) |
| 406 |
err = PutValue(data, c.typ, c.value) |
| 407 |
|
| 408 |
qt.Assert(t, qt.IsNil(err)) |
| 409 |
|
| 410 |
qt.Assert(t, qt.DeepEquals(data, expected)) |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
func TestPutInteger(t *testing.T) { |
| 415 |
t.Parallel() |
| 416 |
|
| 417 |
type testCase struct { |
| 418 |
expected []byte |
| 419 |
integer *btf.Int |
| 420 |
n uint64 |
| 421 |
err bool |
| 422 |
comment string |
| 423 |
} |
| 424 |
|
| 425 |
cases := []testCase{ |
| 426 |
{ |
| 427 |
integer: &btf.Int{Size: 1, Encoding: btf.Unsigned}, |
| 428 |
n: 0x01, |
| 429 |
expected: []byte{0x01}, |
| 430 |
}, |
| 431 |
{ |
| 432 |
integer: &btf.Int{Size: 2, Encoding: btf.Unsigned}, |
| 433 |
n: 0x902a, |
| 434 |
expected: []byte{0x2a, 0x90}, |
| 435 |
}, |
| 436 |
{ |
| 437 |
integer: &btf.Int{Size: 4, Encoding: btf.Unsigned}, |
| 438 |
n: 0x01234567, |
| 439 |
expected: []byte{0x67, 0x45, 0x23, 0x01}, |
| 440 |
}, |
| 441 |
{ |
| 442 |
integer: &btf.Int{Size: 1, Encoding: btf.Signed}, |
| 443 |
n: 0x80, |
| 444 |
err: true, |
| 445 |
comment: "outside of range int8 -128 ~ 127", |
| 446 |
}, |
| 447 |
{ |
| 448 |
integer: &btf.Int{Size: 2, Encoding: btf.Signed}, |
| 449 |
n: 0xabcdabcd, |
| 450 |
err: true, |
| 451 |
comment: "outside of range int16 -32768 ~ 32767", |
| 452 |
}, |
| 453 |
{ |
| 454 |
integer: &btf.Int{Size: 4, Encoding: btf.Signed}, |
| 455 |
n: 0x1234567890, |
| 456 |
err: true, |
| 457 |
comment: "outside of range int32 -2147483648 ~ 2147483647", |
| 458 |
}, |
| 459 |
{ |
| 460 |
integer: &btf.Int{Size: 2, Encoding: btf.Signed}, |
| 461 |
n: 0xffffffffffffffff, |
| 462 |
expected: []byte{0xff, 0xff, 0x00, 0x00}, |
| 463 |
comment: "n means -1", |
| 464 |
}, |
| 465 |
{ |
| 466 |
integer: &btf.Int{Size: 2, Encoding: btf.Signed}, |
| 467 |
n: 0xffffffffffffffff - 0x8000, |
| 468 |
err: true, |
| 469 |
comment: "n means -32768(-MinInt16) - 1 in signed value", |
| 470 |
}, |
| 471 |
{ |
| 472 |
integer: &btf.Int{Size: 2, Encoding: btf.Signed}, |
| 473 |
n: 0x7fff, |
| 474 |
expected: []byte{0xff, 0x7f}, |
| 475 |
comment: "maximum value of int16", |
| 476 |
}, |
| 477 |
{ |
| 478 |
integer: &btf.Int{Size: 2, Encoding: btf.Unsigned}, |
| 479 |
n: 0xffff, |
| 480 |
expected: []byte{0xff, 0xff}, |
| 481 |
}, |
| 482 |
{ |
| 483 |
integer: &btf.Int{Size: 4, Encoding: btf.Unsigned}, |
| 484 |
n: 0xffffffff, |
| 485 |
expected: []byte{0xff, 0xff, 0xff, 0xff}, |
| 486 |
}, |
| 487 |
{ |
| 488 |
integer: &btf.Int{Size: 4, Encoding: btf.Signed}, |
| 489 |
n: 0x80000000, |
| 490 |
err: true, |
| 491 |
comment: "outside of range int32 ~2147483648 ~ 2147483647", |
| 492 |
}, |
| 493 |
{ |
| 494 |
integer: &btf.Int{Size: 4, Encoding: btf.Signed}, |
| 495 |
n: 0xffffffffffffffff - 0x80000000, |
| 496 |
err: true, |
| 497 |
comment: "outside of range int32 ~2147483648 ~ 2147483647", |
| 498 |
}, |
| 499 |
{ |
| 500 |
integer: &btf.Int{Size: 8, Encoding: btf.Unsigned}, |
| 501 |
n: 0xffffffffffffffff, |
| 502 |
expected: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 503 |
}, |
| 504 |
} |
| 505 |
|
| 506 |
for _, c := range cases { |
| 507 |
data := make([]byte, len(c.expected)) |
| 508 |
err := PutInteger(data, c.integer, c.n) |
| 509 |
|
| 510 |
if c.err { |
| 511 |
qt.Assert(t, qt.IsNotNil(err)) |
| 512 |
continue |
| 513 |
} |
| 514 |
|
| 515 |
qt.Assert(t, qt.IsNil(err)) |
| 516 |
qt.Assert(t, qt.DeepEquals(data, c.expected), qt.Commentf(c.comment)) |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
func TestPutIntegerError(t *testing.T) { |
| 521 |
qt.Assert(t, qt.IsNotNil(PutInteger(nil, &btf.Int{Size: 2}, 0)), qt.Commentf("slice too small for int")) |
| 522 |
qt.Assert(t, qt.IsNotNil(PutInteger(nil, &btf.Int{Encoding: btf.Bool}, 2)), qt.Commentf("n too big for bool")) |
| 523 |
} |