2021-07-09 19:36:10 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
2021-07-09 20:12:29 +00:00
|
|
|
import Long from 'long';
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
processDataMessage,
|
|
|
|
ATTACHMENT_MAX,
|
|
|
|
} from '../textsecure/processDataMessage';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ProcessedAttachment } from '../textsecure/Types.d';
|
2021-07-09 19:36:10 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
2022-06-13 21:39:35 +00:00
|
|
|
import { IMAGE_GIF } from '../types/MIME';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { generateAci } from '../types/ServiceId';
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const ACI_1 = generateAci();
|
2021-07-09 19:36:10 +00:00
|
|
|
const FLAGS = Proto.DataMessage.Flags;
|
|
|
|
|
|
|
|
const TIMESTAMP = Date.now();
|
|
|
|
|
|
|
|
const UNPROCESSED_ATTACHMENT: Proto.IAttachmentPointer = {
|
2022-03-23 20:49:27 +00:00
|
|
|
cdnId: Long.fromNumber(123),
|
2021-07-09 19:36:10 +00:00
|
|
|
key: new Uint8Array([1, 2, 3]),
|
|
|
|
digest: new Uint8Array([4, 5, 6]),
|
2022-06-13 21:39:35 +00:00
|
|
|
contentType: IMAGE_GIF,
|
|
|
|
size: 34,
|
2021-07-09 19:36:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const PROCESSED_ATTACHMENT: ProcessedAttachment = {
|
|
|
|
cdnId: '123',
|
|
|
|
key: 'AQID',
|
|
|
|
digest: 'BAUG',
|
2022-06-13 21:39:35 +00:00
|
|
|
contentType: IMAGE_GIF,
|
|
|
|
size: 34,
|
2021-07-09 19:36:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
describe('processDataMessage', () => {
|
|
|
|
const check = (message: Proto.IDataMessage) =>
|
|
|
|
processDataMessage(
|
|
|
|
{
|
2022-03-23 20:49:27 +00:00
|
|
|
timestamp: Long.fromNumber(TIMESTAMP),
|
2021-07-09 19:36:10 +00:00
|
|
|
...message,
|
|
|
|
},
|
|
|
|
TIMESTAMP
|
|
|
|
);
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should process attachments', () => {
|
|
|
|
const out = check({
|
2021-07-09 19:36:10 +00:00
|
|
|
attachments: [UNPROCESSED_ATTACHMENT],
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepStrictEqual(out.attachments, [PROCESSED_ATTACHMENT]);
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should process attachments with 0 cdnId', () => {
|
|
|
|
const out = check({
|
2021-07-09 20:12:29 +00:00
|
|
|
attachments: [
|
|
|
|
{
|
|
|
|
...UNPROCESSED_ATTACHMENT,
|
|
|
|
cdnId: new Long(0),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepStrictEqual(out.attachments, [
|
|
|
|
{
|
|
|
|
...PROCESSED_ATTACHMENT,
|
|
|
|
cdnId: undefined,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should throw on too many attachments', () => {
|
2021-07-09 19:36:10 +00:00
|
|
|
const attachments: Array<Proto.IAttachmentPointer> = [];
|
|
|
|
for (let i = 0; i < ATTACHMENT_MAX + 1; i += 1) {
|
|
|
|
attachments.push(UNPROCESSED_ATTACHMENT);
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
assert.throws(
|
|
|
|
() => check({ attachments }),
|
2021-07-09 19:36:10 +00:00
|
|
|
`Too many attachments: ${ATTACHMENT_MAX + 1} included in one message` +
|
|
|
|
`, max is ${ATTACHMENT_MAX}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should process groupv2 context', () => {
|
|
|
|
const out = check({
|
2021-07-09 19:36:10 +00:00
|
|
|
groupV2: {
|
|
|
|
masterKey: new Uint8Array(32),
|
|
|
|
revision: 1,
|
|
|
|
groupChange: new Uint8Array([4, 5, 6]),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepStrictEqual(out.groupV2, {
|
|
|
|
masterKey: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=',
|
|
|
|
revision: 1,
|
|
|
|
groupChange: 'BAUG',
|
|
|
|
id: 'd/rq8//fR4RzhvN3G9KcKlQoj7cguQFjTOqLV6JUSbo=',
|
|
|
|
secretParams:
|
|
|
|
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAd/rq8//fR' +
|
|
|
|
'4RzhvN3G9KcKlQoj7cguQFjTOqLV6JUSbrURzeILsUmsymGJmHt3kpBJ2zosqp4ex' +
|
|
|
|
'sg+qwF1z6YdB/rxKnxKRLZZP/V0F7bERslYILy2lUh3Sh3iA98yO4CGfzjjFVo1SI' +
|
|
|
|
'7U8XApLeVNQHJo7nkflf/JyBrqPft5gEucbKW/h+S3OYjfQ5zl2Cpw3XrV7N6OKEu' +
|
|
|
|
'tLUWPHQuJx11A4xDPrmtAOnGy2NBxoOybDNlWipeNbn1WQJqOjMF7YA80oEm+5qnM' +
|
|
|
|
'kEYcFVqbYaSzPcMhg3mQ0SYfQpxYgSOJpwp9f/8EDnwJV4ISPBOo2CiaSqVfnd8Dw' +
|
|
|
|
'ZOc58gQA==',
|
|
|
|
publicParams:
|
|
|
|
'AHf66vP/30eEc4bzdxvSnCpUKI+3ILkBY0zqi1eiVEm6LnGylv4fk' +
|
|
|
|
'tzmI30Oc5dgqcN161ezejihLrS1Fjx0LieOJpwp9f/8EDnwJV4ISPBOo2CiaSqVfn' +
|
|
|
|
'd8DwZOc58gQA==',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should base64 profileKey', () => {
|
|
|
|
const out = check({
|
2021-07-09 19:36:10 +00:00
|
|
|
profileKey: new Uint8Array([42, 23, 55]),
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.strictEqual(out.profileKey, 'Khc3');
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should process quote', () => {
|
|
|
|
const out = check({
|
2021-07-09 19:36:10 +00:00
|
|
|
quote: {
|
2022-03-23 20:49:27 +00:00
|
|
|
id: Long.fromNumber(1),
|
2023-08-16 20:54:39 +00:00
|
|
|
authorAci: ACI_1,
|
2021-07-09 19:36:10 +00:00
|
|
|
text: 'text',
|
|
|
|
attachments: [
|
|
|
|
{
|
|
|
|
contentType: 'image/jpeg',
|
|
|
|
fileName: 'image.jpg',
|
|
|
|
thumbnail: UNPROCESSED_ATTACHMENT,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepStrictEqual(out.quote, {
|
|
|
|
id: 1,
|
2023-08-16 20:54:39 +00:00
|
|
|
authorAci: ACI_1,
|
2021-07-09 19:36:10 +00:00
|
|
|
text: 'text',
|
|
|
|
attachments: [
|
|
|
|
{
|
|
|
|
contentType: 'image/jpeg',
|
|
|
|
fileName: 'image.jpg',
|
|
|
|
thumbnail: PROCESSED_ATTACHMENT,
|
|
|
|
},
|
|
|
|
],
|
2023-04-12 00:16:46 +00:00
|
|
|
bodyRanges: undefined,
|
2022-05-11 20:59:58 +00:00
|
|
|
type: 0,
|
2021-07-09 19:36:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should process contact', () => {
|
|
|
|
const out = check({
|
2021-07-09 19:36:10 +00:00
|
|
|
contact: [
|
|
|
|
{
|
|
|
|
avatar: {
|
|
|
|
avatar: UNPROCESSED_ATTACHMENT,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
avatar: {
|
|
|
|
avatar: UNPROCESSED_ATTACHMENT,
|
|
|
|
isProfile: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepStrictEqual(out.contact, [
|
|
|
|
{
|
|
|
|
avatar: { avatar: PROCESSED_ATTACHMENT, isProfile: false },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
avatar: { avatar: PROCESSED_ATTACHMENT, isProfile: true },
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should process reaction', () => {
|
2021-07-09 19:36:10 +00:00
|
|
|
assert.deepStrictEqual(
|
2022-11-09 18:59:32 +00:00
|
|
|
check({
|
|
|
|
reaction: {
|
|
|
|
emoji: '😎',
|
2023-08-16 20:54:39 +00:00
|
|
|
targetAuthorAci: ACI_1,
|
2022-11-09 18:59:32 +00:00
|
|
|
targetTimestamp: Long.fromNumber(TIMESTAMP),
|
|
|
|
},
|
|
|
|
}).reaction,
|
2021-07-09 19:36:10 +00:00
|
|
|
{
|
|
|
|
emoji: '😎',
|
|
|
|
remove: false,
|
2023-08-16 20:54:39 +00:00
|
|
|
targetAuthorAci: ACI_1,
|
2021-07-09 19:36:10 +00:00
|
|
|
targetTimestamp: TIMESTAMP,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepStrictEqual(
|
2022-11-09 18:59:32 +00:00
|
|
|
check({
|
|
|
|
reaction: {
|
|
|
|
emoji: '😎',
|
|
|
|
remove: true,
|
2023-08-16 20:54:39 +00:00
|
|
|
targetAuthorAci: ACI_1,
|
2022-11-09 18:59:32 +00:00
|
|
|
targetTimestamp: Long.fromNumber(TIMESTAMP),
|
|
|
|
},
|
|
|
|
}).reaction,
|
2021-07-09 19:36:10 +00:00
|
|
|
{
|
|
|
|
emoji: '😎',
|
|
|
|
remove: true,
|
2023-08-16 20:54:39 +00:00
|
|
|
targetAuthorAci: ACI_1,
|
2021-07-09 19:36:10 +00:00
|
|
|
targetTimestamp: TIMESTAMP,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should process preview', () => {
|
|
|
|
const out = check({
|
2021-07-09 19:36:10 +00:00
|
|
|
preview: [
|
|
|
|
{
|
2022-03-23 20:49:27 +00:00
|
|
|
date: Long.fromNumber(TIMESTAMP),
|
2021-07-09 19:36:10 +00:00
|
|
|
image: UNPROCESSED_ATTACHMENT,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepStrictEqual(out.preview, [
|
|
|
|
{
|
|
|
|
date: TIMESTAMP,
|
|
|
|
description: undefined,
|
|
|
|
title: undefined,
|
|
|
|
url: undefined,
|
|
|
|
image: PROCESSED_ATTACHMENT,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should process sticker', () => {
|
|
|
|
const out = check({
|
2021-07-09 19:36:10 +00:00
|
|
|
sticker: {
|
|
|
|
packId: new Uint8Array([1, 2, 3]),
|
|
|
|
packKey: new Uint8Array([4, 5, 6]),
|
|
|
|
stickerId: 1,
|
2022-08-08 18:21:00 +00:00
|
|
|
emoji: '💯',
|
2021-07-09 19:36:10 +00:00
|
|
|
data: UNPROCESSED_ATTACHMENT,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepStrictEqual(out.sticker, {
|
|
|
|
packId: '010203',
|
|
|
|
packKey: 'BAUG',
|
|
|
|
stickerId: 1,
|
2022-08-08 18:21:00 +00:00
|
|
|
emoji: '💯',
|
2021-07-09 19:36:10 +00:00
|
|
|
data: PROCESSED_ATTACHMENT,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should process FLAGS=END_SESSION', () => {
|
|
|
|
const out = check({
|
2021-07-09 19:36:10 +00:00
|
|
|
flags: FLAGS.END_SESSION,
|
|
|
|
body: 'should be deleted',
|
|
|
|
attachments: [UNPROCESSED_ATTACHMENT],
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.isUndefined(out.body);
|
|
|
|
assert.deepStrictEqual(out.attachments, []);
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('should process FLAGS=EXPIRATION_TIMER_UPDATE,PROFILE_KEY_UPDATE', () => {
|
2021-07-09 19:36:10 +00:00
|
|
|
const values = [FLAGS.EXPIRATION_TIMER_UPDATE, FLAGS.PROFILE_KEY_UPDATE];
|
|
|
|
for (const flags of values) {
|
2022-11-09 18:59:32 +00:00
|
|
|
const out = check({
|
2021-07-09 19:36:10 +00:00
|
|
|
flags,
|
|
|
|
body: 'should be deleted',
|
|
|
|
attachments: [UNPROCESSED_ATTACHMENT],
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.isUndefined(out.body);
|
|
|
|
assert.deepStrictEqual(out.attachments, []);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
it('processes trivial fields', () => {
|
|
|
|
assert.strictEqual(check({ flags: null }).flags, 0);
|
|
|
|
assert.strictEqual(check({ flags: 1 }).flags, 1);
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
assert.strictEqual(check({ expireTimer: null }).expireTimer, 0);
|
|
|
|
assert.strictEqual(check({ expireTimer: 123 }).expireTimer, 123);
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2022-11-09 18:59:32 +00:00
|
|
|
assert.isFalse(check({ isViewOnce: null }).isViewOnce);
|
|
|
|
assert.isFalse(check({ isViewOnce: false }).isViewOnce);
|
|
|
|
assert.isTrue(check({ isViewOnce: true }).isViewOnce);
|
2021-07-09 19:36:10 +00:00
|
|
|
});
|
|
|
|
});
|