omemo.js 51 KB

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