omemo.js 54 KB

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