From 6fd81e741b5a088c6e7722dfc8f8f799a405a474 Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Fri, 31 Jan 2020 17:39:17 -0500 Subject: [PATCH] Update tests --- tests/assertions.js | 4 +- tests/test.rfb.js | 160 +++++++++++++++++++++--------------------- tests/test.websock.js | 67 +++++++++--------- 3 files changed, 116 insertions(+), 115 deletions(-) diff --git a/tests/assertions.js b/tests/assertions.js index 07a5c297..f734e0c5 100644 --- a/tests/assertions.js +++ b/tests/assertions.js @@ -29,12 +29,12 @@ chai.use(function (_chai, utils) { _chai.Assertion.addMethod('sent', function (target_data) { const obj = this._obj; obj.inspect = () => { - const res = { _websocket: obj._websocket, rQi: obj._rQi, _rQ: new Uint8Array(obj._rQ.buffer, 0, obj._rQlen), + const res = { _webChannel: obj._webChannel, rQi: obj._rQi, _rQ: new Uint8Array(obj._rQ.buffer, 0, obj._rQlen), _sQ: new Uint8Array(obj._sQ.buffer, 0, obj._sQlen) }; res.prototype = obj; return res; }; - const data = obj._websocket._get_sent_data(); + const data = obj._webChannel._get_sent_data(); let same = true; if (data.length != target_data.length) { same = false; diff --git a/tests/test.rfb.js b/tests/test.rfb.js index ba35b9cb..9fc77dae 100644 --- a/tests/test.rfb.js +++ b/tests/test.rfb.js @@ -1,7 +1,7 @@ const expect = chai.expect; import RFB from '../core/rfb.js'; -import Websock from '../core/websock.js'; +import WebChannel from '../core/websock.js'; import { encodings } from '../core/encodings.js'; import FakeWebSocket from './fake.websocket.js'; @@ -62,12 +62,12 @@ describe('Remote Frame Buffer Protocol Client', function () { window.requestAnimationFrame = setTimeout; // Use a single set of buffers instead of reallocating to // speed up tests - const sock = new Websock(); + const sock = new WebChannel(); 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 () { + WebChannel.prototype._old_allocate_buffers = WebChannel.prototype._allocate_buffers; + WebChannel.prototype._allocate_buffers = function () { this._sQ = _sQ; this._rQ = rQ; }; @@ -75,7 +75,7 @@ describe('Remote Frame Buffer Protocol Client', function () { }); after(function () { - Websock.prototype._allocate_buffers = Websock.prototype._old_allocate_buffers; + WebChannel.prototype._allocate_buffers = WebChannel.prototype._old_allocate_buffers; this.clock.restore(); window.requestAnimationFrame = raf; }); @@ -110,7 +110,7 @@ describe('Remote Frame Buffer Protocol Client', function () { url = url || 'wss://host:8675'; const rfb = new RFB(container, url, options); clock.tick(); - rfb._sock._websocket._open(); + rfb._sock._webChannel._open(); rfb._rfb_connection_state = 'connected'; sinon.spy(rfb, "_disconnect"); rfbs.push(rfb); @@ -390,7 +390,7 @@ describe('Remote Frame Buffer Protocol Client', function () { sinon.spy(client._display, "viewportChangeSize"); - client._sock._websocket._receive_data(new Uint8Array(incoming)); + client._sock._webChannel._receive_data(new Uint8Array(incoming)); // FIXME: Display implicitly calls viewportChangeSize() when // resizing the framebuffer, hence calledTwice. @@ -580,7 +580,7 @@ describe('Remote Frame Buffer Protocol Client', function () { sinon.spy(client._display, "autoscale"); - client._sock._websocket._receive_data(new Uint8Array(incoming)); + client._sock._webChannel._receive_data(new Uint8Array(incoming)); expect(client._display.autoscale).to.have.been.calledOnce; expect(client._display.autoscale).to.have.been.calledWith(70, 80); @@ -635,7 +635,7 @@ describe('Remote Frame Buffer Protocol Client', function () { client._supportsSetDesktopSize = false; - client._sock._websocket._receive_data(new Uint8Array(incoming)); + client._sock._webChannel._receive_data(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); @@ -644,7 +644,7 @@ describe('Remote Frame Buffer Protocol Client', function () { // Second message should not trigger a resize - client._sock._websocket._receive_data(new Uint8Array(incoming)); + client._sock._webChannel._receive_data(new Uint8Array(incoming)); expect(RFB.messages.setDesktopSize).to.not.have.been.called; }); @@ -727,7 +727,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00 ]; - client._sock._websocket._receive_data(new Uint8Array(incoming)); + client._sock._webChannel._receive_data(new Uint8Array(incoming)); expect(RFB.messages.setDesktopSize).to.not.have.been.called; }); @@ -858,7 +858,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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._sock._webChannel.open = () => {}; // explicitly don't call onopen client._updateConnectionState('connecting'); expect(spy).to.not.have.been.called; }); @@ -870,9 +870,9 @@ describe('Remote Frame Buffer Protocol Client', function () { client = make_rfb(); }); - it('should force disconnect if we do not call Websock.onclose within the disconnection timeout', function () { + it('should force disconnect if we do not call WebChannel.onclose within the disconnection timeout', function () { sinon.spy(client, '_updateConnectionState'); - client._sock._websocket.close = () => {}; // explicitly don't call onclose + client._sock._webChannel.close = () => {}; // explicitly don't call onclose client._updateConnectionState('disconnecting'); this.clock.tick(3 * 1000); expect(client._updateConnectionState).to.have.been.calledTwice; @@ -880,10 +880,10 @@ describe('Remote Frame Buffer Protocol Client', function () { expect(client._rfb_connection_state).to.equal("disconnected"); }); - it('should not fail if Websock.onclose gets called within the disconnection timeout', function () { + it('should not fail if WebChannel.onclose gets called within the disconnection timeout', function () { client._updateConnectionState('disconnecting'); this.clock.tick(3 * 1000 / 2); - client._sock._websocket.close(); + client._sock._webChannel.close(); this.clock.tick(3 * 1000 / 2 + 1); expect(client._rfb_connection_state).to.equal('disconnected'); }); @@ -897,7 +897,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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._sock._webChannel.close = () => {}; // explicitly don't call onclose client._updateConnectionState('disconnecting'); expect(spy).to.not.have.been.called; }); @@ -945,7 +945,7 @@ describe('Remote Frame Buffer Protocol Client', function () { } arr[0] = 'R'; arr[1] = 'F'; arr[2] = 'B'; arr[3] = ' '; arr[11] = '\n'; - client._sock._websocket._receive_data(arr); + client._sock._webChannel._receive_data(arr); } describe('version parsing', function () { @@ -1023,7 +1023,7 @@ describe('Remote Frame Buffer Protocol Client', function () { send_ver('000.000', client); expect(client._rfb_version).to.equal(0); - const sent_data = client._sock._websocket._get_sent_data(); + const sent_data = client._sock._webChannel._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); }); @@ -1046,14 +1046,14 @@ describe('Remote Frame Buffer Protocol Client', function () { 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)); + client._sock._webChannel._receive_data(new Uint8Array(auth_scheme_raw)); expect(client._rfb_auth_scheme).to.equal(auth_scheme); }); 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)); + client._sock._webChannel._receive_data(new Uint8Array(auth_schemes)); expect(client._rfb_auth_scheme).to.equal(1); expect(client._sock).to.have.sent(new Uint8Array([1, 1])); }); @@ -1061,7 +1061,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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)); + client._sock._webChannel._receive_data(new Uint8Array(auth_schemes)); expect(client._rfb_auth_scheme).to.equal(22); expect(client._sock).to.have.sent(new Uint8Array([22])); }); @@ -1070,7 +1070,7 @@ describe('Remote Frame Buffer Protocol Client', 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._sock._webChannel._receive_data(new Uint8Array(auth_schemes)); expect(client._fail).to.have.been.calledOnce; }); @@ -1078,7 +1078,7 @@ describe('Remote Frame Buffer Protocol Client', function () { client._rfb_version = 3.7; const failure_data = [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._webChannel._receive_data(new Uint8Array(failure_data)); expect(client._fail).to.have.been.calledOnce; expect(client._fail).to.have.been.calledWith( @@ -1089,7 +1089,7 @@ describe('Remote Frame Buffer Protocol Client', 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)); + client._sock._webChannel._receive_data(new Uint8Array(auth_schemes)); expect(client._rfb_init_state).to.equal('Authentication'); expect(client._negotiate_authentication).to.have.been.calledOnce; }); @@ -1101,7 +1101,7 @@ describe('Remote Frame Buffer Protocol Client', function () { }); function send_security(type, cl) { - cl._sock._websocket._receive_data(new Uint8Array([1, type])); + cl._sock._webChannel._receive_data(new Uint8Array([1, type])); } it('should fail on auth scheme 0 (pre 3.7) with the given message', function () { @@ -1115,7 +1115,7 @@ describe('Remote Frame Buffer Protocol Client', function () { } sinon.spy(client, '_fail'); - client._sock._websocket._receive_data(new Uint8Array(data)); + client._sock._webChannel._receive_data(new Uint8Array(data)); expect(client._fail).to.have.been.calledWith( 'Security negotiation failed on authentication scheme (reason: Whoopsies)'); }); @@ -1152,7 +1152,7 @@ describe('Remote Frame Buffer Protocol Client', function () { const challenge = []; for (let i = 0; i < 16; i++) { challenge[i] = i; } - client._sock._websocket._receive_data(new Uint8Array(challenge)); + client._sock._webChannel._receive_data(new Uint8Array(challenge)); expect(client._rfb_credentials).to.be.empty; expect(spy).to.have.been.calledOnce; @@ -1162,11 +1162,11 @@ describe('Remote Frame Buffer Protocol Client', function () { 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._sock._webChannel._get_sent_data(); // 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._webChannel._receive_data(new Uint8Array(challenge)); const des_pass = RFB.genDES('passwd', challenge); expect(client._sock).to.have.sent(new Uint8Array(des_pass)); @@ -1178,7 +1178,7 @@ describe('Remote Frame Buffer Protocol Client', function () { const challenge = []; for (let i = 0; i < 16; i++) { challenge[i] = i; } - client._sock._websocket._receive_data(new Uint8Array(challenge)); + client._sock._webChannel._receive_data(new Uint8Array(challenge)); expect(client._rfb_init_state).to.equal('SecurityResult'); }); @@ -1241,7 +1241,7 @@ describe('Remote Frame Buffer Protocol Client', 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._sock._webChannel._get_sent_data(); // skip the security reply }); function send_num_str_pairs(pairs, client) { @@ -1258,11 +1258,11 @@ describe('Remote Frame Buffer Protocol Client', function () { } } - client._sock._websocket._receive_data(new Uint8Array(data)); + client._sock._webChannel._receive_data(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])); + client._sock._webChannel._receive_data(new Uint8Array([0, 0, 0, 0])); expect(client._rfb_tightvnc).to.be.true; }); @@ -1284,7 +1284,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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 + client._sock._webChannel._get_sent_data(); // skip the tunnel choice here send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client); expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1])); expect(client._rfb_init_state).to.equal('SecurityResult'); @@ -1330,7 +1330,7 @@ describe('Remote Frame Buffer Protocol Client', function () { }); it('should fall through to ServerInitialisation on a response code of 0', function () { - client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0])); + client._sock._webChannel._receive_data(new Uint8Array([0, 0, 0, 0])); expect(client._rfb_init_state).to.equal('ServerInitialisation'); }); @@ -1338,7 +1338,7 @@ describe('Remote Frame Buffer Protocol Client', function () { client._rfb_version = 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)); + client._sock._webChannel._receive_data(new Uint8Array(failure_data)); expect(client._fail).to.have.been.calledWith( 'Security negotiation failed on security result (reason: whoops)'); }); @@ -1346,7 +1346,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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._sock._webChannel._receive_data(new Uint8Array([0, 0, 0, 1])); expect(client._fail).to.have.been.calledWith( 'Security handshake failed'); }); @@ -1354,7 +1354,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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._webChannel._receive_data(new Uint8Array([0, 0, 0, 2])); expect(spy).to.have.been.calledOnce; expect(spy.args[0][0].detail.status).to.equal(2); }); @@ -1365,7 +1365,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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)); + client._sock._webChannel._receive_data(new Uint8Array(failure_data)); expect(spy.args[0][0].detail.status).to.equal(1); expect(spy.args[0][0].detail.reason).to.equal('such failure'); }); @@ -1375,7 +1375,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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)); + client._sock._webChannel._receive_data(new Uint8Array(failure_data)); expect(spy.args[0][0].detail.status).to.equal(1); expect('reason' in spy.args[0][0].detail).to.be.false; }); @@ -1384,7 +1384,7 @@ describe('Remote Frame Buffer Protocol Client', function () { client._rfb_version = 3.6; const spy = sinon.spy(); client.addEventListener("securityfailure", spy); - client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 2])); + client._sock._webChannel._receive_data(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; }); @@ -1395,7 +1395,7 @@ describe('Remote Frame Buffer Protocol Client', 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])); + client._sock._webChannel._receive_data(new Uint8Array([0, 0, 0, 0])); expect(client._rfb_init_state).to.equal('ServerInitialisation'); }); @@ -1403,7 +1403,7 @@ describe('Remote Frame Buffer Protocol Client', 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])); + client._sock._webChannel._receive_data(new Uint8Array([0, 0, 0, 0])); expect(client._sock).to.have.sent(new Uint8Array([1])); }); @@ -1411,7 +1411,7 @@ describe('Remote Frame Buffer Protocol Client', 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])); + client._sock._webChannel._receive_data(new Uint8Array([0, 0, 0, 0])); expect(client._sock).to.have.sent(new Uint8Array([0])); }); }); @@ -1450,15 +1450,15 @@ describe('Remote Frame Buffer Protocol Client', function () { push8(data, 0); push8(data, 0); - client._sock._websocket._receive_data(new Uint8Array(data)); + client._sock._webChannel._receive_data(new Uint8Array(data)); const name_data = []; let name_len = []; pushString(name_data, full_opts.name); push32(name_len, name_data.length); - client._sock._websocket._receive_data(new Uint8Array(name_len)); - client._sock._websocket._receive_data(new Uint8Array(name_data)); + client._sock._webChannel._receive_data(new Uint8Array(name_len)); + client._sock._webChannel._receive_data(new Uint8Array(name_data)); } it('should set the framebuffer width and height', function () { @@ -1493,7 +1493,7 @@ describe('Remote Frame Buffer Protocol Client', function () { for (let i = 0; i < 16 + 32 + 48; i++) { tight_data.push(i); } - client._sock._websocket._receive_data(new Uint8Array(tight_data)); + client._sock._webChannel._receive_data(new Uint8Array(tight_data)); expect(client._rfb_connection_state).to.equal('connected'); }); @@ -1617,7 +1617,7 @@ describe('Remote Frame Buffer Protocol Client', function () { data = data.concat(rect_data[i]); } - client._sock._websocket._receive_data(new Uint8Array(data)); + client._sock._webChannel._receive_data(new Uint8Array(data)); } it('should send an update request if there is sufficient data', function () { @@ -1625,14 +1625,14 @@ describe('Remote Frame Buffer Protocol Client', function () { RFB.messages.fbUpdateRequest(expected_msg, true, 0, 0, 640, 20); client._framebufferUpdate = () => true; - client._sock._websocket._receive_data(new Uint8Array([0])); + client._sock._webChannel._receive_data(new Uint8Array([0])); expect(client._sock).to.have.sent(expected_msg._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._webChannel._receive_data(new Uint8Array([0])); + expect(client._sock._webChannel._get_sent_data()).to.have.length(0); }); it('should resume receiving an update if we previously did not have enough data', function () { @@ -1640,21 +1640,21 @@ describe('Remote Frame Buffer Protocol Client', function () { RFB.messages.fbUpdateRequest(expected_msg, true, 0, 0, 640, 20); // just enough to set FBU.rects - client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 3])); - expect(client._sock._websocket._get_sent_data()).to.have.length(0); + client._sock._webChannel._receive_data(new Uint8Array([0, 0, 0, 3])); + expect(client._sock._webChannel._get_sent_data()).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])); + client._sock._webChannel._receive_data(new Uint8Array([247])); expect(client._sock).to.have.sent(expected_msg._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._webChannel._receive_data(new Uint8Array([0])); - expect(client._sock._websocket._get_sent_data()).to.have.length(0); + expect(client._sock._webChannel._get_sent_data()).to.have.length(0); }); it('should fail on an unsupported encoding', function () { @@ -2322,7 +2322,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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])); + client._sock._webChannel._receive_data(new Uint8Array([250, 0, 10, 1])); expect(client._rfb_xvp_ver).to.equal(10); expect(spy).to.have.been.calledOnce; expect(spy.args[0][0].detail.capabilities.power).to.be.true; @@ -2331,7 +2331,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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._webChannel._receive_data(new Uint8Array([250, 0, 10, 237])); expect(client._fail).to.have.been.calledOnce; }); }); @@ -2344,7 +2344,7 @@ describe('Remote Frame Buffer Protocol Client', function () { const spy = sinon.spy(); client.addEventListener("clipboard", spy); - client._sock._websocket._receive_data(new Uint8Array(data)); + client._sock._webChannel._receive_data(new Uint8Array(data)); expect(spy).to.have.been.calledOnce; expect(spy.args[0][0].detail.text).to.equal(expected_str); }); @@ -2352,7 +2352,7 @@ describe('Remote Frame Buffer Protocol Client', function () { 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._webChannel._receive_data(new Uint8Array([2])); expect(spy).to.have.been.calledOnce; }); @@ -2366,7 +2366,7 @@ describe('Remote Frame Buffer Protocol Client', function () { RFB.messages.clientFence(expected_msg, (1<<0) | (1<<1), payload); RFB.messages.clientFence(incoming_msg, 0xffffffff, payload); - client._sock._websocket._receive_data(incoming_msg._sQ); + client._sock._webChannel._receive_data(incoming_msg._sQ); expect(client._sock).to.have.sent(expected_msg._sQ); @@ -2376,7 +2376,7 @@ describe('Remote Frame Buffer Protocol Client', function () { RFB.messages.clientFence(expected_msg, (1<<0), payload); RFB.messages.clientFence(incoming_msg, (1<<0) | (1<<31), payload); - client._sock._websocket._receive_data(incoming_msg._sQ); + client._sock._webChannel._receive_data(incoming_msg._sQ); expect(client._sock).to.have.sent(expected_msg._sQ); }); @@ -2388,7 +2388,7 @@ describe('Remote Frame Buffer Protocol Client', function () { expect(client._enabledContinuousUpdates).to.be.false; - client._sock._websocket._receive_data(new Uint8Array([150])); + client._sock._webChannel._receive_data(new Uint8Array([150])); expect(client._enabledContinuousUpdates).to.be.true; expect(client._sock).to.have.sent(expected_msg._sQ); @@ -2398,7 +2398,7 @@ describe('Remote Frame Buffer Protocol Client', function () { client._enabledContinuousUpdates = true; client._supportsContinuousUpdates = true; - client._sock._websocket._receive_data(new Uint8Array([150])); + client._sock._webChannel._receive_data(new Uint8Array([150])); expect(client._enabledContinuousUpdates).to.be.false; }); @@ -2409,7 +2409,7 @@ describe('Remote Frame Buffer Protocol Client', function () { client._resize(450, 160); - expect(client._sock._websocket._get_sent_data()).to.have.length(0); + expect(client._sock._webChannel._get_sent_data()).to.have.length(0); client._enabledContinuousUpdates = true; @@ -2420,7 +2420,7 @@ describe('Remote Frame Buffer Protocol Client', function () { it('should fail on an unknown message type', function () { sinon.spy(client, "_fail"); - client._sock._websocket._receive_data(new Uint8Array([87])); + client._sock._webChannel._receive_data(new Uint8Array([87])); expect(client._fail).to.have.been.calledOnce; }); }); @@ -2505,13 +2505,13 @@ describe('Remote Frame Buffer Protocol Client', function () { // message events it('should do nothing if we receive an empty message and have nothing in the queue', function () { client._normal_msg = sinon.spy(); - client._sock._websocket._receive_data(new Uint8Array([])); + client._sock._webChannel._receive_data(new Uint8Array([])); expect(client._normal_msg).to.not.have.been.called; }); 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])); + client._sock._webChannel._receive_data(new Uint8Array([1, 2, 3])); expect(client._normal_msg).to.have.been.called; }); @@ -2519,14 +2519,14 @@ describe('Remote Frame Buffer Protocol Client', 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])); + client._sock._webChannel._receive_data(new Uint8Array([1, 2, 3])); expect(client._init_msg).to.have.been.called; }); 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])); + client._sock._webChannel._receive_data(new Uint8Array([0x02, 0x02])); expect(spy).to.have.been.calledTwice; }); @@ -2534,39 +2534,39 @@ describe('Remote Frame Buffer Protocol Client', function () { it('should update the state to ProtocolVersion on open (if the state is "connecting")', function () { client = new RFB(document.createElement('div'), 'wss://host:8675'); this.clock.tick(); - client._sock._websocket._open(); + client._sock._webChannel._open(); expect(client._rfb_init_state).to.equal('ProtocolVersion'); }); it('should fail if we are not currently ready to connect and we get an "open" event', function () { sinon.spy(client, "_fail"); client._rfb_connection_state = 'connected'; - client._sock._websocket._open(); + client._sock._webChannel._open(); expect(client._fail).to.have.been.calledOnce; }); // close events it('should transition to "disconnected" from "disconnecting" on a close event', function () { - const real = client._sock._websocket.close; - client._sock._websocket.close = () => {}; + const real = client._sock._webChannel.close; + client._sock._webChannel.close = () => {}; client.disconnect(); expect(client._rfb_connection_state).to.equal('disconnecting'); - client._sock._websocket.close = real; - client._sock._websocket.close(); + client._sock._webChannel.close = real; + client._sock._webChannel.close(); expect(client._rfb_connection_state).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._sock._websocket.close(); + client._sock._webChannel.close(); expect(client._fail).to.have.been.calledOnce; }); it('should unregister close event handler', function () { sinon.spy(client._sock, 'off'); client.disconnect(); - client._sock._websocket.close(); + client._sock._webChannel.close(); expect(client._sock.off).to.have.been.calledWith('close'); }); diff --git a/tests/test.websock.js b/tests/test.websock.js index 33d5cec4..4a6f5510 100644 --- a/tests/test.websock.js +++ b/tests/test.websock.js @@ -1,9 +1,9 @@ const expect = chai.expect; -import Websock from '../core/websock.js'; +import WebChannel from '../core/websock.js'; import FakeWebSocket from './fake.websocket.js'; -describe('Websock', function () { +describe('WebChannel', function () { "use strict"; describe('Queue methods', function () { @@ -11,7 +11,7 @@ describe('Websock', function () { const RQ_TEMPLATE = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]); beforeEach(function () { - sock = new Websock(); + sock = new WebChannel(); // skip init sock._allocate_buffers(); sock._rQ.set(RQ_TEMPLATE); @@ -188,30 +188,31 @@ describe('Websock', function () { describe('flush', function () { beforeEach(function () { - sock._websocket = { + sock._webChannel = { send: sinon.spy() }; }); it('should actually send on the websocket', function () { - sock._websocket.bufferedAmount = 8; - sock._websocket.readyState = WebSocket.OPEN; + sock._channelStates = sock._getChannelStates("WebSocket"); + sock._webChannel.bufferedAmount = 8; + sock._webChannel.readyState = WebSocket.OPEN; sock._sQ = new Uint8Array([1, 2, 3]); sock._sQlen = 3; const encoded = sock._encode_message(); sock.flush(); - expect(sock._websocket.send).to.have.been.calledOnce; - expect(sock._websocket.send).to.have.been.calledWith(encoded); + expect(sock._webChannel.send).to.have.been.calledOnce; + expect(sock._webChannel.send).to.have.been.calledWith(encoded); }); it('should not call send if we do not have anything queued up', function () { sock._sQlen = 0; - sock._websocket.bufferedAmount = 8; + sock._webChannel.bufferedAmount = 8; sock.flush(); - expect(sock._websocket.send).not.to.have.been.called; + expect(sock._webChannel.send).not.to.have.been.called; }); }); @@ -252,7 +253,7 @@ describe('Websock', function () { let sock; beforeEach(function () { - sock = new Websock(); + sock = new WebChannel(); // eslint-disable-next-line no-global-assign WebSocket = sinon.spy(); WebSocket.OPEN = old_WS.OPEN; @@ -269,7 +270,7 @@ describe('Websock', function () { }); it('should open the actual websocket', function () { - sock.open('ws://localhost:8675', 'binary'); + sock.open({ uri: 'ws://localhost:8675', protocols: 'binary' }); expect(WebSocket).to.have.been.calledWith('ws://localhost:8675', 'binary'); }); @@ -278,38 +279,38 @@ describe('Websock', function () { describe('closing', function () { beforeEach(function () { - sock.open('ws://'); - sock._websocket.close = sinon.spy(); + sock.open({ uri: 'ws://' }); + sock._webChannel.close = sinon.spy(); }); it('should close the actual websocket if it is open', function () { - sock._websocket.readyState = WebSocket.OPEN; + sock._webChannel.readyState = WebSocket.OPEN; sock.close(); - expect(sock._websocket.close).to.have.been.calledOnce; + expect(sock._webChannel.close).to.have.been.calledOnce; }); it('should close the actual websocket if it is connecting', function () { - sock._websocket.readyState = WebSocket.CONNECTING; + sock._webChannel.readyState = WebSocket.CONNECTING; sock.close(); - expect(sock._websocket.close).to.have.been.calledOnce; + expect(sock._webChannel.close).to.have.been.calledOnce; }); it('should not try to close the actual websocket if closing', function () { - sock._websocket.readyState = WebSocket.CLOSING; + sock._webChannel.readyState = WebSocket.CLOSING; sock.close(); - expect(sock._websocket.close).not.to.have.been.called; + expect(sock._webChannel.close).not.to.have.been.called; }); it('should not try to close the actual websocket if closed', function () { - sock._websocket.readyState = WebSocket.CLOSED; + sock._webChannel.readyState = WebSocket.CLOSED; sock.close(); - expect(sock._websocket.close).not.to.have.been.called; + expect(sock._webChannel.close).not.to.have.been.called; }); it('should reset onmessage to not call _recv_message', function () { sinon.spy(sock, '_recv_message'); sock.close(); - sock._websocket.onmessage(null); + sock._webChannel.onmessage(null); try { expect(sock._recv_message).not.to.have.been.called; } finally { @@ -324,26 +325,26 @@ describe('Websock', function () { sock.on('open', sinon.spy()); sock.on('close', sinon.spy()); sock.on('error', sinon.spy()); - sock.open('ws://'); + sock.open({ uri: 'ws://' }); }); it('should call _recv_message on a message', function () { - sock._websocket.onmessage(null); + sock._webChannel.onmessage(null); expect(sock._recv_message).to.have.been.calledOnce; }); it('should call the open event handler on opening', function () { - sock._websocket.onopen(); + sock._webChannel.onopen(); expect(sock._eventHandlers.open).to.have.been.calledOnce; }); it('should call the close event handler on closing', function () { - sock._websocket.onclose(); + sock._webChannel.onclose(); expect(sock._eventHandlers.close).to.have.been.calledOnce; }); it('should call the error event handler on error', function () { - sock._websocket.onerror(); + sock._webChannel.onerror(); expect(sock._eventHandlers.error).to.have.been.calledOnce; }); }); @@ -357,7 +358,7 @@ describe('Websock', function () { describe('WebSocket Receiving', function () { let sock; beforeEach(function () { - sock = new Websock(); + sock = new WebChannel(); sock._allocate_buffers(); }); @@ -442,9 +443,9 @@ describe('Websock', function () { describe('as binary data', function () { let sock; beforeEach(function () { - sock = new Websock(); - sock.open('ws://', 'binary'); - sock._websocket._open(); + sock = new WebChannel(); + sock.open({ uri: 'ws://', protocols: 'binary' }); + sock._webChannel._open(); }); it('should only send the send queue up to the send queue length', function () { @@ -456,7 +457,7 @@ describe('Websock', function () { it('should properly pass the encoded data off to the actual WebSocket', function () { sock.send([1, 2, 3]); - expect(sock._websocket._get_sent_data()).to.array.equal(new Uint8Array([1, 2, 3])); + expect(sock._webChannel._get_sent_data()).to.array.equal(new Uint8Array([1, 2, 3])); }); }); });