github.com/linuxboot/fiano@v1.2.0/pkg/amd/manifest/psp_directory_table_test.go (about)

     1  // Copyright 2019 the LinuxBoot 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 manifest
     6  
     7  import (
     8  	"encoding/binary"
     9  	"testing"
    10  )
    11  
    12  var pspDirectoryTableDataChunk = []byte{
    13  	0x24, 0x50, 0x53, 0x50,
    14  	0x57, 0x4d, 0x3f, 0xfc,
    15  	0x01, 0x00, 0x00, 0x00,
    16  	0x10, 0x05, 0x00, 0x20,
    17  
    18  	0x00,
    19  	0x00,
    20  	0x00, 0x00,
    21  	0x40, 0x04, 0x00, 0x00,
    22  	0x00, 0x24, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
    23  }
    24  
    25  func TestPSPDirectoryTableHeaderSize(t *testing.T) {
    26  	const expectedPSPDirectoryTableHeaderSize = 0x10
    27  	actualSize := binary.Size(PSPDirectoryTableHeader{})
    28  	if actualSize != expectedPSPDirectoryTableHeaderSize {
    29  		t.Errorf("BIOSDirectoryTableHeader is incorrect: %d, expected %d", actualSize, expectedPSPDirectoryTableHeaderSize)
    30  	}
    31  }
    32  
    33  func TestFindPSPDirectoryTable(t *testing.T) {
    34  	firmwareChunk := []byte{
    35  		0x12, 0x00, 0x15, 0x00, 0x15, // some prefix
    36  	}
    37  
    38  	t.Run("no_psp_table_cookie", func(t *testing.T) {
    39  		table, _, err := FindPSPDirectoryTable(firmwareChunk)
    40  		if err == nil {
    41  			t.Errorf("Expected an error when finding psp directory table in a broken firmware")
    42  		}
    43  		if table != nil {
    44  			t.Errorf("Returned PSP Directory table is not nil")
    45  		}
    46  	})
    47  
    48  	t.Run("psp_table_cookie_found", func(t *testing.T) {
    49  		table, r, err := FindPSPDirectoryTable(append(firmwareChunk, pspDirectoryTableDataChunk...))
    50  		if err != nil {
    51  			t.Fatalf("Unexecpted error when finding PSP Directory table")
    52  		}
    53  		if r.Offset != uint64(len(firmwareChunk)) {
    54  			t.Fatalf("PSP Directory Table address is incorrect: %d, expected: %d", r.Offset, uint64(len(firmwareChunk)))
    55  		}
    56  		if r.Length != uint64(len(pspDirectoryTableDataChunk)) {
    57  			t.Errorf("PSP Directory Table size is incorrect: %d, expected: %d", r.Length, uint64(len(pspDirectoryTableDataChunk)))
    58  		}
    59  		if table == nil {
    60  			t.Fatal("Returned PSP Directory table is nil")
    61  		}
    62  	})
    63  }
    64  
    65  func TestPspDirectoryTableParsing(t *testing.T) {
    66  	data := append(pspDirectoryTableDataChunk, 0xff)
    67  	table, length, err := ParsePSPDirectoryTable(data)
    68  	if err != nil {
    69  		t.Fatalf("Failed to parse PSP Directory table, err: %v", err)
    70  	}
    71  	if length != uint64(len(pspDirectoryTableDataChunk)) {
    72  		t.Errorf("PSP Directory table read bytes is incorrect: %d, expected: %d", length, len(biosDirectoryTableDataChunk))
    73  	}
    74  	if table == nil {
    75  		t.Fatal("result PSP Directory table is nil")
    76  	}
    77  
    78  	if table.PSPCookie != PSPDirectoryTableCookie {
    79  		t.Errorf("PSPCookie is incorrect: %d, expected: %d", table.PSPCookie, PSPDirectoryTableCookie)
    80  	}
    81  	if table.Checksum != 0xfc3f4d57 {
    82  		t.Errorf("Checksum is incorrect: %d, expected: %d", table.Checksum, 0xfc3f4d57)
    83  	}
    84  	if table.TotalEntries != 1 {
    85  		t.Errorf("TotalEntries is incorrect: %d, expected: %d", table.TotalEntries, 1)
    86  	}
    87  	if len(table.Entries) != 1 {
    88  		t.Fatalf("Result number of entries is incorrect: %d, expected: %d", len(table.Entries), 1)
    89  	}
    90  
    91  	if table.Entries[0].Type != AMDPublicKeyEntry {
    92  		t.Errorf("Table entry [0] type is incorrect: %d, expected: %d", table.Entries[0].Type, AMDPublicKeyEntry)
    93  	}
    94  	if table.Entries[0].Subprogram != 0 {
    95  		t.Errorf("Table entry [0] subprogram is incorrect: %d, expected: %d", table.Entries[0].Subprogram, 0)
    96  	}
    97  	if table.Entries[0].LocationOrValue != 0x62400 {
    98  		t.Errorf("Table entry [0] location is incorrect: %d, expected: 0x62400", table.Entries[0].LocationOrValue)
    99  	}
   100  }
   101  
   102  func TestBrokenTotalEntriesPspDirectoryParsing(t *testing.T) {
   103  	pspDirectoryTableData := make([]byte, len(pspDirectoryTableDataChunk))
   104  	copy(pspDirectoryTableData, pspDirectoryTableDataChunk)
   105  
   106  	// 8 is offset of TotalEntries field
   107  	pspDirectoryTableData[8] = 0xff
   108  	pspDirectoryTableData[9] = 0xff
   109  	pspDirectoryTableData[10] = 0xff
   110  	pspDirectoryTableData[11] = 0xff
   111  
   112  	_, _, err := ParsePSPDirectoryTable(pspDirectoryTableData)
   113  	if err == nil {
   114  		t.Errorf("expected error when parsing incorrect psp directory table contents")
   115  	}
   116  }