github.com/jaypipes/ghw@v0.21.1/pkg/usb/usb_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  //
     5  // Use and distribution licensed under the Apache license version 2.
     6  //
     7  // See the COPYING file in the root project directory for full text.
     8  //
     9  
    10  package usb
    11  
    12  import (
    13  	"os"
    14  	"path/filepath"
    15  	"reflect"
    16  	"testing"
    17  )
    18  
    19  func TestUSB(t *testing.T) {
    20  	usbDir, err := os.MkdirTemp("", "TestUSB")
    21  	if err != nil {
    22  		t.Fatalf("could not create temp directory: %v", err)
    23  	}
    24  
    25  	data := `
    26  DEVTYPE=usb_interface
    27  DRIVER=usbhid
    28  PRODUCT=46a/a087/101
    29  TYPE=0/0/0
    30  INTERFACE=3/1/2
    31  MODALIAS=usb:v046ApA087d0101dc00dsc00dp00ic03isc01ip02in00
    32  	`
    33  
    34  	err = os.WriteFile(filepath.Join(usbDir, "uevent"), []byte(data), 0600)
    35  	if err != nil {
    36  		t.Fatalf("could not write uevent file in %s: %+v", usbDir, err)
    37  	}
    38  
    39  	var d Device
    40  	err = fillUSBFromUevent(usbDir, &d)
    41  	if err != nil {
    42  		t.Fatalf("could not fill USB info from uevent file: %v", err)
    43  	}
    44  
    45  	deviceExpected := Device{
    46  		Driver:     "usbhid",
    47  		Type:       "0/0/0",
    48  		VendorID:   "46a",
    49  		ProductID:  "a087",
    50  		RevisionID: "101",
    51  	}
    52  
    53  	if !reflect.DeepEqual(d, deviceExpected) {
    54  		t.Fatalf("expected: %+v, but got %+v", deviceExpected, d)
    55  	}
    56  
    57  }