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

View File

@ -65,12 +65,12 @@ describe('Remote Frame Buffer Protocol Client', function() {
}); });
describe('#connect', 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 () { it('should set the current state to "connect"', function () {
client.connect('host', 8675); client.connect('host', 8675);
expect(client._updateState).to.have.been.calledOnce; expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('connect'); expect(client._updateConnectionState).to.have.been.calledWith('connect');
}); });
it('should fail if we are missing a host', function () { 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'); sinon.spy(client, '_fail');
client.connect('abc'); client.connect('abc');
expect(client._fail).to.have.been.calledOnce; expect(client._fail).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledOnce; expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('failed'); expect(client._updateConnectionState).to.have.been.calledWith('failed');
}); });
}); });
describe('#disconnect', function () { describe('#disconnect', function () {
beforeEach(function () { client._updateState = sinon.spy(); }); beforeEach(function () { client._updateConnectionState = sinon.spy(); });
it('should set the current state to "disconnect"', function () { it('should set the current state to "disconnect"', function () {
client.disconnect(); client.disconnect();
expect(client._updateState).to.have.been.calledOnce; expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('disconnect'); expect(client._updateConnectionState).to.have.been.calledWith('disconnect');
}); });
it('should unregister error event handler', function () { 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.open('ws://', 'binary');
client._sock._websocket._open(); client._sock._websocket._open();
sinon.spy(client._sock, 'flush'); sinon.spy(client._sock, 'flush');
client._rfb_state = "normal"; client._rfb_connection_state = "normal";
client._view_only = false; 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 () { 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(); client.sendCtrlAltDel();
expect(client._sock.flush).to.not.have.been.called; 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.open('ws://', 'binary');
client._sock._websocket._open(); client._sock._websocket._open();
sinon.spy(client._sock, 'flush'); sinon.spy(client._sock, 'flush');
client._rfb_state = "normal"; client._rfb_connection_state = "normal";
client._view_only = false; 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 () { 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); client.sendKey(123);
expect(client._sock.flush).to.not.have.been.called; 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.open('ws://', 'binary');
client._sock._websocket._open(); client._sock._websocket._open();
sinon.spy(client._sock, 'flush'); sinon.spy(client._sock, 'flush');
client._rfb_state = "normal"; client._rfb_connection_state = "normal";
client._view_only = false; 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 () { 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'); client.clipboardPasteFrom('abc');
expect(client._sock.flush).to.not.have.been.called; 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.open('ws://', 'binary');
client._sock._websocket._open(); client._sock._websocket._open();
sinon.spy(client._sock, 'flush'); sinon.spy(client._sock, 'flush');
client._rfb_state = "normal"; client._rfb_connection_state = "normal";
client._view_only = false; client._view_only = false;
client._supportsSetDesktopSize = true; 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 () { 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); client.requestDesktopSize(1,2);
expect(client._sock.flush).to.not.have.been.called; 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.open('ws://', 'binary');
client._sock._websocket._open(); client._sock._websocket._open();
sinon.spy(client._sock, 'flush'); sinon.spy(client._sock, 'flush');
client._rfb_state = "normal"; client._rfb_connection_state = "normal";
client._view_only = false; client._view_only = false;
client._rfb_xvp_ver = 1; client._rfb_xvp_ver = 1;
}); });
@ -318,7 +318,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
}); });
describe('Misc Internals', function () { describe('Misc Internals', function () {
describe('#_updateState', function () { describe('#_updateConnectionState', function () {
var client; var client;
beforeEach(function () { beforeEach(function () {
this.clock = sinon.useFakeTimers(); 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 () { it('should clear the disconnect timer if the state is not disconnect', function () {
var spy = sinon.spy(); var spy = sinon.spy();
client._disconnTimer = setTimeout(spy, 50); client._disconnTimer = setTimeout(spy, 50);
client._updateState('normal'); client._updateConnectionState('normal');
this.clock.tick(51); this.clock.tick(51);
expect(spy).to.not.have.been.called; expect(spy).to.not.have.been.called;
expect(client._disconnTimer).to.be.null; 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 () { describe('loaded', function () {
var client; var client;
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client._rfb_state = 'disconnected'; client._rfb_connection_state = 'disconnected';
}); });
it('should close any open WebSocket connection', function () { it('should close any open WebSocket connection', function () {
sinon.spy(client._sock, 'close'); sinon.spy(client._sock, 'close');
client._updateState('loaded'); client._updateConnectionState('loaded');
expect(client._sock.close).to.have.been.calledOnce; 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 () { it('should close any open WebSocket connection', function () {
sinon.spy(client._sock, 'close'); sinon.spy(client._sock, 'close');
client._updateState('disconnected'); client._updateConnectionState('disconnected');
expect(client._sock.close).to.have.been.calledOnce; 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 () { it('should reset the variable states', function () {
sinon.spy(client, '_init_vars'); sinon.spy(client, '_init_vars');
client._updateState('connect'); client._updateConnectionState('connect');
expect(client._init_vars).to.have.been.calledOnce; expect(client._init_vars).to.have.been.calledOnce;
}); });
it('should actually connect to the websocket', function () { it('should actually connect to the websocket', function () {
sinon.spy(client._sock, 'open'); sinon.spy(client._sock, 'open');
client._updateState('connect'); client._updateConnectionState('connect');
expect(client._sock.open).to.have.been.calledOnce; expect(client._sock.open).to.have.been.calledOnce;
}); });
it('should use wss:// to connect if encryption is enabled', function () { it('should use wss:// to connect if encryption is enabled', function () {
sinon.spy(client._sock, 'open'); sinon.spy(client._sock, 'open');
client.set_encrypt(true); client.set_encrypt(true);
client._updateState('connect'); client._updateConnectionState('connect');
expect(client._sock.open.args[0][0]).to.contain('wss://'); expect(client._sock.open.args[0][0]).to.contain('wss://');
}); });
it('should use ws:// to connect if encryption is not enabled', function () { it('should use ws:// to connect if encryption is not enabled', function () {
sinon.spy(client._sock, 'open'); sinon.spy(client._sock, 'open');
client.set_encrypt(true); client.set_encrypt(true);
client._updateState('connect'); client._updateConnectionState('connect');
expect(client._sock.open.args[0][0]).to.contain('wss://'); 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_host = 'HOST';
client._rfb_port = 8675; client._rfb_port = 8675;
client._rfb_path = 'PATH'; client._rfb_path = 'PATH';
client._updateState('connect'); client._updateConnectionState('connect');
expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH'); 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 () { it('should attempt to close the websocket before we open an new one', function () {
sinon.spy(client._sock, 'close'); sinon.spy(client._sock, 'close');
client._updateState('connect'); client._updateConnectionState('connect');
expect(client._sock.close).to.have.been.calledOnce; 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 () { 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._sock._websocket.close = function () {}; // explicitly don't call onclose
client._updateState('disconnect'); client._updateConnectionState('disconnect');
this.clock.tick(client.get_disconnectTimeout() * 1000); 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 () { 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); this.clock.tick(client.get_disconnectTimeout() * 500);
client._sock._websocket.close(); client._sock._websocket.close();
this.clock.tick(client.get_disconnectTimeout() * 500 + 1); 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 () { it('should close the WebSocket connection', function () {
sinon.spy(client._sock, 'close'); 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 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 () { it('should close the WebSocket connection', function () {
sinon.spy(client._sock, 'close'); sinon.spy(client._sock, 'close');
client._updateState('failed'); client._updateConnectionState('failed');
expect(client._sock.close).to.have.been.called; expect(client._sock.close).to.have.been.called;
}); });
it('should transition to disconnected but stay in failed state', function () { it('should transition to disconnected but stay in failed state', function () {
client.set_onUpdateState(sinon.spy()); client.set_onUpdateState(sinon.spy());
client._updateState('failed'); client._updateConnectionState('failed');
this.clock.tick(50); this.clock.tick(50);
expect(client._rfb_state).to.equal('failed'); expect(client._rfb_connection_state).to.equal('failed');
var onUpdateState = client.get_onUpdateState(); var onUpdateState = client.get_onUpdateState();
expect(onUpdateState).to.have.been.called; 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 () { it('should close any open WebSocket connection', function () {
sinon.spy(client._sock, 'close'); sinon.spy(client._sock, 'close');
client._updateState('fatal'); client._updateConnectionState('fatal');
expect(client._sock.close).to.have.been.calledOnce; 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 () { describe('Protocol Initialization States', function () {
@ -595,7 +595,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should fail on an invalid version', function () { it('should fail on an invalid version', function () {
send_ver('002.000', client); 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 () { it('should transition to the Security state on successful negotiation', function () {
send_ver('003.008', client); 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 = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); 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 () { 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; client._rfb_version = 3.7;
var auth_schemes = [1, 32]; var auth_schemes = [1, 32];
client._sock._websocket._receive_data(auth_schemes); 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 () { 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]; var auth_schemes = [1, 1];
client._negotiate_authentication = sinon.spy(); client._negotiate_authentication = sinon.spy();
client._sock._websocket._receive_data(auth_schemes); 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; expect(client._negotiate_authentication).to.have.been.calledOnce;
}); });
}); });
@ -698,7 +698,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'Security'; client._rfb_init_state = 'Security';
}); });
function send_security(type, cl) { function send_security(type, cl) {
@ -717,28 +717,26 @@ describe('Remote Frame Buffer Protocol Client', function() {
sinon.spy(client, '_fail'); sinon.spy(client, '_fail');
client._sock._websocket._receive_data(new Uint8Array(data)); 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'); 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 () { it('should transition straight to SecurityResult on "no auth" (1) for versions >= 3.8', function () {
client._rfb_version = 3.8; client._rfb_version = 3.8;
send_security(1, client); 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; client._rfb_version = 3.7;
sinon.spy(client, '_updateState');
send_security(1, client); send_security(1, client);
expect(client._updateState).to.have.been.calledWith('ClientInitialisation'); expect(client._rfb_init_state).to.equal('ServerInitialisation');
expect(client._rfb_state).to.equal('ServerInitialisation');
}); });
it('should fail on an unknown auth scheme', function () { it('should fail on an unknown auth scheme', function () {
client._rfb_version = 3.8; client._rfb_version = 3.8;
send_security(57, client); 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 () { describe('VNC Authentication (type 2) Handler', function () {
@ -748,7 +746,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'Security'; client._rfb_init_state = 'Security';
client._rfb_version = 3.8; 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; } for (var i = 0; i < 16; i++) { challenge[i] = i; }
client._sock._websocket._receive_data(new Uint8Array(challenge)); 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 = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'Security'; client._rfb_init_state = 'Security';
client._rfb_version = 3.8; client._rfb_version = 3.8;
}); });
@ -847,7 +845,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'Security'; client._rfb_init_state = 'Security';
client._rfb_version = 3.8; client._rfb_version = 3.8;
send_security(16, client); send_security(16, client);
client._sock._websocket._get_sent_data(); // skip the security reply 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 () { it('should fail if no supported tunnels are listed', function () {
send_num_str_pairs([[123, 'OTHR', 'SOMETHNG']], client); 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 () { 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 client._sock._websocket._get_sent_data(); // skip the tunnel choice here
send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client); send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1])); 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 () { /*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; client._rfb_tightvnc = true;
send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client); send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1])); 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 () { 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 () { it('should fail if there are no supported auth types', function () {
client._rfb_tightvnc = true; client._rfb_tightvnc = true;
send_num_str_pairs([[23, 'stdv', 'badval__']], client); 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 = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); 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 () { it('should fall through to ServerInitialisation on a response code of 0', function () {
client._updateState = sinon.spy(); client._updateConnectionState = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0])); client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
expect(client._updateState).to.have.been.calledOnce; expect(client._rfb_init_state).to.equal('ServerInitialisation');
expect(client._updateState).to.have.been.calledWith('ClientInitialisation');
}); });
it('should fail on an error code of 1 with the given message for versions >= 3.8', function () { 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'); sinon.spy(client, '_fail');
var failure_data = [0, 0, 0, 1, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115]; 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)); 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'); 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 () { it('should fail on an error code of 1 with a standard message for version < 3.8', function () {
client._rfb_version = 3.7; client._rfb_version = 3.7;
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 1])); 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 = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'SecurityResult'; client._rfb_init_state = 'SecurityResult';
}); });
it('should transition to the ServerInitialisation state', function () { it('should transition to the ServerInitialisation state', function () {
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0])); 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 () { 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 = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'ServerInitialisation'; client._rfb_init_state = 'ServerInitialisation';
}); });
function send_server_init(opts, client) { function send_server_init(opts, client) {
@ -1072,7 +1069,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
} }
client._sock._websocket._receive_data(tight_data); 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 () { 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 () { it('should transition to the "normal" state', function () {
send_server_init({}, client); 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 = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'normal'; client._rfb_connection_state = 'normal';
client._fb_name = 'some device'; client._fb_name = 'some device';
client._fb_width = 640; client._fb_width = 640;
client._fb_height = 20; client._fb_height = 20;
@ -1161,7 +1158,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'normal'; client._rfb_connection_state = 'normal';
client._fb_name = 'some device'; client._fb_name = 'some device';
client._fb_width = 640; client._fb_width = 640;
client._fb_height = 20; client._fb_height = 20;
@ -1322,7 +1319,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client.set_onFBUReceive(sinon.spy()); client.set_onFBUReceive(sinon.spy());
var rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 234 }; var rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 234 };
send_fbu_msg([rect_info], [[]], client); 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 () { 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 = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'normal'; client._rfb_connection_state = 'normal';
client._fb_name = 'some device'; client._fb_name = 'some device';
// a really small frame // a really small frame
client._fb_width = 4; client._fb_width = 4;
@ -1428,7 +1425,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'normal'; client._rfb_connection_state = 'normal';
client._fb_name = 'some device'; client._fb_name = 'some device';
// a really small frame // a really small frame
client._fb_width = 4; 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 info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
var rects = [[45]]; // an invalid subencoding var rects = [[45]]; // an invalid subencoding
send_fbu_msg(info, rects, client); 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 = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'normal'; client._rfb_connection_state = 'normal';
client._fb_name = 'some device'; client._fb_name = 'some device';
client._supportsSetDesktopSize = false; client._supportsSetDesktopSize = false;
// a really small frame // a really small frame
@ -1746,7 +1743,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'normal'; client._rfb_connection_state = 'normal';
client._fb_name = 'some device'; client._fb_name = 'some device';
client._fb_width = 27; client._fb_width = 27;
client._fb_height = 32; client._fb_height = 32;
@ -1770,7 +1767,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should fail on unknown XVP message types', function () { it('should fail on unknown XVP message types', function () {
client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 237])); 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 () { it('should fail on an unknown message type', function () {
client._sock._websocket._receive_data(new Uint8Array([87])); 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.open('ws://', 'binary');
client._sock._websocket._open(); client._sock._websocket._open();
sinon.spy(client._sock, 'flush'); 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 () { it('should not send button messages in view-only mode', function () {
@ -2017,7 +2014,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
// message events // message events
it ('should do nothing if we receive an empty message and have nothing in the queue', function () { it ('should do nothing if we receive an empty message and have nothing in the queue', function () {
client.connect('host', 8675); client.connect('host', 8675);
client._rfb_state = 'normal'; client._rfb_connection_state = 'normal';
client._normal_msg = sinon.spy(); client._normal_msg = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([])); client._sock._websocket._receive_data(new Uint8Array([]));
expect(client._normal_msg).to.not.have.been.called; 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 () { it('should handle a message in the normal state as a normal message', function () {
client.connect('host', 8675); client.connect('host', 8675);
client._rfb_state = 'normal'; client._rfb_connection_state = 'normal';
client._normal_msg = sinon.spy(); client._normal_msg = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([1, 2, 3])); client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
expect(client._normal_msg).to.have.been.calledOnce; 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 () { it('should handle a message in any non-disconnected/failed state like an init message', function () {
client.connect('host', 8675); client.connect('host', 8675);
client._rfb_state = 'ProtocolVersion'; client._rfb_init_state = 'ProtocolVersion';
client._init_msg = sinon.spy(); client._init_msg = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([1, 2, 3])); client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
expect(client._init_msg).to.have.been.calledOnce; 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 () { it('should split up the handling of muplitle normal messages across 10ms intervals', function () {
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_state = 'normal'; client._rfb_connection_state = 'normal';
client.set_onBell(sinon.spy()); client.set_onBell(sinon.spy());
client._sock._websocket._receive_data(new Uint8Array([0x02, 0x02])); client._sock._websocket._receive_data(new Uint8Array([0x02, 0x02]));
expect(client.get_onBell()).to.have.been.calledOnce; 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 () { it('should update the state to ProtocolVersion on open (if the state is "connect")', function () {
client.connect('host', 8675); client.connect('host', 8675);
client._sock._websocket._open(); 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 () { it('should fail if we are not currently ready to connect and we get an "open" event', function () {
client.connect('host', 8675); client.connect('host', 8675);
client._rfb_state = 'some_other_state'; client._rfb_connection_state = 'some_other_state';
client._sock._websocket._open(); client._sock._websocket._open();
expect(client._rfb_state).to.equal('failed'); expect(client._rfb_connection_state).to.equal('failed');
}); });
// close events // close events
it('should transition to "disconnected" from "disconnect" on a close event', function () { it('should transition to "disconnected" from "disconnect" on a close event', function () {
client.connect('host', 8675); client.connect('host', 8675);
client._rfb_state = 'disconnect'; client._rfb_connection_state = 'disconnect';
client._sock._websocket.close(); 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 () { it('should transition to failed if we get a close event from any non-"disconnection" state', function () {
client.connect('host', 8675); client.connect('host', 8675);
client._rfb_state = 'normal'; client._rfb_connection_state = 'normal';
client._sock._websocket.close(); 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 () { it('should unregister close event handler', function () {
sinon.spy(client._sock, 'off'); sinon.spy(client._sock, 'off');
client.connect('host', 8675); client.connect('host', 8675);
client._rfb_state = 'disconnect'; client._rfb_connection_state = 'disconnect';
client._sock._websocket.close(); client._sock._websocket.close();
expect(client._sock.off).to.have.been.calledWith('close'); expect(client._sock.off).to.have.been.calledWith('close');
}); });