test.rfb.js 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. // requires local modules: util, websock, rfb, keyboard, keysym, keysymdef, input, inflator, des, display
  2. // requires test modules: fake.websocket, assertions
  3. /* jshint expr: true */
  4. var assert = chai.assert;
  5. var expect = chai.expect;
  6. function make_rfb (extra_opts) {
  7. if (!extra_opts) {
  8. extra_opts = {};
  9. }
  10. extra_opts.target = extra_opts.target || document.createElement('canvas');
  11. return new RFB(extra_opts);
  12. }
  13. describe('Remote Frame Buffer Protocol Client', function() {
  14. "use strict";
  15. before(FakeWebSocket.replace);
  16. after(FakeWebSocket.restore);
  17. before(function () {
  18. this.clock = sinon.useFakeTimers();
  19. // Use a single set of buffers instead of reallocating to
  20. // speed up tests
  21. var sock = new Websock();
  22. var _sQ = new Uint8Array(sock._sQbufferSize);
  23. var rQ = new Uint8Array(sock._rQbufferSize);
  24. Websock.prototype._old_allocate_buffers = Websock.prototype._allocate_buffers;
  25. Websock.prototype._allocate_buffers = function () {
  26. this._sQ = _sQ;
  27. this._rQ = rQ;
  28. };
  29. });
  30. after(function () {
  31. Websock.prototype._allocate_buffers = Websock.prototype._old_allocate_buffers;
  32. this.clock.restore();
  33. });
  34. describe('Public API Basic Behavior', function () {
  35. var client;
  36. beforeEach(function () {
  37. client = make_rfb();
  38. });
  39. describe('#connect', function () {
  40. beforeEach(function () { client._updateState = sinon.spy(); });
  41. it('should set the current state to "connect"', function () {
  42. client.connect('host', 8675);
  43. expect(client._updateState).to.have.been.calledOnce;
  44. expect(client._updateState).to.have.been.calledWith('connect');
  45. });
  46. it('should fail if we are missing a host', function () {
  47. sinon.spy(client, '_fail');
  48. client.connect(undefined, 8675);
  49. expect(client._fail).to.have.been.calledOnce;
  50. });
  51. it('should fail if we are missing a port', function () {
  52. sinon.spy(client, '_fail');
  53. client.connect('abc');
  54. expect(client._fail).to.have.been.calledOnce;
  55. });
  56. it('should not update the state if we are missing a host or port', function () {
  57. sinon.spy(client, '_fail');
  58. client.connect('abc');
  59. expect(client._fail).to.have.been.calledOnce;
  60. expect(client._updateState).to.have.been.calledOnce;
  61. expect(client._updateState).to.have.been.calledWith('failed');
  62. });
  63. });
  64. describe('#disconnect', function () {
  65. beforeEach(function () { client._updateState = sinon.spy(); });
  66. it('should set the current state to "disconnect"', function () {
  67. client.disconnect();
  68. expect(client._updateState).to.have.been.calledOnce;
  69. expect(client._updateState).to.have.been.calledWith('disconnect');
  70. });
  71. it('should unregister error event handler', function () {
  72. sinon.spy(client._sock, 'off');
  73. client.disconnect();
  74. expect(client._sock.off).to.have.been.calledWith('error');
  75. });
  76. it('should unregister message event handler', function () {
  77. sinon.spy(client._sock, 'off');
  78. client.disconnect();
  79. expect(client._sock.off).to.have.been.calledWith('message');
  80. });
  81. it('should unregister open event handler', function () {
  82. sinon.spy(client._sock, 'off');
  83. client.disconnect();
  84. expect(client._sock.off).to.have.been.calledWith('open');
  85. });
  86. });
  87. describe('#sendPassword', function () {
  88. beforeEach(function () { this.clock = sinon.useFakeTimers(); });
  89. afterEach(function () { this.clock.restore(); });
  90. it('should set the state to "Authentication"', function () {
  91. client._rfb_state = "blah";
  92. client.sendPassword('pass');
  93. expect(client._rfb_state).to.equal('Authentication');
  94. });
  95. it('should call init_msg "soon"', function () {
  96. client._init_msg = sinon.spy();
  97. client.sendPassword('pass');
  98. this.clock.tick(5);
  99. expect(client._init_msg).to.have.been.calledOnce;
  100. });
  101. });
  102. describe('#sendCtrlAlDel', function () {
  103. beforeEach(function () {
  104. client._sock = new Websock();
  105. client._sock.open('ws://', 'binary');
  106. client._sock._websocket._open();
  107. sinon.spy(client._sock, 'flush');
  108. client._rfb_state = "normal";
  109. client._view_only = false;
  110. });
  111. it('should sent ctrl[down]-alt[down]-del[down] then del[up]-alt[up]-ctrl[up]', function () {
  112. var expected = {_sQ: new Uint8Array(48), _sQlen: 0};
  113. RFB.messages.keyEvent(expected, 0xFFE3, 1);
  114. RFB.messages.keyEvent(expected, 0xFFE9, 1);
  115. RFB.messages.keyEvent(expected, 0xFFFF, 1);
  116. RFB.messages.keyEvent(expected, 0xFFFF, 0);
  117. RFB.messages.keyEvent(expected, 0xFFE9, 0);
  118. RFB.messages.keyEvent(expected, 0xFFE3, 0);
  119. client.sendCtrlAltDel();
  120. expect(client._sock).to.have.sent(expected._sQ);
  121. });
  122. it('should not send the keys if we are not in a normal state', function () {
  123. client._rfb_state = "broken";
  124. client.sendCtrlAltDel();
  125. expect(client._sock.flush).to.not.have.been.called;
  126. });
  127. it('should not send the keys if we are set as view_only', function () {
  128. client._view_only = true;
  129. client.sendCtrlAltDel();
  130. expect(client._sock.flush).to.not.have.been.called;
  131. });
  132. });
  133. describe('#sendKey', function () {
  134. beforeEach(function () {
  135. client._sock = new Websock();
  136. client._sock.open('ws://', 'binary');
  137. client._sock._websocket._open();
  138. sinon.spy(client._sock, 'flush');
  139. client._rfb_state = "normal";
  140. client._view_only = false;
  141. });
  142. it('should send a single key with the given code and state (down = true)', function () {
  143. var expected = {_sQ: new Uint8Array(8), _sQlen: 0};
  144. RFB.messages.keyEvent(expected, 123, 1);
  145. client.sendKey(123, true);
  146. expect(client._sock).to.have.sent(expected._sQ);
  147. });
  148. it('should send both a down and up event if the state is not specified', function () {
  149. var expected = {_sQ: new Uint8Array(16), _sQlen: 0};
  150. RFB.messages.keyEvent(expected, 123, 1);
  151. RFB.messages.keyEvent(expected, 123, 0);
  152. client.sendKey(123);
  153. expect(client._sock).to.have.sent(expected._sQ);
  154. });
  155. it('should not send the key if we are not in a normal state', function () {
  156. client._rfb_state = "broken";
  157. client.sendKey(123);
  158. expect(client._sock.flush).to.not.have.been.called;
  159. });
  160. it('should not send the key if we are set as view_only', function () {
  161. client._view_only = true;
  162. client.sendKey(123);
  163. expect(client._sock.flush).to.not.have.been.called;
  164. });
  165. });
  166. describe('#clipboardPasteFrom', function () {
  167. beforeEach(function () {
  168. client._sock = new Websock();
  169. client._sock.open('ws://', 'binary');
  170. client._sock._websocket._open();
  171. sinon.spy(client._sock, 'flush');
  172. client._rfb_state = "normal";
  173. client._view_only = false;
  174. });
  175. it('should send the given text in a paste event', function () {
  176. var expected = {_sQ: new Uint8Array(11), _sQlen: 0};
  177. RFB.messages.clientCutText(expected, 'abc');
  178. client.clipboardPasteFrom('abc');
  179. expect(client._sock).to.have.sent(expected._sQ);
  180. });
  181. it('should not send the text if we are not in a normal state', function () {
  182. client._rfb_state = "broken";
  183. client.clipboardPasteFrom('abc');
  184. expect(client._sock.flush).to.not.have.been.called;
  185. });
  186. });
  187. describe("#setDesktopSize", function () {
  188. beforeEach(function() {
  189. client._sock = new Websock();
  190. client._sock.open('ws://', 'binary');
  191. client._sock._websocket._open();
  192. sinon.spy(client._sock, 'flush');
  193. client._rfb_state = "normal";
  194. client._view_only = false;
  195. client._supportsSetDesktopSize = true;
  196. });
  197. it('should send the request with the given width and height', function () {
  198. var expected = [251];
  199. expected.push8(0); // padding
  200. expected.push16(1); // width
  201. expected.push16(2); // height
  202. expected.push8(1); // number-of-screens
  203. expected.push8(0); // padding before screen array
  204. expected.push32(0); // id
  205. expected.push16(0); // x-position
  206. expected.push16(0); // y-position
  207. expected.push16(1); // width
  208. expected.push16(2); // height
  209. expected.push32(0); // flags
  210. client.setDesktopSize(1, 2);
  211. expect(client._sock).to.have.sent(new Uint8Array(expected));
  212. });
  213. it('should not send the request if the client has not recieved a ExtendedDesktopSize rectangle', function () {
  214. client._supportsSetDesktopSize = false;
  215. client.setDesktopSize(1,2);
  216. expect(client._sock.flush).to.not.have.been.called;
  217. });
  218. it('should not send the request if we are not in a normal state', function () {
  219. client._rfb_state = "broken";
  220. client.setDesktopSize(1,2);
  221. expect(client._sock.flush).to.not.have.been.called;
  222. });
  223. });
  224. describe("XVP operations", function () {
  225. beforeEach(function () {
  226. client._sock = new Websock();
  227. client._sock.open('ws://', 'binary');
  228. client._sock._websocket._open();
  229. sinon.spy(client._sock, 'flush');
  230. client._rfb_state = "normal";
  231. client._view_only = false;
  232. client._rfb_xvp_ver = 1;
  233. });
  234. it('should send the shutdown signal on #xvpShutdown', function () {
  235. client.xvpShutdown();
  236. expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x02]));
  237. });
  238. it('should send the reboot signal on #xvpReboot', function () {
  239. client.xvpReboot();
  240. expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x03]));
  241. });
  242. it('should send the reset signal on #xvpReset', function () {
  243. client.xvpReset();
  244. expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x04]));
  245. });
  246. it('should support sending arbitrary XVP operations via #xvpOp', function () {
  247. client.xvpOp(1, 7);
  248. expect(client._sock).to.have.sent(new Uint8Array([0xFA, 0x00, 0x01, 0x07]));
  249. });
  250. it('should not send XVP operations with higher versions than we support', function () {
  251. expect(client.xvpOp(2, 7)).to.be.false;
  252. expect(client._sock.flush).to.not.have.been.called;
  253. });
  254. });
  255. });
  256. describe('Misc Internals', function () {
  257. describe('#_updateState', function () {
  258. var client;
  259. beforeEach(function () {
  260. this.clock = sinon.useFakeTimers();
  261. client = make_rfb();
  262. });
  263. afterEach(function () {
  264. this.clock.restore();
  265. });
  266. it('should clear the disconnect timer if the state is not disconnect', function () {
  267. var spy = sinon.spy();
  268. client._disconnTimer = setTimeout(spy, 50);
  269. client._updateState('normal');
  270. this.clock.tick(51);
  271. expect(spy).to.not.have.been.called;
  272. expect(client._disconnTimer).to.be.null;
  273. });
  274. });
  275. });
  276. describe('Page States', function () {
  277. describe('loaded', function () {
  278. var client;
  279. beforeEach(function () { client = make_rfb(); });
  280. it('should close any open WebSocket connection', function () {
  281. sinon.spy(client._sock, 'close');
  282. client._updateState('loaded');
  283. expect(client._sock.close).to.have.been.calledOnce;
  284. });
  285. });
  286. describe('disconnected', function () {
  287. var client;
  288. beforeEach(function () { client = make_rfb(); });
  289. it('should close any open WebSocket connection', function () {
  290. sinon.spy(client._sock, 'close');
  291. client._updateState('disconnected');
  292. expect(client._sock.close).to.have.been.calledOnce;
  293. });
  294. });
  295. describe('connect', function () {
  296. var client;
  297. beforeEach(function () { client = make_rfb(); });
  298. it('should reset the variable states', function () {
  299. sinon.spy(client, '_init_vars');
  300. client._updateState('connect');
  301. expect(client._init_vars).to.have.been.calledOnce;
  302. });
  303. it('should actually connect to the websocket', function () {
  304. sinon.spy(client._sock, 'open');
  305. client._updateState('connect');
  306. expect(client._sock.open).to.have.been.calledOnce;
  307. });
  308. it('should use wss:// to connect if encryption is enabled', function () {
  309. sinon.spy(client._sock, 'open');
  310. client.set_encrypt(true);
  311. client._updateState('connect');
  312. expect(client._sock.open.args[0][0]).to.contain('wss://');
  313. });
  314. it('should use ws:// to connect if encryption is not enabled', function () {
  315. sinon.spy(client._sock, 'open');
  316. client.set_encrypt(true);
  317. client._updateState('connect');
  318. expect(client._sock.open.args[0][0]).to.contain('wss://');
  319. });
  320. it('should use a uri with the host, port, and path specified to connect', function () {
  321. sinon.spy(client._sock, 'open');
  322. client.set_encrypt(false);
  323. client._rfb_host = 'HOST';
  324. client._rfb_port = 8675;
  325. client._rfb_path = 'PATH';
  326. client._updateState('connect');
  327. expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH');
  328. });
  329. it('should attempt to close the websocket before we open an new one', function () {
  330. sinon.spy(client._sock, 'close');
  331. client._updateState('connect');
  332. expect(client._sock.close).to.have.been.calledOnce;
  333. });
  334. });
  335. describe('disconnect', function () {
  336. var client;
  337. beforeEach(function () {
  338. this.clock = sinon.useFakeTimers();
  339. client = make_rfb();
  340. client.connect('host', 8675);
  341. });
  342. afterEach(function () {
  343. this.clock.restore();
  344. });
  345. it('should fail if we do not call Websock.onclose within the disconnection timeout', function () {
  346. client._sock._websocket.close = function () {}; // explicitly don't call onclose
  347. client._updateState('disconnect');
  348. this.clock.tick(client.get_disconnectTimeout() * 1000);
  349. expect(client._rfb_state).to.equal('failed');
  350. });
  351. it('should not fail if Websock.onclose gets called within the disconnection timeout', function () {
  352. client._updateState('disconnect');
  353. this.clock.tick(client.get_disconnectTimeout() * 500);
  354. client._sock._websocket.close();
  355. this.clock.tick(client.get_disconnectTimeout() * 500 + 1);
  356. expect(client._rfb_state).to.equal('disconnected');
  357. });
  358. it('should close the WebSocket connection', function () {
  359. sinon.spy(client._sock, 'close');
  360. client._updateState('disconnect');
  361. expect(client._sock.close).to.have.been.calledTwice; // once on loaded, once on disconnect
  362. });
  363. });
  364. describe('failed', function () {
  365. var client;
  366. beforeEach(function () {
  367. this.clock = sinon.useFakeTimers();
  368. client = make_rfb();
  369. client.connect('host', 8675);
  370. });
  371. afterEach(function () {
  372. this.clock.restore();
  373. });
  374. it('should close the WebSocket connection', function () {
  375. sinon.spy(client._sock, 'close');
  376. client._updateState('failed');
  377. expect(client._sock.close).to.have.been.called;
  378. });
  379. it('should transition to disconnected but stay in failed state', function () {
  380. client.set_onUpdateState(sinon.spy());
  381. client._updateState('failed');
  382. this.clock.tick(50);
  383. expect(client._rfb_state).to.equal('failed');
  384. var onUpdateState = client.get_onUpdateState();
  385. expect(onUpdateState).to.have.been.called;
  386. // it should be specifically the last call
  387. expect(onUpdateState.args[onUpdateState.args.length - 1][1]).to.equal('disconnected');
  388. expect(onUpdateState.args[onUpdateState.args.length - 1][2]).to.equal('failed');
  389. });
  390. });
  391. describe('fatal', function () {
  392. var client;
  393. beforeEach(function () { client = make_rfb(); });
  394. it('should close any open WebSocket connection', function () {
  395. sinon.spy(client._sock, 'close');
  396. client._updateState('fatal');
  397. expect(client._sock.close).to.have.been.calledOnce;
  398. });
  399. });
  400. // NB(directxman12): Normal does *nothing* in updateState
  401. });
  402. describe('Protocol Initialization States', function () {
  403. describe('ProtocolVersion', function () {
  404. beforeEach(function () {
  405. this.clock = sinon.useFakeTimers();
  406. });
  407. afterEach(function () {
  408. this.clock.restore();
  409. });
  410. function send_ver (ver, client) {
  411. var arr = new Uint8Array(12);
  412. for (var i = 0; i < ver.length; i++) {
  413. arr[i+4] = ver.charCodeAt(i);
  414. }
  415. arr[0] = 'R'; arr[1] = 'F'; arr[2] = 'B'; arr[3] = ' ';
  416. arr[11] = '\n';
  417. client._sock._websocket._receive_data(arr);
  418. }
  419. describe('version parsing', function () {
  420. var client;
  421. beforeEach(function () {
  422. client = make_rfb();
  423. client.connect('host', 8675);
  424. client._sock._websocket._open();
  425. });
  426. it('should interpret version 000.000 as a repeater', function () {
  427. client._repeaterID = '\x01\x02\x03\x04\x05';
  428. send_ver('000.000', client);
  429. expect(client._rfb_version).to.equal(0);
  430. var sent_data = client._sock._websocket._get_sent_data();
  431. expect(new Uint8Array(sent_data.buffer, 0, 5)).to.array.equal(new Uint8Array([1, 2, 3, 4, 5]));
  432. });
  433. it('should interpret version 003.003 as version 3.3', function () {
  434. send_ver('003.003', client);
  435. expect(client._rfb_version).to.equal(3.3);
  436. });
  437. it('should interpret version 003.006 as version 3.3', function () {
  438. send_ver('003.006', client);
  439. expect(client._rfb_version).to.equal(3.3);
  440. });
  441. it('should interpret version 003.889 as version 3.3', function () {
  442. send_ver('003.889', client);
  443. expect(client._rfb_version).to.equal(3.3);
  444. });
  445. it('should interpret version 003.007 as version 3.7', function () {
  446. send_ver('003.007', client);
  447. expect(client._rfb_version).to.equal(3.7);
  448. });
  449. it('should interpret version 003.008 as version 3.8', function () {
  450. send_ver('003.008', client);
  451. expect(client._rfb_version).to.equal(3.8);
  452. });
  453. it('should interpret version 004.000 as version 3.8', function () {
  454. send_ver('004.000', client);
  455. expect(client._rfb_version).to.equal(3.8);
  456. });
  457. it('should interpret version 004.001 as version 3.8', function () {
  458. send_ver('004.001', client);
  459. expect(client._rfb_version).to.equal(3.8);
  460. });
  461. it('should fail on an invalid version', function () {
  462. send_ver('002.000', client);
  463. expect(client._rfb_state).to.equal('failed');
  464. });
  465. });
  466. var client;
  467. beforeEach(function () {
  468. client = make_rfb();
  469. client.connect('host', 8675);
  470. client._sock._websocket._open();
  471. });
  472. it('should handle two step repeater negotiation', function () {
  473. client._repeaterID = '\x01\x02\x03\x04\x05';
  474. send_ver('000.000', client);
  475. expect(client._rfb_version).to.equal(0);
  476. var sent_data = client._sock._websocket._get_sent_data();
  477. expect(new Uint8Array(sent_data.buffer, 0, 5)).to.array.equal(new Uint8Array([1, 2, 3, 4, 5]));
  478. expect(sent_data).to.have.length(250);
  479. send_ver('003.008', client);
  480. expect(client._rfb_version).to.equal(3.8);
  481. });
  482. it('should initialize the flush interval', function () {
  483. client._sock.flush = sinon.spy();
  484. send_ver('003.008', client);
  485. this.clock.tick(100);
  486. expect(client._sock.flush).to.have.been.calledThrice;
  487. });
  488. it('should send back the interpreted version', function () {
  489. send_ver('004.000', client);
  490. var expected_str = 'RFB 003.008\n';
  491. var expected = [];
  492. for (var i = 0; i < expected_str.length; i++) {
  493. expected[i] = expected_str.charCodeAt(i);
  494. }
  495. expect(client._sock).to.have.sent(new Uint8Array(expected));
  496. });
  497. it('should transition to the Security state on successful negotiation', function () {
  498. send_ver('003.008', client);
  499. expect(client._rfb_state).to.equal('Security');
  500. });
  501. });
  502. describe('Security', function () {
  503. var client;
  504. beforeEach(function () {
  505. client = make_rfb();
  506. client.connect('host', 8675);
  507. client._sock._websocket._open();
  508. client._rfb_state = 'Security';
  509. });
  510. it('should simply receive the auth scheme when for versions < 3.7', function () {
  511. client._rfb_version = 3.6;
  512. var auth_scheme_raw = [1, 2, 3, 4];
  513. var auth_scheme = (auth_scheme_raw[0] << 24) + (auth_scheme_raw[1] << 16) +
  514. (auth_scheme_raw[2] << 8) + auth_scheme_raw[3];
  515. client._sock._websocket._receive_data(auth_scheme_raw);
  516. expect(client._rfb_auth_scheme).to.equal(auth_scheme);
  517. });
  518. it('should choose for the most prefered scheme possible for versions >= 3.7', function () {
  519. client._rfb_version = 3.7;
  520. var auth_schemes = [2, 1, 2];
  521. client._sock._websocket._receive_data(auth_schemes);
  522. expect(client._rfb_auth_scheme).to.equal(2);
  523. expect(client._sock).to.have.sent(new Uint8Array([2]));
  524. });
  525. it('should fail if there are no supported schemes for versions >= 3.7', function () {
  526. client._rfb_version = 3.7;
  527. var auth_schemes = [1, 32];
  528. client._sock._websocket._receive_data(auth_schemes);
  529. expect(client._rfb_state).to.equal('failed');
  530. });
  531. it('should fail with the appropriate message if no types are sent for versions >= 3.7', function () {
  532. client._rfb_version = 3.7;
  533. var failure_data = [0, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115];
  534. sinon.spy(client, '_fail');
  535. client._sock._websocket._receive_data(failure_data);
  536. expect(client._fail).to.have.been.calledTwice;
  537. expect(client._fail).to.have.been.calledWith('Security failure: whoops');
  538. });
  539. it('should transition to the Authentication state and continue on successful negotiation', function () {
  540. client._rfb_version = 3.7;
  541. var auth_schemes = [1, 1];
  542. client._negotiate_authentication = sinon.spy();
  543. client._sock._websocket._receive_data(auth_schemes);
  544. expect(client._rfb_state).to.equal('Authentication');
  545. expect(client._negotiate_authentication).to.have.been.calledOnce;
  546. });
  547. });
  548. describe('Authentication', function () {
  549. var client;
  550. beforeEach(function () {
  551. client = make_rfb();
  552. client.connect('host', 8675);
  553. client._sock._websocket._open();
  554. client._rfb_state = 'Security';
  555. });
  556. function send_security(type, cl) {
  557. cl._sock._websocket._receive_data(new Uint8Array([1, type]));
  558. }
  559. it('should fail on auth scheme 0 (pre 3.7) with the given message', function () {
  560. client._rfb_version = 3.6;
  561. var err_msg = "Whoopsies";
  562. var data = [0, 0, 0, 0];
  563. var err_len = err_msg.length;
  564. data.push32(err_len);
  565. for (var i = 0; i < err_len; i++) {
  566. data.push(err_msg.charCodeAt(i));
  567. }
  568. sinon.spy(client, '_fail');
  569. client._sock._websocket._receive_data(new Uint8Array(data));
  570. expect(client._rfb_state).to.equal('failed');
  571. expect(client._fail).to.have.been.calledWith('Auth failure: Whoopsies');
  572. });
  573. it('should transition straight to SecurityResult on "no auth" (1) for versions >= 3.8', function () {
  574. client._rfb_version = 3.8;
  575. send_security(1, client);
  576. expect(client._rfb_state).to.equal('SecurityResult');
  577. });
  578. it('should transition straight to ClientInitialisation on "no auth" for versions < 3.8', function () {
  579. client._rfb_version = 3.7;
  580. sinon.spy(client, '_updateState');
  581. send_security(1, client);
  582. expect(client._updateState).to.have.been.calledWith('ClientInitialisation');
  583. expect(client._rfb_state).to.equal('ServerInitialisation');
  584. });
  585. it('should fail on an unknown auth scheme', function () {
  586. client._rfb_version = 3.8;
  587. send_security(57, client);
  588. expect(client._rfb_state).to.equal('failed');
  589. });
  590. describe('VNC Authentication (type 2) Handler', function () {
  591. var client;
  592. beforeEach(function () {
  593. client = make_rfb();
  594. client.connect('host', 8675);
  595. client._sock._websocket._open();
  596. client._rfb_state = 'Security';
  597. client._rfb_version = 3.8;
  598. });
  599. it('should transition to the "password" state if missing a password', function () {
  600. send_security(2, client);
  601. expect(client._rfb_state).to.equal('password');
  602. });
  603. it('should encrypt the password with DES and then send it back', function () {
  604. client._rfb_password = 'passwd';
  605. send_security(2, client);
  606. client._sock._websocket._get_sent_data(); // skip the choice of auth reply
  607. var challenge = [];
  608. for (var i = 0; i < 16; i++) { challenge[i] = i; }
  609. client._sock._websocket._receive_data(new Uint8Array(challenge));
  610. var des_pass = RFB.genDES('passwd', challenge);
  611. expect(client._sock).to.have.sent(new Uint8Array(des_pass));
  612. });
  613. it('should transition to SecurityResult immediately after sending the password', function () {
  614. client._rfb_password = 'passwd';
  615. send_security(2, client);
  616. var challenge = [];
  617. for (var i = 0; i < 16; i++) { challenge[i] = i; }
  618. client._sock._websocket._receive_data(new Uint8Array(challenge));
  619. expect(client._rfb_state).to.equal('SecurityResult');
  620. });
  621. });
  622. describe('XVP Authentication (type 22) Handler', function () {
  623. var client;
  624. beforeEach(function () {
  625. client = make_rfb();
  626. client.connect('host', 8675);
  627. client._sock._websocket._open();
  628. client._rfb_state = 'Security';
  629. client._rfb_version = 3.8;
  630. });
  631. it('should fall through to standard VNC authentication upon completion', function () {
  632. client.set_xvp_password_sep('#');
  633. client._rfb_password = 'user#target#password';
  634. client._negotiate_std_vnc_auth = sinon.spy();
  635. send_security(22, client);
  636. expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
  637. });
  638. it('should transition to the "password" state if the passwords is missing', function() {
  639. send_security(22, client);
  640. expect(client._rfb_state).to.equal('password');
  641. });
  642. it('should transition to the "password" state if the passwords is improperly formatted', function() {
  643. client._rfb_password = 'user@target';
  644. send_security(22, client);
  645. expect(client._rfb_state).to.equal('password');
  646. });
  647. it('should split the password, send the first two parts, and pass on the last part', function () {
  648. client.set_xvp_password_sep('#');
  649. client._rfb_password = 'user#target#password';
  650. client._negotiate_std_vnc_auth = sinon.spy();
  651. send_security(22, client);
  652. expect(client._rfb_password).to.equal('password');
  653. var expected = [22, 4, 6]; // auth selection, len user, len target
  654. for (var i = 0; i < 10; i++) { expected[i+3] = 'usertarget'.charCodeAt(i); }
  655. expect(client._sock).to.have.sent(new Uint8Array(expected));
  656. });
  657. });
  658. describe('TightVNC Authentication (type 16) Handler', function () {
  659. var client;
  660. beforeEach(function () {
  661. client = make_rfb();
  662. client.connect('host', 8675);
  663. client._sock._websocket._open();
  664. client._rfb_state = 'Security';
  665. client._rfb_version = 3.8;
  666. send_security(16, client);
  667. client._sock._websocket._get_sent_data(); // skip the security reply
  668. });
  669. function send_num_str_pairs(pairs, client) {
  670. var pairs_len = pairs.length;
  671. var data = [];
  672. data.push32(pairs_len);
  673. for (var i = 0; i < pairs_len; i++) {
  674. data.push32(pairs[i][0]);
  675. var j;
  676. for (j = 0; j < 4; j++) {
  677. data.push(pairs[i][1].charCodeAt(j));
  678. }
  679. for (j = 0; j < 8; j++) {
  680. data.push(pairs[i][2].charCodeAt(j));
  681. }
  682. }
  683. client._sock._websocket._receive_data(new Uint8Array(data));
  684. }
  685. it('should skip tunnel negotiation if no tunnels are requested', function () {
  686. client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
  687. expect(client._rfb_tightvnc).to.be.true;
  688. });
  689. it('should fail if no supported tunnels are listed', function () {
  690. send_num_str_pairs([[123, 'OTHR', 'SOMETHNG']], client);
  691. expect(client._rfb_state).to.equal('failed');
  692. });
  693. it('should choose the notunnel tunnel type', function () {
  694. send_num_str_pairs([[0, 'TGHT', 'NOTUNNEL'], [123, 'OTHR', 'SOMETHNG']], client);
  695. expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 0]));
  696. });
  697. it('should continue to sub-auth negotiation after tunnel negotiation', function () {
  698. send_num_str_pairs([[0, 'TGHT', 'NOTUNNEL']], client);
  699. client._sock._websocket._get_sent_data(); // skip the tunnel choice here
  700. send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
  701. expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1]));
  702. expect(client._rfb_state).to.equal('SecurityResult');
  703. });
  704. /*it('should attempt to use VNC auth over no auth when possible', function () {
  705. client._rfb_tightvnc = true;
  706. client._negotiate_std_vnc_auth = sinon.spy();
  707. send_num_str_pairs([[1, 'STDV', 'NOAUTH__'], [2, 'STDV', 'VNCAUTH_']], client);
  708. expect(client._sock).to.have.sent([0, 0, 0, 1]);
  709. expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
  710. expect(client._rfb_auth_scheme).to.equal(2);
  711. });*/ // while this would make sense, the original code doesn't actually do this
  712. it('should accept the "no auth" auth type and transition to SecurityResult', function () {
  713. client._rfb_tightvnc = true;
  714. send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
  715. expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1]));
  716. expect(client._rfb_state).to.equal('SecurityResult');
  717. });
  718. it('should accept VNC authentication and transition to that', function () {
  719. client._rfb_tightvnc = true;
  720. client._negotiate_std_vnc_auth = sinon.spy();
  721. send_num_str_pairs([[2, 'STDV', 'VNCAUTH__']], client);
  722. expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 2]));
  723. expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
  724. expect(client._rfb_auth_scheme).to.equal(2);
  725. });
  726. it('should fail if there are no supported auth types', function () {
  727. client._rfb_tightvnc = true;
  728. send_num_str_pairs([[23, 'stdv', 'badval__']], client);
  729. expect(client._rfb_state).to.equal('failed');
  730. });
  731. });
  732. });
  733. describe('SecurityResult', function () {
  734. var client;
  735. beforeEach(function () {
  736. client = make_rfb();
  737. client.connect('host', 8675);
  738. client._sock._websocket._open();
  739. client._rfb_state = 'SecurityResult';
  740. });
  741. it('should fall through to ClientInitialisation on a response code of 0', function () {
  742. client._updateState = sinon.spy();
  743. client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
  744. expect(client._updateState).to.have.been.calledOnce;
  745. expect(client._updateState).to.have.been.calledWith('ClientInitialisation');
  746. });
  747. it('should fail on an error code of 1 with the given message for versions >= 3.8', function () {
  748. client._rfb_version = 3.8;
  749. sinon.spy(client, '_fail');
  750. var failure_data = [0, 0, 0, 1, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115];
  751. client._sock._websocket._receive_data(new Uint8Array(failure_data));
  752. expect(client._rfb_state).to.equal('failed');
  753. expect(client._fail).to.have.been.calledWith('whoops');
  754. });
  755. it('should fail on an error code of 1 with a standard message for version < 3.8', function () {
  756. client._rfb_version = 3.7;
  757. client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 1]));
  758. expect(client._rfb_state).to.equal('failed');
  759. });
  760. });
  761. describe('ClientInitialisation', function () {
  762. var client;
  763. beforeEach(function () {
  764. client = make_rfb();
  765. client.connect('host', 8675);
  766. client._sock._websocket._open();
  767. client._rfb_state = 'SecurityResult';
  768. });
  769. it('should transition to the ServerInitialisation state', function () {
  770. client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
  771. expect(client._rfb_state).to.equal('ServerInitialisation');
  772. });
  773. it('should send 1 if we are in shared mode', function () {
  774. client.set_shared(true);
  775. client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
  776. expect(client._sock).to.have.sent(new Uint8Array([1]));
  777. });
  778. it('should send 0 if we are not in shared mode', function () {
  779. client.set_shared(false);
  780. client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
  781. expect(client._sock).to.have.sent(new Uint8Array([0]));
  782. });
  783. });
  784. describe('ServerInitialisation', function () {
  785. var client;
  786. beforeEach(function () {
  787. client = make_rfb();
  788. client.connect('host', 8675);
  789. client._sock._websocket._open();
  790. client._rfb_state = 'ServerInitialisation';
  791. });
  792. function send_server_init(opts, client) {
  793. var full_opts = { width: 10, height: 12, bpp: 24, depth: 24, big_endian: 0,
  794. true_color: 1, red_max: 255, green_max: 255, blue_max: 255,
  795. red_shift: 16, green_shift: 8, blue_shift: 0, name: 'a name' };
  796. for (var opt in opts) {
  797. full_opts[opt] = opts[opt];
  798. }
  799. var data = [];
  800. data.push16(full_opts.width);
  801. data.push16(full_opts.height);
  802. data.push(full_opts.bpp);
  803. data.push(full_opts.depth);
  804. data.push(full_opts.big_endian);
  805. data.push(full_opts.true_color);
  806. data.push16(full_opts.red_max);
  807. data.push16(full_opts.green_max);
  808. data.push16(full_opts.blue_max);
  809. data.push8(full_opts.red_shift);
  810. data.push8(full_opts.green_shift);
  811. data.push8(full_opts.blue_shift);
  812. // padding
  813. data.push8(0);
  814. data.push8(0);
  815. data.push8(0);
  816. client._sock._websocket._receive_data(new Uint8Array(data));
  817. var name_data = [];
  818. name_data.push32(full_opts.name.length);
  819. for (var i = 0; i < full_opts.name.length; i++) {
  820. name_data.push(full_opts.name.charCodeAt(i));
  821. }
  822. client._sock._websocket._receive_data(new Uint8Array(name_data));
  823. }
  824. it('should set the framebuffer width and height', function () {
  825. send_server_init({ width: 32, height: 84 }, client);
  826. expect(client._fb_width).to.equal(32);
  827. expect(client._fb_height).to.equal(84);
  828. });
  829. // NB(sross): we just warn, not fail, for endian-ness and shifts, so we don't test them
  830. it('should set the framebuffer name and call the callback', function () {
  831. client.set_onDesktopName(sinon.spy());
  832. send_server_init({ name: 'some name' }, client);
  833. var spy = client.get_onDesktopName();
  834. expect(client._fb_name).to.equal('some name');
  835. expect(spy).to.have.been.calledOnce;
  836. expect(spy.args[0][1]).to.equal('some name');
  837. });
  838. it('should handle the extended init message of the tight encoding', function () {
  839. // NB(sross): we don't actually do anything with it, so just test that we can
  840. // read it w/o throwing an error
  841. client._rfb_tightvnc = true;
  842. send_server_init({}, client);
  843. var tight_data = [];
  844. tight_data.push16(1);
  845. tight_data.push16(2);
  846. tight_data.push16(3);
  847. tight_data.push16(0);
  848. for (var i = 0; i < 16 + 32 + 48; i++) {
  849. tight_data.push(i);
  850. }
  851. client._sock._websocket._receive_data(tight_data);
  852. expect(client._rfb_state).to.equal('normal');
  853. });
  854. it('should set the true color mode on the display to the configuration variable', function () {
  855. client.set_true_color(false);
  856. sinon.spy(client._display, 'set_true_color');
  857. send_server_init({ true_color: 1 }, client);
  858. expect(client._display.set_true_color).to.have.been.calledOnce;
  859. expect(client._display.set_true_color).to.have.been.calledWith(false);
  860. });
  861. it('should call the resize callback and resize the display', function () {
  862. client.set_onFBResize(sinon.spy());
  863. sinon.spy(client._display, 'resize');
  864. send_server_init({ width: 27, height: 32 }, client);
  865. var spy = client.get_onFBResize();
  866. expect(client._display.resize).to.have.been.calledOnce;
  867. expect(client._display.resize).to.have.been.calledWith(27, 32);
  868. expect(spy).to.have.been.calledOnce;
  869. expect(spy.args[0][1]).to.equal(27);
  870. expect(spy.args[0][2]).to.equal(32);
  871. });
  872. it('should grab the mouse and keyboard', function () {
  873. sinon.spy(client._keyboard, 'grab');
  874. sinon.spy(client._mouse, 'grab');
  875. send_server_init({}, client);
  876. expect(client._keyboard.grab).to.have.been.calledOnce;
  877. expect(client._mouse.grab).to.have.been.calledOnce;
  878. });
  879. it('should set the BPP and depth to 4 and 3 respectively if in true color mode', function () {
  880. client.set_true_color(true);
  881. send_server_init({}, client);
  882. expect(client._fb_Bpp).to.equal(4);
  883. expect(client._fb_depth).to.equal(3);
  884. });
  885. it('should set the BPP and depth to 1 and 1 respectively if not in true color mode', function () {
  886. client.set_true_color(false);
  887. send_server_init({}, client);
  888. expect(client._fb_Bpp).to.equal(1);
  889. expect(client._fb_depth).to.equal(1);
  890. });
  891. // TODO(directxman12): test the various options in this configuration matrix
  892. it('should reply with the pixel format, client encodings, and initial update request', function () {
  893. client.set_true_color(true);
  894. client.set_local_cursor(false);
  895. // we skip the cursor encoding
  896. var expected = {_sQ: new Uint8Array(34 + 4 * (client._encodings.length - 1)), _sQlen: 0};
  897. RFB.messages.pixelFormat(expected, 4, 3, true);
  898. RFB.messages.clientEncodings(expected, client._encodings, false, true);
  899. var expected_cdr = { cleanBox: { x: 0, y: 0, w: 0, h: 0 },
  900. dirtyBoxes: [ { x: 0, y: 0, w: 27, h: 32 } ] };
  901. RFB.messages.fbUpdateRequests(expected, expected_cdr, 27, 32);
  902. send_server_init({ width: 27, height: 32 }, client);
  903. expect(client._sock).to.have.sent(expected._sQ);
  904. });
  905. it('should transition to the "normal" state', function () {
  906. send_server_init({}, client);
  907. expect(client._rfb_state).to.equal('normal');
  908. });
  909. });
  910. });
  911. describe('Protocol Message Processing After Completing Initialization', function () {
  912. var client;
  913. beforeEach(function () {
  914. client = make_rfb();
  915. client.connect('host', 8675);
  916. client._sock._websocket._open();
  917. client._rfb_state = 'normal';
  918. client._fb_name = 'some device';
  919. client._fb_width = 640;
  920. client._fb_height = 20;
  921. });
  922. describe('Framebuffer Update Handling', function () {
  923. var client;
  924. beforeEach(function () {
  925. client = make_rfb();
  926. client.connect('host', 8675);
  927. client._sock._websocket._open();
  928. client._rfb_state = 'normal';
  929. client._fb_name = 'some device';
  930. client._fb_width = 640;
  931. client._fb_height = 20;
  932. });
  933. var target_data_arr = [
  934. 0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  935. 0x00, 0xff, 0x00, 255, 0xff, 0x00, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  936. 0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255,
  937. 0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255
  938. ];
  939. var target_data;
  940. var target_data_check_arr = [
  941. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  942. 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
  943. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
  944. 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
  945. ];
  946. var target_data_check;
  947. before(function () {
  948. // NB(directxman12): PhantomJS 1.x doesn't implement Uint8ClampedArray
  949. target_data = new Uint8Array(target_data_arr);
  950. target_data_check = new Uint8Array(target_data_check_arr);
  951. });
  952. function send_fbu_msg (rect_info, rect_data, client, rect_cnt) {
  953. var data = [];
  954. if (!rect_cnt || rect_cnt > -1) {
  955. // header
  956. data.push(0); // msg type
  957. data.push(0); // padding
  958. data.push16(rect_cnt || rect_data.length);
  959. }
  960. for (var i = 0; i < rect_data.length; i++) {
  961. if (rect_info[i]) {
  962. data.push16(rect_info[i].x);
  963. data.push16(rect_info[i].y);
  964. data.push16(rect_info[i].width);
  965. data.push16(rect_info[i].height);
  966. data.push32(rect_info[i].encoding);
  967. }
  968. data = data.concat(rect_data[i]);
  969. }
  970. client._sock._websocket._receive_data(new Uint8Array(data));
  971. }
  972. it('should send an update request if there is sufficient data', function () {
  973. var expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0};
  974. var expected_cdr = { cleanBox: { x: 0, y: 0, w: 0, h: 0 },
  975. dirtyBoxes: [ { x: 0, y: 0, w: 240, h: 20 } ] };
  976. RFB.messages.fbUpdateRequests(expected_msg, expected_cdr, 240, 20);
  977. client._framebufferUpdate = function () { return true; };
  978. client._sock._websocket._receive_data(new Uint8Array([0]));
  979. expect(client._sock).to.have.sent(expected_msg._sQ);
  980. });
  981. it('should not send an update request if we need more data', function () {
  982. client._sock._websocket._receive_data(new Uint8Array([0]));
  983. expect(client._sock._websocket._get_sent_data()).to.have.length(0);
  984. });
  985. it('should resume receiving an update if we previously did not have enough data', function () {
  986. var expected_msg = {_sQ: new Uint8Array(10), _sQlen: 0};
  987. var expected_cdr = { cleanBox: { x: 0, y: 0, w: 0, h: 0 },
  988. dirtyBoxes: [ { x: 0, y: 0, w: 240, h: 20 } ] };
  989. RFB.messages.fbUpdateRequests(expected_msg, expected_cdr, 240, 20);
  990. // just enough to set FBU.rects
  991. client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 3]));
  992. expect(client._sock._websocket._get_sent_data()).to.have.length(0);
  993. client._framebufferUpdate = function () { return true; }; // we magically have enough data
  994. // 247 should *not* be used as the message type here
  995. client._sock._websocket._receive_data(new Uint8Array([247]));
  996. expect(client._sock).to.have.sent(expected_msg._sQ);
  997. });
  998. it('should parse out information from a header before any actual data comes in', function () {
  999. client.set_onFBUReceive(sinon.spy());
  1000. var rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 0x02, encodingName: 'RRE' };
  1001. send_fbu_msg([rect_info], [[]], client);
  1002. var spy = client.get_onFBUReceive();
  1003. expect(spy).to.have.been.calledOnce;
  1004. expect(spy).to.have.been.calledWith(sinon.match.any, rect_info);
  1005. });
  1006. it('should fire onFBUComplete when the update is complete', function () {
  1007. client.set_onFBUComplete(sinon.spy());
  1008. var rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: -224, encodingName: 'last_rect' };
  1009. send_fbu_msg([rect_info], [[]], client); // last_rect
  1010. var spy = client.get_onFBUComplete();
  1011. expect(spy).to.have.been.calledOnce;
  1012. expect(spy).to.have.been.calledWith(sinon.match.any, rect_info);
  1013. });
  1014. it('should not fire onFBUComplete if we have not finished processing the update', function () {
  1015. client.set_onFBUComplete(sinon.spy());
  1016. var rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 0x00, encodingName: 'RAW' };
  1017. send_fbu_msg([rect_info], [[]], client);
  1018. expect(client.get_onFBUComplete()).to.not.have.been.called;
  1019. });
  1020. it('should call the appropriate encoding handler', function () {
  1021. client._encHandlers[0x02] = sinon.spy();
  1022. var rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 0x02 };
  1023. send_fbu_msg([rect_info], [[]], client);
  1024. expect(client._encHandlers[0x02]).to.have.been.calledOnce;
  1025. });
  1026. it('should fail on an unsupported encoding', function () {
  1027. client.set_onFBUReceive(sinon.spy());
  1028. var rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 234 };
  1029. send_fbu_msg([rect_info], [[]], client);
  1030. expect(client._rfb_state).to.equal('failed');
  1031. });
  1032. it('should be able to pause and resume receiving rects if not enought data', function () {
  1033. // seed some initial data to copy
  1034. client._fb_width = 4;
  1035. client._fb_height = 4;
  1036. client._display.resize(4, 4);
  1037. var initial_data = client._display._drawCtx.createImageData(4, 2);
  1038. var initial_data_arr = target_data_check_arr.slice(0, 32);
  1039. for (var i = 0; i < 32; i++) { initial_data.data[i] = initial_data_arr[i]; }
  1040. client._display._drawCtx.putImageData(initial_data, 0, 0);
  1041. var info = [{ x: 0, y: 2, width: 2, height: 2, encoding: 0x01},
  1042. { x: 2, y: 2, width: 2, height: 2, encoding: 0x01}];
  1043. // data says [{ old_x: 0, old_y: 0 }, { old_x: 0, old_y: 0 }]
  1044. var rects = [[0, 2, 0, 0], [0, 0, 0, 0]];
  1045. send_fbu_msg([info[0]], [rects[0]], client, 2);
  1046. send_fbu_msg([info[1]], [rects[1]], client, -1);
  1047. expect(client._display).to.have.displayed(target_data_check);
  1048. });
  1049. describe('Message Encoding Handlers', function () {
  1050. var client;
  1051. beforeEach(function () {
  1052. client = make_rfb();
  1053. client.connect('host', 8675);
  1054. client._sock._websocket._open();
  1055. client._rfb_state = 'normal';
  1056. client._fb_name = 'some device';
  1057. // a really small frame
  1058. client._fb_width = 4;
  1059. client._fb_height = 4;
  1060. client._display._fb_width = 4;
  1061. client._display._fb_height = 4;
  1062. client._display._viewportLoc.w = 4;
  1063. client._display._viewportLoc.h = 4;
  1064. client._fb_Bpp = 4;
  1065. });
  1066. it('should handle the RAW encoding', function () {
  1067. var info = [{ x: 0, y: 0, width: 2, height: 2, encoding: 0x00 },
  1068. { x: 2, y: 0, width: 2, height: 2, encoding: 0x00 },
  1069. { x: 0, y: 2, width: 4, height: 1, encoding: 0x00 },
  1070. { x: 0, y: 3, width: 4, height: 1, encoding: 0x00 }];
  1071. // data is in bgrx
  1072. var rects = [
  1073. [0x00, 0x00, 0xff, 0, 0x00, 0xff, 0x00, 0, 0x00, 0xff, 0x00, 0, 0x00, 0x00, 0xff, 0],
  1074. [0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0],
  1075. [0xff, 0x00, 0xee, 0, 0xff, 0xee, 0x00, 0, 0xff, 0xee, 0xaa, 0, 0xff, 0xee, 0xab, 0],
  1076. [0xff, 0x00, 0xee, 0, 0xff, 0xee, 0x00, 0, 0xff, 0xee, 0xaa, 0, 0xff, 0xee, 0xab, 0]];
  1077. send_fbu_msg(info, rects, client);
  1078. expect(client._display).to.have.displayed(target_data);
  1079. });
  1080. it('should handle the COPYRECT encoding', function () {
  1081. // seed some initial data to copy
  1082. var initial_data = client._display._drawCtx.createImageData(4, 2);
  1083. var initial_data_arr = target_data_check_arr.slice(0, 32);
  1084. for (var i = 0; i < 32; i++) { initial_data.data[i] = initial_data_arr[i]; }
  1085. client._display._drawCtx.putImageData(initial_data, 0, 0);
  1086. var info = [{ x: 0, y: 2, width: 2, height: 2, encoding: 0x01},
  1087. { x: 2, y: 2, width: 2, height: 2, encoding: 0x01}];
  1088. // data says [{ old_x: 0, old_y: 0 }, { old_x: 0, old_y: 0 }]
  1089. var rects = [[0, 2, 0, 0], [0, 0, 0, 0]];
  1090. send_fbu_msg(info, rects, client);
  1091. expect(client._display).to.have.displayed(target_data_check);
  1092. });
  1093. // TODO(directxman12): for encodings with subrects, test resuming on partial send?
  1094. // TODO(directxman12): test rre_chunk_sz (related to above about subrects)?
  1095. it('should handle the RRE encoding', function () {
  1096. var info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x02 }];
  1097. var rect = [];
  1098. rect.push32(2); // 2 subrects
  1099. rect.push32(0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
  1100. rect.push(0xff); // becomes ff0000ff --> #0000FF color
  1101. rect.push(0x00);
  1102. rect.push(0x00);
  1103. rect.push(0xff);
  1104. rect.push16(0); // x: 0
  1105. rect.push16(0); // y: 0
  1106. rect.push16(2); // width: 2
  1107. rect.push16(2); // height: 2
  1108. rect.push(0xff); // becomes ff0000ff --> #0000FF color
  1109. rect.push(0x00);
  1110. rect.push(0x00);
  1111. rect.push(0xff);
  1112. rect.push16(2); // x: 2
  1113. rect.push16(2); // y: 2
  1114. rect.push16(2); // width: 2
  1115. rect.push16(2); // height: 2
  1116. send_fbu_msg(info, [rect], client);
  1117. expect(client._display).to.have.displayed(target_data_check);
  1118. });
  1119. describe('the HEXTILE encoding handler', function () {
  1120. var client;
  1121. beforeEach(function () {
  1122. client = make_rfb();
  1123. client.connect('host', 8675);
  1124. client._sock._websocket._open();
  1125. client._rfb_state = 'normal';
  1126. client._fb_name = 'some device';
  1127. // a really small frame
  1128. client._fb_width = 4;
  1129. client._fb_height = 4;
  1130. client._display._fb_width = 4;
  1131. client._display._fb_height = 4;
  1132. client._display._viewportLoc.w = 4;
  1133. client._display._viewportLoc.h = 4;
  1134. client._fb_Bpp = 4;
  1135. });
  1136. it('should handle a tile with fg, bg specified, normal subrects', function () {
  1137. var info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
  1138. var rect = [];
  1139. rect.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
  1140. rect.push32(0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
  1141. rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
  1142. rect.push(0x00);
  1143. rect.push(0x00);
  1144. rect.push(0xff);
  1145. rect.push(2); // 2 subrects
  1146. rect.push(0); // x: 0, y: 0
  1147. rect.push(1 | (1 << 4)); // width: 2, height: 2
  1148. rect.push(2 | (2 << 4)); // x: 2, y: 2
  1149. rect.push(1 | (1 << 4)); // width: 2, height: 2
  1150. send_fbu_msg(info, [rect], client);
  1151. expect(client._display).to.have.displayed(target_data_check);
  1152. });
  1153. it('should handle a raw tile', function () {
  1154. var info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
  1155. var rect = [];
  1156. rect.push(0x01); // raw
  1157. for (var i = 0; i < target_data.length; i += 4) {
  1158. rect.push(target_data[i + 2]);
  1159. rect.push(target_data[i + 1]);
  1160. rect.push(target_data[i]);
  1161. rect.push(target_data[i + 3]);
  1162. }
  1163. send_fbu_msg(info, [rect], client);
  1164. expect(client._display).to.have.displayed(target_data);
  1165. });
  1166. it('should handle a tile with only bg specified (solid bg)', function () {
  1167. var info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
  1168. var rect = [];
  1169. rect.push(0x02);
  1170. rect.push32(0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
  1171. send_fbu_msg(info, [rect], client);
  1172. var expected = [];
  1173. for (var i = 0; i < 16; i++) { expected.push32(0xff00ff); }
  1174. expect(client._display).to.have.displayed(new Uint8Array(expected));
  1175. });
  1176. it('should handle a tile with only bg specified and an empty frame afterwards', function () {
  1177. // set the width so we can have two tiles
  1178. client._fb_width = 8;
  1179. client._display._fb_width = 8;
  1180. client._display._viewportLoc.w = 8;
  1181. var info = [{ x: 0, y: 0, width: 32, height: 4, encoding: 0x05 }];
  1182. var rect = [];
  1183. // send a bg frame
  1184. rect.push(0x02);
  1185. rect.push32(0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
  1186. // send an empty frame
  1187. rect.push(0x00);
  1188. send_fbu_msg(info, [rect], client);
  1189. var expected = [];
  1190. var i;
  1191. for (i = 0; i < 16; i++) { expected.push32(0xff00ff); } // rect 1: solid
  1192. for (i = 0; i < 16; i++) { expected.push32(0xff00ff); } // rect 2: same bkground color
  1193. expect(client._display).to.have.displayed(new Uint8Array(expected));
  1194. });
  1195. it('should handle a tile with bg and coloured subrects', function () {
  1196. var info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
  1197. var rect = [];
  1198. rect.push(0x02 | 0x08 | 0x10); // bg spec, anysubrects, colouredsubrects
  1199. rect.push32(0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
  1200. rect.push(2); // 2 subrects
  1201. rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
  1202. rect.push(0x00);
  1203. rect.push(0x00);
  1204. rect.push(0xff);
  1205. rect.push(0); // x: 0, y: 0
  1206. rect.push(1 | (1 << 4)); // width: 2, height: 2
  1207. rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
  1208. rect.push(0x00);
  1209. rect.push(0x00);
  1210. rect.push(0xff);
  1211. rect.push(2 | (2 << 4)); // x: 2, y: 2
  1212. rect.push(1 | (1 << 4)); // width: 2, height: 2
  1213. send_fbu_msg(info, [rect], client);
  1214. expect(client._display).to.have.displayed(target_data_check);
  1215. });
  1216. it('should carry over fg and bg colors from the previous tile if not specified', function () {
  1217. client._fb_width = 4;
  1218. client._fb_height = 17;
  1219. client._display.resize(4, 17);
  1220. var info = [{ x: 0, y: 0, width: 4, height: 17, encoding: 0x05}];
  1221. var rect = [];
  1222. rect.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
  1223. rect.push32(0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
  1224. rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
  1225. rect.push(0x00);
  1226. rect.push(0x00);
  1227. rect.push(0xff);
  1228. rect.push(8); // 8 subrects
  1229. var i;
  1230. for (i = 0; i < 4; i++) {
  1231. rect.push((0 << 4) | (i * 4)); // x: 0, y: i*4
  1232. rect.push(1 | (1 << 4)); // width: 2, height: 2
  1233. rect.push((2 << 4) | (i * 4 + 2)); // x: 2, y: i * 4 + 2
  1234. rect.push(1 | (1 << 4)); // width: 2, height: 2
  1235. }
  1236. rect.push(0x08); // anysubrects
  1237. rect.push(1); // 1 subrect
  1238. rect.push(0); // x: 0, y: 0
  1239. rect.push(1 | (1 << 4)); // width: 2, height: 2
  1240. send_fbu_msg(info, [rect], client);
  1241. var expected = [];
  1242. for (i = 0; i < 4; i++) { expected = expected.concat(target_data_check_arr); }
  1243. expected = expected.concat(target_data_check_arr.slice(0, 16));
  1244. expect(client._display).to.have.displayed(new Uint8Array(expected));
  1245. });
  1246. it('should fail on an invalid subencoding', function () {
  1247. var info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
  1248. var rects = [[45]]; // an invalid subencoding
  1249. send_fbu_msg(info, rects, client);
  1250. expect(client._rfb_state).to.equal('failed');
  1251. });
  1252. });
  1253. it.skip('should handle the TIGHT encoding', function () {
  1254. // TODO(directxman12): test this
  1255. });
  1256. it.skip('should handle the TIGHT_PNG encoding', function () {
  1257. // TODO(directxman12): test this
  1258. });
  1259. it('should handle the DesktopSize pseduo-encoding', function () {
  1260. client.set_onFBResize(sinon.spy());
  1261. sinon.spy(client._display, 'resize');
  1262. send_fbu_msg([{ x: 0, y: 0, width: 20, height: 50, encoding: -223 }], [[]], client);
  1263. var spy = client.get_onFBResize();
  1264. expect(spy).to.have.been.calledOnce;
  1265. expect(spy).to.have.been.calledWith(sinon.match.any, 20, 50);
  1266. expect(client._fb_width).to.equal(20);
  1267. expect(client._fb_height).to.equal(50);
  1268. expect(client._display.resize).to.have.been.calledOnce;
  1269. expect(client._display.resize).to.have.been.calledWith(20, 50);
  1270. });
  1271. describe('the ExtendedDesktopSize pseudo-encoding handler', function () {
  1272. var client;
  1273. beforeEach(function () {
  1274. client = make_rfb();
  1275. client.connect('host', 8675);
  1276. client._sock._websocket._open();
  1277. client._rfb_state = 'normal';
  1278. client._fb_name = 'some device';
  1279. client._supportsSetDesktopSize = false;
  1280. // a really small frame
  1281. client._fb_width = 4;
  1282. client._fb_height = 4;
  1283. client._display._fb_width = 4;
  1284. client._display._fb_height = 4;
  1285. client._display._viewportLoc.w = 4;
  1286. client._display._viewportLoc.h = 4;
  1287. client._fb_Bpp = 4;
  1288. sinon.spy(client._display, 'resize');
  1289. client.set_onFBResize(sinon.spy());
  1290. });
  1291. function make_screen_data (nr_of_screens) {
  1292. var data = [];
  1293. data.push8(nr_of_screens); // number-of-screens
  1294. data.push8(0); // padding
  1295. data.push16(0); // padding
  1296. for (var i=0; i<nr_of_screens; i += 1) {
  1297. data.push32(0); // id
  1298. data.push16(0); // x-position
  1299. data.push16(0); // y-position
  1300. data.push16(20); // width
  1301. data.push16(50); // height
  1302. data.push32(0); // flags
  1303. }
  1304. return data;
  1305. }
  1306. it('should handle a resize requested by this client', function () {
  1307. var reason_for_change = 1; // requested by this client
  1308. var status_code = 0; // No error
  1309. send_fbu_msg([{ x: reason_for_change, y: status_code,
  1310. width: 20, height: 50, encoding: -308 }],
  1311. make_screen_data(1), client);
  1312. expect(client._supportsSetDesktopSize).to.be.true;
  1313. expect(client._fb_width).to.equal(20);
  1314. expect(client._fb_height).to.equal(50);
  1315. expect(client._display.resize).to.have.been.calledOnce;
  1316. expect(client._display.resize).to.have.been.calledWith(20, 50);
  1317. var spy = client.get_onFBResize();
  1318. expect(spy).to.have.been.calledOnce;
  1319. expect(spy).to.have.been.calledWith(sinon.match.any, 20, 50);
  1320. });
  1321. it('should handle a resize requested by another client', function () {
  1322. var reason_for_change = 2; // requested by another client
  1323. var status_code = 0; // No error
  1324. send_fbu_msg([{ x: reason_for_change, y: status_code,
  1325. width: 20, height: 50, encoding: -308 }],
  1326. make_screen_data(1), client);
  1327. expect(client._supportsSetDesktopSize).to.be.true;
  1328. expect(client._fb_width).to.equal(20);
  1329. expect(client._fb_height).to.equal(50);
  1330. expect(client._display.resize).to.have.been.calledOnce;
  1331. expect(client._display.resize).to.have.been.calledWith(20, 50);
  1332. var spy = client.get_onFBResize();
  1333. expect(spy).to.have.been.calledOnce;
  1334. expect(spy).to.have.been.calledWith(sinon.match.any, 20, 50);
  1335. });
  1336. it('should be able to recieve requests which contain data for multiple screens', function () {
  1337. var reason_for_change = 2; // requested by another client
  1338. var status_code = 0; // No error
  1339. send_fbu_msg([{ x: reason_for_change, y: status_code,
  1340. width: 60, height: 50, encoding: -308 }],
  1341. make_screen_data(3), client);
  1342. expect(client._supportsSetDesktopSize).to.be.true;
  1343. expect(client._fb_width).to.equal(60);
  1344. expect(client._fb_height).to.equal(50);
  1345. expect(client._display.resize).to.have.been.calledOnce;
  1346. expect(client._display.resize).to.have.been.calledWith(60, 50);
  1347. var spy = client.get_onFBResize();
  1348. expect(spy).to.have.been.calledOnce;
  1349. expect(spy).to.have.been.calledWith(sinon.match.any, 60, 50);
  1350. });
  1351. it('should not handle a failed request', function () {
  1352. var reason_for_change = 1; // requested by this client
  1353. var status_code = 1; // Resize is administratively prohibited
  1354. send_fbu_msg([{ x: reason_for_change, y: status_code,
  1355. width: 20, height: 50, encoding: -308 }],
  1356. make_screen_data(1), client);
  1357. expect(client._fb_width).to.equal(4);
  1358. expect(client._fb_height).to.equal(4);
  1359. expect(client._display.resize).to.not.have.been.called;
  1360. var spy = client.get_onFBResize();
  1361. expect(spy).to.not.have.been.called;
  1362. });
  1363. });
  1364. it.skip('should handle the Cursor pseudo-encoding', function () {
  1365. // TODO(directxman12): test
  1366. });
  1367. it('should handle the last_rect pseudo-encoding', function () {
  1368. client.set_onFBUReceive(sinon.spy());
  1369. send_fbu_msg([{ x: 0, y: 0, width: 0, height: 0, encoding: -224}], [[]], client, 100);
  1370. expect(client._FBU.rects).to.equal(0);
  1371. expect(client.get_onFBUReceive()).to.have.been.calledOnce;
  1372. });
  1373. });
  1374. });
  1375. it('should set the colour map on the display on SetColourMapEntries', function () {
  1376. var expected_cm = [];
  1377. var data = [1, 0, 0, 1, 0, 4];
  1378. var i;
  1379. for (i = 0; i < 4; i++) {
  1380. expected_cm[i + 1] = [i * 10, i * 10 + 1, i * 10 + 2];
  1381. data.push16(expected_cm[i + 1][2] << 8);
  1382. data.push16(expected_cm[i + 1][1] << 8);
  1383. data.push16(expected_cm[i + 1][0] << 8);
  1384. }
  1385. client._sock._websocket._receive_data(new Uint8Array(data));
  1386. expect(client._display.get_colourMap()).to.deep.equal(expected_cm);
  1387. });
  1388. describe('XVP Message Handling', function () {
  1389. beforeEach(function () {
  1390. client = make_rfb();
  1391. client.connect('host', 8675);
  1392. client._sock._websocket._open();
  1393. client._rfb_state = 'normal';
  1394. client._fb_name = 'some device';
  1395. client._fb_width = 27;
  1396. client._fb_height = 32;
  1397. });
  1398. it('should call updateState with a message on XVP_FAIL, but keep the same state', function () {
  1399. client._updateState = sinon.spy();
  1400. client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 0]));
  1401. expect(client._updateState).to.have.been.calledOnce;
  1402. expect(client._updateState).to.have.been.calledWith('normal', 'Operation Failed');
  1403. });
  1404. it('should set the XVP version and fire the callback with the version on XVP_INIT', function () {
  1405. client.set_onXvpInit(sinon.spy());
  1406. client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 1]));
  1407. expect(client._rfb_xvp_ver).to.equal(10);
  1408. expect(client.get_onXvpInit()).to.have.been.calledOnce;
  1409. expect(client.get_onXvpInit()).to.have.been.calledWith(10);
  1410. });
  1411. it('should fail on unknown XVP message types', function () {
  1412. client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 237]));
  1413. expect(client._rfb_state).to.equal('failed');
  1414. });
  1415. });
  1416. it('should fire the clipboard callback with the retrieved text on ServerCutText', function () {
  1417. var expected_str = 'cheese!';
  1418. var data = [3, 0, 0, 0];
  1419. data.push32(expected_str.length);
  1420. for (var i = 0; i < expected_str.length; i++) { data.push(expected_str.charCodeAt(i)); }
  1421. client.set_onClipboard(sinon.spy());
  1422. client._sock._websocket._receive_data(new Uint8Array(data));
  1423. var spy = client.get_onClipboard();
  1424. expect(spy).to.have.been.calledOnce;
  1425. expect(spy.args[0][1]).to.equal(expected_str);
  1426. });
  1427. it('should fire the bell callback on Bell', function () {
  1428. client.set_onBell(sinon.spy());
  1429. client._sock._websocket._receive_data(new Uint8Array([2]));
  1430. expect(client.get_onBell()).to.have.been.calledOnce;
  1431. });
  1432. it('should fail on an unknown message type', function () {
  1433. client._sock._websocket._receive_data(new Uint8Array([87]));
  1434. expect(client._rfb_state).to.equal('failed');
  1435. });
  1436. });
  1437. describe('Asynchronous Events', function () {
  1438. describe('Mouse event handlers', function () {
  1439. var client;
  1440. beforeEach(function () {
  1441. client = make_rfb();
  1442. client._sock = new Websock();
  1443. client._sock.open('ws://', 'binary');
  1444. client._sock._websocket._open();
  1445. sinon.spy(client._sock, 'flush');
  1446. client._rfb_state = 'normal';
  1447. });
  1448. it('should not send button messages in view-only mode', function () {
  1449. client._view_only = true;
  1450. client._mouse._onMouseButton(0, 0, 1, 0x001);
  1451. expect(client._sock.flush).to.not.have.been.called;
  1452. });
  1453. it('should not send movement messages in view-only mode', function () {
  1454. client._view_only = true;
  1455. client._mouse._onMouseMove(0, 0);
  1456. expect(client._sock.flush).to.not.have.been.called;
  1457. });
  1458. it('should send a pointer event on mouse button presses', function () {
  1459. client._mouse._onMouseButton(10, 12, 1, 0x001);
  1460. var pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0};
  1461. RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x001);
  1462. expect(client._sock).to.have.sent(pointer_msg._sQ);
  1463. });
  1464. it('should send a mask of 1 on mousedown', function () {
  1465. client._mouse._onMouseButton(10, 12, 1, 0x001);
  1466. var pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0};
  1467. RFB.messages.pointerEvent(pointer_msg, 0, 10, 12, 0x001);
  1468. expect(client._sock).to.have.sent(pointer_msg._sQ);
  1469. });
  1470. it('should send a mask of 0 on mouseup', function () {
  1471. client._mouse_buttonMask = 0x001;
  1472. client._mouse._onMouseButton(10, 12, 0, 0x001);
  1473. var pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0};
  1474. RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x000);
  1475. expect(client._sock).to.have.sent(pointer_msg._sQ);
  1476. });
  1477. it('should send a pointer event on mouse movement', function () {
  1478. client._mouse._onMouseMove(10, 12);
  1479. var pointer_msg = {_sQ: new Uint8Array(6), _sQlen: 0};
  1480. RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x000);
  1481. expect(client._sock).to.have.sent(pointer_msg._sQ);
  1482. });
  1483. it('should set the button mask so that future mouse movements use it', function () {
  1484. client._mouse._onMouseButton(10, 12, 1, 0x010);
  1485. client._mouse._onMouseMove(13, 9);
  1486. var pointer_msg = {_sQ: new Uint8Array(12), _sQlen: 0};
  1487. RFB.messages.pointerEvent(pointer_msg, 10, 12, 0x010);
  1488. RFB.messages.pointerEvent(pointer_msg, 13, 9, 0x010);
  1489. expect(client._sock).to.have.sent(pointer_msg._sQ);
  1490. });
  1491. // NB(directxman12): we don't need to test not sending messages in
  1492. // non-normal modes, since we haven't grabbed input
  1493. // yet (grabbing input should be checked in the lifecycle tests).
  1494. it('should not send movement messages when viewport dragging', function () {
  1495. client._viewportDragging = true;
  1496. client._display.viewportChangePos = sinon.spy();
  1497. client._mouse._onMouseMove(13, 9);
  1498. expect(client._sock.flush).to.not.have.been.called;
  1499. });
  1500. it('should not send button messages when initiating viewport dragging', function () {
  1501. client._viewportDrag = true;
  1502. client._mouse._onMouseButton(13, 9, 0x001);
  1503. expect(client._sock.flush).to.not.have.been.called;
  1504. });
  1505. it('should be initiate viewport dragging on a button down event, if enabled', function () {
  1506. client._viewportDrag = true;
  1507. client._mouse._onMouseButton(13, 9, 0x001);
  1508. expect(client._viewportDragging).to.be.true;
  1509. expect(client._viewportDragPos).to.deep.equal({ x: 13, y: 9 });
  1510. });
  1511. it('should terminate viewport dragging on a button up event, if enabled', function () {
  1512. client._viewportDrag = true;
  1513. client._viewportDragging = true;
  1514. client._mouse._onMouseButton(13, 9, 0x000);
  1515. expect(client._viewportDragging).to.be.false;
  1516. });
  1517. it('if enabled, viewportDragging should occur on mouse movement while a button is down', function () {
  1518. client._viewportDrag = true;
  1519. client._viewportDragging = true;
  1520. client._viewportDragPos = { x: 13, y: 9 };
  1521. client._display.viewportChangePos = sinon.spy();
  1522. client._mouse._onMouseMove(10, 4);
  1523. expect(client._viewportDragging).to.be.true;
  1524. expect(client._viewportDragPos).to.deep.equal({ x: 10, y: 4 });
  1525. expect(client._display.viewportChangePos).to.have.been.calledOnce;
  1526. expect(client._display.viewportChangePos).to.have.been.calledWith(3, 5);
  1527. });
  1528. });
  1529. describe('Keyboard Event Handlers', function () {
  1530. var client;
  1531. beforeEach(function () {
  1532. client = make_rfb();
  1533. client._sock = new Websock();
  1534. client._sock.open('ws://', 'binary');
  1535. client._sock._websocket._open();
  1536. sinon.spy(client._sock, 'flush');
  1537. });
  1538. it('should send a key message on a key press', function () {
  1539. client._keyboard._onKeyPress(1234, 1);
  1540. var key_msg = {_sQ: new Uint8Array(8), _sQlen: 0};
  1541. RFB.messages.keyEvent(key_msg, 1234, 1);
  1542. expect(client._sock).to.have.sent(key_msg._sQ);
  1543. });
  1544. it('should not send messages in view-only mode', function () {
  1545. client._view_only = true;
  1546. client._keyboard._onKeyPress(1234, 1);
  1547. expect(client._sock.flush).to.not.have.been.called;
  1548. });
  1549. });
  1550. describe('WebSocket event handlers', function () {
  1551. var client;
  1552. beforeEach(function () {
  1553. client = make_rfb();
  1554. this.clock = sinon.useFakeTimers();
  1555. });
  1556. afterEach(function () { this.clock.restore(); });
  1557. // message events
  1558. it ('should do nothing if we receive an empty message and have nothing in the queue', function () {
  1559. client.connect('host', 8675);
  1560. client._rfb_state = 'normal';
  1561. client._normal_msg = sinon.spy();
  1562. client._sock._websocket._receive_data(new Uint8Array([]));
  1563. expect(client._normal_msg).to.not.have.been.called;
  1564. });
  1565. it('should handle a message in the normal state as a normal message', function () {
  1566. client.connect('host', 8675);
  1567. client._rfb_state = 'normal';
  1568. client._normal_msg = sinon.spy();
  1569. client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
  1570. expect(client._normal_msg).to.have.been.calledOnce;
  1571. });
  1572. it('should handle a message in any non-disconnected/failed state like an init message', function () {
  1573. client.connect('host', 8675);
  1574. client._rfb_state = 'ProtocolVersion';
  1575. client._init_msg = sinon.spy();
  1576. client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
  1577. expect(client._init_msg).to.have.been.calledOnce;
  1578. });
  1579. it('should split up the handling of muplitle normal messages across 10ms intervals', function () {
  1580. client.connect('host', 8675);
  1581. client._sock._websocket._open();
  1582. client._rfb_state = 'normal';
  1583. client.set_onBell(sinon.spy());
  1584. client._sock._websocket._receive_data(new Uint8Array([0x02, 0x02]));
  1585. expect(client.get_onBell()).to.have.been.calledOnce;
  1586. this.clock.tick(20);
  1587. expect(client.get_onBell()).to.have.been.calledTwice;
  1588. });
  1589. // open events
  1590. it('should update the state to ProtocolVersion on open (if the state is "connect")', function () {
  1591. client.connect('host', 8675);
  1592. client._sock._websocket._open();
  1593. expect(client._rfb_state).to.equal('ProtocolVersion');
  1594. });
  1595. it('should fail if we are not currently ready to connect and we get an "open" event', function () {
  1596. client.connect('host', 8675);
  1597. client._rfb_state = 'some_other_state';
  1598. client._sock._websocket._open();
  1599. expect(client._rfb_state).to.equal('failed');
  1600. });
  1601. // close events
  1602. it('should transition to "disconnected" from "disconnect" on a close event', function () {
  1603. client.connect('host', 8675);
  1604. client._rfb_state = 'disconnect';
  1605. client._sock._websocket.close();
  1606. expect(client._rfb_state).to.equal('disconnected');
  1607. });
  1608. it('should transition to failed if we get a close event from any non-"disconnection" state', function () {
  1609. client.connect('host', 8675);
  1610. client._rfb_state = 'normal';
  1611. client._sock._websocket.close();
  1612. expect(client._rfb_state).to.equal('failed');
  1613. });
  1614. it('should unregister close event handler', function () {
  1615. sinon.spy(client._sock, 'off');
  1616. client.connect('host', 8675);
  1617. client._rfb_state = 'disconnect';
  1618. client._sock._websocket.close();
  1619. expect(client._sock.off).to.have.been.calledWith('close');
  1620. });
  1621. // error events do nothing
  1622. });
  1623. });
  1624. });