omemo.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. (function (root, factory) {
  2. define(["jasmine", "mock", "test-utils"], factory);
  3. } (this, function (jasmine, mock, test_utils) {
  4. var Strophe = converse.env.Strophe;
  5. var b64_sha1 = converse.env.b64_sha1;
  6. var $iq = converse.env.$iq;
  7. var $msg = converse.env.$msg;
  8. var _ = converse.env._;
  9. var u = converse.env.utils;
  10. function deviceListFetched (_converse, jid) {
  11. return _.get(_.filter(
  12. _converse.connection.IQ_stanzas,
  13. iq => iq.nodeTree.querySelector(`iq[to="${jid}"] items[node="eu.siacs.conversations.axolotl.devicelist"]`)
  14. ).pop(), 'nodeTree');
  15. }
  16. function ownDeviceHasBeenPublished (_converse) {
  17. return _.get(_.filter(
  18. _converse.connection.IQ_stanzas,
  19. iq => iq.nodeTree.querySelector('iq[from="'+_converse.bare_jid+'"] publish[node="eu.siacs.conversations.axolotl.devicelist"]')
  20. ).pop(), 'nodeTree');
  21. }
  22. function bundleHasBeenPublished (_converse) {
  23. return _.get(_.filter(
  24. _converse.connection.IQ_stanzas,
  25. iq => iq.nodeTree.querySelector('publish[node="eu.siacs.conversations.axolotl.bundles:123456789"]')
  26. ).pop(), 'nodeTree');
  27. }
  28. function bundleFetched (_converse, jid, device_id) {
  29. return _.get(_.filter(
  30. _converse.connection.IQ_stanzas,
  31. (iq) => iq.nodeTree.querySelector(`iq[to="${jid}"] items[node="eu.siacs.conversations.axolotl.bundles:${device_id}"]`)
  32. ).pop(), 'nodeTree');
  33. }
  34. function initializedOMEMO (_converse) {
  35. return test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid))
  36. .then(iq_stanza => {
  37. const stanza = $iq({
  38. 'from': _converse.bare_jid,
  39. 'id': iq_stanza.getAttribute('id'),
  40. 'to': _converse.bare_jid,
  41. 'type': 'result',
  42. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  43. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  44. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  45. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  46. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  47. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  48. return test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse))
  49. }).then(iq_stanza => {
  50. const stanza = $iq({
  51. 'from': _converse.bare_jid,
  52. 'id': iq_stanza.getAttribute('id'),
  53. 'to': _converse.bare_jid,
  54. 'type': 'result'});
  55. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  56. return test_utils.waitUntil(() => bundleHasBeenPublished(_converse))
  57. }).then(iq_stanza => {
  58. const stanza = $iq({
  59. 'from': _converse.bare_jid,
  60. 'id': iq_stanza.getAttribute('id'),
  61. 'to': _converse.bare_jid,
  62. 'type': 'result'});
  63. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  64. return _converse.api.waitUntil('OMEMOInitialized');
  65. });
  66. }
  67. describe("The OMEMO module", function() {
  68. it("adds methods for encrypting and decrypting messages via AES GCM",
  69. mock.initConverseWithPromises(
  70. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  71. function (done, _converse) {
  72. const message = 'This message will be encrypted'
  73. let view;
  74. test_utils.createContacts(_converse, 'current', 1);
  75. _converse.emit('rosterContactsFetched');
  76. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  77. test_utils.openChatBoxFor(_converse, contact_jid)
  78. .then((v) => {
  79. view = v;
  80. return view.model.encryptMessage(message);
  81. }).then(payload => {
  82. return view.model.decryptMessage(payload);
  83. }).then(result => {
  84. expect(result).toBe(message);
  85. done();
  86. });
  87. }));
  88. it("enables encrypted messages to be sent and received",
  89. mock.initConverseWithPromises(
  90. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  91. function (done, _converse) {
  92. let view, sent_stanza;
  93. test_utils.createContacts(_converse, 'current', 1);
  94. _converse.emit('rosterContactsFetched');
  95. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  96. return test_utils.waitUntil(() => initializedOMEMO(_converse))
  97. .then(() => test_utils.openChatBoxFor(_converse, contact_jid))
  98. .then(() => test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid)))
  99. .then(iq_stanza => {
  100. const stanza = $iq({
  101. 'from': contact_jid,
  102. 'id': iq_stanza.getAttribute('id'),
  103. 'to': _converse.connection.jid,
  104. 'type': 'result',
  105. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  106. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  107. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  108. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  109. .c('device', {'id': '555'});
  110. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  111. return test_utils.waitUntil(() => _converse.omemo_store);
  112. }).then(() => {
  113. const devicelist = _converse.devicelists.get({'jid': contact_jid});
  114. expect(devicelist.devices.length).toBe(1);
  115. view = _converse.chatboxviews.get(contact_jid);
  116. view.model.set('omemo_active', true);
  117. const textarea = view.el.querySelector('.chat-textarea');
  118. textarea.value = 'This message will be encrypted';
  119. view.keyPressed({
  120. target: textarea,
  121. preventDefault: _.noop,
  122. keyCode: 13 // Enter
  123. });
  124. return test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '555'));
  125. }).then((iq_stanza) => {
  126. const stanza = $iq({
  127. 'from': contact_jid,
  128. 'id': iq_stanza.getAttribute('id'),
  129. 'to': _converse.bare_jid,
  130. 'type': 'result',
  131. }).c('pubsub', {
  132. 'xmlns': 'http://jabber.org/protocol/pubsub'
  133. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
  134. .c('item')
  135. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  136. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  137. .c('signedPreKeySignature').t(btoa('2222')).up()
  138. .c('identityKey').t(btoa('3333')).up()
  139. .c('prekeys')
  140. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  141. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  142. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  143. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  144. return test_utils.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'));
  145. }).then(iq_stanza => {
  146. const stanza = $iq({
  147. 'from': _converse.bare_jid,
  148. 'id': iq_stanza.getAttribute('id'),
  149. 'to': _converse.bare_jid,
  150. 'type': 'result',
  151. }).c('pubsub', {
  152. 'xmlns': 'http://jabber.org/protocol/pubsub'
  153. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  154. .c('item')
  155. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  156. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  157. .c('signedPreKeySignature').t(btoa('200000')).up()
  158. .c('identityKey').t(btoa('300000')).up()
  159. .c('prekeys')
  160. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  161. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  162. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  163. spyOn(_converse.connection, 'send').and.callFake(stanza => { sent_stanza = stanza });
  164. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  165. return test_utils.waitUntil(() => sent_stanza);
  166. }).then(() => {
  167. expect(sent_stanza.toLocaleString()).toBe(
  168. `<message from='dummy@localhost/resource' to='max.frankfurter@localhost' `+
  169. `type='chat' id='${sent_stanza.nodeTree.getAttribute('id')}' xmlns='jabber:client'>`+
  170. `<body>This is an OMEMO encrypted message which your client doesn’t seem to support. Find more information on https://conversations.im/omemo</body>`+
  171. `<encrypted xmlns='eu.siacs.conversations.axolotl'>`+
  172. `<header sid='123456789'>`+
  173. `<key rid='482886413b977930064a5888b92134fe'>YzFwaDNSNzNYNw==</key>`+
  174. `<key rid='555'>YzFwaDNSNzNYNw==</key>`+
  175. `<iv>${sent_stanza.nodeTree.querySelector('iv').textContent}</iv>`+
  176. `</header>`+
  177. `<payload>${sent_stanza.nodeTree.querySelector('payload').textContent}</payload>`+
  178. `</encrypted>`+
  179. `<store xmlns='urn:xmpp:hints'/>`+
  180. `</message>`);
  181. // Test reception of an encrypted message
  182. return view.model.encryptMessage('This is an encrypted message from the contact')
  183. }).then(obj => {
  184. // XXX: Normally the key will be encrypted via libsignal.
  185. // However, we're mocking libsignal in the tests, so we include
  186. // it as plaintext in the message.
  187. // u.stringToArrayBuffer(atob(u.arrayBufferToBase64(obj.key_and_tag)));
  188. // u.stringToArrayBuffer(u.arrayBufferToString(obj.key_and_tag));
  189. const stanza = $msg({
  190. 'from': contact_jid,
  191. 'to': _converse.connection.jid,
  192. 'type': 'chat',
  193. 'id': 'qwerty'
  194. }).c('body').t('This is a fallback message').up()
  195. .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  196. .c('header', {'sid': '555'})
  197. .c('key', {'rid': _converse.omemo_store.get('device_id')}).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  198. .c('iv').t(obj.iv)
  199. .up().up()
  200. .c('payload').t(obj.payload);
  201. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  202. return test_utils.waitUntil(() => view.model.messages.length > 1);
  203. }).then(() => {
  204. expect(view.model.messages.length).toBe(2);
  205. const last_msg = view.model.messages.at(1);
  206. expect(view.el.querySelectorAll('.chat-msg__body')[1].textContent.trim())
  207. .toBe('This is an encrypted message from the contact');
  208. done();
  209. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
  210. }));
  211. it("can receive a PreKeySignalMessage",
  212. mock.initConverseWithPromises(
  213. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  214. function (done, _converse) {
  215. _converse.NUM_PREKEYS = 5; // Restrict to 5, otherwise the resulting stanza is too large to easily test
  216. let view, sent_stanza;
  217. test_utils.createContacts(_converse, 'current', 1);
  218. _converse.emit('rosterContactsFetched');
  219. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  220. return test_utils.waitUntil(() => initializedOMEMO(_converse))
  221. .then(() => _converse.ChatBox.prototype.encryptMessage('This is an encrypted message from the contact'))
  222. .then(obj => {
  223. // XXX: Normally the key will be encrypted via libsignal.
  224. // However, we're mocking libsignal in the tests, so we include
  225. // it as plaintext in the message.
  226. const stanza = $msg({
  227. 'from': contact_jid,
  228. 'to': _converse.connection.jid,
  229. 'type': 'chat',
  230. 'id': 'qwerty'
  231. }).c('body').t('This is a fallback message').up()
  232. .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  233. .c('header', {'sid': '555'})
  234. .c('key', {
  235. 'prekey': 'true',
  236. 'rid': _converse.omemo_store.get('device_id')
  237. }).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  238. .c('iv').t(obj.iv)
  239. .up().up()
  240. .c('payload').t(obj.payload);
  241. const generateMissingPreKeys = _converse.omemo_store.generateMissingPreKeys;
  242. spyOn(_converse.omemo_store, 'generateMissingPreKeys').and.callFake(() => {
  243. // Since it's difficult to override
  244. // decryptPreKeyWhisperMessage, where a prekey will be
  245. // removed from the store, we do it here, before the
  246. // missing prekeys are generated.
  247. _converse.omemo_store.removePreKey(1);
  248. return generateMissingPreKeys.apply(_converse.omemo_store, arguments);
  249. });
  250. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  251. return test_utils.waitUntil(() => _converse.chatboxviews.get(contact_jid))
  252. }).then(iq_stanza => deviceListFetched(_converse, contact_jid))
  253. .then(iq_stanza => {
  254. const stanza = $iq({
  255. 'from': contact_jid,
  256. 'id': iq_stanza.getAttribute('id'),
  257. 'to': _converse.connection.jid,
  258. 'type': 'result',
  259. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  260. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  261. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  262. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  263. .c('device', {'id': '555'});
  264. // XXX: the bundle gets published twice, we want to make sure
  265. // that we wait for the 2nd, so we clear all the already sent
  266. // stanzas.
  267. _converse.connection.IQ_stanzas = [];
  268. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  269. return test_utils.waitUntil(() => _converse.omemo_store);
  270. }).then(() => test_utils.waitUntil(() => bundleHasBeenPublished(_converse)))
  271. .then(iq_stanza => {
  272. expect(iq_stanza.outerHTML).toBe(
  273. `<iq from="dummy@localhost" type="set" xmlns="jabber:client" id="${iq_stanza.getAttribute('id')}">`+
  274. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  275. `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
  276. `<item>`+
  277. `<bundle xmlns="eu.siacs.conversations.axolotl">`+
  278. `<signedPreKeyPublic signedPreKeyId="0">${btoa('1234')}</signedPreKeyPublic>`+
  279. `<signedPreKeySignature>${btoa('11112222333344445555')}</signedPreKeySignature>`+
  280. `<identityKey>${btoa('1234')}</identityKey>`+
  281. `<prekeys>`+
  282. `<preKeyPublic preKeyId="0">${btoa('1234')}</preKeyPublic>`+
  283. `<preKeyPublic preKeyId="1">${btoa('1234')}</preKeyPublic>`+
  284. `<preKeyPublic preKeyId="2">${btoa('1234')}</preKeyPublic>`+
  285. `<preKeyPublic preKeyId="3">${btoa('1234')}</preKeyPublic>`+
  286. `<preKeyPublic preKeyId="4">${btoa('1234')}</preKeyPublic>`+
  287. `</prekeys>`+
  288. `</bundle>`+
  289. `</item>`+
  290. `</publish>`+
  291. `</pubsub>`+
  292. `</iq>`)
  293. const own_device = _converse.devicelists.get(_converse.bare_jid).devices.get(_converse.omemo_store.get('device_id'));
  294. expect(own_device.get('bundle').prekeys.length).toBe(5);
  295. expect(_converse.omemo_store.generateMissingPreKeys).toHaveBeenCalled();
  296. done();
  297. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
  298. }));
  299. it("updates device lists based on PEP messages",
  300. mock.initConverseWithPromises(
  301. null, ['rosterGroupsFetched'], {},
  302. function (done, _converse) {
  303. test_utils.createContacts(_converse, 'current', 1);
  304. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  305. // Wait until own devices are fetched
  306. test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid))
  307. .then(iq_stanza => {
  308. expect(iq_stanza.outerHTML).toBe(
  309. '<iq type="get" from="dummy@localhost" to="dummy@localhost" xmlns="jabber:client" id="'+iq_stanza.getAttribute("id")+'">'+
  310. '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
  311. '<items node="eu.siacs.conversations.axolotl.devicelist"/>'+
  312. '</pubsub>'+
  313. '</iq>');
  314. const stanza = $iq({
  315. 'from': _converse.bare_jid,
  316. 'id': iq_stanza.getAttribute('id'),
  317. 'to': _converse.bare_jid,
  318. 'type': 'result',
  319. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  320. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  321. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  322. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  323. .c('device', {'id': '555'});
  324. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  325. return test_utils.waitUntil(() => _converse.omemo_store);
  326. }).then(() => {
  327. expect(_converse.devicelists.length).toBe(1);
  328. const devicelist = _converse.devicelists.get(_converse.bare_jid);
  329. expect(devicelist.devices.length).toBe(2);
  330. expect(devicelist.devices.at(0).get('id')).toBe('555');
  331. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  332. return test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  333. }).then(iq_stanza => {
  334. const stanza = $iq({
  335. 'from': _converse.bare_jid,
  336. 'id': iq_stanza.getAttribute('id'),
  337. 'to': _converse.bare_jid,
  338. 'type': 'result'});
  339. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  340. return test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  341. }).then(iq_stanza => {
  342. const stanza = $iq({
  343. 'from': _converse.bare_jid,
  344. 'id': iq_stanza.getAttribute('id'),
  345. 'to': _converse.bare_jid,
  346. 'type': 'result'});
  347. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  348. return _converse.api.waitUntil('OMEMOInitialized');
  349. }).then(() => {
  350. let stanza = $msg({
  351. 'from': contact_jid,
  352. 'to': _converse.bare_jid,
  353. 'type': 'headline',
  354. 'id': 'update_01',
  355. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  356. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  357. .c('item')
  358. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  359. .c('device', {'id': '1234'})
  360. .c('device', {'id': '4223'})
  361. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  362. expect(_converse.devicelists.length).toBe(2);
  363. let devices = _converse.devicelists.get(contact_jid).devices;
  364. expect(devices.length).toBe(2);
  365. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('1234,4223');
  366. stanza = $msg({
  367. 'from': contact_jid,
  368. 'to': _converse.bare_jid,
  369. 'type': 'headline',
  370. 'id': 'update_02',
  371. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  372. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  373. .c('item')
  374. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  375. .c('device', {'id': '4223'})
  376. .c('device', {'id': '4224'})
  377. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  378. expect(_converse.devicelists.length).toBe(2);
  379. expect(devices.length).toBe(2);
  380. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('4223,4224');
  381. // Check that own devicelist gets updated
  382. stanza = $msg({
  383. 'from': _converse.bare_jid,
  384. 'to': _converse.bare_jid,
  385. 'type': 'headline',
  386. 'id': 'update_03',
  387. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  388. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  389. .c('item')
  390. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  391. .c('device', {'id': '123456789'})
  392. .c('device', {'id': '555'})
  393. .c('device', {'id': '777'})
  394. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  395. expect(_converse.devicelists.length).toBe(2);
  396. devices = _converse.devicelists.get(_converse.bare_jid).devices;
  397. expect(devices.length).toBe(3);
  398. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,555,777');
  399. _converse.connection.IQ_stanzas = [];
  400. // Check that own device gets re-added
  401. stanza = $msg({
  402. 'from': _converse.bare_jid,
  403. 'to': _converse.bare_jid,
  404. 'type': 'headline',
  405. 'id': 'update_04',
  406. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  407. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  408. .c('item')
  409. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  410. .c('device', {'id': '444'})
  411. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  412. return test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  413. }).then(iq_stanza => {
  414. // Check that our own device is added again, but that removed
  415. // devices are not added.
  416. expect(iq_stanza.outerHTML).toBe(
  417. '<iq from="dummy@localhost" type="set" xmlns="jabber:client" id="'+iq_stanza.getAttribute('id')+'">'+
  418. '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
  419. '<publish node="eu.siacs.conversations.axolotl.devicelist">'+
  420. '<item>'+
  421. '<list xmlns="eu.siacs.conversations.axolotl">'+
  422. '<device id="123456789"/>'+
  423. '<device id="444"/>'+
  424. '</list>'+
  425. '</item>'+
  426. '</publish>'+
  427. '</pubsub>'+
  428. '</iq>');
  429. expect(_converse.devicelists.length).toBe(2);
  430. const devices = _converse.devicelists.get(_converse.bare_jid).devices;
  431. // The device id for this device (123456789) was also generated and added to the list,
  432. // which is why we have 2 devices now.
  433. expect(devices.length).toBe(2);
  434. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,444');
  435. done();
  436. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
  437. }));
  438. it("updates device bundles based on PEP messages",
  439. mock.initConverseWithPromises(
  440. null, ['rosterGroupsFetched'], {},
  441. function (done, _converse) {
  442. test_utils.createContacts(_converse, 'current');
  443. const contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
  444. test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid))
  445. .then(iq_stanza => {
  446. expect(iq_stanza.outerHTML).toBe(
  447. '<iq type="get" from="dummy@localhost" to="dummy@localhost" xmlns="jabber:client" id="'+iq_stanza.getAttribute("id")+'">'+
  448. '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
  449. '<items node="eu.siacs.conversations.axolotl.devicelist"/>'+
  450. '</pubsub>'+
  451. '</iq>');
  452. const stanza = $iq({
  453. 'from': contact_jid,
  454. 'id': iq_stanza.getAttribute('id'),
  455. 'to': _converse.bare_jid,
  456. 'type': 'result',
  457. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  458. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  459. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  460. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  461. .c('device', {'id': '555'});
  462. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  463. return test_utils.waitUntil(() => _converse.omemo_store);
  464. }).then(() => {
  465. expect(_converse.devicelists.length).toBe(1);
  466. const devicelist = _converse.devicelists.get(_converse.bare_jid);
  467. expect(devicelist.devices.length).toBe(2);
  468. expect(devicelist.devices.at(0).get('id')).toBe('555');
  469. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  470. return test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  471. }).then(iq_stanza => {
  472. const stanza = $iq({
  473. 'from': _converse.bare_jid,
  474. 'id': iq_stanza.getAttribute('id'),
  475. 'to': _converse.bare_jid,
  476. 'type': 'result'});
  477. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  478. return test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  479. }).then(iq_stanza => {
  480. const stanza = $iq({
  481. 'from': _converse.bare_jid,
  482. 'id': iq_stanza.getAttribute('id'),
  483. 'to': _converse.bare_jid,
  484. 'type': 'result'});
  485. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  486. return _converse.api.waitUntil('OMEMOInitialized');
  487. }).then(() => {
  488. let stanza = $msg({
  489. 'from': contact_jid,
  490. 'to': _converse.bare_jid,
  491. 'type': 'headline',
  492. 'id': 'update_01',
  493. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  494. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
  495. .c('item')
  496. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  497. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('1111').up()
  498. .c('signedPreKeySignature').t('2222').up()
  499. .c('identityKey').t('3333').up()
  500. .c('prekeys')
  501. .c('preKeyPublic', {'preKeyId': '1001'}).up()
  502. .c('preKeyPublic', {'preKeyId': '1002'}).up()
  503. .c('preKeyPublic', {'preKeyId': '1003'});
  504. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  505. expect(_converse.devicelists.length).toBe(2);
  506. let devicelist = _converse.devicelists.get(contact_jid);
  507. expect(devicelist.devices.length).toBe(1);
  508. let device = devicelist.devices.at(0);
  509. expect(device.get('bundle').identity_key).toBe('3333');
  510. expect(device.get('bundle').signed_prekey.public_key).toBe('1111');
  511. expect(device.get('bundle').signed_prekey.id).toBe(4223);
  512. expect(device.get('bundle').signed_prekey.signature).toBe('2222');
  513. expect(device.get('bundle').prekeys.length).toBe(3);
  514. expect(device.get('bundle').prekeys[0].id).toBe(1001);
  515. expect(device.get('bundle').prekeys[1].id).toBe(1002);
  516. expect(device.get('bundle').prekeys[2].id).toBe(1003);
  517. stanza = $msg({
  518. 'from': contact_jid,
  519. 'to': _converse.bare_jid,
  520. 'type': 'headline',
  521. 'id': 'update_02',
  522. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  523. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
  524. .c('item')
  525. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  526. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('5555').up()
  527. .c('signedPreKeySignature').t('6666').up()
  528. .c('identityKey').t('7777').up()
  529. .c('prekeys')
  530. .c('preKeyPublic', {'preKeyId': '2001'}).up()
  531. .c('preKeyPublic', {'preKeyId': '2002'}).up()
  532. .c('preKeyPublic', {'preKeyId': '2003'});
  533. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  534. expect(_converse.devicelists.length).toBe(2);
  535. devicelist = _converse.devicelists.get(contact_jid);
  536. expect(devicelist.devices.length).toBe(1);
  537. device = devicelist.devices.at(0);
  538. expect(device.get('bundle').identity_key).toBe('7777');
  539. expect(device.get('bundle').signed_prekey.public_key).toBe('5555');
  540. expect(device.get('bundle').signed_prekey.id).toBe(4223);
  541. expect(device.get('bundle').signed_prekey.signature).toBe('6666');
  542. expect(device.get('bundle').prekeys.length).toBe(3);
  543. expect(device.get('bundle').prekeys[0].id).toBe(2001);
  544. expect(device.get('bundle').prekeys[1].id).toBe(2002);
  545. expect(device.get('bundle').prekeys[2].id).toBe(2003);
  546. stanza = $msg({
  547. 'from': _converse.bare_jid,
  548. 'to': _converse.bare_jid,
  549. 'type': 'headline',
  550. 'id': 'update_03',
  551. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  552. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:123456789'})
  553. .c('item')
  554. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  555. .c('signedPreKeyPublic', {'signedPreKeyId': '9999'}).t('8888').up()
  556. .c('signedPreKeySignature').t('3333').up()
  557. .c('identityKey').t('1111').up()
  558. .c('prekeys')
  559. .c('preKeyPublic', {'preKeyId': '3001'}).up()
  560. .c('preKeyPublic', {'preKeyId': '3002'}).up()
  561. .c('preKeyPublic', {'preKeyId': '3003'});
  562. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  563. expect(_converse.devicelists.length).toBe(2);
  564. devicelist = _converse.devicelists.get(_converse.bare_jid);
  565. expect(devicelist.devices.length).toBe(2);
  566. expect(devicelist.devices.at(0).get('id')).toBe('555');
  567. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  568. device = devicelist.devices.at(1);
  569. expect(device.get('bundle').identity_key).toBe('1111');
  570. expect(device.get('bundle').signed_prekey.public_key).toBe('8888');
  571. expect(device.get('bundle').signed_prekey.id).toBe(9999);
  572. expect(device.get('bundle').signed_prekey.signature).toBe('3333');
  573. expect(device.get('bundle').prekeys.length).toBe(3);
  574. expect(device.get('bundle').prekeys[0].id).toBe(3001);
  575. expect(device.get('bundle').prekeys[1].id).toBe(3002);
  576. expect(device.get('bundle').prekeys[2].id).toBe(3003);
  577. done();
  578. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
  579. }));
  580. it("publishes a bundle with which an encrypted session can be created",
  581. mock.initConverseWithPromises(
  582. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  583. function (done, _converse) {
  584. _converse.NUM_PREKEYS = 2; // Restrict to 2, otherwise the resulting stanza is too large to easily test
  585. test_utils.createContacts(_converse, 'current', 1);
  586. _converse.emit('rosterContactsFetched');
  587. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  588. test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid))
  589. .then(iq_stanza => {
  590. const stanza = $iq({
  591. 'from': contact_jid,
  592. 'id': iq_stanza.getAttribute('id'),
  593. 'to': _converse.bare_jid,
  594. 'type': 'result',
  595. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  596. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  597. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  598. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  599. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  600. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  601. expect(_converse.devicelists.length).toBe(1);
  602. return test_utils.openChatBoxFor(_converse, contact_jid);
  603. }).then(() => ownDeviceHasBeenPublished(_converse))
  604. .then(iq_stanza => {
  605. const stanza = $iq({
  606. 'from': _converse.bare_jid,
  607. 'id': iq_stanza.getAttribute('id'),
  608. 'to': _converse.bare_jid,
  609. 'type': 'result'});
  610. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  611. return test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  612. }).then(iq_stanza => {
  613. expect(iq_stanza.outerHTML).toBe(
  614. `<iq from="dummy@localhost" type="set" xmlns="jabber:client" id="${iq_stanza.getAttribute('id')}">`+
  615. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  616. `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
  617. `<item>`+
  618. `<bundle xmlns="eu.siacs.conversations.axolotl">`+
  619. `<signedPreKeyPublic signedPreKeyId="0">${btoa('1234')}</signedPreKeyPublic>`+
  620. `<signedPreKeySignature>${btoa('11112222333344445555')}</signedPreKeySignature>`+
  621. `<identityKey>${btoa('1234')}</identityKey>`+
  622. `<prekeys>`+
  623. `<preKeyPublic preKeyId="0">${btoa('1234')}</preKeyPublic>`+
  624. `<preKeyPublic preKeyId="1">${btoa('1234')}</preKeyPublic>`+
  625. `</prekeys>`+
  626. `</bundle>`+
  627. `</item>`+
  628. `</publish>`+
  629. `</pubsub>`+
  630. `</iq>`)
  631. const stanza = $iq({
  632. 'from': _converse.bare_jid,
  633. 'id': iq_stanza.getAttribute('id'),
  634. 'to': _converse.bare_jid,
  635. 'type': 'result'});
  636. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  637. return _converse.api.waitUntil('OMEMOInitialized');
  638. }).then(done).catch(_.partial(console.error, _));
  639. }));
  640. it("adds a toolbar button for starting an encrypted chat session",
  641. mock.initConverseWithPromises(
  642. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  643. function (done, _converse) {
  644. let modal;
  645. test_utils.createContacts(_converse, 'current', 1);
  646. _converse.emit('rosterContactsFetched');
  647. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  648. test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid))
  649. .then(iq_stanza => {
  650. expect(iq_stanza.outerHTML).toBe(
  651. '<iq type="get" from="dummy@localhost" to="dummy@localhost" xmlns="jabber:client" id="'+iq_stanza.getAttribute("id")+'">'+
  652. '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
  653. '<items node="eu.siacs.conversations.axolotl.devicelist"/>'+
  654. '</pubsub>'+
  655. '</iq>');
  656. const stanza = $iq({
  657. 'from': _converse.bare_jid,
  658. 'id': iq_stanza.getAttribute('id'),
  659. 'to': _converse.bare_jid,
  660. 'type': 'result',
  661. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  662. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  663. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  664. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  665. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  666. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  667. return test_utils.waitUntil(() => _converse.omemo_store);
  668. }).then(() => {
  669. expect(_converse.devicelists.length).toBe(1);
  670. const devicelist = _converse.devicelists.get(_converse.bare_jid);
  671. expect(devicelist.devices.length).toBe(2);
  672. expect(devicelist.devices.at(0).get('id')).toBe('482886413b977930064a5888b92134fe');
  673. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  674. // Check that own device was published
  675. return test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  676. }).then(iq_stanza => {
  677. expect(iq_stanza.outerHTML).toBe(
  678. '<iq from="dummy@localhost" type="set" xmlns="jabber:client" id="'+iq_stanza.getAttribute('id')+'">'+
  679. '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
  680. '<publish node="eu.siacs.conversations.axolotl.devicelist">'+
  681. '<item>'+
  682. '<list xmlns="eu.siacs.conversations.axolotl">'+
  683. '<device id="482886413b977930064a5888b92134fe"/>'+
  684. '<device id="123456789"/>'+
  685. '</list>'+
  686. '</item>'+
  687. '</publish>'+
  688. '</pubsub>'+
  689. '</iq>');
  690. const stanza = $iq({
  691. 'from': _converse.bare_jid,
  692. 'id': iq_stanza.getAttribute('id'),
  693. 'to': _converse.bare_jid,
  694. 'type': 'result'});
  695. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  696. return test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  697. }).then(iq_stanza => {
  698. expect(iq_stanza.getAttributeNames().sort().join()).toBe(["from", "type", "xmlns", "id"].sort().join());
  699. expect(iq_stanza.querySelector('prekeys').childNodes.length).toBe(100);
  700. const signed_prekeys = iq_stanza.querySelectorAll('signedPreKeyPublic');
  701. expect(signed_prekeys.length).toBe(1);
  702. const signed_prekey = signed_prekeys[0];
  703. expect(signed_prekey.getAttribute('signedPreKeyId')).toBe('0')
  704. expect(iq_stanza.querySelectorAll('signedPreKeySignature').length).toBe(1);
  705. expect(iq_stanza.querySelectorAll('identityKey').length).toBe(1);
  706. const stanza = $iq({
  707. 'from': _converse.bare_jid,
  708. 'id': iq_stanza.getAttribute('id'),
  709. 'to': _converse.bare_jid,
  710. 'type': 'result'});
  711. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  712. return _converse.api.waitUntil('OMEMOInitialized', 1000);
  713. }).then(() => {
  714. return test_utils.openChatBoxFor(_converse, contact_jid);
  715. }).then(() => {
  716. return test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  717. }).then(iq_stanza => {
  718. expect(iq_stanza.outerHTML).toBe(
  719. '<iq type="get" from="dummy@localhost" to="'+contact_jid+'" xmlns="jabber:client" id="'+iq_stanza.getAttribute("id")+'">'+
  720. '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
  721. '<items node="eu.siacs.conversations.axolotl.devicelist"/>'+
  722. '</pubsub>'+
  723. '</iq>');
  724. const stanza = $iq({
  725. 'from': contact_jid,
  726. 'id': iq_stanza.getAttribute('id'),
  727. 'to': _converse.bare_jid,
  728. 'type': 'result',
  729. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  730. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  731. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  732. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  733. .c('device', {'id': '368866411b877c30064a5f62b917cffe'}).up()
  734. .c('device', {'id': '3300659945416e274474e469a1f0154c'}).up()
  735. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  736. .c('device', {'id': 'ae890ac52d0df67ed7cfdf51b644e901'});
  737. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  738. const devicelist = _converse.devicelists.get(contact_jid);
  739. return test_utils.waitUntil(() => devicelist.devices.length);
  740. }).then(() => {
  741. expect(_converse.devicelists.length).toBe(2);
  742. const devicelist = _converse.devicelists.get(contact_jid);
  743. expect(devicelist.devices.length).toBe(4);
  744. expect(devicelist.devices.at(0).get('id')).toBe('368866411b877c30064a5f62b917cffe');
  745. expect(devicelist.devices.at(1).get('id')).toBe('3300659945416e274474e469a1f0154c');
  746. expect(devicelist.devices.at(2).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  747. expect(devicelist.devices.at(3).get('id')).toBe('ae890ac52d0df67ed7cfdf51b644e901');
  748. return test_utils.waitUntil(() => _converse.chatboxviews.get(contact_jid).el.querySelector('.chat-toolbar'));
  749. }).then(() => {
  750. const view = _converse.chatboxviews.get(contact_jid);
  751. const toolbar = view.el.querySelector('.chat-toolbar');
  752. expect(view.model.get('omemo_active')).toBe(undefined);
  753. const toggle = toolbar.querySelector('.toggle-omemo');
  754. expect(_.isNull(toggle)).toBe(false);
  755. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  756. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  757. spyOn(view, 'toggleOMEMO').and.callThrough();
  758. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  759. toolbar.querySelector('.toggle-omemo').click();
  760. expect(view.toggleOMEMO).toHaveBeenCalled();
  761. expect(view.model.get('omemo_active')).toBe(true);
  762. return test_utils.waitUntil(() => u.hasClass('fa-lock', toolbar.querySelector('.toggle-omemo')));
  763. }).then(() => {
  764. const view = _converse.chatboxviews.get(contact_jid);
  765. const toolbar = view.el.querySelector('.chat-toolbar');
  766. const toggle = toolbar.querySelector('.toggle-omemo');
  767. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  768. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  769. const textarea = view.el.querySelector('.chat-textarea');
  770. textarea.value = 'This message will be sent encrypted';
  771. view.keyPressed({
  772. target: textarea,
  773. preventDefault: _.noop,
  774. keyCode: 13
  775. });
  776. done();
  777. }).catch(_.partial(console.error, _));
  778. }));
  779. it("shows OMEMO device fingerprints in the user details modal",
  780. mock.initConverseWithPromises(
  781. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  782. function (done, _converse) {
  783. let modal;
  784. test_utils.createContacts(_converse, 'current', 1);
  785. _converse.emit('rosterContactsFetched');
  786. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  787. test_utils.openChatBoxFor(_converse, contact_jid)
  788. .then(() => {
  789. // We simply emit, to avoid doing all the setup work
  790. _converse.emit('OMEMOInitialized');
  791. const view = _converse.chatboxviews.get(contact_jid);
  792. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  793. show_modal_button.click();
  794. modal = view.user_details_modal;
  795. return test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  796. }).then(() => test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid)))
  797. .then(iq_stanza => {
  798. expect(iq_stanza.outerHTML).toBe(
  799. `<iq type="get" from="dummy@localhost" to="max.frankfurter@localhost" xmlns="jabber:client" id="${iq_stanza.getAttribute('id')}">`+
  800. `<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub>`+
  801. `</iq>`);
  802. const stanza = $iq({
  803. 'from': contact_jid,
  804. 'id': iq_stanza.getAttribute('id'),
  805. 'to': _converse.bare_jid,
  806. 'type': 'result',
  807. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  808. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  809. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  810. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  811. .c('device', {'id': '555'});
  812. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  813. return test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  814. }).then(() => test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '555')))
  815. .then(iq_stanza => {
  816. expect(iq_stanza.outerHTML).toBe(
  817. `<iq type="get" from="dummy@localhost" to="max.frankfurter@localhost" xmlns="jabber:client" id="${iq_stanza.getAttribute('id')}">`+
  818. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  819. `<items node="eu.siacs.conversations.axolotl.bundles:555"/>`+
  820. `</pubsub>`+
  821. `</iq>`);
  822. const stanza = $iq({
  823. 'from': contact_jid,
  824. 'id': iq_stanza.getAttribute('id'),
  825. 'to': _converse.bare_jid,
  826. 'type': 'result',
  827. }).c('pubsub', {
  828. 'xmlns': 'http://jabber.org/protocol/pubsub'
  829. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
  830. .c('item')
  831. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  832. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  833. .c('signedPreKeySignature').t(btoa('2222')).up()
  834. .c('identityKey').t('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI').up()
  835. .c('prekeys')
  836. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  837. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  838. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  839. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  840. const view = _converse.chatboxviews.get(contact_jid);
  841. const modal = view.user_details_modal;
  842. return test_utils.waitUntil(() => modal.el.querySelectorAll('.fingerprints .fingerprint').length);
  843. }).then(() => {
  844. const view = _converse.chatboxviews.get(contact_jid);
  845. const modal = view.user_details_modal;
  846. expect(modal.el.querySelectorAll('.fingerprints .fingerprint').length).toBe(1);
  847. const el = modal.el.querySelector('.fingerprints .fingerprint');
  848. expect(el.textContent.trim()).toBe(
  849. u.formatFingerprint(u.arrayBufferToHex(u.base64ToArrayBuffer('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI')))
  850. );
  851. expect(modal.el.querySelectorAll('input[type="radio"]').length).toBe(2);
  852. const devicelist = _converse.devicelists.get(contact_jid);
  853. expect(devicelist.devices.get('555').get('trusted')).toBe(0);
  854. let trusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="1"]');
  855. expect(trusted_radio.checked).toBe(true);
  856. let untrusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="-1"]');
  857. expect(untrusted_radio.checked).toBe(false);
  858. // Test that the device can be set to untrusted
  859. untrusted_radio.click();
  860. trusted_radio = document.querySelector('input[type="radio"][name="555"][value="1"]');
  861. expect(trusted_radio.hasAttribute('checked')).toBe(false);
  862. expect(devicelist.devices.get('555').get('trusted')).toBe(-1);
  863. untrusted_radio = document.querySelector('input[type="radio"][name="555"][value="-1"]');
  864. expect(untrusted_radio.hasAttribute('checked')).toBe(true);
  865. trusted_radio.click();
  866. expect(devicelist.devices.get('555').get('trusted')).toBe(1);
  867. done();
  868. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
  869. }));
  870. });
  871. describe("A chatbox with an active OMEMO session", function() {
  872. it("will not show the spoiler toolbar button",
  873. mock.initConverseWithPromises(
  874. null, ['rosterGroupsFetched'], {},
  875. function (done, _converse) {
  876. // TODO
  877. done()
  878. }));
  879. });
  880. }));