Browse Source

Updated StringArrayScopeCallsWrapperTransformer tests

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

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/index.browser.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 25054
dist/index.cli.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 24085
dist/index.js


+ 15 - 24
test/dev/dev.ts

@@ -2,33 +2,24 @@
 
 import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNodes';
 import { StringArrayIndexesType } from '../../src/enums/node-transformers/string-array-transformers/StringArrayIndexesType';
-import { StringArrayEncoding } from '../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
 
 (function () {
     const JavaScriptObfuscator: any = require('../../index');
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
-            const foo = 'foo';
-            const bar = 'bar';
-            
-            function test1 () {
-                const baz = 'baz';
-                
-                function test2() {
-                    const bark = 'bark';
-                    
-                    console.log(bark);
-                }
-                
-                console.log(baz);
-                
-                test2();
+            // Paste your JavaScript code here
+            function hi() {
+              function inner () {
+                  console.log('inner');
+                  console.log('inner1');
+              }
+              
+              console.log("Hello World!");
+              
+              inner();
             }
-            
-            console.log(foo, bar);
-            
-            test1();
+            hi();
         `,
         {
             ...NO_ADDITIONAL_NODES_PRESET,
@@ -36,19 +27,19 @@ import { StringArrayEncoding } from '../../src/enums/node-transformers/string-ar
             rotateStringArray: true,
             shuffleStringArray: true,
             stringArray: true,
-            stringArrayEncoding: [
+            /*stringArrayEncoding: [
                 StringArrayEncoding.None,
                 StringArrayEncoding.Rc4
-            ],
+            ],*/
             stringArrayIndexesType: [
                 StringArrayIndexesType.HexadecimalNumericString,
                 StringArrayIndexesType.HexadecimalNumber
             ],
             stringArrayIndexShift: true,
             stringArrayThreshold: 1,
-            stringArrayWrappersCount: 2,
+            stringArrayWrappersCount: 1,
             stringArrayWrappersChainedCalls: true,
-            stringArrayWrappersParametersMaxCount: 5,
+            stringArrayWrappersParametersMaxCount: 2,
             stringArrayWrappersType: 'function'
         }
     ).getObfuscatedCode();

+ 131 - 85
test/functional-tests/node-transformers/string-array-transformers/string-array-scope-calls-wrapper-transformer/StringArrayScopeCallsWrapperTransformer.spec.ts

@@ -2,14 +2,15 @@ import { assert } from 'chai';
 
 import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
 import { StringArrayEncoding } from '../../../../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
+import { StringArrayIndexesType } from '../../../../../src/enums/node-transformers/string-array-transformers/StringArrayIndexesType';
 import { StringArrayWrappersType } from '../../../../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';
 
 import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
 
 import { readFileAsString } from '../../../../helpers/readFileAsString';
+import { checkCodeEvaluation } from '../../../../helpers/checkCodeEvaluation';
 
 import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
-import { StringArrayIndexesType } from '../../../../../src/enums/node-transformers/string-array-transformers/StringArrayIndexesType';
 
 describe('StringArrayScopeCallsWrapperTransformer', function () {
     this.timeout(120000);
@@ -576,33 +577,34 @@ describe('StringArrayScopeCallsWrapperTransformer', function () {
         });
 
         describe('Variant #7: `stringArrayWrappersType` option has `Function` value', () => {
+            const evaluationSamplesCount: number = 500;
             const hexadecimalIndexMatch: string = '0x[a-z0-9]{1,3}';
 
             describe('Variant #1: base', () => {
                 describe('Variant #1: `hexadecimal-number` indexes type', () => {
                     const stringArrayCallRegExp: RegExp = new RegExp(
                         'const f *= *function *\\(c, *d\\) *{' +
-                            `return b\\(c *-(?: -)?${hexadecimalIndexMatch}, *d\\);` +
+                            `return b\\([cd] *-(?: -)?${hexadecimalIndexMatch}, *[cd]\\);` +
                         '};' +
-                        `const foo *= *f\\(-? *${hexadecimalIndexMatch}\\);` +
-                        `const bar *= *f\\(-? *${hexadecimalIndexMatch}\\);` +
-                        `const baz *= *f\\(-? *${hexadecimalIndexMatch}\\);` +
+                        `const foo *= *f\\(-? *${hexadecimalIndexMatch}\\, *-? *${hexadecimalIndexMatch}\\);` +
+                        `const bar *= *f\\(-? *${hexadecimalIndexMatch}\\, *-? *${hexadecimalIndexMatch}\\);` +
+                        `const baz *= *f\\(-? *${hexadecimalIndexMatch}\\, *-? *${hexadecimalIndexMatch}\\);` +
                         'function test *\\( *\\) *{' +
                             'const g *= *function *\\(c, *d\\) *{' +
-                                `return b\\(c *-(?: -)?${hexadecimalIndexMatch}, *d\\);` +
+                                `return b\\([cd] *-(?: -)?${hexadecimalIndexMatch}, *[cd]\\);` +
                             '};' +
-                            `const c *= *g\\(-? *${hexadecimalIndexMatch}\\);` +
-                            `const d *= *g\\(-? *${hexadecimalIndexMatch}\\);` +
-                            `const e *= *g\\(-? *${hexadecimalIndexMatch}\\);` +
+                            `const c *= *g\\(-? *${hexadecimalIndexMatch}\\, *-? *${hexadecimalIndexMatch}\\);` +
+                            `const d *= *g\\(-? *${hexadecimalIndexMatch}\\, *-? *${hexadecimalIndexMatch}\\);` +
+                            `const e *= *g\\(-? *${hexadecimalIndexMatch}\\, *-? *${hexadecimalIndexMatch}\\);` +
                         '}'
                     );
 
                     let obfuscatedCode: string;
+                    let areSuccessEvaluations: boolean;
 
                     before(() => {
                         const code: string = readFileAsString(__dirname + '/fixtures/wrappers-count-const.js');
-
-                        obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        const getObfuscatedCode: () => string = () => JavaScriptObfuscator.obfuscate(
                             code,
                             {
                                 ...NO_ADDITIONAL_NODES_PRESET,
@@ -617,37 +619,47 @@ describe('StringArrayScopeCallsWrapperTransformer', function () {
                                 stringArrayWrappersType: StringArrayWrappersType.Function
                             }
                         ).getObfuscatedCode();
+
+                        obfuscatedCode = getObfuscatedCode();
+                        areSuccessEvaluations = checkCodeEvaluation(
+                            getObfuscatedCode,
+                            evaluationSamplesCount
+                        ).areSuccessEvaluations;
                     });
 
                     it('should add correct scope calls wrappers', () => {
                         assert.match(obfuscatedCode, stringArrayCallRegExp);
                     });
+
+                    it('should evaluate code without errors', () => {
+                        assert.isTrue(areSuccessEvaluations);
+                    });
                 });
 
                 describe('Variant #2: `hexadecimal-numeric-string` indexes type', () => {
                     const stringArrayCallRegExp: RegExp = new RegExp(
                         'const f *= *function *\\(c, *d\\) *{' +
-                            `return b\\(c *-(?: -)?'${hexadecimalIndexMatch}', *d\\);` +
+                            `return b\\([cd] *-(?: -)?'${hexadecimalIndexMatch}', *[cd]\\);` +
                         '};' +
-                        `const foo *= *f\\(-? *'${hexadecimalIndexMatch}'\\);` +
-                        `const bar *= *f\\(-? *'${hexadecimalIndexMatch}'\\);` +
-                        `const baz *= *f\\(-? *'${hexadecimalIndexMatch}'\\);` +
+                        `const foo *= *f\\(-? *'${hexadecimalIndexMatch}', *-? *'${hexadecimalIndexMatch}'\\);` +
+                        `const bar *= *f\\(-? *'${hexadecimalIndexMatch}', *-? *'${hexadecimalIndexMatch}'\\);` +
+                        `const baz *= *f\\(-? *'${hexadecimalIndexMatch}', *-? *'${hexadecimalIndexMatch}'\\);` +
                         'function test *\\( *\\) *{' +
                             'const g *= *function *\\(c, *d\\) *{' +
-                                `return b\\(c *-(?: -)?'${hexadecimalIndexMatch}', *d\\);` +
+                                `return b\\([cd] *-(?: -)?'${hexadecimalIndexMatch}', *[cd]\\);` +
                             '};' +
-                            `const c *= *g\\(-? *'${hexadecimalIndexMatch}'\\);` +
-                            `const d *= *g\\(-? *'${hexadecimalIndexMatch}'\\);` +
-                            `const e *= *g\\(-? *'${hexadecimalIndexMatch}'\\);` +
+                            `const c *= *g\\(-? *'${hexadecimalIndexMatch}', *-? *'${hexadecimalIndexMatch}'\\);` +
+                            `const d *= *g\\(-? *'${hexadecimalIndexMatch}', *-? *'${hexadecimalIndexMatch}'\\);` +
+                            `const e *= *g\\(-? *'${hexadecimalIndexMatch}', *-? *'${hexadecimalIndexMatch}'\\);` +
                         '}'
                     );
 
                     let obfuscatedCode: string;
+                    let areSuccessEvaluations: boolean;
 
                     before(() => {
                         const code: string = readFileAsString(__dirname + '/fixtures/wrappers-count-const.js');
-
-                        obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        const getObfuscatedCode = () => JavaScriptObfuscator.obfuscate(
                             code,
                             {
                                 ...NO_ADDITIONAL_NODES_PRESET,
@@ -662,94 +674,128 @@ describe('StringArrayScopeCallsWrapperTransformer', function () {
                                 stringArrayWrappersType: StringArrayWrappersType.Function
                             }
                         ).getObfuscatedCode();
+
+                        obfuscatedCode = getObfuscatedCode();
+                        areSuccessEvaluations = checkCodeEvaluation(
+                            getObfuscatedCode,
+                            evaluationSamplesCount
+                        ).areSuccessEvaluations;
                     });
 
                     it('should add correct scope calls wrappers', () => {
                         assert.match(obfuscatedCode, stringArrayCallRegExp);
                     });
+
+                    it('should evaluate code without errors', () => {
+                        assert.isTrue(areSuccessEvaluations);
+                    });
                 });
             });
 
             describe('Variant #2: correct chained calls', () => {
-                    const stringArrayCallRegExp: RegExp = new RegExp(
-                        'const f *= *function *\\(c, *d\\) *{' +
-                            `return b\\(c *-(?: -)?${hexadecimalIndexMatch}, *d\\);` +
+                const stringArrayCallRegExp: RegExp = new RegExp(
+                    'const f *= *function *\\(c, *d\\) *{' +
+                        `return b\\([cd] *-(?: -)?${hexadecimalIndexMatch}, *[cd]\\);` +
+                    '};' +
+                    `const foo *= *f\\(-? *${hexadecimalIndexMatch}, *-? *${hexadecimalIndexMatch}\\);` +
+                    `const bar *= *f\\(-? *${hexadecimalIndexMatch}, *-? *${hexadecimalIndexMatch}\\);` +
+                    `const baz *= *f\\(-? *${hexadecimalIndexMatch}, *-? *${hexadecimalIndexMatch}\\);` +
+                    'function test *\\( *\\) *{' +
+                        'const g *= *function *\\(c, *d\\) *{' +
+                            `return f\\(` +
+                                // order of arguments depends on the parent wrapper parameters order
+                                `[cd](?: *-(?: -)?${hexadecimalIndexMatch})?, *` +
+                                `[cd](?: *-(?: -)?${hexadecimalIndexMatch})?` +
+                            `\\);` +
                         '};' +
-                        `const foo *= *f\\(-? *${hexadecimalIndexMatch}\\);` +
-                        `const bar *= *f\\(-? *${hexadecimalIndexMatch}\\);` +
-                        `const baz *= *f\\(-? *${hexadecimalIndexMatch}\\);` +
-                        'function test *\\( *\\) *{' +
-                            'const g *= *function *\\(c, *d\\) *{' +
-                                `return f\\(c *-(?: -)?${hexadecimalIndexMatch}, *d\\);` +
-                            '};' +
-                            `const c *= *g\\(-? *${hexadecimalIndexMatch}\\);` +
-                            `const d *= *g\\(-? *${hexadecimalIndexMatch}\\);` +
-                            `const e *= *g\\(-? *${hexadecimalIndexMatch}\\);` +
-                        '}'
-                    );
+                        `const c *= *g\\(-? *${hexadecimalIndexMatch}, *-? *${hexadecimalIndexMatch}\\);` +
+                        `const d *= *g\\(-? *${hexadecimalIndexMatch}, *-? *${hexadecimalIndexMatch}\\);` +
+                        `const e *= *g\\(-? *${hexadecimalIndexMatch}, *-? *${hexadecimalIndexMatch}\\);` +
+                    '}'
+                );
 
-                    let obfuscatedCode: string;
+                let obfuscatedCode: string;
+                let areSuccessEvaluations: boolean;
 
-                    before(() => {
-                        const code: string = readFileAsString(__dirname + '/fixtures/wrappers-count-const.js');
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/wrappers-count-const.js');
+                    const getObfuscatedCode = () => JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
+                            stringArray: true,
+                            stringArrayThreshold: 1,
+                            stringArrayWrappersChainedCalls: true,
+                            stringArrayWrappersCount: 1,
+                            stringArrayWrappersType: StringArrayWrappersType.Function
+                        }
+                    ).getObfuscatedCode();
 
-                        obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                            code,
-                            {
-                                ...NO_ADDITIONAL_NODES_PRESET,
-                                identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
-                                stringArray: true,
-                                stringArrayThreshold: 1,
-                                stringArrayWrappersChainedCalls: true,
-                                stringArrayWrappersCount: 1,
-                                stringArrayWrappersType: StringArrayWrappersType.Function
-                            }
-                        ).getObfuscatedCode();
-                    });
+                    obfuscatedCode = getObfuscatedCode();
+                    areSuccessEvaluations = checkCodeEvaluation(
+                        getObfuscatedCode,
+                        evaluationSamplesCount
+                    ).areSuccessEvaluations;
+                });
 
-                    it('should add correct scope calls wrappers', () => {
-                        assert.match(obfuscatedCode, stringArrayCallRegExp);
-                    });
+                it('should add correct scope calls wrappers', () => {
+                    assert.match(obfuscatedCode, stringArrayCallRegExp);
                 });
 
+                it('should evaluate code without errors', () => {
+                    assert.isTrue(areSuccessEvaluations);
+                });
+            });
+
             describe('Variant #3: no wrappers on a root scope', () => {
-                    const stringArrayCallRegExp: RegExp = new RegExp(
-                            'return e;' +
+                const stringArrayCallRegExp: RegExp = new RegExp(
+                        'return e;' +
+                    '};' +
+                    'function test *\\( *\\) *{' +
+                        'const f *= *function *\\(c, *d\\) *{' +
+                            `return b\\([cd] *-(?: -)?${hexadecimalIndexMatch}, *[cd]\\);` +
                         '};' +
-                        'function test *\\( *\\) *{' +
-                            'const f *= *function *\\(c, *d\\) *{' +
-                                `return b\\(c *-(?: -)?${hexadecimalIndexMatch}, *d\\);` +
-                            '};' +
-                            `const c *= *f\\(-? *${hexadecimalIndexMatch}\\);` +
-                            `const d *= *f\\(-? *${hexadecimalIndexMatch}\\);` +
-                            `const e *= *f\\(-? *${hexadecimalIndexMatch}\\);` +
-                        '}'
-                    );
+                        `const c *= *f\\(-? *${hexadecimalIndexMatch}, *-? *${hexadecimalIndexMatch}\\);` +
+                        `const d *= *f\\(-? *${hexadecimalIndexMatch}, *-? *${hexadecimalIndexMatch}\\);` +
+                        `const e *= *f\\(-? *${hexadecimalIndexMatch}, *-? *${hexadecimalIndexMatch}\\);` +
+                    '}'
+                );
 
-                    let obfuscatedCode: string;
+                let obfuscatedCode: string;
+                let areSuccessEvaluations: boolean;
 
-                    before(() => {
-                        const code: string = readFileAsString(__dirname + '/fixtures/wrappers-count-const-no-root-wrappers.js');
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/wrappers-count-const-no-root-wrappers.js');
+                    const getObfuscatedCode = () => JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
+                            stringArray: true,
+                            stringArrayThreshold: 1,
+                            stringArrayWrappersChainedCalls: true,
+                            stringArrayWrappersCount: 1,
+                            stringArrayWrappersType: StringArrayWrappersType.Function
+                        }
+                    ).getObfuscatedCode();
 
-                        obfuscatedCode = JavaScriptObfuscator.obfuscate(
-                            code,
-                            {
-                                ...NO_ADDITIONAL_NODES_PRESET,
-                                identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
-                                stringArray: true,
-                                stringArrayThreshold: 1,
-                                stringArrayWrappersChainedCalls: true,
-                                stringArrayWrappersCount: 1,
-                                stringArrayWrappersType: StringArrayWrappersType.Function
-                            }
-                        ).getObfuscatedCode();
-                    });
+                    obfuscatedCode = getObfuscatedCode();
+                    areSuccessEvaluations = checkCodeEvaluation(
+                        getObfuscatedCode,
+                        evaluationSamplesCount
+                    ).areSuccessEvaluations;
+                });
 
-                    it('should add correct scope calls wrappers', () => {
-                        assert.match(obfuscatedCode, stringArrayCallRegExp);
-                    });
+                it('should add correct scope calls wrappers', () => {
+                    assert.match(obfuscatedCode, stringArrayCallRegExp);
                 });
 
+                it('should evaluate code without errors', () => {
+                    assert.isTrue(areSuccessEvaluations);
+                });
+            });
+
             describe('Variant #4: correct evaluation of the string array wrappers chained calls', () => {
                 describe('Variant #1: base', () => {
                     describe('Variant #1: `Hexadecimal` identifier names generator', () => {

+ 38 - 0
test/helpers/checkCodeEvaluation.ts

@@ -0,0 +1,38 @@
+/**
+ * Evaluates code passed amount time
+ *
+ * @param {() => string} codeGetterFunction
+ * @param {number} runsAmount
+ * @param expectedResult
+ * @returns {{areSuccessEvaluations: boolean, errorMessage?: string}}
+ */
+export function checkCodeEvaluation (
+    codeGetterFunction: () => string,
+    runsAmount: number,
+    expectedResult?: any
+): {
+    areSuccessEvaluations: boolean;
+    errorMessage?: string;
+} {
+    for (let i = 0; i < runsAmount; i++) {
+        try {
+            const result = eval(codeGetterFunction());
+
+            if (expectedResult !== undefined && result !== expectedResult) {
+                return {
+                    areSuccessEvaluations: false,
+                    errorMessage: `Invalid evaluation result: ${result}. Expected: ${expectedResult}`
+                };
+            }
+        } catch (error) {
+            return {
+                areSuccessEvaluations: false,
+                errorMessage: error.message
+            };
+        }
+    }
+
+    return {
+        areSuccessEvaluations: true
+    };
+}

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác