github.com/linuxboot/fiano@v1.2.0/pkg/visitors/find_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  	"testing"
     9  
    10  	"github.com/linuxboot/fiano/pkg/uefi"
    11  )
    12  
    13  func TestFind(t *testing.T) {
    14  	f := parseImage(t)
    15  	results := find(t, f, testGUID)
    16  
    17  	// We expect one match
    18  	if len(results) != 1 {
    19  		t.Fatalf("got %d matches; expected 1", len(results))
    20  	}
    21  }
    22  
    23  func TestFindExactlyOne(t *testing.T) {
    24  	f := parseImage(t)
    25  	_, err := FindExactlyOne(f, func(_ uefi.Firmware) bool {
    26  		return true
    27  	})
    28  	if err == nil {
    29  		t.Errorf("should have an error from matching too many, got no error")
    30  	}
    31  
    32  	_, err = FindExactlyOne(f, func(_ uefi.Firmware) bool {
    33  		return false
    34  	})
    35  	if err == nil {
    36  		t.Errorf("should have an error from matching none, got no error")
    37  	}
    38  
    39  	pred := FindFileTypePredicate(uefi.FVFileTypeDXECore)
    40  	res, err := FindExactlyOne(f, pred)
    41  	if err != nil {
    42  		t.Fatalf("should match one Dxe Core, got: %v", err)
    43  	}
    44  
    45  	dxecore, ok := res.(*uefi.File)
    46  	if !ok {
    47  		t.Fatalf("result was not a file, got %T", res)
    48  	}
    49  	if dxecore.Header.Type != uefi.FVFileTypeDXECore {
    50  		t.Errorf("result was not the correct file type! got %v", dxecore.Header.Type)
    51  	}
    52  }
    53  
    54  func TestFindDXEFV(t *testing.T) {
    55  	f := parseImage(t)
    56  	fv, err := FindDXEFV(f)
    57  	if err != nil {
    58  		t.Fatalf("should return one dxe FV, got err: %v", err)
    59  	}
    60  
    61  	if fv == nil {
    62  		t.Fatalf("got nil fv")
    63  	}
    64  
    65  	// Search through files for dxecore
    66  	var found bool
    67  	for _, v := range fv.Files {
    68  		if v.Header.Type == uefi.FVFileTypeDXECore {
    69  			found = true
    70  		}
    71  	}
    72  
    73  	if !found {
    74  		t.Errorf("unable to find DXECore in fv's files, this is probably not the DXE firmware volume")
    75  	}
    76  }