-// 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 = {};
expect(client._updateConnectionState).to.have.been.calledWith('connecting');
});
- it('should fail if we are missing a host', function () {
- sinon.spy(client, '_fail');
+ 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 fail if we are missing a port', function () {
- sinon.spy(client, '_fail');
+ 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;
- });
-
- it('should not update the state if we are missing a host or port', function () {
- sinon.spy(client, '_fail');
- client.connect('abc');
- expect(client._fail).to.have.been.calledOnce;
- expect(client._updateConnectionState).to.have.been.calledOnce;
- expect(client._updateConnectionState).to.have.been.calledWith('failed');
+ expect(client._updateConnectionState).to.not.have.been.called;
+ expect(client._rfb_connection_state).to.equal('');
});
});
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;
});
+
+ 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 () {
expect(spy).to.not.have.been.called;
});
});
-
});
describe('Connection States', function () {
- describe('loaded', function () {
- var client;
- beforeEach(function () {
- client = make_rfb();
- client._rfb_connection_state = 'disconnected';
- });
-
- it('should close any open WebSocket connection', function () {
- sinon.spy(client._sock, 'close');
- client._updateConnectionState('loaded');
- expect(client._sock.close).to.have.been.calledOnce;
- });
- });
-
- describe('disconnected', function () {
- var client;
- beforeEach(function () { client = make_rfb(); });
-
- it('should close any open WebSocket connection', function () {
- sinon.spy(client._sock, 'close');
- client._updateConnectionState('disconnected');
- expect(client._sock.close).to.have.been.calledOnce;
- });
- });
-
describe('connecting', function () {
var client;
beforeEach(function () { client = make_rfb(); });
client._updateConnectionState('connecting');
expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH');
});
-
- it('should attempt to close the websocket before we open an new one', function () {
- sinon.spy(client._sock, 'close');
- client._updateConnectionState('connecting');
- expect(client._sock.close).to.have.been.calledOnce;
- });
});
describe('disconnecting', function () {
this.clock.restore();
});
- it('should fail if we do not call Websock.onclose within the disconnection timeout', function () {
+ 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._rfb_connection_state).to.equal('failed');
+ 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 () {
it('should close the WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateConnectionState('disconnecting');
- expect(client._sock.close).to.have.been.calledTwice; // once on loaded, once on disconnect
+ expect(client._sock.close).to.have.been.calledOnce;
});
});
- describe('failed', function () {
+ describe('disconnected', function () {
var client;
- beforeEach(function () {
- this.clock = sinon.useFakeTimers();
- client = make_rfb();
- client.connect('host', 8675);
- });
+ beforeEach(function () { client = make_rfb(); });
- afterEach(function () {
- this.clock.restore();
+ 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 close the WebSocket connection', function () {
- sinon.spy(client._sock, 'close');
- client._updateConnectionState('failed');
- expect(client._sock.close).to.have.been.called;
+ 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 transition to disconnected but stay in failed state', function () {
- client.set_onUpdateState(sinon.spy());
- client._updateConnectionState('failed');
- this.clock.tick(50);
- expect(client._rfb_connection_state).to.equal('failed');
-
- var onUpdateState = client.get_onUpdateState();
- expect(onUpdateState).to.have.been.called;
- // it should be specifically the last call
- expect(onUpdateState.args[onUpdateState.args.length - 1][1]).to.equal('disconnected');
- expect(onUpdateState.args[onUpdateState.args.length - 1][2]).to.equal('failed');
+ 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);
});
- });
-
- describe('fatal', function () {
- var client;
- beforeEach(function () { client = make_rfb(); });
-
- it('should close any open WebSocket connection', function () {
- sinon.spy(client._sock, 'close');
- client._updateConnectionState('fatal');
- expect(client._sock.close).to.have.been.calledOnce;
+ 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;
});
});
});
it('should fail on an invalid version', function () {
+ sinon.spy(client, "_fail");
send_ver('002.000', client);
- expect(client._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledOnce;
});
});
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 () {
+ sinon.spy(client, "_fail");
client._rfb_version = 3.7;
var auth_schemes = [1, 32];
client._sock._websocket._receive_data(auth_schemes);
- expect(client._rfb_connection_state).to.equal('failed');
+ 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._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 () {
sinon.spy(client, '_fail');
client._sock._websocket._receive_data(new Uint8Array(data));
- expect(client._rfb_connection_state).to.equal('failed');
- 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 () {
});
it('should fail on an unknown auth scheme', function () {
+ sinon.spy(client, "_fail");
client._rfb_version = 3.8;
send_security(57, client);
- expect(client._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledOnce;
});
describe('VNC Authentication (type 2) Handler', function () {
});
it('should fail if no supported tunnels are listed', function () {
+ sinon.spy(client, "_fail");
send_num_str_pairs([[123, 'OTHR', 'SOMETHNG']], client);
- expect(client._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledOnce;
});
it('should choose the notunnel tunnel type', function () {
});
it('should fail if there are no supported auth types', function () {
+ sinon.spy(client, "_fail");
client._rfb_tightvnc = true;
send_num_str_pairs([[23, 'stdv', 'badval__']], client);
- expect(client._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledOnce;
});
});
});
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._rfb_connection_state).to.equal('failed');
- 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 () {
+ sinon.spy(client, '_fail');
client._rfb_version = 3.7;
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 1]));
- expect(client._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledWith('Authentication failure');
});
});
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]));
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]));
expect(client._sock._websocket._get_sent_data()).to.have.length(0);
- client._framebufferUpdate = function () { return true; }; // we magically have enough data
+ client._framebufferUpdate = function () { this._sock.rQskip8(); return true; }; // we magically have enough data
// 247 should *not* be used as the message type here
client._sock._websocket._receive_data(new Uint8Array([247]));
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);
-
+ 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).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: [] };
-
- 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);
});
it('should fail on an unsupported encoding', function () {
- client.set_onFBUReceive(sinon.spy());
+ sinon.spy(client, "_fail");
var rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 234 };
send_fbu_msg([rect_info], [[]], client);
- expect(client._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledOnce;
});
it('should be able to pause and resume receiving rects if not enought data', 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);
// 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._display.resize(4, 4);
client._fb_Bpp = 4;
});
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}];
// 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._display.resize(4, 4);
client._fb_Bpp = 4;
});
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 }];
});
it('should fail on an invalid subencoding', function () {
+ sinon.spy(client,"_fail");
var info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
var rects = [[45]]; // an invalid subencoding
send_fbu_msg(info, rects, client);
- expect(client._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledOnce;
});
});
// 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._display.resize(4, 4);
client._fb_Bpp = 4;
sinon.spy(client._display, 'resize');
client.set_onFBResize(sinon.spy());
});
it('should fail on unknown XVP message types', function () {
+ sinon.spy(client, "_fail");
client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 237]));
- expect(client._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledOnce;
});
});
});
it('should fail on an unknown message type', function () {
+ sinon.spy(client, "_fail");
client._sock._websocket._receive_data(new Uint8Array([87]));
- expect(client._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledOnce;
});
});
it('should send a key message on a key press', function () {
var keyevent = {};
keyevent.type = 'keydown';
- keyevent.keysym = {};
- keyevent.keysym.keysym = 1234;
+ keyevent.keysym = 1234;
client._keyboard._onKeyPress(keyevent);
var key_msg = {_sQ: new Uint8Array(8), _sQlen: 0, flush: function () {}};
RFB.messages.keyEvent(key_msg, 1234, 1);
expect(client._init_msg).to.have.been.calledOnce;
});
- it('should split up the handling of muplitle normal messages across 10ms intervals', function () {
+ it('should process all normal messages directly', function () {
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_connection_state = 'connected';
client.set_onBell(sinon.spy());
client._sock._websocket._receive_data(new Uint8Array([0x02, 0x02]));
- expect(client.get_onBell()).to.have.been.calledOnce;
- this.clock.tick(20);
expect(client.get_onBell()).to.have.been.calledTwice;
});
});
it('should fail if we are not currently ready to connect and we get an "open" event', function () {
+ sinon.spy(client, "_fail");
client.connect('host', 8675);
client._rfb_connection_state = 'some_other_state';
client._sock._websocket._open();
- expect(client._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledOnce;
});
// close events
expect(client._rfb_connection_state).to.equal('disconnected');
});
- it('should transition to failed if we get a close event from any non-"disconnection" state', function () {
+ it('should fail if we get a close event while connecting', function () {
+ sinon.spy(client, "_fail");
client.connect('host', 8675);
- client._rfb_connection_state = 'connected';
+ client._rfb_connection_state = 'connecting';
+ client._sock._websocket.close();
+ 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._rfb_connection_state).to.equal('failed');
+ expect(client._fail).to.have.been.calledOnce;
});
it('should unregister close event handler', function () {