github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/exp/dmidecode/dmidecode_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 main
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"fmt"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"strings"
    15  	"testing"
    16  
    17  	flag "github.com/spf13/pflag"
    18  )
    19  
    20  const (
    21  	testDataDir = "testdata"
    22  )
    23  
    24  func resetFlags() {
    25  	*flagFromDump = ""
    26  	*flagType = nil
    27  }
    28  
    29  func testOutput(t *testing.T, dumpFile string, args []string, expectedOutFile string) {
    30  	actualOutFile := fmt.Sprintf("%s.actual", expectedOutFile)
    31  	os.Remove(actualOutFile)
    32  	os.Args = []string{os.Args[0], "--from-dump", dumpFile}
    33  	os.Args = append(os.Args, args...)
    34  	flag.Parse()
    35  	defer resetFlags()
    36  	out := &bytes.Buffer{}
    37  	if err := dmiDecode(out); err != nil {
    38  		t.Errorf("%+v %+v %+v: error: %v", dumpFile, args, expectedOutFile, err)
    39  		return
    40  	}
    41  	actualOut := out.Bytes()
    42  	expectedOut, err := os.ReadFile(expectedOutFile)
    43  	if err != nil {
    44  		t.Errorf("%+v %+v %+v: failed to load %s: %v", dumpFile, args, expectedOutFile, expectedOutFile, err)
    45  		return
    46  	}
    47  	if !bytes.Equal(actualOut, expectedOut) {
    48  		os.WriteFile(actualOutFile, actualOut, 0o644)
    49  		t.Errorf("%+v %+v %+v: output mismatch, see %s", dumpFile, args, expectedOutFile, actualOutFile)
    50  		diffOut, _ := exec.Command("diff", "-u", expectedOutFile, actualOutFile).CombinedOutput()
    51  		t.Errorf("%+v %+v %+v: diff:\n%s", dumpFile, args, expectedOutFile, string(diffOut))
    52  	}
    53  }
    54  
    55  func TestDMIDecode(t *testing.T) {
    56  	bf, err := filepath.Glob("testdata/*.bin")
    57  	if err != nil {
    58  		t.Fatalf("glob failed: %v", err)
    59  	}
    60  	for _, dumpFile := range bf {
    61  		txtFile := strings.TrimSuffix(dumpFile, ".bin") + ".txt"
    62  		testOutput(t, dumpFile, nil, txtFile)
    63  	}
    64  }
    65  
    66  func TestDMIDecodeTypeFilters(t *testing.T) {
    67  	testOutput(t, "testdata/Asus-UX307LA.bin", []string{"-t", "system"}, "testdata/Asus-UX307LA.system.txt")
    68  	testOutput(t, "testdata/Asus-UX307LA.bin", []string{"-t", "1,131"}, "testdata/Asus-UX307LA.1_131.txt")
    69  }
    70  
    71  func testDumpBin(t *testing.T, entryData, expectedOutData []byte) {
    72  	tmpfile, err := os.CreateTemp("", "dmidecode")
    73  	if err != nil {
    74  		t.Fatalf("error creating temp file: %v", err)
    75  	}
    76  	tmpfile.Close()
    77  	defer os.Remove(tmpfile.Name())
    78  	textOut := bytes.NewBuffer(nil)
    79  	if err := dumpBin(
    80  		textOut,
    81  		entryData,
    82  		[]byte{0xaa, 0xbb}, // dummy
    83  		tmpfile.Name(),
    84  	); err != nil {
    85  		t.Fatalf("failed to dump bin: %v", err)
    86  	}
    87  	outData, err := os.ReadFile(tmpfile.Name())
    88  	if err != nil {
    89  		t.Fatalf("failed to read output: %v", err)
    90  	}
    91  	if !bytes.Equal(outData, expectedOutData) {
    92  		t.Fatalf("binary data mismatch,\nexpected:\n  %s\ngot:\n  %s", hex.EncodeToString(expectedOutData), hex.EncodeToString(outData))
    93  	}
    94  }
    95  
    96  func TestDMIDecodeDumpBin32(t *testing.T) {
    97  	// We expect entry point address to be rewritten and checksum adjusted.
    98  	testDumpBin(
    99  		t,
   100  		[]byte{
   101  			0x5f, 0x53, 0x4d, 0x5f, 0x64, 0x1f, 0x02, 0x08, 0x14, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   102  			0x5f, 0x44, 0x4d, 0x49, 0x5f, 0x37, 0x6e, 0x08, 0x00, 0x50, 0x7c, 0xac, 0x1b, 0x00, 0x28,
   103  		},
   104  		[]byte{
   105  			0x5f, 0x53, 0x4d, 0x5f, 0x64, 0x1f, 0x02, 0x08, 0x14, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   106  			0x5f, 0x44, 0x4d, 0x49, 0x5f, 0x8f, 0x6e, 0x08, 0x20, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x28, 0x00,
   107  			0xaa, 0xbb,
   108  		},
   109  	)
   110  }
   111  
   112  func TestDMIDecodeDumpBin64(t *testing.T) {
   113  	// We expect entry point address to be rewritten and checksum adjusted.
   114  	testDumpBin(
   115  		t,
   116  		[]byte{
   117  			0x5f, 0x53, 0x4d, 0x33, 0x5f, 0xe6, 0x18, 0x03, 0x00, 0x00, 0x01, 0x00, 0xe3, 0x0b, 0x00, 0x00,
   118  			0x00, 0xe0, 0x10, 0x8f, 0x00, 0x00, 0x00, 0x00,
   119  		},
   120  		[]byte{
   121  			0x5f, 0x53, 0x4d, 0x33, 0x5f, 0x45, 0x18, 0x03, 0x00, 0x00, 0x01, 0x00, 0xe3, 0x0b, 0x00, 0x00,
   122  			0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   123  			0xaa, 0xbb,
   124  		},
   125  	)
   126  }