Skip to content

Commit d237d48

Browse files
committed
tests refactoring: custom nodes
1 parent e077027 commit d237d48

File tree

5 files changed

+195
-82
lines changed

5 files changed

+195
-82
lines changed

test/functional-tests/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.spec.ts

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,59 @@ describe('ConsoleOutputDisableExpressionNode', () => {
1313
const consoleErrorRegExp: RegExp = /_0x([a-f0-9]){4,6}\['console'\]\['error'\] *= *_0x([a-f0-9]){4,6};/u;
1414
const consoleWarnRegExp: RegExp = /_0x([a-f0-9]){4,6}\['console'\]\['warn'\] *= *_0x([a-f0-9]){4,6};/u;
1515

16-
it('should correctly append `ConsoleOutputDisableExpressionNode` custom node into the obfuscated code if `disableConsoleOutput` option is set', () => {
17-
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
18-
readFileAsString(__dirname + '/fixtures/simple-input.js'),
19-
{
20-
...NO_CUSTOM_NODES_PRESET,
21-
disableConsoleOutput: true
22-
}
23-
);
24-
25-
assert.match(obfuscationResult.getObfuscatedCode(), consoleLogRegExp);
26-
assert.match(obfuscationResult.getObfuscatedCode(), consoleErrorRegExp);
27-
assert.match(obfuscationResult.getObfuscatedCode(), consoleWarnRegExp);
16+
describe('`disableConsoleOutput` option is set', () => {
17+
let obfuscatedCode: string;
18+
19+
before(() => {
20+
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
21+
readFileAsString(__dirname + '/fixtures/simple-input.js'),
22+
{
23+
...NO_CUSTOM_NODES_PRESET,
24+
disableConsoleOutput: true
25+
}
26+
);
27+
28+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
29+
});
30+
31+
it('match #1: should correctly append custom node into the obfuscated code', () => {
32+
assert.match(obfuscatedCode, consoleLogRegExp);
33+
});
34+
35+
it('match #2: should correctly append custom node into the obfuscated code', () => {
36+
assert.match(obfuscatedCode, consoleErrorRegExp);
37+
});
38+
39+
it('match #3: should correctly append custom node into the obfuscated code', () => {
40+
assert.match(obfuscatedCode, consoleWarnRegExp);
41+
});
2842
});
2943

30-
it('should\'t append `ConsoleOutputDisableExpressionNode` custom node into the obfuscated code if `disableConsoleOutput` option is not set', () => {
31-
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
32-
readFileAsString(__dirname + '/fixtures/simple-input.js'),
33-
{
34-
...NO_CUSTOM_NODES_PRESET,
35-
disableConsoleOutput: false,
36-
stringArrayThreshold: 1
37-
}
38-
);
39-
40-
assert.notMatch(obfuscationResult.getObfuscatedCode(), consoleLogRegExp);
41-
assert.notMatch(obfuscationResult.getObfuscatedCode(), consoleErrorRegExp);
42-
assert.notMatch(obfuscationResult.getObfuscatedCode(), consoleWarnRegExp);
44+
describe('`disableConsoleOutput` option isn\'t set', () => {
45+
let obfuscatedCode: string;
46+
47+
before(() => {
48+
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
49+
readFileAsString(__dirname + '/fixtures/simple-input.js'),
50+
{
51+
...NO_CUSTOM_NODES_PRESET,
52+
disableConsoleOutput: false
53+
}
54+
);
55+
56+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
57+
});
58+
59+
it('match #1: shouldn\'t append custom node into the obfuscated code', () => {
60+
assert.notMatch(obfuscatedCode, consoleLogRegExp);
61+
});
62+
63+
it('match #2: shouldn\'t append custom node into the obfuscated code', () => {
64+
assert.notMatch(obfuscatedCode, consoleErrorRegExp);
65+
});
66+
67+
it('match #3: shouldn\'t append custom node into the obfuscated code', () => {
68+
assert.notMatch(obfuscatedCode, consoleWarnRegExp);
69+
});
4370
});
4471
});

test/functional-tests/custom-nodes/domain-lock-nodes/DomainLockNode.spec.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,45 @@ import { readFileAsString } from '../../../helpers/readFileAsString';
99
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
1010

1111
describe('DomainLockNode', () => {
12-
it('should correctly append `DomainLockNode` custom node into the obfuscated code if `domainLock` option is set', () => {
13-
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
14-
readFileAsString(__dirname + '/fixtures/simple-input.js'),
15-
{
16-
...NO_CUSTOM_NODES_PRESET,
17-
domainLock: ['.example.com']
18-
}
19-
);
20-
21-
assert.match(obfuscationResult.getObfuscatedCode(), /var _0x([a-f0-9]){4,6} *= *new RegExp/);
12+
const regExp: RegExp = /var _0x([a-f0-9]){4,6} *= *new RegExp/;
13+
14+
describe('`domainLock` option is set', () => {
15+
let obfuscatedCode: string;
16+
17+
before(() => {
18+
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
19+
readFileAsString(__dirname + '/fixtures/simple-input.js'),
20+
{
21+
...NO_CUSTOM_NODES_PRESET,
22+
domainLock: ['.example.com']
23+
}
24+
);
25+
26+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
27+
});
28+
29+
it('should correctly append custom node into the obfuscated code', () => {
30+
assert.match(obfuscatedCode, regExp);
31+
});
2232
});
2333

24-
it('should\'t append `DomainLockNode` custom node into the obfuscated code if `domainLock` option is not set', () => {
25-
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
26-
readFileAsString(__dirname + '/fixtures/simple-input.js'),
27-
{
28-
...NO_CUSTOM_NODES_PRESET,
29-
domainLock: []
30-
}
31-
);
34+
describe('`domainLock` option isn\'t set', () => {
35+
let obfuscatedCode: string;
3236

33-
assert.notMatch(obfuscationResult.getObfuscatedCode(), /var _0x([a-f0-9]){4,6} *= *new RegExp/);
37+
before(() => {
38+
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
39+
readFileAsString(__dirname + '/fixtures/simple-input.js'),
40+
{
41+
...NO_CUSTOM_NODES_PRESET,
42+
domainLock: []
43+
}
44+
);
45+
46+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
47+
});
48+
49+
it('shouldn\'t append custom node into the obfuscated code', () => {
50+
assert.notMatch(obfuscatedCode, regExp);
51+
});
3452
});
3553
});

test/functional-tests/custom-nodes/string-array-nodes/StringArrayCallsWrapper.spec.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,46 @@ import { readFileAsString } from '../../../helpers/readFileAsString';
99
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
1010

1111
describe('StringArrayCallsWrapper', () => {
12-
it('should correctly append `StringArrayCallsWrapper` custom node into the obfuscated code', () => {
13-
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
14-
readFileAsString(__dirname + '/fixtures/simple-input.js'),
15-
{
16-
...NO_CUSTOM_NODES_PRESET,
17-
stringArray: true,
18-
stringArrayThreshold: 1
19-
}
20-
);
21-
22-
assert.match(
23-
obfuscationResult.getObfuscatedCode(),
24-
/_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6} *- *0x0\;/
25-
);
12+
const regExp: RegExp = /_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6} *- *0x0\;/;
13+
14+
describe('`stringArray` option is set', () => {
15+
let obfuscatedCode: string;
16+
17+
before(() => {
18+
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
19+
readFileAsString(__dirname + '/fixtures/simple-input.js'),
20+
{
21+
...NO_CUSTOM_NODES_PRESET,
22+
stringArray: true,
23+
stringArrayThreshold: 1
24+
}
25+
);
26+
27+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
28+
});
29+
30+
it('should correctly append custom node into the obfuscated code', () => {
31+
assert.match(obfuscatedCode, regExp);
32+
});
33+
});
34+
35+
describe('`stringArray` option isn\'t set', () => {
36+
let obfuscatedCode: string;
37+
38+
before(() => {
39+
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
40+
readFileAsString(__dirname + '/fixtures/simple-input.js'),
41+
{
42+
...NO_CUSTOM_NODES_PRESET,
43+
stringArray: false
44+
}
45+
);
46+
47+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
48+
});
49+
50+
it('shouldn\'t append custom node into the obfuscated code', () => {
51+
assert.notMatch(obfuscatedCode, regExp);
52+
});
2653
});
2754
});

test/functional-tests/custom-nodes/string-array-nodes/StringArrayNode.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@ import { readFileAsString } from '../../../helpers/readFileAsString';
99
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
1010

1111
describe('StringArrayNode', () => {
12+
const regExp: RegExp = /^var _0x([a-f0-9]){4} *= *\[/;
13+
14+
describe('`stringArray` option is set', () => {
15+
let obfuscatedCode: string;
16+
17+
before(() => {
18+
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
19+
readFileAsString(__dirname + '/fixtures/simple-input.js'),
20+
{
21+
...NO_CUSTOM_NODES_PRESET,
22+
stringArray: true,
23+
stringArrayThreshold: 1
24+
}
25+
);
26+
27+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
28+
});
29+
30+
it('should correctly append custom node into the obfuscated code', () => {
31+
assert.match(obfuscatedCode, regExp);
32+
});
33+
});
34+
1235
it('should correctly append `StringArrayNode` custom node into the obfuscated code if `stringArray` option is set', () => {
1336
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
1437
readFileAsString(__dirname + '/fixtures/simple-input.js'),

test/functional-tests/custom-nodes/string-array-nodes/StringArrayRotateFunctionNode.spec.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,49 @@ import { readFileAsString } from '../../../helpers/readFileAsString';
99
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
1010

1111
describe('StringArrayRotateFunctionNode', () => {
12-
it('should correctly append `StringArrayRotateFunctionNode` custom node into the obfuscated code if `rotateStringArray` option is set', () => {
13-
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
14-
readFileAsString(__dirname + '/fixtures/simple-input.js'),
15-
{
16-
...NO_CUSTOM_NODES_PRESET,
17-
rotateStringArray: true,
18-
stringArray: true,
19-
stringArrayThreshold: 1
20-
}
21-
);
22-
23-
assert.match(obfuscationResult.getObfuscatedCode(), /while *\(-- *_0x([a-f0-9]){4,6}\) *\{/);
12+
const regExp: RegExp = /while *\(-- *_0x([a-f0-9]){4,6}\) *\{/;
13+
14+
describe('`stringArray` option is set', () => {
15+
let obfuscatedCode: string;
16+
17+
before(() => {
18+
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
19+
readFileAsString(__dirname + '/fixtures/simple-input.js'),
20+
{
21+
...NO_CUSTOM_NODES_PRESET,
22+
rotateStringArray: true,
23+
stringArray: true,
24+
stringArrayThreshold: 1
25+
}
26+
);
27+
28+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
29+
});
30+
31+
it('should correctly append custom node into the obfuscated code', () => {
32+
assert.match(obfuscatedCode, regExp);
33+
});
2434
});
2535

26-
it('should\'t append `StringArrayRotateFunctionNode` custom node into the obfuscated code if `rotateStringArray` option is not set', () => {
27-
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
28-
readFileAsString(__dirname + '/fixtures/simple-input.js'),
29-
{
30-
...NO_CUSTOM_NODES_PRESET,
31-
rotateStringArray: false,
32-
stringArray: true,
33-
stringArrayThreshold: 1
34-
}
35-
);
36-
37-
assert.notMatch(obfuscationResult.getObfuscatedCode(), /while *\(-- *_0x([a-f0-9]){4,6}\) *\{/);
36+
describe('`stringArray` option isn\'t set', () => {
37+
let obfuscatedCode: string;
38+
39+
before(() => {
40+
let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
41+
readFileAsString(__dirname + '/fixtures/simple-input.js'),
42+
{
43+
...NO_CUSTOM_NODES_PRESET,
44+
rotateStringArray: false,
45+
stringArray: true,
46+
stringArrayThreshold: 1
47+
}
48+
);
49+
50+
obfuscatedCode = obfuscationResult.getObfuscatedCode();
51+
});
52+
53+
it('shouldn\'t append custom node into the obfuscated code', () => {
54+
assert.notMatch(obfuscatedCode, regExp);
55+
});
3856
});
3957
});

0 commit comments

Comments
 (0)