Separate out init state from page state

This makes the state machine simpler as we don't have to confuse
protocol states with visual states.
This commit is contained in:
Samuel Mannehed 2016-08-26 15:14:27 +02:00
parent 159c50c009
commit c00ee156b9
2 changed files with 158 additions and 145 deletions

View File

@ -35,7 +35,8 @@
this._rfb_password = '';
this._rfb_path = '';
this._rfb_state = 'disconnected';
this._rfb_connection_state = 'disconnected';
this._rfb_init_state = '';
this._rfb_version = 0;
this._rfb_max_version = 3.8;
this._rfb_auth_scheme = '';
@ -204,8 +205,10 @@
this._sock = new Websock();
this._sock.on('message', this._handle_message.bind(this));
this._sock.on('open', function () {
if (this._rfb_state === 'connect') {
this._updateState('ProtocolVersion', "Starting VNC handshake");
if ((this._rfb_connection_state === 'connect') &&
(this._rfb_init_state === '')) {
this._rfb_init_state = 'ProtocolVersion';
Util.Debug("Starting VNC handshake");
} else {
this._fail("Got unexpected WebSocket connection");
}
@ -220,14 +223,20 @@
}
msg += ")";
}
if (this._rfb_state === 'disconnect') {
this._updateState('disconnected', 'VNC disconnected' + msg);
} else if (this._rfb_state === 'ProtocolVersion') {
this._fail('Failed to connect to server' + msg);
} else if (this._rfb_state in {'failed': 1, 'disconnected': 1}) {
Util.Error("Received onclose while disconnected" + msg);
} else {
this._fail("Server disconnected" + msg);
switch (this._rfb_connection_state) {
case 'disconnect':
this._updateConnectionState('disconnected', 'VNC disconnected' + msg);
break;
case 'connect':
this._fail('Failed to connect to server' + msg);
break;
case 'failed':
case 'disconnected':
Util.Error("Received onclose while disconnected" + msg);
break;
default:
this._fail("Server disconnected" + msg);
break;
}
this._sock.off('close');
}.bind(this));
@ -239,7 +248,7 @@
var rmode = this._display.get_render_mode();
Util.Info("Using native WebSockets");
this._updateState('loaded', 'noVNC ready: native WebSockets, ' + rmode);
this._updateConnectionState('loaded', 'noVNC ready: native WebSockets, ' + rmode);
Util.Debug("<< RFB.constructor");
};
@ -257,12 +266,13 @@
return this._fail("Must set host and port");
}
this._updateState('connect');
this._rfb_init_state = '';
this._updateConnectionState('connect');
return true;
},
disconnect: function () {
this._updateState('disconnect', 'Disconnecting');
this._updateConnectionState('disconnect', 'Disconnecting');
this._sock.off('error');
this._sock.off('message');
this._sock.off('open');
@ -274,7 +284,7 @@
},
sendCtrlAltDel: function () {
if (this._rfb_state !== 'normal' || this._view_only) { return false; }
if (this._rfb_connection_state !== 'normal' || this._view_only) { return false; }
Util.Info("Sending Ctrl-Alt-Del");
RFB.messages.keyEvent(this._sock, KeyTable.XK_Control_L, 1);
@ -308,7 +318,7 @@
// Send a key press. If 'down' is not specified then send a down key
// followed by an up key.
sendKey: function (code, down) {
if (this._rfb_state !== "normal" || this._view_only) { return false; }
if (this._rfb_connection_state !== "normal" || this._view_only) { return false; }
if (typeof down !== 'undefined') {
Util.Info("Sending key code (" + (down ? "down" : "up") + "): " + code);
RFB.messages.keyEvent(this._sock, code, down ? 1 : 0);
@ -321,14 +331,14 @@
},
clipboardPasteFrom: function (text) {
if (this._rfb_state !== 'normal') { return; }
if (this._rfb_connection_state !== 'normal') { return; }
RFB.messages.clientCutText(this._sock, text);
},
// Requests a change of remote desktop size. This message is an extension
// and may only be sent if we have received an ExtendedDesktopSize message
requestDesktopSize: function (width, height) {
if (this._rfb_state !== "normal") { return; }
if (this._rfb_connection_state !== "normal") { return; }
if (this._supportsSetDesktopSize) {
RFB.messages.setDesktopSize(this._sock, width, height,
@ -420,25 +430,17 @@
},
/*
* Page states:
* Connection states:
* loaded - page load, equivalent to disconnected
* disconnected - idle state
* connect - starting to connect (to ProtocolVersion)
* connect - starting to connect
* normal - connected
* disconnect - starting to disconnect
* failed - abnormal disconnect
* fatal - failed to load page, or fatal error
*
* RFB protocol initialization states:
* ProtocolVersion
* Security
* Authentication
* SecurityResult
* ClientInitialization - not triggered by server message
* ServerInitialization (to normal)
*/
_updateState: function (state, statusMsg) {
var oldstate = this._rfb_state;
_updateConnectionState: function (state, statusMsg) {
var oldstate = this._rfb_connection_state;
if (state === oldstate) {
// Already here, ignore
@ -446,7 +448,7 @@
return;
}
this._rfb_state = state;
this._rfb_connection_state = state;
/*
* These are disconnected states. A previous connect may
@ -516,7 +518,7 @@
if (oldstate === 'failed' && state === 'disconnected') {
// do disconnect action, but stay in failed state and
// keep the previous status message
this._rfb_state = 'failed';
this._rfb_connection_state = 'failed';
this._onUpdateState(this, state, oldstate);
} else {
this._onUpdateState(this, state, oldstate, statusMsg);
@ -524,12 +526,12 @@
// Make sure we transition to disconnected
if (state === 'failed') {
this._updateState('disconnected');
this._updateConnectionState('disconnected');
}
},
_fail: function (msg) {
this._updateState('failed', msg);
this._updateConnectionState('failed', msg);
return false;
},
@ -566,7 +568,7 @@
return;
}
switch (this._rfb_state) {
switch (this._rfb_connection_state) {
case 'disconnected':
case 'failed':
Util.Error("Got data while disconnected");
@ -638,7 +640,7 @@
if (this._view_only) { return; } // View only, skip mouse events
if (this._rfb_state !== "normal") { return; }
if (this._rfb_connection_state !== "normal") { return; }
RFB.messages.pointerEvent(this._sock, this._display.absX(x), this._display.absY(y), this._mouse_buttonMask);
},
@ -665,7 +667,7 @@
if (this._view_only) { return; } // View only, skip mouse events
if (this._rfb_state !== "normal") { return; }
if (this._rfb_connection_state !== "normal") { return; }
RFB.messages.pointerEvent(this._sock, this._display.absX(x), this._display.absY(y), this._mouse_buttonMask);
},
@ -717,7 +719,9 @@
var cversion = "00" + parseInt(this._rfb_version, 10) +
".00" + ((this._rfb_version * 10) % 10);
this._sock.send_string("RFB " + cversion + "\n");
this._updateState('Security', 'Sent ProtocolVersion: ' + cversion);
Util.Debug('Sent ProtocolVersion: ' + cversion);
this._rfb_init_state = 'Security';
},
_negotiate_security: function () {
@ -761,7 +765,9 @@
this._rfb_auth_scheme = this._sock.rQshift32();
}
this._updateState('Authentication', 'Authenticating using scheme: ' + this._rfb_auth_scheme);
this._rfb_init_state = 'Authentication';
Util.Debug('Authenticating using scheme: ' + this._rfb_auth_scheme);
return this._init_msg(); // jump to authentication
},
@ -798,7 +804,7 @@
var challenge = Array.prototype.slice.call(this._sock.rQshiftBytes(16));
var response = RFB.genDES(this._rfb_password, challenge);
this._sock.send(response);
this._updateState("SecurityResult");
this._rfb_init_state = "SecurityResult";
return true;
},
@ -871,7 +877,7 @@
switch (authType) {
case 'STDVNOAUTH__': // no auth
this._updateState('SecurityResult');
this._rfb_init_state = 'SecurityResult';
return true;
case 'STDVVNCAUTH_': // VNC auth
this._rfb_auth_scheme = 2;
@ -895,10 +901,10 @@
case 1: // no auth
if (this._rfb_version >= 3.8) {
this._updateState('SecurityResult');
this._rfb_init_state = 'SecurityResult';
return true;
}
this._updateState('ClientInitialisation', "No auth required");
this._rfb_init_state = 'ClientInitialisation';
return this._init_msg();
case 22: // XVP auth
@ -919,7 +925,8 @@
if (this._sock.rQwait('VNC auth response ', 4)) { return false; }
switch (this._sock.rQshift32()) {
case 0: // OK
this._updateState('ClientInitialisation', 'Authentication OK');
this._rfb_init_state = 'ClientInitialisation';
Util.Debug('Authentication OK');
return this._init_msg();
case 1: // failed
if (this._rfb_version >= 3.8) {
@ -1047,15 +1054,23 @@
this._timing.pixels = 0;
if (this._encrypt) {
this._updateState('normal', 'Connected (encrypted) to: ' + this._fb_name);
this._updateConnectionState('normal', 'Connected (encrypted) to: ' + this._fb_name);
} else {
this._updateState('normal', 'Connected (unencrypted) to: ' + this._fb_name);
this._updateConnectionState('normal', 'Connected (unencrypted) to: ' + this._fb_name);
}
return true;
},
/* RFB protocol initialization states:
* ProtocolVersion
* Security
* Authentication
* SecurityResult
* ClientInitialization - not triggered by server message
* ServerInitialization
*/
_init_msg: function () {
switch (this._rfb_state) {
switch (this._rfb_init_state) {
case 'ProtocolVersion':
return this._negotiate_protocol_version();
@ -1070,14 +1085,15 @@
case 'ClientInitialisation':
this._sock.send([this._shared ? 1 : 0]); // ClientInitialisation
this._updateState('ServerInitialisation', "Authentication OK");
this._rfb_init_state = 'ServerInitialisation';
return true;
case 'ServerInitialisation':
return this._negotiate_server_init();
default:
return this._fail("Unknown state: " + this._rfb_state);
return this._fail("Unknown init state: " +
this._rfb_init_state);
}
},
@ -1255,7 +1271,7 @@
}
while (this._FBU.rects > 0) {
if (this._rfb_state !== "normal") { return false; }
if (this._rfb_connection_state !== "normal") { return false; }
if (this._sock.rQwait("FBU", this._FBU.bytes)) { return false; }
if (this._FBU.bytes === 0) {

View File

@ -65,12 +65,12 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
describe('#connect', function () {
beforeEach(function () { client._updateState = sinon.spy(); });
beforeEach(function () { client._updateConnectionState = sinon.spy(); });
it('should set the current state to "connect"', function () {
client.connect('host', 8675);
expect(client._updateState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('connect');
expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateConnectionState).to.have.been.calledWith('connect');
});
it('should fail if we are missing a host', function () {
@ -89,18 +89,18 @@ describe('Remote Frame Buffer Protocol Client', function() {
sinon.spy(client, '_fail');
client.connect('abc');
expect(client._fail).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('failed');
expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateConnectionState).to.have.been.calledWith('failed');
});
});
describe('#disconnect', function () {
beforeEach(function () { client._updateState = sinon.spy(); });
beforeEach(function () { client._updateConnectionState = sinon.spy(); });
it('should set the current state to "disconnect"', function () {
client.disconnect();
expect(client._updateState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('disconnect');
expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateConnectionState).to.have.been.calledWith('disconnect');
});
it('should unregister error event handler', function () {
@ -145,7 +145,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = "normal";
client._rfb_connection_state = "normal";
client._view_only = false;
});
@ -163,7 +163,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should not send the keys if we are not in a normal state', function () {
client._rfb_state = "broken";
client._rfb_connection_state = "broken";
client.sendCtrlAltDel();
expect(client._sock.flush).to.not.have.been.called;
});
@ -181,7 +181,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = "normal";
client._rfb_connection_state = "normal";
client._view_only = false;
});
@ -201,7 +201,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should not send the key if we are not in a normal state', function () {
client._rfb_state = "broken";
client._rfb_connection_state = "broken";
client.sendKey(123);
expect(client._sock.flush).to.not.have.been.called;
});
@ -219,7 +219,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = "normal";
client._rfb_connection_state = "normal";
client._view_only = false;
});
@ -231,7 +231,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should not send the text if we are not in a normal state', function () {
client._rfb_state = "broken";
client._rfb_connection_state = "broken";
client.clipboardPasteFrom('abc');
expect(client._sock.flush).to.not.have.been.called;
});
@ -243,7 +243,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = "normal";
client._rfb_connection_state = "normal";
client._view_only = false;
client._supportsSetDesktopSize = true;
});
@ -273,7 +273,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should not send the request if we are not in a normal state', function () {
client._rfb_state = "broken";
client._rfb_connection_state = "broken";
client.requestDesktopSize(1,2);
expect(client._sock.flush).to.not.have.been.called;
});
@ -285,7 +285,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = "normal";
client._rfb_connection_state = "normal";
client._view_only = false;
client._rfb_xvp_ver = 1;
});
@ -318,7 +318,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
describe('Misc Internals', function () {
describe('#_updateState', function () {
describe('#_updateConnectionState', function () {
var client;
beforeEach(function () {
this.clock = sinon.useFakeTimers();
@ -332,7 +332,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should clear the disconnect timer if the state is not disconnect', function () {
var spy = sinon.spy();
client._disconnTimer = setTimeout(spy, 50);
client._updateState('normal');
client._updateConnectionState('normal');
this.clock.tick(51);
expect(spy).to.not.have.been.called;
expect(client._disconnTimer).to.be.null;
@ -362,17 +362,17 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
describe('Page States', function () {
describe('Connection States', function () {
describe('loaded', function () {
var client;
beforeEach(function () {
client = make_rfb();
client._rfb_state = 'disconnected';
client._rfb_connection_state = 'disconnected';
});
it('should close any open WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateState('loaded');
client._updateConnectionState('loaded');
expect(client._sock.close).to.have.been.calledOnce;
});
});
@ -383,7 +383,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should close any open WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateState('disconnected');
client._updateConnectionState('disconnected');
expect(client._sock.close).to.have.been.calledOnce;
});
});
@ -394,27 +394,27 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should reset the variable states', function () {
sinon.spy(client, '_init_vars');
client._updateState('connect');
client._updateConnectionState('connect');
expect(client._init_vars).to.have.been.calledOnce;
});
it('should actually connect to the websocket', function () {
sinon.spy(client._sock, 'open');
client._updateState('connect');
client._updateConnectionState('connect');
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._updateState('connect');
client._updateConnectionState('connect');
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._updateState('connect');
client._updateConnectionState('connect');
expect(client._sock.open.args[0][0]).to.contain('wss://');
});
@ -424,13 +424,13 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._rfb_host = 'HOST';
client._rfb_port = 8675;
client._rfb_path = 'PATH';
client._updateState('connect');
client._updateConnectionState('connect');
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._updateState('connect');
client._updateConnectionState('connect');
expect(client._sock.close).to.have.been.calledOnce;
});
});
@ -449,22 +449,22 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should fail if we do not call Websock.onclose within the disconnection timeout', function () {
client._sock._websocket.close = function () {}; // explicitly don't call onclose
client._updateState('disconnect');
client._updateConnectionState('disconnect');
this.clock.tick(client.get_disconnectTimeout() * 1000);
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
it('should not fail if Websock.onclose gets called within the disconnection timeout', function () {
client._updateState('disconnect');
client._updateConnectionState('disconnect');
this.clock.tick(client.get_disconnectTimeout() * 500);
client._sock._websocket.close();
this.clock.tick(client.get_disconnectTimeout() * 500 + 1);
expect(client._rfb_state).to.equal('disconnected');
expect(client._rfb_connection_state).to.equal('disconnected');
});
it('should close the WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateState('disconnect');
client._updateConnectionState('disconnect');
expect(client._sock.close).to.have.been.calledTwice; // once on loaded, once on disconnect
});
});
@ -483,15 +483,15 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should close the WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateState('failed');
client._updateConnectionState('failed');
expect(client._sock.close).to.have.been.called;
});
it('should transition to disconnected but stay in failed state', function () {
client.set_onUpdateState(sinon.spy());
client._updateState('failed');
client._updateConnectionState('failed');
this.clock.tick(50);
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
var onUpdateState = client.get_onUpdateState();
expect(onUpdateState).to.have.been.called;
@ -508,12 +508,12 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should close any open WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateState('fatal');
client._updateConnectionState('fatal');
expect(client._sock.close).to.have.been.calledOnce;
});
});
// NB(directxman12): Normal does *nothing* in updateState
// NB(directxman12): Normal does *nothing* in updateConnectionState
});
describe('Protocol Initialization States', function () {
@ -595,7 +595,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should fail on an invalid version', function () {
send_ver('002.000', client);
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
});
@ -633,7 +633,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should transition to the Security state on successful negotiation', function () {
send_ver('003.008', client);
expect(client._rfb_state).to.equal('Security');
expect(client._rfb_init_state).to.equal('Security');
});
});
@ -644,7 +644,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'Security';
client._rfb_init_state = 'Security';
});
it('should simply receive the auth scheme when for versions < 3.7', function () {
@ -668,7 +668,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._rfb_version = 3.7;
var auth_schemes = [1, 32];
client._sock._websocket._receive_data(auth_schemes);
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
it('should fail with the appropriate message if no types are sent for versions >= 3.7', function () {
@ -686,7 +686,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
var auth_schemes = [1, 1];
client._negotiate_authentication = sinon.spy();
client._sock._websocket._receive_data(auth_schemes);
expect(client._rfb_state).to.equal('Authentication');
expect(client._rfb_init_state).to.equal('Authentication');
expect(client._negotiate_authentication).to.have.been.calledOnce;
});
});
@ -698,7 +698,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'Security';
client._rfb_init_state = 'Security';
});
function send_security(type, cl) {
@ -717,28 +717,26 @@ describe('Remote Frame Buffer Protocol Client', function() {
sinon.spy(client, '_fail');
client._sock._websocket._receive_data(new Uint8Array(data));
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
expect(client._fail).to.have.been.calledWith('Auth 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_state).to.equal('SecurityResult');
expect(client._rfb_init_state).to.equal('SecurityResult');
});
it('should transition straight to ClientInitialisation on "no auth" for versions < 3.8', function () {
it('should transition straight to ServerInitialisation on "no auth" for versions < 3.8', function () {
client._rfb_version = 3.7;
sinon.spy(client, '_updateState');
send_security(1, client);
expect(client._updateState).to.have.been.calledWith('ClientInitialisation');
expect(client._rfb_state).to.equal('ServerInitialisation');
expect(client._rfb_init_state).to.equal('ServerInitialisation');
});
it('should fail on an unknown auth scheme', function () {
client._rfb_version = 3.8;
send_security(57, client);
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
describe('VNC Authentication (type 2) Handler', function () {
@ -748,7 +746,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'Security';
client._rfb_init_state = 'Security';
client._rfb_version = 3.8;
});
@ -782,7 +780,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
for (var i = 0; i < 16; i++) { challenge[i] = i; }
client._sock._websocket._receive_data(new Uint8Array(challenge));
expect(client._rfb_state).to.equal('SecurityResult');
expect(client._rfb_init_state).to.equal('SecurityResult');
});
});
@ -793,7 +791,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'Security';
client._rfb_init_state = 'Security';
client._rfb_version = 3.8;
});
@ -847,7 +845,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'Security';
client._rfb_init_state = 'Security';
client._rfb_version = 3.8;
send_security(16, client);
client._sock._websocket._get_sent_data(); // skip the security reply
@ -879,7 +877,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should fail if no supported tunnels are listed', function () {
send_num_str_pairs([[123, 'OTHR', 'SOMETHNG']], client);
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
it('should choose the notunnel tunnel type', function () {
@ -892,7 +890,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
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_state).to.equal('SecurityResult');
expect(client._rfb_init_state).to.equal('SecurityResult');
});
/*it('should attempt to use VNC auth over no auth when possible', function () {
@ -908,7 +906,7 @@ describe('Remote Frame Buffer Protocol Client', 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_state).to.equal('SecurityResult');
expect(client._rfb_init_state).to.equal('SecurityResult');
});
it('should accept VNC authentication and transition to that', function () {
@ -923,7 +921,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should fail if there are no supported auth types', function () {
client._rfb_tightvnc = true;
send_num_str_pairs([[23, 'stdv', 'badval__']], client);
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
});
});
@ -935,14 +933,13 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'SecurityResult';
client._rfb_init_state = 'SecurityResult';
});
it('should fall through to ClientInitialisation on a response code of 0', function () {
client._updateState = sinon.spy();
it('should fall through to ServerInitialisation on a response code of 0', function () {
client._updateConnectionState = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
expect(client._updateState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('ClientInitialisation');
expect(client._rfb_init_state).to.equal('ServerInitialisation');
});
it('should fail on an error code of 1 with the given message for versions >= 3.8', function () {
@ -950,14 +947,14 @@ 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._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
expect(client._fail).to.have.been.calledWith('whoops');
});
it('should fail on an error code of 1 with a standard message for version < 3.8', function () {
client._rfb_version = 3.7;
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 1]));
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
});
@ -968,12 +965,12 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'SecurityResult';
client._rfb_init_state = 'SecurityResult';
});
it('should transition to the ServerInitialisation state', function () {
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
expect(client._rfb_state).to.equal('ServerInitialisation');
expect(client._rfb_init_state).to.equal('ServerInitialisation');
});
it('should send 1 if we are in shared mode', function () {
@ -996,7 +993,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'ServerInitialisation';
client._rfb_init_state = 'ServerInitialisation';
});
function send_server_init(opts, client) {
@ -1072,7 +1069,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
}
client._sock._websocket._receive_data(tight_data);
expect(client._rfb_state).to.equal('normal');
expect(client._rfb_connection_state).to.equal('normal');
});
it('should set the true color mode on the display to the configuration variable', function () {
@ -1136,7 +1133,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should transition to the "normal" state', function () {
send_server_init({}, client);
expect(client._rfb_state).to.equal('normal');
expect(client._rfb_connection_state).to.equal('normal');
});
});
});
@ -1148,7 +1145,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
client._fb_name = 'some device';
client._fb_width = 640;
client._fb_height = 20;
@ -1161,7 +1158,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
client._fb_name = 'some device';
client._fb_width = 640;
client._fb_height = 20;
@ -1322,7 +1319,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client.set_onFBUReceive(sinon.spy());
var rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 234 };
send_fbu_msg([rect_info], [[]], client);
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
it('should be able to pause and resume receiving rects if not enought data', function () {
@ -1351,7 +1348,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
client._fb_name = 'some device';
// a really small frame
client._fb_width = 4;
@ -1428,7 +1425,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
client._fb_name = 'some device';
// a really small frame
client._fb_width = 4;
@ -1569,7 +1566,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
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_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
});
@ -1604,7 +1601,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
client._fb_name = 'some device';
client._supportsSetDesktopSize = false;
// a really small frame
@ -1746,7 +1743,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
client._fb_name = 'some device';
client._fb_width = 27;
client._fb_height = 32;
@ -1770,7 +1767,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should fail on unknown XVP message types', function () {
client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 237]));
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
});
@ -1863,7 +1860,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should fail on an unknown message type', function () {
client._sock._websocket._receive_data(new Uint8Array([87]));
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
});
@ -1876,7 +1873,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
});
it('should not send button messages in view-only mode', function () {
@ -2017,7 +2014,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
// message events
it ('should do nothing if we receive an empty message and have nothing in the queue', function () {
client.connect('host', 8675);
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
client._normal_msg = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([]));
expect(client._normal_msg).to.not.have.been.called;
@ -2025,7 +2022,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should handle a message in the normal state as a normal message', function () {
client.connect('host', 8675);
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
client._normal_msg = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
expect(client._normal_msg).to.have.been.calledOnce;
@ -2033,7 +2030,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should handle a message in any non-disconnected/failed state like an init message', function () {
client.connect('host', 8675);
client._rfb_state = 'ProtocolVersion';
client._rfb_init_state = 'ProtocolVersion';
client._init_msg = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
expect(client._init_msg).to.have.been.calledOnce;
@ -2042,7 +2039,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should split up the handling of muplitle normal messages across 10ms intervals', function () {
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
client.set_onBell(sinon.spy());
client._sock._websocket._receive_data(new Uint8Array([0x02, 0x02]));
expect(client.get_onBell()).to.have.been.calledOnce;
@ -2054,35 +2051,35 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should update the state to ProtocolVersion on open (if the state is "connect")', function () {
client.connect('host', 8675);
client._sock._websocket._open();
expect(client._rfb_state).to.equal('ProtocolVersion');
expect(client._rfb_init_state).to.equal('ProtocolVersion');
});
it('should fail if we are not currently ready to connect and we get an "open" event', function () {
client.connect('host', 8675);
client._rfb_state = 'some_other_state';
client._rfb_connection_state = 'some_other_state';
client._sock._websocket._open();
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
// close events
it('should transition to "disconnected" from "disconnect" on a close event', function () {
client.connect('host', 8675);
client._rfb_state = 'disconnect';
client._rfb_connection_state = 'disconnect';
client._sock._websocket.close();
expect(client._rfb_state).to.equal('disconnected');
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 () {
client.connect('host', 8675);
client._rfb_state = 'normal';
client._rfb_connection_state = 'normal';
client._sock._websocket.close();
expect(client._rfb_state).to.equal('failed');
expect(client._rfb_connection_state).to.equal('failed');
});
it('should unregister close event handler', function () {
sinon.spy(client._sock, 'off');
client.connect('host', 8675);
client._rfb_state = 'disconnect';
client._rfb_connection_state = 'disconnect';
client._sock._websocket.close();
expect(client._sock.off).to.have.been.calledWith('close');
});