Przeglądaj źródła

Updated DirectivePlacementTransformer tests with all function scope types

sanex 4 lat temu
rodzic
commit
89db80a32c

+ 128 - 8
test/functional-tests/node-transformers/finalizing-transformers/directive-placement-transformer/DirectivePlacementTransformer.spec.ts

@@ -9,7 +9,7 @@ import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFac
 describe('DirectivePlacementTransformer', function () {
     this.timeout(120000);
 
-    describe('Program lexical scope', () => {
+    describe('Variant #1: program scope', () => {
         describe('Variant #1: directive at the top of program scope', () => {
             const directiveRegExp: RegExp = /^'use strict'; *var _0x([a-f0-9]){4} *= *\['test'];/;
 
@@ -62,8 +62,8 @@ describe('DirectivePlacementTransformer', function () {
         });
     });
 
-    describe('Function lexical scope', () => {
-        describe('Variant #1: directive at the top of function scope', () => {
+    describe('Variant #2: function declaration scope', () => {
+        describe('Variant #1: directive at the top of function declaration scope', () => {
             const directiveRegExp: RegExp = new RegExp(
                 'function test\\(\\) *{ *' +
                     '\'use strict\'; *' +
@@ -74,7 +74,7 @@ describe('DirectivePlacementTransformer', function () {
             let obfuscatedCode: string;
 
             before(() => {
-                const code: string = readFileAsString(__dirname + '/fixtures/top-of-function-scope.js');
+                const code: string = readFileAsString(__dirname + '/fixtures/top-of-function-declaration-scope.js');
 
                 obfuscatedCode = JavaScriptObfuscator.obfuscate(
                     code,
@@ -87,12 +87,12 @@ describe('DirectivePlacementTransformer', function () {
                 ).getObfuscatedCode();
             });
 
-            it('should keep directive at the top of function scope', () => {
+            it('should keep directive at the top of function declaration scope', () => {
                 assert.match(obfuscatedCode, directiveRegExp);
             });
         });
 
-        describe('Variant #2: directive-like string literal at the middle of function scope', () => {
+        describe('Variant #2: directive-like string literal at the middle of function-declaration scope', () => {
             const directiveRegExp: RegExp = new RegExp(
                 'function test\\(\\) *{ *' +
                     'var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4}; *' +
@@ -103,7 +103,7 @@ describe('DirectivePlacementTransformer', function () {
             let obfuscatedCode: string;
 
             before(() => {
-                const code: string = readFileAsString(__dirname + '/fixtures/middle-of-function-scope.js');
+                const code: string = readFileAsString(__dirname + '/fixtures/middle-of-function-declaration-scope.js');
 
                 obfuscatedCode = JavaScriptObfuscator.obfuscate(
                     code,
@@ -116,7 +116,127 @@ describe('DirectivePlacementTransformer', function () {
                 ).getObfuscatedCode();
             });
 
-            it('should keep directive-like string literal at the middle of function scope', () => {
+            it('should keep directive-like string literal at the middle of function declaration scope', () => {
+                assert.match(obfuscatedCode, directiveRegExp);
+            });
+        })
+    });
+
+    describe('Variant #3: function expression scope', () => {
+        describe('Variant #1: directive at the top of function expression scope', () => {
+            const directiveRegExp: RegExp = new RegExp(
+                'var test *= *function *\\(\\) *{ *' +
+                    '\'use strict\'; *' +
+                    'var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4}; *' +
+                    'var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\\(0x0\\);'
+            );
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/top-of-function-expression-scope.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1,
+                        stringArrayWrappersCount: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should keep directive at the top of function expression scope', () => {
+                assert.match(obfuscatedCode, directiveRegExp);
+            });
+        });
+
+        describe('Variant #2: directive-like string literal at the middle of function-expression scope', () => {
+            const directiveRegExp: RegExp = new RegExp(
+                'var test *= *function *\\(\\) *{ *' +
+                    'var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4}; *' +
+                    'var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\\(0x0\\);' +
+                    '_0x([a-f0-9]){4,6}\\(0x1\\);'
+            );
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/middle-of-function-expression-scope.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1,
+                        stringArrayWrappersCount: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should keep directive-like string literal at the middle of function expression scope', () => {
+                assert.match(obfuscatedCode, directiveRegExp);
+            });
+        })
+    });
+
+    describe('Variant #4: arrow function expression scope', () => {
+        describe('Variant #1: directive at the top of arrow function expression scope', () => {
+            const directiveRegExp: RegExp = new RegExp(
+                'var test *= *\\(\\) *=> *{ *' +
+                    '\'use strict\'; *' +
+                    'var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4}; *' +
+                    'var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\\(0x0\\);'
+            );
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/top-of-arrow-function-expression-scope.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1,
+                        stringArrayWrappersCount: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should keep directive at the top of arrow function expression scope', () => {
+                assert.match(obfuscatedCode, directiveRegExp);
+            });
+        });
+
+        describe('Variant #2: directive-like string literal at the middle of arrow function-expression scope', () => {
+            const directiveRegExp: RegExp = new RegExp(
+                'var test *= *\\(\\) *=> *{ *' +
+                    'var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4}; *' +
+                    'var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\\(0x0\\);' +
+                    '_0x([a-f0-9]){4,6}\\(0x1\\);'
+            );
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/middle-of-arrow-function-expression-scope.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1,
+                        stringArrayWrappersCount: 1
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should keep directive-like string literal at the middle of arrow function expression scope', () => {
                 assert.match(obfuscatedCode, directiveRegExp);
             });
         })

+ 4 - 0
test/functional-tests/node-transformers/finalizing-transformers/directive-placement-transformer/fixtures/middle-of-arrow-function-expression-scope.js

@@ -0,0 +1,4 @@
+var test = () => {
+    var test = 'test';
+    'use strict';
+};

+ 0 - 0
test/functional-tests/node-transformers/finalizing-transformers/directive-placement-transformer/fixtures/middle-of-function-scope.js → test/functional-tests/node-transformers/finalizing-transformers/directive-placement-transformer/fixtures/middle-of-function-declaration-scope.js


+ 4 - 0
test/functional-tests/node-transformers/finalizing-transformers/directive-placement-transformer/fixtures/middle-of-function-expression-scope.js

@@ -0,0 +1,4 @@
+var test = function () {
+    var test = 'test';
+    'use strict';
+};

+ 4 - 0
test/functional-tests/node-transformers/finalizing-transformers/directive-placement-transformer/fixtures/top-of-arrow-function-expression-scope.js

@@ -0,0 +1,4 @@
+var test = () => {
+    'use strict';
+    var test = 'test';
+};

+ 0 - 0
test/functional-tests/node-transformers/finalizing-transformers/directive-placement-transformer/fixtures/top-of-function-scope.js → test/functional-tests/node-transformers/finalizing-transformers/directive-placement-transformer/fixtures/top-of-function-declaration-scope.js


+ 4 - 0
test/functional-tests/node-transformers/finalizing-transformers/directive-placement-transformer/fixtures/top-of-function-expression-scope.js

@@ -0,0 +1,4 @@
+var test = function () {
+    'use strict';
+    var test = 'test';
+};