From b6794b7d45b7cfddf6d81b465e0528f270c1ed57 Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Mon, 10 Apr 2017 01:25:51 +0200 Subject: [PATCH] Add a test for InEndpoint.Read. Use ID for vid/pid in OpenDevice... --- usb/endpoint_test.go | 31 +++++++++++++++++++++++++++++++ usb/usb.go | 2 +- usb/usb_test.go | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/usb/endpoint_test.go b/usb/endpoint_test.go index ed3b8d3..fc1c0a2 100644 --- a/usb/endpoint_test.go +++ b/usb/endpoint_test.go @@ -146,3 +146,34 @@ func TestEndpointInfo(t *testing.T) { } } } + +func TestEndpointIn(t *testing.T) { + defer func(i libusbIntf) { libusb = i }(libusb) + + lib := newFakeLibusb() + libusb = lib + + ctx := NewContext() + d, err := ctx.OpenDeviceWithVidPid(0x9999, 0x0001) + if err != nil { + t.Fatalf("OpenDeviceWithVidPid(0x9999, 0x0001): got error %v, want nil", err) + } + ep, err := d.InEndpoint(1, 0, 0, 2) + if err != nil { + t.Fatalf("InEndpoint(1, 0, 0, 2): got error %v, want nil", err) + } + dataTransferred := 100 + go func() { + fakeT := lib.waitForSubmitted() + fakeT.length = dataTransferred + fakeT.status = TransferCompleted + close(fakeT.done) + }() + buf := make([]byte, 512) + got, err := ep.Read(buf) + if err != nil { + t.Errorf("ep.Read: got error %v, want nil", err) + } else if got != dataTransferred { + t.Errorf("ep.Read: got %d, want %d", got, dataTransferred) + } +} diff --git a/usb/usb.go b/usb/usb.go index 0e767da..0df0696 100644 --- a/usb/usb.go +++ b/usb/usb.go @@ -82,7 +82,7 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro // If there were any errors during device list traversal, it is possible // it will return a non-nil device and non-nil error. A Device.Close() must // be called to release the device if the returned device wasn't nil. -func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) { +func (c *Context) OpenDeviceWithVidPid(vid, pid ID) (*Device, error) { var found bool devs, err := c.ListDevices(func(desc *Descriptor) bool { if found { diff --git a/usb/usb_test.go b/usb/usb_test.go index cad8646..d121a25 100644 --- a/usb/usb_test.go +++ b/usb/usb_test.go @@ -60,7 +60,7 @@ func TestOpenDeviceWithVidPid(t *testing.T) { libusb = newFakeLibusb() for _, d := range []struct { - vid, pid int + vid, pid ID exists bool }{ {0x7777, 0x0003, false},