forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocketCallbackHandler.test.ts
More file actions
303 lines (270 loc) · 11.3 KB
/
socketCallbackHandler.test.ts
File metadata and controls
303 lines (270 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//
// Note: This example test is leveraging the Mocha test framework.
// Please refer to their documentation on https://mochajs.org/ for help.
//
// Place this right on top
import { initialize } from './../initialize';
// The module 'assert' provides assertion methods from node
import * as assert from 'assert';
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import { SocketStream } from '../../client/common/comms/SocketStream';
import { SocketServer } from '../../client/common/comms/socketServer';
import { SocketCallbackHandler } from '../../client/common/comms/socketCallbackHandler';
import { createDeferred, Deferred } from '../../client/common/helpers';
import { IdDispenser } from '../../client/common/idDispenser';
import * as net from 'net';
const uint64be = require("uint64be");
class Commands {
public static ExitCommandBytes: Buffer = new Buffer("exit");
public static PingBytes: Buffer = new Buffer("ping");
public static ListKernelsBytes: Buffer = new Buffer("lstk");
}
namespace ResponseCommands {
export const Pong = 'PONG';
export const ListKernels = 'LSTK';
export const Error = 'EROR';
}
const GUID = 'This is the Guid';
const PID = 1234;
class MockSocketCallbackHandler extends SocketCallbackHandler {
private idDispenser: IdDispenser;
constructor(socketServer: SocketServer) {
super(socketServer);
this.registerCommandHandler(ResponseCommands.Pong, this.onPong.bind(this));
this.registerCommandHandler(ResponseCommands.Error, this.onError.bind(this));
this.idDispenser = new IdDispenser();
}
private onError() {
const message = this.stream.readStringInTransaction();
if (message == undefined) {
return;
}
this.emit("error", '', '', message);
}
public ping(message: string) {
this.SendRawCommand(Commands.PingBytes);
const stringBuffer = new Buffer(message);
let buffer = Buffer.concat([Buffer.concat([new Buffer('U'), uint64be.encode(stringBuffer.byteLength)]), stringBuffer]);
this.stream.Write(buffer);
}
private onPong() {
const message = this.stream.readStringInTransaction();
if (message == undefined) {
return;
}
this.emit("pong", message);
}
private pid: number;
private guid: string;
protected handleHandshake(): boolean {
if (!this.guid) {
this.guid = this.stream.readStringInTransaction();
if (this.guid == undefined) {
return false;
}
}
if (!this.pid) {
this.pid = this.stream.readInt32InTransaction();
if (this.pid == undefined) {
return false;
}
}
if (this.guid !== GUID) {
this.emit('error', this.guid, GUID, 'Guids not the same');
return true;
}
if (this.pid !== PID) {
this.emit('error', this.pid, PID, 'pids not the same');
return true;
}
this.emit("handshake");
return true;
}
}
class MockSocketClient {
private socket: net.Socket;
public SocketStream: SocketStream;
constructor(private port: number) {
}
private def: Deferred<any>;
public start(): Promise<any> {
this.def = createDeferred<any>();
this.socket = net.connect(this.port, this.connectionListener.bind(this));
return this.def.promise;
}
private connectionListener() {
this.SocketStream = new SocketStream(this.socket, new Buffer(''));
this.def.resolve();
this.socket.on('data', (data: Buffer) => {
try {
this.SocketStream.Append(data);
// We can only receive ping messages
this.SocketStream.BeginTransaction();
const cmdId = new Buffer([this.SocketStream.ReadByte(), this.SocketStream.ReadByte(), this.SocketStream.ReadByte(), this.SocketStream.ReadByte()]).toString();
const message = this.SocketStream.ReadString();
if (message == undefined) {
this.SocketStream.EndTransaction();
return;
}
if (cmdId !== 'ping') {
this.SocketStream.Write(new Buffer(ResponseCommands.Error));
const errorMessage = `Received unknown command '${cmdId}'`;
const errorBuffer = Buffer.concat([Buffer.concat([new Buffer('A'), uint64be.encode(errorMessage.length)]), new Buffer(errorMessage)]);
this.SocketStream.Write(errorBuffer);
return;
}
this.SocketStream.Write(new Buffer(ResponseCommands.Pong));
const messageBuffer = new Buffer(message);
const pongBuffer = Buffer.concat([Buffer.concat([new Buffer('U'), uint64be.encode(messageBuffer.byteLength)]), messageBuffer]);
this.SocketStream.Write(pongBuffer);
}
catch (ex) {
this.SocketStream.Write(new Buffer(ResponseCommands.Error));
const errorMessage = `Fatal error in handling data at socket client. Error: ${ex.message}`;
const errorBuffer = Buffer.concat([Buffer.concat([new Buffer('A'), uint64be.encode(errorMessage.length)]), new Buffer(errorMessage)]);
this.SocketStream.Write(errorBuffer);
}
});
}
}
class MockSocket {
constructor() {
this._data = '';
}
private _data: string;
private _rawDataWritten: any;
public get dataWritten(): string {
return this._data;
}
public get rawDataWritten(): any {
return this._rawDataWritten;
}
write(data: any) {
this._data = data + '';
this._rawDataWritten = data;
}
}
// Defines a Mocha test suite to group tests of similar kind together
suite('SocketCallbackHandler', () => {
test('Succesful Handshake', done => {
const socketServer = new SocketServer();
let socketClient: MockSocketClient;
let callbackHandler: MockSocketCallbackHandler;
socketServer.Start().then(port => {
callbackHandler = new MockSocketCallbackHandler(socketServer);
socketClient = new MockSocketClient(port);
return socketClient.start();
}).then(() => {
const def = createDeferred<any>();
let timeOut = setTimeout(() => {
def.reject('Handshake not completed in allocated time');
}, 5000);
callbackHandler.on('handshake', () => {
if (timeOut) {
clearTimeout(timeOut);
timeOut = null;
}
def.resolve();
});
callbackHandler.on('error', (actual: string, expected: string, message: string) => {
if (timeOut) {
clearTimeout(timeOut);
timeOut = null;
}
def.reject({ actual: actual, expected: expected, message: message });
});
// Client has connected, now send information to the callback handler via sockets
const guidBuffer = Buffer.concat([new Buffer('A'), uint64be.encode(GUID.length), new Buffer(GUID)]);
socketClient.SocketStream.Write(guidBuffer);
socketClient.SocketStream.WriteInt32(PID);
return def.promise;
}).then(done).catch(done);
});
test('Unsuccesful Handshake', done => {
const socketServer = new SocketServer();
let socketClient: MockSocketClient;
let callbackHandler: MockSocketCallbackHandler;
socketServer.Start().then(port => {
callbackHandler = new MockSocketCallbackHandler(socketServer);
socketClient = new MockSocketClient(port);
return socketClient.start();
}).then(() => {
const def = createDeferred<any>();
let timeOut = setTimeout(() => {
def.reject('Handshake not completed in allocated time');
}, 5000);
callbackHandler.on('handshake', () => {
if (timeOut) {
clearTimeout(timeOut);
timeOut = null;
}
def.reject('handshake should fail, but it succeeded!');
});
callbackHandler.on('error', (actual: string | number, expected: string, message: string) => {
if (timeOut) {
clearTimeout(timeOut);
timeOut = null;
}
if (actual === 0 && message === 'pids not the same') {
def.resolve();
}
else {
def.reject({ actual: actual, expected: expected, message: message });
}
});
// Client has connected, now send information to the callback handler via sockets
const guidBuffer = Buffer.concat([new Buffer('A'), uint64be.encode(GUID.length), new Buffer(GUID)]);
socketClient.SocketStream.Write(guidBuffer);
// Send the wrong pid
socketClient.SocketStream.WriteInt32(0);
return def.promise;
}).then(done).catch(done);
});
test('Ping with message', done => {
const socketServer = new SocketServer();
let socketClient: MockSocketClient;
let callbackHandler: MockSocketCallbackHandler;
socketServer.Start().then(port => {
callbackHandler = new MockSocketCallbackHandler(socketServer);
socketClient = new MockSocketClient(port);
return socketClient.start();
}).then(() => {
const def = createDeferred<any>();
const PING_MESSAGE = 'This is the Ping Message - Функция проверки ИНН и КПП - 说明';
let timeOut = setTimeout(() => {
def.reject('Handshake not completed in allocated time');
}, 5000);
callbackHandler.on('handshake', () => {
// Send a custom message (only after handshake has been done)
callbackHandler.ping(PING_MESSAGE);
});
callbackHandler.on('pong', (message: string) => {
if (timeOut) {
clearTimeout(timeOut);
timeOut = null;
}
try {
assert.equal(message, PING_MESSAGE);
def.resolve();
}
catch (ex) {
def.reject(ex);
}
});
callbackHandler.on('error', (actual: string, expected: string, message: string) => {
if (timeOut) {
clearTimeout(timeOut);
timeOut = null;
}
def.reject({ actual: actual, expected: expected, message: message });
});
// Client has connected, now send information to the callback handler via sockets
const guidBuffer = Buffer.concat([new Buffer('A'), uint64be.encode(GUID.length), new Buffer(GUID)]);
socketClient.SocketStream.Write(guidBuffer);
// Send the wrong pid
socketClient.SocketStream.WriteInt32(PID);
return def.promise;
}).then(done).catch(done);
});
});