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