github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/uefivars/boot/efiDevicePathProtocol_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  	"testing"
    12  
    13  	"github.com/u-root/u-root/pkg/uefivars"
    14  )
    15  
    16  var boot7 = []byte{
    17  	0x01, 0x00, 0x00, 0x00, 0x5e, 0x00, 0x55, 0x00, 0x45, 0x00, 0x46, 0x00,
    18  	0x49, 0x00, 0x20, 0x00, 0x4f, 0x00, 0x53, 0x00, 0x00, 0x00, 0x04, 0x01,
    19  	0x2a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
    20  	0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcd, 0x5c,
    21  	0x63, 0x81, 0x4f, 0x1b, 0x3f, 0x4d, 0xb7, 0xb8, 0xf7, 0x8a, 0x5b, 0x02,
    22  	0x9f, 0x35, 0x02, 0x02, 0x04, 0x04, 0x30, 0x00, 0x5c, 0x00, 0x45, 0x00,
    23  	0x46, 0x00, 0x49, 0x00, 0x5c, 0x00, 0x42, 0x00, 0x4f, 0x00, 0x4f, 0x00,
    24  	0x54, 0x00, 0x5c, 0x00, 0x42, 0x00, 0x4f, 0x00, 0x4f, 0x00, 0x54, 0x00,
    25  	0x58, 0x00, 0x36, 0x00, 0x34, 0x00, 0x2e, 0x00, 0x45, 0x00, 0x46, 0x00,
    26  	0x49, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x04, 0x00, 0x00, 0x00, 0x42, 0x4f,
    27  }
    28  
    29  //func ParseFilePathList(in []byte) (EfiDevicePathProtocolList, error)
    30  func TestParseFilePathList(t *testing.T) {
    31  	// When this test runs, you will see log entries like
    32  	// "Skipping loop0: open /dev/loop0: permission denied"
    33  	// These entries are safe to ignore, unless you ran as root (!) in which
    34  	// case the devices ought to be readable.
    35  	e := uefivars.EfiVar{
    36  		Uuid: BootUUID,
    37  		Name: "Boot0007",
    38  		Data: boot7,
    39  	}
    40  	b := BootVar(e)
    41  
    42  	//same as efibootmgr output, except using forward slashes
    43  	wantpath := "HD(1,GPT,81635ccd-1b4f-4d3f-b7b8-f78a5b029f35,0x40,0xf000)/File(/EFI/BOOT/BOOTX64.EFI)"
    44  	gotpath := b.FilePathList.String()
    45  	if gotpath != wantpath {
    46  		t.Errorf("mismatch\nwant %q\n got %q", wantpath, gotpath)
    47  	}
    48  	wantstr := `Boot0007: attrs=0x1, desc="UEFI OS", path=HD(1,GPT,81635ccd-1b4f-4d3f-b7b8-f78a5b029f35,0x40,0xf000)/File(/EFI/BOOT/BOOTX64.EFI), opts=00e4bd82`
    49  	gotstr := b.String()
    50  	if wantstr != gotstr {
    51  		t.Errorf("mismatch\nwant %s\n got %s", wantstr, gotstr)
    52  	}
    53  
    54  	wantdesc := `Boot0007: attrs=0x1, desc="UEFI OS", path=HD(1,GPT,81635ccd-1b4f-4d3f-b7b8-f78a5b029f35,0x40,0xf000)/File(/EFI/BOOT/BOOTX64.EFI), opts=00e4bd82`
    55  	gotdesc := b.String()
    56  	if gotdesc != wantdesc {
    57  		t.Errorf("mismatch\nwant %s\n got %s", wantdesc, gotdesc)
    58  	}
    59  	expectedOutput := "Described device not found\n/EFI/BOOT/BOOTX64.EFI\n"
    60  	resolveFailed := false
    61  	var output string
    62  	for _, p := range b.FilePathList {
    63  		r, err := p.Resolver()
    64  		if err != nil {
    65  			resolveFailed = true
    66  			output += err.Error() + "\n"
    67  		} else {
    68  			output += r.String() + "\n"
    69  		}
    70  	}
    71  	if !resolveFailed {
    72  		t.Error("resolve should fail - the chances of a device matching the guid are infinitesimally small")
    73  	}
    74  	if output != expectedOutput {
    75  		t.Errorf("\nwant %s\n got %s", expectedOutput, output)
    76  	}
    77  }