2024-05-22 16:34:19 +00:00
|
|
|
// Copyright 2024 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import path from 'path';
|
|
|
|
import { tmpdir } from 'os';
|
2024-05-29 17:19:33 +00:00
|
|
|
import { sortBy } from 'lodash';
|
2024-05-22 16:34:19 +00:00
|
|
|
import { createReadStream } from 'fs';
|
|
|
|
import { mkdtemp, rm } from 'fs/promises';
|
2024-05-29 23:46:43 +00:00
|
|
|
import * as sinon from 'sinon';
|
2024-05-30 18:53:30 +00:00
|
|
|
import { BackupLevel } from '@signalapp/libsignal-client/zkgroup';
|
2024-05-22 16:34:19 +00:00
|
|
|
|
|
|
|
import type { MessageAttributesType } from '../../model-types';
|
2024-05-29 17:19:33 +00:00
|
|
|
import type {
|
|
|
|
SendStateByConversationId,
|
|
|
|
SendState,
|
|
|
|
} from '../../messages/MessageSendState';
|
2024-05-22 16:34:19 +00:00
|
|
|
|
|
|
|
import { backupsService } from '../../services/backups';
|
2024-05-29 17:19:33 +00:00
|
|
|
import { isUnsupportedMessage } from '../../state/selectors/message';
|
2024-05-22 16:34:19 +00:00
|
|
|
import { generateAci, generatePni } from '../../types/ServiceId';
|
|
|
|
import Data from '../../sql/Client';
|
|
|
|
import { getRandomBytes } from '../../Crypto';
|
|
|
|
import * as Bytes from '../../Bytes';
|
|
|
|
|
|
|
|
export const OUR_ACI = generateAci();
|
|
|
|
export const OUR_PNI = generatePni();
|
|
|
|
export const MASTER_KEY = Bytes.toBase64(getRandomBytes(32));
|
|
|
|
export const PROFILE_KEY = getRandomBytes(32);
|
|
|
|
|
|
|
|
// This is preserved across data erasure
|
|
|
|
const CONVO_ID_TO_STABLE_ID = new Map<string, string>();
|
|
|
|
|
|
|
|
function mapConvoId(id?: string | null): string | undefined | null {
|
|
|
|
if (id == null) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CONVO_ID_TO_STABLE_ID.get(id) ?? id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to eliminate fields that won't stay stable through import/export
|
|
|
|
function sortAndNormalize(
|
|
|
|
messages: Array<MessageAttributesType>
|
|
|
|
): Array<unknown> {
|
|
|
|
return sortBy(messages, 'sent_at').map(message => {
|
2024-05-29 17:19:33 +00:00
|
|
|
const {
|
|
|
|
changedId,
|
|
|
|
conversationId,
|
|
|
|
editHistory,
|
|
|
|
key_changed: keyChanged,
|
|
|
|
reactions,
|
|
|
|
sendStateByConversationId,
|
|
|
|
verifiedChanged,
|
|
|
|
|
|
|
|
// This is not in the backup
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
id: _id,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
received_at: _receivedAt,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
sourceDevice: _sourceDevice,
|
2024-06-03 17:02:25 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
editMessageReceivedAt: _editMessageReceivedAt,
|
2024-05-29 17:19:33 +00:00
|
|
|
|
|
|
|
...rest
|
|
|
|
} = message;
|
|
|
|
|
|
|
|
function mapSendState(
|
|
|
|
sendState?: SendStateByConversationId
|
|
|
|
): SendStateByConversationId | undefined {
|
|
|
|
if (sendState == null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result: Record<string, SendState> = {};
|
|
|
|
for (const [id, state] of Object.entries(sendState)) {
|
|
|
|
result[mapConvoId(id) ?? id] = state;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2024-05-22 16:34:19 +00:00
|
|
|
|
|
|
|
return {
|
2024-05-29 17:19:33 +00:00
|
|
|
...rest,
|
|
|
|
conversationId: mapConvoId(conversationId),
|
|
|
|
reactions: reactions?.map(({ fromId, ...restOfReaction }) => {
|
2024-05-22 16:34:19 +00:00
|
|
|
return {
|
|
|
|
from: mapConvoId(fromId),
|
2024-05-29 17:19:33 +00:00
|
|
|
...restOfReaction,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
changedId: mapConvoId(changedId),
|
|
|
|
key_changed: mapConvoId(keyChanged),
|
|
|
|
verifiedChanged: mapConvoId(verifiedChanged),
|
|
|
|
sendStateByConverationId: mapSendState(sendStateByConversationId),
|
|
|
|
editHistory: editHistory?.map(history => {
|
|
|
|
const {
|
|
|
|
sendStateByConversationId: historySendState,
|
2024-06-03 17:02:25 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
received_at: _receivedAtHistory,
|
2024-05-29 17:19:33 +00:00
|
|
|
...restOfHistory
|
|
|
|
} = history;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...restOfHistory,
|
|
|
|
sendStateByConversationId: mapSendState(historySendState),
|
2024-05-22 16:34:19 +00:00
|
|
|
};
|
|
|
|
}),
|
2024-05-29 17:19:33 +00:00
|
|
|
|
|
|
|
// Not an original property, but useful
|
|
|
|
isUnsupported: isUnsupportedMessage(message),
|
2024-05-22 16:34:19 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function symmetricRoundtripHarness(
|
2024-05-30 18:53:30 +00:00
|
|
|
messages: Array<MessageAttributesType>,
|
|
|
|
backupLevel: BackupLevel = BackupLevel.Messages
|
2024-05-22 16:34:19 +00:00
|
|
|
): Promise<void> {
|
2024-05-30 18:53:30 +00:00
|
|
|
return asymmetricRoundtripHarness(messages, messages, backupLevel);
|
2024-05-22 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateConvoIdToTitle() {
|
|
|
|
const all = await Data.getAllConversations();
|
|
|
|
for (const convo of all) {
|
|
|
|
CONVO_ID_TO_STABLE_ID.set(
|
|
|
|
convo.id,
|
2024-05-29 17:19:33 +00:00
|
|
|
convo.serviceId ?? convo.e164 ?? convo.masterKey ?? convo.id
|
2024-05-22 16:34:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function asymmetricRoundtripHarness(
|
|
|
|
before: Array<MessageAttributesType>,
|
2024-05-30 18:53:30 +00:00
|
|
|
after: Array<MessageAttributesType>,
|
|
|
|
backupLevel: BackupLevel = BackupLevel.Messages
|
2024-05-22 16:34:19 +00:00
|
|
|
): Promise<void> {
|
|
|
|
const outDir = await mkdtemp(path.join(tmpdir(), 'signal-temp-'));
|
2024-05-29 23:46:43 +00:00
|
|
|
const fetchAndSaveBackupCdnObjectMetadata = sinon.stub(
|
|
|
|
backupsService,
|
|
|
|
'fetchAndSaveBackupCdnObjectMetadata'
|
|
|
|
);
|
2024-05-22 16:34:19 +00:00
|
|
|
try {
|
|
|
|
const targetOutputFile = path.join(outDir, 'backup.bin');
|
|
|
|
|
|
|
|
await Data.saveMessages(before, { forceSave: true, ourAci: OUR_ACI });
|
|
|
|
|
2024-05-30 18:53:30 +00:00
|
|
|
await backupsService.exportToDisk(targetOutputFile, backupLevel);
|
2024-05-22 16:34:19 +00:00
|
|
|
|
|
|
|
await updateConvoIdToTitle();
|
|
|
|
|
|
|
|
await clearData();
|
|
|
|
|
|
|
|
await backupsService.importBackup(() => createReadStream(targetOutputFile));
|
|
|
|
|
|
|
|
const messagesFromDatabase = await Data._getAllMessages();
|
|
|
|
|
|
|
|
await updateConvoIdToTitle();
|
|
|
|
|
|
|
|
const expected = sortAndNormalize(after);
|
|
|
|
const actual = sortAndNormalize(messagesFromDatabase);
|
2024-06-03 17:02:25 +00:00
|
|
|
assert.deepEqual(actual, expected);
|
2024-05-22 16:34:19 +00:00
|
|
|
} finally {
|
2024-05-29 23:46:43 +00:00
|
|
|
fetchAndSaveBackupCdnObjectMetadata.restore();
|
2024-05-22 16:34:19 +00:00
|
|
|
await rm(outDir, { recursive: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function clearData() {
|
|
|
|
await Data._removeAllMessages();
|
|
|
|
await Data._removeAllConversations();
|
|
|
|
await Data.removeAllItems();
|
|
|
|
window.storage.reset();
|
|
|
|
window.ConversationController.reset();
|
|
|
|
|
|
|
|
await setupBasics();
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setupBasics(): Promise<void> {
|
|
|
|
await window.storage.put('uuid_id', `${OUR_ACI}.2`);
|
|
|
|
await window.storage.put('pni', OUR_PNI);
|
|
|
|
await window.storage.put('masterKey', MASTER_KEY);
|
|
|
|
await window.storage.put('profileKey', PROFILE_KEY);
|
|
|
|
|
|
|
|
await window.ConversationController.getOrCreateAndWait(OUR_ACI, 'private', {
|
|
|
|
pni: OUR_PNI,
|
|
|
|
systemGivenName: 'ME',
|
|
|
|
profileKey: Bytes.toBase64(PROFILE_KEY),
|
|
|
|
});
|
|
|
|
|
|
|
|
window.Events = {
|
|
|
|
...window.Events,
|
|
|
|
getTypingIndicatorSetting: () => false,
|
|
|
|
getLinkPreviewSetting: () => false,
|
|
|
|
};
|
|
|
|
}
|