mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Keep comments with specific keywords
This commit is contained in:
@@ -67,6 +67,10 @@ describe('JavaScriptObfuscator', () => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/comments-only.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
controlFlowFlattening: true,
|
||||
deadCodeInjection: true
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
import { assert } from 'chai';
|
||||
|
||||
import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationResult';
|
||||
|
||||
import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
|
||||
|
||||
import { readFileAsString } from '../../../../helpers/readFileAsString';
|
||||
|
||||
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
|
||||
|
||||
describe('CommentsTransformer', () => {
|
||||
describe('variant #1: simple comment without preserved words', () => {
|
||||
const regExp: RegExp = /^var *test *= *0x1;$/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/no-preserved-words.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should remove comments without preserved words', () => {
|
||||
assert.match(obfuscatedCode, regExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('variant #2: simple comment with preserved words', () => {
|
||||
const regExp: RegExp = /^\/\/ *@license *test *comment *\n*var *test *= *0x1;$/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/preserved-words.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should keep comments with preserved words', () => {
|
||||
assert.match(obfuscatedCode, regExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('variant #3: comment with preserved and non-preserved words', () => {
|
||||
const regExp: RegExp = /^\/\/ *@license *test *comment *\n*var *test *= *0x1;$/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/combined-words-1.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should keep comments with preserved words', () => {
|
||||
assert.match(obfuscatedCode, regExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('variant #4: comment with preserved and non-preserved words', () => {
|
||||
const regExp: RegExp = new RegExp(``+
|
||||
`^\\/\\*\\* *\\n` +
|
||||
` *\\* *@license *\\n` +
|
||||
` *\\* *test\\n` +
|
||||
` *\\*\\/\\n` +
|
||||
`var *test *= *0x1;` +
|
||||
`\\/\\/javascript-obfuscator\\n` +
|
||||
` *\\/\\*\\* *@preserved *\\*\\/$` +
|
||||
``);
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/combined-words-2.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should keep comments with preserved words', () => {
|
||||
assert.match(obfuscatedCode, regExp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('variant #5: only comment without preserved words', () => {
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/comments-only-1.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should remove comment without preserved words', () => {
|
||||
assert.isNotOk(obfuscatedCode);
|
||||
});
|
||||
});
|
||||
|
||||
describe('variant #5: only comment with preserved words', () => {
|
||||
const regExp: RegExp = /^\/\/ *@license$/;
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/comments-only-2.js');
|
||||
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_CUSTOM_NODES_PRESET
|
||||
}
|
||||
);
|
||||
|
||||
obfuscatedCode = obfuscationResult.getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should keep comments with preserved words', () => {
|
||||
assert.match(obfuscatedCode, regExp);
|
||||
});
|
||||
});
|
||||
});
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// test comment
|
||||
// @license test comment
|
||||
var test = 1;
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* @license
|
||||
* test
|
||||
*/
|
||||
// test
|
||||
//abc
|
||||
var test = 1;
|
||||
//cde
|
||||
//javascript-obfuscator
|
||||
/** @preserved */
|
||||
+1
@@ -0,0 +1 @@
|
||||
// test comment
|
||||
+1
@@ -0,0 +1 @@
|
||||
// @license
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// test comment
|
||||
var test = 1;
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// @license test comment
|
||||
var test = 1;
|
||||
+3
-2
@@ -61,8 +61,9 @@ import './functional-tests/node-transformers/obfuscating-transformers/labeled-st
|
||||
import './functional-tests/node-transformers/obfuscating-transformers/literal-transformer/LiteralTransformer.spec';
|
||||
import './functional-tests/node-transformers/obfuscating-transformers/object-expression-transformer/ObjectExpressionTransformer.spec';
|
||||
import './functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec';
|
||||
import './functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/black-list-obfuscating-guard/BlackListObfuscatingGuard.spec';
|
||||
import './functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/ConditionalCommentObfuscatingGuard.spec';
|
||||
import './functional-tests/node-transformers/preparing-transformers/comments-transformer/CommentsTransformer.spec';
|
||||
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/black-list-obfuscating-guard/BlackListObfuscatingGuard.spec';
|
||||
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/ConditionalCommentObfuscatingGuard.spec';
|
||||
import './functional-tests/templates/custom-nodes/debug-protection-nodes/DebufProtectionFunctionCallTemplate.spec';
|
||||
import './functional-tests/templates/custom-nodes/domain-lock-nodes/DomainLockNodeTemplate.spec';
|
||||
import './functional-tests/templates/custom-nodes/string-array-nodes/StringArrayCallsWrapperNodeTemplate.spec';
|
||||
|
||||
Reference in New Issue
Block a user