]> git.proxmox.com Git - mirror_novnc.git/blobdiff - tests/test.rfb.js
Merge branch 'qemufix' of https://github.com/CendioOssman/noVNC
[mirror_novnc.git] / tests / test.rfb.js
index 828456afbfa0cc508bb7039caff9da89af1dee50..b39f52e6803324b4c0b0580f347ecf234427d020 100644 (file)
@@ -1,9 +1,16 @@
-// requires local modules: util, websock, rfb, input/util, input/keysym, input/keysymdef, input/devices, inflator, des, display
-// requires test modules: fake.websocket, assertions
 /* 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 = {};
@@ -186,7 +193,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
             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, true);
+                client.sendKey(123, 'Key123', true);
                 expect(client._sock).to.have.sent(expected._sQ);
             });
 
@@ -194,21 +201,29 @@ describe('Remote Frame Buffer Protocol Client', 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);
+                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);
+                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);
+                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 () {
@@ -330,7 +345,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
             it('should clear the disconnect timer if the state is not "disconnecting"', function () {
                 var spy = sinon.spy();
                 client._disconnTimer = setTimeout(spy, 50);
-                client._updateConnectionState('connected');
+                client._updateConnectionState('connecting');
                 this.clock.tick(51);
                 expect(spy).to.not.have.been.called;
                 expect(client._disconnTimer).to.be.null;
@@ -338,27 +353,37 @@ describe('Remote Frame Buffer Protocol Client', function() {
 
             it('should call the updateState callback', function () {
                 client.set_onUpdateState(sinon.spy());
-                client._updateConnectionState('a specific state');
+                client._updateConnectionState('connecting');
                 var spy = client.get_onUpdateState();
                 expect(spy).to.have.been.calledOnce;
-                expect(spy.args[0][1]).to.equal('a specific state');
+                expect(spy.args[0][1]).to.equal('connecting');
             });
 
             it('should set the rfb_connection_state', function () {
-                client._updateConnectionState('a specific state');
-                expect(client._rfb_connection_state).to.equal('a specific state');
+                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('a specific state');
-                expect(client._rfb_connection_state).to.not.equal('a specific state');
+                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 = 'a specific state';
-                client._updateConnectionState('a specific state');
+                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;
             });
@@ -391,11 +416,19 @@ describe('Remote Frame Buffer Protocol Client', function() {
             });
 
             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();
@@ -542,7 +575,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
             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 = 'other state';
+                client._rfb_connection_state = 'disconnecting';
                 client._updateConnectionState('disconnected');
                 var updateStateSpy = client.get_onUpdateState();
                 var disconnectSpy = client.get_onDisconnected();
@@ -694,12 +727,20 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 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, 1, 2];
+                var auth_schemes = [2, 22, 16];
                 client._sock._websocket._receive_data(auth_schemes);
-                expect(client._rfb_auth_scheme).to.equal(2);
-                expect(client._sock).to.have.sent(new Uint8Array([2]));
+                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 () {
@@ -717,7 +758,8 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 client._sock._websocket._receive_data(failure_data);
 
                 expect(client._fail).to.have.been.calledOnce;
-                expect(client._fail).to.have.been.calledWith('Security failure: whoops');
+                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 () {
@@ -756,7 +798,8 @@ describe('Remote Frame Buffer Protocol Client', function() {
 
                 sinon.spy(client, '_fail');
                 client._sock._websocket._receive_data(new Uint8Array(data));
-                expect(client._fail).to.have.been.calledWith('Auth failure: Whoopsies');
+                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 () {
@@ -988,7 +1031,8 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 sinon.spy(client, '_fail');
                 var failure_data = [0, 0, 0, 1, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115];
                 client._sock._websocket._receive_data(new Uint8Array(failure_data));
-                expect(client._fail).to.have.been.calledWith('whoops');
+                expect(client._fail).to.have.been.calledWith(
+                    'Authentication failure', 'whoops');
             });
 
             it('should fail on an error code of 1 with a standard message for version < 3.8', function () {
@@ -1113,14 +1157,6 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 expect(client._rfb_connection_state).to.equal('connected');
             });
 
-            it('should set the true color mode on the display to the configuration variable', function () {
-                client.set_true_color(false);
-                sinon.spy(client._display, 'set_true_color');
-                send_server_init({ true_color: 1 }, client);
-                expect(client._display.set_true_color).to.have.been.calledOnce;
-                expect(client._display.set_true_color).to.have.been.calledWith(false);
-            });
-
             it('should call the resize callback and resize the display', function () {
                 client.set_onFBResize(sinon.spy());
                 sinon.spy(client._display, 'resize');
@@ -1142,23 +1178,8 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 expect(client._mouse.grab).to.have.been.calledOnce;
             });
 
-            it('should set the BPP and depth to 4 and 3 respectively if in true color mode', function () {
-                client.set_true_color(true);
-                send_server_init({}, client);
-                expect(client._fb_Bpp).to.equal(4);
-                expect(client._fb_depth).to.equal(3);
-            });
-
-            it('should set the BPP and depth to 1 and 1 respectively if not in true color mode', function () {
-                client.set_true_color(false);
-                send_server_init({}, client);
-                expect(client._fb_Bpp).to.equal(1);
-                expect(client._fb_depth).to.equal(1);
-            });
-
             // TODO(directxman12): test the various options in this configuration matrix
             it('should reply with the pixel format, client encodings, and initial update request', function () {
-                client.set_true_color(true);
                 client.set_local_cursor(false);
                 // we skip the cursor encoding
                 var expected = {_sQ: new Uint8Array(34 + 4 * (client._encodings.length - 1)),
@@ -1253,7 +1274,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
 
             it('should send an update request if there is sufficient data', function () {
                 var expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: function() {}};
-                RFB.messages.fbUpdateRequest(expected_msg, false, 0, 0, 240, 20);
+                RFB.messages.fbUpdateRequest(expected_msg, true, 0, 0, 640, 20);
 
                 client._framebufferUpdate = function () { return true; };
                 client._sock._websocket._receive_data(new Uint8Array([0]));
@@ -1268,7 +1289,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
 
             it('should resume receiving an update if we previously did not have enough data', function () {
                 var expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: function() {}};
-                RFB.messages.fbUpdateRequest(expected_msg, false, 0, 0, 240, 20);
+                RFB.messages.fbUpdateRequest(expected_msg, true, 0, 0, 640, 20);
 
                 // just enough to set FBU.rects
                 client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 3]));
@@ -1280,43 +1301,9 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 expect(client._sock).to.have.sent(expected_msg._sQ);
             });
 
-            it('should send a request for both clean and dirty areas', function () {
-                var expected_msg = {_sQ: new Uint8Array(20), _sQlen: 0, flush: function() {}};
-                var expected_cdr = { cleanBox: { x: 0, y: 0, w: 120, h: 20 },
-                                     dirtyBoxes: [ { x: 120, y: 0, w: 120, h: 20 } ] };
-
-                RFB.messages.fbUpdateRequest(expected_msg, true, 0, 0, 120, 20);
-                RFB.messages.fbUpdateRequest(expected_msg, false, 120, 0, 120, 20);
-
-                client._framebufferUpdate = function () { return true; };
-                client._display.getCleanDirtyReset = function () { return expected_cdr; };
-                client._sock._websocket._receive_data(new Uint8Array([0]));
-
-                expect(client._sock).to.have.sent(expected_msg._sQ);
-            });
-
-            it('should only request non-incremental rects in continuous updates mode', function () {
-                var expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: function() {}};
-                var expected_cdr = { cleanBox: { x: 0, y: 0, w: 120, h: 20 },
-                                     dirtyBoxes: [ { x: 120, y: 0, w: 120, h: 20 } ] };
-
-                RFB.messages.fbUpdateRequest(expected_msg, false, 120, 0, 120, 20);
-
-                client._enabledContinuousUpdates = true;
-                client._framebufferUpdate = function () { return true; };
-                client._display.getCleanDirtyReset = function () { return expected_cdr; };
-                client._sock._websocket._receive_data(new Uint8Array([0]));
-
-                expect(client._sock).to.have.sent(expected_msg._sQ);
-            });
-
-            it('should not send a request in continuous updates mode when clean', function () {
-                var expected_cdr = { cleanBox: { x: 0, y: 0, w: 240, h: 20 },
-                                     dirtyBoxes: [] };
-
+            it('should not send a request in continuous updates mode', function () {
                 client._enabledContinuousUpdates = true;
                 client._framebufferUpdate = function () { return true; };
-                client._display.getCleanDirtyReset = function () { return expected_cdr; };
                 client._sock._websocket._receive_data(new Uint8Array([0]));
 
                 expect(client._sock._websocket._get_sent_data()).to.have.length(0);
@@ -1368,14 +1355,11 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 client._fb_width = 4;
                 client._fb_height = 4;
                 client._display.resize(4, 4);
-                var initial_data = client._display._drawCtx.createImageData(4, 2);
-                var initial_data_arr = target_data_check_arr.slice(0, 32);
-                for (var i = 0; i < 32; i++) { initial_data.data[i] = initial_data_arr[i]; }
-                client._display._drawCtx.putImageData(initial_data, 0, 0);
+                client._display.blitRgbxImage(0, 0, 4, 2, new Uint8Array(target_data_check_arr.slice(0, 32)), 0);
 
                 var info = [{ x: 0, y: 2, width: 2, height: 2, encoding: 0x01},
                             { x: 2, y: 2, width: 2, height: 2, encoding: 0x01}];
-                // data says [{ old_x: 0, old_y: 0 }, { old_x: 0, old_y: 0 }]
+                // data says [{ old_x: 2, old_y: 0 }, { old_x: 0, old_y: 0 }]
                 var rects = [[0, 2, 0, 0], [0, 0, 0, 0]];
                 send_fbu_msg([info[0]], [rects[0]], client, 2);
                 send_fbu_msg([info[1]], [rects[1]], client, -1);
@@ -1394,11 +1378,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
                     // a really small frame
                     client._fb_width = 4;
                     client._fb_height = 4;
-                    client._display._fb_width = 4;
-                    client._display._fb_height = 4;
-                    client._display._viewportLoc.w = 4;
-                    client._display._viewportLoc.h = 4;
-                    client._fb_Bpp = 4;
+                    client._display.resize(4, 4);
                 });
 
                 it('should handle the RAW encoding', function () {
@@ -1418,10 +1398,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
 
                 it('should handle the COPYRECT encoding', function () {
                     // seed some initial data to copy
-                    var initial_data = client._display._drawCtx.createImageData(4, 2);
-                    var initial_data_arr = target_data_check_arr.slice(0, 32);
-                    for (var i = 0; i < 32; i++) { initial_data.data[i] = initial_data_arr[i]; }
-                    client._display._drawCtx.putImageData(initial_data, 0, 0);
+                    client._display.blitRgbxImage(0, 0, 4, 2, new Uint8Array(target_data_check_arr.slice(0, 32)), 0);
 
                     var info = [{ x: 0, y: 2, width: 2, height: 2, encoding: 0x01},
                                 { x: 2, y: 2, width: 2, height: 2, encoding: 0x01}];
@@ -1471,11 +1448,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
                         // a really small frame
                         client._fb_width = 4;
                         client._fb_height = 4;
-                        client._display._fb_width = 4;
-                        client._display._fb_height = 4;
-                        client._display._viewportLoc.w = 4;
-                        client._display._viewportLoc.h = 4;
-                        client._fb_Bpp = 4;
+                        client._display.resize(4, 4);
                     });
 
                     it('should handle a tile with fg, bg specified, normal subrects', function () {
@@ -1525,8 +1498,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
                     it('should handle a tile with only bg specified and an empty frame afterwards', function () {
                         // set the width so we can have two tiles
                         client._fb_width = 8;
-                        client._display._fb_width = 8;
-                        client._display._viewportLoc.w = 8;
+                        client._display.resize(8, 4);
 
                         var info = [{ x: 0, y: 0, width: 32, height: 4, encoding: 0x05 }];
 
@@ -1649,11 +1621,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
                         // a really small frame
                         client._fb_width = 4;
                         client._fb_height = 4;
-                        client._display._fb_width = 4;
-                        client._display._fb_height = 4;
-                        client._display._viewportLoc.w = 4;
-                        client._display._viewportLoc.h = 4;
-                        client._fb_Bpp = 4;
+                        client._display.resize(4, 4);
                         sinon.spy(client._display, 'resize');
                         client.set_onFBResize(sinon.spy());
                     });
@@ -1765,21 +1733,6 @@ describe('Remote Frame Buffer Protocol Client', function() {
             });
         });
 
-        it('should set the colour map on the display on SetColourMapEntries', function () {
-            var expected_cm = [];
-            var data = [1, 0, 0, 1, 0, 4];
-            var i;
-            for (i = 0; i < 4; i++) {
-                expected_cm[i + 1] = [i * 10, i * 10 + 1, i * 10 + 2];
-                push16(data, expected_cm[i + 1][2] << 8);
-                push16(data, expected_cm[i + 1][1] << 8);
-                push16(data, expected_cm[i + 1][0] << 8);
-            }
-
-            client._sock._websocket._receive_data(new Uint8Array(data));
-            expect(client._display.get_colourMap()).to.deep.equal(expected_cm);
-        });
-
         describe('XVP Message Handling', function () {
             beforeEach(function () {
                 client = make_rfb();
@@ -2002,19 +1955,24 @@ describe('Remote Frame Buffer Protocol Client', function() {
             });
 
             it('if enabled, viewportDragging should occur on mouse movement while a button is down', function () {
+                var oldX = 123;
+                var oldY = 109;
+                var newX = 123 + 11 * window.devicePixelRatio;
+                var newY = 109 + 4 * window.devicePixelRatio;
+
                 client._viewportDrag = true;
                 client._viewportDragging = true;
                 client._viewportHasMoved = false;
-                client._viewportDragPos = { x: 23, y: 9 };
+                client._viewportDragPos = { x: oldX, y: oldY };
                 client._display.viewportChangePos = sinon.spy();
 
-                client._mouse._onMouseMove(10, 4);
+                client._mouse._onMouseMove(newX, newY);
 
                 expect(client._viewportDragging).to.be.true;
                 expect(client._viewportHasMoved).to.be.true;
-                expect(client._viewportDragPos).to.deep.equal({ x: 10, y: 4 });
+                expect(client._viewportDragPos).to.deep.equal({ x: newX, y: newY });
                 expect(client._display.viewportChangePos).to.have.been.calledOnce;
-                expect(client._display.viewportChangePos).to.have.been.calledWith(13, 5);
+                expect(client._display.viewportChangePos).to.have.been.calledWith(oldX - newX, oldY - newY);
             });
         });
 
@@ -2026,22 +1984,21 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 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 key message on a key press', function () {
                 var keyevent = {};
-                keyevent.type = 'keydown';
-                keyevent.keysym = {};
-                keyevent.keysym.keysym = 1234;
-                client._keyboard._onKeyPress(keyevent);
+                client._keyboard._onKeyEvent(0x41, 'KeyA', true);
                 var key_msg = {_sQ: new Uint8Array(8), _sQlen: 0, flush: function () {}};
-                RFB.messages.keyEvent(key_msg, 1234, 1);
+                RFB.messages.keyEvent(key_msg, 0x41, 1);
                 expect(client._sock).to.have.sent(key_msg._sQ);
             });
 
             it('should not send messages in view-only mode', function () {
                 client._view_only = true;
-                client._keyboard._onKeyPress(1234, 1);
+                client._keyboard._onKeyEvent('a', 'KeyA', true);
                 expect(client._sock.flush).to.not.have.been.called;
             });
         });
@@ -2120,6 +2077,14 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 expect(client._fail).to.have.been.calledOnce;
             });
 
+            it('should fail if we get a close event while disconnected', function () {
+                sinon.spy(client, "_fail");
+                client.connect('host', 8675);
+                client._rfb_connection_state = 'disconnected';
+                client._sock._websocket.close();
+                expect(client._fail).to.have.been.calledOnce;
+            });
+
             it('should unregister close event handler', function () {
                 sinon.spy(client._sock, 'off');
                 client.connect('host', 8675);