github.com/system-transparency/u-root@v6.0.1-0.20190919065413-ed07a650de4c+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 "fmt" 10 "io/ioutil" 11 "os" 12 "os/exec" 13 "path/filepath" 14 "testing" 15 16 flag "github.com/spf13/pflag" 17 ) 18 19 const ( 20 testDataDir = "testdata" 21 ) 22 23 func resetFlags() { 24 *flagFromDump = "" 25 *flagType = nil 26 } 27 28 func testOutput(t *testing.T, dumpFile string, args []string, expectedOutFile string) { 29 dumpFile = filepath.Join(testDataDir, dumpFile) 30 expectedOutFile = filepath.Join(testDataDir, expectedOutFile) 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.NewBuffer(nil) 38 if err := dmiDecode(out); err != nil { 39 t.Errorf("%+v %+v %+v: error: %s", 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: %s", 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 testOutput(t, "UX307LA.bin", nil, "UX307LA.txt") 58 testOutput(t, "UX307LA.bin", []string{"-t", "system"}, "UX307LA.system.txt") 59 testOutput(t, "UX307LA.bin", []string{"-t", "1,131"}, "UX307LA.1_131.txt") 60 }