2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-08-27 18:08:37 +00:00
|
|
|
const fs = require('fs');
|
2018-03-16 21:32:17 +00:00
|
|
|
const fse = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const tmp = require('tmp');
|
|
|
|
const { assert } = require('chai');
|
2020-08-27 18:08:37 +00:00
|
|
|
const { app } = require('electron');
|
2018-03-16 21:32:17 +00:00
|
|
|
|
|
|
|
const Attachments = require('../../app/attachments');
|
2021-09-20 16:27:15 +00:00
|
|
|
const { stringToArrayBuffer } = require('../../ts/util/stringToArrayBuffer');
|
2018-03-16 21:32:17 +00:00
|
|
|
|
|
|
|
const PREFIX_LENGTH = 2;
|
|
|
|
const NUM_SEPARATORS = 1;
|
|
|
|
const NAME_LENGTH = 64;
|
|
|
|
const PATH_LENGTH = PREFIX_LENGTH + NUM_SEPARATORS + NAME_LENGTH;
|
|
|
|
|
|
|
|
describe('Attachments', () => {
|
2018-04-04 01:06:29 +00:00
|
|
|
describe('createWriterForNew', () => {
|
2018-03-19 23:44:14 +00:00
|
|
|
let tempRootDirectory = null;
|
2018-03-16 21:32:17 +00:00
|
|
|
before(() => {
|
2018-03-19 23:44:14 +00:00
|
|
|
tempRootDirectory = tmp.dirSync().name;
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
2018-03-19 23:44:14 +00:00
|
|
|
await fse.remove(tempRootDirectory);
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should write file to disk and return path', async () => {
|
|
|
|
const input = stringToArrayBuffer('test string');
|
2018-04-04 01:06:29 +00:00
|
|
|
const tempDirectory = path.join(
|
|
|
|
tempRootDirectory,
|
|
|
|
'Attachments_createWriterForNew'
|
|
|
|
);
|
2018-03-16 21:32:17 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const outputPath = await Attachments.createWriterForNew(tempDirectory)(
|
|
|
|
input
|
|
|
|
);
|
2018-03-16 21:32:17 +00:00
|
|
|
const output = await fse.readFile(path.join(tempDirectory, outputPath));
|
|
|
|
|
|
|
|
assert.lengthOf(outputPath, PATH_LENGTH);
|
|
|
|
|
|
|
|
const inputBuffer = Buffer.from(input);
|
2018-03-19 23:45:03 +00:00
|
|
|
assert.deepEqual(inputBuffer, output);
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|
2018-03-19 23:45:22 +00:00
|
|
|
});
|
|
|
|
|
2018-04-04 01:08:43 +00:00
|
|
|
describe('createWriterForExisting', () => {
|
|
|
|
let tempRootDirectory = null;
|
|
|
|
before(() => {
|
|
|
|
tempRootDirectory = tmp.dirSync().name;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await fse.remove(tempRootDirectory);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should write file to disk on given path and return path', async () => {
|
|
|
|
const input = stringToArrayBuffer('test string');
|
|
|
|
const tempDirectory = path.join(
|
|
|
|
tempRootDirectory,
|
|
|
|
'Attachments_createWriterForExisting'
|
|
|
|
);
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const relativePath = Attachments.getRelativePath(
|
|
|
|
Attachments.createName()
|
|
|
|
);
|
2018-04-04 01:08:43 +00:00
|
|
|
const attachment = {
|
|
|
|
path: relativePath,
|
|
|
|
data: input,
|
|
|
|
};
|
2018-04-27 21:25:04 +00:00
|
|
|
const outputPath = await Attachments.createWriterForExisting(
|
|
|
|
tempDirectory
|
|
|
|
)(attachment);
|
2018-04-04 01:08:43 +00:00
|
|
|
const output = await fse.readFile(path.join(tempDirectory, outputPath));
|
|
|
|
|
|
|
|
assert.equal(outputPath, relativePath);
|
|
|
|
|
|
|
|
const inputBuffer = Buffer.from(input);
|
|
|
|
assert.deepEqual(inputBuffer, output);
|
|
|
|
});
|
2018-05-19 01:08:22 +00:00
|
|
|
|
|
|
|
it('throws if relative path goes higher than root', async () => {
|
|
|
|
const input = stringToArrayBuffer('test string');
|
|
|
|
const tempDirectory = path.join(
|
|
|
|
tempRootDirectory,
|
|
|
|
'Attachments_createWriterForExisting'
|
|
|
|
);
|
|
|
|
|
|
|
|
const relativePath = '../../parent';
|
|
|
|
const attachment = {
|
|
|
|
path: relativePath,
|
|
|
|
data: input,
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
await Attachments.createWriterForExisting(tempDirectory)(attachment);
|
|
|
|
} catch (error) {
|
|
|
|
assert.strictEqual(error.message, 'Invalid relative path');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Expected an error');
|
|
|
|
});
|
2018-04-04 01:08:43 +00:00
|
|
|
});
|
|
|
|
|
2018-04-03 19:25:24 +00:00
|
|
|
describe('createReader', () => {
|
2018-03-19 23:45:22 +00:00
|
|
|
let tempRootDirectory = null;
|
|
|
|
before(() => {
|
|
|
|
tempRootDirectory = tmp.dirSync().name;
|
|
|
|
});
|
2018-03-16 21:32:17 +00:00
|
|
|
|
2018-03-19 23:45:22 +00:00
|
|
|
after(async () => {
|
|
|
|
await fse.remove(tempRootDirectory);
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
const input = stringToArrayBuffer('test string');
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2018-05-19 01:08:22 +00:00
|
|
|
|
|
|
|
it('throws if relative path goes higher than root', async () => {
|
|
|
|
const tempDirectory = path.join(
|
|
|
|
tempRootDirectory,
|
|
|
|
'Attachments_createReader'
|
|
|
|
);
|
|
|
|
|
|
|
|
const relativePath = '../../parent';
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Attachments.createReader(tempDirectory)(relativePath);
|
|
|
|
} catch (error) {
|
|
|
|
assert.strictEqual(error.message, 'Invalid relative path');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Expected an error');
|
|
|
|
});
|
2018-03-19 23:45:22 +00:00
|
|
|
});
|
|
|
|
|
2020-08-27 18:08:37 +00:00
|
|
|
describe('copyIntoAttachmentsDirectory', () => {
|
|
|
|
// These tests use the `userData` path. In `electron-mocha`, these are temporary
|
|
|
|
// directories; no need to be concerned about messing with the "real" directory.
|
|
|
|
before(function thisNeeded() {
|
|
|
|
this.filesToRemove = [];
|
|
|
|
this.getFakeAttachmentsDirectory = () => {
|
|
|
|
const result = path.join(
|
|
|
|
app.getPath('userData'),
|
|
|
|
`fake-attachments-${Date.now()}-${Math.random()
|
|
|
|
.toString()
|
|
|
|
.substring(2)}`
|
|
|
|
);
|
|
|
|
this.filesToRemove.push(result);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
this.getTempFile = () => {
|
|
|
|
const result = tmp.fileSync().name;
|
|
|
|
this.filesToRemove.push(result);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async function thisNeeded() {
|
|
|
|
await Promise.all(
|
|
|
|
this.filesToRemove.map(toRemove => fse.remove(toRemove))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws if passed a non-string', () => {
|
|
|
|
assert.throws(() => {
|
|
|
|
Attachments.copyIntoAttachmentsDirectory(1234);
|
|
|
|
}, TypeError);
|
|
|
|
assert.throws(() => {
|
|
|
|
Attachments.copyIntoAttachmentsDirectory(null);
|
|
|
|
}, TypeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a function that rejects if the source path is not a string', async function thisNeeded() {
|
|
|
|
const copier = Attachments.copyIntoAttachmentsDirectory(
|
|
|
|
await this.getFakeAttachmentsDirectory()
|
|
|
|
);
|
|
|
|
return copier(123)
|
|
|
|
.then(() => {
|
|
|
|
assert.fail('This should never be run');
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
assert.instanceOf(err, TypeError);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a function that rejects if the source path is not in the user config directory', async function thisNeeded() {
|
|
|
|
const copier = Attachments.copyIntoAttachmentsDirectory(
|
|
|
|
await this.getFakeAttachmentsDirectory()
|
|
|
|
);
|
|
|
|
return copier(this.getTempFile())
|
|
|
|
.then(() => {
|
|
|
|
assert.fail('This should never be run');
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
assert.instanceOf(err, Error);
|
|
|
|
assert.strictEqual(
|
|
|
|
err.message,
|
|
|
|
"'sourcePath' must be relative to the user config directory"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a function that copies the source path into the attachments directory', async function thisNeeded() {
|
|
|
|
const attachmentsPath = await this.getFakeAttachmentsDirectory();
|
|
|
|
const someOtherPath = path.join(app.getPath('userData'), 'somethingElse');
|
|
|
|
await fse.outputFile(someOtherPath, 'hello world');
|
|
|
|
this.filesToRemove.push(someOtherPath);
|
|
|
|
|
|
|
|
const copier = Attachments.copyIntoAttachmentsDirectory(attachmentsPath);
|
|
|
|
const relativePath = await copier(someOtherPath);
|
|
|
|
|
|
|
|
const absolutePath = path.join(attachmentsPath, relativePath);
|
|
|
|
assert.notEqual(someOtherPath, absolutePath);
|
|
|
|
assert.strictEqual(
|
|
|
|
await fs.promises.readFile(absolutePath, 'utf8'),
|
|
|
|
'hello world'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-03 19:25:24 +00:00
|
|
|
describe('createDeleter', () => {
|
2018-03-19 23:50:14 +00:00
|
|
|
let tempRootDirectory = null;
|
|
|
|
before(() => {
|
|
|
|
tempRootDirectory = tmp.dirSync().name;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await fse.remove(tempRootDirectory);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete file from disk', async () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
const tempDirectory = path.join(
|
|
|
|
tempRootDirectory,
|
|
|
|
'Attachments_createDeleter'
|
|
|
|
);
|
2018-03-19 23:50:14 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const relativePath = Attachments.getRelativePath(
|
|
|
|
Attachments.createName()
|
|
|
|
);
|
2018-03-19 23:50:14 +00:00
|
|
|
const fullPath = path.join(tempDirectory, relativePath);
|
|
|
|
const input = stringToArrayBuffer('test string');
|
|
|
|
|
|
|
|
const inputBuffer = Buffer.from(input);
|
|
|
|
await fse.ensureFile(fullPath);
|
|
|
|
await fse.writeFile(fullPath, inputBuffer);
|
2018-04-03 19:25:24 +00:00
|
|
|
await Attachments.createDeleter(tempDirectory)(relativePath);
|
2018-03-19 23:50:14 +00:00
|
|
|
|
|
|
|
const existsFile = await fse.exists(fullPath);
|
|
|
|
assert.isFalse(existsFile);
|
|
|
|
});
|
2018-05-19 01:08:22 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
2018-03-19 23:50:14 +00:00
|
|
|
});
|
|
|
|
|
2018-03-19 23:45:22 +00:00
|
|
|
describe('createName', () => {
|
|
|
|
it('should return random file name with correct length', () => {
|
|
|
|
assert.lengthOf(Attachments.createName(), NAME_LENGTH);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getRelativePath', () => {
|
|
|
|
it('should return correct path', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
const name =
|
|
|
|
'608ce3bc536edbf7637a6aeb6040bdfec49349140c0dd43e97c7ce263b15ff7e';
|
2018-03-19 23:45:22 +00:00
|
|
|
assert.lengthOf(Attachments.getRelativePath(name), PATH_LENGTH);
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|
|
|
|
});
|
2018-05-19 01:08:22 +00:00
|
|
|
|
|
|
|
describe('createAbsolutePathGetter', () => {
|
2018-05-31 21:09:41 +00:00
|
|
|
const isWindows = process.platform === 'win32';
|
|
|
|
|
2018-05-19 01:08:22 +00:00
|
|
|
it('combines root and relative path', () => {
|
2018-05-31 21:09:41 +00:00
|
|
|
const root = isWindows ? 'C:\\temp' : '/tmp';
|
2018-05-19 01:08:22 +00:00
|
|
|
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'
|
|
|
|
);
|
2018-05-19 01:08:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('throws if relative path goes higher than root', () => {
|
2018-05-31 21:09:41 +00:00
|
|
|
const root = isWindows ? 'C:\\temp' : 'tmp';
|
2018-05-19 01:08:22 +00:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|