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 { 16 AdbMsgImpl, 17 AdbOverWebUsb, 18 AdbState, 19 DEFAULT_MAX_PAYLOAD_BYTES, 20 VERSION_WITH_CHECKSUM, 21} from './adb'; 22import {utf8Encode} from '../../base/string_utils'; 23 24test('startAuthentication', async () => { 25 const adb = new AdbOverWebUsb(); 26 27 const sendRaw = jest.fn(); 28 adb.sendRaw = sendRaw; 29 const recvRaw = jest.fn(); 30 adb.recvRaw = recvRaw; 31 32 const expectedAuthMessage = AdbMsgImpl.create({ 33 cmd: 'CNXN', 34 arg0: VERSION_WITH_CHECKSUM, 35 arg1: DEFAULT_MAX_PAYLOAD_BYTES, 36 data: 'host:1:UsbADB', 37 useChecksum: true, 38 }); 39 await adb.startAuthentication(); 40 41 expect(sendRaw).toHaveBeenCalledTimes(2); 42 expect(sendRaw).toBeCalledWith(expectedAuthMessage.encodeHeader()); 43 expect(sendRaw).toBeCalledWith(expectedAuthMessage.data); 44}); 45 46test('connectedMessage', async () => { 47 const adb = new AdbOverWebUsb(); 48 adb.key = {} as unknown as CryptoKeyPair; 49 adb.state = AdbState.AUTH_STEP2; 50 51 const onConnected = jest.fn(); 52 adb.onConnected = onConnected; 53 54 const expectedMaxPayload = 42; 55 const connectedMsg = AdbMsgImpl.create({ 56 cmd: 'CNXN', 57 arg0: VERSION_WITH_CHECKSUM, 58 arg1: expectedMaxPayload, 59 data: utf8Encode('device'), 60 useChecksum: true, 61 }); 62 await adb.onMessage(connectedMsg); 63 64 expect(adb.state).toBe(AdbState.CONNECTED); 65 expect(adb.maxPayload).toBe(expectedMaxPayload); 66 expect(adb.devProps).toBe('device'); 67 expect(adb.useChecksum).toBe(true); 68 expect(onConnected).toHaveBeenCalledTimes(1); 69}); 70 71test('shellOpening', () => { 72 const adb = new AdbOverWebUsb(); 73 const openStream = jest.fn(); 74 adb.openStream = openStream; 75 76 adb.shell('test'); 77 expect(openStream).toBeCalledWith('shell:test'); 78}); 79