BlackListNodeGuard.spec.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../src/interfaces/IObfuscationResult';
  3. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
  4. import { NO_CUSTOM_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  5. import { readFileAsString } from '../../../helpers/readFileAsString';
  6. describe('BlackListNodeGuard', () => {
  7. describe('check (node: ESTree.Node): boolean', () => {
  8. describe('`\'use strict\';` operator', () => {
  9. const useStrictOperatorRegExp: RegExp = /'use *strict';/;
  10. const stringArrayLatinRegExp: RegExp = /var _0x(\w){4} *= *\['abc'\];/;
  11. const stringArrayCallRegExp: RegExp = /var *test *= *_0x(\w){4}\('0x0'\);$/;
  12. let obfuscatedCode: string;
  13. beforeEach(() => {
  14. const code: string = readFileAsString(__dirname + '/fixtures/use-strict-operator.js');
  15. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  16. code,
  17. {
  18. ...NO_CUSTOM_NODES_PRESET,
  19. stringArray: true,
  20. stringArrayThreshold: 1
  21. }
  22. );
  23. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  24. });
  25. it('match #1: shouldn\'t obfuscate `use strict` operator', () => {
  26. assert.match(obfuscatedCode, useStrictOperatorRegExp);
  27. });
  28. it('match #2: should return correct obfuscated code', () => {
  29. assert.match(obfuscatedCode, stringArrayLatinRegExp);
  30. });
  31. it('match #3: should return correct obfuscated code', () => {
  32. assert.match(obfuscatedCode, stringArrayCallRegExp);
  33. });
  34. });
  35. });
  36. });