import { encodings } from '../core/encodings.js';
import { toUnsigned32bit } from '../core/util/int.js';
import { encodeUTF8 } from '../core/util/strings.js';
+import KeyTable from '../core/input/keysym.js';
import FakeWebSocket from './fake.websocket.js';
-/* UIEvent constructor polyfill for IE */
-(() => {
- if (typeof window.UIEvent === "function") return;
-
- function UIEvent( event, params ) {
- params = params || { bubbles: false, cancelable: false, view: window, detail: undefined };
- const evt = document.createEvent( 'UIEvent' );
- evt.initUIEvent( event, params.bubbles, params.cancelable, params.view, params.detail );
- return evt;
- }
-
- UIEvent.prototype = window.UIEvent.prototype;
-
- window.UIEvent = UIEvent;
-})();
-
function push8(arr, num) {
"use strict";
arr.push(num & 0xFF);
strm.output = new Uint8Array(chunkSize);
deflateInit(strm, 5);
+ /* eslint-disable camelcase */
strm.input = unCompData;
strm.avail_in = strm.input.length;
strm.next_in = 0;
strm.next_out = 0;
strm.avail_out = chunkSize;
+ /* eslint-enable camelcase */
deflate(strm, 3);
after(FakeWebSocket.restore);
before(function () {
- this.clock = clock = sinon.useFakeTimers();
+ this.clock = clock = sinon.useFakeTimers(Date.now());
// sinon doesn't support this yet
raf = window.requestAnimationFrame;
window.requestAnimationFrame = setTimeout;
const _sQ = new Uint8Array(sock._sQbufferSize);
const rQ = new Uint8Array(sock._rQbufferSize);
- Websock.prototype._old_allocate_buffers = Websock.prototype._allocate_buffers;
- Websock.prototype._allocate_buffers = function () {
+ Websock.prototype._oldAllocateBuffers = Websock.prototype._allocateBuffers;
+ Websock.prototype._allocateBuffers = function () {
this._sQ = _sQ;
this._rQ = rQ;
};
+ // Avoiding printing the entire Websock buffer on errors
+ Websock.prototype.toString = function () { return "[object Websock]"; };
});
after(function () {
- Websock.prototype._allocate_buffers = Websock.prototype._old_allocate_buffers;
+ delete Websock.prototype.toString;
this.clock.restore();
window.requestAnimationFrame = raf;
});
container = null;
});
- function make_rfb(url, options) {
+ function makeRFB(url, options) {
url = url || 'wss://host:8675';
const rfb = new RFB(container, url, options);
clock.tick();
rfb._sock._websocket._open();
- rfb._rfb_connection_state = 'connected';
+ rfb._rfbConnectionState = 'connected';
sinon.spy(rfb, "_disconnect");
rfbs.push(rfb);
return rfb;
}
describe('Connecting/Disconnecting', function () {
- describe('#RFB', function () {
- it('should set the current state to "connecting"', function () {
- const client = new RFB(document.createElement('div'), 'wss://host:8675');
- client._rfb_connection_state = '';
- this.clock.tick();
- expect(client._rfb_connection_state).to.equal('connecting');
+ describe('#RFB (constructor)', function () {
+ let open, attach;
+ beforeEach(function () {
+ open = sinon.spy(Websock.prototype, 'open');
+ attach = sinon.spy(Websock.prototype, 'attach');
+ });
+ afterEach(function () {
+ open.restore();
+ attach.restore();
});
it('should actually connect to the websocket', function () {
- const client = new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
- sinon.spy(client._sock, 'open');
- this.clock.tick();
- expect(client._sock.open).to.have.been.calledOnce;
- expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH');
+ new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
+ expect(open).to.have.been.calledOnceWithExactly('ws://HOST:8675/PATH', []);
+ });
+
+ it('should pass on connection problems', function () {
+ open.restore();
+ open = sinon.stub(Websock.prototype, 'open');
+ open.throws(new Error('Failure'));
+ expect(() => new RFB(document.createElement('div'), 'ws://HOST:8675/PATH')).to.throw('Failure');
+ });
+
+ it('should handle WebSocket/RTCDataChannel objects', function () {
+ let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
+ new RFB(document.createElement('div'), sock);
+ expect(open).to.not.have.been.called;
+ expect(attach).to.have.been.calledOnceWithExactly(sock);
+ });
+
+ it('should handle already open WebSocket/RTCDataChannel objects', function () {
+ let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
+ sock._open();
+ const client = new RFB(document.createElement('div'), sock);
+ let callback = sinon.spy();
+ client.addEventListener('disconnect', callback);
+ expect(open).to.not.have.been.called;
+ expect(attach).to.have.been.calledOnceWithExactly(sock);
+ // Check if it is ready for some data
+ sock._receiveData(new Uint8Array(['R', 'F', 'B', '0', '0', '3', '0', '0', '8']));
+ expect(callback).to.not.have.been.called;
+ });
+
+ it('should refuse closed WebSocket/RTCDataChannel objects', function () {
+ let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
+ sock.readyState = WebSocket.CLOSED;
+ expect(() => new RFB(document.createElement('div'), sock)).to.throw();
+ });
+
+ it('should pass on attach problems', function () {
+ attach.restore();
+ attach = sinon.stub(Websock.prototype, 'attach');
+ attach.throws(new Error('Failure'));
+ let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
+ expect(() => new RFB(document.createElement('div'), sock)).to.throw('Failure');
});
});
describe('#disconnect', function () {
let client;
+ let close;
+
beforeEach(function () {
- client = make_rfb();
+ client = makeRFB();
+ close = sinon.stub(Websock.prototype, "close");
+ });
+ afterEach(function () {
+ close.restore();
});
- it('should go to state "disconnecting" before "disconnected"', function () {
- sinon.spy(client, '_updateConnectionState');
+ it('should start closing WebSocket', function () {
+ let callback = sinon.spy();
+ client.addEventListener('disconnect', callback);
+ client.disconnect();
+ expect(close).to.have.been.calledOnceWithExactly();
+ expect(callback).to.not.have.been.called;
+ });
+
+ it('should send disconnect event', function () {
+ let callback = sinon.spy();
+ client.addEventListener('disconnect', callback);
+ client.disconnect();
+ close.thisValues[0]._eventHandlers.close(new CloseEvent("close", { 'code': 1000, 'reason': "", 'wasClean': true }));
+ expect(callback).to.have.been.calledOnce;
+ expect(callback.args[0][0].detail.clean).to.be.true;
+ });
+
+ it('should force disconnect if disconnecting takes too long', function () {
+ let callback = sinon.spy();
+ client.addEventListener('disconnect', callback);
+ client.disconnect();
+ this.clock.tick(3 * 1000);
+ expect(callback).to.have.been.calledOnce;
+ expect(callback.args[0][0].detail.clean).to.be.true;
+ });
+
+ it('should not fail if disconnect completes before timeout', function () {
+ let callback = sinon.spy();
+ client.addEventListener('disconnect', callback);
client.disconnect();
- expect(client._updateConnectionState).to.have.been.calledTwice;
- expect(client._updateConnectionState.getCall(0).args[0])
- .to.equal('disconnecting');
- expect(client._updateConnectionState.getCall(1).args[0])
- .to.equal('disconnected');
- expect(client._rfb_connection_state).to.equal('disconnected');
+ client._updateConnectionState('disconnecting');
+ this.clock.tick(3 * 1000 / 2);
+ close.thisValues[0]._eventHandlers.close(new CloseEvent("close", { 'code': 1000, 'reason': "", 'wasClean': true }));
+ this.clock.tick(3 * 1000 / 2 + 1);
+ expect(callback).to.have.been.calledOnce;
+ expect(callback.args[0][0].detail.clean).to.be.true;
});
it('should unregister error event handler', function () {
describe('#sendCredentials', function () {
let client;
beforeEach(function () {
- client = make_rfb();
- client._rfb_connection_state = 'connecting';
+ client = makeRFB();
+ client._rfbConnectionState = 'connecting';
});
it('should set the rfb credentials properly"', function () {
client.sendCredentials({ password: 'pass' });
- expect(client._rfb_credentials).to.deep.equal({ password: 'pass' });
+ expect(client._rfbCredentials).to.deep.equal({ password: 'pass' });
});
- it('should call init_msg "soon"', function () {
- client._init_msg = sinon.spy();
+ it('should call initMsg "soon"', function () {
+ client._initMsg = sinon.spy();
client.sendCredentials({ password: 'pass' });
this.clock.tick(5);
- expect(client._init_msg).to.have.been.calledOnce;
+ expect(client._initMsg).to.have.been.calledOnce;
});
});
});
describe('Public API Basic Behavior', function () {
let client;
beforeEach(function () {
- client = make_rfb();
+ client = makeRFB();
});
describe('#sendCtrlAlDel', function () {
it('should not send the keys if we are not in a normal state', function () {
sinon.spy(client._sock, 'flush');
- client._rfb_connection_state = "connecting";
+ client._rfbConnectionState = "connecting";
client.sendCtrlAltDel();
expect(client._sock.flush).to.not.have.been.called;
});
it('should not send the key if we are not in a normal state', function () {
sinon.spy(client._sock, 'flush');
- client._rfb_connection_state = "connecting";
+ client._rfbConnectionState = "connecting";
client.sendKey(123, 'Key123');
expect(client._sock.flush).to.not.have.been.called;
});
push32(data, toUnsigned32bit(-8));
data = data.concat(flags);
data = data.concat(fileSizes);
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
client.clipboardPasteFrom('extended test');
expect(RFB.messages.extendedClipboardNotify).to.have.been.calledOnce;
it('should flush multiple times for large clipboards', function () {
sinon.spy(client._sock, 'flush');
- let long_text = "";
+ let longText = "";
for (let i = 0; i < client._sock._sQbufferSize + 100; i++) {
- long_text += 'a';
+ longText += 'a';
}
- client.clipboardPasteFrom(long_text);
+ client.clipboardPasteFrom(longText);
expect(client._sock.flush).to.have.been.calledTwice;
});
it('should not send the text if we are not in a normal state', function () {
sinon.spy(client._sock, 'flush');
- client._rfb_connection_state = "connecting";
+ client._rfbConnectionState = "connecting";
client.clipboardPasteFrom('abc');
expect(client._sock.flush).to.not.have.been.called;
});
describe("XVP operations", function () {
beforeEach(function () {
- client._rfb_xvp_ver = 1;
+ client._rfbXvpVer = 1;
});
it('should send the shutdown signal on #machineShutdown', function () {
describe('Clipping', function () {
let client;
beforeEach(function () {
- client = make_rfb();
+ client = makeRFB();
container.style.width = '70px';
container.style.height = '80px';
client.clipViewport = true;
sinon.spy(client._display, "viewportChangeSize");
- client._sock._websocket._receive_data(new Uint8Array(incoming));
+ client._sock._websocket._receiveData(new Uint8Array(incoming));
// FIXME: Display implicitly calls viewportChangeSize() when
// resizing the framebuffer, hence calledTwice.
expect(RFB.messages.pointerEvent).to.have.been.calledTwice;
});
+ it('should not send button messages when in view only', function () {
+ client._viewOnly = true;
+ client._handleMouseButton(13, 9, 0x001);
+ client._handleMouseButton(13, 9, 0x000);
+ expect(RFB.messages.pointerEvent).to.not.have.been.called;
+ });
+
it('should send button message directly when drag is disabled', function () {
client.dragViewport = false;
client._handleMouseButton(13, 9, 0x001);
describe('Scaling', function () {
let client;
beforeEach(function () {
- client = make_rfb();
+ client = makeRFB();
container.style.width = '70px';
container.style.height = '80px';
client.scaleViewport = true;
sinon.spy(client._display, "autoscale");
- client._sock._websocket._receive_data(new Uint8Array(incoming));
+ client._sock._websocket._receiveData(new Uint8Array(incoming));
expect(client._display.autoscale).to.have.been.calledOnce;
expect(client._display.autoscale).to.have.been.calledWith(70, 80);
describe('Remote resize', function () {
let client;
beforeEach(function () {
- client = make_rfb();
+ client = makeRFB();
client._supportsSetDesktopSize = true;
client.resizeSession = true;
container.style.width = '70px';
client._supportsSetDesktopSize = false;
- client._sock._websocket._receive_data(new Uint8Array(incoming));
+ client._sock._websocket._receiveData(new Uint8Array(incoming));
expect(RFB.messages.setDesktopSize).to.have.been.calledOnce;
expect(RFB.messages.setDesktopSize).to.have.been.calledWith(sinon.match.object, 70, 80, 0, 0);
// Second message should not trigger a resize
- client._sock._websocket._receive_data(new Uint8Array(incoming));
+ client._sock._websocket._receiveData(new Uint8Array(incoming));
expect(RFB.messages.setDesktopSize).to.not.have.been.called;
});
0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04,
0x00, 0x00, 0x00, 0x00 ];
- client._sock._websocket._receive_data(new Uint8Array(incoming));
+ client._sock._websocket._receiveData(new Uint8Array(incoming));
expect(RFB.messages.setDesktopSize).to.not.have.been.called;
});
});
describe('Misc Internals', function () {
- describe('#_updateConnectionState', function () {
- let client;
- beforeEach(function () {
- client = make_rfb();
- });
-
- it('should clear the disconnect timer if the state is not "disconnecting"', function () {
- const spy = sinon.spy();
- client._disconnTimer = setTimeout(spy, 50);
- client._rfb_connection_state = 'connecting';
- client._updateConnectionState('connected');
- this.clock.tick(51);
- expect(spy).to.not.have.been.called;
- expect(client._disconnTimer).to.be.null;
- });
-
- it('should set the rfb_connection_state', function () {
- client._rfb_connection_state = 'connecting';
- client._updateConnectionState('connected');
- expect(client._rfb_connection_state).to.equal('connected');
- });
-
- it('should not change the state when we are disconnected', function () {
- client.disconnect();
- expect(client._rfb_connection_state).to.equal('disconnected');
- client._updateConnectionState('connecting');
- expect(client._rfb_connection_state).to.not.equal('connecting');
- });
-
- it('should ignore state changes to the same state', function () {
- const connectSpy = sinon.spy();
- client.addEventListener("connect", connectSpy);
-
- expect(client._rfb_connection_state).to.equal('connected');
- client._updateConnectionState('connected');
- expect(connectSpy).to.not.have.been.called;
-
- client.disconnect();
-
- const disconnectSpy = sinon.spy();
- client.addEventListener("disconnect", disconnectSpy);
-
- expect(client._rfb_connection_state).to.equal('disconnected');
- client._updateConnectionState('disconnected');
- expect(disconnectSpy).to.not.have.been.called;
- });
-
- it('should ignore illegal state changes', function () {
- const spy = sinon.spy();
- client.addEventListener("disconnect", spy);
- client._updateConnectionState('disconnected');
- expect(client._rfb_connection_state).to.not.equal('disconnected');
- expect(spy).to.not.have.been.called;
- });
- });
-
describe('#_fail', function () {
let client;
beforeEach(function () {
- client = make_rfb();
+ client = makeRFB();
});
it('should close the WebSocket connection', function () {
client._fail();
this.clock.tick(2000);
expect(client._updateConnectionState).to.have.been.called;
- expect(client._rfb_connection_state).to.equal('disconnected');
+ expect(client._rfbConnectionState).to.equal('disconnected');
});
it('should set clean_disconnect variable', function () {
- client._rfb_clean_disconnect = true;
- client._rfb_connection_state = 'connected';
+ client._rfbCleanDisconnect = true;
+ client._rfbConnectionState = 'connected';
client._fail();
- expect(client._rfb_clean_disconnect).to.be.false;
+ expect(client._rfbCleanDisconnect).to.be.false;
});
it('should result in disconnect event with clean set to false', function () {
- client._rfb_connection_state = 'connected';
+ client._rfbConnectionState = 'connected';
const spy = sinon.spy();
client.addEventListener("disconnect", spy);
client._fail();
});
});
- describe('Connection States', function () {
- describe('connecting', function () {
- it('should open the websocket connection', function () {
- const client = new RFB(document.createElement('div'),
- 'ws://HOST:8675/PATH');
- sinon.spy(client._sock, 'open');
- this.clock.tick();
- expect(client._sock.open).to.have.been.calledOnce;
- });
- });
-
- describe('connected', function () {
- let client;
- beforeEach(function () {
- client = make_rfb();
- });
-
- it('should result in a connect event if state becomes connected', function () {
- const spy = sinon.spy();
- client.addEventListener("connect", spy);
- client._rfb_connection_state = 'connecting';
- client._updateConnectionState('connected');
- expect(spy).to.have.been.calledOnce;
- });
-
- it('should not result in a connect event if the state is not "connected"', function () {
- const spy = sinon.spy();
- client.addEventListener("connect", spy);
- client._sock._websocket.open = () => {}; // explicitly don't call onopen
- client._updateConnectionState('connecting');
- expect(spy).to.not.have.been.called;
- });
- });
-
- describe('disconnecting', function () {
- let client;
- beforeEach(function () {
- client = make_rfb();
- });
-
- it('should force disconnect if we do not call Websock.onclose within the disconnection timeout', function () {
- sinon.spy(client, '_updateConnectionState');
- client._sock._websocket.close = () => {}; // explicitly don't call onclose
- client._updateConnectionState('disconnecting');
- this.clock.tick(3 * 1000);
- expect(client._updateConnectionState).to.have.been.calledTwice;
- expect(client._rfb_disconnect_reason).to.not.equal("");
- expect(client._rfb_connection_state).to.equal("disconnected");
- });
-
- it('should not fail if Websock.onclose gets called within the disconnection timeout', function () {
- client._updateConnectionState('disconnecting');
- this.clock.tick(3 * 1000 / 2);
- client._sock._websocket.close();
- this.clock.tick(3 * 1000 / 2 + 1);
- expect(client._rfb_connection_state).to.equal('disconnected');
- });
-
- it('should close the WebSocket connection', function () {
- sinon.spy(client._sock, 'close');
- client._updateConnectionState('disconnecting');
- expect(client._sock.close).to.have.been.calledOnce;
- });
-
- it('should not result in a disconnect event', function () {
- const spy = sinon.spy();
- client.addEventListener("disconnect", spy);
- client._sock._websocket.close = () => {}; // explicitly don't call onclose
- client._updateConnectionState('disconnecting');
- expect(spy).to.not.have.been.called;
- });
- });
-
- describe('disconnected', function () {
- let client;
- beforeEach(function () {
- client = new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
- });
-
- it('should result in a disconnect event if state becomes "disconnected"', function () {
- const spy = sinon.spy();
- client.addEventListener("disconnect", spy);
- client._rfb_connection_state = 'disconnecting';
- client._updateConnectionState('disconnected');
- expect(spy).to.have.been.calledOnce;
- expect(spy.args[0][0].detail.clean).to.be.true;
- });
-
- it('should result in a disconnect event without msg when no reason given', function () {
- const spy = sinon.spy();
- client.addEventListener("disconnect", spy);
- client._rfb_connection_state = 'disconnecting';
- client._rfb_disconnect_reason = "";
- client._updateConnectionState('disconnected');
- expect(spy).to.have.been.calledOnce;
- expect(spy.args[0].length).to.equal(1);
- });
- });
- });
-
describe('Protocol Initialization States', function () {
let client;
beforeEach(function () {
- client = make_rfb();
- client._rfb_connection_state = 'connecting';
+ client = makeRFB();
+ client._rfbConnectionState = 'connecting';
});
describe('ProtocolVersion', function () {
- function send_ver(ver, client) {
+ function sendVer(ver, client) {
const arr = new Uint8Array(12);
for (let i = 0; i < ver.length; i++) {
arr[i+4] = ver.charCodeAt(i);
}
arr[0] = 'R'; arr[1] = 'F'; arr[2] = 'B'; arr[3] = ' ';
arr[11] = '\n';
- client._sock._websocket._receive_data(arr);
+ client._sock._websocket._receiveData(arr);
}
describe('version parsing', function () {
it('should interpret version 003.003 as version 3.3', function () {
- send_ver('003.003', client);
- expect(client._rfb_version).to.equal(3.3);
+ sendVer('003.003', client);
+ expect(client._rfbVersion).to.equal(3.3);
});
it('should interpret version 003.006 as version 3.3', function () {
- send_ver('003.006', client);
- expect(client._rfb_version).to.equal(3.3);
+ sendVer('003.006', client);
+ expect(client._rfbVersion).to.equal(3.3);
});
it('should interpret version 003.889 as version 3.3', function () {
- send_ver('003.889', client);
- expect(client._rfb_version).to.equal(3.3);
+ sendVer('003.889', client);
+ expect(client._rfbVersion).to.equal(3.3);
});
it('should interpret version 003.007 as version 3.7', function () {
- send_ver('003.007', client);
- expect(client._rfb_version).to.equal(3.7);
+ sendVer('003.007', client);
+ expect(client._rfbVersion).to.equal(3.7);
});
it('should interpret version 003.008 as version 3.8', function () {
- send_ver('003.008', client);
- expect(client._rfb_version).to.equal(3.8);
+ sendVer('003.008', client);
+ expect(client._rfbVersion).to.equal(3.8);
});
it('should interpret version 004.000 as version 3.8', function () {
- send_ver('004.000', client);
- expect(client._rfb_version).to.equal(3.8);
+ sendVer('004.000', client);
+ expect(client._rfbVersion).to.equal(3.8);
});
it('should interpret version 004.001 as version 3.8', function () {
- send_ver('004.001', client);
- expect(client._rfb_version).to.equal(3.8);
+ sendVer('004.001', client);
+ expect(client._rfbVersion).to.equal(3.8);
});
it('should interpret version 005.000 as version 3.8', function () {
- send_ver('005.000', client);
- expect(client._rfb_version).to.equal(3.8);
+ sendVer('005.000', client);
+ expect(client._rfbVersion).to.equal(3.8);
});
it('should fail on an invalid version', function () {
sinon.spy(client, "_fail");
- send_ver('002.000', client);
+ sendVer('002.000', client);
expect(client._fail).to.have.been.calledOnce;
});
});
it('should send back the interpreted version', function () {
- send_ver('004.000', client);
+ sendVer('004.000', client);
- const expected_str = 'RFB 003.008\n';
+ const expectedStr = 'RFB 003.008\n';
const expected = [];
- for (let i = 0; i < expected_str.length; i++) {
- expected[i] = expected_str.charCodeAt(i);
+ for (let i = 0; i < expectedStr.length; i++) {
+ expected[i] = expectedStr.charCodeAt(i);
}
expect(client._sock).to.have.sent(new Uint8Array(expected));
});
it('should transition to the Security state on successful negotiation', function () {
- send_ver('003.008', client);
- expect(client._rfb_init_state).to.equal('Security');
+ sendVer('003.008', client);
+ expect(client._rfbInitState).to.equal('Security');
});
describe('Repeater', function () {
beforeEach(function () {
- client = make_rfb('wss://host:8675', { repeaterID: "12345" });
- client._rfb_connection_state = 'connecting';
+ client = makeRFB('wss://host:8675', { repeaterID: "12345" });
+ client._rfbConnectionState = 'connecting';
});
it('should interpret version 000.000 as a repeater', function () {
- send_ver('000.000', client);
- expect(client._rfb_version).to.equal(0);
+ sendVer('000.000', client);
+ expect(client._rfbVersion).to.equal(0);
- const sent_data = client._sock._websocket._get_sent_data();
- expect(new Uint8Array(sent_data.buffer, 0, 9)).to.array.equal(new Uint8Array([73, 68, 58, 49, 50, 51, 52, 53, 0]));
- expect(sent_data).to.have.length(250);
+ const sentData = client._sock._websocket._getSentData();
+ expect(new Uint8Array(sentData.buffer, 0, 9)).to.array.equal(new Uint8Array([73, 68, 58, 49, 50, 51, 52, 53, 0]));
+ expect(sentData).to.have.length(250);
});
it('should handle two step repeater negotiation', function () {
- send_ver('000.000', client);
- send_ver('003.008', client);
- expect(client._rfb_version).to.equal(3.8);
+ sendVer('000.000', client);
+ sendVer('003.008', client);
+ expect(client._rfbVersion).to.equal(3.8);
});
});
});
describe('Security', function () {
beforeEach(function () {
- client._rfb_init_state = 'Security';
+ client._rfbInitState = 'Security';
});
it('should simply receive the auth scheme when for versions < 3.7', function () {
- client._rfb_version = 3.6;
- const auth_scheme_raw = [1, 2, 3, 4];
- const auth_scheme = (auth_scheme_raw[0] << 24) + (auth_scheme_raw[1] << 16) +
- (auth_scheme_raw[2] << 8) + auth_scheme_raw[3];
- client._sock._websocket._receive_data(new Uint8Array(auth_scheme_raw));
- expect(client._rfb_auth_scheme).to.equal(auth_scheme);
+ client._rfbVersion = 3.6;
+ const authSchemeRaw = [1, 2, 3, 4];
+ const authScheme = (authSchemeRaw[0] << 24) + (authSchemeRaw[1] << 16) +
+ (authSchemeRaw[2] << 8) + authSchemeRaw[3];
+ client._sock._websocket._receiveData(new Uint8Array(authSchemeRaw));
+ expect(client._rfbAuthScheme).to.equal(authScheme);
});
it('should prefer no authentication is possible', function () {
- client._rfb_version = 3.7;
- const auth_schemes = [2, 1, 3];
- client._sock._websocket._receive_data(new Uint8Array(auth_schemes));
- expect(client._rfb_auth_scheme).to.equal(1);
+ client._rfbVersion = 3.7;
+ const authSchemes = [2, 1, 3];
+ client._sock._websocket._receiveData(new Uint8Array(authSchemes));
+ expect(client._rfbAuthScheme).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;
- const auth_schemes = [2, 22, 16];
- client._sock._websocket._receive_data(new Uint8Array(auth_schemes));
- expect(client._rfb_auth_scheme).to.equal(22);
+ client._rfbVersion = 3.7;
+ const authSchemes = [2, 22, 16];
+ client._sock._websocket._receiveData(new Uint8Array(authSchemes));
+ expect(client._rfbAuthScheme).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;
- const auth_schemes = [1, 32];
- client._sock._websocket._receive_data(new Uint8Array(auth_schemes));
+ client._rfbVersion = 3.7;
+ const authSchemes = [1, 32];
+ client._sock._websocket._receiveData(new Uint8Array(authSchemes));
expect(client._fail).to.have.been.calledOnce;
});
it('should fail with the appropriate message if no types are sent for versions >= 3.7', function () {
- client._rfb_version = 3.7;
- const failure_data = [0, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115];
+ client._rfbVersion = 3.7;
+ const failureData = [0, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115];
sinon.spy(client, '_fail');
- client._sock._websocket._receive_data(new Uint8Array(failure_data));
+ client._sock._websocket._receiveData(new Uint8Array(failureData));
expect(client._fail).to.have.been.calledOnce;
expect(client._fail).to.have.been.calledWith(
});
it('should transition to the Authentication state and continue on successful negotiation', function () {
- client._rfb_version = 3.7;
- const auth_schemes = [1, 1];
- client._negotiate_authentication = sinon.spy();
- client._sock._websocket._receive_data(new Uint8Array(auth_schemes));
- expect(client._rfb_init_state).to.equal('Authentication');
- expect(client._negotiate_authentication).to.have.been.calledOnce;
+ client._rfbVersion = 3.7;
+ const authSchemes = [1, 1];
+ client._negotiateAuthentication = sinon.spy();
+ client._sock._websocket._receiveData(new Uint8Array(authSchemes));
+ expect(client._rfbInitState).to.equal('Authentication');
+ expect(client._negotiateAuthentication).to.have.been.calledOnce;
});
});
describe('Authentication', function () {
beforeEach(function () {
- client._rfb_init_state = 'Security';
+ client._rfbInitState = 'Security';
});
- function send_security(type, cl) {
- cl._sock._websocket._receive_data(new Uint8Array([1, type]));
+ function sendSecurity(type, cl) {
+ cl._sock._websocket._receiveData(new Uint8Array([1, type]));
}
it('should fail on auth scheme 0 (pre 3.7) with the given message', function () {
- client._rfb_version = 3.6;
- const err_msg = "Whoopsies";
+ client._rfbVersion = 3.6;
+ const errMsg = "Whoopsies";
const data = [0, 0, 0, 0];
- const err_len = err_msg.length;
- push32(data, err_len);
- for (let i = 0; i < err_len; i++) {
- data.push(err_msg.charCodeAt(i));
+ const errLen = errMsg.length;
+ push32(data, errLen);
+ for (let i = 0; i < errLen; i++) {
+ data.push(errMsg.charCodeAt(i));
}
sinon.spy(client, '_fail');
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
expect(client._fail).to.have.been.calledWith(
'Security negotiation failed on authentication scheme (reason: Whoopsies)');
});
it('should transition straight to SecurityResult on "no auth" (1) for versions >= 3.8', function () {
- client._rfb_version = 3.8;
- send_security(1, client);
- expect(client._rfb_init_state).to.equal('SecurityResult');
+ client._rfbVersion = 3.8;
+ sendSecurity(1, client);
+ expect(client._rfbInitState).to.equal('SecurityResult');
});
it('should transition straight to ServerInitialisation on "no auth" for versions < 3.8', function () {
- client._rfb_version = 3.7;
- send_security(1, client);
- expect(client._rfb_init_state).to.equal('ServerInitialisation');
+ client._rfbVersion = 3.7;
+ sendSecurity(1, client);
+ expect(client._rfbInitState).to.equal('ServerInitialisation');
});
it('should fail on an unknown auth scheme', function () {
sinon.spy(client, "_fail");
- client._rfb_version = 3.8;
- send_security(57, client);
+ client._rfbVersion = 3.8;
+ sendSecurity(57, client);
expect(client._fail).to.have.been.calledOnce;
});
describe('VNC Authentication (type 2) Handler', function () {
beforeEach(function () {
- client._rfb_init_state = 'Security';
- client._rfb_version = 3.8;
+ client._rfbInitState = 'Security';
+ client._rfbVersion = 3.8;
});
it('should fire the credentialsrequired event if missing a password', function () {
const spy = sinon.spy();
client.addEventListener("credentialsrequired", spy);
- send_security(2, client);
+ sendSecurity(2, client);
const challenge = [];
for (let i = 0; i < 16; i++) { challenge[i] = i; }
- client._sock._websocket._receive_data(new Uint8Array(challenge));
+ client._sock._websocket._receiveData(new Uint8Array(challenge));
- expect(client._rfb_credentials).to.be.empty;
+ expect(client._rfbCredentials).to.be.empty;
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][0].detail.types).to.have.members(["password"]);
});
it('should encrypt the password with DES and then send it back', function () {
- client._rfb_credentials = { password: 'passwd' };
- send_security(2, client);
- client._sock._websocket._get_sent_data(); // skip the choice of auth reply
+ client._rfbCredentials = { password: 'passwd' };
+ sendSecurity(2, client);
+ client._sock._websocket._getSentData(); // skip the choice of auth reply
const challenge = [];
for (let i = 0; i < 16; i++) { challenge[i] = i; }
- client._sock._websocket._receive_data(new Uint8Array(challenge));
+ client._sock._websocket._receiveData(new Uint8Array(challenge));
- const des_pass = RFB.genDES('passwd', challenge);
- expect(client._sock).to.have.sent(new Uint8Array(des_pass));
+ const desPass = RFB.genDES('passwd', challenge);
+ expect(client._sock).to.have.sent(new Uint8Array(desPass));
});
it('should transition to SecurityResult immediately after sending the password', function () {
- client._rfb_credentials = { password: 'passwd' };
- send_security(2, client);
+ client._rfbCredentials = { password: 'passwd' };
+ sendSecurity(2, client);
const challenge = [];
for (let i = 0; i < 16; i++) { challenge[i] = i; }
- client._sock._websocket._receive_data(new Uint8Array(challenge));
+ client._sock._websocket._receiveData(new Uint8Array(challenge));
- expect(client._rfb_init_state).to.equal('SecurityResult');
+ expect(client._rfbInitState).to.equal('SecurityResult');
});
});
describe('XVP Authentication (type 22) Handler', function () {
beforeEach(function () {
- client._rfb_init_state = 'Security';
- client._rfb_version = 3.8;
+ client._rfbInitState = 'Security';
+ client._rfbVersion = 3.8;
});
it('should fall through to standard VNC authentication upon completion', function () {
- client._rfb_credentials = { username: 'user',
- target: 'target',
- password: 'password' };
- client._negotiate_std_vnc_auth = sinon.spy();
- send_security(22, client);
- expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
+ client._rfbCredentials = { username: 'user',
+ target: 'target',
+ password: 'password' };
+ client._negotiateStdVNCAuth = sinon.spy();
+ sendSecurity(22, client);
+ expect(client._negotiateStdVNCAuth).to.have.been.calledOnce;
});
it('should fire the credentialsrequired event if all credentials are missing', function () {
const spy = sinon.spy();
client.addEventListener("credentialsrequired", spy);
- client._rfb_credentials = {};
- send_security(22, client);
+ client._rfbCredentials = {};
+ sendSecurity(22, client);
- expect(client._rfb_credentials).to.be.empty;
+ expect(client._rfbCredentials).to.be.empty;
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][0].detail.types).to.have.members(["username", "password", "target"]);
});
it('should fire the credentialsrequired event if some credentials are missing', function () {
const spy = sinon.spy();
client.addEventListener("credentialsrequired", spy);
- client._rfb_credentials = { username: 'user',
- target: 'target' };
- send_security(22, client);
+ client._rfbCredentials = { username: 'user',
+ target: 'target' };
+ sendSecurity(22, client);
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][0].detail.types).to.have.members(["username", "password", "target"]);
});
it('should send user and target separately', function () {
- client._rfb_credentials = { username: 'user',
- target: 'target',
- password: 'password' };
- client._negotiate_std_vnc_auth = sinon.spy();
+ client._rfbCredentials = { username: 'user',
+ target: 'target',
+ password: 'password' };
+ client._negotiateStdVNCAuth = sinon.spy();
- send_security(22, client);
+ sendSecurity(22, client);
const expected = [22, 4, 6]; // auth selection, len user, len target
for (let i = 0; i < 10; i++) { expected[i+3] = 'usertarget'.charCodeAt(i); }
describe('TightVNC Authentication (type 16) Handler', function () {
beforeEach(function () {
- client._rfb_init_state = 'Security';
- client._rfb_version = 3.8;
- send_security(16, client);
- client._sock._websocket._get_sent_data(); // skip the security reply
+ client._rfbInitState = 'Security';
+ client._rfbVersion = 3.8;
+ sendSecurity(16, client);
+ client._sock._websocket._getSentData(); // skip the security reply
});
- function send_num_str_pairs(pairs, client) {
+ function sendNumStrPairs(pairs, client) {
const data = [];
push32(data, pairs.length);
}
}
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
}
it('should skip tunnel negotiation if no tunnels are requested', function () {
- client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
- expect(client._rfb_tightvnc).to.be.true;
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
+ expect(client._rfbTightVNC).to.be.true;
});
it('should fail if no supported tunnels are listed', function () {
sinon.spy(client, "_fail");
- send_num_str_pairs([[123, 'OTHR', 'SOMETHNG']], client);
+ sendNumStrPairs([[123, 'OTHR', 'SOMETHNG']], client);
expect(client._fail).to.have.been.calledOnce;
});
it('should choose the notunnel tunnel type', function () {
- send_num_str_pairs([[0, 'TGHT', 'NOTUNNEL'], [123, 'OTHR', 'SOMETHNG']], client);
+ sendNumStrPairs([[0, 'TGHT', 'NOTUNNEL'], [123, 'OTHR', 'SOMETHNG']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 0]));
});
it('should choose the notunnel tunnel type for Siemens devices', function () {
- send_num_str_pairs([[1, 'SICR', 'SCHANNEL'], [2, 'SICR', 'SCHANLPW']], client);
+ sendNumStrPairs([[1, 'SICR', 'SCHANNEL'], [2, 'SICR', 'SCHANLPW']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 0]));
});
it('should continue to sub-auth negotiation after tunnel negotiation', function () {
- send_num_str_pairs([[0, 'TGHT', 'NOTUNNEL']], client);
- client._sock._websocket._get_sent_data(); // skip the tunnel choice here
- send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
+ sendNumStrPairs([[0, 'TGHT', 'NOTUNNEL']], client);
+ client._sock._websocket._getSentData(); // skip the tunnel choice here
+ sendNumStrPairs([[1, 'STDV', 'NOAUTH__']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1]));
- expect(client._rfb_init_state).to.equal('SecurityResult');
+ expect(client._rfbInitState).to.equal('SecurityResult');
});
/*it('should attempt to use VNC auth over no auth when possible', function () {
- client._rfb_tightvnc = true;
- client._negotiate_std_vnc_auth = sinon.spy();
- send_num_str_pairs([[1, 'STDV', 'NOAUTH__'], [2, 'STDV', 'VNCAUTH_']], client);
+ client._rfbTightVNC = true;
+ client._negotiateStdVNCAuth = sinon.spy();
+ sendNumStrPairs([[1, 'STDV', 'NOAUTH__'], [2, 'STDV', 'VNCAUTH_']], client);
expect(client._sock).to.have.sent([0, 0, 0, 1]);
- expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
- expect(client._rfb_auth_scheme).to.equal(2);
+ expect(client._negotiateStdVNCAuth).to.have.been.calledOnce;
+ expect(client._rfbAuthScheme).to.equal(2);
});*/ // while this would make sense, the original code doesn't actually do this
it('should accept the "no auth" auth type and transition to SecurityResult', function () {
- client._rfb_tightvnc = true;
- send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
+ client._rfbTightVNC = true;
+ sendNumStrPairs([[1, 'STDV', 'NOAUTH__']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1]));
- expect(client._rfb_init_state).to.equal('SecurityResult');
+ expect(client._rfbInitState).to.equal('SecurityResult');
});
it('should accept VNC authentication and transition to that', function () {
- client._rfb_tightvnc = true;
- client._negotiate_std_vnc_auth = sinon.spy();
- send_num_str_pairs([[2, 'STDV', 'VNCAUTH__']], client);
+ client._rfbTightVNC = true;
+ client._negotiateStdVNCAuth = sinon.spy();
+ sendNumStrPairs([[2, 'STDV', 'VNCAUTH__']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 2]));
- expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
- expect(client._rfb_auth_scheme).to.equal(2);
+ expect(client._negotiateStdVNCAuth).to.have.been.calledOnce;
+ expect(client._rfbAuthScheme).to.equal(2);
});
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);
+ client._rfbTightVNC = true;
+ sendNumStrPairs([[23, 'stdv', 'badval__']], client);
+ expect(client._fail).to.have.been.calledOnce;
+ });
+ });
+
+ describe('VeNCrypt Authentication (type 19) Handler', function () {
+ beforeEach(function () {
+ client._rfbInitState = 'Security';
+ client._rfbVersion = 3.8;
+ sendSecurity(19, client);
+ expect(client._sock).to.have.sent(new Uint8Array([19]));
+ });
+
+ it('should fail with non-0.2 versions', function () {
+ sinon.spy(client, "_fail");
+ client._sock._websocket._receiveData(new Uint8Array([0, 1]));
+ expect(client._fail).to.have.been.calledOnce;
+ });
+
+ it('should fail if the Plain authentication is not present', function () {
+ // VeNCrypt version
+ client._sock._websocket._receiveData(new Uint8Array([0, 2]));
+ expect(client._sock).to.have.sent(new Uint8Array([0, 2]));
+ // Server ACK.
+ client._sock._websocket._receiveData(new Uint8Array([0]));
+ // Subtype list, only list subtype 1.
+ sinon.spy(client, "_fail");
+ client._sock._websocket._receiveData(new Uint8Array([1, 0, 0, 0, 1]));
expect(client._fail).to.have.been.calledOnce;
});
+
+ it('should support Plain authentication', function () {
+ client._rfbCredentials = { username: 'username', password: 'password' };
+ // VeNCrypt version
+ client._sock._websocket._receiveData(new Uint8Array([0, 2]));
+ expect(client._sock).to.have.sent(new Uint8Array([0, 2]));
+ // Server ACK.
+ client._sock._websocket._receiveData(new Uint8Array([0]));
+ // Subtype list.
+ client._sock._websocket._receiveData(new Uint8Array([1, 0, 0, 1, 0]));
+
+ const expectedResponse = [];
+ push32(expectedResponse, 256); // Chosen subtype.
+ push32(expectedResponse, client._rfbCredentials.username.length);
+ push32(expectedResponse, client._rfbCredentials.password.length);
+ pushString(expectedResponse, client._rfbCredentials.username);
+ pushString(expectedResponse, client._rfbCredentials.password);
+ expect(client._sock).to.have.sent(new Uint8Array(expectedResponse));
+
+ client._initMsg = sinon.spy();
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
+ expect(client._initMsg).to.have.been.called;
+ });
+
+ it('should support Plain authentication with an empty password', function () {
+ client._rfbCredentials = { username: 'username', password: '' };
+ // VeNCrypt version
+ client._sock._websocket._receiveData(new Uint8Array([0, 2]));
+ expect(client._sock).to.have.sent(new Uint8Array([0, 2]));
+ // Server ACK.
+ client._sock._websocket._receiveData(new Uint8Array([0]));
+ // Subtype list.
+ client._sock._websocket._receiveData(new Uint8Array([1, 0, 0, 1, 0]));
+
+ const expectedResponse = [];
+ push32(expectedResponse, 256); // Chosen subtype.
+ push32(expectedResponse, client._rfbCredentials.username.length);
+ push32(expectedResponse, client._rfbCredentials.password.length);
+ pushString(expectedResponse, client._rfbCredentials.username);
+ pushString(expectedResponse, client._rfbCredentials.password);
+ expect(client._sock).to.have.sent(new Uint8Array(expectedResponse));
+
+ client._initMsg = sinon.spy();
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
+ expect(client._initMsg).to.have.been.called;
+ });
+
+ it('should support Plain authentication with a very long username and password', function () {
+ client._rfbCredentials = { username: 'a'.repeat(300), password: 'a'.repeat(300) };
+ // VeNCrypt version
+ client._sock._websocket._receiveData(new Uint8Array([0, 2]));
+ expect(client._sock).to.have.sent(new Uint8Array([0, 2]));
+ // Server ACK.
+ client._sock._websocket._receiveData(new Uint8Array([0]));
+ // Subtype list.
+ client._sock._websocket._receiveData(new Uint8Array([1, 0, 0, 1, 0]));
+
+ const expectedResponse = [];
+ push32(expectedResponse, 256); // Chosen subtype.
+ push32(expectedResponse, client._rfbCredentials.username.length);
+ push32(expectedResponse, client._rfbCredentials.password.length);
+ pushString(expectedResponse, client._rfbCredentials.username);
+ pushString(expectedResponse, client._rfbCredentials.password);
+ expect(client._sock).to.have.sent(new Uint8Array(expectedResponse));
+
+ client._initMsg = sinon.spy();
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
+ expect(client._initMsg).to.have.been.called;
+ });
});
});
describe('SecurityResult', function () {
beforeEach(function () {
- client._rfb_init_state = 'SecurityResult';
+ client._rfbInitState = 'SecurityResult';
});
it('should fall through to ServerInitialisation on a response code of 0', function () {
- client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
- expect(client._rfb_init_state).to.equal('ServerInitialisation');
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
+ expect(client._rfbInitState).to.equal('ServerInitialisation');
});
it('should fail on an error code of 1 with the given message for versions >= 3.8', function () {
- client._rfb_version = 3.8;
+ client._rfbVersion = 3.8;
sinon.spy(client, '_fail');
- const 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));
+ const failureData = [0, 0, 0, 1, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115];
+ client._sock._websocket._receiveData(new Uint8Array(failureData));
expect(client._fail).to.have.been.calledWith(
'Security negotiation failed on security result (reason: 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]));
+ client._rfbVersion = 3.7;
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 1]));
expect(client._fail).to.have.been.calledWith(
'Security handshake failed');
});
it('should result in securityfailure event when receiving a non zero status', function () {
const spy = sinon.spy();
client.addEventListener("securityfailure", spy);
- client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 2]));
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 2]));
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][0].detail.status).to.equal(2);
});
it('should include reason when provided in securityfailure event', function () {
- client._rfb_version = 3.8;
+ client._rfbVersion = 3.8;
const spy = sinon.spy();
client.addEventListener("securityfailure", spy);
- const failure_data = [0, 0, 0, 1, 0, 0, 0, 12, 115, 117, 99, 104,
- 32, 102, 97, 105, 108, 117, 114, 101];
- client._sock._websocket._receive_data(new Uint8Array(failure_data));
+ const failureData = [0, 0, 0, 1, 0, 0, 0, 12, 115, 117, 99, 104,
+ 32, 102, 97, 105, 108, 117, 114, 101];
+ client._sock._websocket._receiveData(new Uint8Array(failureData));
expect(spy.args[0][0].detail.status).to.equal(1);
expect(spy.args[0][0].detail.reason).to.equal('such failure');
});
it('should not include reason when length is zero in securityfailure event', function () {
- client._rfb_version = 3.9;
+ client._rfbVersion = 3.9;
const spy = sinon.spy();
client.addEventListener("securityfailure", spy);
- const failure_data = [0, 0, 0, 1, 0, 0, 0, 0];
- client._sock._websocket._receive_data(new Uint8Array(failure_data));
+ const failureData = [0, 0, 0, 1, 0, 0, 0, 0];
+ client._sock._websocket._receiveData(new Uint8Array(failureData));
expect(spy.args[0][0].detail.status).to.equal(1);
expect('reason' in spy.args[0][0].detail).to.be.false;
});
it('should not include reason in securityfailure event for version < 3.8', function () {
- client._rfb_version = 3.6;
+ client._rfbVersion = 3.6;
const spy = sinon.spy();
client.addEventListener("securityfailure", spy);
- client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 2]));
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 2]));
expect(spy.args[0][0].detail.status).to.equal(2);
expect('reason' in spy.args[0][0].detail).to.be.false;
});
describe('ClientInitialisation', function () {
it('should transition to the ServerInitialisation state', function () {
- const client = make_rfb();
- client._rfb_connection_state = 'connecting';
- client._rfb_init_state = 'SecurityResult';
- client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
- expect(client._rfb_init_state).to.equal('ServerInitialisation');
+ const client = makeRFB();
+ client._rfbConnectionState = 'connecting';
+ client._rfbInitState = 'SecurityResult';
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
+ expect(client._rfbInitState).to.equal('ServerInitialisation');
});
it('should send 1 if we are in shared mode', function () {
- const client = make_rfb('wss://host:8675', { shared: true });
- client._rfb_connection_state = 'connecting';
- client._rfb_init_state = 'SecurityResult';
- client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
+ const client = makeRFB('wss://host:8675', { shared: true });
+ client._rfbConnectionState = 'connecting';
+ client._rfbInitState = 'SecurityResult';
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
expect(client._sock).to.have.sent(new Uint8Array([1]));
});
it('should send 0 if we are not in shared mode', function () {
- const client = make_rfb('wss://host:8675', { shared: false });
- client._rfb_connection_state = 'connecting';
- client._rfb_init_state = 'SecurityResult';
- client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
+ const client = makeRFB('wss://host:8675', { shared: false });
+ client._rfbConnectionState = 'connecting';
+ client._rfbInitState = 'SecurityResult';
+ client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
expect(client._sock).to.have.sent(new Uint8Array([0]));
});
});
describe('ServerInitialisation', function () {
beforeEach(function () {
- client._rfb_init_state = 'ServerInitialisation';
+ client._rfbInitState = 'ServerInitialisation';
});
- function send_server_init(opts, client) {
- const full_opts = { width: 10, height: 12, bpp: 24, depth: 24, big_endian: 0,
- true_color: 1, red_max: 255, green_max: 255, blue_max: 255,
- red_shift: 16, green_shift: 8, blue_shift: 0, name: 'a name' };
+ function sendServerInit(opts, client) {
+ const fullOpts = { width: 10, height: 12, bpp: 24, depth: 24, bigEndian: 0,
+ trueColor: 1, redMax: 255, greenMax: 255, blueMax: 255,
+ redShift: 16, greenShift: 8, blueShift: 0, name: 'a name' };
for (let opt in opts) {
- full_opts[opt] = opts[opt];
+ fullOpts[opt] = opts[opt];
}
const data = [];
- push16(data, full_opts.width);
- push16(data, full_opts.height);
+ push16(data, fullOpts.width);
+ push16(data, fullOpts.height);
- data.push(full_opts.bpp);
- data.push(full_opts.depth);
- data.push(full_opts.big_endian);
- data.push(full_opts.true_color);
+ data.push(fullOpts.bpp);
+ data.push(fullOpts.depth);
+ data.push(fullOpts.bigEndian);
+ data.push(fullOpts.trueColor);
- push16(data, full_opts.red_max);
- push16(data, full_opts.green_max);
- push16(data, full_opts.blue_max);
- push8(data, full_opts.red_shift);
- push8(data, full_opts.green_shift);
- push8(data, full_opts.blue_shift);
+ push16(data, fullOpts.redMax);
+ push16(data, fullOpts.greenMax);
+ push16(data, fullOpts.blueMax);
+ push8(data, fullOpts.redShift);
+ push8(data, fullOpts.greenShift);
+ push8(data, fullOpts.blueShift);
// padding
push8(data, 0);
push8(data, 0);
push8(data, 0);
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
- const name_data = [];
- let name_len = [];
- pushString(name_data, full_opts.name);
- push32(name_len, name_data.length);
+ const nameData = [];
+ let nameLen = [];
+ pushString(nameData, fullOpts.name);
+ push32(nameLen, nameData.length);
- client._sock._websocket._receive_data(new Uint8Array(name_len));
- client._sock._websocket._receive_data(new Uint8Array(name_data));
+ client._sock._websocket._receiveData(new Uint8Array(nameLen));
+ client._sock._websocket._receiveData(new Uint8Array(nameData));
}
it('should set the framebuffer width and height', function () {
- send_server_init({ width: 32, height: 84 }, client);
- expect(client._fb_width).to.equal(32);
- expect(client._fb_height).to.equal(84);
+ sendServerInit({ width: 32, height: 84 }, client);
+ expect(client._fbWidth).to.equal(32);
+ expect(client._fbHeight).to.equal(84);
});
// NB(sross): we just warn, not fail, for endian-ness and shifts, so we don't test them
it('should set the framebuffer name and call the callback', function () {
const spy = sinon.spy();
client.addEventListener("desktopname", spy);
- send_server_init({ name: 'som€ nam€' }, client);
+ sendServerInit({ name: 'som€ nam€' }, client);
- expect(client._fb_name).to.equal('som€ nam€');
+ expect(client._fbName).to.equal('som€ nam€');
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][0].detail.name).to.equal('som€ nam€');
});
it('should handle the extended init message of the tight encoding', function () {
// NB(sross): we don't actually do anything with it, so just test that we can
// read it w/o throwing an error
- client._rfb_tightvnc = true;
- send_server_init({}, client);
-
- const tight_data = [];
- push16(tight_data, 1);
- push16(tight_data, 2);
- push16(tight_data, 3);
- push16(tight_data, 0);
+ client._rfbTightVNC = true;
+ sendServerInit({}, client);
+
+ const tightData = [];
+ push16(tightData, 1);
+ push16(tightData, 2);
+ push16(tightData, 3);
+ push16(tightData, 0);
for (let i = 0; i < 16 + 32 + 48; i++) {
- tight_data.push(i);
+ tightData.push(i);
}
- client._sock._websocket._receive_data(new Uint8Array(tight_data));
+ client._sock._websocket._receiveData(new Uint8Array(tightData));
- expect(client._rfb_connection_state).to.equal('connected');
+ expect(client._rfbConnectionState).to.equal('connected');
});
it('should resize the display', function () {
sinon.spy(client._display, 'resize');
- send_server_init({ width: 27, height: 32 }, client);
+ sendServerInit({ width: 27, height: 32 }, client);
expect(client._display.resize).to.have.been.calledOnce;
expect(client._display.resize).to.have.been.calledWith(27, 32);
});
- it('should grab the mouse and keyboard', function () {
+ it('should grab the keyboard', function () {
sinon.spy(client._keyboard, 'grab');
- sinon.spy(client._mouse, 'grab');
- send_server_init({}, client);
+ sendServerInit({}, client);
expect(client._keyboard.grab).to.have.been.calledOnce;
- expect(client._mouse.grab).to.have.been.calledOnce;
});
describe('Initial Update Request', function () {
// TODO(directxman12): test the various options in this configuration matrix
it('should reply with the pixel format, client encodings, and initial update request', function () {
- send_server_init({ width: 27, height: 32 }, client);
+ sendServerInit({ width: 27, height: 32 }, client);
expect(RFB.messages.pixelFormat).to.have.been.calledOnce;
expect(RFB.messages.pixelFormat).to.have.been.calledWith(client._sock, 24, true);
});
it('should reply with restricted settings for Intel AMT servers', function () {
- send_server_init({ width: 27, height: 32, name: "Intel(r) AMT KVM"}, client);
+ sendServerInit({ width: 27, height: 32, name: "Intel(r) AMT KVM"}, client);
expect(RFB.messages.pixelFormat).to.have.been.calledOnce;
expect(RFB.messages.pixelFormat).to.have.been.calledWith(client._sock, 8, true);
});
});
- it('should transition to the "connected" state', function () {
- send_server_init({}, client);
- expect(client._rfb_connection_state).to.equal('connected');
+ it('should send the "connect" event', function () {
+ let spy = sinon.spy();
+ client.addEventListener('connect', spy);
+ sendServerInit({}, client);
+ expect(spy).to.have.been.calledOnce;
});
});
});
let client;
beforeEach(function () {
- client = make_rfb();
- client._fb_name = 'some device';
- client._fb_width = 640;
- client._fb_height = 20;
+ client = makeRFB();
+ client._fbName = 'some device';
+ client._fbWidth = 640;
+ client._fbHeight = 20;
});
describe('Framebuffer Update Handling', function () {
- const target_data_arr = [
- 0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
- 0x00, 0xff, 0x00, 255, 0xff, 0x00, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
- 0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255,
- 0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255
- ];
- let target_data;
-
- const target_data_check_arr = [
- 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
- 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
- 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
- 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
- ];
- let target_data_check;
-
- before(function () {
- // NB(directxman12): PhantomJS 1.x doesn't implement Uint8ClampedArray
- target_data = new Uint8Array(target_data_arr);
- target_data_check = new Uint8Array(target_data_check_arr);
- });
-
- function send_fbu_msg(rect_info, rect_data, client, rect_cnt) {
+ function sendFbuMsg(rectInfo, rectData, client, rectCnt) {
let data = [];
- if (!rect_cnt || rect_cnt > -1) {
+ if (!rectCnt || rectCnt > -1) {
// header
data.push(0); // msg type
data.push(0); // padding
- push16(data, rect_cnt || rect_data.length);
+ push16(data, rectCnt || rectData.length);
}
- for (let i = 0; i < rect_data.length; i++) {
- if (rect_info[i]) {
- push16(data, rect_info[i].x);
- push16(data, rect_info[i].y);
- push16(data, rect_info[i].width);
- push16(data, rect_info[i].height);
- push32(data, rect_info[i].encoding);
+ for (let i = 0; i < rectData.length; i++) {
+ if (rectInfo[i]) {
+ push16(data, rectInfo[i].x);
+ push16(data, rectInfo[i].y);
+ push16(data, rectInfo[i].width);
+ push16(data, rectInfo[i].height);
+ push32(data, rectInfo[i].encoding);
}
- data = data.concat(rect_data[i]);
+ data = data.concat(rectData[i]);
}
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
}
it('should send an update request if there is sufficient data', function () {
- const expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
- RFB.messages.fbUpdateRequest(expected_msg, true, 0, 0, 640, 20);
+ const expectedMsg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
+ RFB.messages.fbUpdateRequest(expectedMsg, true, 0, 0, 640, 20);
client._framebufferUpdate = () => true;
- client._sock._websocket._receive_data(new Uint8Array([0]));
+ client._sock._websocket._receiveData(new Uint8Array([0]));
- expect(client._sock).to.have.sent(expected_msg._sQ);
+ expect(client._sock).to.have.sent(expectedMsg._sQ);
});
it('should not send an update request if we need more data', function () {
- client._sock._websocket._receive_data(new Uint8Array([0]));
- expect(client._sock._websocket._get_sent_data()).to.have.length(0);
+ client._sock._websocket._receiveData(new Uint8Array([0]));
+ expect(client._sock._websocket._getSentData()).to.have.length(0);
});
it('should resume receiving an update if we previously did not have enough data', function () {
- const expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
- RFB.messages.fbUpdateRequest(expected_msg, true, 0, 0, 640, 20);
+ const expectedMsg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
+ RFB.messages.fbUpdateRequest(expectedMsg, 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._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 3]));
+ expect(client._sock._websocket._getSentData()).to.have.length(0);
client._framebufferUpdate = function () { this._sock.rQskipBytes(1); 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);
+ client._sock._websocket._receiveData(new Uint8Array([247]));
+ expect(client._sock).to.have.sent(expectedMsg._sQ);
});
it('should not send a request in continuous updates mode', function () {
client._enabledContinuousUpdates = true;
client._framebufferUpdate = () => true;
- client._sock._websocket._receive_data(new Uint8Array([0]));
+ client._sock._websocket._receiveData(new Uint8Array([0]));
- expect(client._sock._websocket._get_sent_data()).to.have.length(0);
+ expect(client._sock._websocket._getSentData()).to.have.length(0);
});
it('should fail on an unsupported encoding', function () {
sinon.spy(client, "_fail");
- const rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 234 };
- send_fbu_msg([rect_info], [[]], client);
+ const rectInfo = { x: 8, y: 11, width: 27, height: 32, encoding: 234 };
+ sendFbuMsg([rectInfo], [[]], client);
expect(client._fail).to.have.been.calledOnce;
});
- it('should be able to pause and resume receiving rects if not enought data', function () {
- // seed some initial data to copy
- client._fb_width = 4;
- client._fb_height = 4;
- client._display.resize(4, 4);
- client._display.blitRgbxImage(0, 0, 4, 2, new Uint8Array(target_data_check_arr.slice(0, 32)), 0);
-
- const 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: 2, old_y: 0 }, { old_x: 0, old_y: 0 }]
- const 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);
- expect(client._display).to.have.displayed(target_data_check);
- });
-
describe('Message Encoding Handlers', function () {
beforeEach(function () {
// a really small frame
- client._fb_width = 4;
- client._fb_height = 4;
- client._fb_depth = 24;
+ client._fbWidth = 4;
+ client._fbHeight = 4;
+ client._fbDepth = 24;
client._display.resize(4, 4);
});
- it('should handle the RAW encoding', function () {
- const info = [{ x: 0, y: 0, width: 2, height: 2, encoding: 0x00 },
- { x: 2, y: 0, width: 2, height: 2, encoding: 0x00 },
- { x: 0, y: 2, width: 4, height: 1, encoding: 0x00 },
- { x: 0, y: 3, width: 4, height: 1, encoding: 0x00 }];
- // data is in bgrx
- const rects = [
- [0x00, 0x00, 0xff, 0, 0x00, 0xff, 0x00, 0, 0x00, 0xff, 0x00, 0, 0x00, 0x00, 0xff, 0],
- [0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0],
- [0xff, 0x00, 0xee, 0, 0xff, 0xee, 0x00, 0, 0xff, 0xee, 0xaa, 0, 0xff, 0xee, 0xab, 0],
- [0xff, 0x00, 0xee, 0, 0xff, 0xee, 0x00, 0, 0xff, 0xee, 0xaa, 0, 0xff, 0xee, 0xab, 0]];
- send_fbu_msg(info, rects, client);
- expect(client._display).to.have.displayed(target_data);
- });
-
- it('should handle the RAW encoding in low colour mode', function () {
- const info = [{ x: 0, y: 0, width: 2, height: 2, encoding: 0x00 },
- { x: 2, y: 0, width: 2, height: 2, encoding: 0x00 },
- { x: 0, y: 2, width: 4, height: 1, encoding: 0x00 },
- { x: 0, y: 3, width: 4, height: 1, encoding: 0x00 }];
- const rects = [
- [0x03, 0x03, 0x03, 0x03],
- [0x0c, 0x0c, 0x0c, 0x0c],
- [0x0c, 0x0c, 0x03, 0x03],
- [0x0c, 0x0c, 0x03, 0x03]];
- client._fb_depth = 8;
- send_fbu_msg(info, rects, client);
- expect(client._display).to.have.displayed(target_data_check);
- });
-
- it('should handle the COPYRECT encoding', function () {
- // seed some initial data to copy
- client._display.blitRgbxImage(0, 0, 4, 2, new Uint8Array(target_data_check_arr.slice(0, 32)), 0);
-
- const 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 }]
- const rects = [[0, 2, 0, 0], [0, 0, 0, 0]];
- send_fbu_msg(info, rects, client);
- expect(client._display).to.have.displayed(target_data_check);
- });
-
- // TODO(directxman12): for encodings with subrects, test resuming on partial send?
- // TODO(directxman12): test rre_chunk_sz (related to above about subrects)?
-
- it('should handle the RRE encoding', function () {
- const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x02 }];
- const rect = [];
- push32(rect, 2); // 2 subrects
- push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
- rect.push(0xff); // becomes ff0000ff --> #0000FF color
- rect.push(0x00);
- rect.push(0x00);
- rect.push(0xff);
- push16(rect, 0); // x: 0
- push16(rect, 0); // y: 0
- push16(rect, 2); // width: 2
- push16(rect, 2); // height: 2
- rect.push(0xff); // becomes ff0000ff --> #0000FF color
- rect.push(0x00);
- rect.push(0x00);
- rect.push(0xff);
- push16(rect, 2); // x: 2
- push16(rect, 2); // y: 2
- push16(rect, 2); // width: 2
- push16(rect, 2); // height: 2
-
- send_fbu_msg(info, [rect], client);
- expect(client._display).to.have.displayed(target_data_check);
- });
-
- describe('the HEXTILE encoding handler', function () {
- it('should handle a tile with fg, bg specified, normal subrects', function () {
- const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
- const rect = [];
- rect.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
- push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
- rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
- rect.push(0x00);
- rect.push(0x00);
- rect.push(0xff);
- rect.push(2); // 2 subrects
- rect.push(0); // x: 0, y: 0
- rect.push(1 | (1 << 4)); // width: 2, height: 2
- rect.push(2 | (2 << 4)); // x: 2, y: 2
- rect.push(1 | (1 << 4)); // width: 2, height: 2
- send_fbu_msg(info, [rect], client);
- expect(client._display).to.have.displayed(target_data_check);
- });
-
- it('should handle a raw tile', function () {
- const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
- const rect = [];
- rect.push(0x01); // raw
- for (let i = 0; i < target_data.length; i += 4) {
- rect.push(target_data[i + 2]);
- rect.push(target_data[i + 1]);
- rect.push(target_data[i]);
- rect.push(target_data[i + 3]);
- }
- send_fbu_msg(info, [rect], client);
- expect(client._display).to.have.displayed(target_data);
- });
-
- it('should handle a tile with only bg specified (solid bg)', function () {
- const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
- const rect = [];
- rect.push(0x02);
- push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
- send_fbu_msg(info, [rect], client);
-
- const expected = [];
- for (let i = 0; i < 16; i++) { push32(expected, 0xff00ff); }
- expect(client._display).to.have.displayed(new Uint8Array(expected));
- });
-
- 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.resize(8, 4);
-
- const info = [{ x: 0, y: 0, width: 32, height: 4, encoding: 0x05 }];
-
- const rect = [];
-
- // send a bg frame
- rect.push(0x02);
- push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
-
- // send an empty frame
- rect.push(0x00);
-
- send_fbu_msg(info, [rect], client);
-
- const expected = [];
- for (let i = 0; i < 16; i++) { push32(expected, 0xff00ff); } // rect 1: solid
- for (let i = 0; i < 16; i++) { push32(expected, 0xff00ff); } // rect 2: same bkground color
- expect(client._display).to.have.displayed(new Uint8Array(expected));
- });
-
- it('should handle a tile with bg and coloured subrects', function () {
- const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
- const rect = [];
- rect.push(0x02 | 0x08 | 0x10); // bg spec, anysubrects, colouredsubrects
- push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
- rect.push(2); // 2 subrects
- rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
- rect.push(0x00);
- rect.push(0x00);
- rect.push(0xff);
- rect.push(0); // x: 0, y: 0
- rect.push(1 | (1 << 4)); // width: 2, height: 2
- rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
- rect.push(0x00);
- rect.push(0x00);
- rect.push(0xff);
- rect.push(2 | (2 << 4)); // x: 2, y: 2
- rect.push(1 | (1 << 4)); // width: 2, height: 2
- send_fbu_msg(info, [rect], client);
- expect(client._display).to.have.displayed(target_data_check);
- });
-
- it('should carry over fg and bg colors from the previous tile if not specified', function () {
- client._fb_width = 4;
- client._fb_height = 17;
- client._display.resize(4, 17);
-
- const info = [{ x: 0, y: 0, width: 4, height: 17, encoding: 0x05}];
- const rect = [];
- rect.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
- push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
- rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
- rect.push(0x00);
- rect.push(0x00);
- rect.push(0xff);
- rect.push(8); // 8 subrects
- for (let i = 0; i < 4; i++) {
- rect.push((0 << 4) | (i * 4)); // x: 0, y: i*4
- rect.push(1 | (1 << 4)); // width: 2, height: 2
- rect.push((2 << 4) | (i * 4 + 2)); // x: 2, y: i * 4 + 2
- rect.push(1 | (1 << 4)); // width: 2, height: 2
- }
- rect.push(0x08); // anysubrects
- rect.push(1); // 1 subrect
- rect.push(0); // x: 0, y: 0
- rect.push(1 | (1 << 4)); // width: 2, height: 2
- send_fbu_msg(info, [rect], client);
-
- let expected = [];
- for (let i = 0; i < 4; i++) { expected = expected.concat(target_data_check_arr); }
- expected = expected.concat(target_data_check_arr.slice(0, 16));
- expect(client._display).to.have.displayed(new Uint8Array(expected));
- });
-
- it('should fail on an invalid subencoding', function () {
- sinon.spy(client, "_fail");
- const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
- const rects = [[45]]; // an invalid subencoding
- send_fbu_msg(info, rects, client);
- expect(client._fail).to.have.been.calledOnce;
- });
- });
-
- it.skip('should handle the TIGHT encoding', function () {
- // TODO(directxman12): test this
- });
-
- it.skip('should handle the TIGHT_PNG encoding', function () {
- // TODO(directxman12): test this
- });
-
it('should handle the DesktopSize pseduo-encoding', function () {
sinon.spy(client._display, 'resize');
- send_fbu_msg([{ x: 0, y: 0, width: 20, height: 50, encoding: -223 }], [[]], client);
+ sendFbuMsg([{ x: 0, y: 0, width: 20, height: 50, encoding: -223 }], [[]], client);
- expect(client._fb_width).to.equal(20);
- expect(client._fb_height).to.equal(50);
+ expect(client._fbWidth).to.equal(20);
+ expect(client._fbHeight).to.equal(50);
expect(client._display.resize).to.have.been.calledOnce;
expect(client._display.resize).to.have.been.calledWith(20, 50);
describe('the ExtendedDesktopSize pseudo-encoding handler', function () {
beforeEach(function () {
// a really small frame
- client._fb_width = 4;
- client._fb_height = 4;
+ client._fbWidth = 4;
+ client._fbHeight = 4;
client._display.resize(4, 4);
sinon.spy(client._display, 'resize');
});
- function make_screen_data(nr_of_screens) {
+ function makeScreenData(nrOfScreens) {
const data = [];
- push8(data, nr_of_screens); // number-of-screens
+ push8(data, nrOfScreens); // number-of-screens
push8(data, 0); // padding
push16(data, 0); // padding
- for (let i=0; i<nr_of_screens; i += 1) {
+ for (let i=0; i<nrOfScreens; i += 1) {
push32(data, 0); // id
push16(data, 0); // x-position
push16(data, 0); // y-position
}
it('should handle a resize requested by this client', function () {
- const reason_for_change = 1; // requested by this client
- const status_code = 0; // No error
+ const reasonForChange = 1; // requested by this client
+ const statusCode = 0; // No error
- send_fbu_msg([{ x: reason_for_change, y: status_code,
- width: 20, height: 50, encoding: -308 }],
- make_screen_data(1), client);
+ sendFbuMsg([{ x: reasonForChange, y: statusCode,
+ width: 20, height: 50, encoding: -308 }],
+ makeScreenData(1), client);
- expect(client._fb_width).to.equal(20);
- expect(client._fb_height).to.equal(50);
+ expect(client._fbWidth).to.equal(20);
+ expect(client._fbHeight).to.equal(50);
expect(client._display.resize).to.have.been.calledOnce;
expect(client._display.resize).to.have.been.calledWith(20, 50);
});
it('should handle a resize requested by another client', function () {
- const reason_for_change = 2; // requested by another client
- const status_code = 0; // No error
+ const reasonForChange = 2; // requested by another client
+ const statusCode = 0; // No error
- send_fbu_msg([{ x: reason_for_change, y: status_code,
- width: 20, height: 50, encoding: -308 }],
- make_screen_data(1), client);
+ sendFbuMsg([{ x: reasonForChange, y: statusCode,
+ width: 20, height: 50, encoding: -308 }],
+ makeScreenData(1), client);
- expect(client._fb_width).to.equal(20);
- expect(client._fb_height).to.equal(50);
+ expect(client._fbWidth).to.equal(20);
+ expect(client._fbHeight).to.equal(50);
expect(client._display.resize).to.have.been.calledOnce;
expect(client._display.resize).to.have.been.calledWith(20, 50);
});
it('should be able to recieve requests which contain data for multiple screens', function () {
- const reason_for_change = 2; // requested by another client
- const status_code = 0; // No error
+ const reasonForChange = 2; // requested by another client
+ const statusCode = 0; // No error
- send_fbu_msg([{ x: reason_for_change, y: status_code,
- width: 60, height: 50, encoding: -308 }],
- make_screen_data(3), client);
+ sendFbuMsg([{ x: reasonForChange, y: statusCode,
+ width: 60, height: 50, encoding: -308 }],
+ makeScreenData(3), client);
- expect(client._fb_width).to.equal(60);
- expect(client._fb_height).to.equal(50);
+ expect(client._fbWidth).to.equal(60);
+ expect(client._fbHeight).to.equal(50);
expect(client._display.resize).to.have.been.calledOnce;
expect(client._display.resize).to.have.been.calledWith(60, 50);
});
it('should not handle a failed request', function () {
- const reason_for_change = 1; // requested by this client
- const status_code = 1; // Resize is administratively prohibited
+ const reasonForChange = 1; // requested by this client
+ const statusCode = 1; // Resize is administratively prohibited
- send_fbu_msg([{ x: reason_for_change, y: status_code,
- width: 20, height: 50, encoding: -308 }],
- make_screen_data(1), client);
+ sendFbuMsg([{ x: reasonForChange, y: statusCode,
+ width: 20, height: 50, encoding: -308 }],
+ makeScreenData(1), client);
- expect(client._fb_width).to.equal(4);
- expect(client._fb_height).to.equal(4);
+ expect(client._fbWidth).to.equal(4);
+ expect(client._fbHeight).to.equal(4);
expect(client._display.resize).to.not.have.been.called;
});
}
expected = new Uint8Array(expected);
- send_fbu_msg([info], [rect], client);
+ sendFbuMsg([info], [rect], client);
expect(client._cursor.change).to.have.been.calledOnce;
expect(client._cursor.change).to.have.been.calledWith(expected, 5, 7, 4, 4);
encoding: -239};
const rect = [];
- send_fbu_msg([info], [rect], client);
+ sendFbuMsg([info], [rect], client);
expect(client._cursor.change).to.have.been.calledOnce;
expect(client._cursor.change).to.have.been.calledWith(new Uint8Array, 0, 0, 0, 0);
}
expected = new Uint8Array(expected);
- send_fbu_msg([info], [rect], client);
+ sendFbuMsg([info], [rect], client);
expect(client._cursor.change).to.have.been.calledOnce;
expect(client._cursor.change).to.have.been.calledWith(expected, 5, 7, 4, 4);
}
expected = new Uint8Array(expected);
- send_fbu_msg([info], [rect], client);
+ sendFbuMsg([info], [rect], client);
expect(client._cursor.change).to.have.been.calledOnce;
expect(client._cursor.change).to.have.been.calledWith(expected, 5, 7, 4, 4);
const rect = [];
const dot = RFB.cursors.dot;
- send_fbu_msg([info], [rect], client);
+ sendFbuMsg([info], [rect], client);
expect(client._cursor.change).to.have.been.calledOnce;
expect(client._cursor.change).to.have.been.calledWith(dot.rgbaPixels,
}
push32(rect, 0x00000000);
- send_fbu_msg([info], [rect], client);
+ sendFbuMsg([info], [rect], client);
expect(client._cursor.change).to.have.been.calledOnce;
expect(client._cursor.change).to.have.been.calledWith(dot.rgbaPixels,
push8(rect, data[i]);
}
- send_fbu_msg([{ x: 0, y: 0, width: 2, height: 2,
- encoding: 0x574d5664}],
- [rect], client);
+ sendFbuMsg([{ x: 0, y: 0, width: 2, height: 2,
+ encoding: 0x574d5664}],
+ [rect], client);
expect(client._FBU.rects).to.equal(0);
});
push8(rect, data[i]);
}
- send_fbu_msg([{ x: 0, y: 0, width: w, height: h,
- encoding: 0x574d5664}],
- [rect], client);
+ sendFbuMsg([{ x: 0, y: 0, width: w, height: h,
+ encoding: 0x574d5664}],
+ [rect], client);
// expect one FBU to remain unhandled
expect(client._FBU.rects).to.equal(1);
});
it('should update the cursor when type is classic', function () {
- let and_mask =
+ let andMask =
[0xff, 0xff, 0xff, 0xff, //Transparent
0xff, 0xff, 0xff, 0xff, //Transparent
0x00, 0x00, 0x00, 0x00, //Opaque
0xff, 0xff, 0xff, 0xff]; //Inverted
- let xor_mask =
+ let xorMask =
[0x00, 0x00, 0x00, 0x00, //Transparent
0x00, 0x00, 0x00, 0x00, //Transparent
0x11, 0x22, 0x33, 0x44, //Opaque
let h = 2;
//AND-mask
- for (let i = 0; i < and_mask.length; i++) {
- push8(rect, and_mask[i]);
+ for (let i = 0; i < andMask.length; i++) {
+ push8(rect, andMask[i]);
}
//XOR-mask
- for (let i = 0; i < xor_mask.length; i++) {
- push8(rect, xor_mask[i]);
+ for (let i = 0; i < xorMask.length; i++) {
+ push8(rect, xorMask[i]);
}
- let expected_rgba = [0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,
- 0x33, 0x22, 0x11, 0xff,
- 0x00, 0x00, 0x00, 0xff];
+ let expectedRgba = [0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0x33, 0x22, 0x11, 0xff,
+ 0x00, 0x00, 0x00, 0xff];
- send_fbu_msg([{ x: hotx, y: hoty,
- width: w, height: h,
- encoding: 0x574d5664}],
- [rect], client);
+ sendFbuMsg([{ x: hotx, y: hoty,
+ width: w, height: h,
+ encoding: 0x574d5664}],
+ [rect], client);
expect(client._cursor.change)
.to.have.been.calledOnce;
expect(client._cursor.change)
- .to.have.been.calledWith(expected_rgba,
+ .to.have.been.calledWith(expectedRgba,
hotx, hoty,
w, h);
});
push8(rect, data[i]);
}
- let expected_rgba = [0xee, 0x55, 0xff, 0x00,
- 0x00, 0xff, 0x00, 0xff,
- 0x00, 0xff, 0x00, 0x22,
- 0x00, 0xff, 0x00, 0x22,
- 0x00, 0xff, 0x00, 0x22,
- 0x00, 0x00, 0xff, 0xee];
+ let expectedRgba = [0xee, 0x55, 0xff, 0x00,
+ 0x00, 0xff, 0x00, 0xff,
+ 0x00, 0xff, 0x00, 0x22,
+ 0x00, 0xff, 0x00, 0x22,
+ 0x00, 0xff, 0x00, 0x22,
+ 0x00, 0x00, 0xff, 0xee];
- send_fbu_msg([{ x: hotx, y: hoty,
- width: w, height: h,
- encoding: 0x574d5664}],
- [rect], client);
+ sendFbuMsg([{ x: hotx, y: hoty,
+ width: w, height: h,
+ encoding: 0x574d5664}],
+ [rect], client);
expect(client._cursor.change)
.to.have.been.calledOnce;
expect(client._cursor.change)
- .to.have.been.calledWith(expected_rgba,
+ .to.have.been.calledWith(expectedRgba,
hotx, hoty,
w, h);
});
push8(rect, 0); // padding
client._cursor.change.resetHistory();
- send_fbu_msg([{ x: 0, y: 0, width: 2, height: 2,
- encoding: 0x574d5664}],
- [rect], client);
+ sendFbuMsg([{ x: 0, y: 0, width: 2, height: 2,
+ encoding: 0x574d5664}],
+ [rect], client);
expect(client._cursor.change)
.to.not.have.been.called;
});
it('should handle the last_rect pseudo-encoding', function () {
- send_fbu_msg([{ x: 0, y: 0, width: 0, height: 0, encoding: -224}], [[]], client, 100);
+ sendFbuMsg([{ x: 0, y: 0, width: 0, height: 0, encoding: -224}], [[]], client, 100);
expect(client._FBU.rects).to.equal(0);
});
const spy = sinon.spy();
client.addEventListener("desktopname", spy);
- send_fbu_msg([{ x: 0, y: 0, width: 0, height: 0, encoding: -307 }], [data], client);
+ sendFbuMsg([{ x: 0, y: 0, width: 0, height: 0, encoding: -307 }], [data], client);
- expect(client._fb_name).to.equal('som€ nam€');
+ expect(client._fbName).to.equal('som€ nam€');
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][0].detail.name).to.equal('som€ nam€');
});
it('should set the XVP version and fire the callback with the version on XVP_INIT', function () {
const spy = sinon.spy();
client.addEventListener("capabilities", spy);
- client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 1]));
- expect(client._rfb_xvp_ver).to.equal(10);
+ client._sock._websocket._receiveData(new Uint8Array([250, 0, 10, 1]));
+ expect(client._rfbXvpVer).to.equal(10);
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][0].detail.capabilities.power).to.be.true;
expect(client.capabilities.power).to.be.true;
it('should fail on unknown XVP message types', function () {
sinon.spy(client, "_fail");
- client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 237]));
+ client._sock._websocket._receiveData(new Uint8Array([250, 0, 10, 237]));
expect(client._fail).to.have.been.calledOnce;
});
});
describe('Normal Clipboard Handling Receive', function () {
it('should fire the clipboard callback with the retrieved text on ServerCutText', function () {
- const expected_str = 'cheese!';
+ const expectedStr = 'cheese!';
const data = [3, 0, 0, 0];
- push32(data, expected_str.length);
- for (let i = 0; i < expected_str.length; i++) { data.push(expected_str.charCodeAt(i)); }
+ push32(data, expectedStr.length);
+ for (let i = 0; i < expectedStr.length; i++) { data.push(expectedStr.charCodeAt(i)); }
const spy = sinon.spy();
client.addEventListener("clipboard", spy);
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
expect(spy).to.have.been.calledOnce;
- expect(spy.args[0][0].detail.text).to.equal(expected_str);
+ expect(spy.args[0][0].detail.text).to.equal(expectedStr);
});
});
push32(data, toUnsigned32bit(-12));
data = data.concat(flags);
data = data.concat(fileSizes);
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
// Check that we give an response caps when we receive one
expect(RFB.messages.extendedClipboardCaps).to.have.been.calledOnce;
push32(data, toUnsigned32bit(-8));
data = data.concat(flags);
data = data.concat(fileSizes);
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
});
describe('Handle Provide', function () {
const spy = sinon.spy();
client.addEventListener("clipboard", spy);
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][0].detail.text).to.equal(expectedData);
client.removeEventListener("clipboard", spy);
const spy = sinon.spy();
client.addEventListener("clipboard", spy);
- client._sock._websocket._receive_data(sendData);
+ client._sock._websocket._receiveData(sendData);
+ expect(spy).to.have.been.calledOnce;
+ expect(spy.args[0][0].detail.text).to.equal(expectedData);
+ client.removeEventListener("clipboard", spy);
+ });
+
+ it('should be able to handle large Provide messages', function () {
+ let expectedData = "hello".repeat(100000);
+ let data = [3, 0, 0, 0];
+ const flags = [0x10, 0x00, 0x00, 0x01];
+
+ let text = encodeUTF8(expectedData + "\0");
+
+ let deflatedText = deflateWithSize(text);
+
+ // How much data we are sending.
+ push32(data, toUnsigned32bit(-(4 + deflatedText.length)));
+
+ data = data.concat(flags);
+
+ let sendData = new Uint8Array(data.length + deflatedText.length);
+ sendData.set(data);
+ sendData.set(deflatedText, data.length);
+
+ const spy = sinon.spy();
+ client.addEventListener("clipboard", spy);
+
+ client._sock._websocket._receiveData(sendData);
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][0].detail.text).to.equal(expectedData);
client.removeEventListener("clipboard", spy);
data = data.concat(flags);
let expectedData = [0x01];
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
expect(RFB.messages.extendedClipboardRequest).to.have.been.calledOnce;
expect(RFB.messages.extendedClipboardRequest).to.have.been.calledWith(client._sock, expectedData);
data = data.concat(flags);
let expectedData = [];
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
expect(RFB.messages.extendedClipboardNotify).to.have.been.calledOnce;
expect(RFB.messages.extendedClipboardNotify).to.have.been.calledWith(client._sock, expectedData);
client.clipboardPasteFrom("HejHej");
RFB.messages.extendedClipboardNotify.resetHistory();
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
expect(RFB.messages.extendedClipboardNotify).to.have.been.calledOnce;
expect(RFB.messages.extendedClipboardNotify).to.have.been.calledWith(client._sock, expectedData);
client.clipboardPasteFrom("HejHej");
expect(RFB.messages.extendedClipboardProvide).to.not.have.been.called;
- client._sock._websocket._receive_data(new Uint8Array(data));
+ client._sock._websocket._receiveData(new Uint8Array(data));
expect(RFB.messages.extendedClipboardProvide).to.have.been.calledOnce;
expect(RFB.messages.extendedClipboardProvide).to.have.been.calledWith(client._sock, expectedData, ["HejHej"]);
it('should fire the bell callback on Bell', function () {
const spy = sinon.spy();
client.addEventListener("bell", spy);
- client._sock._websocket._receive_data(new Uint8Array([2]));
+ client._sock._websocket._receiveData(new Uint8Array([2]));
expect(spy).to.have.been.calledOnce;
});
it('should respond correctly to ServerFence', function () {
- const expected_msg = {_sQ: new Uint8Array(16), _sQlen: 0, flush: () => {}};
- const incoming_msg = {_sQ: new Uint8Array(16), _sQlen: 0, flush: () => {}};
+ const expectedMsg = {_sQ: new Uint8Array(16), _sQlen: 0, flush: () => {}};
+ const incomingMsg = {_sQ: new Uint8Array(16), _sQlen: 0, flush: () => {}};
const payload = "foo\x00ab9";
// ClientFence and ServerFence are identical in structure
- RFB.messages.clientFence(expected_msg, (1<<0) | (1<<1), payload);
- RFB.messages.clientFence(incoming_msg, 0xffffffff, payload);
+ RFB.messages.clientFence(expectedMsg, (1<<0) | (1<<1), payload);
+ RFB.messages.clientFence(incomingMsg, 0xffffffff, payload);
- client._sock._websocket._receive_data(incoming_msg._sQ);
+ client._sock._websocket._receiveData(incomingMsg._sQ);
- expect(client._sock).to.have.sent(expected_msg._sQ);
+ expect(client._sock).to.have.sent(expectedMsg._sQ);
- expected_msg._sQlen = 0;
- incoming_msg._sQlen = 0;
+ expectedMsg._sQlen = 0;
+ incomingMsg._sQlen = 0;
- RFB.messages.clientFence(expected_msg, (1<<0), payload);
- RFB.messages.clientFence(incoming_msg, (1<<0) | (1<<31), payload);
+ RFB.messages.clientFence(expectedMsg, (1<<0), payload);
+ RFB.messages.clientFence(incomingMsg, (1<<0) | (1<<31), payload);
- client._sock._websocket._receive_data(incoming_msg._sQ);
+ client._sock._websocket._receiveData(incomingMsg._sQ);
- expect(client._sock).to.have.sent(expected_msg._sQ);
+ expect(client._sock).to.have.sent(expectedMsg._sQ);
});
it('should enable continuous updates on first EndOfContinousUpdates', function () {
- const expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
+ const expectedMsg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
- RFB.messages.enableContinuousUpdates(expected_msg, true, 0, 0, 640, 20);
+ RFB.messages.enableContinuousUpdates(expectedMsg, true, 0, 0, 640, 20);
expect(client._enabledContinuousUpdates).to.be.false;
- client._sock._websocket._receive_data(new Uint8Array([150]));
+ client._sock._websocket._receiveData(new Uint8Array([150]));
expect(client._enabledContinuousUpdates).to.be.true;
- expect(client._sock).to.have.sent(expected_msg._sQ);
+ expect(client._sock).to.have.sent(expectedMsg._sQ);
});
it('should disable continuous updates on subsequent EndOfContinousUpdates', function () {
client._enabledContinuousUpdates = true;
client._supportsContinuousUpdates = true;
- client._sock._websocket._receive_data(new Uint8Array([150]));
+ client._sock._websocket._receiveData(new Uint8Array([150]));
expect(client._enabledContinuousUpdates).to.be.false;
});
it('should update continuous updates on resize', function () {
- const expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
- RFB.messages.enableContinuousUpdates(expected_msg, true, 0, 0, 90, 700);
+ const expectedMsg = {_sQ: new Uint8Array(10), _sQlen: 0, flush: () => {}};
+ RFB.messages.enableContinuousUpdates(expectedMsg, true, 0, 0, 90, 700);
client._resize(450, 160);
- expect(client._sock._websocket._get_sent_data()).to.have.length(0);
+ expect(client._sock._websocket._getSentData()).to.have.length(0);
client._enabledContinuousUpdates = true;
client._resize(90, 700);
- expect(client._sock).to.have.sent(expected_msg._sQ);
+ expect(client._sock).to.have.sent(expectedMsg._sQ);
});
it('should fail on an unknown message type', function () {
sinon.spy(client, "_fail");
- client._sock._websocket._receive_data(new Uint8Array([87]));
+ client._sock._websocket._receiveData(new Uint8Array([87]));
expect(client._fail).to.have.been.calledOnce;
});
});
describe('Asynchronous Events', function () {
let client;
+ let pointerEvent;
+ let keyEvent;
+ let qemuKeyEvent;
+
beforeEach(function () {
- client = make_rfb();
+ client = makeRFB();
+ client._display.resize(100, 100);
+
+ // We need to disable this as focusing the canvas will
+ // cause the browser to scoll to it, messing up our
+ // client coordinate calculations
+ client.focusOnClick = false;
+
+ pointerEvent = sinon.spy(RFB.messages, 'pointerEvent');
+ keyEvent = sinon.spy(RFB.messages, 'keyEvent');
+ qemuKeyEvent = sinon.spy(RFB.messages, 'QEMUExtendedKeyEvent');
});
- describe('Mouse event handlers', function () {
+ afterEach(function () {
+ pointerEvent.restore();
+ keyEvent.restore();
+ qemuKeyEvent.restore();
+ });
+
+ function elementToClient(x, y) {
+ let res = { x: 0, y: 0 };
+
+ let bounds = client._canvas.getBoundingClientRect();
+
+ /*
+ * If the canvas is on a fractional position we will calculate
+ * a fractional mouse position. But that gets truncated when we
+ * send the event, AND the same thing happens in RFB when it
+ * generates the PointerEvent message. To compensate for that
+ * fact we round the value upwards here.
+ */
+ res.x = Math.ceil(bounds.left + x);
+ res.y = Math.ceil(bounds.top + y);
+
+ return res;
+ }
+
+ describe('Mouse Events', function () {
+ function sendMouseMoveEvent(x, y) {
+ let pos = elementToClient(x, y);
+ let ev;
+
+ ev = new MouseEvent('mousemove',
+ { 'screenX': pos.x + window.screenX,
+ 'screenY': pos.y + window.screenY,
+ 'clientX': pos.x,
+ 'clientY': pos.y });
+ client._canvas.dispatchEvent(ev);
+ }
+
+ function sendMouseButtonEvent(x, y, down, button) {
+ let pos = elementToClient(x, y);
+ let ev;
+
+ ev = new MouseEvent(down ? 'mousedown' : 'mouseup',
+ { 'screenX': pos.x + window.screenX,
+ 'screenY': pos.y + window.screenY,
+ 'clientX': pos.x,
+ 'clientY': pos.y,
+ 'button': button,
+ 'buttons': 1 << button });
+ client._canvas.dispatchEvent(ev);
+ }
+
it('should not send button messages in view-only mode', function () {
client._viewOnly = true;
- sinon.spy(client._sock, 'flush');
- client._handleMouseButton(0, 0, 1, 0x001);
- expect(client._sock.flush).to.not.have.been.called;
+ sendMouseButtonEvent(10, 10, true, 0);
+ clock.tick(50);
+ expect(pointerEvent).to.not.have.been.called;
});
it('should not send movement messages in view-only mode', function () {
client._viewOnly = true;
- sinon.spy(client._sock, 'flush');
- client._handleMouseMove(0, 0);
- expect(client._sock.flush).to.not.have.been.called;
+ sendMouseMoveEvent(10, 10);
+ clock.tick(50);
+ expect(pointerEvent).to.not.have.been.called;
});
- it('should send a pointer event on mouse button presses', function () {
- client._handleMouseButton(10, 12, 1, 0x001);
- const pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
- RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x001);
- expect(client._sock).to.have.sent(pointer_msg._sQ);
- });
+ it('should handle left mouse button', function () {
+ sendMouseButtonEvent(10, 10, true, 0);
- it('should send a mask of 1 on mousedown', function () {
- client._handleMouseButton(10, 12, 1, 0x001);
- const pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
- RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x001);
- expect(client._sock).to.have.sent(pointer_msg._sQ);
- });
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 10, 10, 0x1);
+ pointerEvent.resetHistory();
- it('should send a mask of 0 on mouseup', function () {
- client._mouse_buttonMask = 0x001;
- client._handleMouseButton(10, 12, 0, 0x001);
- const pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
- RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x000);
- expect(client._sock).to.have.sent(pointer_msg._sQ);
- });
+ sendMouseButtonEvent(10, 10, false, 0);
- it('should send a pointer event on mouse movement', function () {
- client._handleMouseMove(10, 12);
- const pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
- RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x000);
- expect(client._sock).to.have.sent(pointer_msg._sQ);
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 10, 10, 0x0);
});
- it('should set the button mask so that future mouse movements use it', function () {
- client._handleMouseButton(10, 12, 1, 0x010);
- client._handleMouseMove(13, 9);
- const pointer_msg = {_sQ: new Uint8Array(12), _sQlen: 0, flush: () => {}};
- RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x010);
- RFB.messages.pointerEvent(pointer_msg, 13, 9, 0x010);
- expect(client._sock).to.have.sent(pointer_msg._sQ);
- });
- });
+ it('should handle middle mouse button', function () {
+ sendMouseButtonEvent(10, 10, true, 1);
- describe('Keyboard Event Handlers', function () {
- it('should send a key message on a key press', function () {
- client._handleKeyEvent(0x41, 'KeyA', true);
- const key_msg = {_sQ: new Uint8Array(8), _sQlen: 0, flush: () => {}};
- RFB.messages.keyEvent(key_msg, 0x41, 1);
- expect(client._sock).to.have.sent(key_msg._sQ);
- });
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 10, 10, 0x2);
+ pointerEvent.resetHistory();
- it('should not send messages in view-only mode', function () {
- client._viewOnly = true;
- sinon.spy(client._sock, 'flush');
- client._handleKeyEvent('a', 'KeyA', true);
- expect(client._sock.flush).to.not.have.been.called;
- });
- });
+ sendMouseButtonEvent(10, 10, false, 1);
- describe('WebSocket event handlers', function () {
- // message events
- it('should do nothing if we receive an empty message and have nothing in the queue', function () {
- client._normal_msg = sinon.spy();
- client._sock._websocket._receive_data(new Uint8Array([]));
- expect(client._normal_msg).to.not.have.been.called;
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 10, 10, 0x0);
});
- it('should handle a message in the connected state as a normal message', function () {
- client._normal_msg = sinon.spy();
- client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
- expect(client._normal_msg).to.have.been.called;
+ it('should handle right mouse button', function () {
+ sendMouseButtonEvent(10, 10, true, 2);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 10, 10, 0x4);
+ pointerEvent.resetHistory();
+
+ sendMouseButtonEvent(10, 10, false, 2);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 10, 10, 0x0);
});
- it('should handle a message in any non-disconnected/failed state like an init message', function () {
- client._rfb_connection_state = 'connecting';
- 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.called;
+ it('should handle multiple mouse buttons', function () {
+ sendMouseButtonEvent(10, 10, true, 0);
+ sendMouseButtonEvent(10, 10, true, 2);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0x1);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0x5);
+
+ pointerEvent.resetHistory();
+
+ sendMouseButtonEvent(10, 10, false, 0);
+ sendMouseButtonEvent(10, 10, false, 2);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0x4);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0x0);
});
- it('should process all normal messages directly', function () {
- const spy = sinon.spy();
- client.addEventListener("bell", spy);
- client._sock._websocket._receive_data(new Uint8Array([0x02, 0x02]));
- expect(spy).to.have.been.calledTwice;
+ it('should handle mouse movement', function () {
+ sendMouseMoveEvent(50, 70);
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 50, 70, 0x0);
+ });
+
+ it('should handle click and drag', function () {
+ sendMouseButtonEvent(10, 10, true, 0);
+ sendMouseMoveEvent(50, 70);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0x1);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 50, 70, 0x1);
+
+ pointerEvent.resetHistory();
+
+ sendMouseButtonEvent(50, 70, false, 0);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 50, 70, 0x0);
+ });
+
+ describe('Event Aggregation', function () {
+ it('should send a single pointer event on mouse movement', function () {
+ sendMouseMoveEvent(50, 70);
+ clock.tick(100);
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 50, 70, 0x0);
+ });
+
+ it('should delay one move if two events are too close', function () {
+ sendMouseMoveEvent(18, 30);
+ sendMouseMoveEvent(20, 50);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 18, 30, 0x0);
+ pointerEvent.resetHistory();
+
+ clock.tick(100);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 50, 0x0);
+ });
+
+ it('should only send first and last move of many close events', function () {
+ sendMouseMoveEvent(18, 30);
+ sendMouseMoveEvent(20, 50);
+ sendMouseMoveEvent(21, 55);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 18, 30, 0x0);
+ pointerEvent.resetHistory();
+
+ clock.tick(100);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 21, 55, 0x0);
+ });
+
+ // We selected the 17ms since that is ~60 FPS
+ it('should send move events every 17 ms', function () {
+ sendMouseMoveEvent(1, 10); // instant send
+ clock.tick(10);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 1, 10, 0x0);
+ pointerEvent.resetHistory();
+
+ sendMouseMoveEvent(2, 20); // delayed
+ clock.tick(10); // timeout send
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 2, 20, 0x0);
+ pointerEvent.resetHistory();
+
+ sendMouseMoveEvent(3, 30); // delayed
+ clock.tick(10);
+ sendMouseMoveEvent(4, 40); // delayed
+ clock.tick(10); // timeout send
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 4, 40, 0x0);
+ pointerEvent.resetHistory();
+
+ sendMouseMoveEvent(5, 50); // delayed
+
+ expect(pointerEvent).to.not.have.been.called;
+ });
+
+ it('should send waiting move events before a button press', function () {
+ sendMouseMoveEvent(13, 9);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 13, 9, 0x0);
+ pointerEvent.resetHistory();
+
+ sendMouseMoveEvent(20, 70);
+
+ expect(pointerEvent).to.not.have.been.called;
+
+ sendMouseButtonEvent(20, 70, true, 0);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 70, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 70, 0x1);
+ });
+
+ it('should send move events with enough time apart normally', function () {
+ sendMouseMoveEvent(58, 60);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 58, 60, 0x0);
+ pointerEvent.resetHistory();
+
+ clock.tick(20);
+
+ sendMouseMoveEvent(25, 60);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 25, 60, 0x0);
+ pointerEvent.resetHistory();
+ });
+
+ it('should not send waiting move events if disconnected', function () {
+ sendMouseMoveEvent(88, 99);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 88, 99, 0x0);
+ pointerEvent.resetHistory();
+
+ sendMouseMoveEvent(66, 77);
+ client.disconnect();
+ clock.tick(20);
+
+ expect(pointerEvent).to.not.have.been.called;
+ });
+ });
+
+ it.skip('should block click events', function () {
+ /* FIXME */
+ });
+
+ it.skip('should block contextmenu events', function () {
+ /* FIXME */
+ });
+ });
+
+ describe('Wheel Events', function () {
+ function sendWheelEvent(x, y, dx, dy, mode=0) {
+ let pos = elementToClient(x, y);
+ let ev;
+
+ ev = new WheelEvent('wheel',
+ { 'screenX': pos.x + window.screenX,
+ 'screenY': pos.y + window.screenY,
+ 'clientX': pos.x,
+ 'clientY': pos.y,
+ 'deltaX': dx,
+ 'deltaY': dy,
+ 'deltaMode': mode });
+ client._canvas.dispatchEvent(ev);
+ }
+
+ it('should handle wheel up event', function () {
+ sendWheelEvent(10, 10, 0, -50);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 1<<3);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0);
+ });
+
+ it('should handle wheel down event', function () {
+ sendWheelEvent(10, 10, 0, 50);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 1<<4);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0);
+ });
+
+ it('should handle wheel left event', function () {
+ sendWheelEvent(10, 10, -50, 0);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 1<<5);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0);
+ });
+
+ it('should handle wheel right event', function () {
+ sendWheelEvent(10, 10, 50, 0);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 1<<6);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0);
+ });
+
+ it('should ignore wheel when in view only', function () {
+ client._viewOnly = true;
+
+ sendWheelEvent(10, 10, 50, 0);
+
+ expect(pointerEvent).to.not.have.been.called;
+ });
+
+ it('should accumulate wheel events if small enough', function () {
+ sendWheelEvent(10, 10, 0, 20);
+ sendWheelEvent(10, 10, 0, 20);
+
+ expect(pointerEvent).to.not.have.been.called;
+
+ sendWheelEvent(10, 10, 0, 20);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 1<<4);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0);
+ });
+
+ it('should not accumulate large wheel events', function () {
+ sendWheelEvent(10, 10, 0, 400);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 1<<4);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0);
+ });
+
+ it('should handle line based wheel event', function () {
+ sendWheelEvent(10, 10, 0, 3, 1);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 1<<4);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0);
+ });
+
+ it('should handle page based wheel event', function () {
+ sendWheelEvent(10, 10, 0, 3, 2);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 10, 1<<4);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 10, 0);
+ });
+ });
+
+ describe('Keyboard Events', function () {
+ it('should send a key message on a key press', function () {
+ client._handleKeyEvent(0x41, 'KeyA', true);
+ const keyMsg = {_sQ: new Uint8Array(8), _sQlen: 0, flush: () => {}};
+ RFB.messages.keyEvent(keyMsg, 0x41, 1);
+ expect(client._sock).to.have.sent(keyMsg._sQ);
+ });
+
+ it('should not send messages in view-only mode', function () {
+ client._viewOnly = true;
+ sinon.spy(client._sock, 'flush');
+ client._handleKeyEvent('a', 'KeyA', true);
+ expect(client._sock.flush).to.not.have.been.called;
+ });
+ });
+
+ describe('Gesture event handlers', function () {
+ function gestureStart(gestureType, x, y,
+ magnitudeX = 0, magnitudeY = 0) {
+ let pos = elementToClient(x, y);
+ let detail = {type: gestureType, clientX: pos.x, clientY: pos.y};
+
+ detail.magnitudeX = magnitudeX;
+ detail.magnitudeY = magnitudeY;
+
+ let ev = new CustomEvent('gesturestart', { detail: detail });
+ client._canvas.dispatchEvent(ev);
+ }
+
+ function gestureMove(gestureType, x, y,
+ magnitudeX = 0, magnitudeY = 0) {
+ let pos = elementToClient(x, y);
+ let detail = {type: gestureType, clientX: pos.x, clientY: pos.y};
+
+ detail.magnitudeX = magnitudeX;
+ detail.magnitudeY = magnitudeY;
+
+ let ev = new CustomEvent('gesturemove', { detail: detail });
+ client._canvas.dispatchEvent(ev);
+ }
+
+ function gestureEnd(gestureType, x, y) {
+ let pos = elementToClient(x, y);
+ let detail = {type: gestureType, clientX: pos.x, clientY: pos.y};
+ let ev = new CustomEvent('gestureend', { detail: detail });
+ client._canvas.dispatchEvent(ev);
+ }
+
+ describe('Gesture onetap', function () {
+ it('should handle onetap events', function () {
+ let bmask = 0x1;
+
+ gestureStart('onetap', 20, 40);
+ gestureEnd('onetap', 20, 40);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ });
+
+ it('should keep same position for multiple onetap events', function () {
+ let bmask = 0x1;
+
+ gestureStart('onetap', 20, 40);
+ gestureEnd('onetap', 20, 40);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureStart('onetap', 20, 50);
+ gestureEnd('onetap', 20, 50);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureStart('onetap', 30, 50);
+ gestureEnd('onetap', 30, 50);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ });
+
+ it('should not keep same position for onetap events when too far apart', function () {
+ let bmask = 0x1;
+
+ gestureStart('onetap', 20, 40);
+ gestureEnd('onetap', 20, 40);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureStart('onetap', 80, 95);
+ gestureEnd('onetap', 80, 95);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 80, 95, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 80, 95, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 80, 95, 0x0);
+ });
+
+ it('should not keep same position for onetap events when enough time inbetween', function () {
+ let bmask = 0x1;
+
+ gestureStart('onetap', 10, 20);
+ gestureEnd('onetap', 10, 20);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 20, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 20, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 10, 20, 0x0);
+
+ pointerEvent.resetHistory();
+ this.clock.tick(1500);
+
+ gestureStart('onetap', 15, 20);
+ gestureEnd('onetap', 15, 20);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 15, 20, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 15, 20, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 15, 20, 0x0);
+
+ pointerEvent.resetHistory();
+ });
+ });
+
+ describe('Gesture twotap', function () {
+ it('should handle gesture twotap events', function () {
+ let bmask = 0x4;
+
+ gestureStart("twotap", 20, 40);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ });
+
+ it('should keep same position for multiple twotap events', function () {
+ let bmask = 0x4;
+
+ for (let offset = 0;offset < 30;offset += 10) {
+ pointerEvent.resetHistory();
+
+ gestureStart('twotap', 20, 40 + offset);
+ gestureEnd('twotap', 20, 40 + offset);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ }
+ });
+ });
+
+ describe('Gesture threetap', function () {
+ it('should handle gesture start for threetap events', function () {
+ let bmask = 0x2;
+
+ gestureStart("threetap", 20, 40);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ });
+
+ it('should keep same position for multiple threetap events', function () {
+ let bmask = 0x2;
+
+ for (let offset = 0;offset < 30;offset += 10) {
+ pointerEvent.resetHistory();
+
+ gestureStart('threetap', 20, 40 + offset);
+ gestureEnd('threetap', 20, 40 + offset);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ }
+ });
+ });
+
+ describe('Gesture drag', function () {
+ it('should handle gesture drag events', function () {
+ let bmask = 0x1;
+
+ gestureStart('drag', 20, 40);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('drag', 30, 50);
+ clock.tick(50);
+
+ expect(pointerEvent).to.have.been.calledOnce;
+ expect(pointerEvent).to.have.been.calledWith(client._sock,
+ 30, 50, bmask);
+
+ pointerEvent.resetHistory();
+
+ gestureEnd('drag', 30, 50);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 30, 50, bmask);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 30, 50, 0x0);
+ });
+ });
+
+ describe('Gesture long press', function () {
+ it('should handle long press events', function () {
+ let bmask = 0x4;
+
+ gestureStart('longpress', 20, 40);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ pointerEvent.resetHistory();
+
+ gestureMove('longpress', 40, 60);
+ clock.tick(50);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 40, 60, bmask);
+
+ pointerEvent.resetHistory();
+
+ gestureEnd('longpress', 40, 60);
+
+ expect(pointerEvent).to.have.been.calledTwice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 40, 60, bmask);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 40, 60, 0x0);
+ });
+ });
+
+ describe('Gesture twodrag', function () {
+ it('should handle gesture twodrag up events', function () {
+ let bmask = 0x10; // Button mask for scroll down
+
+ gestureStart('twodrag', 20, 40, 0, 0);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('twodrag', 20, 40, 0, -60);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ });
+
+ it('should handle gesture twodrag down events', function () {
+ let bmask = 0x8; // Button mask for scroll up
+
+ gestureStart('twodrag', 20, 40, 0, 0);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('twodrag', 20, 40, 0, 60);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ });
+
+ it('should handle gesture twodrag right events', function () {
+ let bmask = 0x20; // Button mask for scroll right
+
+ gestureStart('twodrag', 20, 40, 0, 0);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('twodrag', 20, 40, 60, 0);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ });
+
+ it('should handle gesture twodrag left events', function () {
+ let bmask = 0x40; // Button mask for scroll left
+
+ gestureStart('twodrag', 20, 40, 0, 0);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('twodrag', 20, 40, -60, 0);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ });
+
+ it('should handle gesture twodrag diag events', function () {
+ let scrlUp = 0x8; // Button mask for scroll up
+ let scrlRight = 0x20; // Button mask for scroll right
+
+ gestureStart('twodrag', 20, 40, 0, 0);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('twodrag', 20, 40, 60, 60);
+
+ expect(pointerEvent).to.have.been.callCount(5);
+ expect(pointerEvent.getCall(0)).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.getCall(1)).to.have.been.calledWith(client._sock,
+ 20, 40, scrlUp);
+ expect(pointerEvent.getCall(2)).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.getCall(3)).to.have.been.calledWith(client._sock,
+ 20, 40, scrlRight);
+ expect(pointerEvent.getCall(4)).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ });
+
+ it('should handle multiple small gesture twodrag events', function () {
+ let bmask = 0x8; // Button mask for scroll up
+
+ gestureStart('twodrag', 20, 40, 0, 0);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('twodrag', 20, 40, 0, 10);
+ clock.tick(50);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('twodrag', 20, 40, 0, 20);
+ clock.tick(50);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('twodrag', 20, 40, 0, 60);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ });
+
+ it('should handle large gesture twodrag events', function () {
+ let bmask = 0x8; // Button mask for scroll up
+
+ gestureStart('twodrag', 30, 50, 0, 0);
+
+ expect(pointerEvent).
+ to.have.been.calledOnceWith(client._sock, 30, 50, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('twodrag', 30, 50, 0, 200);
+
+ expect(pointerEvent).to.have.callCount(7);
+ expect(pointerEvent.getCall(0)).to.have.been.calledWith(client._sock,
+ 30, 50, 0x0);
+ expect(pointerEvent.getCall(1)).to.have.been.calledWith(client._sock,
+ 30, 50, bmask);
+ expect(pointerEvent.getCall(2)).to.have.been.calledWith(client._sock,
+ 30, 50, 0x0);
+ expect(pointerEvent.getCall(3)).to.have.been.calledWith(client._sock,
+ 30, 50, bmask);
+ expect(pointerEvent.getCall(4)).to.have.been.calledWith(client._sock,
+ 30, 50, 0x0);
+ expect(pointerEvent.getCall(5)).to.have.been.calledWith(client._sock,
+ 30, 50, bmask);
+ expect(pointerEvent.getCall(6)).to.have.been.calledWith(client._sock,
+ 30, 50, 0x0);
+ });
+ });
+
+ describe('Gesture pinch', function () {
+ it('should handle gesture pinch in events', function () {
+ let keysym = KeyTable.XK_Control_L;
+ let bmask = 0x10; // Button mask for scroll down
+
+ gestureStart('pinch', 20, 40, 90, 90);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+ expect(keyEvent).to.not.have.been.called;
+
+ pointerEvent.resetHistory();
+
+ gestureMove('pinch', 20, 40, 30, 30);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+
+ expect(keyEvent).to.have.been.calledTwice;
+ expect(keyEvent.firstCall).to.have.been.calledWith(client._sock,
+ keysym, 1);
+ expect(keyEvent.secondCall).to.have.been.calledWith(client._sock,
+ keysym, 0);
+
+ expect(keyEvent.firstCall).to.have.been.calledBefore(pointerEvent.secondCall);
+ expect(keyEvent.lastCall).to.have.been.calledAfter(pointerEvent.lastCall);
+
+ pointerEvent.resetHistory();
+ keyEvent.resetHistory();
+
+ gestureEnd('pinch', 20, 40);
+
+ expect(pointerEvent).to.not.have.been.called;
+ expect(keyEvent).to.not.have.been.called;
+ });
+
+ it('should handle gesture pinch out events', function () {
+ let keysym = KeyTable.XK_Control_L;
+ let bmask = 0x8; // Button mask for scroll up
+
+ gestureStart('pinch', 10, 20, 10, 20);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 10, 20, 0x0);
+ expect(keyEvent).to.not.have.been.called;
+
+ pointerEvent.resetHistory();
+
+ gestureMove('pinch', 10, 20, 70, 80);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 10, 20, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 10, 20, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 10, 20, 0x0);
+
+ expect(keyEvent).to.have.been.calledTwice;
+ expect(keyEvent.firstCall).to.have.been.calledWith(client._sock,
+ keysym, 1);
+ expect(keyEvent.secondCall).to.have.been.calledWith(client._sock,
+ keysym, 0);
+
+ expect(keyEvent.firstCall).to.have.been.calledBefore(pointerEvent.secondCall);
+ expect(keyEvent.lastCall).to.have.been.calledAfter(pointerEvent.lastCall);
+
+ pointerEvent.resetHistory();
+ keyEvent.resetHistory();
+
+ gestureEnd('pinch', 10, 20);
+
+ expect(pointerEvent).to.not.have.been.called;
+ expect(keyEvent).to.not.have.been.called;
+ });
+
+ it('should handle large gesture pinch', function () {
+ let keysym = KeyTable.XK_Control_L;
+ let bmask = 0x10; // Button mask for scroll down
+
+ gestureStart('pinch', 20, 40, 150, 150);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+ expect(keyEvent).to.not.have.been.called;
+
+ pointerEvent.resetHistory();
+
+ gestureMove('pinch', 20, 40, 30, 30);
+
+ expect(pointerEvent).to.have.been.callCount(5);
+ expect(pointerEvent.getCall(0)).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.getCall(1)).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.getCall(2)).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.getCall(3)).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.getCall(4)).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+
+ expect(keyEvent).to.have.been.calledTwice;
+ expect(keyEvent.firstCall).to.have.been.calledWith(client._sock,
+ keysym, 1);
+ expect(keyEvent.secondCall).to.have.been.calledWith(client._sock,
+ keysym, 0);
+
+ expect(keyEvent.firstCall).to.have.been.calledBefore(pointerEvent.secondCall);
+ expect(keyEvent.lastCall).to.have.been.calledAfter(pointerEvent.lastCall);
+
+ pointerEvent.resetHistory();
+ keyEvent.resetHistory();
+
+ gestureEnd('pinch', 20, 40);
+
+ expect(pointerEvent).to.not.have.been.called;
+ expect(keyEvent).to.not.have.been.called;
+ });
+
+ it('should handle multiple small gesture pinch out events', function () {
+ let keysym = KeyTable.XK_Control_L;
+ let bmask = 0x8; // Button mask for scroll down
+
+ gestureStart('pinch', 20, 40, 0, 10);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+ expect(keyEvent).to.not.have.been.called;
+
+ pointerEvent.resetHistory();
+
+ gestureMove('pinch', 20, 40, 0, 30);
+ clock.tick(50);
+
+ expect(pointerEvent).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+
+ gestureMove('pinch', 20, 40, 0, 60);
+ clock.tick(50);
+
+ expect(pointerEvent).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+
+ pointerEvent.resetHistory();
+ keyEvent.resetHistory();
+
+ gestureMove('pinch', 20, 40, 0, 90);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+
+ expect(keyEvent).to.have.been.calledTwice;
+ expect(keyEvent.firstCall).to.have.been.calledWith(client._sock,
+ keysym, 1);
+ expect(keyEvent.secondCall).to.have.been.calledWith(client._sock,
+ keysym, 0);
+
+ expect(keyEvent.firstCall).to.have.been.calledBefore(pointerEvent.secondCall);
+ expect(keyEvent.lastCall).to.have.been.calledAfter(pointerEvent.lastCall);
+
+ pointerEvent.resetHistory();
+ keyEvent.resetHistory();
+
+ gestureEnd('pinch', 20, 40);
+
+ expect(keyEvent).to.not.have.been.called;
+ });
+
+ it('should send correct key control code', function () {
+ let keysym = KeyTable.XK_Control_L;
+ let code = 0x1d;
+ let bmask = 0x10; // Button mask for scroll down
+
+ client._qemuExtKeyEventSupported = true;
+
+ gestureStart('pinch', 20, 40, 90, 90);
+
+ expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+ 20, 40, 0x0);
+ expect(qemuKeyEvent).to.not.have.been.called;
+
+ pointerEvent.resetHistory();
+
+ gestureMove('pinch', 20, 40, 30, 30);
+
+ expect(pointerEvent).to.have.been.calledThrice;
+ expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+ expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+ 20, 40, bmask);
+ expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+ 20, 40, 0x0);
+
+ expect(qemuKeyEvent).to.have.been.calledTwice;
+ expect(qemuKeyEvent.firstCall).to.have.been.calledWith(client._sock,
+ keysym,
+ true,
+ code);
+ expect(qemuKeyEvent.secondCall).to.have.been.calledWith(client._sock,
+ keysym,
+ false,
+ code);
+
+ expect(qemuKeyEvent.firstCall).to.have.been.calledBefore(pointerEvent.secondCall);
+ expect(qemuKeyEvent.lastCall).to.have.been.calledAfter(pointerEvent.lastCall);
+
+ pointerEvent.resetHistory();
+ qemuKeyEvent.resetHistory();
+
+ gestureEnd('pinch', 20, 40);
+
+ expect(pointerEvent).to.not.have.been.called;
+ expect(qemuKeyEvent).to.not.have.been.called;
+ });
+ });
+ });
+
+ describe('WebSocket Events', function () {
+ // message events
+ it('should do nothing if we receive an empty message and have nothing in the queue', function () {
+ client._normalMsg = sinon.spy();
+ client._sock._websocket._receiveData(new Uint8Array([]));
+ expect(client._normalMsg).to.not.have.been.called;
+ });
+
+ it('should handle a message in the connected state as a normal message', function () {
+ client._normalMsg = sinon.spy();
+ client._sock._websocket._receiveData(new Uint8Array([1, 2, 3]));
+ expect(client._normalMsg).to.have.been.called;
+ });
+
+ it('should handle a message in any non-disconnected/failed state like an init message', function () {
+ client._rfbConnectionState = 'connecting';
+ client._rfbInitState = 'ProtocolVersion';
+ client._initMsg = sinon.spy();
+ client._sock._websocket._receiveData(new Uint8Array([1, 2, 3]));
+ expect(client._initMsg).to.have.been.called;
+ });
+
+ it('should process all normal messages directly', function () {
+ const spy = sinon.spy();
+ client.addEventListener("bell", spy);
+ client._sock._websocket._receiveData(new Uint8Array([0x02, 0x02]));
+ expect(spy).to.have.been.calledTwice;
});
// open events
client = new RFB(document.createElement('div'), 'wss://host:8675');
this.clock.tick();
client._sock._websocket._open();
- expect(client._rfb_init_state).to.equal('ProtocolVersion');
+ expect(client._rfbInitState).to.equal('ProtocolVersion');
});
it('should fail if we are not currently ready to connect and we get an "open" event', function () {
sinon.spy(client, "_fail");
- client._rfb_connection_state = 'connected';
+ client._rfbConnectionState = 'connected';
client._sock._websocket._open();
expect(client._fail).to.have.been.calledOnce;
});
const real = client._sock._websocket.close;
client._sock._websocket.close = () => {};
client.disconnect();
- expect(client._rfb_connection_state).to.equal('disconnecting');
+ expect(client._rfbConnectionState).to.equal('disconnecting');
client._sock._websocket.close = real;
client._sock._websocket.close();
- expect(client._rfb_connection_state).to.equal('disconnected');
+ expect(client._rfbConnectionState).to.equal('disconnected');
});
it('should fail if we get a close event while connecting', function () {
sinon.spy(client, "_fail");
- client._rfb_connection_state = 'connecting';
+ client._rfbConnectionState = 'connecting';
client._sock._websocket.close();
expect(client._fail).to.have.been.calledOnce;
});
// error events do nothing
});
});
+
+ describe('Quality level setting', function () {
+ const defaultQuality = 6;
+
+ let client;
+
+ beforeEach(function () {
+ client = makeRFB();
+ sinon.spy(RFB.messages, "clientEncodings");
+ });
+
+ afterEach(function () {
+ RFB.messages.clientEncodings.restore();
+ });
+
+ it(`should equal ${defaultQuality} by default`, function () {
+ expect(client._qualityLevel).to.equal(defaultQuality);
+ });
+
+ it('should ignore non-integers when set', function () {
+ client.qualityLevel = '1';
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.qualityLevel = 1.5;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.qualityLevel = null;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.qualityLevel = undefined;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.qualityLevel = {};
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+ });
+
+ it('should ignore integers out of range [0, 9]', function () {
+ client.qualityLevel = -1;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.qualityLevel = 10;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+ });
+
+ it('should send clientEncodings with new quality value', function () {
+ let newQuality;
+
+ newQuality = 8;
+ client.qualityLevel = newQuality;
+ expect(client.qualityLevel).to.equal(newQuality);
+ expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
+ expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.pseudoEncodingQualityLevel0 + newQuality);
+ });
+
+ it('should not send clientEncodings if quality is the same', function () {
+ let newQuality;
+
+ newQuality = 2;
+ client.qualityLevel = newQuality;
+ expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
+ expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.pseudoEncodingQualityLevel0 + newQuality);
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.qualityLevel = newQuality;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+ });
+
+ it('should not send clientEncodings if not in connected state', function () {
+ let newQuality;
+
+ client._rfbConnectionState = '';
+ newQuality = 2;
+ client.qualityLevel = newQuality;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client._rfbConnectionState = 'connnecting';
+ newQuality = 6;
+ client.qualityLevel = newQuality;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client._rfbConnectionState = 'connected';
+ newQuality = 5;
+ client.qualityLevel = newQuality;
+ expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
+ expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.pseudoEncodingQualityLevel0 + newQuality);
+ });
+ });
+
+ describe('Compression level setting', function () {
+ const defaultCompression = 2;
+
+ let client;
+
+ beforeEach(function () {
+ client = makeRFB();
+ sinon.spy(RFB.messages, "clientEncodings");
+ });
+
+ afterEach(function () {
+ RFB.messages.clientEncodings.restore();
+ });
+
+ it(`should equal ${defaultCompression} by default`, function () {
+ expect(client._compressionLevel).to.equal(defaultCompression);
+ });
+
+ it('should ignore non-integers when set', function () {
+ client.compressionLevel = '1';
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.compressionLevel = 1.5;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.compressionLevel = null;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.compressionLevel = undefined;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.compressionLevel = {};
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+ });
+
+ it('should ignore integers out of range [0, 9]', function () {
+ client.compressionLevel = -1;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.compressionLevel = 10;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+ });
+
+ it('should send clientEncodings with new compression value', function () {
+ let newCompression;
+
+ newCompression = 5;
+ client.compressionLevel = newCompression;
+ expect(client.compressionLevel).to.equal(newCompression);
+ expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
+ expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.pseudoEncodingCompressLevel0 + newCompression);
+ });
+
+ it('should not send clientEncodings if compression is the same', function () {
+ let newCompression;
+
+ newCompression = 9;
+ client.compressionLevel = newCompression;
+ expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
+ expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.pseudoEncodingCompressLevel0 + newCompression);
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client.compressionLevel = newCompression;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+ });
+
+ it('should not send clientEncodings if not in connected state', function () {
+ let newCompression;
+
+ client._rfbConnectionState = '';
+ newCompression = 7;
+ client.compressionLevel = newCompression;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client._rfbConnectionState = 'connnecting';
+ newCompression = 6;
+ client.compressionLevel = newCompression;
+ expect(RFB.messages.clientEncodings).to.not.have.been.called;
+
+ RFB.messages.clientEncodings.resetHistory();
+
+ client._rfbConnectionState = 'connected';
+ newCompression = 5;
+ client.compressionLevel = newCompression;
+ expect(RFB.messages.clientEncodings).to.have.been.calledOnce;
+ expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.pseudoEncodingCompressLevel0 + newCompression);
+ });
+ });
});
describe('RFB messages', function () {