test.rfb.js 86 KB

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