2018-05-29 12:00:23 +02:00
|
|
|
(function (root, factory) {
|
|
|
|
define(["jasmine", "mock", "test-utils"], factory);
|
|
|
|
} (this, function (jasmine, mock, test_utils) {
|
|
|
|
"use strict";
|
|
|
|
const $iq = converse.env.$iq;
|
|
|
|
const Strophe = converse.env.Strophe;
|
|
|
|
const sizzle = converse.env.sizzle;
|
|
|
|
const u = converse.env.utils;
|
|
|
|
|
|
|
|
describe("XEP-0198 Stream Management", function () {
|
|
|
|
|
|
|
|
it("gets enabled with an <enable> stanza and resumed with a <resume> stanza",
|
|
|
|
mock.initConverse(
|
2019-10-11 16:38:01 +02:00
|
|
|
['chatBoxesInitialized'],
|
2018-05-29 12:00:23 +02:00
|
|
|
{ 'auto_login': false,
|
|
|
|
'enable_smacks': true,
|
|
|
|
'show_controlbox_by_default': true,
|
|
|
|
'smacks_max_unacked_stanzas': 2
|
|
|
|
},
|
|
|
|
async function (done, _converse) {
|
|
|
|
|
|
|
|
const view = _converse.chatboxviews.get('controlbox');
|
|
|
|
spyOn(view, 'renderControlBoxPane').and.callThrough();
|
|
|
|
|
2019-06-25 22:53:39 +02:00
|
|
|
_converse.api.user.login('romeo@montague.lit/orchard', 'secret');
|
2018-05-29 12:00:23 +02:00
|
|
|
const sent_stanzas = _converse.connection.sent_stanzas;
|
2019-07-11 22:50:30 +02:00
|
|
|
let stanza = await u.waitUntil(() =>
|
2018-05-29 12:00:23 +02:00
|
|
|
sent_stanzas.filter(s => (s.tagName === 'enable')).pop());
|
|
|
|
|
|
|
|
expect(_converse.session.get('smacks_enabled')).toBe(false);
|
|
|
|
expect(Strophe.serialize(stanza)).toEqual('<enable resume="true" xmlns="urn:xmpp:sm:3"/>');
|
|
|
|
|
|
|
|
let result = u.toStanza(`<enabled xmlns="urn:xmpp:sm:3" id="some-long-sm-id" resume="true"/>`);
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(result));
|
|
|
|
expect(_converse.session.get('smacks_enabled')).toBe(true);
|
|
|
|
|
2019-07-11 22:50:30 +02:00
|
|
|
await u.waitUntil(() => view.renderControlBoxPane.calls.count());
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
|
|
let IQ_stanzas = _converse.connection.IQ_stanzas;
|
2019-07-11 22:50:30 +02:00
|
|
|
await u.waitUntil(() => IQ_stanzas.length === 4);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
2019-10-24 14:29:15 +02:00
|
|
|
let iq = IQ_stanzas[IQ_stanzas.length-1];
|
2019-10-17 13:50:32 +02:00
|
|
|
expect(Strophe.serialize(iq)).toBe(
|
|
|
|
`<iq id="${iq.getAttribute('id')}" type="get" xmlns="jabber:client"><query xmlns="jabber:iq:roster"/></iq>`);
|
|
|
|
await test_utils.waitForRoster(_converse, 'current', 1);
|
2019-10-15 12:55:11 +02:00
|
|
|
IQ_stanzas.pop();
|
2019-10-24 14:29:15 +02:00
|
|
|
|
2019-10-15 12:55:11 +02:00
|
|
|
const disco_iq = IQ_stanzas.pop();
|
|
|
|
expect(Strophe.serialize(disco_iq)).toBe(
|
|
|
|
`<iq from="romeo@montague.lit" id="${disco_iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
|
|
|
|
`<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub></iq>`);
|
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
iq = IQ_stanzas.pop();
|
|
|
|
expect(Strophe.serialize(iq)).toBe(
|
2019-10-24 14:29:15 +02:00
|
|
|
`<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
|
2018-05-29 12:00:23 +02:00
|
|
|
`<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
|
|
|
|
|
2019-10-24 14:29:15 +02:00
|
|
|
iq = IQ_stanzas.pop();
|
|
|
|
expect(Strophe.serialize(iq)).toBe(
|
|
|
|
`<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="montague.lit" type="get" xmlns="jabber:client">`+
|
|
|
|
`<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
|
|
expect(sent_stanzas.filter(s => (s.nodeName === 'r')).length).toBe(2);
|
2019-10-15 14:24:01 +02:00
|
|
|
expect(_converse.session.get('unacked_stanzas').length).toBe(5);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
|
|
// test handling of acks
|
2019-10-17 13:50:32 +02:00
|
|
|
let ack = u.toStanza(`<a xmlns="urn:xmpp:sm:3" h="2"/>`);
|
2018-05-29 12:00:23 +02:00
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(ack));
|
2019-10-17 13:50:32 +02:00
|
|
|
expect(_converse.session.get('unacked_stanzas').length).toBe(3);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
|
|
// test handling of ack requests
|
|
|
|
let r = u.toStanza(`<r xmlns="urn:xmpp:sm:3"/>`);
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(r));
|
2019-10-15 14:24:01 +02:00
|
|
|
|
2019-07-11 22:50:30 +02:00
|
|
|
ack = await u.waitUntil(() => sent_stanzas.filter(s => (s.nodeName === 'a')).pop());
|
2019-10-15 14:24:01 +02:00
|
|
|
expect(Strophe.serialize(ack)).toBe('<a h="1" xmlns="urn:xmpp:sm:3"/>');
|
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
|
|
const disco_result = $iq({
|
|
|
|
'type': 'result',
|
2019-06-03 07:58:51 +02:00
|
|
|
'from': 'montague.lit',
|
|
|
|
'to': 'romeo@montague.lit/orchard',
|
2018-05-29 12:00:23 +02:00
|
|
|
'id': disco_iq.getAttribute('id'),
|
|
|
|
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
|
|
|
|
.c('identity', {
|
|
|
|
'category': 'server',
|
|
|
|
'type': 'im'
|
|
|
|
}).up()
|
|
|
|
.c('feature', {'var': 'http://jabber.org/protocol/disco#info'}).up()
|
|
|
|
.c('feature', {'var': 'http://jabber.org/protocol/disco#items'});
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(disco_result));
|
|
|
|
|
2019-10-17 13:50:32 +02:00
|
|
|
ack = u.toStanza(`<a xmlns="urn:xmpp:sm:3" h="3"/>`);
|
2018-05-29 12:00:23 +02:00
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(ack));
|
2019-10-17 13:50:32 +02:00
|
|
|
expect(_converse.session.get('unacked_stanzas').length).toBe(2);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
|
|
r = u.toStanza(`<r xmlns="urn:xmpp:sm:3"/>`);
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(r));
|
2019-07-11 22:50:30 +02:00
|
|
|
ack = await u.waitUntil(() => sent_stanzas.filter(s => (s.nodeName === 'a' && s.getAttribute('h') === '1')).pop());
|
2018-05-29 12:00:23 +02:00
|
|
|
expect(Strophe.serialize(ack)).toBe('<a h="1" xmlns="urn:xmpp:sm:3"/>');
|
|
|
|
|
|
|
|
// test session resumption
|
|
|
|
_converse.connection.IQ_stanzas = [];
|
|
|
|
IQ_stanzas = _converse.connection.IQ_stanzas;
|
2019-10-24 14:29:15 +02:00
|
|
|
await _converse.api.connection.reconnect();
|
2019-10-15 14:24:01 +02:00
|
|
|
stanza = await u.waitUntil(() => sent_stanzas.filter(s => (s.tagName === 'resume')).pop());
|
2019-10-17 13:50:32 +02:00
|
|
|
|
2019-10-15 14:24:01 +02:00
|
|
|
expect(Strophe.serialize(stanza)).toEqual('<resume h="2" previd="some-long-sm-id" xmlns="urn:xmpp:sm:3"/>');
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
|
|
result = u.toStanza(`<resumed xmlns="urn:xmpp:sm:3" h="another-sequence-number" previd="some-long-sm-id"/>`);
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(result));
|
|
|
|
|
|
|
|
// Another <enable> stanza doesn't get sent out
|
2019-10-15 14:24:01 +02:00
|
|
|
expect(sent_stanzas.filter(s => (s.tagName === 'enable')).length).toBe(1);
|
2018-05-29 12:00:23 +02:00
|
|
|
expect(_converse.session.get('smacks_enabled')).toBe(true);
|
|
|
|
|
2019-10-17 13:50:32 +02:00
|
|
|
await u.waitUntil(() => IQ_stanzas.length === 1);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
|
|
// Test that unacked stanzas get resent out
|
|
|
|
iq = IQ_stanzas.pop();
|
2019-10-24 14:29:15 +02:00
|
|
|
expect(Strophe.serialize(iq)).toBe(`<iq id="${iq.getAttribute('id')}" type="get" xmlns="jabber:client"><query xmlns="jabber:iq:roster"/></iq>`);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
2019-10-15 12:23:41 +02:00
|
|
|
expect(IQ_stanzas.filter(iq => sizzle('query[xmlns="jabber:iq:roster"]', iq).pop()).length).toBe(0);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
2019-06-05 11:27:59 +02:00
|
|
|
await _converse.api.waitUntil('statusInitialized');
|
2018-05-29 12:00:23 +02:00
|
|
|
done();
|
|
|
|
}));
|
2019-10-15 14:24:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
it("might not resume and the session will then be reset",
|
|
|
|
mock.initConverse(
|
|
|
|
['chatBoxesInitialized'],
|
|
|
|
{ 'auto_login': false,
|
|
|
|
'enable_smacks': true,
|
|
|
|
'show_controlbox_by_default': true,
|
|
|
|
'smacks_max_unacked_stanzas': 2
|
|
|
|
},
|
|
|
|
async function (done, _converse) {
|
|
|
|
|
|
|
|
_converse.api.user.login('romeo@montague.lit/orchard', 'secret');
|
|
|
|
const sent_stanzas = _converse.connection.sent_stanzas;
|
|
|
|
let stanza = await u.waitUntil(() => sent_stanzas.filter(s => (s.tagName === 'enable')).pop());
|
|
|
|
expect(Strophe.serialize(stanza)).toEqual('<enable resume="true" xmlns="urn:xmpp:sm:3"/>');
|
|
|
|
let result = u.toStanza(`<enabled xmlns="urn:xmpp:sm:3" id="some-long-sm-id" resume="true"/>`);
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(result));
|
|
|
|
|
|
|
|
await test_utils.waitForRoster(_converse, 'current', 1);
|
|
|
|
|
|
|
|
// test session resumption
|
2019-10-24 14:29:15 +02:00
|
|
|
await _converse.api.connection.reconnect();
|
2019-10-15 14:24:01 +02:00
|
|
|
stanza = await u.waitUntil(() => sent_stanzas.filter(s => (s.tagName === 'resume')).pop());
|
|
|
|
expect(Strophe.serialize(stanza)).toEqual('<resume h="1" previd="some-long-sm-id" xmlns="urn:xmpp:sm:3"/>');
|
|
|
|
|
|
|
|
result = u.toStanza(
|
|
|
|
`<failed xmlns="urn:xmpp:sm:3" h="another-sequence-number">`+
|
|
|
|
`<item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>`+
|
|
|
|
`</failed>`);
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(result));
|
|
|
|
|
|
|
|
// Session data gets reset
|
|
|
|
expect(_converse.session.get('smacks_enabled')).toBe(false);
|
|
|
|
expect(_converse.session.get('num_stanzas_handled')).toBe(0);
|
|
|
|
expect(_converse.session.get('num_stanzas_handled_by_server')).toBe(0);
|
|
|
|
expect(_converse.session.get('num_stanzas_since_last_ack')).toBe(0);
|
|
|
|
expect(_converse.session.get('unacked_stanzas').length).toBe(0);
|
2019-10-17 13:50:32 +02:00
|
|
|
expect(_converse.session.get('roster_cached')).toBeFalsy();
|
|
|
|
|
2019-10-15 14:24:01 +02:00
|
|
|
|
|
|
|
await u.waitUntil(() => sent_stanzas.filter(s => (s.tagName === 'enable')).length === 2);
|
|
|
|
stanza = sent_stanzas.filter(s => (s.tagName === 'enable')).pop();
|
|
|
|
expect(Strophe.serialize(stanza)).toEqual('<enable resume="true" xmlns="urn:xmpp:sm:3"/>');
|
|
|
|
|
|
|
|
result = u.toStanza(`<enabled xmlns="urn:xmpp:sm:3" id="another-long-sm-id" resume="true"/>`);
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(result));
|
|
|
|
expect(_converse.session.get('smacks_enabled')).toBe(true);
|
|
|
|
|
|
|
|
// Check that the roster gets fetched
|
|
|
|
await test_utils.waitForRoster(_converse, 'current', 1);
|
|
|
|
done();
|
|
|
|
}));
|
2018-05-29 12:00:23 +02:00
|
|
|
});
|
|
|
|
}));
|