• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  const expectedAuthMessage = AdbMsgImpl.create({
34    cmd: 'CNXN',
35    arg0: VERSION_WITH_CHECKSUM,
36    arg1: DEFAULT_MAX_PAYLOAD_BYTES,
37    data: 'host:1:UsbADB',
38    useChecksum: true,
39  });
40  await adb.startAuthentication();
41
42  expect(sendRaw).toHaveBeenCalledTimes(2);
43  expect(sendRaw).toBeCalledWith(expectedAuthMessage.encodeHeader());
44  expect(sendRaw).toBeCalledWith(expectedAuthMessage.data);
45});
46
47test('connectedMessage', async () => {
48  const adb = new AdbOverWebUsb();
49  adb.key = {} as unknown as CryptoKeyPair;
50  adb.state = AdbState.AUTH_STEP2;
51
52  const onConnected = jest.fn();
53  adb.onConnected = onConnected;
54
55  const expectedMaxPayload = 42;
56  const connectedMsg = AdbMsgImpl.create({
57    cmd: 'CNXN',
58    arg0: VERSION_WITH_CHECKSUM,
59    arg1: expectedMaxPayload,
60    data: new TextEncoder().encode('device'),
61    useChecksum: true,
62  });
63  await adb.onMessage(connectedMsg);
64
65  expect(adb.state).toBe(AdbState.CONNECTED);
66  expect(adb.maxPayload).toBe(expectedMaxPayload);
67  expect(adb.devProps).toBe('device');
68  expect(adb.useChecksum).toBe(true);
69  expect(onConnected).toHaveBeenCalledTimes(1);
70});
71
72test('shellOpening', () => {
73  const adb = new AdbOverWebUsb();
74  const openStream = jest.fn();
75  adb.openStream = openStream;
76
77  adb.shell('test');
78  expect(openStream).toBeCalledWith('shell:test');
79});
80