blob: a0032a4b4c7fb07d55ba948a10249b59583e2600 [file] [log] [blame]
Lorenz Brun1cf17952023-02-13 17:41:59 +01001// If this is bootparam we have an import cycle
2package bootparam_test
3
4import (
5 "strings"
6 "testing"
7
8 "github.com/google/go-cmp/cmp"
9
10 "source.monogon.dev/metropolis/pkg/bootparam"
11 "source.monogon.dev/metropolis/pkg/bootparam/ref"
12)
13
14// Fuzzers can be run with
15// bazel test //metropolis/pkg/bootparam:bootparam_test
16// --test_arg=-test.fuzz=FuzzMarshal
17// --test_arg=-test.fuzzcachedir=/tmp/fuzz
18// --test_arg=-test.fuzztime=60s
19
20func FuzzUnmarshal(f *testing.F) {
21 f.Add(`initrd="\test\some=value" root=yolo "definitely quoted" ro rootflags=`)
22 f.Fuzz(func(t *testing.T, a string) {
23 refOut, refRest := ref.Parse(a)
24 out, rest, err := bootparam.Unmarshal(a)
25 if err != nil {
26 return
27 }
28 if diff := cmp.Diff(refOut, out); diff != "" {
29 t.Errorf("Parse(%q): params mismatch (-want +got):\n%s", a, diff)
30 }
31 if refRest != rest {
32 t.Errorf("Parse(%q): expected rest to be %q, got %q", a, refRest, rest)
33 }
34 })
35}
36
37func FuzzMarshal(f *testing.F) {
38 // Choose delimiters which mean nothing to the parser
39 f.Add("a:b;assd:9dsf;1234", "some fancy rest")
40 f.Fuzz(func(t *testing.T, paramsRaw string, rest string) {
41 paramsSeparated := strings.Split(paramsRaw, ";")
42 var params bootparam.Params
43 for _, p := range paramsSeparated {
44 a, b, _ := strings.Cut(p, ":")
45 params = append(params, bootparam.Param{Param: a, Value: b})
46 }
47 rest = bootparam.TrimLeftSpace(rest)
48 encoded, err := bootparam.Marshal(params, rest)
49 if err != nil {
50 return // Invalid input
51 }
52 refOut, refRest := ref.Parse(encoded)
53 if diff := cmp.Diff(refOut, params); diff != "" {
54 t.Errorf("Marshal(%q): params mismatch (-want +got):\n%s", paramsRaw, diff)
55 }
56 if refRest != rest {
57 t.Errorf("Parse(%q, %q): expected rest to be %q, got %q", paramsRaw, rest, refRest, rest)
58 }
59 })
60}