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

     1  // Copyright 2017-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 systembooter
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"testing"
    11  )
    12  
    13  func TestGetBooterForNetBooter(t *testing.T) {
    14  	validConfig := BootEntry{
    15  		Name:   "Boot0000",
    16  		Config: []byte(`{"type": "netboot", "method": "dhcpv6", "mac": "aa:bb:cc:dd:ee:ff"}`),
    17  	}
    18  	booter := GetBooterFor(validConfig)
    19  	if booter == nil {
    20  		t.Fatalf(`GetBooterFor(validConfig) = %v, want not nil`, booter)
    21  	}
    22  	if booter.TypeName() != "netboot" {
    23  		t.Errorf(`GetBooterFor(validConfig).TypeName() = %q, want "netboot"`, booter.TypeName())
    24  	}
    25  	if booter.(*NetBooter) == nil {
    26  		t.Errorf(`booter.(*NetBooter) = %v, want not nil`, booter.(*NetBooter))
    27  	}
    28  }
    29  
    30  func TestGetBooterForNullBooter(t *testing.T) {
    31  	validConfig := BootEntry{
    32  		Name:   "Boot0000",
    33  		Config: []byte(`{"type": "null"}`),
    34  	}
    35  	booter := GetBooterFor(validConfig)
    36  	if booter == nil {
    37  		t.Fatalf(`GetBooterFor(validConfig) = %v, want not nil`, booter)
    38  	}
    39  	if booter.TypeName() != "null" {
    40  		t.Errorf(`GetBooterFor(validConfig).TypeName() = %q, want "null"`, booter.TypeName())
    41  	}
    42  	if booter.(*NullBooter) == nil {
    43  		t.Errorf(`booter.(*NetBooter) = %v, want not nil`, booter.(*NetBooter))
    44  	}
    45  	if booter.Boot(true) != nil {
    46  		t.Errorf(`booter.Boot(true) = %v, want nil`, booter.Boot(true))
    47  	}
    48  }
    49  
    50  func TestGetBooterForInvalidBooter(t *testing.T) {
    51  	invalidConfig := BootEntry{
    52  		Name:   "Boot0000",
    53  		Config: []byte(`{"type": "invalid"`),
    54  	}
    55  	booter := GetBooterFor(invalidConfig)
    56  
    57  	if booter == nil {
    58  		t.Fatalf(`GetBooterFor(invalidConfig) = %v, want not nil`, booter)
    59  	}
    60  	// an invalid config returns always a NullBooter
    61  	if booter.TypeName() != "null" {
    62  		t.Errorf(`GetBooterFor(invalidConfig).TypeName() = %q, want "null"`, booter.TypeName())
    63  	}
    64  	if booter.(*NullBooter) == nil {
    65  		t.Errorf(`booter.(*NetBooter) = %v, want not nil`, booter.(*NetBooter))
    66  	}
    67  	if booter.Boot(true) != nil {
    68  		t.Errorf(`booter.Boot(true) = %v, want nil`, booter.Boot(true))
    69  	}
    70  }
    71  
    72  func TestGetBootEntries(t *testing.T) {
    73  	var (
    74  		bootConfig0000 = []byte(`{"type": "netboot", "method": "dhcpv6", "mac": "aa:bb:cc:dd:ee:ff"}`)
    75  		bootConfig0001 = []byte(`{"type": "localboot", "uuid": "blah-bleh", "kernel": "/path/to/kernel"}`)
    76  	)
    77  	// Override the package-level variable Get so it will use our test getter
    78  	// instead of VPD
    79  	Get = func(key string, readOnly bool) ([]byte, error) {
    80  		switch key {
    81  		case "Boot0000":
    82  			return bootConfig0000, nil
    83  		case "Boot0001":
    84  			return bootConfig0001, nil
    85  		default:
    86  			return nil, errors.New("No such key")
    87  		}
    88  	}
    89  	entries := GetBootEntries()
    90  	if len(entries) != 2 {
    91  		t.Errorf(`len(entries) = %d, want "2"`, len(entries))
    92  	}
    93  	if entries[0].Name != "Boot0000" {
    94  		t.Errorf(`entries[0].Name = %q, want "Boot0000"`, entries[0].Name)
    95  	}
    96  	if !bytes.Equal(entries[0].Config, bootConfig0000) {
    97  		t.Errorf(`entries[0].Config = %v, want %v`, entries[0].Config, bootConfig0000)
    98  	}
    99  	if entries[1].Name != "Boot0001" {
   100  		t.Errorf(`entries[1].Name = %q, want "Boot0001"`, entries[1].Name)
   101  	}
   102  	if !bytes.Equal(entries[1].Config, bootConfig0001) {
   103  		t.Errorf(`entries[1].Config = %v, want %v`, entries[1].Config, bootConfig0001)
   104  	}
   105  }
   106  
   107  func TestGetBootEntriesOnlyRO(t *testing.T) {
   108  	// Override the package-level variable Get so it will use our test getter
   109  	// instead of VPD
   110  	Get = func(key string, readOnly bool) ([]byte, error) {
   111  		if !readOnly || key != "Boot0000" {
   112  			return nil, errors.New("No such key")
   113  		}
   114  		return []byte(`{"type": "netboot", "method": "dhcpv6", "mac": "aa:bb:cc:dd:ee:ff"}`), nil
   115  	}
   116  	entries := GetBootEntries()
   117  	if len(entries) != 1 {
   118  		t.Errorf(`len(entries) = %d, want "1"`, len(entries))
   119  	}
   120  }