|
| 1 | +{test, wait, next} = require('./test-helper.coffee') |
| 2 | +assert = require('assert') |
| 3 | +usb = require("../usb.js") |
| 4 | + |
| 5 | +test "Basic constants must exist", -> |
| 6 | + assert.notEqual(usb, undefined, "usb must be undefined") |
| 7 | + assert.ok((usb.LIBUSB_CLASS_PER_INTERFACE != undefined), "Constants must be defined") |
| 8 | + assert.ok((usb.LIBUSB_ENDPOINT_IN == 128)) |
| 9 | + assert.notEqual(usb.revision, "unknown", "Revision should not unknown") |
| 10 | + |
| 11 | +test "setDebugLevel must error with invalid args", -> |
| 12 | + assert.throws((-> usb.setDebugLevel()), TypeError) |
| 13 | + assert.throws((-> usb.setDebugLevel(-1)), TypeError) |
| 14 | + assert.throws((-> usb.setDebugLevel(4)), TypeError) |
| 15 | + |
| 16 | +test "setDebugLevel must succeed with good args", -> |
| 17 | + assert.doesNotThrow(-> usb.setDebugLevel(0)) |
| 18 | + |
| 19 | +devices = null |
| 20 | + |
| 21 | +test "getDevices works", -> |
| 22 | + devices = usb.getDevices() |
| 23 | + assert.notEqual(devices, undefined, "getDevices() must not be undefined") |
| 24 | + assert.ok((devices.length > 0), "getDevices() must be larger than 0 (assume that at least one host controller is available)") |
| 25 | + |
| 26 | +assert_extra_length = (obj) -> |
| 27 | + r = obj.getExtraData() |
| 28 | + assert.ok((r.length == obj.extra_length), "getExtraLength() (length is: " + r.length + ") + must be equal to .extra_length (is: " + obj.extra_length + ")") |
| 29 | + |
| 30 | + |
| 31 | +arr = null |
| 32 | +device = null |
| 33 | + |
| 34 | +test "Finding demo device", -> |
| 35 | + arr = usb.find_by_vid_and_pid(0x59e3, 0x0a23) |
| 36 | + assert.ok((arr != undefined), "usb.find_by_vid_and_pid() must return array") |
| 37 | + assert.ok((arr.length > 0), "usb.find_by_vid_and_pid() must return array with length > 0") |
| 38 | + device = arr[0] |
| 39 | + |
| 40 | +test "Device properties are sane" , -> |
| 41 | + assert.ok((device.busNumber > 0), "deviceAddress must be larger than 0") |
| 42 | + assert.ok((device.deviceAddress > 0), "deviceAddress must be larger than 0") |
| 43 | + |
| 44 | +test "deviceDescriptor must return an object", -> |
| 45 | + assert.ok(((deviceDesc = device.deviceDescriptor) != undefined)) |
| 46 | + |
| 47 | +test "getConfigDescriptor() must return an object", -> |
| 48 | + assert.ok(((deviceConfigDesc = device.getConfigDescriptor()) != undefined)) |
| 49 | + |
| 50 | +test "Invalid timeout is an error", -> |
| 51 | + assert.throws(-> device.timeout = 'foo') |
| 52 | + |
| 53 | +test "Default timeout is 1000", -> |
| 54 | + assert.equal(device.timeout, 1000); |
| 55 | + |
| 56 | +test "Changing timeout", -> |
| 57 | + device.timeout = 100; |
| 58 | + assert.equal(device.timeout, 100); |
| 59 | + |
| 60 | +b = new Buffer([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]) |
| 61 | + |
| 62 | +test "Control transfer out", -> |
| 63 | + device.controlTransfer 0x40, 0x81, 0, 0, b, (d, e) -> |
| 64 | + console.log("ControlTransferOut", d, e) |
| 65 | + assert.ok(e == undefined, e) |
| 66 | + next() |
| 67 | + wait() |
| 68 | + |
| 69 | +test "Control transfer fails when bmRequestType doesn't match buffer / length", -> |
| 70 | + assert.throws(-> device.controlTransfer(0x40, 0x81, 0, 0, 64)) |
| 71 | + |
| 72 | +test "Control transfer in", -> |
| 73 | + device.controlTransfer 0xc0, 0x81, 0, 0, 64, (d, e) -> |
| 74 | + console.log("ControlTransferIn", d, e) |
| 75 | + assert.ok(e == undefined, e) |
| 76 | + assert.equal(d.toString(), b.toString()) |
| 77 | + next() |
| 78 | + wait() |
| 79 | + |
| 80 | +interface = null |
| 81 | +test "Get interface", -> |
| 82 | + interfaces = device.getInterfaces() |
| 83 | + interface = interfaces[0] |
| 84 | + assert.notEqual(interface, undefined, "Interface must be defined") |
| 85 | + |
| 86 | +test "Claim interface", -> |
| 87 | + interface.claim() |
| 88 | + |
| 89 | +endpoints = null |
| 90 | +inEndpoint = null |
| 91 | +outEndpoint = null |
| 92 | + |
| 93 | +test "Get in endpoint", -> |
| 94 | + endpoints = interface.getEndpoints() |
| 95 | + inEndpoint = endpoints[0] |
| 96 | + assert.notEqual(inEndpoint, undefined, "Endpoint must be defined") |
| 97 | + assert.equal(inEndpoint.__endpointType, usb.LIBUSB_ENDPOINT_IN) |
| 98 | + |
| 99 | +test "Attempt to write to IN endpoint", -> |
| 100 | + assert.throws -> inEndpoint.transfer(b) |
| 101 | + |
| 102 | +test "Read from IN endpoint", -> |
| 103 | + inEndpoint.transfer 64, (d, e) -> |
| 104 | + console.log("BulkTransferIn", d, e) |
| 105 | + assert.ok(e == undefined, e) |
| 106 | + next() |
| 107 | + wait() |
| 108 | + |
| 109 | +test "Get out endpoint", -> |
| 110 | + outEndpoint = endpoints[1] |
| 111 | + assert.notEqual(outEndpoint, undefined, "Endpoint must be defined") |
| 112 | + assert.equal(outEndpoint.__endpointType, usb.LIBUSB_ENDPOINT_OUT) |
| 113 | + |
| 114 | +test "Attempt to read from OUT endpoint", -> |
| 115 | + assert.throws -> outEndpoint.transfer(64) |
| 116 | + |
| 117 | +test "Write to OUT endpoint", -> |
| 118 | + outEndpoint.transfer b, (d, e) -> |
| 119 | + console.log("BulkTransferIn", d, e) |
| 120 | + next() |
| 121 | + wait() |
| 122 | + |
| 123 | +test "Complete!", -> |
| 124 | + |
0 commit comments