github.com/linuxboot/fiano@v1.2.0/pkg/visitors/dxecleaner_test.go (about)

     1  // Copyright 2018 the LinuxBoot 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 visitors
     6  
     7  import (
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/linuxboot/fiano/pkg/guid"
    12  	"github.com/linuxboot/fiano/pkg/uefi"
    13  )
    14  
    15  var (
    16  	testDXE1 = guid.MustParse("93B80004-9FB3-11D4-9A3A-0090273FC14D")
    17  	testDXE2 = guid.MustParse("4B28E4C7-FF36-4E10-93CF-A82159E777C5")
    18  	testDXE3 = guid.MustParse("C8339973-A563-4561-B858-D8476F9DEFC4")
    19  	testDXE4 = guid.MustParse("378D7B65-8DA9-4773-B6E4-A47826A833E1")
    20  	testDXE5 = guid.MustParse("33CB97AF-6C33-4C42-986B-07581FA366D4")
    21  )
    22  
    23  func contains(t *testing.T, f uefi.Firmware, g *guid.GUID) bool {
    24  	return len(find(t, f, g)) > 0
    25  }
    26  
    27  func TestDXECleaner(t *testing.T) {
    28  	// This test to see if an image "boots" by looking for fake dependencies
    29  	// between the DXEs.
    30  	testDXEDependencies := func(f uefi.Firmware) (bool, error) {
    31  		// Dependencies
    32  		return contains(t, f, testDXE5) &&
    33  			(!contains(t, f, testDXE5) || contains(t, f, testDXE4)) &&
    34  			(!contains(t, f, testDXE2) || contains(t, f, testDXE1)) &&
    35  			(!contains(t, f, testDXE3) || contains(t, f, testDXE2)), nil
    36  	}
    37  
    38  	// Parse image and run the visitor.
    39  	f := parseImage(t)
    40  	dxeCleaner := DXECleaner{
    41  		Test:      testDXEDependencies,
    42  		Predicate: FindFileTypePredicate(uefi.FVFileTypeDriver),
    43  		W:         os.Stdout,
    44  	}
    45  	if err := dxeCleaner.Run(f); err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	// Check that the correct DXEs remain.
    50  	for _, d := range []*guid.GUID{testDXE1, testDXE2, testDXE3} {
    51  		if contains(t, f, d) {
    52  			t.Errorf("expected %v to be deleted", d)
    53  		}
    54  	}
    55  	for _, d := range []*guid.GUID{testDXE4, testDXE5} {
    56  		if !contains(t, f, d) {
    57  			t.Errorf("expected %v to be remain", d)
    58  		}
    59  	}
    60  }
    61  
    62  func TestParseBlackList(t *testing.T) {
    63  	tests := []struct {
    64  		name, input, output string
    65  	}{
    66  		{"empty_file", "", ""},
    67  		{"regex_and_comments", "a.*c \nde\n # comment\nf\n", "(a.*c)|(de)|(f)"},
    68  	}
    69  
    70  	for _, tt := range tests {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			re, err := parseBlackList(tt.name, tt.input)
    73  			if err != nil {
    74  				t.Error(err)
    75  			}
    76  			if re != tt.output {
    77  				t.Errorf("parseBlackList() = %q; want %q", re, tt.output)
    78  			}
    79  		})
    80  	}
    81  }