github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/boot/localboot/grub_test.go (about) 1 // Copyright 2020 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "io" 9 "log" 10 "os" 11 "path/filepath" 12 "testing" 13 14 "github.com/mvdan/u-root-coreutils/pkg/mount/block" 15 ) 16 17 func FuzzParseGrubCfg(f *testing.F) { 18 19 tmpDir := f.TempDir() 20 21 //no log output 22 log.SetOutput(io.Discard) 23 log.SetFlags(0) 24 25 // get seed corpora from testdata_new files 26 seeds, err := filepath.Glob("../../../pkg/boot/grub/testdata_new/*/*/*/grub.cfg") 27 if err != nil { 28 f.Fatalf("failed to find seed corpora files: %v", err) 29 } 30 31 seeds2, err := filepath.Glob("../../../pkg/boot/grub/testdata_new/*/*/grub.cfg") 32 if err != nil { 33 f.Fatalf("failed to find seed corpora files: %v", err) 34 } 35 36 seeds = append(seeds, seeds2...) 37 for _, seed := range seeds { 38 seedBytes, err := os.ReadFile(seed) 39 if err != nil { 40 f.Fatalf("failed read seed corpora from files %v: %v", seed, err) 41 } 42 43 f.Add(string(seedBytes)) 44 } 45 46 f.Fuzz(func(t *testing.T, data string) { 47 if len(data) > 256000 { 48 return 49 } 50 51 blockDevs := block.BlockDevices{&block.BlockDev{Name: tmpDir, FSType: "test", FsUUID: "07338180-4a96-4611-aa6a-a452600e4cfe"}} 52 ParseGrubCfg(grubV2, blockDevs, data, tmpDir) 53 54 }) 55 }