github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/pci/pci_linux_test.go (about) 1 // Copyright 2012-2017 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 "bytes" 9 "log" 10 "os" 11 "testing" 12 ) 13 14 func TestNewBusReaderNoGlob(t *testing.T) { 15 n, err := NewBusReader() 16 if err != nil { 17 t.Fatal(err) 18 } 19 g, err := NewBusReader("*", "*") 20 if err != nil { 21 t.Fatal(err) 22 } 23 if len(n.(*bus).Devices) != len(g.(*bus).Devices) { 24 t.Fatalf("Got %v, want %v", len(n.(*bus).Devices), len(g.(*bus).Devices)) 25 } 26 27 for i := range n.(*bus).Devices { 28 if n.(*bus).Devices[i] != g.(*bus).Devices[i] { 29 t.Errorf("%d: got %q, want %q", i, n.(*bus).Devices[i], g.(*bus).Devices[i]) 30 } 31 } 32 } 33 34 func TestBusReader(t *testing.T) { 35 n, err := NewBusReader() 36 if err != nil { 37 t.Fatal(err) 38 } 39 if len(n.(*bus).Devices) == 0 { 40 t.Fatal("got 0 devices, want at least 1") 41 } 42 43 // A single read should be okay. 44 d, err := n.Read() 45 if err != nil { 46 t.Fatal(err) 47 } 48 if len(n.(*bus).Devices) != len(d) { 49 t.Fatalf("Got %d devices, wanted %d", len(d), len(n.(*bus).Devices)) 50 } 51 52 // Multiple reads should be ok 53 d, err = n.Read() 54 if err != nil { 55 t.Fatal(err) 56 } 57 if len(n.(*bus).Devices) != len(d) { 58 t.Fatalf("Got %d devices, wanted %d", len(d), len(n.(*bus).Devices)) 59 } 60 61 // We are going to partition the set into devices which match and 62 // devices which don't match ven:dev. 63 ven, dev := d[0].Vendor, d[0].Device 64 65 matches, err := n.Read(func(p *PCI) bool { 66 return p.Vendor == ven && p.Device == dev 67 }) 68 if err != nil { 69 t.Fatal(err) 70 } 71 notMatches, err := n.Read(func(p *PCI) bool { 72 return !(p.Vendor == ven && p.Device == dev) 73 }) 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 // Check that the partitions add up. 79 if len(matches)+len(notMatches) != len(n.(*bus).Devices) { 80 t.Fatalf("Got %d+%d devices, wanted %d", len(matches), len(notMatches), len(n.(*bus).Devices)) 81 } 82 } 83 84 func TestBusReadConfig(t *testing.T) { 85 var fullread bool 86 if os.Getuid() == 0 { 87 fullread = true 88 } 89 90 r, err := NewBusReader() 91 if err != nil { 92 log.Fatalf("%v", err) 93 } 94 95 d, err := r.Read() 96 if err != nil { 97 log.Fatalf("Read: %v", err) 98 } 99 o := &bytes.Buffer{} 100 // First test is a low verbosity one that should only require 64 bytes. 101 if err := d.Print(o, 0, 64); err != nil { 102 log.Fatal(err) 103 } 104 // Second test is a bit more complex. If we are not root, it should 105 // get an error. If we are root, it should be ok. 106 err = d.Print(o, 0, 256) 107 if fullread && err != nil { 108 log.Fatalf("Doing a full config read as root: got %v, want nil", err) 109 } 110 if !fullread && err == nil { 111 log.Fatalf("Doing a full config read as ! root: got nil, want %v", os.ErrPermission) 112 } 113 } 114 115 func testBaseLimType(t *testing.T) { 116 tests := []struct { 117 bar string 118 r1, r2 string 119 }{ 120 {bar: "0x0000000000001860 0x0000000000001867 0x0000000000040101", r1: "0x0000000000001860", r2: "0x0000000000001867"}, 121 {bar: "0x0000000000001867 0x0000000000040101"}, 122 {bar: "0x000000000001860 0x0?00000000001867 0x0000000000040101"}, 123 {bar: "0x000000000001860 0x0000000000001867 0x0?00000000040101"}, 124 {bar: "0x000000?000001860 0x0000000000001867 0x0000000000040101"}, 125 } 126 for _, tt := range tests { 127 b, l, a, err := BaseLimType(tt.bar) 128 t.Logf("%v %v %v %v", b, l, a, err) 129 // if r1 != tt.r1 || r2 != tt.r2 { 130 // t.Errorf("BAR %s: got \n(%q,%q) want \n(%q,%q)", tt.bar, r1, r2, tt.r1, tt.r2) 131 //} 132 } 133 }