github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/core/pci/pci_test.go (about) 1 // Copyright 2022 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 "io" 10 "log" 11 "os" 12 "path/filepath" 13 "strings" 14 "testing" 15 16 "github.com/mvdan/u-root-coreutils/pkg/pci" 17 ) 18 19 func TestPCIExecution(t *testing.T) { 20 // Cover the switch case 21 for _, tt := range []struct { 22 name string 23 hexdump int 24 }{ 25 { 26 name: "switch hexdump case 1", 27 hexdump: 1, 28 }, 29 { 30 name: "switch hexdump case 2", 31 hexdump: 2, 32 }, 33 { 34 name: "switch hexdump case 3", 35 hexdump: 3, 36 }, 37 { 38 name: "switch hexdump case 4", 39 hexdump: 4, 40 }, 41 } { 42 t.Run(tt.name, func(t *testing.T) { 43 *hexdump = tt.hexdump 44 pciExecution(io.Discard, []string{}...) 45 }) 46 } 47 // Cover the rest 48 for _, tt := range []struct { 49 name string 50 args []string 51 numbers bool 52 devs string 53 dumpJSON bool 54 verbosity int 55 readJSON string 56 wantErr string 57 }{ 58 { 59 name: "readJSON true, without error", 60 readJSON: "testfiles/testfile1.json", 61 }, 62 { 63 name: "readJSON true, error in os.ReadFile", 64 readJSON: "testfiles/testfile.json", 65 wantErr: "no such file or directory", 66 }, 67 { 68 name: "readJSON true, error in json.Unmarshal", 69 readJSON: "testfiles/testfile2.json", 70 wantErr: "unexpected end of JSON input", 71 }, 72 { 73 name: "dumpJSON", 74 readJSON: "testfiles/testfile1.json", 75 dumpJSON: true, 76 }, 77 { 78 name: "invoke registers", 79 args: []string{"examplearg"}, 80 }, 81 } { 82 t.Run(tt.name, func(t *testing.T) { 83 *numbers = tt.numbers 84 *devs = tt.devs 85 *dumpJSON = tt.dumpJSON 86 *verbosity = tt.verbosity 87 *readJSON = tt.readJSON 88 if got := pciExecution(io.Discard, tt.args...); got != nil { 89 if !strings.Contains(got.Error(), tt.wantErr) { 90 t.Errorf("pciExecution() = %q, should contain: %q", got, tt.wantErr) 91 } 92 } 93 }) 94 } 95 } 96 97 func TestRegisters(t *testing.T) { 98 configBytes := []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77} 99 dir := t.TempDir() 100 f, err := os.Create(filepath.Join(dir, "config")) 101 if err != nil { 102 t.Errorf("Creating file failed: %v", err) 103 } 104 _, err = f.Write(configBytes) 105 if err != nil { 106 t.Errorf("Writing to file failed: %v", err) 107 } 108 for _, tt := range []struct { 109 name string 110 devices pci.Devices 111 cmds []string 112 wantErr string 113 }{ 114 { 115 name: "trigger first log.Printf", 116 devices: []*pci.PCI{ 117 { 118 FullPath: dir, 119 }, 120 }, 121 cmds: []string{"cmd=cmd=cmd"}, 122 wantErr: "only one = allowed", 123 }, 124 { 125 name: "trigger second log.Printf", 126 devices: []*pci.PCI{ 127 { 128 FullPath: dir, 129 }, 130 }, 131 cmds: []string{"c.m.d=cmd"}, 132 wantErr: "only one . allowed", 133 }, 134 { 135 name: "error in first strconv.ParseUint satisfying l case", 136 devices: []*pci.PCI{ 137 { 138 FullPath: dir, 139 }, 140 }, 141 cmds: []string{"cmd.l=cmd"}, 142 wantErr: "parsing \"cmd\": invalid syntax", 143 }, 144 { 145 name: "error in first strconv.ParseUint satisfying w case", 146 devices: []*pci.PCI{ 147 { 148 FullPath: dir, 149 }, 150 }, 151 cmds: []string{"cmd.w=cmd"}, 152 wantErr: "parsing \"cmd\": invalid syntax", 153 }, 154 { 155 name: "error in first strconv.ParseUint satisfying b case", 156 devices: []*pci.PCI{ 157 { 158 FullPath: dir, 159 }, 160 }, 161 cmds: []string{"cmd.b=cmd"}, 162 wantErr: "parsing \"cmd\": invalid syntax", 163 }, 164 { 165 name: "triggers Bad size log and satisfies the justCheck check", 166 devices: []*pci.PCI{ 167 { 168 FullPath: dir, 169 }, 170 }, 171 cmds: []string{"cmd.cmd=cmd", "cmd.b=cmd"}, 172 wantErr: "Bad size", 173 }, 174 { 175 name: "triggers error, reading out of bounce, EOF", 176 devices: []*pci.PCI{ 177 { 178 FullPath: dir, 179 }, 180 }, 181 cmds: []string{"10.b"}, 182 wantErr: "EOF", 183 }, 184 { 185 name: "reading works and write in PCI.ExtraInfo", 186 devices: []*pci.PCI{ 187 { 188 FullPath: dir, 189 }, 190 }, 191 cmds: []string{"0.w"}, 192 }, 193 { 194 name: "error in second strconv.ParseUint", 195 devices: []*pci.PCI{ 196 { 197 FullPath: dir, 198 }, 199 }, 200 cmds: []string{"0.w=cmd"}, 201 wantErr: "parsing \"cmd\": invalid syntax", 202 }, 203 { 204 name: "writing successful", 205 devices: []*pci.PCI{ 206 { 207 FullPath: dir, 208 }, 209 }, 210 cmds: []string{"0.w=10"}, 211 }, 212 { 213 name: "writing failes because config file does not exist", 214 devices: []*pci.PCI{ 215 { 216 Config: []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}, 217 }, 218 }, 219 cmds: []string{"0.w=10"}, 220 wantErr: "open config: no such file or directory", 221 }, 222 } { 223 t.Run(tt.name, func(t *testing.T) { 224 buf := &bytes.Buffer{} 225 log.SetOutput(buf) 226 registers(tt.devices, tt.cmds...) 227 if !strings.Contains(buf.String(), tt.wantErr) { 228 t.Errorf("registers() = %q, should contain: %q", buf.String(), tt.wantErr) 229 } 230 }) 231 } 232 }