omemo.js 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. (function (root, factory) {
  2. define(["jasmine", "mock", "test-utils"], factory);
  3. } (this, function (jasmine, mock, test_utils) {
  4. const { $iq, $pres, $msg, _, Strophe } = converse.env;
  5. const u = converse.env.utils;
  6. function deviceListFetched (_converse, jid) {
  7. return _.filter(
  8. _converse.connection.IQ_stanzas,
  9. iq => iq.nodeTree.querySelector(`iq[to="${jid}"] items[node="eu.siacs.conversations.axolotl.devicelist"]`)
  10. ).pop();
  11. }
  12. function ownDeviceHasBeenPublished (_converse) {
  13. return _.filter(
  14. _converse.connection.IQ_stanzas,
  15. iq => iq.nodeTree.querySelector('iq[from="'+_converse.bare_jid+'"] publish[node="eu.siacs.conversations.axolotl.devicelist"]')
  16. ).pop();
  17. }
  18. function bundleHasBeenPublished (_converse) {
  19. return _.filter(
  20. _converse.connection.IQ_stanzas,
  21. iq => iq.nodeTree.querySelector('publish[node="eu.siacs.conversations.axolotl.bundles:123456789"]')
  22. ).pop();
  23. }
  24. function bundleFetched (_converse, jid, device_id) {
  25. return _.filter(
  26. _converse.connection.IQ_stanzas,
  27. (iq) => iq.nodeTree.querySelector(`iq[to="${jid}"] items[node="eu.siacs.conversations.axolotl.bundles:${device_id}"]`)
  28. ).pop();
  29. }
  30. async function initializedOMEMO (_converse) {
  31. await test_utils.waitUntilDiscoConfirmed(
  32. _converse, _converse.bare_jid,
  33. [{'category': 'pubsub', 'type': 'pep'}],
  34. ['http://jabber.org/protocol/pubsub#publish-options']
  35. );
  36. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  37. let stanza = $iq({
  38. 'from': _converse.bare_jid,
  39. 'id': iq_stanza.nodeTree.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. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse))
  49. stanza = $iq({
  50. 'from': _converse.bare_jid,
  51. 'id': iq_stanza.nodeTree.getAttribute('id'),
  52. 'to': _converse.bare_jid,
  53. 'type': 'result'});
  54. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  55. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse))
  56. stanza = $iq({
  57. 'from': _converse.bare_jid,
  58. 'id': iq_stanza.nodeTree.getAttribute('id'),
  59. 'to': _converse.bare_jid,
  60. 'type': 'result'});
  61. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  62. await _converse.api.waitUntil('OMEMOInitialized');
  63. }
  64. describe("The OMEMO module", function() {
  65. it("adds methods for encrypting and decrypting messages via AES GCM",
  66. mock.initConverseWithPromises(
  67. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  68. async function (done, _converse) {
  69. const message = 'This message will be encrypted'
  70. test_utils.createContacts(_converse, 'current', 1);
  71. _converse.emit('rosterContactsFetched');
  72. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  73. const view = await test_utils.openChatBoxFor(_converse, contact_jid);
  74. const payload = await view.model.encryptMessage(message);
  75. const result = await view.model.decryptMessage(payload);
  76. expect(result).toBe(message);
  77. done();
  78. }));
  79. it("enables encrypted messages to be sent and received",
  80. mock.initConverseWithPromises(
  81. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  82. async function (done, _converse) {
  83. let sent_stanza;
  84. test_utils.createContacts(_converse, 'current', 1);
  85. _converse.emit('rosterContactsFetched');
  86. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  87. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  88. await test_utils.openChatBoxFor(_converse, contact_jid);
  89. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  90. let stanza = $iq({
  91. 'from': contact_jid,
  92. 'id': iq_stanza.nodeTree.getAttribute('id'),
  93. 'to': _converse.connection.jid,
  94. 'type': 'result',
  95. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  96. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  97. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  98. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  99. .c('device', {'id': '555'});
  100. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  101. await test_utils.waitUntil(() => _converse.omemo_store);
  102. const devicelist = _converse.devicelists.get({'jid': contact_jid});
  103. expect(devicelist.devices.length).toBe(1);
  104. const view = _converse.chatboxviews.get(contact_jid);
  105. view.model.set('omemo_active', true);
  106. const textarea = view.el.querySelector('.chat-textarea');
  107. textarea.value = 'This message will be encrypted';
  108. view.keyPressed({
  109. target: textarea,
  110. preventDefault: _.noop,
  111. keyCode: 13 // Enter
  112. });
  113. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '555'));
  114. stanza = $iq({
  115. 'from': contact_jid,
  116. 'id': iq_stanza.nodeTree.getAttribute('id'),
  117. 'to': _converse.bare_jid,
  118. 'type': 'result',
  119. }).c('pubsub', {
  120. 'xmlns': 'http://jabber.org/protocol/pubsub'
  121. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
  122. .c('item')
  123. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  124. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  125. .c('signedPreKeySignature').t(btoa('2222')).up()
  126. .c('identityKey').t(btoa('3333')).up()
  127. .c('prekeys')
  128. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  129. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  130. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  131. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  132. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'));
  133. stanza = $iq({
  134. 'from': _converse.bare_jid,
  135. 'id': iq_stanza.nodeTree.getAttribute('id'),
  136. 'to': _converse.bare_jid,
  137. 'type': 'result',
  138. }).c('pubsub', {
  139. 'xmlns': 'http://jabber.org/protocol/pubsub'
  140. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  141. .c('item')
  142. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  143. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  144. .c('signedPreKeySignature').t(btoa('200000')).up()
  145. .c('identityKey').t(btoa('300000')).up()
  146. .c('prekeys')
  147. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  148. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  149. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  150. spyOn(_converse.connection, 'send').and.callFake(stanza => { sent_stanza = stanza });
  151. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  152. await test_utils.waitUntil(() => sent_stanza);
  153. expect(sent_stanza.toLocaleString()).toBe(
  154. `<message from="dummy@localhost/resource" id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  155. `to="max.frankfurter@localhost" `+
  156. `type="chat" xmlns="jabber:client">`+
  157. `<body>This is an OMEMO encrypted message which your client doesn’t seem to support. Find more information on https://conversations.im/omemo</body>`+
  158. `<request xmlns="urn:xmpp:receipts"/>`+
  159. `<encrypted xmlns="eu.siacs.conversations.axolotl">`+
  160. `<header sid="123456789">`+
  161. `<key rid="482886413b977930064a5888b92134fe">YzFwaDNSNzNYNw==</key>`+
  162. `<key rid="555">YzFwaDNSNzNYNw==</key>`+
  163. `<iv>${sent_stanza.nodeTree.querySelector("iv").textContent}</iv>`+
  164. `</header>`+
  165. `<payload>${sent_stanza.nodeTree.querySelector("payload").textContent}</payload>`+
  166. `</encrypted>`+
  167. `<store xmlns="urn:xmpp:hints"/>`+
  168. `</message>`);
  169. // Test reception of an encrypted message
  170. let obj = await view.model.encryptMessage('This is an encrypted message from the contact')
  171. // XXX: Normally the key will be encrypted via libsignal.
  172. // However, we're mocking libsignal in the tests, so we include
  173. // it as plaintext in the message.
  174. stanza = $msg({
  175. 'from': contact_jid,
  176. 'to': _converse.connection.jid,
  177. 'type': 'chat',
  178. 'id': _converse.connection.getUniqueId()
  179. }).c('body').t('This is a fallback message').up()
  180. .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  181. .c('header', {'sid': '555'})
  182. .c('key', {'rid': _converse.omemo_store.get('device_id')}).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  183. .c('iv').t(obj.iv)
  184. .up().up()
  185. .c('payload').t(obj.payload);
  186. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  187. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  188. expect(view.model.messages.length).toBe(2);
  189. expect(view.el.querySelectorAll('.chat-msg__body')[1].textContent.trim())
  190. .toBe('This is an encrypted message from the contact');
  191. // #1193 Check for a received message without <body> tag
  192. obj = await view.model.encryptMessage('Another received encrypted message without fallback')
  193. stanza = $msg({
  194. 'from': contact_jid,
  195. 'to': _converse.connection.jid,
  196. 'type': 'chat',
  197. 'id': _converse.connection.getUniqueId()
  198. }).c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  199. .c('header', {'sid': '555'})
  200. .c('key', {'rid': _converse.omemo_store.get('device_id')}).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  201. .c('iv').t(obj.iv)
  202. .up().up()
  203. .c('payload').t(obj.payload);
  204. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  205. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  206. await test_utils.waitUntil(() => view.model.messages.length > 1);
  207. expect(view.model.messages.length).toBe(3);
  208. expect(view.el.querySelectorAll('.chat-msg__body')[2].textContent.trim())
  209. .toBe('Another received encrypted message without fallback');
  210. done();
  211. }));
  212. it("enables encrypted groupchat messages to be sent and received",
  213. mock.initConverseWithPromises(
  214. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  215. async function (done, _converse) {
  216. // MEMO encryption works only in members only conferences
  217. // that are non-anonymous.
  218. const features = [
  219. 'http://jabber.org/protocol/muc',
  220. 'jabber:iq:register',
  221. 'muc_passwordprotected',
  222. 'muc_hidden',
  223. 'muc_temporary',
  224. 'muc_membersonly',
  225. 'muc_unmoderated',
  226. 'muc_nonanonymous'
  227. ];
  228. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy', features);
  229. const view = _converse.chatboxviews.get('lounge@localhost');
  230. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  231. const toolbar = view.el.querySelector('.chat-toolbar');
  232. let toggle = toolbar.querySelector('.toggle-omemo');
  233. toggle.click();
  234. expect(view.model.get('omemo_active')).toBe(true);
  235. const contact_jid = 'newguy@localhost';
  236. let stanza = $pres({
  237. 'to': 'dummy@localhost/resource',
  238. 'from': 'lounge@localhost/newguy'
  239. })
  240. .c('x', {xmlns: Strophe.NS.MUC_USER})
  241. .c('item', {
  242. 'affiliation': 'none',
  243. 'jid': 'newguy@localhost/_converse.js-290929789',
  244. 'role': 'participant'
  245. }).tree();
  246. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  247. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  248. expect(iq_stanza.toLocaleString()).toBe(
  249. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  250. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  251. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  252. `</pubsub>`+
  253. `</iq>`);
  254. stanza = $iq({
  255. 'from': contact_jid,
  256. 'id': iq_stanza.nodeTree.getAttribute('id'),
  257. 'to': _converse.bare_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': '4e30f35051b7b8b42abe083742187228'}).up()
  264. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  265. await test_utils.waitUntil(() => _converse.omemo_store);
  266. expect(_converse.devicelists.length).toBe(2);
  267. const devicelist = _converse.devicelists.get(contact_jid);
  268. expect(devicelist.devices.length).toBe(1);
  269. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  270. toggle = toolbar.querySelector('.toggle-omemo');
  271. expect(view.model.get('omemo_active')).toBe(true);
  272. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  273. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  274. const textarea = view.el.querySelector('.chat-textarea');
  275. textarea.value = 'This message will be encrypted';
  276. view.keyPressed({
  277. target: textarea,
  278. preventDefault: _.noop,
  279. keyCode: 13 // Enter
  280. });
  281. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '4e30f35051b7b8b42abe083742187228'));
  282. stanza = $iq({
  283. 'from': contact_jid,
  284. 'id': iq_stanza.nodeTree.getAttribute('id'),
  285. 'to': _converse.bare_jid,
  286. 'type': 'result',
  287. }).c('pubsub', {
  288. 'xmlns': 'http://jabber.org/protocol/pubsub'
  289. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:4e30f35051b7b8b42abe083742187228"})
  290. .c('item')
  291. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  292. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  293. .c('signedPreKeySignature').t(btoa('2222')).up()
  294. .c('identityKey').t(btoa('3333')).up()
  295. .c('prekeys')
  296. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  297. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  298. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  299. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  300. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'));
  301. stanza = $iq({
  302. 'from': _converse.bare_jid,
  303. 'id': iq_stanza.nodeTree.getAttribute('id'),
  304. 'to': _converse.bare_jid,
  305. 'type': 'result',
  306. }).c('pubsub', {
  307. 'xmlns': 'http://jabber.org/protocol/pubsub'
  308. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  309. .c('item')
  310. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  311. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  312. .c('signedPreKeySignature').t(btoa('200000')).up()
  313. .c('identityKey').t(btoa('300000')).up()
  314. .c('prekeys')
  315. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  316. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  317. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  318. spyOn(_converse.connection, 'send');
  319. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  320. await test_utils.waitUntil(() => _converse.connection.send.calls.count());
  321. const sent_stanza = _converse.connection.send.calls.all()[0].args[0];
  322. expect(Strophe.serialize(sent_stanza)).toBe(
  323. `<message from="dummy@localhost/resource" `+
  324. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  325. `to="lounge@localhost" `+
  326. `type="groupchat" `+
  327. `xmlns="jabber:client">`+
  328. `<body>This is an OMEMO encrypted message which your client doesn’t seem to support. Find more information on https://conversations.im/omemo</body>`+
  329. `<encrypted xmlns="eu.siacs.conversations.axolotl">`+
  330. `<header sid="123456789">`+
  331. `<key rid="482886413b977930064a5888b92134fe">YzFwaDNSNzNYNw==</key>`+
  332. `<key rid="4e30f35051b7b8b42abe083742187228">YzFwaDNSNzNYNw==</key>`+
  333. `<iv>${sent_stanza.nodeTree.querySelector("iv").textContent}</iv>`+
  334. `</header>`+
  335. `<payload>${sent_stanza.nodeTree.querySelector("payload").textContent}</payload>`+
  336. `</encrypted>`+
  337. `<store xmlns="urn:xmpp:hints"/>`+
  338. `</message>`);
  339. done();
  340. }));
  341. it("gracefully handles auth errors when trying to send encrypted groupchat messages",
  342. mock.initConverseWithPromises(
  343. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  344. async function (done, _converse) {
  345. // MEMO encryption works only in members only conferences
  346. // that are non-anonymous.
  347. const features = [
  348. 'http://jabber.org/protocol/muc',
  349. 'jabber:iq:register',
  350. 'muc_passwordprotected',
  351. 'muc_hidden',
  352. 'muc_temporary',
  353. 'muc_membersonly',
  354. 'muc_unmoderated',
  355. 'muc_nonanonymous'
  356. ];
  357. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy', features);
  358. const view = _converse.chatboxviews.get('lounge@localhost');
  359. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  360. const contact_jid = 'newguy@localhost';
  361. let stanza = $pres({
  362. 'to': 'dummy@localhost/resource',
  363. 'from': 'lounge@localhost/newguy'
  364. })
  365. .c('x', {xmlns: Strophe.NS.MUC_USER})
  366. .c('item', {
  367. 'affiliation': 'none',
  368. 'jid': 'newguy@localhost/_converse.js-290929789',
  369. 'role': 'participant'
  370. }).tree();
  371. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  372. const toolbar = view.el.querySelector('.chat-toolbar');
  373. const toggle = toolbar.querySelector('.toggle-omemo');
  374. toggle.click();
  375. expect(view.model.get('omemo_active')).toBe(true);
  376. expect(view.model.get('omemo_supported')).toBe(true);
  377. const textarea = view.el.querySelector('.chat-textarea');
  378. textarea.value = 'This message will be encrypted';
  379. view.keyPressed({
  380. target: textarea,
  381. preventDefault: _.noop,
  382. keyCode: 13 // Enter
  383. });
  384. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  385. expect(iq_stanza.toLocaleString()).toBe(
  386. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  387. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  388. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  389. `</pubsub>`+
  390. `</iq>`);
  391. stanza = $iq({
  392. 'from': contact_jid,
  393. 'id': iq_stanza.nodeTree.getAttribute('id'),
  394. 'to': _converse.bare_jid,
  395. 'type': 'result',
  396. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  397. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  398. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  399. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  400. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  401. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  402. await test_utils.waitUntil(() => _converse.omemo_store);
  403. expect(_converse.devicelists.length).toBe(2);
  404. const devicelist = _converse.devicelists.get(contact_jid);
  405. expect(devicelist.devices.length).toBe(1);
  406. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  407. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, _converse.bare_jid, '482886413b977930064a5888b92134fe'));
  408. stanza = $iq({
  409. 'from': _converse.bare_jid,
  410. 'id': iq_stanza.nodeTree.getAttribute('id'),
  411. 'to': _converse.bare_jid,
  412. 'type': 'result',
  413. }).c('pubsub', {
  414. 'xmlns': 'http://jabber.org/protocol/pubsub'
  415. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:482886413b977930064a5888b92134fe"})
  416. .c('item')
  417. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  418. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('100000')).up()
  419. .c('signedPreKeySignature').t(btoa('200000')).up()
  420. .c('identityKey').t(btoa('300000')).up()
  421. .c('prekeys')
  422. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1991')).up()
  423. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1992')).up()
  424. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1993'));
  425. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '4e30f35051b7b8b42abe083742187228'));
  426. /* <iq xmlns="jabber:client" to="jc@opkode.com/converse.js-34183907" type="error" id="945c8ab3-b561-4d8a-92da-77c226bb1689:sendIQ" from="joris@konuro.net">
  427. * <pubsub xmlns="http://jabber.org/protocol/pubsub">
  428. * <items node="eu.siacs.conversations.axolotl.bundles:7580"/>
  429. * </pubsub>
  430. * <error code="401" type="auth">
  431. * <presence-subscription-required xmlns="http://jabber.org/protocol/pubsub#errors"/>
  432. * <not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  433. * </error>
  434. * </iq>
  435. */
  436. stanza = $iq({
  437. 'from': contact_jid,
  438. 'id': iq_stanza.nodeTree.getAttribute('id'),
  439. 'to': _converse.bare_jid,
  440. 'type': 'result',
  441. }).c('pubsub', {'xmlns': 'http://jabber.org/protocol/pubsub'})
  442. .c('items', {'node': "eu.siacs.conversations.axolotl.bundles:4e30f35051b7b8b42abe083742187228"}).up().up()
  443. .c('error', {'code': '401', 'type': 'auth'})
  444. .c('presence-subscription-required', {'xmlns':"http://jabber.org/protocol/pubsub#errors" }).up()
  445. .c('not-authorized', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  446. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  447. await test_utils.waitUntil(() => document.querySelectorAll('.alert-danger').length, 2000);
  448. const header = document.querySelector('.alert-danger .modal-title');
  449. expect(header.textContent).toBe("Error");
  450. expect(u.ancestor(header, '.modal-content').querySelector('.modal-body p').textContent.trim())
  451. .toBe("Sorry, we're unable to send an encrypted message because newguy@localhost requires you "+
  452. "to be subscribed to their presence in order to see their OMEMO information");
  453. expect(view.model.get('omemo_supported')).toBe(false);
  454. expect(view.el.querySelector('.chat-textarea').value).toBe('This message will be encrypted');
  455. done();
  456. }));
  457. it("can receive a PreKeySignalMessage",
  458. mock.initConverseWithPromises(
  459. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  460. async function (done, _converse) {
  461. _converse.NUM_PREKEYS = 5; // Restrict to 5, otherwise the resulting stanza is too large to easily test
  462. let view, sent_stanza;
  463. test_utils.createContacts(_converse, 'current', 1);
  464. _converse.emit('rosterContactsFetched');
  465. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  466. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  467. const obj = await _converse.ChatBox.prototype.encryptMessage('This is an encrypted message from the contact');
  468. // XXX: Normally the key will be encrypted via libsignal.
  469. // However, we're mocking libsignal in the tests, so we include
  470. // it as plaintext in the message.
  471. let stanza = $msg({
  472. 'from': contact_jid,
  473. 'to': _converse.connection.jid,
  474. 'type': 'chat',
  475. 'id': 'qwerty'
  476. }).c('body').t('This is a fallback message').up()
  477. .c('encrypted', {'xmlns': Strophe.NS.OMEMO})
  478. .c('header', {'sid': '555'})
  479. .c('key', {
  480. 'prekey': 'true',
  481. 'rid': _converse.omemo_store.get('device_id')
  482. }).t(u.arrayBufferToBase64(obj.key_and_tag)).up()
  483. .c('iv').t(obj.iv)
  484. .up().up()
  485. .c('payload').t(obj.payload);
  486. const generateMissingPreKeys = _converse.omemo_store.generateMissingPreKeys;
  487. spyOn(_converse.omemo_store, 'generateMissingPreKeys').and.callFake(() => {
  488. // Since it's difficult to override
  489. // decryptPreKeyWhisperMessage, where a prekey will be
  490. // removed from the store, we do it here, before the
  491. // missing prekeys are generated.
  492. _converse.omemo_store.removePreKey(1);
  493. return generateMissingPreKeys.apply(_converse.omemo_store, arguments);
  494. });
  495. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  496. let iq_stanza = await test_utils.waitUntil(() => _converse.chatboxviews.get(contact_jid));
  497. iq_stanza = await deviceListFetched(_converse, contact_jid);
  498. stanza = $iq({
  499. 'from': contact_jid,
  500. 'id': iq_stanza.nodeTree.getAttribute('id'),
  501. 'to': _converse.connection.jid,
  502. 'type': 'result',
  503. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  504. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  505. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  506. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  507. .c('device', {'id': '555'});
  508. // XXX: the bundle gets published twice, we want to make sure
  509. // that we wait for the 2nd, so we clear all the already sent
  510. // stanzas.
  511. _converse.connection.IQ_stanzas = [];
  512. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  513. await test_utils.waitUntil(() => _converse.omemo_store);
  514. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  515. expect(iq_stanza.toLocaleString()).toBe(
  516. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" type="set" xmlns="jabber:client">`+
  517. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  518. `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
  519. `<item>`+
  520. `<bundle xmlns="eu.siacs.conversations.axolotl">`+
  521. `<signedPreKeyPublic signedPreKeyId="0">${btoa("1234")}</signedPreKeyPublic>`+
  522. `<signedPreKeySignature>${btoa("11112222333344445555")}</signedPreKeySignature>`+
  523. `<identityKey>${btoa("1234")}</identityKey>`+
  524. `<prekeys>`+
  525. `<preKeyPublic preKeyId="0">${btoa("1234")}</preKeyPublic>`+
  526. `<preKeyPublic preKeyId="1">${btoa("1234")}</preKeyPublic>`+
  527. `<preKeyPublic preKeyId="2">${btoa("1234")}</preKeyPublic>`+
  528. `<preKeyPublic preKeyId="3">${btoa("1234")}</preKeyPublic>`+
  529. `<preKeyPublic preKeyId="4">${btoa("1234")}</preKeyPublic>`+
  530. `</prekeys>`+
  531. `</bundle>`+
  532. `</item>`+
  533. `</publish>`+
  534. `<publish-options>`+
  535. `<x type="submit" xmlns="jabber:x:data">`+
  536. `<field type="hidden" var="FORM_TYPE">`+
  537. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  538. `</field>`+
  539. `<field var="pubsub#access_model">`+
  540. `<value>open</value>`+
  541. `</field>`+
  542. `</x>`+
  543. `</publish-options>`+
  544. `</pubsub>`+
  545. `</iq>`)
  546. const own_device = _converse.devicelists.get(_converse.bare_jid).devices.get(_converse.omemo_store.get('device_id'));
  547. expect(own_device.get('bundle').prekeys.length).toBe(5);
  548. expect(_converse.omemo_store.generateMissingPreKeys).toHaveBeenCalled();
  549. done();
  550. }));
  551. it("updates device lists based on PEP messages",
  552. mock.initConverseWithPromises(
  553. null, ['rosterGroupsFetched'], {'allow_non_roster_messaging': true},
  554. async function (done, _converse) {
  555. await test_utils.waitUntilDiscoConfirmed(
  556. _converse, _converse.bare_jid,
  557. [{'category': 'pubsub', 'type': 'pep'}],
  558. ['http://jabber.org/protocol/pubsub#publish-options']
  559. );
  560. test_utils.createContacts(_converse, 'current', 1);
  561. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  562. // Wait until own devices are fetched
  563. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  564. expect(iq_stanza.toLocaleString()).toBe(
  565. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="dummy@localhost" type="get" xmlns="jabber:client">`+
  566. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  567. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  568. `</pubsub>`+
  569. `</iq>`);
  570. let stanza = $iq({
  571. 'from': _converse.bare_jid,
  572. 'id': iq_stanza.nodeTree.getAttribute('id'),
  573. 'to': _converse.bare_jid,
  574. 'type': 'result',
  575. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  576. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  577. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  578. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  579. .c('device', {'id': '555'});
  580. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  581. await test_utils.waitUntil(() => _converse.omemo_store);
  582. expect(_converse.chatboxes.length).toBe(1);
  583. expect(_converse.devicelists.length).toBe(1);
  584. const devicelist = _converse.devicelists.get(_converse.bare_jid);
  585. expect(devicelist.devices.length).toBe(2);
  586. expect(devicelist.devices.at(0).get('id')).toBe('555');
  587. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  588. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  589. stanza = $iq({
  590. 'from': _converse.bare_jid,
  591. 'id': iq_stanza.nodeTree.getAttribute('id'),
  592. 'to': _converse.bare_jid,
  593. 'type': 'result'});
  594. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  595. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  596. stanza = $iq({
  597. 'from': _converse.bare_jid,
  598. 'id': iq_stanza.nodeTree.getAttribute('id'),
  599. 'to': _converse.bare_jid,
  600. 'type': 'result'});
  601. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  602. await _converse.api.waitUntil('OMEMOInitialized');
  603. stanza = $msg({
  604. 'from': contact_jid,
  605. 'to': _converse.bare_jid,
  606. 'type': 'headline',
  607. 'id': 'update_01',
  608. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  609. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  610. .c('item')
  611. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  612. .c('device', {'id': '1234'})
  613. .c('device', {'id': '4223'})
  614. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  615. expect(_converse.devicelists.length).toBe(2);
  616. let devices = _converse.devicelists.get(contact_jid).devices;
  617. expect(devices.length).toBe(2);
  618. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('1234,4223');
  619. stanza = $msg({
  620. 'from': contact_jid,
  621. 'to': _converse.bare_jid,
  622. 'type': 'headline',
  623. 'id': 'update_02',
  624. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  625. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  626. .c('item')
  627. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  628. .c('device', {'id': '4223'})
  629. .c('device', {'id': '4224'})
  630. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  631. expect(_converse.devicelists.length).toBe(2);
  632. expect(devices.length).toBe(2);
  633. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('4223,4224');
  634. // Check that own devicelist gets updated
  635. stanza = $msg({
  636. 'from': _converse.bare_jid,
  637. 'to': _converse.bare_jid,
  638. 'type': 'headline',
  639. 'id': 'update_03',
  640. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  641. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  642. .c('item')
  643. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  644. .c('device', {'id': '123456789'})
  645. .c('device', {'id': '555'})
  646. .c('device', {'id': '777'})
  647. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  648. expect(_converse.devicelists.length).toBe(2);
  649. devices = _converse.devicelists.get(_converse.bare_jid).devices;
  650. expect(devices.length).toBe(3);
  651. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,555,777');
  652. _converse.connection.IQ_stanzas = [];
  653. // Check that own device gets re-added
  654. stanza = $msg({
  655. 'from': _converse.bare_jid,
  656. 'to': _converse.bare_jid,
  657. 'type': 'headline',
  658. 'id': 'update_04',
  659. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  660. .c('items', {'node': 'eu.siacs.conversations.axolotl.devicelist'})
  661. .c('item')
  662. .c('list', {'xmlns': 'eu.siacs.conversations.axolotl'})
  663. .c('device', {'id': '444'})
  664. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  665. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  666. // Check that our own device is added again, but that removed
  667. // devices are not added.
  668. expect(iq_stanza.toLocaleString()).toBe(
  669. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute(`id`)}" type="set" xmlns="jabber:client">`+
  670. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  671. `<publish node="eu.siacs.conversations.axolotl.devicelist">`+
  672. `<item>`+
  673. `<list xmlns="eu.siacs.conversations.axolotl">`+
  674. `<device id="123456789"/>`+
  675. `<device id="444"/>`+
  676. `</list>`+
  677. `</item>`+
  678. `</publish>`+
  679. `<publish-options>`+
  680. `<x type="submit" xmlns="jabber:x:data">`+
  681. `<field type="hidden" var="FORM_TYPE">`+
  682. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  683. `</field>`+
  684. `<field var="pubsub#access_model">`+
  685. `<value>open</value>`+
  686. `</field>`+
  687. `</x>`+
  688. `</publish-options>`+
  689. `</pubsub>`+
  690. `</iq>`);
  691. expect(_converse.devicelists.length).toBe(2);
  692. devices = _converse.devicelists.get(_converse.bare_jid).devices;
  693. // The device id for this device (123456789) was also generated and added to the list,
  694. // which is why we have 2 devices now.
  695. expect(devices.length).toBe(2);
  696. expect(_.map(devices.models, 'attributes.id').sort().join()).toBe('123456789,444');
  697. done();
  698. }));
  699. it("updates device bundles based on PEP messages",
  700. mock.initConverseWithPromises(
  701. null, ['rosterGroupsFetched'], {},
  702. async function (done, _converse) {
  703. await test_utils.waitUntilDiscoConfirmed(
  704. _converse, _converse.bare_jid,
  705. [{'category': 'pubsub', 'type': 'pep'}],
  706. ['http://jabber.org/protocol/pubsub#publish-options']
  707. );
  708. test_utils.createContacts(_converse, 'current');
  709. const contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
  710. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  711. expect(iq_stanza.toLocaleString()).toBe(
  712. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="dummy@localhost" type="get" xmlns="jabber:client">`+
  713. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  714. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  715. `</pubsub>`+
  716. `</iq>`);
  717. let stanza = $iq({
  718. 'from': contact_jid,
  719. 'id': iq_stanza.nodeTree.getAttribute('id'),
  720. 'to': _converse.bare_jid,
  721. 'type': 'result',
  722. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  723. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  724. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  725. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  726. .c('device', {'id': '555'});
  727. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  728. await await test_utils.waitUntil(() => _converse.omemo_store);
  729. expect(_converse.devicelists.length).toBe(1);
  730. let devicelist = _converse.devicelists.get(_converse.bare_jid);
  731. expect(devicelist.devices.length).toBe(2);
  732. expect(devicelist.devices.at(0).get('id')).toBe('555');
  733. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  734. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  735. stanza = $iq({
  736. 'from': _converse.bare_jid,
  737. 'id': iq_stanza.nodeTree.getAttribute('id'),
  738. 'to': _converse.bare_jid,
  739. 'type': 'result'});
  740. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  741. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  742. stanza = $iq({
  743. 'from': _converse.bare_jid,
  744. 'id': iq_stanza.nodeTree.getAttribute('id'),
  745. 'to': _converse.bare_jid,
  746. 'type': 'result'});
  747. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  748. await _converse.api.waitUntil('OMEMOInitialized');
  749. stanza = $msg({
  750. 'from': contact_jid,
  751. 'to': _converse.bare_jid,
  752. 'type': 'headline',
  753. 'id': 'update_01',
  754. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  755. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
  756. .c('item')
  757. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  758. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('1111').up()
  759. .c('signedPreKeySignature').t('2222').up()
  760. .c('identityKey').t('3333').up()
  761. .c('prekeys')
  762. .c('preKeyPublic', {'preKeyId': '1001'}).up()
  763. .c('preKeyPublic', {'preKeyId': '1002'}).up()
  764. .c('preKeyPublic', {'preKeyId': '1003'});
  765. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  766. expect(_converse.devicelists.length).toBe(2);
  767. devicelist = _converse.devicelists.get(contact_jid);
  768. expect(devicelist.devices.length).toBe(1);
  769. let device = devicelist.devices.at(0);
  770. expect(device.get('bundle').identity_key).toBe('3333');
  771. expect(device.get('bundle').signed_prekey.public_key).toBe('1111');
  772. expect(device.get('bundle').signed_prekey.id).toBe(4223);
  773. expect(device.get('bundle').signed_prekey.signature).toBe('2222');
  774. expect(device.get('bundle').prekeys.length).toBe(3);
  775. expect(device.get('bundle').prekeys[0].id).toBe(1001);
  776. expect(device.get('bundle').prekeys[1].id).toBe(1002);
  777. expect(device.get('bundle').prekeys[2].id).toBe(1003);
  778. stanza = $msg({
  779. 'from': contact_jid,
  780. 'to': _converse.bare_jid,
  781. 'type': 'headline',
  782. 'id': 'update_02',
  783. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  784. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:555'})
  785. .c('item')
  786. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  787. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t('5555').up()
  788. .c('signedPreKeySignature').t('6666').up()
  789. .c('identityKey').t('7777').up()
  790. .c('prekeys')
  791. .c('preKeyPublic', {'preKeyId': '2001'}).up()
  792. .c('preKeyPublic', {'preKeyId': '2002'}).up()
  793. .c('preKeyPublic', {'preKeyId': '2003'});
  794. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  795. expect(_converse.devicelists.length).toBe(2);
  796. devicelist = _converse.devicelists.get(contact_jid);
  797. expect(devicelist.devices.length).toBe(1);
  798. device = devicelist.devices.at(0);
  799. expect(device.get('bundle').identity_key).toBe('7777');
  800. expect(device.get('bundle').signed_prekey.public_key).toBe('5555');
  801. expect(device.get('bundle').signed_prekey.id).toBe(4223);
  802. expect(device.get('bundle').signed_prekey.signature).toBe('6666');
  803. expect(device.get('bundle').prekeys.length).toBe(3);
  804. expect(device.get('bundle').prekeys[0].id).toBe(2001);
  805. expect(device.get('bundle').prekeys[1].id).toBe(2002);
  806. expect(device.get('bundle').prekeys[2].id).toBe(2003);
  807. stanza = $msg({
  808. 'from': _converse.bare_jid,
  809. 'to': _converse.bare_jid,
  810. 'type': 'headline',
  811. 'id': 'update_03',
  812. }).c('event', {'xmlns': 'http://jabber.org/protocol/pubsub#event'})
  813. .c('items', {'node': 'eu.siacs.conversations.axolotl.bundles:123456789'})
  814. .c('item')
  815. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  816. .c('signedPreKeyPublic', {'signedPreKeyId': '9999'}).t('8888').up()
  817. .c('signedPreKeySignature').t('3333').up()
  818. .c('identityKey').t('1111').up()
  819. .c('prekeys')
  820. .c('preKeyPublic', {'preKeyId': '3001'}).up()
  821. .c('preKeyPublic', {'preKeyId': '3002'}).up()
  822. .c('preKeyPublic', {'preKeyId': '3003'});
  823. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  824. expect(_converse.devicelists.length).toBe(2);
  825. devicelist = _converse.devicelists.get(_converse.bare_jid);
  826. expect(devicelist.devices.length).toBe(2);
  827. expect(devicelist.devices.at(0).get('id')).toBe('555');
  828. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  829. device = devicelist.devices.at(1);
  830. expect(device.get('bundle').identity_key).toBe('1111');
  831. expect(device.get('bundle').signed_prekey.public_key).toBe('8888');
  832. expect(device.get('bundle').signed_prekey.id).toBe(9999);
  833. expect(device.get('bundle').signed_prekey.signature).toBe('3333');
  834. expect(device.get('bundle').prekeys.length).toBe(3);
  835. expect(device.get('bundle').prekeys[0].id).toBe(3001);
  836. expect(device.get('bundle').prekeys[1].id).toBe(3002);
  837. expect(device.get('bundle').prekeys[2].id).toBe(3003);
  838. done();
  839. }));
  840. it("publishes a bundle with which an encrypted session can be created",
  841. mock.initConverseWithPromises(
  842. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  843. async function (done, _converse) {
  844. await test_utils.waitUntilDiscoConfirmed(
  845. _converse, _converse.bare_jid,
  846. [{'category': 'pubsub', 'type': 'pep'}],
  847. ['http://jabber.org/protocol/pubsub#publish-options']
  848. );
  849. _converse.NUM_PREKEYS = 2; // Restrict to 2, otherwise the resulting stanza is too large to easily test
  850. test_utils.createContacts(_converse, 'current', 1);
  851. _converse.emit('rosterContactsFetched');
  852. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  853. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  854. let stanza = $iq({
  855. 'from': contact_jid,
  856. 'id': iq_stanza.nodeTree.getAttribute('id'),
  857. 'to': _converse.bare_jid,
  858. 'type': 'result',
  859. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  860. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  861. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  862. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  863. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  864. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  865. expect(_converse.devicelists.length).toBe(1);
  866. await test_utils.openChatBoxFor(_converse, contact_jid);
  867. iq_stanza = await ownDeviceHasBeenPublished(_converse);
  868. stanza = $iq({
  869. 'from': _converse.bare_jid,
  870. 'id': iq_stanza.nodeTree.getAttribute('id'),
  871. 'to': _converse.bare_jid,
  872. 'type': 'result'});
  873. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  874. iq_stanza = await test_utils.waitUntil(() => bundleHasBeenPublished(_converse));
  875. expect(iq_stanza.toLocaleString()).toBe(
  876. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" type="set" xmlns="jabber:client">`+
  877. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  878. `<publish node="eu.siacs.conversations.axolotl.bundles:123456789">`+
  879. `<item>`+
  880. `<bundle xmlns="eu.siacs.conversations.axolotl">`+
  881. `<signedPreKeyPublic signedPreKeyId="0">${btoa("1234")}</signedPreKeyPublic>`+
  882. `<signedPreKeySignature>${btoa("11112222333344445555")}</signedPreKeySignature>`+
  883. `<identityKey>${btoa("1234")}</identityKey>`+
  884. `<prekeys>`+
  885. `<preKeyPublic preKeyId="0">${btoa("1234")}</preKeyPublic>`+
  886. `<preKeyPublic preKeyId="1">${btoa("1234")}</preKeyPublic>`+
  887. `</prekeys>`+
  888. `</bundle>`+
  889. `</item>`+
  890. `</publish>`+
  891. `<publish-options>`+
  892. `<x type="submit" xmlns="jabber:x:data">`+
  893. `<field type="hidden" var="FORM_TYPE">`+
  894. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  895. `</field>`+
  896. `<field var="pubsub#access_model">`+
  897. `<value>open</value>`+
  898. `</field>`+
  899. `</x>`+
  900. `</publish-options>`+
  901. `</pubsub>`+
  902. `</iq>`)
  903. stanza = $iq({
  904. 'from': _converse.bare_jid,
  905. 'id': iq_stanza.nodeTree.getAttribute('id'),
  906. 'to': _converse.bare_jid,
  907. 'type': 'result'});
  908. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  909. await _converse.api.waitUntil('OMEMOInitialized');
  910. done();
  911. }));
  912. it("adds a toolbar button for starting an encrypted chat session",
  913. mock.initConverseWithPromises(
  914. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  915. async function (done, _converse) {
  916. await test_utils.waitUntilDiscoConfirmed(
  917. _converse, _converse.bare_jid,
  918. [{'category': 'pubsub', 'type': 'pep'}],
  919. ['http://jabber.org/protocol/pubsub#publish-options']
  920. );
  921. test_utils.createContacts(_converse, 'current', 1);
  922. _converse.emit('rosterContactsFetched');
  923. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  924. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, _converse.bare_jid));
  925. expect(iq_stanza.toLocaleString()).toBe(
  926. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="dummy@localhost" type="get" xmlns="jabber:client">`+
  927. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  928. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  929. `</pubsub>`+
  930. `</iq>`);
  931. let stanza = $iq({
  932. 'from': _converse.bare_jid,
  933. 'id': iq_stanza.nodeTree.getAttribute('id'),
  934. 'to': _converse.bare_jid,
  935. 'type': 'result',
  936. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  937. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  938. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  939. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  940. .c('device', {'id': '482886413b977930064a5888b92134fe'});
  941. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  942. await test_utils.waitUntil(() => _converse.omemo_store);
  943. expect(_converse.devicelists.length).toBe(1);
  944. let devicelist = _converse.devicelists.get(_converse.bare_jid);
  945. expect(devicelist.devices.length).toBe(2);
  946. expect(devicelist.devices.at(0).get('id')).toBe('482886413b977930064a5888b92134fe');
  947. expect(devicelist.devices.at(1).get('id')).toBe('123456789');
  948. // Check that own device was published
  949. iq_stanza = await test_utils.waitUntil(() => ownDeviceHasBeenPublished(_converse));
  950. expect(iq_stanza.toLocaleString()).toBe(
  951. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute(`id`)}" type="set" xmlns="jabber:client">`+
  952. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  953. `<publish node="eu.siacs.conversations.axolotl.devicelist">`+
  954. `<item>`+
  955. `<list xmlns="eu.siacs.conversations.axolotl">`+
  956. `<device id="482886413b977930064a5888b92134fe"/>`+
  957. `<device id="123456789"/>`+
  958. `</list>`+
  959. `</item>`+
  960. `</publish>`+
  961. `<publish-options>`+
  962. `<x type="submit" xmlns="jabber:x:data">`+
  963. `<field type="hidden" var="FORM_TYPE">`+
  964. `<value>http://jabber.org/protocol/pubsub#publish-options</value>`+
  965. `</field>`+
  966. `<field var="pubsub#access_model">`+
  967. `<value>open</value>`+
  968. `</field>`+
  969. `</x>`+
  970. `</publish-options>`+
  971. `</pubsub>`+
  972. `</iq>`);
  973. stanza = $iq({
  974. 'from': _converse.bare_jid,
  975. 'id': iq_stanza.nodeTree.getAttribute('id'),
  976. 'to': _converse.bare_jid,
  977. 'type': 'result'});
  978. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  979. const iq_el = await test_utils.waitUntil(() => _.get(bundleHasBeenPublished(_converse), 'nodeTree'));
  980. expect(iq_el.getAttributeNames().sort().join()).toBe(["from", "type", "xmlns", "id"].sort().join());
  981. expect(iq_el.querySelector('prekeys').childNodes.length).toBe(100);
  982. const signed_prekeys = iq_el.querySelectorAll('signedPreKeyPublic');
  983. expect(signed_prekeys.length).toBe(1);
  984. const signed_prekey = signed_prekeys[0];
  985. expect(signed_prekey.getAttribute('signedPreKeyId')).toBe('0')
  986. expect(iq_el.querySelectorAll('signedPreKeySignature').length).toBe(1);
  987. expect(iq_el.querySelectorAll('identityKey').length).toBe(1);
  988. stanza = $iq({
  989. 'from': _converse.bare_jid,
  990. 'id': iq_el.getAttribute('id'),
  991. 'to': _converse.bare_jid,
  992. 'type': 'result'});
  993. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  994. await _converse.api.waitUntil('OMEMOInitialized', 1000);
  995. await test_utils.openChatBoxFor(_converse, contact_jid);
  996. iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  997. expect(iq_stanza.toLocaleString()).toBe(
  998. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  999. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1000. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1001. `</pubsub>`+
  1002. `</iq>`);
  1003. stanza = $iq({
  1004. 'from': contact_jid,
  1005. 'id': iq_stanza.nodeTree.getAttribute('id'),
  1006. 'to': _converse.bare_jid,
  1007. 'type': 'result',
  1008. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1009. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1010. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1011. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1012. .c('device', {'id': '368866411b877c30064a5f62b917cffe'}).up()
  1013. .c('device', {'id': '3300659945416e274474e469a1f0154c'}).up()
  1014. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  1015. .c('device', {'id': 'ae890ac52d0df67ed7cfdf51b644e901'});
  1016. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1017. devicelist = _converse.devicelists.get(contact_jid);
  1018. await test_utils.waitUntil(() => devicelist.devices.length);
  1019. expect(_converse.devicelists.length).toBe(2);
  1020. devicelist = _converse.devicelists.get(contact_jid);
  1021. expect(devicelist.devices.length).toBe(4);
  1022. expect(devicelist.devices.at(0).get('id')).toBe('368866411b877c30064a5f62b917cffe');
  1023. expect(devicelist.devices.at(1).get('id')).toBe('3300659945416e274474e469a1f0154c');
  1024. expect(devicelist.devices.at(2).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  1025. expect(devicelist.devices.at(3).get('id')).toBe('ae890ac52d0df67ed7cfdf51b644e901');
  1026. await test_utils.waitUntil(() => _converse.chatboxviews.get(contact_jid).el.querySelector('.chat-toolbar'));
  1027. const view = _converse.chatboxviews.get(contact_jid);
  1028. const toolbar = view.el.querySelector('.chat-toolbar');
  1029. expect(view.model.get('omemo_active')).toBe(undefined);
  1030. let toggle = toolbar.querySelector('.toggle-omemo');
  1031. expect(_.isNull(toggle)).toBe(false);
  1032. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1033. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1034. spyOn(view, 'toggleOMEMO').and.callThrough();
  1035. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1036. toolbar.querySelector('.toggle-omemo').click();
  1037. expect(view.toggleOMEMO).toHaveBeenCalled();
  1038. expect(view.model.get('omemo_active')).toBe(true);
  1039. await test_utils.waitUntil(() => u.hasClass('fa-lock', toolbar.querySelector('.toggle-omemo')));
  1040. toggle = toolbar.querySelector('.toggle-omemo');
  1041. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1042. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1043. const textarea = view.el.querySelector('.chat-textarea');
  1044. textarea.value = 'This message will be sent encrypted';
  1045. view.keyPressed({
  1046. target: textarea,
  1047. preventDefault: _.noop,
  1048. keyCode: 13
  1049. });
  1050. view.model.save({'omemo_supported': false});
  1051. toggle = toolbar.querySelector('.toggle-omemo');
  1052. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1053. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1054. expect(u.hasClass('disabled', toggle)).toBe(true);
  1055. view.model.save({'omemo_supported': true});
  1056. toggle = toolbar.querySelector('.toggle-omemo');
  1057. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1058. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1059. expect(u.hasClass('disabled', toggle)).toBe(false);
  1060. done();
  1061. }));
  1062. it("adds a toolbar button for starting an encrypted groupchat session",
  1063. mock.initConverseWithPromises(
  1064. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {'view_mode': 'fullscreen'},
  1065. async function (done, _converse) {
  1066. await test_utils.waitUntilDiscoConfirmed(
  1067. _converse, _converse.bare_jid,
  1068. [{'category': 'pubsub', 'type': 'pep'}],
  1069. ['http://jabber.org/protocol/pubsub#publish-options']
  1070. );
  1071. // MEMO encryption works only in members-only conferences that are non-anonymous.
  1072. const features = [
  1073. 'http://jabber.org/protocol/muc',
  1074. 'jabber:iq:register',
  1075. 'muc_passwordprotected',
  1076. 'muc_hidden',
  1077. 'muc_temporary',
  1078. 'muc_membersonly',
  1079. 'muc_unmoderated',
  1080. 'muc_nonanonymous'
  1081. ];
  1082. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy', features);
  1083. const view = _converse.chatboxviews.get('lounge@localhost');
  1084. await test_utils.waitUntil(() => initializedOMEMO(_converse));
  1085. const toolbar = view.el.querySelector('.chat-toolbar');
  1086. let toggle = toolbar.querySelector('.toggle-omemo');
  1087. expect(view.model.get('omemo_active')).toBe(undefined);
  1088. expect(_.isNull(toggle)).toBe(false);
  1089. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1090. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1091. expect(u.hasClass('disabled', toggle)).toBe(false);
  1092. expect(view.model.get('omemo_supported')).toBe(true);
  1093. toggle.click();
  1094. toggle = toolbar.querySelector('.toggle-omemo');
  1095. expect(view.model.get('omemo_active')).toBe(true);
  1096. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1097. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1098. expect(u.hasClass('disabled', toggle)).toBe(false);
  1099. expect(view.model.get('omemo_supported')).toBe(true);
  1100. let contact_jid = 'newguy@localhost';
  1101. let stanza = $pres({
  1102. to: 'dummy@localhost/resource',
  1103. from: 'lounge@localhost/newguy'
  1104. })
  1105. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1106. .c('item', {
  1107. 'affiliation': 'none',
  1108. 'jid': 'newguy@localhost/_converse.js-290929789',
  1109. 'role': 'participant'
  1110. }).tree();
  1111. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1112. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1113. expect(iq_stanza.toLocaleString()).toBe(
  1114. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1115. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1116. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1117. `</pubsub>`+
  1118. `</iq>`);
  1119. stanza = $iq({
  1120. 'from': contact_jid,
  1121. 'id': iq_stanza.nodeTree.getAttribute('id'),
  1122. 'to': _converse.bare_jid,
  1123. 'type': 'result',
  1124. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1125. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1126. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1127. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1128. .c('device', {'id': '4e30f35051b7b8b42abe083742187228'}).up()
  1129. .c('device', {'id': 'ae890ac52d0df67ed7cfdf51b644e901'});
  1130. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1131. await test_utils.waitUntil(() => _converse.omemo_store);
  1132. expect(_converse.devicelists.length).toBe(2);
  1133. const devicelist = _converse.devicelists.get(contact_jid);
  1134. expect(devicelist.devices.length).toBe(2);
  1135. expect(devicelist.devices.at(0).get('id')).toBe('4e30f35051b7b8b42abe083742187228');
  1136. expect(devicelist.devices.at(1).get('id')).toBe('ae890ac52d0df67ed7cfdf51b644e901');
  1137. expect(view.model.get('omemo_active')).toBe(true);
  1138. toggle = toolbar.querySelector('.toggle-omemo');
  1139. expect(_.isNull(toggle)).toBe(false);
  1140. expect(u.hasClass('fa-unlock', toggle)).toBe(false);
  1141. expect(u.hasClass('fa-lock', toggle)).toBe(true);
  1142. expect(u.hasClass('disabled', toggle)).toBe(false);
  1143. expect(view.model.get('omemo_supported')).toBe(true);
  1144. // Test that the button gets disabled when the room becomes
  1145. // anonymous or semi-anonymous
  1146. view.model.save({'nonanonymous': false, 'semianonymous': true});
  1147. await test_utils.waitUntil(() => !view.model.get('omemo_supported'));
  1148. toggle = toolbar.querySelector('.toggle-omemo');
  1149. expect(_.isNull(toggle)).toBe(true);
  1150. expect(view.model.get('omemo_supported')).toBe(false);
  1151. view.model.save({'nonanonymous': true, 'semianonymous': false});
  1152. await test_utils.waitUntil(() => view.model.get('omemo_supported'));
  1153. toggle = toolbar.querySelector('.toggle-omemo');
  1154. expect(_.isNull(toggle)).toBe(false);
  1155. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1156. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1157. expect(u.hasClass('disabled', toggle)).toBe(false);
  1158. // Test that the button gets disabled when the room becomes open
  1159. view.model.save({'membersonly': false, 'open': true});
  1160. await test_utils.waitUntil(() => !view.model.get('omemo_supported'));
  1161. toggle = toolbar.querySelector('.toggle-omemo');
  1162. expect(_.isNull(toggle)).toBe(true);
  1163. view.model.save({'membersonly': true, 'open': false});
  1164. await test_utils.waitUntil(() => view.model.get('omemo_supported'));
  1165. toggle = toolbar.querySelector('.toggle-omemo');
  1166. expect(_.isNull(toggle)).toBe(false);
  1167. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1168. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1169. expect(u.hasClass('disabled', toggle)).toBe(false);
  1170. expect(view.model.get('omemo_supported')).toBe(true);
  1171. expect(view.model.get('omemo_active')).toBe(false);
  1172. toggle.click();
  1173. expect(view.model.get('omemo_active')).toBe(true);
  1174. // Someone enters the room who doesn't have OMEMO support, while we
  1175. // have OMEMO activated...
  1176. contact_jid = 'oldguy@localhost';
  1177. stanza = $pres({
  1178. to: 'dummy@localhost/resource',
  1179. from: 'lounge@localhost/oldguy'
  1180. })
  1181. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1182. .c('item', {
  1183. 'affiliation': 'none',
  1184. 'jid': `${contact_jid}/_converse.js-290929788`,
  1185. 'role': 'participant'
  1186. }).tree();
  1187. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1188. iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1189. expect(iq_stanza.toLocaleString()).toBe(
  1190. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="${contact_jid}" type="get" xmlns="jabber:client">`+
  1191. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1192. `<items node="eu.siacs.conversations.axolotl.devicelist"/>`+
  1193. `</pubsub>`+
  1194. `</iq>`);
  1195. stanza = $iq({
  1196. 'from': contact_jid,
  1197. 'id': iq_stanza.nodeTree.getAttribute('id'),
  1198. 'to': _converse.bare_jid,
  1199. 'type': 'error'
  1200. }).c('error', {'type': 'cancel'})
  1201. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1202. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1203. await test_utils.waitUntil(() => !view.model.get('omemo_supported'));
  1204. expect(view.el.querySelector('.chat-error').textContent).toBe(
  1205. "oldguy doesn't appear to have a client that supports OMEMO. "+
  1206. "Encrypted chat will no longer be possible in this grouchat."
  1207. );
  1208. toggle = toolbar.querySelector('.toggle-omemo');
  1209. expect(_.isNull(toggle)).toBe(false);
  1210. expect(u.hasClass('fa-unlock', toggle)).toBe(true);
  1211. expect(u.hasClass('fa-lock', toggle)).toBe(false);
  1212. expect(u.hasClass('disabled', toggle)).toBe(true);
  1213. expect( _converse.chatboxviews.el.querySelector('.modal-body p')).toBe(null);
  1214. toggle.click();
  1215. const msg = _converse.chatboxviews.el.querySelector('.modal-body p');
  1216. expect(msg.textContent).toBe(
  1217. 'Cannot use end-to-end encryption in this groupchat, '+
  1218. 'either the groupchat has some anonymity or not all participants support OMEMO.');
  1219. done();
  1220. }));
  1221. it("shows OMEMO device fingerprints in the user details modal",
  1222. mock.initConverseWithPromises(
  1223. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1224. async function (done, _converse) {
  1225. await test_utils.waitUntilDiscoConfirmed(
  1226. _converse, _converse.bare_jid,
  1227. [{'category': 'pubsub', 'type': 'pep'}],
  1228. ['http://jabber.org/protocol/pubsub#publish-options']
  1229. );
  1230. test_utils.createContacts(_converse, 'current', 1);
  1231. _converse.emit('rosterContactsFetched');
  1232. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  1233. await test_utils.openChatBoxFor(_converse, contact_jid)
  1234. // We simply emit, to avoid doing all the setup work
  1235. _converse.emit('OMEMOInitialized');
  1236. const view = _converse.chatboxviews.get(contact_jid);
  1237. const show_modal_button = view.el.querySelector('.show-user-details-modal');
  1238. show_modal_button.click();
  1239. const modal = view.user_details_modal;
  1240. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  1241. let iq_stanza = await test_utils.waitUntil(() => deviceListFetched(_converse, contact_jid));
  1242. expect(iq_stanza.toLocaleString()).toBe(
  1243. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="max.frankfurter@localhost" type="get" xmlns="jabber:client">`+
  1244. `<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub>`+
  1245. `</iq>`);
  1246. let stanza = $iq({
  1247. 'from': contact_jid,
  1248. 'id': iq_stanza.nodeTree.getAttribute('id'),
  1249. 'to': _converse.bare_jid,
  1250. 'type': 'result',
  1251. }).c('pubsub', {'xmlns': "http://jabber.org/protocol/pubsub"})
  1252. .c('items', {'node': "eu.siacs.conversations.axolotl.devicelist"})
  1253. .c('item', {'xmlns': "http://jabber.org/protocol/pubsub"}) // TODO: must have an id attribute
  1254. .c('list', {'xmlns': "eu.siacs.conversations.axolotl"})
  1255. .c('device', {'id': '555'});
  1256. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1257. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  1258. iq_stanza = await test_utils.waitUntil(() => bundleFetched(_converse, contact_jid, '555'));
  1259. expect(iq_stanza.toLocaleString()).toBe(
  1260. `<iq from="dummy@localhost" id="${iq_stanza.nodeTree.getAttribute("id")}" to="max.frankfurter@localhost" type="get" xmlns="jabber:client">`+
  1261. `<pubsub xmlns="http://jabber.org/protocol/pubsub">`+
  1262. `<items node="eu.siacs.conversations.axolotl.bundles:555"/>`+
  1263. `</pubsub>`+
  1264. `</iq>`);
  1265. stanza = $iq({
  1266. 'from': contact_jid,
  1267. 'id': iq_stanza.nodeTree.getAttribute('id'),
  1268. 'to': _converse.bare_jid,
  1269. 'type': 'result',
  1270. }).c('pubsub', {
  1271. 'xmlns': 'http://jabber.org/protocol/pubsub'
  1272. }).c('items', {'node': "eu.siacs.conversations.axolotl.bundles:555"})
  1273. .c('item')
  1274. .c('bundle', {'xmlns': 'eu.siacs.conversations.axolotl'})
  1275. .c('signedPreKeyPublic', {'signedPreKeyId': '4223'}).t(btoa('1111')).up()
  1276. .c('signedPreKeySignature').t(btoa('2222')).up()
  1277. .c('identityKey').t('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI').up()
  1278. .c('prekeys')
  1279. .c('preKeyPublic', {'preKeyId': '1'}).t(btoa('1001')).up()
  1280. .c('preKeyPublic', {'preKeyId': '2'}).t(btoa('1002')).up()
  1281. .c('preKeyPublic', {'preKeyId': '3'}).t(btoa('1003'));
  1282. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1283. await test_utils.waitUntil(() => modal.el.querySelectorAll('.fingerprints .fingerprint').length);
  1284. expect(modal.el.querySelectorAll('.fingerprints .fingerprint').length).toBe(1);
  1285. const el = modal.el.querySelector('.fingerprints .fingerprint');
  1286. expect(el.textContent.trim()).toBe(
  1287. u.formatFingerprint(u.arrayBufferToHex(u.base64ToArrayBuffer('BQmHEOHjsYm3w5M8VqxAtqJmLCi7CaxxsdZz6G0YpuMI')))
  1288. );
  1289. expect(modal.el.querySelectorAll('input[type="radio"]').length).toBe(2);
  1290. const devicelist = _converse.devicelists.get(contact_jid);
  1291. expect(devicelist.devices.get('555').get('trusted')).toBe(0);
  1292. let trusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="1"]');
  1293. expect(trusted_radio.checked).toBe(true);
  1294. let untrusted_radio = modal.el.querySelector('input[type="radio"][name="555"][value="-1"]');
  1295. expect(untrusted_radio.checked).toBe(false);
  1296. // Test that the device can be set to untrusted
  1297. untrusted_radio.click();
  1298. trusted_radio = document.querySelector('input[type="radio"][name="555"][value="1"]');
  1299. expect(trusted_radio.hasAttribute('checked')).toBe(false);
  1300. expect(devicelist.devices.get('555').get('trusted')).toBe(-1);
  1301. untrusted_radio = document.querySelector('input[type="radio"][name="555"][value="-1"]');
  1302. expect(untrusted_radio.hasAttribute('checked')).toBe(true);
  1303. trusted_radio.click();
  1304. expect(devicelist.devices.get('555').get('trusted')).toBe(1);
  1305. done();
  1306. }));
  1307. });
  1308. describe("A chatbox with an active OMEMO session", function() {
  1309. it("will not show the spoiler toolbar button",
  1310. mock.initConverseWithPromises(
  1311. null, ['rosterGroupsFetched'], {},
  1312. function (done, _converse) {
  1313. // TODO
  1314. done()
  1315. }));
  1316. });
  1317. }));