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

     1  // Copyright 2016-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  package smbios
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"reflect"
    11  	"testing"
    12  )
    13  
    14  var (
    15  	testbinary = "testdata/satellite_pro_l70_testdata.bin"
    16  )
    17  
    18  func checkError(got error, want error) bool {
    19  	if got != nil && want != nil {
    20  		if got.Error() == want.Error() {
    21  			return true
    22  		}
    23  	}
    24  
    25  	return errors.Is(got, want)
    26  }
    27  
    28  func TestParseSMBIOS(t *testing.T) {
    29  	data, err := ioutil.ReadFile(testbinary)
    30  	if err != nil {
    31  		t.Error(err)
    32  	}
    33  	datalen := len(data)
    34  	readlen := 0
    35  	for i := 0; datalen > i; i += readlen {
    36  		_, rest, err := ParseTable(data)
    37  		if err != nil {
    38  			t.Error(err)
    39  		}
    40  		readlen = datalen - len(rest)
    41  	}
    42  }
    43  
    44  func Test64Len(t *testing.T) {
    45  	info, err := setupMockData()
    46  
    47  	if err != nil {
    48  		t.Errorf("error parsing info data: %v", err)
    49  	}
    50  
    51  	if info.Tables != nil {
    52  		if info.Tables[0].Len() != 14 {
    53  			t.Errorf("Wrong length: Got %d want %d", info.Tables[0].Len(), 14)
    54  		}
    55  	}
    56  }
    57  
    58  func Test64String(t *testing.T) {
    59  
    60  	tableString := `Handle 0x0000, DMI type 222, 14 bytes
    61  OEM-specific Type
    62  	Header and Data:
    63  		DE 0E 00 00 01 99 00 03 10 01 20 02 30 03
    64  	Strings:
    65  		Memory Init Complete
    66  		End of DXE Phase
    67  		BIOS Boot Complete`
    68  
    69  	info, err := setupMockData()
    70  
    71  	if err != nil {
    72  		t.Errorf("error parsing info data: %v", err)
    73  	}
    74  
    75  	if info.Tables != nil {
    76  		if info.Tables[0].String() != tableString {
    77  			t.Errorf("Wrong length: Got %s want %s", info.Tables[0].String(), tableString)
    78  		}
    79  	}
    80  }
    81  
    82  func Test64GetByteAt(t *testing.T) {
    83  	testStruct := Table{
    84  		Header: Header{
    85  			Type:   TableTypeBIOSInfo,
    86  			Length: 16,
    87  			Handle: 0,
    88  		},
    89  		data:    []byte{1, 0, 0, 0, 213, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    90  		strings: []string{"BIOS Boot Complete", "TestString #1"},
    91  	}
    92  
    93  	tests := []struct {
    94  		name         string
    95  		offset       int
    96  		expectedByte uint8
    97  		want         error
    98  	}{
    99  		{
   100  			name:         "GetByteAt",
   101  			offset:       0,
   102  			expectedByte: 1,
   103  			want:         nil,
   104  		},
   105  		{
   106  			name:         "GetByteAt Wrong Offset",
   107  			offset:       213,
   108  			expectedByte: 0,
   109  			want:         fmt.Errorf("invalid offset %d", 213),
   110  		},
   111  	}
   112  
   113  	for _, tt := range tests {
   114  		t.Run(tt.name, func(t *testing.T) {
   115  			resultByte, err := testStruct.GetByteAt(tt.offset)
   116  			if !checkError(err, tt.want) {
   117  				t.Errorf("GetByteAt(): '%v', want '%v'", err, tt.want)
   118  			}
   119  			if resultByte != tt.expectedByte {
   120  				t.Errorf("GetByteAt() = %x, want %x", resultByte, tt.expectedByte)
   121  			}
   122  		})
   123  	}
   124  }
   125  
   126  func Test64GetBytesAt(t *testing.T) {
   127  	testStruct := Table{
   128  		Header: Header{
   129  			Type:   TableTypeBIOSInfo,
   130  			Length: 16,
   131  			Handle: 0,
   132  		},
   133  		data:    []byte{1, 0, 0, 0, 213, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   134  		strings: []string{"BIOS Boot Complete", "TestString #1"},
   135  	}
   136  
   137  	tests := []struct {
   138  		name          string
   139  		offset        int
   140  		length        int
   141  		expectedBytes []byte
   142  		want          error
   143  	}{
   144  		{
   145  			name:          "Get two bytes",
   146  			offset:        0,
   147  			length:        2,
   148  			expectedBytes: []byte{1, 0},
   149  			want:          nil,
   150  		},
   151  		{
   152  			name:          "Wrong Offset",
   153  			offset:        213,
   154  			expectedBytes: []byte{},
   155  			want:          fmt.Errorf("invalid offset 213"),
   156  		},
   157  		{
   158  			name:          "Read out-of-bounds",
   159  			offset:        7,
   160  			length:        16,
   161  			expectedBytes: []byte{},
   162  			want:          fmt.Errorf("invalid offset 7"),
   163  		},
   164  	}
   165  
   166  	for _, tt := range tests {
   167  		t.Run(tt.name, func(t *testing.T) {
   168  
   169  			resultBytes, err := testStruct.GetBytesAt(tt.offset, tt.length)
   170  
   171  			if !checkError(err, tt.want) {
   172  				t.Errorf("GetBytesAt(): '%v', want '%v'", err, tt.want)
   173  			}
   174  			if !reflect.DeepEqual(resultBytes, tt.expectedBytes) && err == nil {
   175  				t.Errorf("GetBytesAt(): Wrong byte size, %x, want %x", resultBytes, tt.expectedBytes)
   176  			}
   177  		})
   178  	}
   179  }
   180  
   181  func Test64GetWordAt(t *testing.T) {
   182  	testStruct := Table{
   183  		Header: Header{
   184  			Type:   TableTypeBIOSInfo,
   185  			Length: 16,
   186  			Handle: 0,
   187  		},
   188  		data:    []byte{1, 0, 0, 0, 213, 0, 0, 11, 12, 0, 0, 0, 0, 0, 0},
   189  		strings: []string{"BIOS Boot Complete", "TestString #1"},
   190  	}
   191  
   192  	tests := []struct {
   193  		name          string
   194  		offset        int
   195  		expectedBytes uint16
   196  		want          error
   197  	}{
   198  		{
   199  			name:          "Get two bytes",
   200  			offset:        0,
   201  			expectedBytes: 1,
   202  			want:          nil,
   203  		},
   204  		{
   205  			name:          "Wrong Offset",
   206  			offset:        213,
   207  			expectedBytes: 0,
   208  			want:          fmt.Errorf("invalid offset 213"),
   209  		},
   210  		{
   211  			name:          "Read position 7",
   212  			offset:        7,
   213  			expectedBytes: 0xc0b,
   214  			want:          nil,
   215  		},
   216  	}
   217  
   218  	for _, tt := range tests {
   219  		t.Run(tt.name, func(t *testing.T) {
   220  
   221  			resultBytes, err := testStruct.GetWordAt(tt.offset)
   222  			if !checkError(err, tt.want) {
   223  				t.Errorf("GetBytesAt(): '%v', want '%v'", err, tt.want)
   224  			}
   225  			if !reflect.DeepEqual(resultBytes, tt.expectedBytes) && err == nil {
   226  				t.Errorf("GetBytesAt(): Wrong byte size, %x, want %x", resultBytes, tt.expectedBytes)
   227  			}
   228  		})
   229  	}
   230  }
   231  
   232  func Test64GetDWordAt(t *testing.T) {
   233  	testStruct := Table{
   234  		Header: Header{
   235  			Type:   TableTypeBIOSInfo,
   236  			Length: 16,
   237  			Handle: 0,
   238  		},
   239  		data:    []byte{1, 0, 0, 0, 213, 0, 0, 11, 12, 13, 14, 0, 0, 0, 0},
   240  		strings: []string{"BIOS Boot Complete", "TestString #1"},
   241  	}
   242  
   243  	tests := []struct {
   244  		name          string
   245  		offset        int
   246  		expectedBytes uint32
   247  		want          error
   248  	}{
   249  		{
   250  			name:          "Get two bytes",
   251  			offset:        0,
   252  			expectedBytes: 1,
   253  			want:          nil,
   254  		},
   255  		{
   256  			name:          "Wrong Offset",
   257  			offset:        213,
   258  			expectedBytes: 0,
   259  			want:          fmt.Errorf("invalid offset 213"),
   260  		},
   261  		{
   262  			name:          "Read position 7",
   263  			offset:        7,
   264  			expectedBytes: 0xe0d0c0b,
   265  			want:          nil,
   266  		},
   267  	}
   268  
   269  	for _, tt := range tests {
   270  		t.Run(tt.name, func(t *testing.T) {
   271  
   272  			resultBytes, err := testStruct.GetDWordAt(tt.offset)
   273  			if !checkError(err, tt.want) {
   274  				t.Errorf("GetBytesAt(): '%v', want '%v'", err, tt.want)
   275  			}
   276  			if !reflect.DeepEqual(resultBytes, tt.expectedBytes) && err == nil {
   277  				t.Errorf("GetBytesAt(): Wrong byte size, %x, want %x", resultBytes, tt.expectedBytes)
   278  			}
   279  		})
   280  	}
   281  }
   282  
   283  func Test64GetQWordAt(t *testing.T) {
   284  	testStruct := Table{
   285  		Header: Header{
   286  			Type:   TableTypeBIOSInfo,
   287  			Length: 16,
   288  			Handle: 0,
   289  		},
   290  		data:    []byte{1, 0, 0, 0, 213, 0, 0, 11, 12, 13, 14, 15, 16, 17, 18},
   291  		strings: []string{"BIOS Boot Complete", "TestString #1"},
   292  	}
   293  
   294  	tests := []struct {
   295  		name          string
   296  		offset        int
   297  		expectedBytes uint64
   298  		want          error
   299  	}{
   300  		{
   301  			name:          "Get two bytes",
   302  			offset:        0,
   303  			expectedBytes: 0xb0000d500000001,
   304  			want:          nil,
   305  		},
   306  		{
   307  			name:          "Wrong Offset",
   308  			offset:        213,
   309  			expectedBytes: 0,
   310  			want:          fmt.Errorf("invalid offset 213"),
   311  		},
   312  		{
   313  			name:          "Read position 7",
   314  			offset:        7,
   315  			expectedBytes: 0x1211100f0e0d0c0b,
   316  			want:          nil,
   317  		},
   318  	}
   319  
   320  	for _, tt := range tests {
   321  		t.Run(tt.name, func(t *testing.T) {
   322  			resultBytes, err := testStruct.GetQWordAt(tt.offset)
   323  			if !checkError(err, tt.want) {
   324  				t.Errorf("GetBytesAt(): '%v', want '%v'", err, tt.want)
   325  			}
   326  			if !reflect.DeepEqual(resultBytes, tt.expectedBytes) && err == nil {
   327  				t.Errorf("GetBytesAt(): Wrong byte size, %x, want %x", resultBytes, tt.expectedBytes)
   328  			}
   329  		})
   330  	}
   331  }
   332  
   333  func TestKmgt(t *testing.T) {
   334  
   335  	tests := []struct {
   336  		name   string
   337  		value  uint64
   338  		expect string
   339  	}{
   340  		{
   341  			name:   "Just bytes",
   342  			value:  512,
   343  			expect: "512 bytes",
   344  		},
   345  		{
   346  			name:   "Two Kb",
   347  			value:  2 * 1024,
   348  			expect: "2 kB",
   349  		},
   350  		{
   351  			name:   "512 MB",
   352  			value:  512 * 1024 * 1024,
   353  			expect: "512 MB",
   354  		},
   355  		{
   356  			name:   "8 GB",
   357  			value:  8 * 1024 * 1024 * 1024,
   358  			expect: "8 GB",
   359  		},
   360  		{
   361  			name:   "3 TB",
   362  			value:  3 * 1024 * 1024 * 1024 * 1024,
   363  			expect: "3 TB",
   364  		},
   365  	}
   366  
   367  	for _, tt := range tests {
   368  		t.Run(tt.name, func(t *testing.T) {
   369  			if kmgt(tt.value) != tt.expect {
   370  				t.Errorf("kgmt(): %v - want '%v'", kmgt(tt.value), tt.expect)
   371  			}
   372  		})
   373  	}
   374  }
   375  
   376  func Test64GetStringAt(t *testing.T) {
   377  
   378  	testStruct := Table{
   379  		Header: Header{
   380  			Type:   TableTypeBIOSInfo,
   381  			Length: 16,
   382  			Handle: 0,
   383  		},
   384  		data:    []byte{1, 0, 0, 0, 213, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   385  		strings: []string{"BIOS Boot Complete", "TestString #1"},
   386  	}
   387  
   388  	tests := []struct {
   389  		name           string
   390  		offset         int
   391  		expectedString string
   392  	}{
   393  		{
   394  			name:           "Valid offset",
   395  			offset:         0,
   396  			expectedString: "BIOS Boot Complete",
   397  		},
   398  		{
   399  			name:           "Not Specified",
   400  			offset:         2,
   401  			expectedString: "Not Specified",
   402  		},
   403  		{
   404  			name:           "Bad Index",
   405  			offset:         4,
   406  			expectedString: "<BAD INDEX>",
   407  		},
   408  	}
   409  
   410  	for _, tt := range tests {
   411  		resultString, _ := testStruct.GetStringAt(tt.offset)
   412  		if resultString != tt.expectedString {
   413  			t.Errorf("GetStringAt(): %s, want '%s'", resultString, tt.expectedString)
   414  		}
   415  	}
   416  }