gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/uefivars/boot/boot_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  	"os"
    12  	"testing"
    13  
    14  	"github.com/u-root/u-root/pkg/uefivars"
    15  	"github.com/u-root/u-root/pkg/uefivars/vartest"
    16  )
    17  
    18  // main is needed to extract the testdata from a zip to temp dir, and to clean
    19  // up the temp dir after
    20  func TestMain(m *testing.M) {
    21  	efiVarDir, cleanup, err := vartest.SetupVarZip("../testdata/sys_fw_efi_vars.zip")
    22  	if err != nil {
    23  		panic(err)
    24  	}
    25  	defer cleanup()
    26  	uefivars.EfiVarDir = efiVarDir
    27  	os.Exit(m.Run())
    28  }
    29  
    30  //func ReadBootVar(num uint16) (b BootVar)
    31  func TestReadBootVar(t *testing.T) {
    32  	var n uint16
    33  	var strs []string
    34  	for n = 0; n < 11; n++ {
    35  		b, err := ReadBootVar(n)
    36  		if err != nil {
    37  			t.Error(err)
    38  		}
    39  		strs = append(strs, b.String())
    40  	}
    41  	if t.Failed() {
    42  		for _, s := range strs {
    43  			t.Log(s)
    44  		}
    45  	}
    46  }
    47  
    48  //func AllBootEntryVars() (list []BootEntryVar)
    49  func TestAllBootEntryVars(t *testing.T) {
    50  	bevs := AllBootEntryVars()
    51  	if len(bevs) != 11 {
    52  		for i, e := range bevs {
    53  			t.Logf("#%d: %s", i, e)
    54  		}
    55  		t.Errorf("expected 11 boot vars, got %d", len(bevs))
    56  	}
    57  }
    58  
    59  //func AllBootVars() (list EfiVars)
    60  func TestAllBootVars(t *testing.T) {
    61  	n := 14
    62  	bvs := AllBootVars()
    63  	if len(bvs) != n {
    64  		t.Errorf("expected %d boot vars, got %d", n, len(bvs))
    65  	}
    66  	be := bvs.Filter(BootEntryFilter)
    67  	if len(be) != n-3 {
    68  		t.Errorf("expected %d entries, got %d", n-3, len(be))
    69  	}
    70  	//find boot vars that are not boot entries
    71  	nbe := bvs.Filter(uefivars.NotFilter(BootEntryFilter))
    72  	want := []string{"BootCurrent", "BootOptionSupport", "BootOrder"}
    73  	if len(nbe) != len(want) {
    74  		t.Fatalf("want %d got %d", len(want), len(nbe))
    75  	}
    76  	for i, bv := range nbe {
    77  		s := bv.Name
    78  		if i >= len(want) || s != want[i] {
    79  			t.Errorf("%d: %s", i, s)
    80  		}
    81  	}
    82  }
    83  
    84  //func ReadCurrentBootVar() (b *BootEntryVar)
    85  func TestReadCurrentBootVar(t *testing.T) {
    86  	v, err := ReadCurrentBootVar()
    87  	if err != nil {
    88  		t.Error(err)
    89  	}
    90  
    91  	if v == nil {
    92  		t.Fatal("nil")
    93  	}
    94  	if v.Number != 10 {
    95  		t.Errorf("expected 10, got %d", v.Number)
    96  	}
    97  	if t.Failed() {
    98  		t.Log(v)
    99  	}
   100  }
   101  
   102  //func BootCurrent(vars uefivars.EfiVars) *BootCurrentVar
   103  func TestBootCurrent(t *testing.T) {
   104  	bc := BootCurrent(AllBootVars())
   105  	if bc == nil {
   106  		t.Fatal("nil")
   107  	}
   108  	var want uint16 = 10
   109  	if bc.Current != want {
   110  		t.Errorf("want %d got %d", want, bc.Current)
   111  	}
   112  }