diff --git a/ts/session/messages/outgoing/content/EndSessionMessage.ts b/ts/session/messages/outgoing/content/EndSessionMessage.ts index 6cda04338..e7092413c 100644 --- a/ts/session/messages/outgoing/content/EndSessionMessage.ts +++ b/ts/session/messages/outgoing/content/EndSessionMessage.ts @@ -4,10 +4,19 @@ import { SignalService } from '../../../../protobuf'; export class EndSessionMessage extends SessionResetMessage { public ttl(): number { - return this.getDefaultTTL(); + return 4 * 24 * 60 * 60 * 1000; // 4 days } protected contentProto(): SignalService.Content { - throw new Error('Not implemented'); + const dataMessage = new SignalService.DataMessage({ + body: 'TERMINATE', + flags: SignalService.DataMessage.Flags.END_SESSION, + }); + const preKeyBundleMessage = this.getPreKeyBundleMessage(); + + return new SignalService.Content({ + dataMessage, + preKeyBundleMessage, + }); } } diff --git a/ts/session/messages/outgoing/content/SessionResetMessage.ts b/ts/session/messages/outgoing/content/SessionResetMessage.ts index 6b550fe96..77ccf0060 100644 --- a/ts/session/messages/outgoing/content/SessionResetMessage.ts +++ b/ts/session/messages/outgoing/content/SessionResetMessage.ts @@ -1,13 +1,47 @@ import { ContentMessage } from './ContentMessage'; import { SignalService } from '../../../../protobuf'; + +export interface PreKeyBundleType { + identityKey?: (Uint8Array|null); + deviceId?: (number|null); + preKeyId?: (number|null); + signedKeyId?: (number|null); + preKey?: (Uint8Array|null); + signedKey?: (Uint8Array|null); + signature?: (Uint8Array|null); +} + + +interface SessionResetParams { + timestamp: number; + identifier: string; + preKeyBundle: PreKeyBundleType; +} + export class SessionResetMessage extends ContentMessage { + private readonly preKeyBundle: PreKeyBundleType; + + constructor(params: SessionResetParams) { + super({ timestamp: params.timestamp, identifier: params.identifier }); + this.preKeyBundle = params.preKeyBundle; + } public ttl(): number { - return this.getDefaultTTL(); + return 4 * 24 * 60 * 60 * 1000; // 4 days + } + + protected getPreKeyBundleMessage(): SignalService.PreKeyBundleMessage { + return new SignalService.PreKeyBundleMessage(this.preKeyBundle); } protected contentProto(): SignalService.Content { - throw new Error('Not implemented'); + const nullMessage = new SignalService.NullMessage({}); + const preKeyBundleMessage = this.getPreKeyBundleMessage(); + + return new SignalService.Content({ + nullMessage, + preKeyBundleMessage, + }); } } diff --git a/ts/test/session/messages/EndSessionMessage_test.ts b/ts/test/session/messages/EndSessionMessage_test.ts new file mode 100644 index 000000000..e091b53e2 --- /dev/null +++ b/ts/test/session/messages/EndSessionMessage_test.ts @@ -0,0 +1,34 @@ +import { expect } from 'chai'; +import { beforeEach} from 'mocha'; + +import { EndSessionMessage } from '../../../session/messages/outgoing'; +import { SignalService } from '../../../protobuf'; + +describe('EndSessionMessage', () => { + let message: EndSessionMessage; + beforeEach(() => { + const timestamp = Date.now(); + const identifier = '123456'; + const preKeyBundle = {deviceId: 123456}; + message = new EndSessionMessage({timestamp, identifier, preKeyBundle}); + }); + + it('has a preKeyBundle', () => { + const plainText = message.plainTextBuffer(); + const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); + + expect(decoded.preKeyBundleMessage).to.have.property('deviceId', 123456); + }); + + it('has a dataMessage with `END_SESSION` flag and `TERMINATE` as body', () => { + const plainText = message.plainTextBuffer(); + const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); + + expect(decoded.dataMessage).to.have.property('flags', SignalService.DataMessage.Flags.END_SESSION); + expect(decoded.dataMessage).to.have.deep.property('body', 'TERMINATE'); + }); + + it('ttl of 4 days', () => { + expect(message.ttl()).to.equal(4 * 24 * 60 * 60 * 1000); + }); +}); diff --git a/ts/test/session/messages/SessionResetMessage_test.ts b/ts/test/session/messages/SessionResetMessage_test.ts new file mode 100644 index 000000000..d770b7799 --- /dev/null +++ b/ts/test/session/messages/SessionResetMessage_test.ts @@ -0,0 +1,33 @@ +import { expect } from 'chai'; +import { beforeEach} from 'mocha'; + +import { SessionResetMessage } from '../../../session/messages/outgoing'; +import { SignalService } from '../../../protobuf'; + +describe('SessionResetMessage', () => { + let message: SessionResetMessage; + beforeEach(() => { + const timestamp = Date.now(); + const identifier = '123456'; + const preKeyBundle = {deviceId: 123456}; + message = new SessionResetMessage({timestamp, identifier, preKeyBundle}); + }); + + it('has a preKeyBundle', () => { + const plainText = message.plainTextBuffer(); + const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); + + expect(decoded.preKeyBundleMessage).to.have.property('deviceId', 123456); + }); + + it('has a nullMessage not null', () => { + const plainText = message.plainTextBuffer(); + const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); + + expect(decoded.nullMessage).to.be.not.equal(null, 'decoded.dataMessage.nullMessage should not be null'); + }); + + it('ttl of 4 days', () => { + expect(message.ttl()).to.equal(4 * 24 * 60 * 60 * 1000); + }); +});