Nicolò Mazzucato | 4921962 | 2019-08-30 12:44:59 +0100 | [diff] [blame] | 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 | |
| 15 | import {TextEncoder} from 'util'; |
| 16 | |
| 17 | import { |
| 18 | AdbMsgImpl, |
| 19 | AdbOverWebUsb, |
| 20 | AdbState, |
| 21 | DEFAULT_MAX_PAYLOAD_BYTES, |
Hector Dearman | 23c078b | 2022-06-05 12:00:25 +0100 | [diff] [blame] | 22 | VERSION_WITH_CHECKSUM, |
Nicolò Mazzucato | 4921962 | 2019-08-30 12:44:59 +0100 | [diff] [blame] | 23 | } from './adb'; |
| 24 | |
| 25 | test('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', |
Nicolò Mazzucato | 7c4a15f | 2019-09-05 11:45:41 +0100 | [diff] [blame] | 36 | arg0: VERSION_WITH_CHECKSUM, |
Nicolò Mazzucato | 4921962 | 2019-08-30 12:44:59 +0100 | [diff] [blame] | 37 | arg1: DEFAULT_MAX_PAYLOAD_BYTES, |
| 38 | data: 'host:1:UsbADB', |
Hector Dearman | 23c078b | 2022-06-05 12:00:25 +0100 | [diff] [blame] | 39 | useChecksum: true, |
Nicolò Mazzucato | 4921962 | 2019-08-30 12:44:59 +0100 | [diff] [blame] | 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 | |
| 48 | test('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'), |
Hector Dearman | 23c078b | 2022-06-05 12:00:25 +0100 | [diff] [blame] | 62 | useChecksum: true, |
Nicolò Mazzucato | 4921962 | 2019-08-30 12:44:59 +0100 | [diff] [blame] | 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 | |
| 74 | test('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 | }); |