omemo.js 80 KB

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