github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/vpd/vpd_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 vpd
     6  
     7  import (
     8  	"bytes"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestGetReadOnly(t *testing.T) {
    14  	r := NewReader()
    15  	r.VpdDir = "./tests"
    16  	value, err := r.Get("key1", true)
    17  	if err != nil || !bytes.Equal(value, []byte("value1\n")) {
    18  		t.Errorf(`r.Get("key1", true) = %v, %v, want nil, %v`, value, err, []byte("value1\n"))
    19  	}
    20  }
    21  
    22  func TestGetReadWrite(t *testing.T) {
    23  	r := NewReader()
    24  	r.VpdDir = "./tests"
    25  	value, err := r.Get("mysecretpassword", false)
    26  	if err != nil || !bytes.Equal(value, []byte("passw0rd\n")) {
    27  		t.Errorf(`r.Get("mysecretpassword", false) = %v, %v, want nil, %v`, value, err, []byte("passwor0rd\n"))
    28  	}
    29  }
    30  
    31  func TestGetReadBinary(t *testing.T) {
    32  	r := NewReader()
    33  	r.VpdDir = "./tests"
    34  	value, err := r.Get("binary1", true)
    35  	if err != nil || !bytes.Equal(value, []byte("some\x00binary\ndata")) {
    36  		t.Errorf(`r.Get("binary1", true) = %v, %v, want nil, %v`, value, err, []byte("some\x00binary\ndata"))
    37  	}
    38  }
    39  
    40  func TestGetAllReadOnly(t *testing.T) {
    41  	r := NewReader()
    42  	r.VpdDir = "./tests"
    43  	expected := map[string][]byte{
    44  		"binary1": []byte("some\x00binary\ndata"),
    45  		"key1":    []byte("value1\n"),
    46  	}
    47  	vpdMap, err := r.GetAll(true)
    48  	if err != nil || !reflect.DeepEqual(vpdMap, expected) {
    49  		t.Errorf(`r.GetAll(true) = %v, %v, want nil, %v`, vpdMap, err, expected)
    50  		t.FailNow()
    51  	}
    52  }