-
Notifications
You must be signed in to change notification settings - Fork 53
/
tcpsocket.integration.src.js
280 lines (253 loc) · 9.07 KB
/
tcpsocket.integration.src.js
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
/*jslint node:true,bitwise:true*/
/*globals Uint8Array, beforeEach, afterEach, it, expect, jasmine */
var testUtil = require('../../util');
var PromiseCompat = require('es6-promise').Promise;
module.exports = function (provider, setup) {
'use strict';
var socket, dispatch;
beforeEach(function () {
setup();
dispatch = testUtil.createTestPort('msgs');
socket = new provider.provider(undefined, dispatch.onMessage.bind(dispatch));
});
function rawStringToBuffer(str) {
var idx, len = str.length, arr = [];
for (idx = 0; idx < len; idx += 1) {
arr[idx] = str.charCodeAt(idx) & 0xFF;
}
return new Uint8Array(arr).buffer;
}
it("Connects and closes cleanly", function (done) {
socket.connect('www.google.com', 80, function () {
expect(true).toEqual(true);
socket.close(function() {
expect(dispatch.gotMessage('onDisconnect').errcode).toEqual('SUCCESS');
done();
});
});
});
it("Works as a Client", function (done) {
socket.connect('www.google.com', 80, function () {
var written = false;
socket.write(rawStringToBuffer('GET / HTTP/1.0\n\n'), function (okay) {
written = true;
});
dispatch.gotMessageAsync('onData', [], function (msg) {
expect(written).toEqual(true);
expect(dispatch.gotMessage('onData', [])).not.toEqual(false);
socket.close(done);
});
});
});
it("Closes socket and open new one on the same port", function (done) {
socket.listen('127.0.0.1', 9981, function () {
socket.close(function () {
socket.listen('127.0.0.1', 9981, function () {
expect(true).toEqual(true);
socket.close(done);
});
});
});
});
it("Gets info on client & server sockets", function (done) {
var cspy = jasmine.createSpy('client'),
client,
onConnect = function () {
client.getInfo(function (info) {
expect(info.localPort).toBeGreaterThan(1023);
PromiseCompat.all([
client.close(function () {}),
socket.close(function () {}),
done()]);
});
};
socket.getInfo(function (info) {
expect(info.connected).toEqual(false);
expect(info.localPort).not.toBeDefined();
});
socket.listen('127.0.0.1', 0, function () {
socket.getInfo(function (info) {
expect(info.localPort).toBeGreaterThan(1023);
client = new provider.provider(undefined, cspy);
client.connect('127.0.0.1', info.localPort, onConnect);
});
});
});
it("Sends from Client to Server, onConnection/onDisconnect", function (done) {
var cspy = jasmine.createSpy('client'),
client,
receiver,
onDispatch,
onConnect,
onConnectionReceived;
onDispatch = function (evt, msg) {
if (evt === 'onDisconnect') {
return;
}
expect(evt).toEqual('onData');
expect(msg.data.byteLength).toEqual(10);
PromiseCompat.all([ client.close(function () {}),
socket.close(function () {}) ]);
};
dispatch.gotMessageAsync('onConnection', [], function (msg) {
expect(msg.socket).toBeDefined();
onConnectionReceived = true;
receiver = new provider.provider(undefined, onDispatch, msg.socket);
});
dispatch.gotMessageAsync('onDisconnect', [], function (msg) {
expect(onConnectionReceived).toEqual(true);
PromiseCompat.all([ receiver.close(function () {}),
done() ]);
});
onConnect = function () {
var buf = new Uint8Array(10);
client.write(buf.buffer, function () {});
};
socket.listen('127.0.0.1', 9981, function () {
client = new provider.provider(undefined, cspy);
client.connect('127.0.0.1', 9981, onConnect);
});
});
it("Pauses and resumes", function (done) {
socket.connect('www.google.com', 80, function () {
var paused = false;
var closed = false;
var pausedMessageCount = 0;
dispatch.on('onMessage', function (msg) {
if (!msg || !(msg.hasOwnProperty('data'))) {
// Not an 'onData' message.
return;
}
// One onData is allowed during pause due to https://crbug.com/360026
if (paused && ++pausedMessageCount === 1) {
return;
}
// Apart from the above exception, check that data doesn't arrive until
// after the socket resumes.
expect(paused).toEqual(false);
if (!closed) {
closed = true;
socket.close(done);
}
});
socket.pause(function () {
paused = true;
// This URL is selected to be a file large enough to generate
// multiple onData events.
socket.write(
rawStringToBuffer('GET /images/srpr/logo11w.png HTTP/1.0\n\n'),
function (okay) {});
// Wait a second before unpausing. Data received in this second
// will fail the expectation that paused is false in gotMessageAsync.
setTimeout(function () {
// The observed behavior is that the next packet is received after
// the call to resume, but before resume returns.
socket.resume(function () {});
paused = false;
}, 1000);
});
});
});
it("Secures a socket", function (done) {
socket.prepareSecure(function(success, err) {
expect(err).toBeUndefined();
socket.connect('www.google.com', 443, function (success, err) {
expect(err).toBeUndefined();
socket.secure(function(success, err) {
expect(err).toBeUndefined();
socket.getInfo(function (info) {
expect(info.connected).toEqual(true);
PromiseCompat.all([socket.close(function () {}),
done()]);
});
});
});
});
});
it("Gives error when securing a disconnected socket", function(done) {
socket.secure(function (success, err) {
expect(err.errcode).toEqual('NOT_CONNECTED');
done();
});
});
it("Gives error when connecting to invalid domain", function (done) {
// Some ISPs (e.g. Time Warner) hijack all failed domains, so we use
// a failing subdomain on a real domain.
socket.connect('nosuchdomain.google.com', 80,
function (success, err) {
var allowedErrors = ['CONNECTION_FAILED',
'NAME_NOT_RESOLVED'];
expect(allowedErrors).toContain(err.errcode);
done();
});
});
it("Gives error when writing a disconnected socket", function(done) {
socket.write(rawStringToBuffer(''), function (success, err) {
expect(err.errcode).toEqual('NOT_CONNECTED');
done();
});
});
it("Gives error when closing a disconnected socket", function(done) {
socket.close(function (success, err) {
expect(err.errcode).toEqual('SOCKET_CLOSED');
done();
});
});
it("Gives error when listening on already allocated socket", function(done) {
socket.listen('127.0.0.1', 9981, function () {
socket.listen('127.0.0.1', 9981, function (success, err) {
expect(err.errcode).toEqual('ALREADY_CONNECTED');
PromiseCompat.resolve(socket.close(done));
});
});
});
it("Socket reusing id of closed socket is also closed", function(done) {
var cspy = jasmine.createSpy('client'),
client,
clientCopy;
dispatch.gotMessageAsync('onConnection', [], function (msg) {
expect(msg.socket).toBeDefined();
client.close(function () {
clientCopy = new provider.provider(undefined, function () {},
client.id);
clientCopy.getInfo(function (info) {
// TODO consider changing implementation to give more explicit failure
expect(info.connected).toEqual(false);
done();
});
});
});
socket.listen('127.0.0.1', 9981, function () {
client = new provider.provider(undefined, cspy);
client.connect('127.0.0.1', 9981, function () {});
});
});
// Tests that the sockets passed to server sockets' onConnection events
// correctly dispatch onDisconnect events.
//
// Note that this also implicitly tests the close() method of client sockets.
it("onConnection sockets dispatch onDisconnect", function(done) {
var cspy = jasmine.createSpy('client'),
client,
receiver,
received;
var onDispatch = function (evt, msg) {
if (!received) {
received = true;
expect(evt).toEqual('onDisconnect');
socket.close(function () { receiver.close(function () { done() })});
}
};
dispatch.gotMessageAsync('onConnection', [], function (msg) {
expect(msg.socket).toBeDefined();
receiver = new provider.provider(undefined, onDispatch, msg.socket);
client.close(function () {});
});
socket.listen('127.0.0.1', 0, function () {
socket.getInfo(function (info) {
client = new provider.provider(undefined, cspy);
client.connect('127.0.0.1', info.localPort, function () {});
});
});
});
};