github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/boot/uefi/fv_test.go (about)

     1  // Copyright 2021 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 uefi
     6  
     7  import (
     8  	"os"
     9  	"testing"
    10  )
    11  
    12  func TestFindSecurityCorePEEntry(t *testing.T) {
    13  	dat, err := os.ReadFile("testdata/fv_with_sec.fd")
    14  	if err != nil {
    15  		t.Fatalf("fail to read firmware volume: %v", err)
    16  	}
    17  	offset, err := findSecurityCorePEEntry(dat)
    18  	if err != nil {
    19  		t.Fatalf("fail to find SEC in Firmware Volume: %v", err)
    20  	}
    21  	want := 0x15da0
    22  	if offset != want {
    23  		t.Fatalf("want '%x', get '%x'", want, offset)
    24  	}
    25  }
    26  
    27  func TestFindSecurityCorePEEntryNestedSec(t *testing.T) {
    28  	dat, err := os.ReadFile("testdata/fv_with_nested_sec.fd")
    29  	if err != nil {
    30  		t.Fatalf("fail to read firmware volume: %v", err)
    31  	}
    32  	offset, err := findSecurityCorePEEntry(dat)
    33  	if err != nil {
    34  		t.Fatalf("fail to find SEC in Firmware Volume: %v", err)
    35  	}
    36  	want := 0x160b4
    37  	if offset != want {
    38  		t.Fatalf("want '%x', get '%x'", want, offset)
    39  	}
    40  }
    41  
    42  func TestFindSecurityCorePEEntryNotFound(t *testing.T) {
    43  	dat, err := os.ReadFile("testdata/fv_without_sec.fd")
    44  	if err != nil {
    45  		t.Fatalf("fail to read firmware volume: %v", err)
    46  	}
    47  	_, err = findSecurityCorePEEntry(dat)
    48  	if err == nil {
    49  		t.Fatalf("should not found a sec in uefi_no_sec.fd")
    50  	}
    51  }
    52  
    53  func TestFFSHeaderUnmarshalBinaryFailForSize(t *testing.T) {
    54  	var fh EFIFFSFileHeader
    55  	err := fh.UnmarshalBinary([]byte{0x0})
    56  	want := "invalid entry point stucture length 1"
    57  	if err.Error() != want {
    58  		t.Fatalf("Should be '%s', but get '%v'", want, err)
    59  	}
    60  }
    61  
    62  func TestUnmarshalBinaryFailForSize(t *testing.T) {
    63  	var fvh EFIFirmwareVolumeHeader
    64  	err := fvh.UnmarshalBinary([]byte{0x0})
    65  	want := "invalid entry point stucture length 1"
    66  	if err.Error() != want {
    67  		t.Fatalf("Should be '%s', but get '%v'", want, err)
    68  	}
    69  }
    70  
    71  func TestIncorrectFVHSignature(t *testing.T) {
    72  	dat := []byte{
    73  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    74  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    75  		0x78, 0xe5, 0x8c, 0x8c, 0x3d, 0x8a, 0x1c, 0x4f,
    76  		0x99, 0x35, 0x89, 0x61, 0x85, 0xc3, 0x2d, 0xd3,
    77  		0x00, 0xd0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
    78  		0x5f, 0x56, 0x56, 0x48, 0xff, 0xfe, 0x07, 0x00,
    79  		0x48, 0x00, 0x4e, 0x16, 0x60, 0x00, 0x00, 0x02,
    80  		0x1d, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
    81  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    82  	}
    83  	var fvh EFIFirmwareVolumeHeader
    84  	err := fvh.UnmarshalBinary(dat)
    85  	want := "invalid Signature string \"_VVH\""
    86  	if err.Error() != want {
    87  		t.Fatalf("Should be '%s', but get '%v'", want, err)
    88  	}
    89  }