jsobx
Version:
a simple js obfuscator
109 lines (101 loc) • 2.9 kB
JavaScript
const jsobx = require('./index');
const fs = require('fs');
const path = require('path');
const defaults = {
groupDomProperties: false,
groupDomMethods: false,
groupDomMethodsNoArgs: false,
groupStrings: false,
groupRegexes: false,
shuffle: false,
mangle: false,
wrap: false,
splitString: false
};
let testCases = [
{
options: {},
inputFile: 'tests/input1.js',
expectedFile: 'tests/expected1.0.js'
},
{
options: {
groupDomProperties: true
},
inputFile: 'tests/input1.js',
expectedFile: 'tests/expected1.1.js'
},
{
options: {
groupDomMethodsNoArgs: true
},
inputFile: 'tests/input1.js',
expectedFile: 'tests/expected1.2.js'
},
{
options: {
groupDomMethods: true
},
inputFile: 'tests/input1.js',
expectedFile: 'tests/expected1.3.js'
},
{
options: {
groupStrings: true
},
inputFile: 'tests/input1.js',
expectedFile: 'tests/expected1.4.js'
},
{
options: {
groupRegexes: true
},
inputFile: 'tests/input1.js',
expectedFile: 'tests/expected1.5.js'
},
{
options: {
groupStrings: true,
splitString: true
},
inputFile: 'tests/input1.js',
expectedFile: 'tests/expected1.6.js'
}
];
const isDebug = typeof test === 'undefined';
if (isDebug) {
testCases = [
{
options: {
// groupDomProperties: true,
groupDomMethodsNoArgs: true,
// groupDomMethods: true,
// groupStrings: true,
// groupRegexes: true,
// splitString: true
},
inputFile: 'tests/input0.js',
expectedFile: 'tests/expected0.js'
}
];
}
testCases.forEach(({ inputFile, expectedFile, options}, index) => {
const input = fs.readFileSync(inputFile, 'utf8').trim();
if (isDebug) {
const output = jsobx.process(input, Object.assign({}, defaults, options));
console.log(output);
} else {
test(`Test case: ${index}`, () => {
const outputFile = path.dirname(expectedFile) + '/' + path.basename(expectedFile).replace('expected', 'output');
const expected = fs.existsSync(expectedFile) ? fs.readFileSync(expectedFile, 'utf8').trim() : '';
const output = jsobx.process(input, Object.assign({}, defaults, options));
try {
expect(output).toBe(expected);
} catch (err) {
fs.writeFileSync(outputFile, output);
throw err;
}
});
}
});
// find . -name "output*.js" | xargs -I % bash -c "N=\$(echo % | sed 's|output|expected|'); mv % \$N"