DeadCodeInjectionTransformer.spec.ts 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../src/interfaces/IObfuscationResult';
  3. import { NO_CUSTOM_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  4. import { readFileAsString } from '../../../helpers/readFileAsString';
  5. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
  6. describe('DeadCodeInjectionTransformer', () => {
  7. const variableMatch: string = '_0x([a-f0-9]){4,6}';
  8. describe('transformNode (programNode: ESTree.Program, parentNode: ESTree.Node): ESTree.Node', () => {
  9. describe('variant #1 - 5 simple block statements', () => {
  10. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  11. readFileAsString(__dirname + '/fixtures/input-1.js'),
  12. {
  13. ...NO_CUSTOM_NODES_PRESET,
  14. deadCodeInjection: true,
  15. deadCodeInjectionThreshold: 1,
  16. stringArray: true,
  17. stringArrayThreshold: 1
  18. }
  19. );
  20. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  21. const deadCodeMatch: string = `` +
  22. `if *\\(${variableMatch}\\('0x[a-z0-9]'\\) *[=|!]== *${variableMatch}\\('0x[a-z0-9]'\\)\\) *\\{`+
  23. `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
  24. `\\} *else *\\{`+
  25. `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
  26. `\\}` +
  27. ``;
  28. const regexp: RegExp = new RegExp(deadCodeMatch, 'g');
  29. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  30. it('should replace block statements with condition with original block statements and dead code', () => {
  31. assert.isNotNull(matches);
  32. assert.equal(matches.length, 5);
  33. });
  34. });
  35. describe('variant #2 - 4 simple block statements', () => {
  36. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  37. readFileAsString(__dirname + '/fixtures/block-statements-min-count.js'),
  38. {
  39. ...NO_CUSTOM_NODES_PRESET,
  40. deadCodeInjection: true,
  41. deadCodeInjectionThreshold: 1,
  42. stringArray: true,
  43. stringArrayThreshold: 1
  44. }
  45. );
  46. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  47. const codeMatch: string = `` +
  48. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  49. `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
  50. `\\};` +
  51. ``;
  52. const regexp: RegExp = new RegExp(codeMatch, 'g');
  53. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  54. it('shouldn\'t add dead code if block statements count is less than 5', () => {
  55. assert.isNotNull(matches);
  56. assert.equal(matches.length, 4);
  57. });
  58. });
  59. describe('variant #3 - deadCodeInjectionThreshold: 0', () => {
  60. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  61. readFileAsString(__dirname + '/fixtures/input-1.js'),
  62. {
  63. ...NO_CUSTOM_NODES_PRESET,
  64. deadCodeInjection: true,
  65. deadCodeInjectionThreshold: 0,
  66. stringArray: true,
  67. stringArrayThreshold: 1
  68. }
  69. );
  70. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  71. const codeMatch: string = `` +
  72. `var *${variableMatch} *= *function *\\(\\) *\\{` +
  73. `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
  74. `\\};` +
  75. ``;
  76. const regexp: RegExp = new RegExp(codeMatch, 'g');
  77. const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
  78. it('shouldn\'t add dead code if `deadCodeInjectionThreshold` option value is `0`', () => {
  79. assert.isNotNull(matches);
  80. assert.equal(matches.length, 5);
  81. });
  82. });
  83. });
  84. });