2024-05-15 14:55:20 +00:00
|
|
|
// Copyright 2024 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import Long from 'long';
|
|
|
|
import { join } from 'path';
|
|
|
|
import * as sinon from 'sinon';
|
|
|
|
import { BackupLevel } from '@signalapp/libsignal-client/zkgroup';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataWriter } from '../../sql/Client';
|
2024-05-15 14:55:20 +00:00
|
|
|
import { Backups } from '../../protobuf';
|
|
|
|
import {
|
2024-05-29 23:46:43 +00:00
|
|
|
getFilePointerForAttachment,
|
2024-05-15 14:55:20 +00:00
|
|
|
convertFilePointerToAttachment,
|
2024-05-29 23:46:43 +00:00
|
|
|
maybeGetBackupJobForAttachmentAndFilePointer,
|
2024-05-15 14:55:20 +00:00
|
|
|
} from '../../services/backups/util/filePointers';
|
|
|
|
import { APPLICATION_OCTET_STREAM, IMAGE_PNG } from '../../types/MIME';
|
|
|
|
import * as Bytes from '../../Bytes';
|
|
|
|
import type { AttachmentType } from '../../types/Attachment';
|
|
|
|
import { strictAssert } from '../../util/assert';
|
2024-05-29 23:46:43 +00:00
|
|
|
import type { GetBackupCdnInfoType } from '../../services/backups/util/mediaId';
|
2024-06-11 21:22:54 +00:00
|
|
|
import { MASTER_KEY } from './helpers';
|
2024-07-16 20:39:56 +00:00
|
|
|
import { getRandomBytes } from '../../Crypto';
|
2024-05-15 14:55:20 +00:00
|
|
|
|
|
|
|
describe('convertFilePointerToAttachment', () => {
|
|
|
|
it('processes filepointer with attachmentLocator', () => {
|
|
|
|
const result = convertFilePointerToAttachment(
|
|
|
|
new Backups.FilePointer({
|
|
|
|
contentType: 'image/png',
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
blurHash: 'blurhash',
|
|
|
|
fileName: 'filename',
|
|
|
|
caption: 'caption',
|
|
|
|
incrementalMac: Bytes.fromString('incrementalMac'),
|
|
|
|
incrementalMacChunkSize: 1000,
|
|
|
|
attachmentLocator: new Backups.FilePointer.AttachmentLocator({
|
|
|
|
size: 128,
|
|
|
|
cdnKey: 'cdnKey',
|
|
|
|
cdnNumber: 2,
|
|
|
|
key: Bytes.fromString('key'),
|
|
|
|
digest: Bytes.fromString('digest'),
|
|
|
|
uploadTimestamp: Long.fromNumber(1970),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepStrictEqual(result, {
|
|
|
|
contentType: IMAGE_PNG,
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
size: 128,
|
|
|
|
blurHash: 'blurhash',
|
|
|
|
fileName: 'filename',
|
|
|
|
caption: 'caption',
|
|
|
|
cdnKey: 'cdnKey',
|
|
|
|
cdnNumber: 2,
|
|
|
|
key: Bytes.toBase64(Bytes.fromString('key')),
|
|
|
|
digest: Bytes.toBase64(Bytes.fromString('digest')),
|
|
|
|
uploadTimestamp: 1970,
|
|
|
|
incrementalMac: Bytes.toBase64(Bytes.fromString('incrementalMac')),
|
|
|
|
incrementalMacChunkSize: 1000,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('processes filepointer with backupLocator and missing fields', () => {
|
|
|
|
const result = convertFilePointerToAttachment(
|
|
|
|
new Backups.FilePointer({
|
|
|
|
contentType: 'image/png',
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
blurHash: 'blurhash',
|
|
|
|
fileName: 'filename',
|
|
|
|
caption: 'caption',
|
|
|
|
incrementalMac: Bytes.fromString('incrementalMac'),
|
|
|
|
incrementalMacChunkSize: 1000,
|
|
|
|
backupLocator: new Backups.FilePointer.BackupLocator({
|
|
|
|
mediaName: 'mediaName',
|
|
|
|
cdnNumber: 3,
|
2024-08-20 00:47:02 +00:00
|
|
|
size: 128,
|
2024-05-15 14:55:20 +00:00
|
|
|
key: Bytes.fromString('key'),
|
|
|
|
digest: Bytes.fromString('digest'),
|
|
|
|
transitCdnKey: 'transitCdnKey',
|
|
|
|
transitCdnNumber: 2,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepStrictEqual(result, {
|
|
|
|
contentType: IMAGE_PNG,
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
size: 128,
|
|
|
|
blurHash: 'blurhash',
|
|
|
|
fileName: 'filename',
|
|
|
|
caption: 'caption',
|
|
|
|
cdnKey: 'transitCdnKey',
|
|
|
|
cdnNumber: 2,
|
|
|
|
key: Bytes.toBase64(Bytes.fromString('key')),
|
|
|
|
digest: Bytes.toBase64(Bytes.fromString('digest')),
|
|
|
|
incrementalMac: Bytes.toBase64(Bytes.fromString('incrementalMac')),
|
|
|
|
incrementalMacChunkSize: 1000,
|
|
|
|
backupLocator: {
|
|
|
|
mediaName: 'mediaName',
|
|
|
|
cdnNumber: 3,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('processes filepointer with invalidAttachmentLocator', () => {
|
|
|
|
const result = convertFilePointerToAttachment(
|
|
|
|
new Backups.FilePointer({
|
|
|
|
contentType: 'image/png',
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
blurHash: 'blurhash',
|
|
|
|
fileName: 'filename',
|
|
|
|
caption: 'caption',
|
|
|
|
incrementalMac: Bytes.fromString('incrementalMac'),
|
|
|
|
incrementalMacChunkSize: 1000,
|
|
|
|
invalidAttachmentLocator:
|
|
|
|
new Backups.FilePointer.InvalidAttachmentLocator(),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepStrictEqual(result, {
|
|
|
|
contentType: IMAGE_PNG,
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
blurHash: 'blurhash',
|
|
|
|
fileName: 'filename',
|
|
|
|
caption: 'caption',
|
|
|
|
incrementalMac: Bytes.toBase64(Bytes.fromString('incrementalMac')),
|
|
|
|
incrementalMacChunkSize: 1000,
|
|
|
|
size: 0,
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('accepts missing / null fields and adds defaults to contentType and size', () => {
|
|
|
|
const result = convertFilePointerToAttachment(
|
|
|
|
new Backups.FilePointer({
|
|
|
|
backupLocator: new Backups.FilePointer.BackupLocator(),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepStrictEqual(result, {
|
|
|
|
contentType: APPLICATION_OCTET_STREAM,
|
|
|
|
size: 0,
|
|
|
|
width: undefined,
|
|
|
|
height: undefined,
|
|
|
|
blurHash: undefined,
|
|
|
|
fileName: undefined,
|
|
|
|
caption: undefined,
|
|
|
|
cdnKey: undefined,
|
|
|
|
cdnNumber: undefined,
|
|
|
|
key: undefined,
|
|
|
|
digest: undefined,
|
|
|
|
incrementalMac: undefined,
|
|
|
|
incrementalMacChunkSize: undefined,
|
|
|
|
backupLocator: undefined,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-08-01 06:13:38 +00:00
|
|
|
const defaultDigest = Bytes.fromBase64('digest');
|
|
|
|
const defaultMediaName = Bytes.toHex(defaultDigest);
|
|
|
|
|
2024-05-15 14:55:20 +00:00
|
|
|
function composeAttachment(
|
|
|
|
overrides: Partial<AttachmentType> = {}
|
|
|
|
): AttachmentType {
|
|
|
|
return {
|
|
|
|
size: 100,
|
|
|
|
contentType: IMAGE_PNG,
|
|
|
|
cdnKey: 'cdnKey',
|
|
|
|
cdnNumber: 2,
|
|
|
|
path: 'path/to/file.png',
|
|
|
|
key: 'key',
|
2024-08-01 06:13:38 +00:00
|
|
|
digest: Bytes.toBase64(defaultDigest),
|
2024-05-29 23:46:43 +00:00
|
|
|
iv: 'iv',
|
2024-05-15 14:55:20 +00:00
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
blurHash: 'blurhash',
|
|
|
|
fileName: 'filename',
|
|
|
|
caption: 'caption',
|
|
|
|
incrementalMac: 'incrementalMac',
|
|
|
|
incrementalMacChunkSize: 1000,
|
|
|
|
uploadTimestamp: 1234,
|
2024-07-16 20:39:56 +00:00
|
|
|
localKey: Bytes.toBase64(getRandomBytes(32)),
|
|
|
|
version: 2,
|
2024-05-15 14:55:20 +00:00
|
|
|
...overrides,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultFilePointer = new Backups.FilePointer({
|
|
|
|
contentType: IMAGE_PNG,
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
blurHash: 'blurhash',
|
|
|
|
fileName: 'filename',
|
|
|
|
caption: 'caption',
|
|
|
|
incrementalMac: Bytes.fromBase64('incrementalMac'),
|
|
|
|
incrementalMacChunkSize: 1000,
|
|
|
|
});
|
|
|
|
|
|
|
|
const defaultAttachmentLocator = new Backups.FilePointer.AttachmentLocator({
|
|
|
|
cdnKey: 'cdnKey',
|
|
|
|
cdnNumber: 2,
|
|
|
|
key: Bytes.fromBase64('key'),
|
2024-08-01 06:13:38 +00:00
|
|
|
digest: defaultDigest,
|
2024-05-15 14:55:20 +00:00
|
|
|
size: 100,
|
|
|
|
uploadTimestamp: Long.fromNumber(1234),
|
|
|
|
});
|
|
|
|
|
|
|
|
const defaultBackupLocator = new Backups.FilePointer.BackupLocator({
|
|
|
|
mediaName: defaultMediaName,
|
|
|
|
cdnNumber: null,
|
|
|
|
key: Bytes.fromBase64('key'),
|
2024-08-01 06:13:38 +00:00
|
|
|
digest: defaultDigest,
|
2024-08-20 00:47:02 +00:00
|
|
|
size: 100,
|
2024-05-15 14:55:20 +00:00
|
|
|
transitCdnKey: 'cdnKey',
|
|
|
|
transitCdnNumber: 2,
|
|
|
|
});
|
|
|
|
|
|
|
|
const filePointerWithAttachmentLocator = new Backups.FilePointer({
|
|
|
|
...defaultFilePointer,
|
|
|
|
attachmentLocator: defaultAttachmentLocator,
|
|
|
|
});
|
|
|
|
|
|
|
|
const filePointerWithBackupLocator = new Backups.FilePointer({
|
|
|
|
...defaultFilePointer,
|
|
|
|
backupLocator: defaultBackupLocator,
|
|
|
|
});
|
|
|
|
const filePointerWithInvalidLocator = new Backups.FilePointer({
|
|
|
|
...defaultFilePointer,
|
|
|
|
invalidAttachmentLocator: new Backups.FilePointer.InvalidAttachmentLocator(),
|
|
|
|
});
|
|
|
|
|
|
|
|
async function testAttachmentToFilePointer(
|
|
|
|
attachment: AttachmentType,
|
|
|
|
filePointer: Backups.FilePointer,
|
2024-05-29 23:46:43 +00:00
|
|
|
options?: {
|
|
|
|
backupLevel?: BackupLevel;
|
|
|
|
backupCdnNumber?: number;
|
|
|
|
updatedAttachment?: AttachmentType;
|
|
|
|
}
|
2024-05-15 14:55:20 +00:00
|
|
|
) {
|
|
|
|
async function _doTest(withBackupLevel: BackupLevel) {
|
2024-05-29 23:46:43 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
await getFilePointerForAttachment({
|
2024-05-15 14:55:20 +00:00
|
|
|
attachment,
|
|
|
|
backupLevel: withBackupLevel,
|
2024-05-29 23:46:43 +00:00
|
|
|
getBackupCdnInfo: async _mediaId => {
|
2024-05-15 14:55:20 +00:00
|
|
|
if (options?.backupCdnNumber != null) {
|
|
|
|
return { isInBackupTier: true, cdnNumber: options.backupCdnNumber };
|
|
|
|
}
|
|
|
|
return { isInBackupTier: false };
|
|
|
|
},
|
|
|
|
}),
|
2024-05-29 23:46:43 +00:00
|
|
|
{
|
|
|
|
filePointer,
|
|
|
|
...(options?.updatedAttachment
|
|
|
|
? { updatedAttachment: options?.updatedAttachment }
|
|
|
|
: {}),
|
|
|
|
}
|
2024-05-15 14:55:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options?.backupLevel) {
|
|
|
|
await _doTest(BackupLevel.Messages);
|
|
|
|
await _doTest(BackupLevel.Media);
|
|
|
|
} else {
|
|
|
|
await _doTest(options.backupLevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
const notInBackupCdn: GetBackupCdnInfoType = async () => {
|
|
|
|
return { isInBackupTier: false };
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('getFilePointerForAttachment', () => {
|
2024-06-11 21:22:54 +00:00
|
|
|
let sandbox: sinon.SinonSandbox;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
sandbox = sinon.createSandbox();
|
|
|
|
sandbox.stub(window.storage, 'get').callsFake(key => {
|
|
|
|
if (key === 'masterKey') {
|
|
|
|
return MASTER_KEY;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
2024-05-15 14:55:20 +00:00
|
|
|
describe('not downloaded locally', () => {
|
|
|
|
const undownloadedAttachment = composeAttachment({ path: undefined });
|
|
|
|
it('returns invalidAttachmentLocator if missing critical decryption info', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{
|
|
|
|
...undownloadedAttachment,
|
|
|
|
key: undefined,
|
|
|
|
},
|
|
|
|
filePointerWithInvalidLocator
|
|
|
|
);
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{
|
|
|
|
...undownloadedAttachment,
|
|
|
|
digest: undefined,
|
|
|
|
},
|
|
|
|
filePointerWithInvalidLocator
|
|
|
|
);
|
|
|
|
});
|
|
|
|
describe('attachment does not have attachment.backupLocator', () => {
|
|
|
|
it('returns attachmentLocator, regardless of backupLevel or backup tier status', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
undownloadedAttachment,
|
|
|
|
filePointerWithAttachmentLocator,
|
|
|
|
{ backupCdnNumber: 3 }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns invalidAttachmentLocator if missing critical locator info', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{
|
|
|
|
...undownloadedAttachment,
|
|
|
|
cdnKey: undefined,
|
|
|
|
},
|
|
|
|
filePointerWithInvalidLocator
|
|
|
|
);
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{
|
|
|
|
...undownloadedAttachment,
|
|
|
|
cdnNumber: undefined,
|
|
|
|
},
|
|
|
|
filePointerWithInvalidLocator
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('attachment has attachment.backupLocator', () => {
|
|
|
|
const undownloadedAttachmentWithBackupLocator = {
|
|
|
|
...undownloadedAttachment,
|
|
|
|
backupLocator: { mediaName: defaultMediaName },
|
|
|
|
};
|
|
|
|
|
|
|
|
it('returns backupLocator if backupLevel is Media', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
undownloadedAttachmentWithBackupLocator,
|
|
|
|
filePointerWithBackupLocator,
|
|
|
|
{ backupLevel: BackupLevel.Media }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns backupLocator even if missing transit CDN info', async () => {
|
|
|
|
// Even if missing transit CDNKey
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{ ...undownloadedAttachmentWithBackupLocator, cdnKey: undefined },
|
|
|
|
new Backups.FilePointer({
|
|
|
|
...filePointerWithBackupLocator,
|
|
|
|
backupLocator: new Backups.FilePointer.BackupLocator({
|
|
|
|
...defaultBackupLocator,
|
|
|
|
transitCdnKey: undefined,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
{ backupLevel: BackupLevel.Media }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns attachmentLocator if backupLevel is Messages', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
undownloadedAttachmentWithBackupLocator,
|
|
|
|
filePointerWithAttachmentLocator,
|
|
|
|
{ backupLevel: BackupLevel.Messages }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('downloaded locally', () => {
|
|
|
|
const downloadedAttachment = composeAttachment();
|
|
|
|
describe('BackupLevel.Messages', () => {
|
|
|
|
it('returns attachmentLocator', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
downloadedAttachment,
|
|
|
|
filePointerWithAttachmentLocator,
|
|
|
|
{ backupLevel: BackupLevel.Messages }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('returns invalidAttachmentLocator if missing critical locator info', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{
|
|
|
|
...downloadedAttachment,
|
|
|
|
cdnKey: undefined,
|
|
|
|
},
|
|
|
|
filePointerWithInvalidLocator,
|
|
|
|
{ backupLevel: BackupLevel.Messages }
|
|
|
|
);
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{
|
|
|
|
...downloadedAttachment,
|
|
|
|
cdnNumber: undefined,
|
|
|
|
},
|
|
|
|
filePointerWithInvalidLocator,
|
|
|
|
{ backupLevel: BackupLevel.Messages }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('returns invalidAttachmentLocator if missing critical decryption info', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{
|
|
|
|
...downloadedAttachment,
|
|
|
|
key: undefined,
|
|
|
|
},
|
|
|
|
filePointerWithInvalidLocator,
|
|
|
|
{ backupLevel: BackupLevel.Messages }
|
|
|
|
);
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{
|
|
|
|
...downloadedAttachment,
|
|
|
|
digest: undefined,
|
|
|
|
},
|
|
|
|
filePointerWithInvalidLocator,
|
|
|
|
{ backupLevel: BackupLevel.Messages }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('BackupLevel.Media', () => {
|
2024-05-29 23:46:43 +00:00
|
|
|
describe('if missing critical decryption / encryption info', () => {
|
2024-05-15 14:55:20 +00:00
|
|
|
const FILE_PATH = join(__dirname, '../../../fixtures/ghost-kitty.mp4');
|
|
|
|
beforeEach(() => {
|
|
|
|
sandbox
|
|
|
|
.stub(window.Signal.Migrations, 'getAbsoluteAttachmentPath')
|
|
|
|
.callsFake(relPath => {
|
|
|
|
if (relPath === downloadedAttachment.path) {
|
|
|
|
return FILE_PATH;
|
|
|
|
}
|
|
|
|
return relPath;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
it('if missing key, generates new key & digest and removes existing CDN info', async () => {
|
|
|
|
const { filePointer: result } = await getFilePointerForAttachment({
|
2024-05-15 14:55:20 +00:00
|
|
|
attachment: {
|
|
|
|
...downloadedAttachment,
|
|
|
|
key: undefined,
|
|
|
|
},
|
|
|
|
backupLevel: BackupLevel.Media,
|
2024-05-29 23:46:43 +00:00
|
|
|
getBackupCdnInfo: notInBackupCdn,
|
|
|
|
});
|
|
|
|
const newKey = result.backupLocator?.key;
|
|
|
|
const newDigest = result.backupLocator?.digest;
|
|
|
|
|
|
|
|
strictAssert(newDigest, 'must create new digest');
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
result,
|
|
|
|
new Backups.FilePointer({
|
|
|
|
...filePointerWithBackupLocator,
|
|
|
|
backupLocator: new Backups.FilePointer.BackupLocator({
|
|
|
|
...defaultBackupLocator,
|
|
|
|
key: newKey,
|
|
|
|
digest: newDigest,
|
2024-08-01 06:13:38 +00:00
|
|
|
mediaName: Bytes.toHex(newDigest),
|
2024-05-29 23:46:43 +00:00
|
|
|
transitCdnKey: undefined,
|
|
|
|
transitCdnNumber: undefined,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-06-11 21:22:54 +00:00
|
|
|
it('if file does not exist at local path, returns invalid attachment locator', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{
|
|
|
|
...downloadedAttachment,
|
|
|
|
path: 'no/file/here.png',
|
|
|
|
key: undefined,
|
|
|
|
},
|
|
|
|
filePointerWithInvalidLocator,
|
|
|
|
{ backupLevel: BackupLevel.Media }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
it('if not on backup tier, and missing iv, regenerates encryption info', async () => {
|
|
|
|
const { filePointer: result } = await getFilePointerForAttachment({
|
|
|
|
attachment: {
|
|
|
|
...downloadedAttachment,
|
|
|
|
iv: undefined,
|
|
|
|
},
|
|
|
|
backupLevel: BackupLevel.Media,
|
|
|
|
getBackupCdnInfo: notInBackupCdn,
|
2024-05-15 14:55:20 +00:00
|
|
|
});
|
2024-05-29 23:46:43 +00:00
|
|
|
|
2024-05-15 14:55:20 +00:00
|
|
|
const newKey = result.backupLocator?.key;
|
|
|
|
const newDigest = result.backupLocator?.digest;
|
|
|
|
|
|
|
|
strictAssert(newDigest, 'must create new digest');
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
result,
|
|
|
|
new Backups.FilePointer({
|
|
|
|
...filePointerWithBackupLocator,
|
|
|
|
backupLocator: new Backups.FilePointer.BackupLocator({
|
|
|
|
...defaultBackupLocator,
|
|
|
|
key: newKey,
|
|
|
|
digest: newDigest,
|
2024-08-01 06:13:38 +00:00
|
|
|
mediaName: Bytes.toHex(newDigest),
|
2024-05-15 14:55:20 +00:00
|
|
|
transitCdnKey: undefined,
|
|
|
|
transitCdnNumber: undefined,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2024-05-29 23:46:43 +00:00
|
|
|
|
|
|
|
it('if on backup tier, and not missing iv, does not regenerate encryption info', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
{
|
|
|
|
...downloadedAttachment,
|
|
|
|
iv: undefined,
|
|
|
|
},
|
|
|
|
new Backups.FilePointer({
|
|
|
|
...filePointerWithBackupLocator,
|
|
|
|
backupLocator: new Backups.FilePointer.BackupLocator({
|
|
|
|
...defaultBackupLocator,
|
|
|
|
cdnNumber: 12,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
{ backupLevel: BackupLevel.Media, backupCdnNumber: 12 }
|
|
|
|
);
|
|
|
|
});
|
2024-05-15 14:55:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns BackupLocator, with cdnNumber if in backup tier already', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
downloadedAttachment,
|
|
|
|
new Backups.FilePointer({
|
|
|
|
...filePointerWithBackupLocator,
|
|
|
|
backupLocator: new Backups.FilePointer.BackupLocator({
|
|
|
|
...defaultBackupLocator,
|
|
|
|
cdnNumber: 12,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
{ backupLevel: BackupLevel.Media, backupCdnNumber: 12 }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns BackupLocator, with empty cdnNumber if not in backup tier', async () => {
|
|
|
|
await testAttachmentToFilePointer(
|
|
|
|
downloadedAttachment,
|
|
|
|
filePointerWithBackupLocator,
|
|
|
|
{ backupLevel: BackupLevel.Media }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2024-05-29 23:46:43 +00:00
|
|
|
|
|
|
|
describe('getBackupJobForAttachmentAndFilePointer', async () => {
|
2024-07-16 20:39:56 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
await window.storage.put('masterKey', Bytes.toBase64(getRandomBytes(32)));
|
|
|
|
});
|
|
|
|
afterEach(async () => {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.removeAll();
|
2024-07-16 20:39:56 +00:00
|
|
|
});
|
2024-05-29 23:46:43 +00:00
|
|
|
const attachment = composeAttachment();
|
|
|
|
|
|
|
|
it('returns null if filePointer does not have backupLocator', async () => {
|
|
|
|
const { filePointer } = await getFilePointerForAttachment({
|
|
|
|
attachment,
|
|
|
|
backupLevel: BackupLevel.Messages,
|
|
|
|
getBackupCdnInfo: notInBackupCdn,
|
|
|
|
});
|
|
|
|
assert.strictEqual(
|
|
|
|
await maybeGetBackupJobForAttachmentAndFilePointer({
|
|
|
|
attachment,
|
|
|
|
filePointer,
|
|
|
|
messageReceivedAt: 100,
|
|
|
|
getBackupCdnInfo: notInBackupCdn,
|
|
|
|
}),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns job if filePointer does have backupLocator', async () => {
|
|
|
|
const { filePointer, updatedAttachment } =
|
|
|
|
await getFilePointerForAttachment({
|
|
|
|
attachment,
|
|
|
|
backupLevel: BackupLevel.Media,
|
|
|
|
getBackupCdnInfo: notInBackupCdn,
|
|
|
|
});
|
|
|
|
const attachmentToUse = updatedAttachment ?? attachment;
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
await maybeGetBackupJobForAttachmentAndFilePointer({
|
|
|
|
attachment: attachmentToUse,
|
|
|
|
filePointer,
|
|
|
|
messageReceivedAt: 100,
|
|
|
|
getBackupCdnInfo: notInBackupCdn,
|
|
|
|
}),
|
|
|
|
{
|
2024-08-01 06:13:38 +00:00
|
|
|
mediaName: Bytes.toHex(defaultDigest),
|
2024-05-29 23:46:43 +00:00
|
|
|
receivedAt: 100,
|
|
|
|
type: 'standard',
|
|
|
|
data: {
|
|
|
|
path: 'path/to/file.png',
|
|
|
|
contentType: IMAGE_PNG,
|
|
|
|
keys: 'key',
|
2024-08-01 06:13:38 +00:00
|
|
|
digest: Bytes.toBase64(defaultDigest),
|
2024-05-29 23:46:43 +00:00
|
|
|
iv: 'iv',
|
|
|
|
size: 100,
|
2024-07-16 20:39:56 +00:00
|
|
|
localKey: attachment.localKey,
|
|
|
|
version: attachment.version,
|
2024-05-29 23:46:43 +00:00
|
|
|
transitCdnInfo: {
|
|
|
|
cdnKey: 'cdnKey',
|
|
|
|
cdnNumber: 2,
|
|
|
|
uploadTimestamp: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('does not return job if already in backup tier', async () => {
|
|
|
|
const isInBackupTier = async () => ({
|
|
|
|
isInBackupTier: true,
|
|
|
|
cdnNumber: 42,
|
|
|
|
});
|
|
|
|
const { filePointer } = await getFilePointerForAttachment({
|
|
|
|
attachment,
|
|
|
|
backupLevel: BackupLevel.Media,
|
|
|
|
getBackupCdnInfo: isInBackupTier,
|
|
|
|
});
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
await maybeGetBackupJobForAttachmentAndFilePointer({
|
|
|
|
attachment,
|
|
|
|
filePointer,
|
|
|
|
messageReceivedAt: 100,
|
|
|
|
getBackupCdnInfo: isInBackupTier,
|
|
|
|
}),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|