Skip to content

Commit

Permalink
Fix - parse args with equal sign (#50)
Browse files Browse the repository at this point in the history
* Refactor parseArgs to allow usage of '-k=value' style args

* Improve maxIterations validation

* Create tests for Utils and add parseArgs and argsAreValid tests

* Update dependencies

* 1.4.6
  • Loading branch information
BenBaryoPX authored Dec 11, 2022
1 parent 7286817 commit 08b557b
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 23 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "restringer",
"version": "1.4.5",
"version": "1.4.6",
"description": "Deobfuscate Javascript with emphasis on reconstructing strings",
"main": "index.js",
"bin": {
Expand Down
31 changes: 17 additions & 14 deletions src/utils/parseArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ function parseArgs(args) {
let opts;
try {
const inputFilename = args[0] && args[0][0] !== '-' ? args[0] : '';
const argsStr = args.join(' ');
opts = {
inputFilename,
help: args.includes('-h') || args.includes('--help'),
clean: args.includes('-c') || args.includes('--clean'),
quiet: args.includes('-q') || args.includes('--quiet'),
verbose: args.includes('-v') || args.includes('--verbose'),
outputToFile: args.includes('-o') || args.includes('--output'),
maxIterations: args.includes('-m') || args.includes('--max-iterations'),
help: /(^|\s)(-h|--help)/.test(argsStr),
clean: /(^|\s)(-c|--clean)/.test(argsStr),
quiet: /(^|\s)(-q|--quiet)/.test(argsStr),
verbose: /(^|\s)(-v|--verbose)/.test(argsStr),
outputToFile: /(^|\s)(-o|--output)/.test(argsStr),
maxIterations: /(^|\s)(-m|--max-iterations)/.test(argsStr),
outputFilename: `${inputFilename}-deob.js`,
};
if (opts.outputToFile) {
const outFileIdx = (~args.indexOf('-o') ? args.indexOf('-o') : args.indexOf('--output')) + 1;
if (args[outFileIdx] && args[outFileIdx][0] !== '-') opts.outputFilename = opts[outFileIdx];
}
if (opts.maxIterations) {
const maxItersIdx = (~args.indexOf('-m') ? args.indexOf('-m') : args.indexOf('--max-iterations')) + 1;
if (args[maxItersIdx] && args[maxItersIdx][0] !== '-') opts.maxIterations = Number(args[maxItersIdx]);
for (let i = 1; i < args.length; i++) {
if (opts.outputToFile && /-o|--output/.exec(args[i])) {
if (args[i].includes('=')) opts.outputFilename = args[i].split('=')[1];
else if (args[i + 1] && args[i + 1][0] !== '-') opts.outputFilename = args[i + 1];
break;
} else if (opts.maxIterations && /-m|--max-iterations/.exec(args[i])) {
if (args[i].includes('=')) opts.maxIterations = Number(args[i].split('=')[1]);
else if (args[i + 1] && args[i + 1][0] !== '-') opts.maxIterations = Number(args[i + 1]);
}
}
} catch {}
return opts;
Expand All @@ -53,7 +56,7 @@ function argsAreValid(args) {
if (args.help) console.log(printHelp());
else if (!args.inputFilename) console.log(`Error: Input filename must be provided`);
else if (args.verbose && args.quiet) console.log(`Error: Don't set both -q and -v at the same time *smh*`);
else if (args.maxIterations === true) console.log(`Error: --max-iterations requires a number (e.g. --max-iterations 12)`);
else if (args.maxIterations !== false && Number.isNaN(parseInt(args.maxIterations))) console.log(`Error: --max-iterations requires a number larger than 0 (e.g. --max-iterations 12)`);
else return true;
return false;
}
Expand Down
1 change: 1 addition & 0 deletions tests/testRestringer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const availableTests = {
Utils: __dirname + '/testUtils',
Modules: __dirname + '/testModules',
Processors: __dirname + '/testProcessors',
Deobfuscation: __dirname + '/testDeobfuscations',
Expand Down
40 changes: 40 additions & 0 deletions tests/testUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const assert = require('node:assert');

const tests = {
Utils: __dirname + '/utils-tests',
};

/**
* Generic function for verifying a utility function is behaving as expected.
* @param testName {string} - The name of the test to be displayed
* @param testFunc {function} - The source code to be used
* @param verifyFunc {function} - The expected output
*/
function testCodeSample(testName, testFunc, verifyFunc) {
process.stdout.write(`Testing ${testName}... `);
console.time('PASS');
const results = testFunc();
const expected = verifyFunc();
assert.deepEqual(results, expected);
console.timeEnd('PASS');
}

let allTests = 0;
let skippedTests = 0;
console.time('tests in');
for (const [moduleName, moduleTests] of Object.entries(tests)) {
const loadedTests = require(moduleTests);
for (const test of loadedTests) {
allTests++;
if (test.enabled) {
testCodeSample(`[${moduleName}] ${test.name}`.padEnd(90, '.'), test.testFunc, test.verifyFunc);
} else {
skippedTests++;
console.log(`Testing [${moduleName}] ${test.name}...`.padEnd(101, '.') + ` SKIPPED: ${test.reason}`);
}
}
}
if (skippedTests > 0) {
process.stdout.write(`Completed ${allTests - skippedTests}/${allTests} (${skippedTests} skipped) utility `);
} else process.stdout.write(`Completed ${allTests} utility `);
console.timeEnd('tests in');
Loading

0 comments on commit 08b557b

Please sign in to comment.