Browse Source

Added `ScopeThroughIdentifiersTransformer` tests

sanex 4 năm trước cách đây
mục cha
commit
b62f49369c
10 tập tin đã thay đổi với 282 bổ sung0 xóa
  1. 89 0
      test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/class-declaration/ClassDeclaration.spec.ts
  2. 3 0
      test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/class-declaration/fixtures/class-call-with-declaration.js
  3. 1 0
      test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/class-declaration/fixtures/class-call-without-declaration.js
  4. 89 0
      test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/function-declaration/FunctionDeclaration.spec.ts
  5. 3 0
      test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/function-declaration/fixtures/function-call-with-declaration.js
  6. 1 0
      test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/function-declaration/fixtures/function-call-without-declaration.js
  7. 89 0
      test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/VariableDeclaration.spec.ts
  8. 3 0
      test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-with-declaration.js
  9. 1 0
      test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration.js
  10. 3 0
      test/index.spec.ts

+ 89 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/class-declaration/ClassDeclaration.spec.ts

@@ -0,0 +1,89 @@
+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('ScopeThroughIdentifiersTransformer ClassDeclaration identifiers', () => {
+    describe('Variant #1: class declaration is exist', () => {
+        const classNameIdentifierRegExp: RegExp = /class *(_0x[a-f0-9]{4,6}) *\{/;
+        const classCallIdentifierRegExp: RegExp = /new *(_0x[a-f0-9]{4,6}) *\( *\);/;
+
+        let obfuscatedCode: string;
+
+        before(() => {
+            const code: string = readFileAsString(__dirname + '/fixtures/class-call-with-declaration.js');
+
+            obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                code,
+                {
+                    ...NO_ADDITIONAL_NODES_PRESET,
+                    renameGlobals: true,
+                    identifierNamesCache: {
+                        'Foo': 'Foo_from_cache'
+                    }
+                }
+            ).getObfuscatedCode();
+        });
+
+        it('should skip transformation of class name', () => {
+            assert.match(obfuscatedCode, classNameIdentifierRegExp);
+        });
+
+        it('should skip transformation of class name', () => {
+            assert.match(obfuscatedCode, classCallIdentifierRegExp);
+        });
+    });
+
+    describe('Variant #2: class declaration is missing', () => {
+        describe('Variant #1: transformation of class call identifier node name based on identifier names cache', () => {
+            const classCallIdentifierRegExp: RegExp = /new *Foo_from_cache *\( *\);/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/class-call-without-declaration.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        renameGlobals: true,
+                        identifierNamesCache: {
+                            'Foo': 'Foo_from_cache'
+                        }
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should transform class name', () => {
+                assert.match(obfuscatedCode, classCallIdentifierRegExp);
+            });
+        });
+
+        describe('Variant #2: ignore transformation of class call identifier node name when no identifier names cache value is available', () => {
+            const classCallIdentifierRegExp: RegExp = /new *Foo *\( *\);/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/class-call-without-declaration.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        renameGlobals: true,
+                        identifierNamesCache: {}
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should not transform class name', () => {
+                assert.match(obfuscatedCode, classCallIdentifierRegExp);
+            });
+        });
+    });
+});

+ 3 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/class-declaration/fixtures/class-call-with-declaration.js

@@ -0,0 +1,3 @@
+class Foo {}
+
+new Foo();

+ 1 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/class-declaration/fixtures/class-call-without-declaration.js

@@ -0,0 +1 @@
+new Foo();

+ 89 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/function-declaration/FunctionDeclaration.spec.ts

@@ -0,0 +1,89 @@
+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('ScopeThroughIdentifiersTransformer FunctionDeclaration identifiers', () => {
+    describe('Variant #1: function declaration is exist', () => {
+        const functionNameIdentifierRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\(\) *\{/;
+        const functionCallIdentifierRegExp: RegExp = /(_0x[a-f0-9]{4,6}) *\( *\);/;
+
+        let obfuscatedCode: string;
+
+        before(() => {
+            const code: string = readFileAsString(__dirname + '/fixtures/function-call-with-declaration.js');
+
+            obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                code,
+                {
+                    ...NO_ADDITIONAL_NODES_PRESET,
+                    renameGlobals: true,
+                    identifierNamesCache: {
+                        'foo': 'foo_from_cache'
+                    }
+                }
+            ).getObfuscatedCode();
+        });
+
+        it('should skip transformation of function name', () => {
+            assert.match(obfuscatedCode, functionNameIdentifierRegExp);
+        });
+
+        it('should skip transformation of function name', () => {
+            assert.match(obfuscatedCode, functionCallIdentifierRegExp);
+        });
+    });
+
+    describe('Variant #2: function declaration is missing', () => {
+        describe('Variant #1: transformation of function call identifier node name based on identifier names cache', () => {
+            const functionCallIdentifierRegExp: RegExp = /foo_from_cache *\( *\);/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/function-call-without-declaration.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        renameGlobals: true,
+                        identifierNamesCache: {
+                            'foo': 'foo_from_cache'
+                        }
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should transform function name', () => {
+                assert.match(obfuscatedCode, functionCallIdentifierRegExp);
+            });
+        });
+
+        describe('Variant #2: ignore transformation of function call identifier node name when no identifier names cache value is available', () => {
+            const functionCallIdentifierRegExp: RegExp = /foo *\( *\);/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/function-call-without-declaration.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        renameGlobals: true,
+                        identifierNamesCache: {}
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should not transform function name', () => {
+                assert.match(obfuscatedCode, functionCallIdentifierRegExp);
+            });
+        });
+    });
+});

+ 3 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/function-declaration/fixtures/function-call-with-declaration.js

@@ -0,0 +1,3 @@
+function foo () {}
+
+foo();

+ 1 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/function-declaration/fixtures/function-call-without-declaration.js

@@ -0,0 +1 @@
+foo();

+ 89 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/VariableDeclaration.spec.ts

@@ -0,0 +1,89 @@
+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('ScopeThroughIdentifiersTransformer VariableDeclaration identifiers', () => {
+    describe('Variant #1: variable declaration is exist', () => {
+        const variableIdentifierRegExp: RegExp = /var *(_0x[a-f0-9]{4,6}) *= *0x1/;
+        const variableReferenceIdentifierRegExp: RegExp = /(_0x[a-f0-9]{4,6});/;
+
+        let obfuscatedCode: string;
+
+        before(() => {
+            const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-with-declaration.js');
+
+            obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                code,
+                {
+                    ...NO_ADDITIONAL_NODES_PRESET,
+                    renameGlobals: true,
+                    identifierNamesCache: {
+                        'foo': 'foo_from_cache'
+                    }
+                }
+            ).getObfuscatedCode();
+        });
+
+        it('should skip transformation of variable declaration name', () => {
+            assert.match(obfuscatedCode, variableIdentifierRegExp);
+        });
+
+        it('should skip transformation of variable reference name', () => {
+            assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+        });
+    });
+
+    describe('Variant #2: variable declaration is missing', () => {
+        describe('Variant #1: transformation of variable reference identifier node name based on identifier names cache', () => {
+            const variableReferenceIdentifierRegExp: RegExp = /foo_from_cache;/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        renameGlobals: true,
+                        identifierNamesCache: {
+                            'foo': 'foo_from_cache'
+                        }
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should transform variable reference name', () => {
+                assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+            });
+        });
+
+        describe('Variant #2: ignore transformation of variable reference identifier node name when no identifier names cache value is available', () => {
+            const variableReferenceIdentifierRegExp: RegExp = /foo;/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/variable-reference-without-declaration.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        renameGlobals: true,
+                        identifierNamesCache: {}
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should not transform variable reference name', () => {
+                assert.match(obfuscatedCode, variableReferenceIdentifierRegExp);
+            });
+        });
+    });
+});

+ 3 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-with-declaration.js

@@ -0,0 +1,3 @@
+var foo = 1;
+
+foo;

+ 1 - 0
test/functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/fixtures/variable-reference-without-declaration.js

@@ -0,0 +1 @@
+foo;

+ 3 - 0
test/index.spec.ts

@@ -118,6 +118,9 @@ import './functional-tests/node-transformers/rename-identifiers-transformers/sco
 import './functional-tests/node-transformers/rename-identifiers-transformers/scope-identifiers-transformer/function/Function.spec';
 import './functional-tests/node-transformers/rename-identifiers-transformers/scope-identifiers-transformer/import-declaration/ImportDeclaration.spec';
 import './functional-tests/node-transformers/rename-identifiers-transformers/scope-identifiers-transformer/variable-declaration/VariableDeclaration.spec';
+import './functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/class-declaration/ClassDeclaration.spec';
+import './functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/function-declaration/FunctionDeclaration.spec';
+import './functional-tests/node-transformers/rename-identifiers-transformers/scope-through-identifiers-transformer/variable-declaration/VariableDeclaration.spec';
 import './functional-tests/node-transformers/rename-properties-transformers/rename-properties-transformer/RenamePropertiesTransformer.spec';
 import './functional-tests/node-transformers/simplifying-transformers/block-statement-simplify-transformer/BlockStatementSimplifyTransformer.spec';
 import './functional-tests/node-transformers/simplifying-transformers/expression-statements-merge-transformer/ExpressionStatementsMergeTransformer.spec';