2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-11 21:17:07 +00:00
|
|
|
import { assert } from 'chai';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as os from 'os';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as fse from 'fs-extra';
|
|
|
|
import * as Sinon from 'sinon';
|
|
|
|
|
|
|
|
import { writeWindowsZoneIdentifier } from '../../util/windowsZoneIdentifier';
|
|
|
|
|
|
|
|
describe('writeWindowsZoneIdentifier', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
before(function (this: Mocha.Context) {
|
2020-09-11 21:17:07 +00:00
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
this.skip();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(async function (this: Mocha.Context) {
|
2020-09-11 21:17:07 +00:00
|
|
|
this.sandbox = Sinon.createSandbox();
|
|
|
|
this.tmpdir = await fs.promises.mkdtemp(
|
|
|
|
path.join(os.tmpdir(), 'signal-test-')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
afterEach(async function (this: Mocha.Context) {
|
2020-09-11 21:17:07 +00:00
|
|
|
this.sandbox.restore();
|
|
|
|
await fse.remove(this.tmpdir);
|
|
|
|
});
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
it('writes zone transfer ID 3 (internet) to the Zone.Identifier file', async function (this: Mocha.Context) {
|
2020-09-11 21:17:07 +00:00
|
|
|
const file = path.join(this.tmpdir, 'file.txt');
|
|
|
|
await fse.outputFile(file, 'hello');
|
|
|
|
|
|
|
|
await writeWindowsZoneIdentifier(file);
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
await fs.promises.readFile(`${file}:Zone.Identifier`, 'utf8'),
|
|
|
|
'[ZoneTransfer]\r\nZoneId=3'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
it('fails if there is an existing Zone.Identifier file', async function (this: Mocha.Context) {
|
2020-09-11 21:17:07 +00:00
|
|
|
const file = path.join(this.tmpdir, 'file.txt');
|
|
|
|
await fse.outputFile(file, 'hello');
|
|
|
|
await fs.promises.writeFile(`${file}:Zone.Identifier`, '# already here');
|
|
|
|
|
2021-12-13 17:38:40 +00:00
|
|
|
await assert.isRejected(writeWindowsZoneIdentifier(file));
|
2020-09-11 21:17:07 +00:00
|
|
|
});
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
it('fails if the original file does not exist', async function (this: Mocha.Context) {
|
2020-09-11 21:17:07 +00:00
|
|
|
const file = path.join(this.tmpdir, 'file-never-created.txt');
|
|
|
|
|
2021-12-13 17:38:40 +00:00
|
|
|
await assert.isRejected(writeWindowsZoneIdentifier(file));
|
2020-09-11 21:17:07 +00:00
|
|
|
});
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
it('fails if not on Windows', async function (this: Mocha.Context) {
|
2020-09-11 21:17:07 +00:00
|
|
|
this.sandbox.stub(process, 'platform').get(() => 'darwin');
|
|
|
|
|
|
|
|
const file = path.join(this.tmpdir, 'file.txt');
|
|
|
|
await fse.outputFile(file, 'hello');
|
|
|
|
|
2021-12-13 17:38:40 +00:00
|
|
|
await assert.isRejected(writeWindowsZoneIdentifier(file));
|
2020-09-11 21:17:07 +00:00
|
|
|
});
|
|
|
|
});
|