usb 2.4.3 → 2.5.2-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/usb.coffee CHANGED
@@ -7,208 +7,208 @@ findBySerialNumber = require('../').findBySerialNumber
7
7
  Worker = require('worker_threads').Worker
8
8
 
9
9
  if typeof gc is 'function'
10
- # running with --expose-gc, do a sweep between tests so valgrind blames the right one
11
- afterEach -> gc()
10
+ # running with --expose-gc, do a sweep between tests so valgrind blames the right one
11
+ afterEach -> gc()
12
12
 
13
13
  describe 'USB Module', ->
14
- it 'should describe basic constants', ->
15
- assert.notEqual(usb, undefined, "usb must be undefined")
16
- assert.ok((usb.LIBUSB_CLASS_PER_INTERFACE != undefined), "Constants must be described")
17
- assert.ok((usb.LIBUSB_ENDPOINT_IN == 128))
14
+ it 'should describe basic constants', ->
15
+ assert.notEqual(usb, undefined, "usb must be undefined")
16
+ assert.ok((usb.LIBUSB_CLASS_PER_INTERFACE != undefined), "Constants must be described")
17
+ assert.ok((usb.LIBUSB_ENDPOINT_IN == 128))
18
18
 
19
- it 'should handle abuse without crashing', ->
20
- assert.throws -> new usb.Device()
21
- assert.throws -> usb.Device()
22
- assert.throws -> usb.Device.prototype.open.call({})
19
+ it 'should handle abuse without crashing', ->
20
+ assert.throws -> new usb.Device()
21
+ assert.throws -> usb.Device()
22
+ assert.throws -> usb.Device.prototype.open.call({})
23
23
 
24
24
  describe 'setDebugLevel', ->
25
- it 'should throw when passed invalid args', ->
26
- assert.throws((-> usb.setDebugLevel()), TypeError)
27
- assert.throws((-> usb.setDebugLevel(-1)), TypeError)
28
- assert.throws((-> usb.setDebugLevel(5)), TypeError)
25
+ it 'should throw when passed invalid args', ->
26
+ assert.throws((-> usb.setDebugLevel()), TypeError)
27
+ assert.throws((-> usb.setDebugLevel(-1)), TypeError)
28
+ assert.throws((-> usb.setDebugLevel(5)), TypeError)
29
29
 
30
- it 'should succeed with good args', ->
31
- assert.doesNotThrow(-> usb.setDebugLevel(0))
30
+ it 'should succeed with good args', ->
31
+ assert.doesNotThrow(-> usb.setDebugLevel(0))
32
32
 
33
33
  describe 'getDeviceList', ->
34
- it 'should return at least one device', ->
35
- l = getDeviceList()
36
- assert.ok((l.length > 0))
34
+ it 'should return at least one device', ->
35
+ l = getDeviceList()
36
+ assert.ok((l.length > 0))
37
37
 
38
38
  describe 'findByIds', ->
39
- it 'should return an array with length > 0', ->
40
- dev = findByIds(0x59e3, 0x0a23)
41
- assert.ok(dev, "Demo device is not attached")
39
+ it 'should return an array with length > 0', ->
40
+ dev = findByIds(0x59e3, 0x0a23)
41
+ assert.ok(dev, "Demo device is not attached")
42
42
 
43
43
  describe 'findBySerialNumber', ->
44
- it 'should return a single device ', ->
45
- dev = findBySerialNumber('TEST_DEVICE')
46
- assert.ok(dev, "Demo device is not attached")
44
+ it 'should return a single device ', ->
45
+ dev = findBySerialNumber('TEST_DEVICE')
46
+ assert.ok(dev, "Demo device is not attached")
47
47
 
48
48
  describe 'Device', ->
49
- device = null
50
- before ->
51
- device = findByIds(0x59e3, 0x0a23)
52
-
53
- it 'should have sane properties', ->
54
- assert.ok((device.busNumber > 0), "busNumber must be larger than 0")
55
- assert.ok((device.deviceAddress > 0), "deviceAddress must be larger than 0")
56
- if process.platform != 'darwin' || process.arch != 'arm64'
57
- assert.ok((util.isArray(device.portNumbers)), "portNumbers must be an array")
58
-
59
- it 'should have a deviceDescriptor property', ->
60
- assert.ok(((deviceDesc = device.deviceDescriptor) != undefined))
61
-
62
- it 'should have a configDescriptor property', ->
63
- assert.ok(device.configDescriptor != undefined)
64
-
65
- it 'should open', ->
66
- device.open()
67
-
68
- it 'gets string descriptors', (done) ->
69
- device.getStringDescriptor device.deviceDescriptor.iManufacturer, (e, s) ->
70
- assert.ok(e == undefined, e)
71
- assert.equal(s, 'Nonolith Labs')
72
- done()
73
-
74
- it 'supports null string descriptors', (done) ->
75
- device.getStringDescriptor device.configDescriptor.iConfiguration, (e, s) ->
76
- assert.ok(e == undefined, e)
77
- assert.equal(s, undefined)
78
- done()
79
-
80
- describe 'control transfer', ->
81
- b = Buffer.from([0x30...0x40])
82
- it 'should OUT transfer when the IN bit is not set', (done) ->
83
- device.controlTransfer 0x40, 0x81, 0, 0, b, (e) ->
84
- assert.ok(e == undefined, e)
85
- done()
86
-
87
- it 'should fail when bmRequestType doesn\'t match buffer / length', ->
88
- assert.throws(-> device.controlTransfer(0x40, 0x81, 0, 0, 64))
89
-
90
- it 'should IN transfer when the IN bit is set', (done) ->
91
- device.controlTransfer 0xc0, 0x81, 0, 0, 128, (e, d) ->
92
- #console.log("ControlTransferIn", d, e)
93
- assert.ok(e == undefined, e)
94
- assert.equal(d.toString(), b.toString())
95
- done()
96
-
97
- it 'should signal errors', (done) ->
98
- device.controlTransfer 0xc0, 0xff, 0, 0, 64, (e, d) ->
99
- assert.equal e.errno, usb.LIBUSB_TRANSFER_STALL
100
- done()
101
-
102
- describe 'Interface', ->
103
- iface = null
104
- before ->
105
- iface = device.interfaces[0]
106
- iface.claim()
107
-
108
- it 'should have one interface', ->
109
- assert.notEqual(iface, undefined)
110
-
111
- it 'should be the same as the interfaceNo 0', ->
112
- assert.strictEqual iface, device.interface(0)
113
-
114
- if process.platform == 'linux'
115
- it "shouldn't have a kernel driver", ->
116
- assert.equal iface.isKernelDriverActive(), false
117
-
118
- it "should fail to detach the kernel driver", ->
119
- assert.throws -> iface.detachKernelDriver()
120
-
121
- it "should fail to attach the kernel driver", ->
122
- assert.throws -> iface.attachKernelDriver()
123
-
124
- describe 'IN endpoint', ->
125
- inEndpoint = null
126
- before ->
127
- inEndpoint = iface.endpoints[0]
128
-
129
- it 'should be able to get the endpoint', ->
130
- assert.ok inEndpoint?
131
-
132
- it 'should be able to get the endpoint by address', ->
133
- assert.equal(inEndpoint, iface.endpoint(0x81))
134
-
135
- it 'should have the IN direction flag', ->
136
- assert.equal(inEndpoint.direction, 'in')
137
-
138
- it 'should have a descriptor', ->
139
- assert.equal(inEndpoint.descriptor.bEndpointAddress, 0x81)
140
- assert.equal(inEndpoint.descriptor.wMaxPacketSize, 64)
141
-
142
- it 'should fail to write', ->
143
- assert.throws -> inEndpoint.transfer(b)
144
-
145
- it 'should support read', (done) ->
146
- inEndpoint.transfer 64, (e, d) ->
147
- assert.ok(e == undefined, e)
148
- assert.ok(d.length == 64)
149
- done()
150
-
151
- it 'times out', (done) ->
152
- iface.endpoints[2].timeout = 20
153
- iface.endpoints[2].transfer 64, (e, d) ->
154
- assert.equal e.errno, usb.LIBUSB_TRANSFER_TIMED_OUT
155
- done()
49
+ device = null
50
+ before ->
51
+ device = findByIds(0x59e3, 0x0a23)
52
+
53
+ it 'should have sane properties', ->
54
+ assert.ok((device.busNumber > 0), "busNumber must be larger than 0")
55
+ assert.ok((device.deviceAddress > 0), "deviceAddress must be larger than 0")
56
+ if process.platform != 'darwin' || process.arch != 'arm64'
57
+ assert.ok((util.isArray(device.portNumbers)), "portNumbers must be an array")
58
+
59
+ it 'should have a deviceDescriptor property', ->
60
+ assert.ok(((deviceDesc = device.deviceDescriptor) != undefined))
61
+
62
+ it 'should have a configDescriptor property', ->
63
+ assert.ok(device.configDescriptor != undefined)
64
+
65
+ it 'should open', ->
66
+ device.open()
67
+
68
+ it 'gets string descriptors', (done) ->
69
+ device.getStringDescriptor device.deviceDescriptor.iManufacturer, (e, s) ->
70
+ assert.ok(e == undefined, e)
71
+ assert.equal(s, 'Nonolith Labs')
72
+ done()
73
+
74
+ it 'supports null string descriptors', (done) ->
75
+ device.getStringDescriptor device.configDescriptor.iConfiguration, (e, s) ->
76
+ assert.ok(e == undefined, e)
77
+ assert.equal(s, undefined)
78
+ done()
79
+
80
+ describe 'control transfer', ->
81
+ b = Buffer.from([0x30...0x40])
82
+ it 'should OUT transfer when the IN bit is not set', (done) ->
83
+ device.controlTransfer 0x40, 0x81, 0, 0, b, (e) ->
84
+ assert.ok(e == undefined, e)
85
+ done()
86
+
87
+ it 'should fail when bmRequestType doesn\'t match buffer / length', ->
88
+ assert.throws(-> device.controlTransfer(0x40, 0x81, 0, 0, 64))
89
+
90
+ it 'should IN transfer when the IN bit is set', (done) ->
91
+ device.controlTransfer 0xc0, 0x81, 0, 0, 128, (e, d) ->
92
+ #console.log("ControlTransferIn", d, e)
93
+ assert.ok(e == undefined, e)
94
+ assert.equal(d.toString(), b.toString())
95
+ done()
96
+
97
+ it 'should signal errors', (done) ->
98
+ device.controlTransfer 0xc0, 0xff, 0, 0, 64, (e, d) ->
99
+ assert.equal e.errno, usb.LIBUSB_TRANSFER_STALL
100
+ done()
101
+
102
+ describe 'Interface', ->
103
+ iface = null
104
+ before ->
105
+ iface = device.interfaces[0]
106
+ iface.claim()
107
+
108
+ it 'should have one interface', ->
109
+ assert.notEqual(iface, undefined)
110
+
111
+ it 'should be the same as the interfaceNo 0', ->
112
+ assert.strictEqual iface, device.interface(0)
113
+
114
+ if process.platform == 'linux'
115
+ it "shouldn't have a kernel driver", ->
116
+ assert.equal iface.isKernelDriverActive(), false
117
+
118
+ it "should fail to detach the kernel driver", ->
119
+ assert.throws -> iface.detachKernelDriver()
120
+
121
+ it "should fail to attach the kernel driver", ->
122
+ assert.throws -> iface.attachKernelDriver()
123
+
124
+ describe 'IN endpoint', ->
125
+ inEndpoint = null
126
+ before ->
127
+ inEndpoint = iface.endpoints[0]
128
+
129
+ it 'should be able to get the endpoint', ->
130
+ assert.ok inEndpoint?
131
+
132
+ it 'should be able to get the endpoint by address', ->
133
+ assert.equal(inEndpoint, iface.endpoint(0x81))
134
+
135
+ it 'should have the IN direction flag', ->
136
+ assert.equal(inEndpoint.direction, 'in')
137
+
138
+ it 'should have a descriptor', ->
139
+ assert.equal(inEndpoint.descriptor.bEndpointAddress, 0x81)
140
+ assert.equal(inEndpoint.descriptor.wMaxPacketSize, 64)
141
+
142
+ it 'should fail to write', ->
143
+ assert.throws -> inEndpoint.transfer(b)
144
+
145
+ it 'should support read', (done) ->
146
+ inEndpoint.transfer 64, (e, d) ->
147
+ assert.ok(e == undefined, e)
148
+ assert.ok(d.length == 64)
149
+ done()
150
+
151
+ it 'times out', (done) ->
152
+ iface.endpoints[2].timeout = 20
153
+ iface.endpoints[2].transfer 64, (e, d) ->
154
+ assert.equal e.errno, usb.LIBUSB_TRANSFER_TIMED_OUT
155
+ done()
156
156
 
157
- it 'polls the device', (done) ->
158
- pkts = 0
157
+ it 'polls the device', (done) ->
158
+ pkts = 0
159
159
 
160
- inEndpoint.startPoll 8, 64
161
- inEndpoint.on 'data', (d) ->
162
- assert.equal d.length, 64
163
- pkts++
160
+ inEndpoint.startPoll 8, 64
161
+ inEndpoint.on 'data', (d) ->
162
+ assert.equal d.length, 64
163
+ pkts++
164
164
 
165
- if pkts == 100
166
- inEndpoint.stopPoll()
165
+ if pkts == 100
166
+ inEndpoint.stopPoll()
167
167
 
168
- inEndpoint.on 'error', (e) ->
169
- throw e
170
-
171
- inEndpoint.on 'end', ->
172
- #console.log("Stream stopped")
173
- done()
174
-
175
- describe 'OUT endpoint', ->
176
- outEndpoint = null
177
- before ->
178
- outEndpoint = iface.endpoints[1]
168
+ inEndpoint.on 'error', (e) ->
169
+ throw e
170
+
171
+ inEndpoint.on 'end', ->
172
+ #console.log("Stream stopped")
173
+ done()
174
+
175
+ describe 'OUT endpoint', ->
176
+ outEndpoint = null
177
+ before ->
178
+ outEndpoint = iface.endpoints[1]
179
179
 
180
- it 'should be able to get the endpoint', ->
181
- assert.ok outEndpoint?
180
+ it 'should be able to get the endpoint', ->
181
+ assert.ok outEndpoint?
182
182
 
183
- it 'should be able to get the endpoint by address', ->
184
- assert.equal(outEndpoint, iface.endpoint(0x02))
183
+ it 'should be able to get the endpoint by address', ->
184
+ assert.equal(outEndpoint, iface.endpoint(0x02))
185
185
 
186
- it 'should have the OUT direction flag', ->
187
- assert.equal(outEndpoint.direction, 'out')
186
+ it 'should have the OUT direction flag', ->
187
+ assert.equal(outEndpoint.direction, 'out')
188
188
 
189
- it 'should support write', (done) ->
190
- outEndpoint.transfer [1,2,3,4], (e) ->
191
- assert.ok(e == undefined, e)
192
- done()
189
+ it 'should support write', (done) ->
190
+ outEndpoint.transfer [1,2,3,4], (e) ->
191
+ assert.ok(e == undefined, e)
192
+ done()
193
193
 
194
- it 'times out', (done) ->
195
- iface.endpoints[3].timeout = 20
196
- iface.endpoints[3].transfer [1,2,3,4], (e) ->
197
- assert.equal e.errno, usb.LIBUSB_TRANSFER_TIMED_OUT
198
- done()
194
+ it 'times out', (done) ->
195
+ iface.endpoints[3].timeout = 20
196
+ iface.endpoints[3].transfer [1,2,3,4], (e) ->
197
+ assert.equal e.errno, usb.LIBUSB_TRANSFER_TIMED_OUT
198
+ done()
199
199
 
200
- after (cb) ->
201
- iface.release(cb)
200
+ after (cb) ->
201
+ iface.release(cb)
202
202
 
203
- after ->
204
- device.close()
203
+ after ->
204
+ device.close()
205
205
 
206
206
  if process.platform != 'win32'
207
- describe 'Context Aware', ->
208
- it 'should handle opening the same device from different contexts', ->
209
- for n in [1..5]
210
- worker = new Worker('./test/worker.cjs')
211
- worker.on 'message', (serial) ->
212
- assert.equal(serial, 'TEST_DEVICE')
213
- worker.on 'exit', (code) ->
214
- assert.equal(code, 0)
207
+ describe 'Context Aware', ->
208
+ it 'should handle opening the same device from different contexts', ->
209
+ for n in [1..5]
210
+ worker = new Worker('./test/worker.cjs')
211
+ worker.on 'message', (serial) ->
212
+ assert.equal(serial, 'TEST_DEVICE')
213
+ worker.on 'exit', (code) ->
214
+ assert.equal(code, 0)