|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as assert from 'assert'; |
| 7 | +import * as crypto from 'crypto'; |
| 8 | +import * as net from 'net'; |
| 9 | +import * as platform from 'vs/base/common/platform'; |
| 10 | +import { tmpdir } from 'os'; |
| 11 | +import { join } from 'vs/base/common/path'; |
| 12 | +import { SocketDebugAdapter, NamedPipeDebugAdapter, StreamDebugAdapter } from 'vs/workbench/contrib/debug/node/debugAdapter'; |
| 13 | + |
| 14 | +function rndPort(): number { |
| 15 | + const min = 8000; |
| 16 | + const max = 9000; |
| 17 | + return Math.floor(Math.random() * (max - min) + min); |
| 18 | +} |
| 19 | + |
| 20 | +function sendInitializeRequest(debugAdapter: StreamDebugAdapter): Promise<DebugProtocol.Response> { |
| 21 | + return new Promise((resolve, reject) => { |
| 22 | + debugAdapter.sendRequest('initialize', { adapterID: 'test' }, (result) => { |
| 23 | + resolve(result); |
| 24 | + }); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +function serverConnection(socket: net.Socket) { |
| 29 | + socket.on('data', (data: Buffer) => { |
| 30 | + const str = data.toString().split('\r\n')[2]; |
| 31 | + const request = JSON.parse(str); |
| 32 | + const response: any = { |
| 33 | + seq: request.seq, |
| 34 | + request_seq: request.seq, |
| 35 | + type: 'response', |
| 36 | + command: request.command |
| 37 | + }; |
| 38 | + if (request.arguments.adapterID === 'test') { |
| 39 | + response.success = true; |
| 40 | + } else { |
| 41 | + response.success = false; |
| 42 | + response.message = 'failed'; |
| 43 | + } |
| 44 | + |
| 45 | + const responsePayload = JSON.stringify(response); |
| 46 | + socket.write(`Content-Length: ${responsePayload.length}\r\n\r\n${responsePayload}`); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +suite('Debug - StreamDebugAdapter', () => { |
| 51 | + const port = rndPort(); |
| 52 | + const pipeName = crypto.randomBytes(10).toString('utf8'); |
| 53 | + const pipePath = platform.isWindows ? join('\\\\.\\pipe\\', pipeName) : join(tmpdir(), pipeName); |
| 54 | + |
| 55 | + const testCases: { testName: string, debugAdapter: StreamDebugAdapter, connectionDetail: string | number }[] = [ |
| 56 | + { |
| 57 | + testName: 'NamedPipeDebugAdapter', |
| 58 | + debugAdapter: new NamedPipeDebugAdapter({ |
| 59 | + type: 'pipeServer', |
| 60 | + path: pipePath |
| 61 | + }), |
| 62 | + connectionDetail: pipePath |
| 63 | + }, |
| 64 | + { |
| 65 | + testName: 'SocketDebugAdapter', |
| 66 | + debugAdapter: new SocketDebugAdapter({ |
| 67 | + type: 'server', |
| 68 | + port |
| 69 | + }), |
| 70 | + connectionDetail: port |
| 71 | + } |
| 72 | + ]; |
| 73 | + |
| 74 | + for (const testCase of testCases) { |
| 75 | + test(`StreamDebugAdapter (${testCase.testName}) can initialize a connection`, async () => { |
| 76 | + const server = net.createServer(serverConnection).listen(testCase.connectionDetail); |
| 77 | + const debugAdapter = testCase.debugAdapter; |
| 78 | + try { |
| 79 | + await debugAdapter.startSession(); |
| 80 | + const response: DebugProtocol.Response = await sendInitializeRequest(debugAdapter); |
| 81 | + assert.strictEqual(response.command, 'initialize'); |
| 82 | + assert.strictEqual(response.request_seq, 1); |
| 83 | + assert.strictEqual(response.success, true, response.message); |
| 84 | + } finally { |
| 85 | + await debugAdapter.stopSession(); |
| 86 | + server.close(); |
| 87 | + debugAdapter.dispose(); |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | +}); |
0 commit comments