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