Browse Source

Added tests for StringArrayRotateFunction transformer, code helper, and template

sanex 4 năm trước cách đây
mục cha
commit
3f5b2c6811

+ 26 - 0
test/functional-tests/custom-code-helpers/string-array/StringArrayRotateFunctionCodeHelper.spec.ts

@@ -57,6 +57,32 @@ describe('StringArrayRotateFunctionCodeHelper', () => {
         });
     });
 
+    describe('Comparison expression', () => {
+        describe('Should add comparison expression to the code helper', () => {
+            const comparisonExpressionRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *-?parseInt\(_0x([a-f0-9]){4,6}\(0x.\)\)/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        rotateStringArray: true,
+                        stringArray: true,
+                        stringArrayThreshold: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should add comparison expression to the code', () => {
+                assert.match(obfuscatedCode, comparisonExpressionRegExp);
+            });
+        });
+    });
+
     describe('Preserve string array name', () => {
         const arrayRotateRegExp: RegExp = /c\['push']\(c\['shift']\(\)\);/;
         const comparisonRegExp: RegExp = /if *\(e *=== *d\) *{/;

+ 62 - 0
test/functional-tests/custom-code-helpers/string-array/templates/string-array-rotate-function-template/StringArrayRotateFunctionTemplate.spec.ts

@@ -10,6 +10,68 @@ import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscator
 import { readFileAsString } from '../../../../../helpers/readFileAsString';
 
 describe('StringArrayRotateFunctionTemplate', () => {
+    describe('Computed member expressions as array method calls', () => {
+        describe('Array push', () => {
+            const arrayPushBaseRegExp: RegExp = /_0x([a-f0-9]){4,6}\.push/;
+            const arrayPushComputedRegExp: RegExp = /_0x([a-f0-9]){4,6}\['push']/;
+
+            let obfuscatedCode: string;
+
+            beforeEach(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
+                const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1,
+                        rotateStringArray: true
+                    }
+                );
+
+                obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
+            });
+
+            it('Should use computed member expression in `array.push` method', () => {
+                assert.match(obfuscatedCode, arrayPushComputedRegExp);
+            });
+
+            it('Should not use base member expression in `array.push` method', () => {
+                assert.notMatch(obfuscatedCode, arrayPushBaseRegExp);
+            });
+        });
+
+        describe('Array shift', () => {
+            const arrayShiftBaseRegExp: RegExp = /_0x([a-f0-9]){4,6}\.shift/;
+            const arrayShiftComputedRegExp: RegExp = /_0x([a-f0-9]){4,6}\['shift']/;
+
+            let obfuscatedCode: string;
+
+            beforeEach(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
+                const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1,
+                        rotateStringArray: true
+                    }
+                );
+
+                obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
+            });
+
+            it('Should use computed member expression in `array.shift` method', () => {
+                assert.match(obfuscatedCode, arrayShiftComputedRegExp);
+            });
+
+            it('Should not use base member expression in `array.shift` method', () => {
+                assert.notMatch(obfuscatedCode, arrayShiftBaseRegExp);
+            });
+        });
+    });
+
     describe('Prevailing kind of variables', () => {
         describe('`var` kind', () => {
             let obfuscatedCode: string,

+ 1 - 0
test/functional-tests/custom-code-helpers/string-array/templates/string-array-rotate-function-template/fixtures/simple-input.js

@@ -0,0 +1 @@
+const foo = 'foo';

+ 123 - 0
test/functional-tests/node-transformers/string-array-transformers/string-array-rotate-function-transformer/StringArrayRotateFunctionTransformer.spec.ts

@@ -0,0 +1,123 @@
+import { assert } from 'chai';
+
+import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
+
+import { readFileAsString } from '../../../../helpers/readFileAsString';
+
+import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
+
+describe('StringArrayRotateFunctionTransformer', () => {
+    describe('Code helper append', () => {
+        const regExp: RegExp = /while *\(!!\[]\) *\{/;
+
+        describe('`stringArray` option is set', () => {
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        rotateStringArray: true,
+                        stringArray: true,
+                        stringArrayThreshold: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should correctly append code helper into the obfuscated code', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
+        });
+
+        describe('`stringArray` option isn\'t set', () => {
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        rotateStringArray: false,
+                        stringArray: true,
+                        stringArrayThreshold: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('shouldn\'t append code helper into the obfuscated code', () => {
+                assert.notMatch(obfuscatedCode, regExp);
+            });
+        });
+
+        describe('`stringArrayThreshold` option is `0.00001`', () => {
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        rotateStringArray: true,
+                        stringArray: true,
+                        stringArrayThreshold: 0.00001
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should correctly append code helper into the obfuscated code', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
+        });
+
+        describe('`stringArrayThreshold` option is `0`', () => {
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        rotateStringArray: true,
+                        stringArray: true,
+                        stringArrayThreshold: 0
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should correctly append code helper into the obfuscated code', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
+        });
+
+        describe('Input code has no string literals', () => {
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/no-string-literals.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        rotateStringArray: true,
+                        stringArray: true,
+                        stringArrayThreshold: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('shouldn\'t append code helper into the obfuscated code', () => {
+                assert.notMatch(obfuscatedCode, regExp);
+            });
+        });
+    });
+});

+ 1 - 0
test/functional-tests/node-transformers/string-array-transformers/string-array-rotate-function-transformer/fixtures/no-string-literals.js

@@ -0,0 +1 @@
+var test = 123;

+ 1 - 0
test/functional-tests/node-transformers/string-array-transformers/string-array-rotate-function-transformer/fixtures/simple-input.js

@@ -0,0 +1 @@
+var test = 'test';

+ 2 - 1
test/index.spec.ts

@@ -121,8 +121,9 @@ import './functional-tests/node-transformers/simplifying-transformers/block-stat
 import './functional-tests/node-transformers/simplifying-transformers/expression-statements-merge-transformer/ExpressionStatementsMergeTransformer.spec';
 import './functional-tests/node-transformers/simplifying-transformers/if-statement-simplify-transformer/IfStatementSimplifyTransformer.spec';
 import './functional-tests/node-transformers/simplifying-transformers/variable-declarations-merge-transformer/VariableDeclarationsMergeTransformer.spec';
-import './functional-tests/node-transformers/string-array-transformers/string-array-transformer/StringArrayTransformer.spec';
+import './functional-tests/node-transformers/string-array-transformers/string-array-rotate-function-transformer/StringArrayRotateFunctionTransformer.spec';
 import './functional-tests/node-transformers/string-array-transformers/string-array-scope-calls-wrapper-transformer/StringArrayScopeCallsWrapperTransformer.spec';
+import './functional-tests/node-transformers/string-array-transformers/string-array-transformer/StringArrayTransformer.spec';
 import './functional-tests/options/OptionsNormalizer.spec';
 import './functional-tests/options/domain-lock/Validation.spec';
 import './functional-tests/storages/string-array-transformers/string-array-storage/StringArrayStorage.spec';