github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/efivarfs/vars_test.go (about)

     1  // Copyright 2022 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 efivarfs
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"os"
    11  	"testing"
    12  
    13  	guid "github.com/google/uuid"
    14  )
    15  
    16  type fake struct {
    17  	err error
    18  }
    19  
    20  func (f *fake) Get(desc VariableDescriptor) (VariableAttributes, []byte, error) {
    21  	return VariableAttributes(0), make([]byte, 32), f.err
    22  }
    23  
    24  func (f *fake) Set(desc VariableDescriptor, attrs VariableAttributes, data []byte) error {
    25  	return f.err
    26  }
    27  
    28  func (f *fake) Remove(desc VariableDescriptor) error {
    29  	return f.err
    30  }
    31  
    32  var fakeGUID = guid.MustParse("bc54d3fb-ed45-462d-9df8-b9f736228350")
    33  
    34  func (f *fake) List() ([]VariableDescriptor, error) {
    35  
    36  	return []VariableDescriptor{
    37  		{Name: "fake", GUID: fakeGUID},
    38  	}, f.err
    39  }
    40  
    41  var _ EFIVar = &fake{}
    42  
    43  func TestReadVariableErrNoFS(t *testing.T) {
    44  	if _, err := NewPath("/tmp"); !errors.Is(err, ErrNoFS) {
    45  		t.Fatalf(`NewPath("/tmp"): %s != %v`, err, ErrNoFS)
    46  	}
    47  }
    48  
    49  func TestSimpleReadVariable(t *testing.T) {
    50  	var tests = []struct {
    51  		name   string
    52  		val    string
    53  		err    error
    54  		efivar EFIVar
    55  	}{
    56  		{
    57  			name:   "bad variable no -",
    58  			val:    "xy",
    59  			err:    ErrBadGUID,
    60  			efivar: &fake{},
    61  		},
    62  		{
    63  			name:   "bad variable",
    64  			val:    "xy-b-c",
    65  			err:    ErrBadGUID,
    66  			efivar: &fake{},
    67  		},
    68  		{
    69  			name:   "good variable, bad get",
    70  			val:    "WriteOnceStatus-4b3082a3-80c6-4d7e-9cd0-583917265df1",
    71  			err:    os.ErrPermission,
    72  			efivar: &fake{err: os.ErrPermission},
    73  		},
    74  		{
    75  			name:   "good variable, good get",
    76  			val:    "WriteOnceStatus-4b3082a3-80c6-4d7e-9cd0-583917265df1",
    77  			err:    nil,
    78  			efivar: &fake{},
    79  		},
    80  	}
    81  
    82  	for _, tt := range tests {
    83  		_, _, err := SimpleReadVariable(tt.efivar, tt.val)
    84  		if !errors.Is(err, tt.err) {
    85  			t.Errorf("SimpleReadVariable(tt.efivar, %s): %v != %v", tt.val, err, tt.err)
    86  		}
    87  	}
    88  
    89  }
    90  
    91  func TestSimpleWriteVariable(t *testing.T) {
    92  	var tests = []struct {
    93  		name   string
    94  		val    string
    95  		err    error
    96  		efivar EFIVar
    97  	}{
    98  		{
    99  			name:   "bad variable",
   100  			val:    "xy-b-c",
   101  			err:    ErrBadGUID,
   102  			efivar: &fake{},
   103  		},
   104  		{
   105  			name:   "good variable, bad set",
   106  			val:    "WriteOnceStatus-4b3082a3-80c6-4d7e-9cd0-583917265df1",
   107  			err:    os.ErrPermission,
   108  			efivar: &fake{err: os.ErrPermission},
   109  		},
   110  		{
   111  			name:   "good variable, good set",
   112  			val:    "WriteOnceStatus-4b3082a3-80c6-4d7e-9cd0-583917265df1",
   113  			err:    nil,
   114  			efivar: &fake{},
   115  		},
   116  	}
   117  
   118  	for _, tt := range tests {
   119  		err := SimpleWriteVariable(tt.efivar, tt.val, VariableAttributes(0), &bytes.Buffer{})
   120  		if !errors.Is(err, tt.err) {
   121  			t.Errorf("SimpleWriteVariable(tt.efivar, %s): %v != %v", tt.val, err, tt.err)
   122  		}
   123  	}
   124  
   125  }
   126  
   127  func TestSimpleRemoveVariable(t *testing.T) {
   128  	var tests = []struct {
   129  		name   string
   130  		val    string
   131  		err    error
   132  		efivar EFIVar
   133  	}{
   134  		{
   135  			name:   "bad variable",
   136  			val:    "xy-b-c",
   137  			err:    ErrBadGUID,
   138  			efivar: &fake{},
   139  		},
   140  		{
   141  			name:   "good variable, bad Remove",
   142  			val:    "WriteOnceStatus-4b3082a3-80c6-4d7e-9cd0-583917265df1",
   143  			err:    os.ErrPermission,
   144  			efivar: &fake{err: os.ErrPermission},
   145  		},
   146  		{
   147  			name:   "good variable, good remove",
   148  			val:    "WriteOnceStatus-4b3082a3-80c6-4d7e-9cd0-583917265df1",
   149  			err:    nil,
   150  			efivar: &fake{},
   151  		},
   152  	}
   153  
   154  	for _, tt := range tests {
   155  		err := SimpleRemoveVariable(tt.efivar, tt.val)
   156  		if !errors.Is(err, tt.err) {
   157  			t.Errorf("SimpleRemoveVariable(tt.efivar, %s): %v != %v", tt.val, err, tt.err)
   158  		}
   159  	}
   160  
   161  }
   162  
   163  func TestSimpleListVariable(t *testing.T) {
   164  	var tests = []struct {
   165  		name   string
   166  		err    error
   167  		efivar EFIVar
   168  	}{
   169  		{
   170  			name:   "bad List",
   171  			err:    os.ErrPermission,
   172  			efivar: &fake{err: os.ErrPermission},
   173  		},
   174  		{
   175  			name:   "good List",
   176  			err:    nil,
   177  			efivar: &fake{},
   178  		},
   179  	}
   180  
   181  	for _, tt := range tests {
   182  		_, err := SimpleListVariables(tt.efivar)
   183  		if !errors.Is(err, tt.err) {
   184  			t.Errorf("SimpleListVariable(tt.efivar): %v != %v", err, tt.err)
   185  		}
   186  	}
   187  
   188  }