forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketStream.test.ts
More file actions
192 lines (158 loc) · 6.7 KB
/
socketStream.test.ts
File metadata and controls
192 lines (158 loc) · 6.7 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
//
// 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
// 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 * as net from 'net';
import { SocketStream } from '../../client/common/net/socket/SocketStream';
const uint64be = require('uint64be');
class MockSocket {
private _data: string;
private _rawDataWritten: any;
constructor() {
this._data = '';
}
public get dataWritten(): string {
return this._data;
}
public get rawDataWritten(): any {
return this._rawDataWritten;
}
public write(data: any) {
this._data = `${data}` + '';
this._rawDataWritten = data;
}
}
// Defines a Mocha test suite to group tests of similar kind together
suite('SocketStream', () => {
test('Read Byte', (done) => {
const buffer = Buffer.from('X');
const byteValue = buffer[0];
const socket = new MockSocket();
const stream = new SocketStream((socket as any) as net.Socket, buffer);
assert.strictEqual(stream.ReadByte(), byteValue);
done();
});
test('Read Int32', (done) => {
const num = 1234;
const socket = new MockSocket();
const buffer = uint64be.encode(num);
const stream = new SocketStream((socket as any) as net.Socket, buffer);
assert.strictEqual(stream.ReadInt32(), num);
done();
});
test('Read Int64', (done) => {
const num = 9007199254740993;
const socket = new MockSocket();
const buffer = uint64be.encode(num);
const stream = new SocketStream((socket as any) as net.Socket, buffer);
assert.strictEqual(stream.ReadInt64(), num);
done();
});
test('Read Ascii String', (done) => {
const message = 'Hello World';
const socket = new MockSocket();
const buffer = Buffer.concat([Buffer.from('A'), uint64be.encode(message.length), Buffer.from(message)]);
const stream = new SocketStream((socket as any) as net.Socket, buffer);
assert.strictEqual(stream.ReadString(), message);
done();
});
test('Read Unicode String', (done) => {
const message = 'Hello World - Функция проверки ИНН и КПП - 说明';
const socket = new MockSocket();
const stringBuffer = Buffer.from(message);
const buffer = Buffer.concat([
Buffer.concat([Buffer.from('U'), uint64be.encode(stringBuffer.byteLength)]),
stringBuffer,
]);
const stream = new SocketStream((socket as any) as net.Socket, buffer);
assert.strictEqual(stream.ReadString(), message);
done();
});
test('Read RollBackTransaction', (done) => {
const message = 'Hello World';
const socket = new MockSocket();
let buffer = Buffer.concat([Buffer.from('A'), uint64be.encode(message.length), Buffer.from(message)]);
// Write part of a second message
const partOfSecondMessage = Buffer.concat([Buffer.from('A'), uint64be.encode(message.length)]);
buffer = Buffer.concat([buffer, partOfSecondMessage]);
const stream = new SocketStream((socket as any) as net.Socket, buffer);
stream.BeginTransaction();
assert.strictEqual(stream.ReadString(), message, 'First message not read properly');
stream.ReadString();
assert.strictEqual(stream.HasInsufficientDataForReading, true, 'Should not have sufficient data for reading');
stream.RollBackTransaction();
assert.strictEqual(
stream.ReadString(),
message,
'First message not read properly after rolling back transaction',
);
done();
});
test('Read EndTransaction', (done) => {
const message = 'Hello World';
const socket = new MockSocket();
let buffer = Buffer.concat([Buffer.from('A'), uint64be.encode(message.length), Buffer.from(message)]);
// Write part of a second message
const partOfSecondMessage = Buffer.concat([Buffer.from('A'), uint64be.encode(message.length)]);
buffer = Buffer.concat([buffer, partOfSecondMessage]);
const stream = new SocketStream((socket as any) as net.Socket, buffer);
stream.BeginTransaction();
assert.strictEqual(stream.ReadString(), message, 'First message not read properly');
stream.ReadString();
assert.strictEqual(stream.HasInsufficientDataForReading, true, 'Should not have sufficient data for reading');
stream.EndTransaction();
stream.RollBackTransaction();
assert.notStrictEqual(stream.ReadString(), message, 'First message cannot be read after commit transaction');
done();
});
test('Write Buffer', (done) => {
const message = 'Hello World';
const buffer = Buffer.from('');
const socket = new MockSocket();
const stream = new SocketStream((socket as any) as net.Socket, buffer);
stream.Write(Buffer.from(message));
assert.strictEqual(socket.dataWritten, message);
done();
});
test('Write Int32', (done) => {
const num = 1234;
const buffer = Buffer.from('');
const socket = new MockSocket();
const stream = new SocketStream((socket as any) as net.Socket, buffer);
stream.WriteInt32(num);
assert.strictEqual(uint64be.decode(socket.rawDataWritten), num);
done();
});
test('Write Int64', (done) => {
const num = 9007199254740993;
const buffer = Buffer.from('');
const socket = new MockSocket();
const stream = new SocketStream((socket as any) as net.Socket, buffer);
stream.WriteInt64(num);
assert.strictEqual(uint64be.decode(socket.rawDataWritten), num);
done();
});
test('Write Ascii String', (done) => {
const message = 'Hello World';
const buffer = Buffer.from('');
const socket = new MockSocket();
const stream = new SocketStream((socket as any) as net.Socket, buffer);
stream.WriteString(message);
assert.strictEqual(socket.dataWritten, message);
done();
});
test('Write Unicode String', (done) => {
const message = 'Hello World - Функция проверки ИНН и КПП - 说明';
const buffer = Buffer.from('');
const socket = new MockSocket();
const stream = new SocketStream((socket as any) as net.Socket, buffer);
stream.WriteString(message);
assert.strictEqual(socket.dataWritten, message);
done();
});
});