@stoprocent/noble 1.19.0 → 2.0.0
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/README.md +393 -650
- package/examples/advertisement-discovery.js +57 -48
- package/examples/connect-address.js +59 -34
- package/examples/echo.js +59 -69
- package/examples/enter-exit.js +55 -49
- package/examples/multiple-bindings.js +53 -0
- package/examples/peripheral-explorer-async.js +39 -21
- package/examples/peripheral-explorer.ts +52 -0
- package/index.d.ts +249 -209
- package/index.js +4 -1
- package/jest.config.js +4 -0
- package/lib/characteristic.js +153 -127
- package/lib/{win/src/callbacks.h → common/include/Emit.h} +17 -14
- package/lib/common/include/Peripheral.h +31 -0
- package/lib/common/include/ThreadSafeCallback.h +95 -0
- package/lib/{win/src/callbacks.cc → common/src/Emit.cc} +111 -68
- package/lib/descriptor.js +57 -54
- package/lib/hci-socket/acl-stream.js +2 -4
- package/lib/hci-socket/bindings.js +96 -73
- package/lib/hci-socket/gap.js +2 -3
- package/lib/hci-socket/gatt.js +2 -5
- package/lib/hci-socket/hci.js +19 -7
- package/lib/hci-socket/signaling.js +2 -3
- package/lib/hci-socket/smp.js +2 -3
- package/lib/hci-socket/vs.js +1 -0
- package/lib/mac/binding.gyp +5 -7
- package/lib/mac/bindings.js +1 -3
- package/lib/mac/src/ble_manager.h +1 -8
- package/lib/mac/src/ble_manager.mm +87 -44
- package/lib/mac/src/napi_objc.h +1 -0
- package/lib/mac/src/napi_objc.mm +0 -6
- package/lib/mac/src/noble_mac.h +5 -3
- package/lib/mac/src/noble_mac.mm +99 -57
- package/lib/mac/src/objc_cpp.h +3 -2
- package/lib/mac/src/objc_cpp.mm +0 -6
- package/lib/noble.js +579 -488
- package/lib/peripheral.js +171 -174
- package/lib/resolve-bindings.js +37 -30
- package/lib/service.js +58 -55
- package/lib/win/binding.gyp +4 -11
- package/lib/win/bindings.js +1 -3
- package/lib/win/src/ble_manager.cc +291 -166
- package/lib/win/src/ble_manager.h +11 -13
- package/lib/win/src/napi_winrt.cc +1 -7
- package/lib/win/src/napi_winrt.h +1 -1
- package/lib/win/src/noble_winrt.cc +88 -61
- package/lib/win/src/noble_winrt.h +5 -3
- package/lib/win/src/notify_map.cc +0 -7
- package/lib/win/src/notify_map.h +1 -8
- package/lib/win/src/peripheral_winrt.cc +29 -11
- package/lib/win/src/peripheral_winrt.h +1 -1
- package/lib/win/src/radio_watcher.cc +79 -69
- package/lib/win/src/radio_watcher.h +30 -11
- package/lib/win/src/winrt_cpp.cc +1 -1
- package/lib/win/src/winrt_cpp.h +3 -0
- package/package.json +14 -17
- package/prebuilds/darwin-x64+arm64/@stoprocent+noble.node +0 -0
- package/prebuilds/win32-ia32/@stoprocent+noble.node +0 -0
- package/prebuilds/win32-x64/@stoprocent+noble.node +0 -0
- package/test/lib/characteristic.test.js +202 -322
- package/test/lib/descriptor.test.js +62 -95
- package/test/lib/hci-socket/acl-stream.test.js +112 -108
- package/test/lib/hci-socket/bindings.test.js +576 -365
- package/test/lib/hci-socket/hci.test.js +442 -473
- package/test/lib/hci-socket/signaling.test.js +45 -48
- package/test/lib/hci-socket/smp.test.js +144 -142
- package/test/lib/hci-socket/vs.test.js +193 -18
- package/test/lib/peripheral.test.js +492 -322
- package/test/lib/resolve-bindings.test.js +207 -82
- package/test/lib/service.test.js +79 -88
- package/test/noble.test.js +381 -1085
- package/.editorconfig +0 -11
- package/.nycrc.json +0 -4
- package/codecov.yml +0 -5
- package/examples/cache-gatt-discovery.js +0 -198
- package/examples/cache-gatt-reconnect.js +0 -164
- package/examples/ext-advertisement-discovery.js +0 -65
- package/examples/peripheral-explorer.js +0 -225
- package/examples/pizza/central.js +0 -194
- package/examples/pizza/pizza.js +0 -60
- package/examples/test/test.custom.js +0 -131
- package/examples/uart-bind-params.js +0 -28
- package/lib/distributed/bindings.js +0 -326
- package/lib/mac/src/callbacks.cc +0 -222
- package/lib/mac/src/callbacks.h +0 -84
- package/lib/mac/src/peripheral.h +0 -23
- package/lib/resolve-bindings-web.js +0 -9
- package/lib/webbluetooth/bindings.js +0 -368
- package/lib/websocket/bindings.js +0 -321
- package/lib/win/src/peripheral.h +0 -23
- package/test/lib/distributed/bindings.test.js +0 -918
- package/test/lib/webbluetooth/bindings.test.js +0 -190
- package/test/lib/websocket/bindings.test.js +0 -456
- package/test/mocha.setup.js +0 -0
- package/with-bindings.js +0 -5
- package/with-custom-binding.js +0 -6
- package/ws-slave.js +0 -404
|
@@ -1,94 +1,91 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// Mock the os module
|
|
2
|
+
jest.mock('os', () => ({
|
|
3
|
+
platform: jest.fn()
|
|
4
|
+
}));
|
|
4
5
|
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const Signaling = proxyquire('../../../lib/hci-socket/signaling', { os: fakeOs });
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const Signaling = require('../../../lib/hci-socket/signaling');
|
|
8
8
|
|
|
9
9
|
describe('hci-socket signaling', () => {
|
|
10
10
|
let signaling;
|
|
11
|
-
|
|
11
|
+
let aclStream;
|
|
12
12
|
const handle = 'handle';
|
|
13
|
-
const aclStream = {
|
|
14
|
-
on: sinon.spy(),
|
|
15
|
-
removeListener: sinon.spy(),
|
|
16
|
-
write: sinon.spy()
|
|
17
|
-
};
|
|
18
13
|
|
|
19
14
|
beforeEach(() => {
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
// Reset mocks
|
|
16
|
+
jest.clearAllMocks();
|
|
17
|
+
|
|
18
|
+
// Setup aclStream mock
|
|
19
|
+
aclStream = {
|
|
20
|
+
on: jest.fn(),
|
|
21
|
+
removeListener: jest.fn(),
|
|
22
|
+
write: jest.fn()
|
|
23
|
+
};
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
sinon.reset();
|
|
25
|
+
signaling = new Signaling(handle, aclStream);
|
|
25
26
|
});
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
test('construct', () => {
|
|
29
|
+
expect(aclStream.on).toHaveBeenCalledTimes(2);
|
|
30
|
+
expect(aclStream.on).toHaveBeenCalledWith('data', expect.any(Function));
|
|
31
|
+
expect(aclStream.on).toHaveBeenCalledWith('end', expect.any(Function));
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
expect(signaling._handle).toBe(handle);
|
|
34
|
+
expect(signaling._aclStream).toBe(aclStream);
|
|
34
35
|
});
|
|
35
36
|
|
|
36
37
|
describe('onAclStreamData', () => {
|
|
37
38
|
beforeEach(() => {
|
|
38
|
-
signaling.processConnectionParameterUpdateRequest =
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
afterEach(() => {
|
|
42
|
-
sinon.reset();
|
|
39
|
+
signaling.processConnectionParameterUpdateRequest = jest.fn();
|
|
43
40
|
});
|
|
44
41
|
|
|
45
|
-
|
|
42
|
+
test('should do nothing as not SIGNALING_CID', () => {
|
|
46
43
|
signaling.onAclStreamData(0, 'data');
|
|
47
|
-
|
|
44
|
+
expect(signaling.processConnectionParameterUpdateRequest).not.toHaveBeenCalled();
|
|
48
45
|
});
|
|
49
46
|
|
|
50
|
-
|
|
47
|
+
test('should do nothing as not CONNECTION_PARAMETER_UPDATE_REQUEST', () => {
|
|
51
48
|
const data = Buffer.from([0, 1, 2, 3, 4]);
|
|
52
49
|
signaling.onAclStreamData(5, data);
|
|
53
|
-
|
|
50
|
+
expect(signaling.processConnectionParameterUpdateRequest).not.toHaveBeenCalled();
|
|
54
51
|
});
|
|
55
52
|
|
|
56
|
-
|
|
53
|
+
test('should call processConnectionParameterUpdateRequest', () => {
|
|
57
54
|
const data = Buffer.from([18, 1, 2, 3, 4, 5]);
|
|
58
55
|
signaling.onAclStreamData(5, data);
|
|
59
|
-
|
|
56
|
+
expect(signaling.processConnectionParameterUpdateRequest).toHaveBeenCalledWith(1, Buffer.from([4, 5]));
|
|
60
57
|
});
|
|
61
58
|
});
|
|
62
59
|
|
|
63
|
-
|
|
60
|
+
test('onAclStreamEnd', () => {
|
|
64
61
|
signaling.onAclStreamEnd();
|
|
65
62
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
63
|
+
expect(aclStream.removeListener).toHaveBeenCalledTimes(2);
|
|
64
|
+
expect(aclStream.removeListener).toHaveBeenCalledWith('data', expect.any(Function));
|
|
65
|
+
expect(aclStream.removeListener).toHaveBeenCalledWith('end', expect.any(Function));
|
|
69
66
|
});
|
|
70
67
|
|
|
71
68
|
describe('processConnectionParameterUpdateRequest', () => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const callback =
|
|
69
|
+
test('should not write on linux', () => {
|
|
70
|
+
os.platform.mockReturnValue('linux');
|
|
71
|
+
const callback = jest.fn();
|
|
75
72
|
|
|
76
73
|
signaling.on('connectionParameterUpdateRequest', callback);
|
|
77
74
|
signaling.processConnectionParameterUpdateRequest(1, Buffer.from([1, 0, 2, 0, 3, 0, 4, 0]));
|
|
78
75
|
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
expect(callback).not.toHaveBeenCalled();
|
|
77
|
+
expect(aclStream.write).not.toHaveBeenCalled();
|
|
81
78
|
});
|
|
82
79
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const callback =
|
|
80
|
+
test('should write on !linux', () => {
|
|
81
|
+
os.platform.mockReturnValue('!linux');
|
|
82
|
+
const callback = jest.fn();
|
|
86
83
|
|
|
87
84
|
signaling.on('connectionParameterUpdateRequest', callback);
|
|
88
85
|
signaling.processConnectionParameterUpdateRequest(1, Buffer.from([1, 0, 2, 0, 3, 0, 4, 0]));
|
|
89
86
|
|
|
90
|
-
|
|
91
|
-
|
|
87
|
+
expect(callback).toHaveBeenCalledWith(handle, 1.25, 2.5, 3, 40);
|
|
88
|
+
expect(aclStream.write).toHaveBeenCalledWith(5, Buffer.from([0x13, 0x01, 0x02, 0x00, 0x00, 0x00]));
|
|
92
89
|
});
|
|
93
90
|
});
|
|
94
|
-
});
|
|
91
|
+
});
|
|
@@ -1,268 +1,270 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// Mock the crypto module
|
|
2
|
+
jest.mock('../../../lib/hci-socket/crypto', () => ({
|
|
3
|
+
r: jest.fn(),
|
|
4
|
+
c1: jest.fn(),
|
|
5
|
+
s1: jest.fn()
|
|
6
|
+
}));
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const Smp =
|
|
8
|
+
// Import the mocked modules
|
|
9
|
+
const crypto = require('../../../lib/hci-socket/crypto');
|
|
10
|
+
const Smp = require('../../../lib/hci-socket/smp');
|
|
8
11
|
|
|
9
12
|
describe('hci-socket smp', () => {
|
|
10
13
|
let smp;
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
let aclStream;
|
|
15
|
+
|
|
13
16
|
const localAddressType = 'public';
|
|
14
17
|
const localAddress = 'aa:bb:cc:dd:ee:ff';
|
|
15
18
|
const remoteAddressType = 'random';
|
|
16
19
|
const remoteAddress = '00:11:22:33:44:55';
|
|
17
20
|
|
|
18
21
|
beforeEach(() => {
|
|
19
|
-
aclStream
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
aclStream = {
|
|
23
|
+
on: jest.fn(),
|
|
24
|
+
removeListener: jest.fn(),
|
|
25
|
+
write: jest.fn()
|
|
26
|
+
};
|
|
22
27
|
|
|
23
28
|
smp = new Smp(aclStream, localAddressType, localAddress, remoteAddressType, remoteAddress);
|
|
24
29
|
});
|
|
25
30
|
|
|
26
31
|
afterEach(() => {
|
|
27
|
-
|
|
32
|
+
jest.clearAllMocks();
|
|
28
33
|
});
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
test('construct 1', () => {
|
|
36
|
+
expect(aclStream.on).toHaveBeenCalledTimes(2);
|
|
37
|
+
expect(aclStream.on).toHaveBeenCalledWith('data', expect.any(Function));
|
|
38
|
+
expect(aclStream.on).toHaveBeenCalledWith('end', expect.any(Function));
|
|
34
39
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
expect(smp._iat).toEqual(Buffer.from([0x00]));
|
|
41
|
+
expect(smp._ia).toEqual(Buffer.from([0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa]));
|
|
42
|
+
expect(smp._rat).toEqual(Buffer.from([0x01]));
|
|
43
|
+
expect(smp._ra).toEqual(Buffer.from([0x55, 0x44, 0x33, 0x22, 0x11, 0x00]));
|
|
39
44
|
});
|
|
40
45
|
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
test('construct 2', () => {
|
|
47
|
+
jest.clearAllMocks();
|
|
43
48
|
const smp = new Smp(aclStream, remoteAddressType, remoteAddress, localAddressType, localAddress);
|
|
44
49
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
expect(aclStream.on).toHaveBeenCalledTimes(2);
|
|
51
|
+
expect(aclStream.on).toHaveBeenCalledWith('data', expect.any(Function));
|
|
52
|
+
expect(aclStream.on).toHaveBeenCalledWith('end', expect.any(Function));
|
|
48
53
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
expect(smp._iat).toEqual(Buffer.from([0x01]));
|
|
55
|
+
expect(smp._ia).toEqual(Buffer.from([0x55, 0x44, 0x33, 0x22, 0x11, 0x00]));
|
|
56
|
+
expect(smp._rat).toEqual(Buffer.from([0x00]));
|
|
57
|
+
expect(smp._ra).toEqual(Buffer.from([0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa]));
|
|
53
58
|
});
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
smp.write =
|
|
60
|
+
test('should write sendPairingRequest', () => {
|
|
61
|
+
smp.write = jest.fn();
|
|
57
62
|
|
|
58
63
|
smp.sendPairingRequest();
|
|
59
64
|
|
|
60
|
-
|
|
65
|
+
expect(smp.write).toHaveBeenCalledWith(Buffer.from([0x01, 0x03, 0x00, 0x01, 0x10, 0x00, 0x01]));
|
|
61
66
|
});
|
|
62
67
|
|
|
63
68
|
describe('onAclStreamData', () => {
|
|
64
69
|
beforeEach(() => {
|
|
65
|
-
smp.handlePairingResponse =
|
|
66
|
-
smp.handlePairingConfirm =
|
|
67
|
-
smp.handlePairingRandom =
|
|
68
|
-
smp.handlePairingFailed =
|
|
69
|
-
smp.handleEncryptInfo =
|
|
70
|
-
smp.handleMasterIdent =
|
|
70
|
+
smp.handlePairingResponse = jest.fn();
|
|
71
|
+
smp.handlePairingConfirm = jest.fn();
|
|
72
|
+
smp.handlePairingRandom = jest.fn();
|
|
73
|
+
smp.handlePairingFailed = jest.fn();
|
|
74
|
+
smp.handleEncryptInfo = jest.fn();
|
|
75
|
+
smp.handleMasterIdent = jest.fn();
|
|
71
76
|
});
|
|
72
77
|
|
|
73
|
-
|
|
78
|
+
test('should do nothing with !SMP_CID', () => {
|
|
74
79
|
smp.onAclStreamData(0);
|
|
75
80
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
expect(smp.handlePairingResponse).not.toHaveBeenCalled();
|
|
82
|
+
expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
|
|
83
|
+
expect(smp.handlePairingRandom).not.toHaveBeenCalled();
|
|
84
|
+
expect(smp.handlePairingFailed).not.toHaveBeenCalled();
|
|
85
|
+
expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
|
|
86
|
+
expect(smp.handleMasterIdent).not.toHaveBeenCalled();
|
|
82
87
|
});
|
|
83
88
|
|
|
84
|
-
|
|
89
|
+
test('should handlePairingResponse', () => {
|
|
85
90
|
const data = Buffer.from([0x02, 0x33, 0x44]);
|
|
86
91
|
smp.onAclStreamData(6, data);
|
|
87
92
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
93
|
+
expect(smp.handlePairingResponse).toHaveBeenCalledWith(data);
|
|
94
|
+
expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
|
|
95
|
+
expect(smp.handlePairingRandom).not.toHaveBeenCalled();
|
|
96
|
+
expect(smp.handlePairingFailed).not.toHaveBeenCalled();
|
|
97
|
+
expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
|
|
98
|
+
expect(smp.handleMasterIdent).not.toHaveBeenCalled();
|
|
94
99
|
});
|
|
95
100
|
|
|
96
|
-
|
|
101
|
+
test('should handlePairingConfirm', () => {
|
|
97
102
|
const data = Buffer.from([0x03, 0x33, 0x44]);
|
|
98
103
|
smp.onAclStreamData(6, data);
|
|
99
104
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
105
|
+
expect(smp.handlePairingResponse).not.toHaveBeenCalled();
|
|
106
|
+
expect(smp.handlePairingConfirm).toHaveBeenCalledWith(data);
|
|
107
|
+
expect(smp.handlePairingRandom).not.toHaveBeenCalled();
|
|
108
|
+
expect(smp.handlePairingFailed).not.toHaveBeenCalled();
|
|
109
|
+
expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
|
|
110
|
+
expect(smp.handleMasterIdent).not.toHaveBeenCalled();
|
|
106
111
|
});
|
|
107
112
|
|
|
108
|
-
|
|
113
|
+
test('should handlePairingRandom', () => {
|
|
109
114
|
const data = Buffer.from([0x04, 0x33, 0x44]);
|
|
110
115
|
smp.onAclStreamData(6, data);
|
|
111
116
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
117
|
+
expect(smp.handlePairingResponse).not.toHaveBeenCalled();
|
|
118
|
+
expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
|
|
119
|
+
expect(smp.handlePairingRandom).toHaveBeenCalledWith(data);
|
|
120
|
+
expect(smp.handlePairingFailed).not.toHaveBeenCalled();
|
|
121
|
+
expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
|
|
122
|
+
expect(smp.handleMasterIdent).not.toHaveBeenCalled();
|
|
118
123
|
});
|
|
119
124
|
|
|
120
|
-
|
|
125
|
+
test('should handlePairingFailed', () => {
|
|
121
126
|
const data = Buffer.from([0x05, 0x33, 0x44]);
|
|
122
127
|
smp.onAclStreamData(6, data);
|
|
123
128
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
129
|
+
expect(smp.handlePairingResponse).not.toHaveBeenCalled();
|
|
130
|
+
expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
|
|
131
|
+
expect(smp.handlePairingRandom).not.toHaveBeenCalled();
|
|
132
|
+
expect(smp.handlePairingFailed).toHaveBeenCalledWith(data);
|
|
133
|
+
expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
|
|
134
|
+
expect(smp.handleMasterIdent).not.toHaveBeenCalled();
|
|
130
135
|
});
|
|
131
136
|
|
|
132
|
-
|
|
137
|
+
test('should handleEncryptInfo', () => {
|
|
133
138
|
const data = Buffer.from([0x06, 0x33, 0x44]);
|
|
134
139
|
smp.onAclStreamData(6, data);
|
|
135
140
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
141
|
+
expect(smp.handlePairingResponse).not.toHaveBeenCalled();
|
|
142
|
+
expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
|
|
143
|
+
expect(smp.handlePairingRandom).not.toHaveBeenCalled();
|
|
144
|
+
expect(smp.handlePairingFailed).not.toHaveBeenCalled();
|
|
145
|
+
expect(smp.handleEncryptInfo).toHaveBeenCalledWith(data);
|
|
146
|
+
expect(smp.handleMasterIdent).not.toHaveBeenCalled();
|
|
142
147
|
});
|
|
143
148
|
|
|
144
|
-
|
|
149
|
+
test('should handleMasterIdent', () => {
|
|
145
150
|
const data = Buffer.from([0x07, 0x33, 0x44]);
|
|
146
151
|
smp.onAclStreamData(6, data);
|
|
147
152
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
153
|
+
expect(smp.handlePairingResponse).not.toHaveBeenCalled();
|
|
154
|
+
expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
|
|
155
|
+
expect(smp.handlePairingRandom).not.toHaveBeenCalled();
|
|
156
|
+
expect(smp.handlePairingFailed).not.toHaveBeenCalled();
|
|
157
|
+
expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
|
|
158
|
+
expect(smp.handleMasterIdent).toHaveBeenCalledWith(data);
|
|
154
159
|
});
|
|
155
160
|
|
|
156
|
-
|
|
161
|
+
test('should do nothing on bad code', () => {
|
|
157
162
|
const data = Buffer.from([0x08, 0x33, 0x44]);
|
|
158
163
|
smp.onAclStreamData(6, data);
|
|
159
164
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
165
|
+
expect(smp.handlePairingResponse).not.toHaveBeenCalled();
|
|
166
|
+
expect(smp.handlePairingConfirm).not.toHaveBeenCalled();
|
|
167
|
+
expect(smp.handlePairingRandom).not.toHaveBeenCalled();
|
|
168
|
+
expect(smp.handlePairingFailed).not.toHaveBeenCalled();
|
|
169
|
+
expect(smp.handleEncryptInfo).not.toHaveBeenCalled();
|
|
170
|
+
expect(smp.handleMasterIdent).not.toHaveBeenCalled();
|
|
166
171
|
});
|
|
167
172
|
});
|
|
168
173
|
|
|
169
|
-
|
|
170
|
-
const callback =
|
|
174
|
+
test('onAclStreamEnd', () => {
|
|
175
|
+
const callback = jest.fn();
|
|
171
176
|
smp.on('end', callback);
|
|
172
177
|
smp.onAclStreamEnd();
|
|
173
178
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
179
|
+
expect(aclStream.removeListener).toHaveBeenCalledTimes(2);
|
|
180
|
+
expect(aclStream.removeListener).toHaveBeenCalledWith('data', expect.any(Function));
|
|
181
|
+
expect(aclStream.removeListener).toHaveBeenCalledWith('end', expect.any(Function));
|
|
182
|
+
expect(callback).toHaveBeenCalled();
|
|
178
183
|
});
|
|
179
184
|
|
|
180
|
-
|
|
181
|
-
smp.write =
|
|
182
|
-
crypto.
|
|
183
|
-
crypto.c1 = sinon.fake.returns(Buffer.from([0x99]));
|
|
185
|
+
test('handlePairingResponse', () => {
|
|
186
|
+
smp.write = jest.fn();
|
|
187
|
+
crypto.c1.mockReturnValue(Buffer.from([0x99]));
|
|
184
188
|
|
|
185
189
|
smp.handlePairingResponse('data');
|
|
186
190
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
assert.calledOnceWithExactly(smp.write, Buffer.from([0x03, 0x99]));
|
|
191
|
+
expect(smp._pres).toBe('data');
|
|
192
|
+
expect(crypto.r).toHaveBeenCalled();
|
|
193
|
+
expect(crypto.c1).toHaveBeenCalled();
|
|
194
|
+
expect(smp.write).toHaveBeenCalledWith(Buffer.from([0x03, 0x99]));
|
|
192
195
|
});
|
|
193
196
|
|
|
194
|
-
|
|
195
|
-
smp.write =
|
|
197
|
+
test('handlePairingConfirm', () => {
|
|
198
|
+
smp.write = jest.fn();
|
|
196
199
|
smp._r = Buffer.from([0x99]);
|
|
197
200
|
|
|
198
201
|
smp.handlePairingConfirm('data');
|
|
199
202
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
assert.calledOnceWithExactly(smp.write, Buffer.from([0x04, 0x99]));
|
|
203
|
+
expect(smp._pcnf).toBe('data');
|
|
204
|
+
expect(smp.write).toHaveBeenCalledWith(Buffer.from([0x04, 0x99]));
|
|
203
205
|
});
|
|
204
206
|
|
|
205
207
|
describe('handlePairingRandom', () => {
|
|
206
|
-
|
|
207
|
-
crypto.c1
|
|
208
|
-
crypto.s1
|
|
208
|
+
test('should emit stk', () => {
|
|
209
|
+
crypto.c1.mockReturnValue(Buffer.from([0x99]));
|
|
210
|
+
crypto.s1.mockReturnValue('stk_answer');
|
|
209
211
|
|
|
210
212
|
const data = Buffer.from([0, 1]);
|
|
211
|
-
const callback =
|
|
212
|
-
const failCallback =
|
|
213
|
+
const callback = jest.fn();
|
|
214
|
+
const failCallback = jest.fn();
|
|
213
215
|
|
|
214
216
|
smp._pcnf = Buffer.from([3, 153]);
|
|
215
217
|
smp.on('stk', callback);
|
|
216
218
|
smp.on('fail', failCallback);
|
|
217
219
|
smp.handlePairingRandom(data);
|
|
218
220
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
+
expect(callback).toHaveBeenCalledWith('stk_answer');
|
|
222
|
+
expect(failCallback).not.toHaveBeenCalled();
|
|
221
223
|
});
|
|
222
224
|
|
|
223
|
-
|
|
224
|
-
crypto.c1
|
|
225
|
-
crypto.s1
|
|
225
|
+
test('should write and emit fail stk', () => {
|
|
226
|
+
crypto.c1.mockReturnValue(Buffer.from([0x99]));
|
|
227
|
+
crypto.s1.mockReturnValue('stk_answer');
|
|
226
228
|
|
|
227
229
|
const data = Buffer.from([0, 1]);
|
|
228
|
-
const callback =
|
|
229
|
-
const failCallback =
|
|
230
|
+
const callback = jest.fn();
|
|
231
|
+
const failCallback = jest.fn();
|
|
230
232
|
|
|
231
|
-
smp.write =
|
|
233
|
+
smp.write = jest.fn();
|
|
232
234
|
smp._pcnf = Buffer.from([0]);
|
|
233
235
|
smp.on('stk', callback);
|
|
234
236
|
smp.on('fail', failCallback);
|
|
235
237
|
smp.handlePairingRandom(data);
|
|
236
238
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
239
|
+
expect(smp.write).toHaveBeenCalledWith(Buffer.from([4, 3]));
|
|
240
|
+
expect(callback).not.toHaveBeenCalled();
|
|
241
|
+
expect(failCallback).toHaveBeenCalled();
|
|
240
242
|
});
|
|
241
243
|
});
|
|
242
244
|
|
|
243
|
-
|
|
244
|
-
const callback =
|
|
245
|
+
test('should emit fail on handlePairingFailed', () => {
|
|
246
|
+
const callback = jest.fn();
|
|
245
247
|
smp.on('fail', callback);
|
|
246
248
|
smp.handlePairingFailed();
|
|
247
|
-
|
|
249
|
+
expect(callback).toHaveBeenCalled();
|
|
248
250
|
});
|
|
249
251
|
|
|
250
|
-
|
|
251
|
-
const callback =
|
|
252
|
+
test('should emit ltk on handleEncryptInfo', () => {
|
|
253
|
+
const callback = jest.fn();
|
|
252
254
|
smp.on('ltk', callback);
|
|
253
255
|
smp.handleEncryptInfo(Buffer.from([0x02, 0x03, 0x04]));
|
|
254
|
-
|
|
256
|
+
expect(callback).toHaveBeenCalledWith(Buffer.from([0x03, 0x04]));
|
|
255
257
|
});
|
|
256
258
|
|
|
257
|
-
|
|
258
|
-
const callback =
|
|
259
|
+
test('should emit masterIdent on handleMasterIdent', () => {
|
|
260
|
+
const callback = jest.fn();
|
|
259
261
|
smp.on('masterIdent', callback);
|
|
260
262
|
smp.handleMasterIdent(Buffer.from([0x02, 0x03, 0x04, 0x05, 0x06]));
|
|
261
|
-
|
|
263
|
+
expect(callback).toHaveBeenCalledWith(Buffer.from([0x03, 0x04]), Buffer.from([0x05, 0x06]));
|
|
262
264
|
});
|
|
263
265
|
|
|
264
|
-
|
|
266
|
+
test('should write on aclStream', () => {
|
|
265
267
|
smp.write('data');
|
|
266
|
-
|
|
268
|
+
expect(aclStream.write).toHaveBeenCalledWith(6, 'data');
|
|
267
269
|
});
|
|
268
|
-
});
|
|
270
|
+
});
|