Ver código fonte

MemberExpressionObfuscator functional tests

sanex3339 8 anos atrás
pai
commit
b6cae792ec

+ 35 - 37
test/functional-tests/node-obfuscators/LiteralObfuscator.spec.ts

@@ -7,48 +7,19 @@ import { JavaScriptObfuscator } from "../../../src/JavaScriptObfuscator";
 const assert: Chai.AssertStatic = require('chai').assert;
 
 describe('LiteralObfuscator', () => {
-    describe('obfuscateNode (literalNode: ILiteralNode, parentNode: INode): void', () => {
-        describe('obfuscation of literal node with string value', () => {
-            it('should replace literal node value with unicode value without encoding to base64', () => {
-                let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
-                    `var test = 'test';`,
-                    Object.assign({}, NO_CUSTOM_NODES_PRESET)
-                );
-
-                assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *'\\x74\\x65\\x73\\x74';$/);
-            });
-
-            it('should replace literal node value with unicode value with encoding to base64', () => {
-                let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
-                    `var test = 'test';`,
-                    Object.assign({}, NO_CUSTOM_NODES_PRESET, {
-                        encodeUnicodeLiterals: true,
-                        unicodeArray: true,
-                        unicodeArrayThreshold: 1
-                    })
-                );
-
-                assert.match(obfuscationResult.getObfuscatedCode(),  /^var *_0x([a-z0-9]){4} *= *\['\\x64\\x47\\x56\\x7a\\x64\\x41\\x3d\\x3d'\];/);
-                assert.match(obfuscationResult.getObfuscatedCode(),  /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/);
-            });
-        });
-
-        it('should obfuscate literal node with boolean value', () => {
+    describe('obfuscation of literal node with string value', () => {
+        it('should replace literal node value with unicode value without encoding to base64', () => {
             let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
-                `var test = true;`,
-                Object.assign({}, NO_CUSTOM_NODES_PRESET, {
-                    encodeUnicodeLiterals: true,
-                    unicodeArray: true,
-                    unicodeArrayThreshold: 1
-                })
+                `var test = 'test';`,
+                Object.assign({}, NO_CUSTOM_NODES_PRESET)
             );
 
-            assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *!!\[\];$/);
+            assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *'\\x74\\x65\\x73\\x74';$/);
         });
 
-        it('should obfuscate literal node with number value', () => {
+        it('should replace literal node value with unicode value with encoding to base64', () => {
             let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
-                `var test = 0;`,
+                `var test = 'test';`,
                 Object.assign({}, NO_CUSTOM_NODES_PRESET, {
                     encodeUnicodeLiterals: true,
                     unicodeArray: true,
@@ -56,7 +27,34 @@ describe('LiteralObfuscator', () => {
                 })
             );
 
-            assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *0x0;$/);
+            assert.match(obfuscationResult.getObfuscatedCode(),  /^var *_0x([a-z0-9]){4} *= *\['\\x64\\x47\\x56\\x7a\\x64\\x41\\x3d\\x3d'\];/);
+            assert.match(obfuscationResult.getObfuscatedCode(),  /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/);
         });
     });
+
+    it('should obfuscate literal node with boolean value', () => {
+        let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+            `var test = true;`,
+            Object.assign({}, NO_CUSTOM_NODES_PRESET, {
+                encodeUnicodeLiterals: true,
+                unicodeArray: true,
+                unicodeArrayThreshold: 1
+            })
+        );
+
+        assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *!!\[\];$/);
+    });
+
+    it('should obfuscate literal node with number value', () => {
+        let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+            `var test = 0;`,
+            Object.assign({}, NO_CUSTOM_NODES_PRESET, {
+                encodeUnicodeLiterals: true,
+                unicodeArray: true,
+                unicodeArrayThreshold: 1
+            })
+        );
+
+        assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *0x0;$/);
+    });
 });

+ 60 - 0
test/functional-tests/node-obfuscators/MemberExpressionObfuscator.spec.ts

@@ -0,0 +1,60 @@
+import { IObfuscationResult } from "../../../src/interfaces/IObfuscationResult";
+
+import { NO_CUSTOM_NODES_PRESET } from "../../../src/preset-options/NoCustomNodesPreset";
+
+import { JavaScriptObfuscator } from "../../../src/JavaScriptObfuscator";
+
+const assert: Chai.AssertStatic = require('chai').assert;
+
+describe('MemberExpressionObfuscator', () => {
+    describe('obfuscation of member expression node with dot notation', () => {
+        it('should replace member expression dot notation call by square brackets call with unicode literal value', () => {
+            let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                `var test = console.log;`,
+                Object.assign({}, NO_CUSTOM_NODES_PRESET)
+            );
+
+            assert.match(obfuscationResult.getObfuscatedCode(),  /var *test *= *console\['\\x6c\\x6f\\x67'\];/);
+        });
+
+        it('should replace member expression dot notation call by square brackets call to unicode array', () => {
+            let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                `var test = console.log;`,
+                Object.assign({}, NO_CUSTOM_NODES_PRESET, {
+                    unicodeArray: true,
+                    unicodeArrayThreshold: 1
+                })
+            );
+
+            assert.match(obfuscationResult.getObfuscatedCode(),  /var *_0x([a-z0-9]){4} *= *\['\\x6c\\x6f\\x67'\];/);
+            assert.match(obfuscationResult.getObfuscatedCode(),  /var *test *= *console\[_0x([a-z0-9]){4}\[0x0\]\];/);
+        });
+    });
+
+    describe('obfuscation of member expression node without dot notation', () => {
+        it('should replace member expression square brackets call by square brackets call to unicode array', () => {
+            let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                `var test = console['log'];`,
+                Object.assign({}, NO_CUSTOM_NODES_PRESET, {
+                    unicodeArray: true,
+                    unicodeArrayThreshold: 1
+                })
+            );
+
+            assert.match(obfuscationResult.getObfuscatedCode(),  /var *_0x([a-z0-9]){4} *= *\['\\x6c\\x6f\\x67'\];/);
+            assert.match(obfuscationResult.getObfuscatedCode(),  /var *test *= *console\[_0x([a-z0-9]){4}\[0x0\]\];/);
+        });
+
+        it('should ignore square brackets call with identifier value', () => {
+            let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                `
+                    var identifier = 'log'; 
+                    var test = console[identifier];
+                `,
+                Object.assign({}, NO_CUSTOM_NODES_PRESET)
+            );
+
+            assert.match(obfuscationResult.getObfuscatedCode(),  /var *test *= *console\[identifier\];/);
+        });
+    });
+});

+ 2 - 1
test/index.spec.ts

@@ -23,4 +23,5 @@ import './unit-tests/node-obfuscators/FunctionObfuscator.spec';
 import './functional-tests/JavaScriptObfuscator.spec';
 import './functional-tests/JavaScriptObfuscatorCLI.spec';
 import './functional-tests/JavaScriptObfuscatorInternal.spec';
-import './functional-tests/node-obfuscators/LiteralObfuscator.spec';
+import './functional-tests/node-obfuscators/LiteralObfuscator.spec';
+import './functional-tests/node-obfuscators/MemberExpressionObfuscator.spec';