mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Improved CLI directory obfuscation
Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/157 Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/156
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import * as fs from 'fs';
|
||||
import * as mkdirp from 'mkdirp';
|
||||
import * as rimraf from 'rimraf';
|
||||
import * as sinon from 'sinon';
|
||||
|
||||
import { assert } from 'chai';
|
||||
@@ -170,8 +171,70 @@ describe('JavaScriptObfuscatorCLI', function (): void {
|
||||
});
|
||||
|
||||
after(() => {
|
||||
fs.unlinkSync(outputFixturesFilePath1);
|
||||
fs.unlinkSync(outputFixturesFilePath2);
|
||||
rimraf.sync(outputFixturesFilePath1);
|
||||
rimraf.sync(outputFixturesFilePath2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: obfuscation of directory with `output` option', () => {
|
||||
const directoryPath: string = `${fixturesDirName}/directory-obfuscation`;
|
||||
const outputDirectoryName: string = 'obfuscated';
|
||||
const outputDirectoryPath: string = `${directoryPath}/${outputDirectoryName}`;
|
||||
const outputFileName1: string = 'foo.js';
|
||||
const outputFileName2: string = 'bar.js';
|
||||
const outputFileName3: string = 'baz.js';
|
||||
|
||||
let outputFixturesFilePath1: string,
|
||||
outputFixturesFilePath2: string,
|
||||
outputFixturesFilePath3: string,
|
||||
isFileExist1: boolean,
|
||||
isFileExist2: boolean,
|
||||
isFileExist3: boolean;
|
||||
|
||||
before(() => {
|
||||
outputFixturesFilePath1 = `${outputDirectoryPath}/${directoryPath}/${outputFileName1}`;
|
||||
outputFixturesFilePath2 = `${outputDirectoryPath}/${directoryPath}/${outputFileName2}`;
|
||||
outputFixturesFilePath3 = `${outputDirectoryPath}/${directoryPath}/${outputFileName3}`;
|
||||
|
||||
JavaScriptObfuscator.runCLI([
|
||||
'node',
|
||||
'javascript-obfuscator',
|
||||
directoryPath,
|
||||
'--output',
|
||||
outputDirectoryPath
|
||||
]);
|
||||
|
||||
isFileExist1 = fs.existsSync(outputFixturesFilePath1);
|
||||
isFileExist2 = fs.existsSync(outputFixturesFilePath2);
|
||||
isFileExist3 = fs.existsSync(outputFixturesFilePath3);
|
||||
});
|
||||
|
||||
it(
|
||||
`should create file \`${outputFileName1}\` with obfuscated code in ` +
|
||||
`\`${fixturesDirName}/${outputDirectoryName}\` directory`,
|
||||
() => {
|
||||
assert.equal(isFileExist1, true);
|
||||
}
|
||||
);
|
||||
|
||||
it(
|
||||
`should create file \`${outputFileName2}\` with obfuscated code in ` +
|
||||
`\`${fixturesDirName}/${outputDirectoryName}\` directory`,
|
||||
() => {
|
||||
assert.equal(isFileExist2, true);
|
||||
}
|
||||
);
|
||||
|
||||
it(
|
||||
`shouldn't create file \`${outputFileName3}\` in ` +
|
||||
`\`${fixturesDirName}/${outputDirectoryName}\` directory`,
|
||||
() => {
|
||||
assert.equal(isFileExist3, false);
|
||||
}
|
||||
);
|
||||
|
||||
after(() => {
|
||||
rimraf.sync(outputDirectoryPath);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -225,8 +288,8 @@ describe('JavaScriptObfuscatorCLI', function (): void {
|
||||
});
|
||||
|
||||
after(() => {
|
||||
fs.unlinkSync(outputFilePath);
|
||||
fs.unlinkSync(outputSourceMapPath);
|
||||
rimraf.sync(outputFilePath);
|
||||
rimraf.sync(outputSourceMapPath);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -516,7 +579,7 @@ describe('JavaScriptObfuscatorCLI', function (): void {
|
||||
});
|
||||
|
||||
after(() => {
|
||||
fs.rmdirSync(outputDirName);
|
||||
rimraf.sync(outputDirName);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,11 +6,22 @@ import { CLIUtils } from '../../../../src/cli/utils/CLIUtils';
|
||||
|
||||
describe('CLIUtils', () => {
|
||||
describe('getOutputCodePath (inputPath: string): string', () => {
|
||||
let expectedInputPath: string = 'test/input/test-obfuscated.js',
|
||||
inputPath: string = 'test/input/test.js';
|
||||
describe('variant #1: base input path', () => {
|
||||
let expectedOutputPath: string = 'test/input/test-obfuscated.js',
|
||||
inputPath: string = 'test/input/test.js';
|
||||
|
||||
it('should output path based on `inputPath`', () => {
|
||||
assert.equal(CLIUtils.getOutputCodePath(inputPath), expectedInputPath);
|
||||
it('should output path based on `inputPath`', () => {
|
||||
assert.equal(CLIUtils.getOutputCodePath(inputPath), expectedOutputPath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('variant #2: relative input path with dot', () => {
|
||||
let expectedOutputPath: string = 'input-obfuscated.js',
|
||||
inputPath: string = './input.js';
|
||||
|
||||
it('should output path based on `inputPath`', () => {
|
||||
assert.equal(CLIUtils.getOutputCodePath(inputPath), expectedOutputPath);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as fs from 'fs';
|
||||
import * as mkdirp from 'mkdirp';
|
||||
import * as rimraf from 'rimraf';
|
||||
|
||||
import { assert } from 'chai';
|
||||
|
||||
@@ -8,17 +9,17 @@ import { IFileData } from '../../../../src/interfaces/cli/IFileData';
|
||||
|
||||
describe('SourceCodeReader', () => {
|
||||
const fileContent: string = 'test';
|
||||
const tmpDir: string = 'test/tmp';
|
||||
const tmpDirectoryPath: string = 'test/tmp';
|
||||
|
||||
before(() => {
|
||||
mkdirp.sync(tmpDir);
|
||||
mkdirp.sync(tmpDirectoryPath);
|
||||
});
|
||||
|
||||
describe('readSourceCode (inputPath: string): void', () => {
|
||||
describe('Variant #1: input path is a file path', () => {
|
||||
describe('`inputPath` is a valid path', () => {
|
||||
const tmpFileName: string = 'test.js';
|
||||
const inputPath: string = `${tmpDir}/${tmpFileName}`;
|
||||
const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
|
||||
|
||||
let result: string | IFileData[];
|
||||
|
||||
@@ -38,7 +39,7 @@ describe('SourceCodeReader', () => {
|
||||
|
||||
describe('`inputPath` is not a valid path', () => {
|
||||
const tmpFileName: string = 'test.js';
|
||||
const inputPath: string = `${tmpDir}/${tmpFileName}`;
|
||||
const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
|
||||
|
||||
let testFunc: () => void;
|
||||
|
||||
@@ -53,7 +54,7 @@ describe('SourceCodeReader', () => {
|
||||
|
||||
describe('`inputPath` has invalid extension', () => {
|
||||
const tmpFileName: string = 'test.ts';
|
||||
const inputPath: string = `${tmpDir}/${tmpFileName}`;
|
||||
const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
|
||||
|
||||
let testFunc: () => void;
|
||||
|
||||
@@ -78,10 +79,10 @@ describe('SourceCodeReader', () => {
|
||||
const tmpFileName2: string = 'bar.js';
|
||||
const tmpFileName3: string = 'baz.png';
|
||||
const tmpFileName4: string = 'bark-obfuscated.js';
|
||||
const filePath1: string = `${tmpDir}/${tmpFileName1}`;
|
||||
const filePath2: string = `${tmpDir}/${tmpFileName2}`;
|
||||
const filePath3: string = `${tmpDir}/${tmpFileName3}`;
|
||||
const filePath4: string = `${tmpDir}/${tmpFileName4}`;
|
||||
const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
|
||||
const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
|
||||
const filePath3: string = `${tmpDirectoryPath}/${tmpFileName3}`;
|
||||
const filePath4: string = `${tmpDirectoryPath}/${tmpFileName4}`;
|
||||
|
||||
const expectedResult: IFileData[] = [
|
||||
{
|
||||
@@ -101,7 +102,7 @@ describe('SourceCodeReader', () => {
|
||||
fs.writeFileSync(filePath2, fileContent);
|
||||
fs.writeFileSync(filePath3, fileContent);
|
||||
fs.writeFileSync(filePath4, fileContent);
|
||||
result = SourceCodeReader.readSourceCode(tmpDir);
|
||||
result = SourceCodeReader.readSourceCode(tmpDirectoryPath);
|
||||
});
|
||||
|
||||
it('should return files data', () => {
|
||||
@@ -129,10 +130,69 @@ describe('SourceCodeReader', () => {
|
||||
assert.throws(testFunc, ReferenceError);
|
||||
});
|
||||
});
|
||||
|
||||
describe('`inputPath` is a directory with sub-directories', () => {
|
||||
const parentDirectoryName1: string = 'parent1';
|
||||
const parentDirectoryName2: string = 'parent';
|
||||
const parentDirectoryPath1: string = `${tmpDirectoryPath}/${parentDirectoryName1}`;
|
||||
const parentDirectoryPath2: string = `${tmpDirectoryPath}/${parentDirectoryName2}`;
|
||||
const tmpFileName1: string = 'foo.js';
|
||||
const tmpFileName2: string = 'bar.js';
|
||||
const tmpFileName3: string = 'baz.js';
|
||||
const tmpFileName4: string = 'bark.js';
|
||||
const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
|
||||
const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
|
||||
const filePath3: string = `${parentDirectoryPath1}/${tmpFileName3}`;
|
||||
const filePath4: string = `${parentDirectoryPath2}/${tmpFileName4}`;
|
||||
|
||||
const expectedResult: IFileData[] = [
|
||||
{
|
||||
filePath: filePath2,
|
||||
content: fileContent
|
||||
},
|
||||
{
|
||||
filePath: filePath1,
|
||||
content: fileContent
|
||||
},
|
||||
{
|
||||
filePath: filePath4,
|
||||
content: fileContent
|
||||
},
|
||||
{
|
||||
filePath: filePath3,
|
||||
content: fileContent
|
||||
}
|
||||
];
|
||||
|
||||
let result: string | IFileData[];
|
||||
|
||||
before(() => {
|
||||
mkdirp.sync(parentDirectoryPath1);
|
||||
mkdirp.sync(parentDirectoryPath2);
|
||||
fs.writeFileSync(filePath1, fileContent);
|
||||
fs.writeFileSync(filePath2, fileContent);
|
||||
fs.writeFileSync(filePath3, fileContent);
|
||||
fs.writeFileSync(filePath4, fileContent);
|
||||
result = SourceCodeReader.readSourceCode(tmpDirectoryPath);
|
||||
});
|
||||
|
||||
it('should return files data', () => {
|
||||
assert.deepEqual(result, expectedResult);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
fs.unlinkSync(filePath1);
|
||||
fs.unlinkSync(filePath2);
|
||||
fs.unlinkSync(filePath3);
|
||||
fs.unlinkSync(filePath4);
|
||||
rimraf.sync(parentDirectoryPath1);
|
||||
rimraf.sync(parentDirectoryPath2);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
after(() => {
|
||||
fs.rmdirSync(tmpDir);
|
||||
rimraf.sync(tmpDirectoryPath);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user