session-desktop/test/backup_test.js

743 lines
20 KiB
JavaScript
Raw Normal View History

2018-04-20 21:11:56 +02:00
/* global Signal: false */
2018-04-20 23:55:33 +02:00
/* global Whisper: false */
2018-04-20 21:11:56 +02:00
/* global assert: false */
2018-04-20 23:55:33 +02:00
/* global textsecure: false */
/* global _: false */
2018-04-20 21:11:56 +02:00
'use strict';
2018-04-20 21:11:56 +02:00
describe('Backup', () => {
describe('_sanitizeFileName', () => {
it('leaves a basic string alone', () => {
2018-04-27 23:25:04 +02:00
const initial = "Hello, how are you #5 ('fine' + great).jpg";
2018-04-20 21:11:56 +02:00
const expected = initial;
assert.strictEqual(Signal.Backup._sanitizeFileName(initial), expected);
});
2018-04-20 21:11:56 +02:00
it('replaces all unknown characters', () => {
const initial = '!@$%^&*=';
const expected = '________';
assert.strictEqual(Signal.Backup._sanitizeFileName(initial), expected);
});
});
2018-04-20 21:11:56 +02:00
describe('_trimFileName', () => {
it('handles a file with no extension', () => {
const initial = '0123456789012345678901234567890123456789';
const expected = '012345678901234567890123456789';
assert.strictEqual(Signal.Backup._trimFileName(initial), expected);
});
2018-04-20 21:11:56 +02:00
it('handles a file with a long extension', () => {
2018-04-27 23:25:04 +02:00
const initial =
'0123456789012345678901234567890123456789.01234567890123456789';
2018-04-20 21:11:56 +02:00
const expected = '012345678901234567890123456789';
assert.strictEqual(Signal.Backup._trimFileName(initial), expected);
});
2018-04-20 21:11:56 +02:00
it('handles a file with a normal extension', () => {
const initial = '01234567890123456789012345678901234567890123456789.jpg';
const expected = '012345678901234567890123.jpg';
assert.strictEqual(Signal.Backup._trimFileName(initial), expected);
});
});
2018-04-20 21:11:56 +02:00
describe('_getExportAttachmentFileName', () => {
it('uses original filename if attachment has one', () => {
const message = {
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 0;
const attachment = {
fileName: 'blah.jpg',
};
2018-04-20 21:11:56 +02:00
const expected = 'blah.jpg';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getExportAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
2018-04-20 21:11:56 +02:00
it('uses attachment id if no filename', () => {
const message = {
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 0;
const attachment = {
id: '123',
};
2018-04-20 21:11:56 +02:00
const expected = '123';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getExportAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
2018-04-20 21:11:56 +02:00
it('uses filename and contentType if available', () => {
const message = {
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 0;
const attachment = {
id: '123',
2018-04-20 21:11:56 +02:00
contentType: 'image/jpeg',
};
2018-04-20 21:11:56 +02:00
const expected = '123.jpeg';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getExportAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
2018-04-20 21:11:56 +02:00
it('handles strange contentType', () => {
const message = {
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 0;
const attachment = {
id: '123',
2018-04-20 21:11:56 +02:00
contentType: 'something',
};
2018-04-20 21:11:56 +02:00
const expected = '123.something';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getExportAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
});
2018-04-20 21:11:56 +02:00
describe('_getAnonymousAttachmentFileName', () => {
it('uses message id', () => {
const message = {
id: 'id-45',
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 0;
const attachment = {
fileName: 'blah.jpg',
};
2018-04-20 21:11:56 +02:00
const expected = 'id-45';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getAnonymousAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
2018-04-20 21:11:56 +02:00
it('appends index if it is above zero', () => {
const message = {
id: 'id-45',
body: 'something',
};
2018-04-20 21:11:56 +02:00
const index = 1;
const attachment = {
fileName: 'blah.jpg',
};
2018-04-20 21:11:56 +02:00
const expected = 'id-45-1';
2018-04-20 21:11:56 +02:00
const actual = Signal.Backup._getAnonymousAttachmentFileName(
message,
index,
attachment
);
assert.strictEqual(actual, expected);
});
});
2018-04-20 21:11:56 +02:00
describe('_getConversationDirName', () => {
it('uses name if available', () => {
const conversation = {
active_at: 123,
name: '0123456789012345678901234567890123456789',
2018-04-20 21:11:56 +02:00
id: 'id',
};
2018-04-20 21:11:56 +02:00
const expected = '123 (012345678901234567890123456789 id)';
2018-04-27 23:25:04 +02:00
assert.strictEqual(
Signal.Backup._getConversationDirName(conversation),
expected
);
});
2018-04-20 21:11:56 +02:00
it('uses just id if name is not available', () => {
const conversation = {
active_at: 123,
2018-04-20 21:11:56 +02:00
id: 'id',
};
2018-04-20 21:11:56 +02:00
const expected = '123 (id)';
2018-04-27 23:25:04 +02:00
assert.strictEqual(
Signal.Backup._getConversationDirName(conversation),
expected
);
});
2018-04-20 21:11:56 +02:00
it('uses inactive for missing active_at', () => {
const conversation = {
name: 'name',
2018-04-20 21:11:56 +02:00
id: 'id',
};
2018-04-20 21:11:56 +02:00
const expected = 'inactive (name id)';
2018-04-27 23:25:04 +02:00
assert.strictEqual(
Signal.Backup._getConversationDirName(conversation),
expected
);
});
});
2018-04-20 21:11:56 +02:00
describe('_getConversationLoggingName', () => {
it('uses plain id if conversation is private', () => {
const conversation = {
active_at: 123,
id: 'id',
2018-04-20 21:11:56 +02:00
type: 'private',
};
2018-04-20 21:11:56 +02:00
const expected = '123 (id)';
assert.strictEqual(
Signal.Backup._getConversationLoggingName(conversation),
expected
);
});
2018-04-20 21:11:56 +02:00
it('uses just id if name is not available', () => {
const conversation = {
active_at: 123,
id: 'groupId',
2018-04-20 21:11:56 +02:00
type: 'group',
};
2018-04-20 21:11:56 +02:00
const expected = '123 ([REDACTED_GROUP]pId)';
assert.strictEqual(
Signal.Backup._getConversationLoggingName(conversation),
expected
);
});
2018-04-20 21:11:56 +02:00
it('uses inactive for missing active_at', () => {
const conversation = {
id: 'id',
2018-04-20 21:11:56 +02:00
type: 'private',
};
2018-04-20 21:11:56 +02:00
const expected = 'inactive (id)';
assert.strictEqual(
Signal.Backup._getConversationLoggingName(conversation),
expected
);
});
});
2018-04-20 23:55:33 +02:00
describe('end-to-end', () => {
it('exports then imports to produce the same data we started with', async () => {
2018-04-27 23:25:04 +02:00
const { attachmentsPath, fse, glob, path, tmp } = window.test;
2018-04-20 23:55:33 +02:00
const {
upgradeMessageSchema,
loadAttachmentData,
} = window.Signal.Migrations;
const key = new Uint8Array([
2018-04-27 23:25:04 +02:00
1,
3,
4,
5,
6,
7,
8,
11,
23,
34,
1,
34,
3,
5,
45,
45,
1,
3,
4,
5,
6,
7,
8,
11,
23,
34,
1,
34,
3,
5,
45,
45,
2018-04-20 23:55:33 +02:00
]);
const attachmentsPattern = path.join(attachmentsPath, '**');
const OUR_NUMBER = '+12025550000';
const CONTACT_ONE_NUMBER = '+12025550001';
const CONTACT_TWO_NUMBER = '+12025550002';
2018-04-20 23:55:33 +02:00
async function wrappedLoadAttachment(attachment) {
return _.omit(await loadAttachmentData(attachment), ['path']);
}
async function clearAllData() {
await textsecure.storage.protocol.removeAllData();
await fse.emptyDir(attachmentsPath);
}
function removeId(model) {
return _.omit(model, ['id']);
}
const getUndefinedKeys = object =>
Object.entries(object)
.filter(([, value]) => value === undefined)
.map(([name]) => name);
const omitUndefinedKeys = object =>
_.omit(object, getUndefinedKeys(object));
// We want to know which paths have two slashes, since that tells us which files
// in the attachment fan-out are files vs. directories.
const TWO_SLASHES = /[^/]*\/[^/]*\/[^/]*/;
// On windows, attachmentsPath has a normal windows path format (\ separators), but
// glob returns only /. We normalize to / separators for our manipulations.
const normalizedBase = attachmentsPath.replace(/\\/g, '/');
2018-04-20 23:55:33 +02:00
function removeDirs(dirs) {
2018-04-27 23:25:04 +02:00
return _.filter(dirs, fullDir => {
const dir = fullDir.replace(normalizedBase, '');
return TWO_SLASHES.test(dir);
2018-04-20 23:55:33 +02:00
});
}
function _mapQuotedAttachments(mapper) {
return async (message, context) => {
if (!message.quote) {
return message;
}
2018-04-27 23:25:04 +02:00
const wrappedMapper = async attachment => {
2018-04-20 23:55:33 +02:00
if (!attachment || !attachment.thumbnail) {
return attachment;
}
return Object.assign({}, attachment, {
thumbnail: await mapper(attachment.thumbnail, context),
});
};
2018-04-27 23:25:04 +02:00
const quotedAttachments =
(message.quote && message.quote.attachments) || [];
2018-04-20 23:55:33 +02:00
return Object.assign({}, message, {
quote: Object.assign({}, message.quote, {
2018-04-27 23:25:04 +02:00
attachments: await Promise.all(
quotedAttachments.map(wrappedMapper)
),
2018-04-20 23:55:33 +02:00
}),
});
};
}
async function loadAllFilesFromDisk(message) {
2018-04-27 23:25:04 +02:00
const loadThumbnails = _mapQuotedAttachments(thumbnail => {
2018-04-20 23:55:33 +02:00
// we want to be bulletproof to thumbnails without data
if (!thumbnail.path) {
return thumbnail;
}
return wrappedLoadAttachment(thumbnail);
});
2018-04-27 23:25:04 +02:00
return Object.assign({}, await loadThumbnails(message), {
contact: await Promise.all(
(message.contact || []).map(async contact => {
return contact && contact.avatar && contact.avatar.avatar
? Object.assign({}, contact, {
avatar: Object.assign({}, contact.avatar, {
avatar: await wrappedLoadAttachment(
contact.avatar.avatar
),
}),
})
: contact;
})
),
attachments: await Promise.all(
(message.attachments || []).map(attachment =>
wrappedLoadAttachment(attachment)
)
),
2018-04-27 23:25:04 +02:00
});
2018-04-20 23:55:33 +02:00
}
let backupDir;
try {
const ATTACHMENT_COUNT = 3;
2018-04-20 23:55:33 +02:00
const MESSAGE_COUNT = 1;
const CONVERSATION_COUNT = 1;
const messageWithAttachments = {
conversationId: CONTACT_ONE_NUMBER,
body: 'Totally!',
source: OUR_NUMBER,
received_at: 1524185933350,
timestamp: 1524185933350,
errors: [],
2018-04-27 23:25:04 +02:00
attachments: [
{
contentType: 'image/gif',
fileName: 'sad_cat.gif',
data: new Uint8Array([
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
]).buffer,
},
],
2018-04-25 00:44:57 +02:00
hasAttachments: 1,
hasFileAttachments: undefined,
2018-04-25 00:44:57 +02:00
hasVisualMediaAttachments: 1,
2018-04-20 23:55:33 +02:00
quote: {
text: "Isn't it cute?",
author: CONTACT_ONE_NUMBER,
id: 12345678,
2018-04-27 23:25:04 +02:00
attachments: [
{
contentType: 'audio/mp3',
fileName: 'song.mp3',
2018-04-20 23:55:33 +02:00
},
2018-04-27 23:25:04 +02:00
{
contentType: 'image/gif',
fileName: 'happy_cat.gif',
thumbnail: {
contentType: 'image/png',
data: new Uint8Array([
2,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
]).buffer,
},
},
],
2018-04-20 23:55:33 +02:00
},
contact: [
{
name: {
displayName: 'Someone Somewhere',
},
number: [
{
value: CONTACT_TWO_NUMBER,
type: 1,
},
],
avatar: {
isProfile: false,
avatar: {
contentType: 'image/png',
data: new Uint8Array([
3,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
]).buffer,
},
},
},
],
2018-04-20 23:55:33 +02:00
};
console.log('Backup test: Clear all data');
await clearAllData();
console.log('Backup test: Create models, save to db/disk');
const message = await upgradeMessageSchema(messageWithAttachments);
console.log({ message });
const messageModel = new Whisper.Message(message);
await window.wrapDeferred(messageModel.save());
const conversation = {
active_at: 1524185933350,
color: 'orange',
expireTimer: 0,
id: CONTACT_ONE_NUMBER,
lastMessage: 'Heyo!',
name: 'Someone Somewhere',
profileAvatar: {
contentType: 'image/jpeg',
data: new Uint8Array([
4,
2018-04-27 23:25:04 +02:00
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
2018-04-20 23:55:33 +02:00
]).buffer,
size: 64,
},
profileKey: new Uint8Array([
5,
2018-04-27 23:25:04 +02:00
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
8,
2018-04-20 23:55:33 +02:00
]).buffer,
profileName: 'Someone! 🤔',
profileSharing: true,
timestamp: 1524185933350,
tokens: [
'someone somewhere',
'someone',
'somewhere',
'2025550001',
'12025550001',
],
type: 'private',
unreadCount: 0,
verified: 0,
};
console.log({ conversation });
const conversationModel = new Whisper.Conversation(conversation);
await window.wrapDeferred(conversationModel.save());
2018-04-27 23:25:04 +02:00
console.log(
'Backup test: Ensure that all attachments were saved to disk'
);
2018-04-20 23:55:33 +02:00
const attachmentFiles = removeDirs(glob.sync(attachmentsPattern));
console.log({ attachmentFiles });
assert.strictEqual(ATTACHMENT_COUNT, attachmentFiles.length);
console.log('Backup test: Export!');
backupDir = tmp.dirSync().name;
console.log({ backupDir });
await Signal.Backup.exportToDirectory(backupDir, { key });
console.log('Backup test: Ensure that messages.zip exists');
const zipPath = path.join(backupDir, 'messages.zip');
const messageZipExists = fse.existsSync(zipPath);
2018-04-20 23:55:33 +02:00
assert.strictEqual(true, messageZipExists);
2018-04-27 23:25:04 +02:00
console.log(
'Backup test: Ensure that all attachments made it to backup dir'
);
2018-04-20 23:55:33 +02:00
const backupAttachmentPattern = path.join(backupDir, 'attachments/*');
const backupAttachments = glob.sync(backupAttachmentPattern);
console.log({ backupAttachments });
assert.strictEqual(ATTACHMENT_COUNT, backupAttachments.length);
console.log('Backup test: Clear all data');
await clearAllData();
console.log('Backup test: Import!');
await Signal.Backup.importFromDirectory(backupDir, { key });
console.log('Backup test: ensure that all attachments were imported');
2018-04-27 23:25:04 +02:00
const recreatedAttachmentFiles = removeDirs(
glob.sync(attachmentsPattern)
);
2018-04-20 23:55:33 +02:00
console.log({ recreatedAttachmentFiles });
assert.strictEqual(ATTACHMENT_COUNT, recreatedAttachmentFiles.length);
assert.deepEqual(attachmentFiles, recreatedAttachmentFiles);
console.log('Backup test: Check messages');
const messageCollection = new Whisper.MessageCollection();
await window.wrapDeferred(messageCollection.fetch());
assert.strictEqual(messageCollection.length, MESSAGE_COUNT);
const messageFromDB = removeId(messageCollection.at(0).attributes);
const expectedMessage = omitUndefinedKeys(message);
console.log({ messageFromDB, expectedMessage });
2018-04-27 23:25:04 +02:00
assert.deepEqual(messageFromDB, expectedMessage);
2018-04-20 23:55:33 +02:00
2018-04-27 23:25:04 +02:00
console.log(
'Backup test: Check that all attachments were successfully imported'
);
const messageWithAttachmentsFromDB = await loadAllFilesFromDisk(
messageFromDB
);
const expectedMessageWithAttachments = omitUndefinedKeys(
messageWithAttachments
);
console.log({
messageWithAttachmentsFromDB,
expectedMessageWithAttachments,
});
2018-04-20 23:55:33 +02:00
assert.deepEqual(
_.omit(messageWithAttachmentsFromDB, ['schemaVersion']),
expectedMessageWithAttachments
2018-04-20 23:55:33 +02:00
);
console.log('Backup test: Check conversations');
2018-04-20 23:55:33 +02:00
const conversationCollection = new Whisper.ConversationCollection();
await window.wrapDeferred(conversationCollection.fetch());
assert.strictEqual(conversationCollection.length, CONVERSATION_COUNT);
const conversationFromDB = conversationCollection.at(0).attributes;
console.log({ conversationFromDB, conversation });
assert.deepEqual(
conversationFromDB,
_.omit(conversation, ['profileAvatar'])
);
console.log('Backup test: Clear all data');
await clearAllData();
console.log('Backup test: Complete!');
} finally {
if (backupDir) {
console.log({ backupDir });
console.log('Deleting', backupDir);
await fse.remove(backupDir);
}
}
});
});
});