forked from novnc/noVNC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rfb.js
2099 lines (1755 loc) · 94.4 KB
/
test.rfb.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* jshint expr: true */
var assert = chai.assert;
var expect = chai.expect;
import RFB from '../core/rfb.js';
import Websock from '../core/websock.js';
import FakeWebSocket from './fake.websocket.js';
import './assertions';
import 'sinon';
import sinonChai from '../node_modules/sinon-chai/lib/sinon-chai.js';
chai.use(sinonChai);
function make_rfb (extra_opts) {
if (!extra_opts) {
extra_opts = {};
}
extra_opts.target = extra_opts.target || document.createElement('canvas');
return new RFB(extra_opts);
}
var push8 = function (arr, num) {
"use strict";
arr.push(num & 0xFF);
};
var push16 = function (arr, num) {
"use strict";
arr.push((num >> 8) & 0xFF,
num & 0xFF);
};
var push32 = function (arr, num) {
"use strict";
arr.push((num >> 24) & 0xFF,
(num >> 16) & 0xFF,
(num >> 8) & 0xFF,
num & 0xFF);
};
describe('Remote Frame Buffer Protocol Client', function() {
"use strict";
before(FakeWebSocket.replace);
after(FakeWebSocket.restore);
before(function () {
this.clock = sinon.useFakeTimers();
// Use a single set of buffers instead of reallocating to
// speed up tests
var sock = new Websock();
var _sQ = new Uint8Array(sock._sQbufferSize);
var rQ = new Uint8Array(sock._rQbufferSize);
Websock.prototype._old_allocate_buffers = Websock.prototype._allocate_buffers;
Websock.prototype._allocate_buffers = function () {
this._sQ = _sQ;
this._rQ = rQ;
};
});
after(function () {
Websock.prototype._allocate_buffers = Websock.prototype._old_allocate_buffers;
this.clock.restore();
});
describe('Public API Basic Behavior', function () {
var client;
beforeEach(function () {
client = make_rfb();
});
describe('#connect', function () {
beforeEach(function () { client._updateConnectionState = sinon.spy(); });
it('should set the current state to "connecting"', function () {
client.connect('host', 8675);
expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateConnectionState).to.have.been.calledWith('connecting');
});
it('should not try to connect if we are missing a host', function () {
client._fail = sinon.spy();
client._rfb_connection_state = '';
client.connect(undefined, 8675);
expect(client._fail).to.have.been.calledOnce;
expect(client._updateConnectionState).to.not.have.been.called;
expect(client._rfb_connection_state).to.equal('');
});
it('should not try to connect if we are missing a port', function () {
client._fail = sinon.spy();
client._rfb_connection_state = '';
client.connect('abc');
expect(client._fail).to.have.been.calledOnce;
expect(client._updateConnectionState).to.not.have.been.called;
expect(client._rfb_connection_state).to.equal('');
});
});
describe('#disconnect', function () {
beforeEach(function () { client._updateConnectionState = sinon.spy(); });
it('should set the current state to "disconnecting"', function () {
client.disconnect();
expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateConnectionState).to.have.been.calledWith('disconnecting');
});
it('should unregister error event handler', function () {
sinon.spy(client._sock, 'off');
client.disconnect();
expect(client._sock.off).to.have.been.calledWith('error');
});
it('should unregister message event handler', function () {
sinon.spy(client._sock, 'off');
client.disconnect();
expect(client._sock.off).to.have.been.calledWith('message');
});
it('should unregister open event handler', function () {
sinon.spy(client._sock, 'off');
client.disconnect();
expect(client._sock.off).to.have.been.calledWith('open');
});
});
describe('#sendPassword', function () {
beforeEach(function () { this.clock = sinon.useFakeTimers(); });
afterEach(function () { this.clock.restore(); });
it('should set the rfb password properly"', function () {
client.sendPassword('pass');
expect(client._rfb_password).to.equal('pass');
});
it('should call init_msg "soon"', function () {
client._init_msg = sinon.spy();
client.sendPassword('pass');
this.clock.tick(5);
expect(client._init_msg).to.have.been.calledOnce;
});
});
describe('#sendCtrlAlDel', function () {
beforeEach(function () {
client._sock = new Websock();
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_connection_state = 'connected';
client._view_only = false;
});
it('should sent ctrl[down]-alt[down]-del[down] then del[up]-alt[up]-ctrl[up]', function () {
var expected = {_sQ: new Uint8Array(48), _sQlen: 0, flush: function () {}};
RFB.messages.keyEvent(expected, 0xFFE3, 1);
RFB.messages.keyEvent(expected, 0xFFE9, 1);
RFB.messages.keyEvent(expected, 0xFFFF, 1);
RFB.messages.keyEvent(expected, 0xFFFF, 0);
RFB.messages.keyEvent(expected, 0xFFE9, 0);
RFB.messages.keyEvent(expected, 0xFFE3, 0);
client.sendCtrlAltDel();
expect(client._sock).to.have.sent(expected._sQ);
});
it('should not send the keys if we are not in a normal state', function () {
client._rfb_connection_state = "broken";
client.sendCtrlAltDel();
expect(client._sock.flush).to.not.have.been.called;
});
it('should not send the keys if we are set as view_only', function () {
client._view_only = true;
client.sendCtrlAltDel();
expect(client._sock.flush).to.not.have.been.called;
});
});
describe('#sendKey', function () {
beforeEach(function () {
client._sock = new Websock();
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_connection_state = 'connected';
client._view_only = false;
});
it('should send a single key with the given code and state (down = true)', function () {
var expected = {_sQ: new Uint8Array(8), _sQlen: 0, flush: function () {}};
RFB.messages.keyEvent(expected, 123, 1);
client.sendKey(123, 'Key123', true);
expect(client._sock).to.have.sent(expected._sQ);
});
it('should send both a down and up event if the state is not specified', function () {
var expected = {_sQ: new Uint8Array(16), _sQlen: 0, flush: function () {}};
RFB.messages.keyEvent(expected, 123, 1);
RFB.messages.keyEvent(expected, 123, 0);
client.sendKey(123, 'Key123');
expect(client._sock).to.have.sent(expected._sQ);
});
it('should not send the key if we are not in a normal state', function () {
client._rfb_connection_state = "broken";
client.sendKey(123, 'Key123');
expect(client._sock.flush).to.not.have.been.called;
});
it('should not send the key if we are set as view_only', function () {
client._view_only = true;
client.sendKey(123, 'Key123');
expect(client._sock.flush).to.not.have.been.called;
});
it('should send QEMU extended events if supported', function () {
client._qemuExtKeyEventSupported = true;
var expected = {_sQ: new Uint8Array(12), _sQlen: 0, flush: function () {}};
RFB.messages.QEMUExtendedKeyEvent(expected, 0x20, true, 0x0039);
client.sendKey(0x20, 'Space', true);
expect(client._sock).to.have.sent(expected._sQ);
});
});
describe('#clipboardPasteFrom', function () {
beforeEach(function () {
client._sock = new Websock();
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_connection_state = 'connected';
client._view_only = false;
});
it('should send the given text in a paste event', function () {
var expected = {_sQ: new Uint8Array(11), _sQlen: 0, flush: function () {}};
RFB.messages.clientCutText(expected, 'abc');
client.clipboardPasteFrom('abc');
expect(client._sock).to.have.sent(expected._sQ);
});
it('should not send the text if we are not in a normal state', function () {
client._rfb_connection_state = "broken";
client.clipboardPasteFrom('abc');
expect(client._sock.flush).to.not.have.been.called;
});
});
describe("#requestDesktopSize", function () {
beforeEach(function() {
client._sock = new Websock();
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_connection_state = 'connected';
client._view_only = false;
client._supportsSetDesktopSize = true;
});
it('should send the request with the given width and height', function () {
var expected = [251];
push8(expected, 0); // padding
push16(expected, 1); // width
push16(expected, 2); // height
push8(expected, 1); // number-of-screens
push8(expected, 0); // padding before screen array
push32(expected, 0); // id
push16(expected, 0); // x-position
push16(expected, 0); // y-position
push16(expected, 1); // width
push16(expected, 2); // height
push32(expected, 0); // flags
client.requestDesktopSize(1, 2);
expect(client._sock).to.have.sent(new Uint8Array(expected));
});
it('should not send the request if the client has not recieved a ExtendedDesktopSize rectangle', function () {
client._supportsSetDesktopSize = false;
client.requestDesktopSize(1,2);
expect(client._sock.flush).to.not.have.been.called;
});
it('should not send the request if we are not in a normal state', function () {
client._rfb_connection_state = "broken";
client.requestDesktopSize(1,2);
expect(client._sock.flush).to.not.have.been.called;
});
});
describe("XVP operations", function () {
beforeEach(function () {
client._sock = new Websock();
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_connection_state = 'connected';
client._view_only = false;
client._rfb_xvp_ver = 1;
});
it('should send the shutdown signal on #xvpShutdown', function () {
client.xvpShutdown();
expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x02]));
});
it('should send the reboot signal on #xvpReboot', function () {
client.xvpReboot();
expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x03]));
});
it('should send the reset signal on #xvpReset', function () {
client.xvpReset();
expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x04]));
});
it('should support sending arbitrary XVP operations via #xvpOp', function () {
client.xvpOp(1, 7);
expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x07]));
});
it('should not send XVP operations with higher versions than we support', function () {
expect(client.xvpOp(2, 7)).to.be.false;
expect(client._sock.flush).to.not.have.been.called;
});
});
});
describe('Misc Internals', function () {
describe('#_updateConnectionState', function () {
var client;
beforeEach(function () {
this.clock = sinon.useFakeTimers();
client = make_rfb();
});
afterEach(function () {
this.clock.restore();
});
it('should clear the disconnect timer if the state is not "disconnecting"', function () {
var spy = sinon.spy();
client._disconnTimer = setTimeout(spy, 50);
client._updateConnectionState('connecting');
this.clock.tick(51);
expect(spy).to.not.have.been.called;
expect(client._disconnTimer).to.be.null;
});
it('should call the updateState callback', function () {
client.set_onUpdateState(sinon.spy());
client._updateConnectionState('connecting');
var spy = client.get_onUpdateState();
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][1]).to.equal('connecting');
});
it('should set the rfb_connection_state', function () {
client._rfb_connection_state = 'disconnecting';
client._updateConnectionState('disconnected');
expect(client._rfb_connection_state).to.equal('disconnected');
});
it('should not change the state when we are disconnected', function () {
client._rfb_connection_state = 'disconnected';
client._updateConnectionState('connecting');
expect(client._rfb_connection_state).to.not.equal('connecting');
});
it('should ignore state changes to the same state', function () {
client.set_onUpdateState(sinon.spy());
client._rfb_connection_state = 'connecting';
client._updateConnectionState('connecting');
var spy = client.get_onUpdateState();
expect(spy).to.not.have.been.called;
});
it('should ignore illegal state changes', function () {
client.set_onUpdateState(sinon.spy());
client._rfb_connection_state = 'connected';
client._updateConnectionState('disconnected');
expect(client._rfb_connection_state).to.not.equal('disconnected');
var spy = client.get_onUpdateState();
expect(spy).to.not.have.been.called;
});
});
describe('#_fail', function () {
var client;
beforeEach(function () {
this.clock = sinon.useFakeTimers();
client = make_rfb();
client.connect('host', 8675);
});
afterEach(function () {
this.clock.restore();
});
it('should close the WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._fail();
expect(client._sock.close).to.have.been.calledOnce;
});
it('should transition to disconnected', function () {
sinon.spy(client, '_updateConnectionState');
client._fail();
this.clock.tick(2000);
expect(client._updateConnectionState).to.have.been.called;
expect(client._rfb_connection_state).to.equal('disconnected');
});
it('should set disconnect_reason', function () {
client._rfb_connection_state = 'connected';
client._fail('a reason');
expect(client._rfb_disconnect_reason).to.equal('a reason');
});
it('should not include details in disconnect_reason', function () {
client._rfb_connection_state = 'connected';
client._fail('a reason', 'details');
expect(client._rfb_disconnect_reason).to.equal('a reason');
});
it('should result in disconnect callback with message when reason given', function () {
client._rfb_connection_state = 'connected';
client.set_onDisconnected(sinon.spy());
client._fail('a reason');
var spy = client.get_onDisconnected();
this.clock.tick(2000);
expect(spy).to.have.been.calledOnce;
expect(spy.args[0].length).to.equal(2);
expect(spy.args[0][1]).to.equal('a reason');
});
});
describe('#_notification', function () {
var client;
beforeEach(function () { client = make_rfb(); });
it('should call the notification callback', function () {
client.set_onNotification(sinon.spy());
client._notification('notify!', 'warn');
var spy = client.get_onNotification();
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][1]).to.equal('notify!');
expect(spy.args[0][2]).to.equal('warn');
});
it('should not call the notification callback when level is invalid', function () {
client.set_onNotification(sinon.spy());
client._notification('notify!', 'invalid');
var spy = client.get_onNotification();
expect(spy).to.not.have.been.called;
});
});
});
describe('Connection States', function () {
describe('connecting', function () {
var client;
beforeEach(function () { client = make_rfb(); });
it('should reset the variable states', function () {
sinon.spy(client, '_init_vars');
client._updateConnectionState('connecting');
expect(client._init_vars).to.have.been.calledOnce;
});
it('should actually connect to the websocket', function () {
sinon.spy(client._sock, 'open');
client._updateConnectionState('connecting');
expect(client._sock.open).to.have.been.calledOnce;
});
it('should use wss:// to connect if encryption is enabled', function () {
sinon.spy(client._sock, 'open');
client.set_encrypt(true);
client._updateConnectionState('connecting');
expect(client._sock.open.args[0][0]).to.contain('wss://');
});
it('should use ws:// to connect if encryption is not enabled', function () {
sinon.spy(client._sock, 'open');
client.set_encrypt(true);
client._updateConnectionState('connecting');
expect(client._sock.open.args[0][0]).to.contain('wss://');
});
it('should use a uri with the host, port, and path specified to connect', function () {
sinon.spy(client._sock, 'open');
client.set_encrypt(false);
client._rfb_host = 'HOST';
client._rfb_port = 8675;
client._rfb_path = 'PATH';
client._updateConnectionState('connecting');
expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH');
});
});
describe('disconnecting', function () {
var client;
beforeEach(function () {
this.clock = sinon.useFakeTimers();
client = make_rfb();
client.connect('host', 8675);
});
afterEach(function () {
this.clock.restore();
});
it('should force disconnect if we do not call Websock.onclose within the disconnection timeout', function () {
sinon.spy(client, '_updateConnectionState');
client._sock._websocket.close = function () {}; // explicitly don't call onclose
client._updateConnectionState('disconnecting');
this.clock.tick(client.get_disconnectTimeout() * 1000);
expect(client._updateConnectionState).to.have.been.calledTwice;
expect(client._rfb_disconnect_reason).to.not.equal("");
expect(client._rfb_connection_state).to.equal("disconnected");
});
it('should not fail if Websock.onclose gets called within the disconnection timeout', function () {
client._updateConnectionState('disconnecting');
this.clock.tick(client.get_disconnectTimeout() * 500);
client._sock._websocket.close();
this.clock.tick(client.get_disconnectTimeout() * 500 + 1);
expect(client._rfb_connection_state).to.equal('disconnected');
});
it('should close the WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateConnectionState('disconnecting');
expect(client._sock.close).to.have.been.calledOnce;
});
});
describe('disconnected', function () {
var client;
beforeEach(function () { client = make_rfb(); });
it('should call the disconnect callback if the state is "disconnected"', function () {
client.set_onDisconnected(sinon.spy());
client._rfb_connection_state = 'disconnecting';
client._rfb_disconnect_reason = "error";
client._updateConnectionState('disconnected');
var spy = client.get_onDisconnected();
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][1]).to.equal("error");
});
it('should not call the disconnect callback if the state is not "disconnected"', function () {
client.set_onDisconnected(sinon.spy());
client._updateConnectionState('disconnecting');
var spy = client.get_onDisconnected();
expect(spy).to.not.have.been.called;
});
it('should call the disconnect callback without msg when no reason given', function () {
client.set_onDisconnected(sinon.spy());
client._rfb_connection_state = 'disconnecting';
client._rfb_disconnect_reason = "";
client._updateConnectionState('disconnected');
var spy = client.get_onDisconnected();
expect(spy).to.have.been.calledOnce;
expect(spy.args[0].length).to.equal(1);
});
it('should call the updateState callback before the disconnect callback', function () {
client.set_onDisconnected(sinon.spy());
client.set_onUpdateState(sinon.spy());
client._rfb_connection_state = 'disconnecting';
client._updateConnectionState('disconnected');
var updateStateSpy = client.get_onUpdateState();
var disconnectSpy = client.get_onDisconnected();
expect(updateStateSpy.calledBefore(disconnectSpy)).to.be.true;
});
});
// NB(directxman12): Connected does *nothing* in updateConnectionState
});
describe('Protocol Initialization States', function () {
describe('ProtocolVersion', function () {
beforeEach(function () {
this.clock = sinon.useFakeTimers();
});
afterEach(function () {
this.clock.restore();
});
function send_ver (ver, client) {
var arr = new Uint8Array(12);
for (var i = 0; i < ver.length; i++) {
arr[i+4] = ver.charCodeAt(i);
}
arr[0] = 'R'; arr[1] = 'F'; arr[2] = 'B'; arr[3] = ' ';
arr[11] = '\n';
client._sock._websocket._receive_data(arr);
}
describe('version parsing', function () {
var client;
beforeEach(function () {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
});
it('should interpret version 000.000 as a repeater', function () {
client._repeaterID = '\x01\x02\x03\x04\x05';
send_ver('000.000', client);
expect(client._rfb_version).to.equal(0);
var sent_data = client._sock._websocket._get_sent_data();
expect(new Uint8Array(sent_data.buffer, 0, 5)).to.array.equal(new Uint8Array([1, 2, 3, 4, 5]));
});
it('should interpret version 003.003 as version 3.3', function () {
send_ver('003.003', client);
expect(client._rfb_version).to.equal(3.3);
});
it('should interpret version 003.006 as version 3.3', function () {
send_ver('003.006', client);
expect(client._rfb_version).to.equal(3.3);
});
it('should interpret version 003.889 as version 3.3', function () {
send_ver('003.889', client);
expect(client._rfb_version).to.equal(3.3);
});
it('should interpret version 003.007 as version 3.7', function () {
send_ver('003.007', client);
expect(client._rfb_version).to.equal(3.7);
});
it('should interpret version 003.008 as version 3.8', function () {
send_ver('003.008', client);
expect(client._rfb_version).to.equal(3.8);
});
it('should interpret version 004.000 as version 3.8', function () {
send_ver('004.000', client);
expect(client._rfb_version).to.equal(3.8);
});
it('should interpret version 004.001 as version 3.8', function () {
send_ver('004.001', client);
expect(client._rfb_version).to.equal(3.8);
});
it('should interpret version 005.000 as version 3.8', function () {
send_ver('005.000', client);
expect(client._rfb_version).to.equal(3.8);
});
it('should fail on an invalid version', function () {
sinon.spy(client, "_fail");
send_ver('002.000', client);
expect(client._fail).to.have.been.calledOnce;
});
});
var client;
beforeEach(function () {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
});
it('should handle two step repeater negotiation', function () {
client._repeaterID = '\x01\x02\x03\x04\x05';
send_ver('000.000', client);
expect(client._rfb_version).to.equal(0);
var sent_data = client._sock._websocket._get_sent_data();
expect(new Uint8Array(sent_data.buffer, 0, 5)).to.array.equal(new Uint8Array([1, 2, 3, 4, 5]));
expect(sent_data).to.have.length(250);
send_ver('003.008', client);
expect(client._rfb_version).to.equal(3.8);
});
it('should send back the interpreted version', function () {
send_ver('004.000', client);
var expected_str = 'RFB 003.008\n';
var expected = [];
for (var i = 0; i < expected_str.length; i++) {
expected[i] = expected_str.charCodeAt(i);
}
expect(client._sock).to.have.sent(new Uint8Array(expected));
});
it('should transition to the Security state on successful negotiation', function () {
send_ver('003.008', client);
expect(client._rfb_init_state).to.equal('Security');
});
});
describe('Security', function () {
var client;
beforeEach(function () {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_init_state = 'Security';
});
it('should simply receive the auth scheme when for versions < 3.7', function () {
client._rfb_version = 3.6;
var auth_scheme_raw = [1, 2, 3, 4];
var auth_scheme = (auth_scheme_raw[0] << 24) + (auth_scheme_raw[1] << 16) +
(auth_scheme_raw[2] << 8) + auth_scheme_raw[3];
client._sock._websocket._receive_data(auth_scheme_raw);
expect(client._rfb_auth_scheme).to.equal(auth_scheme);
});
it('should prefer no authentication is possible', function () {
client._rfb_version = 3.7;
var auth_schemes = [2, 1, 3];
client._sock._websocket._receive_data(auth_schemes);
expect(client._rfb_auth_scheme).to.equal(1);
expect(client._sock).to.have.sent(new Uint8Array([1, 1]));
});
it('should choose for the most prefered scheme possible for versions >= 3.7', function () {
client._rfb_version = 3.7;
var auth_schemes = [2, 22, 16];
client._sock._websocket._receive_data(auth_schemes);
expect(client._rfb_auth_scheme).to.equal(22);
expect(client._sock).to.have.sent(new Uint8Array([22]));
});
it('should fail if there are no supported schemes for versions >= 3.7', function () {
sinon.spy(client, "_fail");
client._rfb_version = 3.7;
var auth_schemes = [1, 32];
client._sock._websocket._receive_data(auth_schemes);
expect(client._fail).to.have.been.calledOnce;
});
it('should fail with the appropriate message if no types are sent for versions >= 3.7', function () {
client._rfb_version = 3.7;
var failure_data = [0, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115];
sinon.spy(client, '_fail');
client._sock._websocket._receive_data(failure_data);
expect(client._fail).to.have.been.calledOnce;
expect(client._fail).to.have.been.calledWith(
'Error while negotiating with server','Security failure: whoops');
});
it('should transition to the Authentication state and continue on successful negotiation', function () {
client._rfb_version = 3.7;
var auth_schemes = [1, 1];
client._negotiate_authentication = sinon.spy();
client._sock._websocket._receive_data(auth_schemes);
expect(client._rfb_init_state).to.equal('Authentication');
expect(client._negotiate_authentication).to.have.been.calledOnce;
});
});
describe('Authentication', function () {
var client;
beforeEach(function () {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_init_state = 'Security';
});
function send_security(type, cl) {
cl._sock._websocket._receive_data(new Uint8Array([1, type]));
}
it('should fail on auth scheme 0 (pre 3.7) with the given message', function () {
client._rfb_version = 3.6;
var err_msg = "Whoopsies";
var data = [0, 0, 0, 0];
var err_len = err_msg.length;
push32(data, err_len);
for (var i = 0; i < err_len; i++) {
data.push(err_msg.charCodeAt(i));
}
sinon.spy(client, '_fail');
client._sock._websocket._receive_data(new Uint8Array(data));
expect(client._fail).to.have.been.calledWith(
'Authentication failure', 'Whoopsies');
});
it('should transition straight to SecurityResult on "no auth" (1) for versions >= 3.8', function () {
client._rfb_version = 3.8;
send_security(1, client);
expect(client._rfb_init_state).to.equal('SecurityResult');
});
it('should transition straight to ServerInitialisation on "no auth" for versions < 3.8', function () {
client._rfb_version = 3.7;
send_security(1, client);
expect(client._rfb_init_state).to.equal('ServerInitialisation');
});
it('should fail on an unknown auth scheme', function () {
sinon.spy(client, "_fail");
client._rfb_version = 3.8;
send_security(57, client);
expect(client._fail).to.have.been.calledOnce;
});
describe('VNC Authentication (type 2) Handler', function () {
var client;
beforeEach(function () {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_init_state = 'Security';
client._rfb_version = 3.8;
});
it('should call the passwordRequired callback if missing a password', function () {
client.set_onPasswordRequired(sinon.spy());
send_security(2, client);
var spy = client.get_onPasswordRequired();
expect(client._rfb_password.length).to.equal(0);
expect(spy).to.have.been.calledOnce;
});
it('should encrypt the password with DES and then send it back', function () {
client._rfb_password = 'passwd';
send_security(2, client);
client._sock._websocket._get_sent_data(); // skip the choice of auth reply
var challenge = [];
for (var i = 0; i < 16; i++) { challenge[i] = i; }
client._sock._websocket._receive_data(new Uint8Array(challenge));
var des_pass = RFB.genDES('passwd', challenge);
expect(client._sock).to.have.sent(new Uint8Array(des_pass));
});
it('should transition to SecurityResult immediately after sending the password', function () {
client._rfb_password = 'passwd';
send_security(2, client);
var challenge = [];
for (var i = 0; i < 16; i++) { challenge[i] = i; }
client._sock._websocket._receive_data(new Uint8Array(challenge));
expect(client._rfb_init_state).to.equal('SecurityResult');
});
});
describe('XVP Authentication (type 22) Handler', function () {
var client;
beforeEach(function () {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_init_state = 'Security';
client._rfb_version = 3.8;
});
it('should fall through to standard VNC authentication upon completion', function () {
client.set_xvp_password_sep('#');
client._rfb_password = 'user#target#password';
client._negotiate_std_vnc_auth = sinon.spy();
send_security(22, client);
expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
});
it('should call the passwordRequired callback if the password is missing', function() {
client.set_onPasswordRequired(sinon.spy());
client._rfb_password = '';
send_security(22, client);
var spy = client.get_onPasswordRequired();
expect(client._rfb_password.length).to.equal(0);
expect(spy).to.have.been.calledOnce;
});
it('should call the passwordRequired callback if the password is improperly formatted', function() {
client.set_onPasswordRequired(sinon.spy());
client._rfb_password = 'user@target';
send_security(22, client);
var spy = client.get_onPasswordRequired();
expect(spy).to.have.been.calledOnce;
});
it('should split the password, send the first two parts, and pass on the last part', function () {
client.set_xvp_password_sep('#');
client._rfb_password = 'user#target#password';
client._negotiate_std_vnc_auth = sinon.spy();
send_security(22, client);
expect(client._rfb_password).to.equal('password');
var expected = [22, 4, 6]; // auth selection, len user, len target
for (var i = 0; i < 10; i++) { expected[i+3] = 'usertarget'.charCodeAt(i); }
expect(client._sock).to.have.sent(new Uint8Array(expected));
});
});
describe('TightVNC Authentication (type 16) Handler', function () {
var client;
beforeEach(function () {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_init_state = 'Security';
client._rfb_version = 3.8;
send_security(16, client);
client._sock._websocket._get_sent_data(); // skip the security reply
});
function send_num_str_pairs(pairs, client) {
var pairs_len = pairs.length;
var data = [];
push32(data, pairs_len);
for (var i = 0; i < pairs_len; i++) {
push32(data, pairs[i][0]);
var j;
for (j = 0; j < 4; j++) {
data.push(pairs[i][1].charCodeAt(j));
}
for (j = 0; j < 8; j++) {
data.push(pairs[i][2].charCodeAt(j));
}
}
client._sock._websocket._receive_data(new Uint8Array(data));
}
it('should skip tunnel negotiation if no tunnels are requested', function () {
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
expect(client._rfb_tightvnc).to.be.true;
});
it('should fail if no supported tunnels are listed', function () {
sinon.spy(client, "_fail");
send_num_str_pairs([[123, 'OTHR', 'SOMETHNG']], client);
expect(client._fail).to.have.been.calledOnce;
});
it('should choose the notunnel tunnel type', function () {
send_num_str_pairs([[0, 'TGHT', 'NOTUNNEL'], [123, 'OTHR', 'SOMETHNG']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 0]));
});
it('should continue to sub-auth negotiation after tunnel negotiation', function () {
send_num_str_pairs([[0, 'TGHT', 'NOTUNNEL']], client);
client._sock._websocket._get_sent_data(); // skip the tunnel choice here
send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1]));
expect(client._rfb_init_state).to.equal('SecurityResult');
});
/*it('should attempt to use VNC auth over no auth when possible', function () {
client._rfb_tightvnc = true;
client._negotiate_std_vnc_auth = sinon.spy();
send_num_str_pairs([[1, 'STDV', 'NOAUTH__'], [2, 'STDV', 'VNCAUTH_']], client);
expect(client._sock).to.have.sent([0, 0, 0, 1]);
expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
expect(client._rfb_auth_scheme).to.equal(2);
});*/ // while this would make sense, the original code doesn't actually do this
it('should accept the "no auth" auth type and transition to SecurityResult', function () {
client._rfb_tightvnc = true;
send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1]));
expect(client._rfb_init_state).to.equal('SecurityResult');
});
it('should accept VNC authentication and transition to that', function () {
client._rfb_tightvnc = true;
client._negotiate_std_vnc_auth = sinon.spy();
send_num_str_pairs([[2, 'STDV', 'VNCAUTH__']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 2]));
expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;