]> git.proxmox.com Git - mirror_novnc.git/blob - tests/test.websock.js
Merge pull request #989 from PeterDaveHelloKitchen/update-travis-ci
[mirror_novnc.git] / tests / test.websock.js
1 var assert = chai.assert;
2 var expect = chai.expect;
3
4 import Websock from '../core/websock.js';
5 import FakeWebSocket from './fake.websocket.js';
6
7 import sinon from '../vendor/sinon.js';
8
9 describe('Websock', function() {
10 "use strict";
11
12 describe('Queue methods', function () {
13 var sock;
14 var RQ_TEMPLATE = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
15
16 beforeEach(function () {
17 sock = new Websock();
18 // skip init
19 sock._allocate_buffers();
20 sock._rQ.set(RQ_TEMPLATE);
21 sock._rQlen = RQ_TEMPLATE.length;
22 });
23 describe('rQlen', function () {
24 it('should return the length of the receive queue', function () {
25 sock.set_rQi(0);
26
27 expect(sock.rQlen()).to.equal(RQ_TEMPLATE.length);
28 });
29
30 it("should return the proper length if we read some from the receive queue", function () {
31 sock.set_rQi(1);
32
33 expect(sock.rQlen()).to.equal(RQ_TEMPLATE.length - 1);
34 });
35 });
36
37 describe('rQpeek8', function () {
38 it('should peek at the next byte without poping it off the queue', function () {
39 var bef_len = sock.rQlen();
40 var peek = sock.rQpeek8();
41 expect(sock.rQpeek8()).to.equal(peek);
42 expect(sock.rQlen()).to.equal(bef_len);
43 });
44 });
45
46 describe('rQshift8', function () {
47 it('should pop a single byte from the receive queue', function () {
48 var peek = sock.rQpeek8();
49 var bef_len = sock.rQlen();
50 expect(sock.rQshift8()).to.equal(peek);
51 expect(sock.rQlen()).to.equal(bef_len - 1);
52 });
53 });
54
55 describe('rQshift16', function () {
56 it('should pop two bytes from the receive queue and return a single number', function () {
57 var bef_len = sock.rQlen();
58 var expected = (RQ_TEMPLATE[0] << 8) + RQ_TEMPLATE[1];
59 expect(sock.rQshift16()).to.equal(expected);
60 expect(sock.rQlen()).to.equal(bef_len - 2);
61 });
62 });
63
64 describe('rQshift32', function () {
65 it('should pop four bytes from the receive queue and return a single number', function () {
66 var bef_len = sock.rQlen();
67 var expected = (RQ_TEMPLATE[0] << 24) +
68 (RQ_TEMPLATE[1] << 16) +
69 (RQ_TEMPLATE[2] << 8) +
70 RQ_TEMPLATE[3];
71 expect(sock.rQshift32()).to.equal(expected);
72 expect(sock.rQlen()).to.equal(bef_len - 4);
73 });
74 });
75
76 describe('rQshiftStr', function () {
77 it('should shift the given number of bytes off of the receive queue and return a string', function () {
78 var bef_len = sock.rQlen();
79 var bef_rQi = sock.get_rQi();
80 var shifted = sock.rQshiftStr(3);
81 expect(shifted).to.be.a('string');
82 expect(shifted).to.equal(String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3))));
83 expect(sock.rQlen()).to.equal(bef_len - 3);
84 });
85
86 it('should shift the entire rest of the queue off if no length is given', function () {
87 sock.rQshiftStr();
88 expect(sock.rQlen()).to.equal(0);
89 });
90 });
91
92 describe('rQshiftBytes', function () {
93 it('should shift the given number of bytes of the receive queue and return an array', function () {
94 var bef_len = sock.rQlen();
95 var bef_rQi = sock.get_rQi();
96 var shifted = sock.rQshiftBytes(3);
97 expect(shifted).to.be.an.instanceof(Uint8Array);
98 expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3));
99 expect(sock.rQlen()).to.equal(bef_len - 3);
100 });
101
102 it('should shift the entire rest of the queue off if no length is given', function () {
103 sock.rQshiftBytes();
104 expect(sock.rQlen()).to.equal(0);
105 });
106 });
107
108 describe('rQslice', function () {
109 beforeEach(function () {
110 sock.set_rQi(0);
111 });
112
113 it('should not modify the receive queue', function () {
114 var bef_len = sock.rQlen();
115 sock.rQslice(0, 2);
116 expect(sock.rQlen()).to.equal(bef_len);
117 });
118
119 it('should return an array containing the given slice of the receive queue', function () {
120 var sl = sock.rQslice(0, 2);
121 expect(sl).to.be.an.instanceof(Uint8Array);
122 expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2));
123 });
124
125 it('should use the rest of the receive queue if no end is given', function () {
126 var sl = sock.rQslice(1);
127 expect(sl).to.have.length(RQ_TEMPLATE.length - 1);
128 expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1));
129 });
130
131 it('should take the current rQi in to account', function () {
132 sock.set_rQi(1);
133 expect(sock.rQslice(0, 2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2));
134 });
135 });
136
137 describe('rQwait', function () {
138 beforeEach(function () {
139 sock.set_rQi(0);
140 });
141
142 it('should return true if there are not enough bytes in the receive queue', function () {
143 expect(sock.rQwait('hi', RQ_TEMPLATE.length + 1)).to.be.true;
144 });
145
146 it('should return false if there are enough bytes in the receive queue', function () {
147 expect(sock.rQwait('hi', RQ_TEMPLATE.length)).to.be.false;
148 });
149
150 it('should return true and reduce rQi by "goback" if there are not enough bytes', function () {
151 sock.set_rQi(5);
152 expect(sock.rQwait('hi', RQ_TEMPLATE.length, 4)).to.be.true;
153 expect(sock.get_rQi()).to.equal(1);
154 });
155
156 it('should raise an error if we try to go back more than possible', function () {
157 sock.set_rQi(5);
158 expect(function () { sock.rQwait('hi', RQ_TEMPLATE.length, 6); }).to.throw(Error);
159 });
160
161 it('should not reduce rQi if there are enough bytes', function () {
162 sock.set_rQi(5);
163 sock.rQwait('hi', 1, 6);
164 expect(sock.get_rQi()).to.equal(5);
165 });
166 });
167
168 describe('flush', function () {
169 beforeEach(function () {
170 sock._websocket = {
171 send: sinon.spy()
172 };
173 });
174
175 it('should actually send on the websocket', function () {
176 sock._websocket.bufferedAmount = 8;
177 sock._websocket.readyState = WebSocket.OPEN
178 sock._sQ = new Uint8Array([1, 2, 3]);
179 sock._sQlen = 3;
180 var encoded = sock._encode_message();
181
182 sock.flush();
183 expect(sock._websocket.send).to.have.been.calledOnce;
184 expect(sock._websocket.send).to.have.been.calledWith(encoded);
185 });
186
187 it('should not call send if we do not have anything queued up', function () {
188 sock._sQlen = 0;
189 sock._websocket.bufferedAmount = 8;
190
191 sock.flush();
192
193 expect(sock._websocket.send).not.to.have.been.called;
194 });
195 });
196
197 describe('send', function () {
198 beforeEach(function () {
199 sock.flush = sinon.spy();
200 });
201
202 it('should add to the send queue', function () {
203 sock.send([1, 2, 3]);
204 var sq = sock.get_sQ();
205 expect(new Uint8Array(sq.buffer, sock._sQlen - 3, 3)).to.array.equal(new Uint8Array([1, 2, 3]));
206 });
207
208 it('should call flush', function () {
209 sock.send([1, 2, 3]);
210 expect(sock.flush).to.have.been.calledOnce;
211 });
212 });
213
214 describe('send_string', function () {
215 beforeEach(function () {
216 sock.send = sinon.spy();
217 });
218
219 it('should call send after converting the string to an array', function () {
220 sock.send_string("\x01\x02\x03");
221 expect(sock.send).to.have.been.calledWith([1, 2, 3]);
222 });
223 });
224 });
225
226 describe('lifecycle methods', function () {
227 var old_WS;
228 before(function () {
229 old_WS = WebSocket;
230 });
231
232 var sock;
233 beforeEach(function () {
234 sock = new Websock();
235 WebSocket = sinon.spy();
236 WebSocket.OPEN = old_WS.OPEN;
237 WebSocket.CONNECTING = old_WS.CONNECTING;
238 WebSocket.CLOSING = old_WS.CLOSING;
239 WebSocket.CLOSED = old_WS.CLOSED;
240
241 WebSocket.prototype.binaryType = 'arraybuffer';
242 });
243
244 describe('opening', function () {
245 it('should pick the correct protocols if none are given' , function () {
246
247 });
248
249 it('should open the actual websocket', function () {
250 sock.open('ws://localhost:8675', 'binary');
251 expect(WebSocket).to.have.been.calledWith('ws://localhost:8675', 'binary');
252 });
253
254 // it('should initialize the event handlers')?
255 });
256
257 describe('closing', function () {
258 beforeEach(function () {
259 sock.open('ws://');
260 sock._websocket.close = sinon.spy();
261 });
262
263 it('should close the actual websocket if it is open', function () {
264 sock._websocket.readyState = WebSocket.OPEN;
265 sock.close();
266 expect(sock._websocket.close).to.have.been.calledOnce;
267 });
268
269 it('should close the actual websocket if it is connecting', function () {
270 sock._websocket.readyState = WebSocket.CONNECTING;
271 sock.close();
272 expect(sock._websocket.close).to.have.been.calledOnce;
273 });
274
275 it('should not try to close the actual websocket if closing', function () {
276 sock._websocket.readyState = WebSocket.CLOSING;
277 sock.close();
278 expect(sock._websocket.close).not.to.have.been.called;
279 });
280
281 it('should not try to close the actual websocket if closed', function () {
282 sock._websocket.readyState = WebSocket.CLOSED;
283 sock.close();
284 expect(sock._websocket.close).not.to.have.been.called;
285 });
286
287 it('should reset onmessage to not call _recv_message', function () {
288 sinon.spy(sock, '_recv_message');
289 sock.close();
290 sock._websocket.onmessage(null);
291 try {
292 expect(sock._recv_message).not.to.have.been.called;
293 } finally {
294 sock._recv_message.restore();
295 }
296 });
297 });
298
299 describe('event handlers', function () {
300 beforeEach(function () {
301 sock._recv_message = sinon.spy();
302 sock.on('open', sinon.spy());
303 sock.on('close', sinon.spy());
304 sock.on('error', sinon.spy());
305 sock.open('ws://');
306 });
307
308 it('should call _recv_message on a message', function () {
309 sock._websocket.onmessage(null);
310 expect(sock._recv_message).to.have.been.calledOnce;
311 });
312
313 it('should call the open event handler on opening', function () {
314 sock._websocket.onopen();
315 expect(sock._eventHandlers.open).to.have.been.calledOnce;
316 });
317
318 it('should call the close event handler on closing', function () {
319 sock._websocket.onclose();
320 expect(sock._eventHandlers.close).to.have.been.calledOnce;
321 });
322
323 it('should call the error event handler on error', function () {
324 sock._websocket.onerror();
325 expect(sock._eventHandlers.error).to.have.been.calledOnce;
326 });
327 });
328
329 after(function () {
330 WebSocket = old_WS;
331 });
332 });
333
334 describe('WebSocket Receiving', function () {
335 var sock;
336 beforeEach(function () {
337 sock = new Websock();
338 sock._allocate_buffers();
339 });
340
341 it('should support adding binary Uint8Array data to the receive queue', function () {
342 var msg = { data: new Uint8Array([1, 2, 3]) };
343 sock._mode = 'binary';
344 sock._recv_message(msg);
345 expect(sock.rQshiftStr(3)).to.equal('\x01\x02\x03');
346 });
347
348 it('should call the message event handler if present', function () {
349 sock._eventHandlers.message = sinon.spy();
350 var msg = { data: new Uint8Array([1, 2, 3]).buffer };
351 sock._mode = 'binary';
352 sock._recv_message(msg);
353 expect(sock._eventHandlers.message).to.have.been.calledOnce;
354 });
355
356 it('should not call the message event handler if there is nothing in the receive queue', function () {
357 sock._eventHandlers.message = sinon.spy();
358 var msg = { data: new Uint8Array([]).buffer };
359 sock._mode = 'binary';
360 sock._recv_message(msg);
361 expect(sock._eventHandlers.message).not.to.have.been.called;
362 });
363
364 it('should compact the receive queue', function () {
365 // NB(sross): while this is an internal implementation detail, it's important to
366 // test, otherwise the receive queue could become very large very quickly
367 sock._rQ = new Uint8Array([0, 1, 2, 3, 4, 5, 0, 0, 0, 0]);
368 sock._rQlen = 6;
369 sock.set_rQi(6);
370 sock._rQmax = 3;
371 var msg = { data: new Uint8Array([1, 2, 3]).buffer };
372 sock._mode = 'binary';
373 sock._recv_message(msg);
374 expect(sock._rQlen).to.equal(3);
375 expect(sock.get_rQi()).to.equal(0);
376 });
377
378 it('should automatically resize the receive queue if the incoming message is too large', function () {
379 sock._rQ = new Uint8Array(20);
380 sock._rQlen = 0;
381 sock.set_rQi(0);
382 sock._rQbufferSize = 20;
383 sock._rQmax = 2;
384 var msg = { data: new Uint8Array(30).buffer };
385 sock._mode = 'binary';
386 sock._recv_message(msg);
387 expect(sock._rQlen).to.equal(30);
388 expect(sock.get_rQi()).to.equal(0);
389 expect(sock._rQ.length).to.equal(240); // keep the invariant that rQbufferSize / 8 >= rQlen
390 });
391 });
392
393 describe('Data encoding', function () {
394 before(function () { FakeWebSocket.replace(); });
395 after(function () { FakeWebSocket.restore(); });
396
397 describe('as binary data', function () {
398 var sock;
399 beforeEach(function () {
400 sock = new Websock();
401 sock.open('ws://', 'binary');
402 sock._websocket._open();
403 });
404
405 it('should only send the send queue up to the send queue length', function () {
406 sock._sQ = new Uint8Array([1, 2, 3, 4, 5]);
407 sock._sQlen = 3;
408 var res = sock._encode_message();
409 expect(res).to.array.equal(new Uint8Array([1, 2, 3]));
410 });
411
412 it('should properly pass the encoded data off to the actual WebSocket', function () {
413 sock.send([1, 2, 3]);
414 expect(sock._websocket._get_sent_data()).to.array.equal(new Uint8Array([1, 2, 3]));
415 });
416 });
417 });
418 });