gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/uefivars/boot/fuzz_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  // SPDX-License-Identifier: BSD-3-Clause
     6  //
     7  
     8  package boot
     9  
    10  import (
    11  	"archive/zip"
    12  	"fmt"
    13  	"io/ioutil"
    14  	"log"
    15  	"os"
    16  	"strings"
    17  	"testing"
    18  )
    19  
    20  // Tests using input from fuzzing runs. Ignores any errors, just checks that the
    21  // inputs do not cause crashes.
    22  //
    23  // To update the input zip after a fuzzing run:
    24  // cd fuzz/corpus
    25  // zip ../../testdata/fuzz_in.zip *
    26  //
    27  // Similarly, the zip can be extracted to use as input corpus. See fuzz.go for
    28  // go-fuzz instructions.
    29  func TestFuzzInputs(t *testing.T) {
    30  	if testing.CoverMode() != "" {
    31  		// NOTE - since this test doesn't validate the outputs, coverage from
    32  		// it is very low value, essentially inflating the coverage numbers.
    33  		t.Skip("this test will inflate coverage")
    34  	}
    35  	//restore log behavior at end
    36  	logOut := log.Writer()
    37  	defer log.SetOutput(logOut)
    38  
    39  	//no logging output for this test, to increase speed
    40  	null, err := os.OpenFile("/dev/null", os.O_WRONLY, 0200)
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	log.SetOutput(null)
    45  	log.SetFlags(0)
    46  
    47  	z, err := zip.OpenReader("testdata/fuzz_in.zip")
    48  	if err != nil {
    49  		t.Error(err)
    50  	}
    51  	defer z.Close()
    52  	for i, zf := range z.File {
    53  		name := fmt.Sprintf("%03d_%s", i, zf.Name[:10])
    54  		t.Run(name, func(t *testing.T) {
    55  			f, err := zf.Open()
    56  			if err != nil {
    57  				t.Error(err)
    58  			}
    59  			data, err := ioutil.ReadAll(f)
    60  			if err != nil {
    61  				t.Error(err)
    62  			}
    63  			//ignore any errors - just catch crashes
    64  			list, err := ParseFilePathList(data)
    65  			if err != nil {
    66  				return
    67  			}
    68  			_ = list.String()
    69  			for _, p := range list {
    70  				r, err := p.Resolver()
    71  				if err == nil {
    72  					_ = r.String()
    73  				}
    74  				if strings.Contains(strings.ToLower(p.String()), "acpi") {
    75  					t.Logf("acpi: %s ", p.String())
    76  				}
    77  			}
    78  		})
    79  	}
    80  }