|
1 | 1 | import assert from 'node:assert/strict'; |
2 | | -import {type Server, type Socket} from 'node:net'; |
| 2 | +import {type AddressInfo, type Server, type Socket, createConnection, createServer} from 'node:net'; |
| 3 | +import {resolve} from 'node:path'; |
3 | 4 | import {afterEach, beforeEach, describe, it} from 'node:test'; |
| 5 | +import {fileURLToPath} from 'node:url'; |
| 6 | + |
| 7 | +import {fs, node} from '@appium/support'; |
4 | 8 |
|
5 | 9 | import {type Device, Usbmux} from '../../../src/lib/usbmux/index.js'; |
| 10 | +import {UsbmuxDecoder} from '../../../src/lib/usbmux/usbmux-decoder.js'; |
6 | 11 | import {prioritizeUsbOverNetworkForDuplicateUdids} from '../../../src/lib/usbmux/utils.js'; |
7 | 12 | import {UDID, fixtures, getServerWithFixtures} from '../fixtures/index.js'; |
8 | 13 |
|
| 14 | +const PKG_ROOT = node.getModuleRootSync('appium-ios-remotexpc', fileURLToPath(import.meta.url)); |
| 15 | + |
9 | 16 | const DUP_UDID = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; |
10 | 17 |
|
11 | 18 | function mockUsbmuxDevice( |
@@ -95,6 +102,60 @@ describe('usbmux', function () { |
95 | 102 | } |
96 | 103 | }); |
97 | 104 |
|
| 105 | + it('should preserve remainder bytes in decoder buffer when partial chunk arrives', function () { |
| 106 | + const decoder = new UsbmuxDecoder(); |
| 107 | + const chunk = Buffer.from([0x05, 0x00, 0x00, 0x00]); |
| 108 | + decoder.write(chunk); |
| 109 | + assert.deepStrictEqual(decoder.buffer, chunk); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should emit error on malformed payload instead of throwing synchronously in decoder', async function () { |
| 113 | + const decoder = new UsbmuxDecoder(); |
| 114 | + const errorPromise = new Promise<Error>((resolve) => { |
| 115 | + decoder.once('error', resolve); |
| 116 | + }); |
| 117 | + |
| 118 | + const header = Buffer.alloc(16); |
| 119 | + header.writeUInt32LE(20, 0); // length: 20 bytes total (16 header + 4 payload) |
| 120 | + header.writeUInt32LE(1, 4); // version |
| 121 | + header.writeUInt32LE(8, 8); // type |
| 122 | + header.writeUInt32LE(1, 12); // tag |
| 123 | + const invalidPayload = Buffer.from('bad!'); |
| 124 | + |
| 125 | + decoder.write(Buffer.concat([header, invalidPayload])); |
| 126 | + const err = await errorPromise; |
| 127 | + assert.ok(err instanceof Error); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should unshift unconsumed trailing bytes received alongside connect result', async function () { |
| 131 | + const connectFixture = Buffer.from( |
| 132 | + await fs.readFile(resolve(PKG_ROOT, 'test', 'unit', 'fixtures', 'usbmuxconnectmessage.bin')), |
| 133 | + ); |
| 134 | + const trailingGreeting = Buffer.from('REMOTE_SERVICE_GREETING'); |
| 135 | + |
| 136 | + server = createServer((s: Socket) => { |
| 137 | + s.once('data', (data: Buffer) => { |
| 138 | + const clientTag = data.readUInt32LE(12); |
| 139 | + connectFixture.writeUInt32LE(clientTag, 12); |
| 140 | + s.write(Buffer.concat([connectFixture, trailingGreeting])); |
| 141 | + }); |
| 142 | + }); |
| 143 | + server.listen(); |
| 144 | + const addr = server.address() as AddressInfo; |
| 145 | + socket = createConnection(addr.port); |
| 146 | + |
| 147 | + usbmux = new Usbmux(socket); |
| 148 | + const connectedSocket = await usbmux.connect(1, 62078); |
| 149 | + |
| 150 | + const received = await new Promise<Buffer>((resolve) => { |
| 151 | + connectedSocket.once('data', resolve); |
| 152 | + connectedSocket.resume(); |
| 153 | + }); |
| 154 | + |
| 155 | + assert.deepStrictEqual(received, trailingGreeting); |
| 156 | + connectedSocket.destroy(); |
| 157 | + }); |
| 158 | + |
98 | 159 | it('should order duplicate UDIDs with USB before Network', function () { |
99 | 160 | const net = mockUsbmuxDevice(2, DUP_UDID, 'Network'); |
100 | 161 | const usb = mockUsbmuxDevice(1, DUP_UDID, 'USB'); |
|
0 commit comments