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

     1  // Copyright 2016-2019 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 smbios
     6  
     7  import (
     8  	"encoding/binary"
     9  	"fmt"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  type UnknownTypes struct {
    15  	Table
    16  	SupportedField   uint64
    17  	UnsupportedField float32
    18  }
    19  
    20  func TestParseStructUnsupported(t *testing.T) {
    21  	buffer := []byte{
    22  		0x77,
    23  		0xFF,
    24  		0x00, 0x11,
    25  		0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    26  		0x00, 0x01, 0x02, 0x03,
    27  	}
    28  
    29  	want := "unsupported type float32"
    30  
    31  	table := Table{
    32  		data: buffer,
    33  	}
    34  
    35  	UnknownType := &UnknownTypes{
    36  		Table: table,
    37  	}
    38  
    39  	off, err := parseStruct(&table, 0, false, UnknownType)
    40  	if err == nil {
    41  		t.Errorf("TestParseStructUnsupported : parseStruct() = %d, '%v' want: %q", off, err, want)
    42  	} else {
    43  		if !strings.Contains(err.Error(), want) {
    44  			t.Errorf("TestParseStructUnsupported : parseStruct() = %d, '%v' want: %q", off, err, want)
    45  		}
    46  	}
    47  }
    48  
    49  func TestParseStructSupported(t *testing.T) {
    50  	buffer := []byte{
    51  		0x77,
    52  		0xFF,
    53  		0x00, 0x11,
    54  		0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    55  	}
    56  
    57  	table := Table{
    58  		data: buffer,
    59  	}
    60  
    61  	UnknownType := &UnknownTypes{
    62  		Table: table,
    63  	}
    64  
    65  	off, err := parseStruct(&table, 0, false, UnknownType)
    66  	if err != nil {
    67  		t.Errorf("TestParseStructUnsupported : parseStruct() = %d, '%v' want: 'nil'", off, err)
    68  	}
    69  }
    70  
    71  func TestParseStructWithTPMDevice(t *testing.T) {
    72  
    73  	tests := []struct {
    74  		name     string
    75  		buffer   []byte
    76  		strings  []string
    77  		complete bool
    78  		want     TPMDevice
    79  		wantErr  error
    80  	}{
    81  		{
    82  			name: "Type43TPMDevice",
    83  			buffer: []byte{
    84  				0x2B,       // Type
    85  				0xFF,       // Length
    86  				0x00, 0x11, // Handle
    87  				0x00, 0x00, 0x00, 0x00, // VendorID
    88  				0x02,       // Major
    89  				0x03,       // Minor
    90  				0x01, 0x00, // FirmwareVersion1
    91  				0x02, 0x00, // FirmwareVersion1
    92  				0x00, 0x00, 0x00, 0x00, //FirmwareVersion2
    93  				0x01,                   // String Index
    94  				1 << 3,                 // Characteristics
    95  				0x78, 0x56, 0x34, 0x12, // OEMDefined
    96  			},
    97  			strings:  []string{"Test TPM"},
    98  			complete: false,
    99  			want: TPMDevice{
   100  				VendorID:         [4]byte{0x00, 0x00, 0x00, 0x00},
   101  				MajorSpecVersion: 2,
   102  				MinorSpecVersion: 3,
   103  				FirmwareVersion1: 0x00020001,
   104  				FirmwareVersion2: 0x00000000,
   105  				Description:      "Test TPM",
   106  				Characteristics:  TPMDeviceCharacteristicsFamilyConfigurableViaFirmwareUpdate,
   107  				OEMDefined:       0x12345678,
   108  			},
   109  			wantErr: nil,
   110  		},
   111  		{
   112  			name: "Type43TPMDevice Incomplete",
   113  			buffer: []byte{
   114  				0x2B,       // Type
   115  				0xFF,       // Length
   116  				0x00, 0x11, // Handle
   117  				0x00, 0x00, 0x00, 0x00, // VendorID
   118  				0x02,       // Major
   119  				0x03,       // Minor
   120  				0x01, 0x00, // FirmwareVersion1
   121  				0x02, 0x00, // FirmwareVersion1
   122  				0x00, 0x00, 0x00, 0x00, //FirmwareVersion2
   123  				0x01,   // String Index
   124  				1 << 3, // Characteristics
   125  			},
   126  			strings:  []string{"Test TPM"},
   127  			complete: true,
   128  			want: TPMDevice{
   129  				VendorID:         [4]byte{0x00, 0x00, 0x00, 0x00},
   130  				MajorSpecVersion: 2,
   131  				MinorSpecVersion: 3,
   132  				FirmwareVersion1: 0x00020001,
   133  				FirmwareVersion2: 0x00000000,
   134  				Description:      "Test TPM",
   135  				Characteristics:  TPMDeviceCharacteristicsFamilyConfigurableViaFirmwareUpdate,
   136  				OEMDefined:       0x12345678,
   137  			},
   138  			wantErr: fmt.Errorf("TPMDevice incomplete, got 8 of 9 fields"),
   139  		},
   140  	}
   141  
   142  	for _, tt := range tests {
   143  		t.Run(tt.name, func(t *testing.T) {
   144  			table := Table{
   145  				data:    tt.buffer,
   146  				strings: tt.strings,
   147  			}
   148  			TPMDev := &TPMDevice{
   149  				Table: table,
   150  			}
   151  
   152  			// We need to modify tt.want with runtime data
   153  			tt.want.Table = Table{
   154  				Header: Header{
   155  					Type:   TableType(tt.buffer[0]),
   156  					Length: tt.buffer[1],
   157  					Handle: binary.BigEndian.Uint16([]byte{tt.buffer[3], tt.buffer[2]}),
   158  				},
   159  				data:    tt.buffer,
   160  				strings: tt.strings,
   161  			}
   162  
   163  			off, err := parseStruct(&table, 0, tt.complete, TPMDev)
   164  			if err != tt.wantErr {
   165  				if !strings.Contains(err.Error(), tt.wantErr.Error()) {
   166  					t.Errorf("parseStruct() = %d, '%v' want '%v'", off, err, tt.wantErr)
   167  				}
   168  			}
   169  			if tt.wantErr == nil {
   170  				if TPMDev.VendorID != tt.want.VendorID {
   171  					t.Errorf("parseStruct().VendorID = %q, want %q", TPMDev.VendorID, tt.want.VendorID)
   172  				}
   173  
   174  				if TPMDev.MajorSpecVersion != tt.want.MajorSpecVersion {
   175  					t.Errorf("parseStruct().MajorSpecVersion = %q, want %q", TPMDev.MajorSpecVersion, tt.want.MajorSpecVersion)
   176  				}
   177  
   178  				if TPMDev.MinorSpecVersion != tt.want.MinorSpecVersion {
   179  					t.Errorf("parseStruct().MinorSpecVersion = %q, want %q", TPMDev.MinorSpecVersion, tt.want.MinorSpecVersion)
   180  				}
   181  
   182  				if TPMDev.FirmwareVersion1 != tt.want.FirmwareVersion1 {
   183  					t.Errorf("parseStruct().FirmwareVersion1 = %q, want %q", TPMDev.FirmwareVersion1, tt.want.FirmwareVersion1)
   184  				}
   185  
   186  				if TPMDev.FirmwareVersion2 != tt.want.FirmwareVersion2 {
   187  					t.Errorf("parseStruct().FirmwareVersion2 = %q, want %q", TPMDev.FirmwareVersion2, tt.want.FirmwareVersion2)
   188  				}
   189  
   190  				if TPMDev.Description != tt.want.Description {
   191  					t.Errorf("parseStruct().Description = %q, want %q", TPMDev.Description, tt.want.Description)
   192  				}
   193  
   194  				if TPMDev.Characteristics != tt.want.Characteristics {
   195  					t.Errorf("parseStruct().Characteristics = %q, want %q", TPMDev.Characteristics, tt.want.Characteristics)
   196  				}
   197  
   198  				if TPMDev.OEMDefined != tt.want.OEMDefined {
   199  					t.Errorf("parseStruct().OEMDefined = %q, want %q", TPMDev.OEMDefined, tt.want.OEMDefined)
   200  				}
   201  			}
   202  		})
   203  	}
   204  
   205  }