LabeledStatementObfuscator.spec.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../src/interfaces/IObfuscationResult';
  3. import { NO_CUSTOM_NODES_PRESET } from '../../../../src/preset-options/NoCustomNodesPreset';
  4. import { readFileAsString } from '../../../helpers/readFileAsString';
  5. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
  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. {
  13. ...NO_CUSTOM_NODES_PRESET
  14. }
  15. );
  16. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  17. const labeledStatementRegExp: RegExp = /(_0x([a-z0-9]){4,6}): *\{/;
  18. const continueStatementRegExp: RegExp = /continue *(_0x([a-z0-9]){4,6});/;
  19. const breakStatementRegExp: RegExp = /break *(_0x([a-z0-9]){4,6});/;
  20. it('should obfuscate `labeledStatement` identifier', () => {
  21. assert.match(obfuscatedCode, labeledStatementRegExp);
  22. });
  23. it('should obfuscate `continueStatement` identifier', () => {
  24. assert.match(obfuscatedCode, continueStatementRegExp);
  25. });
  26. it('should obfuscate `breakStatement` identifier', () => {
  27. assert.match(obfuscatedCode, breakStatementRegExp);
  28. });
  29. it('`labeledStatement` identifier name and `labeledStatement` body `breakStatement` should be same', () => {
  30. const firstMatchArray: RegExpMatchArray|null = obfuscatedCode.match(labeledStatementRegExp);
  31. const secondMatchArray: RegExpMatchArray|null = obfuscatedCode.match(continueStatementRegExp);
  32. const thirdMatchArray: RegExpMatchArray|null = obfuscatedCode.match(breakStatementRegExp);
  33. const firstMatch: string|undefined = firstMatchArray ? firstMatchArray[1] : undefined;
  34. const secondMatch: string|undefined = secondMatchArray ? secondMatchArray[1] : undefined;
  35. const thirdMatch: string|undefined = thirdMatchArray ? thirdMatchArray[1] : undefined;
  36. assert.isOk(firstMatch);
  37. assert.isOk(secondMatch);
  38. assert.isOk(thirdMatchArray);
  39. assert.equal(firstMatch, secondMatch);
  40. assert.equal(secondMatch, thirdMatch);
  41. });
  42. });
  43. });