github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/smbios/smbios_legacy_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  
     5  package smbios
     6  
     7  import (
     8  	"fmt"
     9  	"runtime"
    10  	"testing"
    11  
    12  	"github.com/mvdan/u-root-coreutils/pkg/memio"
    13  	"github.com/mvdan/u-root-coreutils/pkg/testutil"
    14  )
    15  
    16  var tmpBuf = []byte{0, 0, 0, 0, 0, 0}
    17  
    18  func mockMemioRead(base int64, uintn memio.UintN) error {
    19  	dat, ok := uintn.(*memio.ByteSlice)
    20  	if !ok {
    21  		return fmt.Errorf("not supported")
    22  	}
    23  	bufLen := len(tmpBuf)
    24  	for i := int64(0); i < dat.Size(); i++ {
    25  		(*dat)[i] = tmpBuf[(base+i)%int64(bufLen)]
    26  	}
    27  	return nil
    28  }
    29  
    30  func TestSMBIOSLegacyNotFound(t *testing.T) {
    31  	defer func(old func(base int64, uintn memio.UintN) error) { memioRead = old }(memioRead)
    32  	memioRead = mockMemioRead
    33  
    34  	_, _, err := SMBIOSBaseLegacy()
    35  
    36  	want := "could not find _SM_ or _SM3_ via /dev/mem from 0x000f0000 to 0x00100000"
    37  	if err.Error() != want {
    38  		t.Errorf("SMBIOSBaseLegacy(): %v, want '%v'", err, want)
    39  	}
    40  }
    41  
    42  func TestSMBIOSLegacyMemIoReadError(t *testing.T) {
    43  	defer func(old func(base int64, uintn memio.UintN) error) { memioRead = old }(memioRead)
    44  	memioRead = func(base int64, uintn memio.UintN) error {
    45  		return fmt.Errorf("MEMIOREAD_ERROR")
    46  	}
    47  
    48  	_, _, err := SMBIOSBaseLegacy()
    49  
    50  	want := "MEMIOREAD_ERROR"
    51  	if err.Error() != want {
    52  		t.Errorf("SMBIOSBaseLegacy(): %v, want '%v'", err, want)
    53  	}
    54  }
    55  
    56  func TestSMBIOSLegacySMBIOS(t *testing.T) {
    57  	tmpBuf = []byte{0, '_', 'M', 'S', '_', 0, 0, '_', 'S', 'M', '_', 0, 0, 0, 0, 0}
    58  	defer func(old func(base int64, uintn memio.UintN) error) { memioRead = old }(memioRead)
    59  	memioRead = mockMemioRead
    60  	base, size, err := SMBIOSBaseLegacy()
    61  	if err != nil {
    62  		t.Errorf("SMBIOSBaseLegacy(): %v", err)
    63  	}
    64  
    65  	var want int64 = 0xf0007
    66  
    67  	if base != want {
    68  		t.Errorf("SMBIOSBaseLegacy(): %v, want '%v'", base, want)
    69  	}
    70  
    71  	var wantSize int64 = 0x1f
    72  
    73  	if size != wantSize {
    74  		t.Errorf("SMBIOSBaseLegacy(): %v, want '%v'", size, wantSize)
    75  	}
    76  }
    77  
    78  func TestSMBIOSLegacySMBIOS3(t *testing.T) {
    79  	tmpBuf = []byte{0, '_', 'M', 'S', '_', 0, 0, '_', 'S', 'M', '3', '_', 0, 0, 0, 0, 0}
    80  	defer func(old func(base int64, uintn memio.UintN) error) { memioRead = old }(memioRead)
    81  	memioRead = mockMemioRead
    82  	base, size, err := SMBIOSBaseLegacy()
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	var want int64 = 0xf0009
    88  
    89  	if base != want {
    90  		t.Errorf("SMBIOSBaseLegacy(): %v, want '%v'", base, want)
    91  	}
    92  
    93  	var wantSize int64 = 0x18
    94  
    95  	if size != wantSize {
    96  		t.Errorf("SMBIOSBaseLegacy(): %v, want '%v'", size, wantSize)
    97  	}
    98  }
    99  
   100  func TestSMBIOSLegacyQEMU(t *testing.T) {
   101  	if runtime.GOARCH != "amd64" {
   102  		t.Skipf("test not supported on %s", runtime.GOARCH)
   103  	}
   104  	testutil.SkipIfNotRoot(t)
   105  	base, size, err := SMBIOSBaseLegacy()
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	if base == 0 {
   111  		t.Errorf("SMBIOSLegacy() does not get SMBIOS base")
   112  	}
   113  
   114  	if size != smbios2HeaderSize && size != smbios3HeaderSize {
   115  		t.Errorf("SMBIOSBaseLegacy(): %v, want '%v' or '%v'", size, smbios2HeaderSize, smbios3HeaderSize)
   116  	}
   117  }