github.com/linuxboot/fiano@v1.2.0/pkg/uefi/firmwarevolume_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 uefi
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/linuxboot/fiano/pkg/log"
    13  )
    14  
    15  var (
    16  	// FV examples
    17  	emptyFV  = []byte{} // Empty file
    18  	sampleFV []byte     // Sample FV from OVMF
    19  )
    20  
    21  func init() {
    22  	var err error
    23  	sampleFV, err = os.ReadFile("../../integration/roms/ovmfSECFV.fv")
    24  	if err != nil {
    25  		log.Fatalf("%v", err)
    26  	}
    27  }
    28  
    29  func TestNewFirmwareVolume(t *testing.T) {
    30  	var tests = []struct {
    31  		name string
    32  		buf  []byte
    33  		msg  string
    34  	}{
    35  		{"emptyFV", emptyFV, fmt.Sprintf("Firmware Volume size too small: expected %d bytes, got %d",
    36  			FirmwareVolumeMinSize, len(emptyFV))},
    37  		{"sampleFV", sampleFV, ""},
    38  	}
    39  	for _, test := range tests {
    40  		t.Run(test.name, func(t *testing.T) {
    41  			_, err := NewFirmwareVolume(test.buf, 0, false)
    42  			if err == nil && test.msg != "" {
    43  				t.Errorf("Error was not returned, expected %v", test.msg)
    44  			} else if err != nil && err.Error() != test.msg {
    45  				t.Errorf("Mismatched Error returned, expected \n%v\n got \n%v\n", test.msg, err.Error())
    46  			}
    47  		})
    48  	}
    49  }