LabeledStatementObfuscator.spec.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { IObfuscationResult } from '../../../src/interfaces/IObfuscationResult';
  2. import { NO_CUSTOM_NODES_PRESET } from '../../../src/preset-options/NoCustomNodesPreset';
  3. import { readFileAsString } from '../../helpers/readFileAsString';
  4. import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscator';
  5. const assert: Chai.AssertStatic = require('chai').assert;
  6. describe('LabeledStatementObfuscator', () => {
  7. describe('obfuscateNode (labeledStatementNode: ESTree.LabeledStatement): void', () => {
  8. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  9. readFileAsString('./test/fixtures/node-obfuscators/labeled-statement-obfuscator/labeled-statement-obfuscator.js'),
  10. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  11. );
  12. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  13. const labeledStatementRegExp: RegExp = /(_0x([a-z0-9]){4,6}): *\{/;
  14. const continueStatementRegExp: RegExp = /continue *(_0x([a-z0-9]){4,6});/;
  15. const breakStatementRegExp: RegExp = /break *(_0x([a-z0-9]){4,6});/;
  16. it('should obfuscate `labeledStatement` identifier', () => {
  17. assert.match(obfuscatedCode, labeledStatementRegExp);
  18. });
  19. it('should obfuscate `continueStatement` identifier', () => {
  20. assert.match(obfuscatedCode, continueStatementRegExp);
  21. });
  22. it('should obfuscate `breakStatement` identifier', () => {
  23. assert.match(obfuscatedCode, breakStatementRegExp);
  24. });
  25. it('`labeledStatement` identifier name and `labeledStatement` body `breakStatement` should be same', () => {
  26. const firstMatchArray: RegExpMatchArray|null = obfuscatedCode.match(labeledStatementRegExp);
  27. const secondMatchArray: RegExpMatchArray|null = obfuscatedCode.match(continueStatementRegExp);
  28. const thirdMatchArray: RegExpMatchArray|null = obfuscatedCode.match(breakStatementRegExp);
  29. const firstMatch: string|undefined = firstMatchArray ? firstMatchArray[1] : undefined;
  30. const secondMatch: string|undefined = secondMatchArray ? secondMatchArray[1] : undefined;
  31. const thirdMatch: string|undefined = thirdMatchArray ? thirdMatchArray[1] : undefined;
  32. assert.isOk(firstMatch);
  33. assert.isOk(secondMatch);
  34. assert.isOk(thirdMatchArray);
  35. assert.equal(firstMatch, secondMatch);
  36. assert.equal(secondMatch, thirdMatch);
  37. });
  38. });
  39. });