LabeledStatementObfuscator.spec.ts 2.5 KB

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