1// Copyright (C) 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {TextEncoder} from 'util';
16
17import {
18  AdbMsgImpl,
19  AdbOverWebUsb,
20  AdbState,
21  DEFAULT_MAX_PAYLOAD_BYTES,
22  VERSION_WITH_CHECKSUM
23} from './adb';
24
25test('startAuthentication', async () => {
26  const adb = new AdbOverWebUsb();
27
28  const sendRaw = jest.fn();
29  adb.sendRaw = sendRaw;
30  const recvRaw = jest.fn();
31  adb.recvRaw = recvRaw;
32
33
34  const expectedAuthMessage = AdbMsgImpl.create({
35    cmd: 'CNXN',
36    arg0: VERSION_WITH_CHECKSUM,
37    arg1: DEFAULT_MAX_PAYLOAD_BYTES,
38    data: 'host:1:UsbADB',
39    useChecksum: true
40  });
41  await adb.startAuthentication();
42
43  expect(sendRaw).toHaveBeenCalledTimes(2);
44  expect(sendRaw).toBeCalledWith(expectedAuthMessage.encodeHeader());
45  expect(sendRaw).toBeCalledWith(expectedAuthMessage.data);
46});
47
48test('connectedMessage', async () => {
49  const adb = new AdbOverWebUsb();
50  adb.key = {} as unknown as CryptoKeyPair;
51  adb.state = AdbState.AUTH_STEP2;
52
53  const onConnected = jest.fn();
54  adb.onConnected = onConnected;
55
56  const expectedMaxPayload = 42;
57  const connectedMsg = AdbMsgImpl.create({
58    cmd: 'CNXN',
59    arg0: VERSION_WITH_CHECKSUM,
60    arg1: expectedMaxPayload,
61    data: new TextEncoder().encode('device'),
62    useChecksum: true
63  });
64  await adb.onMessage(connectedMsg);
65
66  expect(adb.state).toBe(AdbState.CONNECTED);
67  expect(adb.maxPayload).toBe(expectedMaxPayload);
68  expect(adb.devProps).toBe('device');
69  expect(adb.useChecksum).toBe(true);
70  expect(onConnected).toHaveBeenCalledTimes(1);
71});
72
73
74test('shellOpening', () => {
75  const adb = new AdbOverWebUsb();
76  const openStream = jest.fn();
77  adb.openStream = openStream;
78
79  adb.shell('test');
80  expect(openStream).toBeCalledWith('shell:test');
81});
82