test.rfb.js 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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("#requestDesktopSize", 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.requestDesktopSize(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.requestDesktopSize(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.requestDesktopSize(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._viewportHasMoved = false;
  1521. client._viewportDragPos = { x: 23, y: 9 };
  1522. client._display.viewportChangePos = sinon.spy();
  1523. client._mouse._onMouseMove(10, 4);
  1524. expect(client._viewportDragging).to.be.true;
  1525. expect(client._viewportHasMoved).to.be.true;
  1526. expect(client._viewportDragPos).to.deep.equal({ x: 10, y: 4 });
  1527. expect(client._display.viewportChangePos).to.have.been.calledOnce;
  1528. expect(client._display.viewportChangePos).to.have.been.calledWith(13, 5);
  1529. });
  1530. });
  1531. describe('Keyboard Event Handlers', function () {
  1532. var client;
  1533. beforeEach(function () {
  1534. client = make_rfb();
  1535. client._sock = new Websock();
  1536. client._sock.open('ws://', 'binary');
  1537. client._sock._websocket._open();
  1538. sinon.spy(client._sock, 'flush');
  1539. });
  1540. it('should send a key message on a key press', function () {
  1541. client._keyboard._onKeyPress(1234, 1);
  1542. var key_msg = {_sQ: new Uint8Array(8), _sQlen: 0};
  1543. RFB.messages.keyEvent(key_msg, 1234, 1);
  1544. expect(client._sock).to.have.sent(key_msg._sQ);
  1545. });
  1546. it('should not send messages in view-only mode', function () {
  1547. client._view_only = true;
  1548. client._keyboard._onKeyPress(1234, 1);
  1549. expect(client._sock.flush).to.not.have.been.called;
  1550. });
  1551. });
  1552. describe('WebSocket event handlers', function () {
  1553. var client;
  1554. beforeEach(function () {
  1555. client = make_rfb();
  1556. this.clock = sinon.useFakeTimers();
  1557. });
  1558. afterEach(function () { this.clock.restore(); });
  1559. // message events
  1560. it ('should do nothing if we receive an empty message and have nothing in the queue', function () {
  1561. client.connect('host', 8675);
  1562. client._rfb_state = 'normal';
  1563. client._normal_msg = sinon.spy();
  1564. client._sock._websocket._receive_data(new Uint8Array([]));
  1565. expect(client._normal_msg).to.not.have.been.called;
  1566. });
  1567. it('should handle a message in the normal state as a normal message', function () {
  1568. client.connect('host', 8675);
  1569. client._rfb_state = 'normal';
  1570. client._normal_msg = sinon.spy();
  1571. client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
  1572. expect(client._normal_msg).to.have.been.calledOnce;
  1573. });
  1574. it('should handle a message in any non-disconnected/failed state like an init message', function () {
  1575. client.connect('host', 8675);
  1576. client._rfb_state = 'ProtocolVersion';
  1577. client._init_msg = sinon.spy();
  1578. client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
  1579. expect(client._init_msg).to.have.been.calledOnce;
  1580. });
  1581. it('should split up the handling of muplitle normal messages across 10ms intervals', function () {
  1582. client.connect('host', 8675);
  1583. client._sock._websocket._open();
  1584. client._rfb_state = 'normal';
  1585. client.set_onBell(sinon.spy());
  1586. client._sock._websocket._receive_data(new Uint8Array([0x02, 0x02]));
  1587. expect(client.get_onBell()).to.have.been.calledOnce;
  1588. this.clock.tick(20);
  1589. expect(client.get_onBell()).to.have.been.calledTwice;
  1590. });
  1591. // open events
  1592. it('should update the state to ProtocolVersion on open (if the state is "connect")', function () {
  1593. client.connect('host', 8675);
  1594. client._sock._websocket._open();
  1595. expect(client._rfb_state).to.equal('ProtocolVersion');
  1596. });
  1597. it('should fail if we are not currently ready to connect and we get an "open" event', function () {
  1598. client.connect('host', 8675);
  1599. client._rfb_state = 'some_other_state';
  1600. client._sock._websocket._open();
  1601. expect(client._rfb_state).to.equal('failed');
  1602. });
  1603. // close events
  1604. it('should transition to "disconnected" from "disconnect" on a close event', function () {
  1605. client.connect('host', 8675);
  1606. client._rfb_state = 'disconnect';
  1607. client._sock._websocket.close();
  1608. expect(client._rfb_state).to.equal('disconnected');
  1609. });
  1610. it('should transition to failed if we get a close event from any non-"disconnection" state', function () {
  1611. client.connect('host', 8675);
  1612. client._rfb_state = 'normal';
  1613. client._sock._websocket.close();
  1614. expect(client._rfb_state).to.equal('failed');
  1615. });
  1616. it('should unregister close event handler', function () {
  1617. sinon.spy(client._sock, 'off');
  1618. client.connect('host', 8675);
  1619. client._rfb_state = 'disconnect';
  1620. client._sock._websocket.close();
  1621. expect(client._sock.off).to.have.been.calledWith('close');
  1622. });
  1623. // error events do nothing
  1624. });
  1625. });
  1626. });