signal-desktop/ts/test-electron/windows/attachments_test.ts

300 lines
9 KiB
TypeScript
Raw Normal View History

2021-10-27 17:54:16 +00:00
// Copyright 2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2021-10-27 17:54:16 +00:00
import { assert } from 'chai';
import path from 'path';
import fs from 'fs';
import os from 'os';
import fse from 'fs-extra';
import * as Attachments from '../../windows/attachments';
import * as Bytes from '../../Bytes';
const PREFIX_LENGTH = 2;
const NUM_SEPARATORS = 1;
const NAME_LENGTH = 64;
const PATH_LENGTH = PREFIX_LENGTH + NUM_SEPARATORS + NAME_LENGTH;
describe('Attachments', () => {
2021-10-27 17:54:16 +00:00
const USER_DATA = window.SignalContext.getPath('userData');
2021-10-27 17:54:16 +00:00
let tempRootDirectory: string;
2021-10-27 17:54:16 +00:00
before(() => {
tempRootDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'Signal'));
2018-03-19 23:45:22 +00:00
});
2021-10-27 17:54:16 +00:00
after(async () => {
await fse.remove(tempRootDirectory);
});
describe('createReader', () => {
2018-03-19 23:45:22 +00:00
it('should read file from disk', async () => {
2018-04-27 21:25:04 +00:00
const tempDirectory = path.join(
tempRootDirectory,
'Attachments_createReader'
);
2018-03-19 23:45:22 +00:00
2018-04-27 21:25:04 +00:00
const relativePath = Attachments.getRelativePath(
Attachments.createName()
);
2018-03-19 23:45:22 +00:00
const fullPath = path.join(tempDirectory, relativePath);
2021-09-24 00:49:05 +00:00
const input = Bytes.fromString('test string');
2018-03-19 23:45:22 +00:00
const inputBuffer = Buffer.from(input);
await fse.ensureFile(fullPath);
await fse.writeFile(fullPath, inputBuffer);
2018-04-27 21:25:04 +00:00
const output = await Attachments.createReader(tempDirectory)(
relativePath
);
2018-03-19 23:45:22 +00:00
assert.deepEqual(input, output);
});
it('throws if relative path goes higher than root', async () => {
const tempDirectory = path.join(
tempRootDirectory,
'Attachments_createReader'
);
const relativePath = '../../parent';
2021-10-27 17:54:16 +00:00
await assert.isRejected(
Attachments.createReader(tempDirectory)(relativePath),
'Invalid relative path'
);
});
2018-03-19 23:45:22 +00:00
});
describe('copyIntoAttachmentsDirectory', () => {
2021-10-27 17:54:16 +00:00
let filesToRemove: Array<string>;
const getFakeAttachmentsDirectory = () => {
const result = path.join(
USER_DATA,
`fake-attachments-${Date.now()}-${Math.random()
.toString()
.substring(2)}`
);
filesToRemove.push(result);
return result;
};
// These tests use the `userData` path. In `electron-mocha`, these are temporary
// directories; no need to be concerned about messing with the "real" directory.
2021-10-27 17:54:16 +00:00
before(() => {
filesToRemove = [];
});
2021-10-27 17:54:16 +00:00
after(async () => {
await Promise.all(filesToRemove.map(toRemove => fse.remove(toRemove)));
filesToRemove = [];
});
it('throws if passed a non-string', () => {
assert.throws(() => {
2021-11-11 22:43:05 +00:00
Attachments.copyIntoAttachmentsDirectory(1234 as unknown as string);
}, TypeError);
assert.throws(() => {
2021-11-11 22:43:05 +00:00
Attachments.copyIntoAttachmentsDirectory(null as unknown as string);
}, TypeError);
});
2021-10-27 17:54:16 +00:00
it('returns a function that rejects if the source path is not a string', async () => {
const copier = Attachments.copyIntoAttachmentsDirectory(
2021-10-27 17:54:16 +00:00
await getFakeAttachmentsDirectory()
);
2021-11-11 22:43:05 +00:00
await assert.isRejected(copier(123 as unknown as string));
});
2021-10-27 17:54:16 +00:00
it('returns a function that rejects if the source path is not in the user config directory', async () => {
const copier = Attachments.copyIntoAttachmentsDirectory(
2021-10-27 17:54:16 +00:00
await getFakeAttachmentsDirectory()
);
await assert.isRejected(
copier(path.join(tempRootDirectory, 'hello.txt')),
"'sourcePath' must be relative to the user config directory"
);
});
2021-10-27 17:54:16 +00:00
it('returns a function that copies the source path into the attachments directory and returns its path and size', async () => {
const attachmentsPath = await getFakeAttachmentsDirectory();
const someOtherPath = path.join(USER_DATA, 'somethingElse');
await fse.outputFile(someOtherPath, 'hello world');
2021-10-27 17:54:16 +00:00
filesToRemove.push(someOtherPath);
const copier = Attachments.copyIntoAttachmentsDirectory(attachmentsPath);
const { path: relativePath, size } = await copier(someOtherPath);
const absolutePath = path.join(attachmentsPath, relativePath);
assert.notEqual(someOtherPath, absolutePath);
assert.strictEqual(
await fs.promises.readFile(absolutePath, 'utf8'),
'hello world'
);
assert.strictEqual(size, 'hello world'.length);
});
});
2021-10-27 17:54:16 +00:00
describe('createWriterForExisting', () => {
it('should write file to disk on given path and return path', async () => {
const input = Bytes.fromString('test string');
2018-04-27 21:25:04 +00:00
const tempDirectory = path.join(
tempRootDirectory,
2021-10-27 17:54:16 +00:00
'Attachments_createWriterForExisting'
2018-04-27 21:25:04 +00:00
);
2018-03-19 23:50:14 +00:00
2018-04-27 21:25:04 +00:00
const relativePath = Attachments.getRelativePath(
Attachments.createName()
);
2021-10-27 17:54:16 +00:00
const attachment = {
path: relativePath,
data: input,
};
const outputPath = await Attachments.createWriterForExisting(
tempDirectory
)(attachment);
const output = await fse.readFile(path.join(tempDirectory, outputPath));
2018-03-19 23:50:14 +00:00
2021-10-27 17:54:16 +00:00
assert.equal(outputPath, relativePath);
2018-03-19 23:50:14 +00:00
2021-10-27 17:54:16 +00:00
const inputBuffer = Buffer.from(input);
assert.deepEqual(inputBuffer, output);
2018-03-19 23:50:14 +00:00
});
it('throws if relative path goes higher than root', async () => {
2021-10-27 17:54:16 +00:00
const input = Bytes.fromString('test string');
const tempDirectory = path.join(
tempRootDirectory,
2021-10-27 17:54:16 +00:00
'Attachments_createWriterForExisting'
);
const relativePath = '../../parent';
2021-10-27 17:54:16 +00:00
const attachment = {
path: relativePath,
data: input,
};
try {
2021-10-27 17:54:16 +00:00
await Attachments.createWriterForExisting(tempDirectory)(attachment);
} catch (error) {
assert.strictEqual(error.message, 'Invalid relative path');
return;
}
throw new Error('Expected an error');
});
2018-03-19 23:50:14 +00:00
});
2021-10-27 17:54:16 +00:00
describe('createWriterForNew', () => {
it('should write file to disk and return path', async () => {
const input = Bytes.fromString('test string');
const tempDirectory = path.join(
tempRootDirectory,
'Attachments_createWriterForNew'
);
2018-03-19 23:45:22 +00:00
2021-10-27 17:54:16 +00:00
const outputPath = await Attachments.createWriterForNew(tempDirectory)(
input
);
const output = await fse.readFile(path.join(tempDirectory, outputPath));
assert.lengthOf(outputPath, PATH_LENGTH);
const inputBuffer = Buffer.from(input);
assert.deepEqual(inputBuffer, output);
});
});
describe('createAbsolutePathGetter', () => {
2018-05-31 21:09:41 +00:00
const isWindows = process.platform === 'win32';
it('combines root and relative path', () => {
2018-05-31 21:09:41 +00:00
const root = isWindows ? 'C:\\temp' : '/tmp';
const relative = 'ab/abcdef';
const pathGetter = Attachments.createAbsolutePathGetter(root);
const absolutePath = pathGetter(relative);
2018-05-31 21:09:41 +00:00
assert.strictEqual(
absolutePath,
isWindows ? 'C:\\temp\\ab\\abcdef' : '/tmp/ab/abcdef'
);
});
it('throws if relative path goes higher than root', () => {
2018-05-31 21:09:41 +00:00
const root = isWindows ? 'C:\\temp' : 'tmp';
const relative = '../../ab/abcdef';
const pathGetter = Attachments.createAbsolutePathGetter(root);
try {
pathGetter(relative);
} catch (error) {
assert.strictEqual(error.message, 'Invalid relative path');
return;
}
throw new Error('Expected an error');
});
});
2021-10-27 17:54:16 +00:00
describe('createName', () => {
it('should return random file name with correct length', () => {
assert.lengthOf(Attachments.createName(), NAME_LENGTH);
});
2021-11-02 23:01:13 +00:00
it('can include a suffix', () => {
const result = Attachments.createName('.txt');
assert.lengthOf(result, NAME_LENGTH + '.txt'.length);
assert(result.endsWith('.txt'));
});
2021-10-27 17:54:16 +00:00
});
describe('getRelativePath', () => {
it('should return correct path', () => {
const name =
'608ce3bc536edbf7637a6aeb6040bdfec49349140c0dd43e97c7ce263b15ff7e';
assert.lengthOf(Attachments.getRelativePath(name), PATH_LENGTH);
});
});
describe('createDeleter', () => {
it('should delete file from disk', async () => {
const tempDirectory = path.join(
tempRootDirectory,
'Attachments_createDeleter'
);
const relativePath = Attachments.getRelativePath(
Attachments.createName()
);
const fullPath = path.join(tempDirectory, relativePath);
const input = Bytes.fromString('test string');
const inputBuffer = Buffer.from(input);
await fse.ensureFile(fullPath);
await fse.writeFile(fullPath, inputBuffer);
await Attachments.createDeleter(tempDirectory)(relativePath);
const existsFile = await fse.pathExists(fullPath);
assert.isFalse(existsFile);
});
it('throws if relative path goes higher than root', async () => {
const tempDirectory = path.join(
tempRootDirectory,
'Attachments_createDeleter'
);
const relativePath = '../../parent';
try {
await Attachments.createDeleter(tempDirectory)(relativePath);
} catch (error) {
assert.strictEqual(error.message, 'Invalid relative path');
return;
}
throw new Error('Expected an error');
});
});
});