github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/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  	"testing"
     9  )
    10  
    11  func TestNewBusReaderNoGlob(t *testing.T) {
    12  	n, err := NewBusReader()
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	g, err := NewBusReader("*", "*")
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	if len(n.(*bus).Devices) != len(g.(*bus).Devices) {
    21  		t.Fatalf("Got %v, want %v", len(n.(*bus).Devices), len(g.(*bus).Devices))
    22  	}
    23  
    24  	for i := range n.(*bus).Devices {
    25  		if n.(*bus).Devices[i] != g.(*bus).Devices[i] {
    26  			t.Errorf("%d: got %q, want %q", i, n.(*bus).Devices[i], g.(*bus).Devices[i])
    27  		}
    28  	}
    29  }
    30  
    31  func TestBusReader(t *testing.T) {
    32  	n, err := NewBusReader()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	if len(n.(*bus).Devices) == 0 {
    37  		t.Fatal("got 0 devices, want at least 1")
    38  	}
    39  	d, err := n.Read()
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	if len(n.(*bus).Devices) != len(d) {
    44  		t.Fatalf("Got %d devices, wanted %d", len(d), len(n.(*bus).Devices))
    45  	}
    46  	// Multiple reads should be ok
    47  	d, err = n.Read()
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if len(n.(*bus).Devices) != len(d) {
    52  		t.Fatalf("Got %d devices, wanted %d", len(d), len(n.(*bus).Devices))
    53  	}
    54  	// Filter by vendor and dev id.
    55  	ven, dev := d[0].Vendor, d[0].Device
    56  	d, err = n.Read(func(p *PCI) bool {
    57  		if p.Vendor == ven && p.Device == dev {
    58  			return false
    59  		}
    60  		return true
    61  	})
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	// That should filter just one thing.
    66  	if len(n.(*bus).Devices)-1 != len(d) {
    67  		t.Fatalf("Got %d devices, wanted %d", len(d), len(n.(*bus).Devices)-1)
    68  	}
    69  
    70  }