|
|
|
@ -6,11 +6,13 @@ import { UserUtil } from '../../../util';
|
|
|
|
|
import { SessionResetMessage } from '../../../session/messages/outgoing';
|
|
|
|
|
import { TextEncoder } from 'util';
|
|
|
|
|
import { MessageSender } from '../../../session/sending';
|
|
|
|
|
import { PubKey } from '../../../session/types';
|
|
|
|
|
|
|
|
|
|
// tslint:disable-next-line: max-func-body-length
|
|
|
|
|
describe('SessionProtocol', () => {
|
|
|
|
|
const sandbox = sinon.createSandbox();
|
|
|
|
|
const ourNumber = 'ourNumber';
|
|
|
|
|
const pubkey = new PubKey('deviceid');
|
|
|
|
|
let getItemById: sinon.SinonStub;
|
|
|
|
|
let send: sinon.SinonStub;
|
|
|
|
|
|
|
|
|
@ -61,17 +63,17 @@ describe('SessionProtocol', () => {
|
|
|
|
|
|
|
|
|
|
describe('db fetch', () => {
|
|
|
|
|
it('protocol: should fetch from DB `sentSessionsTimestamp` and `processedSessionsTimestamp`', async () => {
|
|
|
|
|
await SessionProtocol.hasSentSessionRequest('test');
|
|
|
|
|
await SessionProtocol.hasSentSessionRequest(pubkey);
|
|
|
|
|
expect(getItemById.calledWith('sentSessionsTimestamp'));
|
|
|
|
|
expect(getItemById.calledWith('processedSessionsTimestamp'));
|
|
|
|
|
expect(getItemById.callCount).to.equal(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: should fetch only once', async () => {
|
|
|
|
|
await SessionProtocol.hasSentSessionRequest('test');
|
|
|
|
|
await SessionProtocol.hasSentSessionRequest('test');
|
|
|
|
|
await SessionProtocol.hasSentSessionRequest('test');
|
|
|
|
|
await SessionProtocol.hasSentSessionRequest('test');
|
|
|
|
|
await SessionProtocol.hasSentSessionRequest(pubkey);
|
|
|
|
|
await SessionProtocol.hasSentSessionRequest(pubkey);
|
|
|
|
|
await SessionProtocol.hasSentSessionRequest(pubkey);
|
|
|
|
|
await SessionProtocol.hasSentSessionRequest(pubkey);
|
|
|
|
|
expect(getItemById.calledWith('sentSessionsTimestamp'));
|
|
|
|
|
expect(getItemById.calledWith('processedSessionsTimestamp'));
|
|
|
|
|
expect(getItemById.callCount).to.equal(2);
|
|
|
|
@ -81,33 +83,33 @@ describe('SessionProtocol', () => {
|
|
|
|
|
describe('sendSessionRequest', () => {
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
// trigger a sessionReset
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, 'deviceID');
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, pubkey);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: sendSessionRequest should add the deviceID to the sentMap', async () => {
|
|
|
|
|
expect(SessionProtocol.getSentSessionsTimestamp())
|
|
|
|
|
.to.have.property('deviceID')
|
|
|
|
|
.to.have.property('deviceid')
|
|
|
|
|
.to.be.approximately(Date.now(), 100);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: sendSessionRequest should not have pendingSend set after', async () => {
|
|
|
|
|
expect(
|
|
|
|
|
SessionProtocol.getPendingSendSessionTimestamp()
|
|
|
|
|
).to.not.have.property('deviceID');
|
|
|
|
|
).to.not.have.property('deviceid');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('onSessionEstablished', () => {
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
// add an existing entry in the sentMap
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, 'deviceid');
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, pubkey);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: onSessionEstablished should remove the device in sentTimestamps', async () => {
|
|
|
|
|
expect(SessionProtocol.getSentSessionsTimestamp()).to.have.property(
|
|
|
|
|
'deviceid'
|
|
|
|
|
);
|
|
|
|
|
await SessionProtocol.onSessionEstablished('deviceid');
|
|
|
|
|
await SessionProtocol.onSessionEstablished(pubkey);
|
|
|
|
|
expect(SessionProtocol.getSentSessionsTimestamp()).to.not.have.property(
|
|
|
|
|
'deviceid'
|
|
|
|
|
);
|
|
|
|
@ -115,7 +117,7 @@ describe('SessionProtocol', () => {
|
|
|
|
|
|
|
|
|
|
it('protocol: onSessionEstablished should remove the device in sentTimestamps and ONLY that one', async () => {
|
|
|
|
|
// add a second item to the map
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, 'deviceid2');
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, new PubKey('deviceid2'));
|
|
|
|
|
|
|
|
|
|
expect(SessionProtocol.getSentSessionsTimestamp()).to.have.property(
|
|
|
|
|
'deviceid'
|
|
|
|
@ -124,7 +126,7 @@ describe('SessionProtocol', () => {
|
|
|
|
|
'deviceid2'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await SessionProtocol.onSessionEstablished('deviceid');
|
|
|
|
|
await SessionProtocol.onSessionEstablished(pubkey);
|
|
|
|
|
expect(SessionProtocol.getSentSessionsTimestamp()).to.not.have.property(
|
|
|
|
|
'deviceid'
|
|
|
|
|
);
|
|
|
|
@ -136,7 +138,7 @@ describe('SessionProtocol', () => {
|
|
|
|
|
|
|
|
|
|
describe('hasSentSessionRequest', () => {
|
|
|
|
|
it('protocol: hasSentSessionRequest returns false if a message was not sent to that device', async () => {
|
|
|
|
|
const hasSent = await SessionProtocol.hasSentSessionRequest('deviceid');
|
|
|
|
|
const hasSent = await SessionProtocol.hasSentSessionRequest(pubkey);
|
|
|
|
|
expect(hasSent).to.be.equal(
|
|
|
|
|
false,
|
|
|
|
|
'hasSent should be false for `deviceid`'
|
|
|
|
@ -145,8 +147,8 @@ describe('SessionProtocol', () => {
|
|
|
|
|
|
|
|
|
|
it('protocol: hasSentSessionRequest returns true if a message is already sent for that device', async () => {
|
|
|
|
|
// add an existing entry in the sentMap
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, 'deviceid');
|
|
|
|
|
const hasSent = await SessionProtocol.hasSentSessionRequest('deviceid');
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, pubkey);
|
|
|
|
|
const hasSent = await SessionProtocol.hasSentSessionRequest(pubkey);
|
|
|
|
|
expect(hasSent).to.be.equal(
|
|
|
|
|
true,
|
|
|
|
|
'hasSent should be true for `deviceid`'
|
|
|
|
@ -159,14 +161,14 @@ describe('SessionProtocol', () => {
|
|
|
|
|
describe('sendSessionRequestIfNeeded', () => {
|
|
|
|
|
it('protocol: sendSessionRequestIfNeeded should send a new sessionMessage ', async () => {
|
|
|
|
|
// not called before, so the message reset sending should be triggered
|
|
|
|
|
await SessionProtocol.sendSessionRequestIfNeeded('deviceid');
|
|
|
|
|
await SessionProtocol.sendSessionRequestIfNeeded(pubkey);
|
|
|
|
|
expect(send.callCount).to.be.equal(
|
|
|
|
|
1,
|
|
|
|
|
'MessageSender.send() should have been called'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// check that the map is updated with that ID
|
|
|
|
|
const hasSent = await SessionProtocol.hasSentSessionRequest('deviceid');
|
|
|
|
|
const hasSent = await SessionProtocol.hasSentSessionRequest(pubkey);
|
|
|
|
|
expect(hasSent).to.be.equal(
|
|
|
|
|
true,
|
|
|
|
|
'hasSent should be true for `deviceid`'
|
|
|
|
@ -174,14 +176,14 @@ describe('SessionProtocol', () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: sendSessionRequestIfNeeded should NOT send a new sessionMessage on second try ', async () => {
|
|
|
|
|
await SessionProtocol.sendSessionRequestIfNeeded('deviceid');
|
|
|
|
|
await SessionProtocol.sendSessionRequestIfNeeded(pubkey);
|
|
|
|
|
expect(send.callCount).to.be.equal(
|
|
|
|
|
1,
|
|
|
|
|
'MessageSender.send() should have been called'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// check that the map is updated with that ID
|
|
|
|
|
const hasSent = await SessionProtocol.hasSentSessionRequest('deviceid');
|
|
|
|
|
const hasSent = await SessionProtocol.hasSentSessionRequest(pubkey);
|
|
|
|
|
expect(hasSent).to.be.equal(
|
|
|
|
|
true,
|
|
|
|
|
'hasSent should be true for `deviceid`'
|
|
|
|
@ -189,7 +191,7 @@ describe('SessionProtocol', () => {
|
|
|
|
|
send.resetHistory();
|
|
|
|
|
|
|
|
|
|
// trigger a second call, Message.send().calledCount should still be 1
|
|
|
|
|
await SessionProtocol.sendSessionRequestIfNeeded('deviceid');
|
|
|
|
|
await SessionProtocol.sendSessionRequestIfNeeded(pubkey);
|
|
|
|
|
expect(send.callCount).to.be.equal(
|
|
|
|
|
0,
|
|
|
|
|
'MessageSender.send() should NOT have been called a second time'
|
|
|
|
@ -200,7 +202,7 @@ describe('SessionProtocol', () => {
|
|
|
|
|
describe('onSessionRequestProcessed', () => {
|
|
|
|
|
it('protocol: onSessionRequestProcessed should insert a new item in the processedMap ', async () => {
|
|
|
|
|
// trigger the requestProcessed and check the map is updated
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed('deviceid');
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed(pubkey);
|
|
|
|
|
expect(SessionProtocol.getProcessedSessionsTimestamp())
|
|
|
|
|
.to.have.property('deviceid')
|
|
|
|
|
.to.be.approximately(Date.now(), 5);
|
|
|
|
@ -210,14 +212,14 @@ describe('SessionProtocol', () => {
|
|
|
|
|
// trigger the requestProcessed and check the map is updated
|
|
|
|
|
// then trigger it a second time, and expect a change in the processed timestamp
|
|
|
|
|
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed('deviceid');
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed(pubkey);
|
|
|
|
|
expect(SessionProtocol.getProcessedSessionsTimestamp())
|
|
|
|
|
.to.have.property('deviceid')
|
|
|
|
|
.to.be.approximately(Date.now(), 5);
|
|
|
|
|
await timeout(5);
|
|
|
|
|
const oldTimestamp = SessionProtocol.getProcessedSessionsTimestamp()
|
|
|
|
|
.deviceid;
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed('deviceid');
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed(pubkey);
|
|
|
|
|
expect(SessionProtocol.getProcessedSessionsTimestamp())
|
|
|
|
|
.to.have.property('deviceid')
|
|
|
|
|
.to.be.approximately(Date.now(), 5)
|
|
|
|
@ -227,10 +229,10 @@ describe('SessionProtocol', () => {
|
|
|
|
|
|
|
|
|
|
describe('shouldProcessSessionRequest', () => {
|
|
|
|
|
it('protocol: shouldProcessSessionRequest returns true if timestamp is more recent than processed timestamp', async () => {
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed('deviceid'); // adds a Date.now() entry
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed(pubkey); // adds a Date.now() entry
|
|
|
|
|
expect(
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest(
|
|
|
|
|
'deviceid',
|
|
|
|
|
pubkey,
|
|
|
|
|
Date.now() + 1000
|
|
|
|
|
)
|
|
|
|
|
).to.be.eventually.equal(
|
|
|
|
@ -241,7 +243,7 @@ describe('SessionProtocol', () => {
|
|
|
|
|
|
|
|
|
|
it('protocol: shouldProcessSessionRequest returns true if there is no processed timestamp yet for this device', async () => {
|
|
|
|
|
expect(
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest('deviceid', 100)
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest(pubkey, 100)
|
|
|
|
|
).to.be.eventually.equal(
|
|
|
|
|
true,
|
|
|
|
|
'shouldProcessSessionRequest should return false when existingProcessed is empty for this device'
|
|
|
|
@ -249,9 +251,9 @@ describe('SessionProtocol', () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: shouldProcessSessionRequest returns false if timestamp is less recent than current processed timestamp', async () => {
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed('deviceid'); // adds a Date.now() entry
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed(pubkey); // adds a Date.now() entry
|
|
|
|
|
expect(
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest('deviceid', 100)
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest(pubkey, 100)
|
|
|
|
|
).to.be.eventually.equal(
|
|
|
|
|
false,
|
|
|
|
|
'shouldProcessSessionRequest should return false when existingProcessed is more recent'
|
|
|
|
@ -259,9 +261,9 @@ describe('SessionProtocol', () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: shouldProcessSessionRequest returns false if timestamp is less recent than current sent timestamp', async () => {
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, 'deviceid'); // adds a Date.now() entry
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, pubkey); // adds a Date.now() entry
|
|
|
|
|
expect(
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest('deviceid', 100)
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest(pubkey, 100)
|
|
|
|
|
).to.be.eventually.equal(
|
|
|
|
|
false,
|
|
|
|
|
'shouldProcessSessionRequest should return false when existingSent is more recent'
|
|
|
|
@ -269,10 +271,10 @@ describe('SessionProtocol', () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: shouldProcessSessionRequest returns true if timestamp is more recent than current sent timestamp', async () => {
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, 'deviceid'); // adds a Date.now() entry
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, pubkey); // adds a Date.now() entry
|
|
|
|
|
expect(
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest(
|
|
|
|
|
'deviceid',
|
|
|
|
|
pubkey,
|
|
|
|
|
Date.now() + 1000
|
|
|
|
|
)
|
|
|
|
|
).to.be.eventually.equal(
|
|
|
|
@ -283,7 +285,7 @@ describe('SessionProtocol', () => {
|
|
|
|
|
|
|
|
|
|
it('protocol: shouldProcessSessionRequest returns true if there is no sent timestamp', async () => {
|
|
|
|
|
expect(
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest('deviceid', 100)
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest(pubkey, 100)
|
|
|
|
|
).to.be.eventually.equal(
|
|
|
|
|
true,
|
|
|
|
|
'shouldProcessSessionRequest should return true as there is no sent timestamp'
|
|
|
|
@ -291,12 +293,12 @@ describe('SessionProtocol', () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: shouldProcessSessionRequest returns false if there is a more recent sent but a less recent processed', async () => {
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, 'deviceid'); // adds a Date.now() entry
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, pubkey); // adds a Date.now() entry
|
|
|
|
|
await timeout(100);
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed('deviceid'); // adds a Date.now() entry 100ms after
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed(pubkey); // adds a Date.now() entry 100ms after
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest('deviceid', Date.now() - 50)
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest(pubkey, Date.now() - 50)
|
|
|
|
|
).to.be.eventually.equal(
|
|
|
|
|
false,
|
|
|
|
|
'shouldProcessSessionRequest should return false if there is a more recent sent but a less recent processed'
|
|
|
|
@ -304,12 +306,12 @@ describe('SessionProtocol', () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: shouldProcessSessionRequest returns false if there is a more recent processed but a less recent sent', async () => {
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed('deviceid'); // adds a Date.now() entry
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed(pubkey); // adds a Date.now() entry
|
|
|
|
|
await timeout(100);
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, 'deviceid'); // adds a Date.now() entry 100ms after
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, pubkey); // adds a Date.now() entry 100ms after
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest('deviceid', Date.now() - 50)
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest(pubkey, Date.now() - 50)
|
|
|
|
|
).to.be.eventually.equal(
|
|
|
|
|
false,
|
|
|
|
|
'shouldProcessSessionRequest should return false if there is a more recent processed but a less recent sent'
|
|
|
|
@ -317,11 +319,11 @@ describe('SessionProtocol', () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('protocol: shouldProcessSessionRequest returns true if both sent and processed timestamp are older', async () => {
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed('deviceid'); // adds a Date.now() entry
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, 'deviceid'); // adds a Date.now() entry
|
|
|
|
|
await SessionProtocol.onSessionRequestProcessed(pubkey); // adds a Date.now() entry
|
|
|
|
|
await SessionProtocol.sendSessionRequest(resetMessage, pubkey); // adds a Date.now() entry
|
|
|
|
|
expect(
|
|
|
|
|
SessionProtocol.shouldProcessSessionRequest(
|
|
|
|
|
'deviceid',
|
|
|
|
|
pubkey,
|
|
|
|
|
Date.now() + 1000
|
|
|
|
|
)
|
|
|
|
|
).to.be.eventually.equal(
|
|
|
|
|