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