forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketCallbackHandler.test.ts
More file actions
327 lines (292 loc) · 13.3 KB
/
socketCallbackHandler.test.ts
File metadata and controls
327 lines (292 loc) · 13.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// tslint:disable:no-any max-classes-per-file max-func-body-length no-stateless-class no-require-imports no-var-requires no-empty
import { expect } from 'chai';
import * as getFreePort from 'get-port';
import * as net from 'net';
import { SocketCallbackHandler } from '../../client/common/net/socket/socketCallbackHandler';
import { SocketServer } from '../../client/common/net/socket/socketServer';
import { SocketStream } from '../../client/common/net/socket/SocketStream';
import { createDeferred, Deferred } from '../../client/common/utils/async';
const uint64be = require('uint64be');
// tslint:disable-next-line:no-unnecessary-class
class Commands {
public static ExitCommandBytes: Buffer = new Buffer('exit');
public static PingBytes: Buffer = new Buffer('ping');
}
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 pid?: number;
private guid?: string;
constructor(socketServer: SocketServer) {
super(socketServer);
this.registerCommandHandler(ResponseCommands.Pong, this.onPong.bind(this));
this.registerCommandHandler(ResponseCommands.Error, this.onError.bind(this));
}
public ping(message: string) {
this.SendRawCommand(Commands.PingBytes);
const stringBuffer = new Buffer(message);
const buffer = Buffer.concat([Buffer.concat([new Buffer('U'), uint64be.encode(stringBuffer.byteLength)]), stringBuffer]);
this.stream.Write(buffer);
}
protected handleHandshake(): boolean {
if (!this.guid) {
this.guid = this.stream.readStringInTransaction();
if (typeof this.guid !== 'string') {
return false;
}
}
if (!this.pid) {
this.pid = this.stream.readInt32InTransaction();
if (typeof this.pid !== 'number') {
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;
}
private onError() {
const message = this.stream.readStringInTransaction();
if (typeof message !== 'string') {
return;
}
this.emit('error', '', '', message);
}
private onPong() {
const message = this.stream.readStringInTransaction();
if (typeof message !== 'string') {
return;
}
this.emit('pong', message);
}
}
class MockSocketClient {
private socket?: net.Socket;
private socketStream?: SocketStream;
private def?: Deferred<any>;
constructor(private port: number) {}
public get SocketStream(): SocketStream {
if (this.socketStream === undefined) {
throw Error('not listening');
}
return this.socketStream;
}
public start(): Promise<any> {
this.def = createDeferred<any>();
this.socket = net.connect(this.port as any, this.connectionListener.bind(this));
return this.def.promise;
}
private connectionListener() {
if (this.socket === undefined || this.def === undefined) {
throw Error('not started');
}
this.socketStream = new SocketStream(this.socket, new Buffer(''));
this.def.resolve();
this.socket.on('error', () => {});
this.socket.on('data', (data: Buffer) => {
try {
this.SocketStream.Append(data);
// We can only receive ping messages
this.SocketStream.BeginTransaction();
const cmdIdBytes: number[] = [];
for (let counter = 0; counter < 4; counter += 1) {
const byte = this.SocketStream.ReadByte();
if (typeof byte !== 'number') {
this.SocketStream.RollBackTransaction();
return;
}
cmdIdBytes.push(byte);
}
const cmdId = new Buffer(cmdIdBytes).toString();
const message = this.SocketStream.ReadString();
if (typeof message !== 'string') {
this.SocketStream.RollBackTransaction();
return;
}
this.SocketStream.EndTransaction();
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);
}
});
}
}
// Defines a Mocha test suite to group tests of similar kind together
suite('SocketCallbackHandler', () => {
let socketServer: SocketServer;
setup(() => (socketServer = new SocketServer()));
teardown(() => socketServer.Stop());
test('Succesfully starts without any specific host or port', async () => {
const port = await socketServer.Start();
expect(port).to.be.greaterThan(0);
});
test('Succesfully starts with port=0 and no host', async () => {
const port = await socketServer.Start({ port: 0 });
expect(port).to.be.greaterThan(0);
});
test('Succesfully starts with port=0 and host=localhost', async () => {
const port = await socketServer.Start({ port: 0, host: 'localhost' });
expect(port).to.be.greaterThan(0);
});
test('Succesfully starts with host=127.0.0.1', async () => {
const port = await socketServer.Start({ host: '127.0.0.1' });
expect(port).to.be.greaterThan(0);
});
test('Succesfully starts with port=0 and host=127.0.0.1', async () => {
const port = await socketServer.Start({ port: 0, host: '127.0.0.1' });
expect(port).to.be.greaterThan(0);
});
test('Succesfully starts with specific port', async () => {
const availablePort = await getFreePort({ host: 'localhost' });
const port = await socketServer.Start({ port: availablePort, host: 'localhost' });
expect(port).to.be.equal(availablePort);
});
test('Succesful Handshake', async () => {
const port = await socketServer.Start();
const callbackHandler = new MockSocketCallbackHandler(socketServer);
const socketClient = new MockSocketClient(port);
await socketClient.start();
const def = createDeferred<any>();
callbackHandler.on('handshake', () => {
def.resolve();
});
callbackHandler.on('error', (actual: string, expected: string, message: string) => {
if (!def.completed) {
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);
await def.promise;
});
test('Unsuccesful Handshake', async () => {
const port = await socketServer.Start();
const callbackHandler = new MockSocketCallbackHandler(socketServer);
const socketClient = new MockSocketClient(port);
await socketClient.start();
const def = createDeferred<any>();
let timeOut: NodeJS.Timer | undefined | number = setTimeout(() => {
def.reject('Handshake not completed in allocated time');
}, 5000);
callbackHandler.on('handshake', () => {
if (timeOut) {
clearTimeout(timeOut as any);
timeOut = undefined;
}
def.reject('handshake should fail, but it succeeded!');
});
callbackHandler.on('error', (actual: string | number, expected: string, message: string) => {
if (timeOut) {
clearTimeout(timeOut as any);
timeOut = undefined;
}
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);
await def.promise;
});
test('Ping with message', async () => {
const port = await socketServer.Start();
const callbackHandler = new MockSocketCallbackHandler(socketServer);
const socketClient = new MockSocketClient(port);
await socketClient.start();
const def = createDeferred<any>();
const PING_MESSAGE = 'This is the Ping Message - Функция проверки ИНН и КПП - 说明';
callbackHandler.on('handshake', () => {
// Send a custom message (only after handshake has been done)
callbackHandler.ping(PING_MESSAGE);
});
callbackHandler.on('pong', (message: string) => {
try {
expect(message).to.be.equal(PING_MESSAGE);
def.resolve();
} catch (ex) {
def.reject(ex);
}
});
callbackHandler.on('error', (actual: string, expected: string, message: string) => {
if (!def.completed) {
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);
await def.promise;
});
test('Succesful Handshake with port=0 and host=localhost', async () => {
const port = await socketServer.Start({ port: 0, host: 'localhost' });
const callbackHandler = new MockSocketCallbackHandler(socketServer);
const socketClient = new MockSocketClient(port);
await socketClient.start();
const def = createDeferred<any>();
callbackHandler.on('handshake', () => def.resolve());
callbackHandler.on('error', (actual: string, expected: string, message: string) => {
if (!def.completed) {
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);
await def.promise;
});
test('Succesful Handshake with specific port', async () => {
const availablePort = await new Promise<number>((resolve, reject) => getFreePort({ host: 'localhost' }).then(resolve, reject));
const port = await socketServer.Start({ port: availablePort, host: 'localhost' });
expect(port).to.be.equal(availablePort, 'Server is not listening on the provided port number');
const callbackHandler = new MockSocketCallbackHandler(socketServer);
const socketClient = new MockSocketClient(port);
await socketClient.start();
const def = createDeferred<any>();
callbackHandler.on('handshake', () => def.resolve());
callbackHandler.on('error', (actual: string, expected: string, message: string) => {
if (!def.completed) {
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);
await def.promise;
});
});