blob: 97711fdec34a78687c5aff1e3594d3ee70596605 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Lorenz Brun1cf17952023-02-13 17:41:59 +01004// If this is bootparam we have an import cycle
5package bootparam_test
6
7import (
8 "strings"
9 "testing"
10
11 "github.com/google/go-cmp/cmp"
12
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020013 "source.monogon.dev/osbase/bootparam"
14 "source.monogon.dev/osbase/bootparam/ref"
Lorenz Brun1cf17952023-02-13 17:41:59 +010015)
16
17// Fuzzers can be run with
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020018// bazel test //osbase/bootparam:bootparam_test
Lorenz Brun1cf17952023-02-13 17:41:59 +010019// --test_arg=-test.fuzz=FuzzMarshal
20// --test_arg=-test.fuzzcachedir=/tmp/fuzz
21// --test_arg=-test.fuzztime=60s
22
23func 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
40func 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}