github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/hsskey/hsskey_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 hsskey
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  
    11  	"github.com/mvdan/u-root-coreutils/pkg/ipmi/blobs"
    12  )
    13  
    14  type mockBlobReader struct {
    15  	data []uint8
    16  }
    17  
    18  func (h *mockBlobReader) BlobOpen(id string, flags int16) (blobs.SessionID, error) {
    19  	return 0, nil
    20  }
    21  
    22  func (h *mockBlobReader) BlobRead(sid blobs.SessionID, offset, size uint32) ([]uint8, error) {
    23  	return h.data, nil
    24  }
    25  
    26  func (h *mockBlobReader) BlobClose(sid blobs.SessionID) error {
    27  	return nil
    28  }
    29  
    30  func TestGenPassword(t *testing.T) {
    31  	mockHss := []byte{0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55,
    32  		0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55,
    33  		0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55,
    34  		0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55}
    35  
    36  	expected := []byte{175, 97, 131, 232, 89, 61, 152, 29, 245,
    37  		45, 164, 141, 98, 78, 7, 243, 120, 96, 179, 166, 18,
    38  		59, 22, 172, 16, 151, 191, 99, 141, 25, 35, 246}
    39  
    40  	key, err := GenPassword(mockHss, DefaultPasswordSalt, "a", "b")
    41  
    42  	if err != nil || !bytes.Equal(key, expected) {
    43  		t.Fatalf("GenPassword generated a wrong key, expected:\n%v\nbut returned:\n%v", expected, key)
    44  	}
    45  }
    46  
    47  func TestReadHssBlobEmpty(t *testing.T) {
    48  	h := mockBlobReader{[]uint8{}}
    49  	_, err := readHssBlob("", &h)
    50  
    51  	if err == nil {
    52  		t.Fatalf("Expected invalid length failure")
    53  	}
    54  }
    55  
    56  func TestReadHssBlob(t *testing.T) {
    57  	data := [hostSecretSeedLen]uint8{}
    58  	h := mockBlobReader{data: data[:]}
    59  	_, err := readHssBlob("", &h)
    60  
    61  	if err != nil {
    62  		t.Fatalf("Expected success, got err: %v", err)
    63  	}
    64  }