github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/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 pci 6 7 import ( 8 "encoding/hex" 9 "os" 10 "path/filepath" 11 "strings" 12 "testing" 13 ) 14 15 func TestPCIReadConfigRegister(t *testing.T) { 16 configBytes := []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77} 17 dir := t.TempDir() 18 f, err := os.Create(filepath.Join(dir, "config")) 19 if err != nil { 20 t.Errorf("Creating file failed: %v", err) 21 } 22 _, err = f.Write(configBytes) 23 if err != nil { 24 t.Errorf("Writing to file failed: %v", err) 25 } 26 for _, tt := range []struct { 27 name string 28 pci PCI 29 offset int64 30 size int64 31 valsWant uint64 32 errWant string 33 }{ 34 { 35 name: "read byte 1 from config file", 36 pci: PCI{ 37 FullPath: dir, 38 }, 39 offset: 0, 40 size: 8, 41 valsWant: 0x00, 42 }, 43 { 44 name: "read byte 1,2 from config file", 45 pci: PCI{ 46 FullPath: dir, 47 }, 48 offset: 0, 49 size: 16, 50 valsWant: 0x1100, 51 }, 52 { 53 name: "read byte 1,2,3,4 from config file", 54 pci: PCI{ 55 FullPath: dir, 56 }, 57 offset: 0, 58 size: 32, 59 valsWant: 0x33221100, 60 }, 61 { 62 name: "read byte 1,2,3,4,5,6,7,8 from config file", 63 pci: PCI{ 64 FullPath: dir, 65 }, 66 offset: 0, 67 size: 64, 68 valsWant: 0x7766554433221100, 69 }, 70 { 71 name: "read byte 1,2,3,4,5,6,7,8 from config file with error", 72 pci: PCI{ 73 FullPath: dir, 74 }, 75 offset: 2, 76 size: 64, 77 valsWant: 0x7766554433221100, 78 errWant: "EOF", 79 }, 80 { 81 name: "wrong size", 82 pci: PCI{ 83 FullPath: dir, 84 }, 85 offset: 0, 86 size: 0, 87 errWant: "only options are 8, 16, 32, 64", 88 }, 89 { 90 name: "config file does not exist", 91 pci: PCI{ 92 FullPath: "d", 93 }, 94 errWant: "no such file or directory", 95 }, 96 } { 97 t.Run(tt.name, func(t *testing.T) { 98 if vals, got := tt.pci.ReadConfigRegister(tt.offset, tt.size); got != nil { 99 if !strings.Contains(got.Error(), tt.errWant) { 100 t.Errorf("ReadConfig() = %q, want to contain: %q", got, tt.errWant) 101 } 102 } else { 103 if vals != tt.valsWant { 104 t.Errorf("ReadConfig() = '%#x', want: '%#x'", vals, tt.valsWant) 105 } 106 } 107 }) 108 } 109 } 110 111 func TestPCIWriteConfigRegister(t *testing.T) { 112 configBytes := []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77} 113 dir := t.TempDir() 114 f, err := os.Create(filepath.Join(dir, "config")) 115 if err != nil { 116 t.Errorf("Creating file failed: %v", err) 117 } 118 _, err = f.Write(configBytes) 119 if err != nil { 120 t.Errorf("Writing to file failed: %v", err) 121 } 122 for _, tt := range []struct { 123 name string 124 pci PCI 125 offset int64 126 size int64 127 val uint64 128 want string 129 errWant string 130 }{ 131 { 132 name: "Writing 1 byte to config file", 133 pci: PCI{ 134 FullPath: dir, 135 }, 136 offset: 0, 137 size: 8, 138 val: 0x00, 139 want: "0011223344556677", 140 }, 141 { 142 name: "Writing 2 bytes to config file", 143 pci: PCI{ 144 FullPath: dir, 145 }, 146 offset: 0, 147 size: 16, 148 val: 0x0011, 149 want: "1100223344556677", 150 }, 151 { 152 name: "Writing 4 bytes to config file", 153 pci: PCI{ 154 FullPath: dir, 155 }, 156 offset: 0, 157 size: 32, 158 val: 0x00112233, 159 want: "3322110044556677", 160 }, 161 { 162 name: "Writing 8 bytes to config file", 163 pci: PCI{ 164 FullPath: dir, 165 }, 166 offset: 0, 167 size: 64, 168 val: 0x0011223344556677, 169 want: "7766554433221100", 170 }, 171 { 172 name: "Writing 8 bytes to config file with offset of 2 bytes", 173 pci: PCI{ 174 FullPath: dir, 175 }, 176 offset: 2, 177 size: 64, 178 val: 0x0011223344556677, 179 want: "77667766554433221100", 180 }, 181 { 182 name: "wrong size", 183 pci: PCI{ 184 FullPath: dir, 185 }, 186 offset: 0, 187 size: 0, 188 errWant: "only options are 8, 16, 32, 64", 189 }, 190 { 191 name: "More than 32 bits", 192 pci: PCI{ 193 FullPath: dir, 194 }, 195 offset: 0, 196 size: 32, 197 val: 1 << 33, 198 errWant: "out of range", 199 }, 200 { 201 name: "More than 16 bits", 202 pci: PCI{ 203 FullPath: dir, 204 }, 205 offset: 0, 206 size: 16, 207 val: 1 << 17, 208 errWant: "out of range", 209 }, 210 { 211 name: "More than 8 bits", 212 pci: PCI{ 213 FullPath: dir, 214 }, 215 offset: 0, 216 size: 8, 217 val: 1 << 17, 218 errWant: "out of range", 219 }, 220 { 221 name: "config file does not exist", 222 pci: PCI{ 223 FullPath: "d", 224 }, 225 errWant: "no such file or directory", 226 }, 227 } { 228 t.Run(tt.name, func(t *testing.T) { 229 if got := tt.pci.WriteConfigRegister(tt.offset, tt.size, tt.val); got != nil { 230 if !strings.Contains(got.Error(), tt.errWant) { 231 t.Errorf("ReadConfig() = %q, want to contain: %q", got, tt.errWant) 232 } 233 } else { 234 got, err := os.ReadFile(filepath.Join(dir, "config")) 235 if err != nil { 236 t.Errorf("Failed to read file %v", err) 237 } 238 if hex.EncodeToString(got) != tt.want { 239 t.Errorf("Config file contains = %q, want: %q", hex.EncodeToString(got), tt.want) 240 } 241 } 242 }) 243 } 244 }