VariableDeclarationObfuscator.spec.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { IObfuscationResult } from "../../../src/interfaces/IObfuscationResult";
  2. import { NO_CUSTOM_NODES_PRESET } from "../../../src/preset-options/NoCustomNodesPreset";
  3. import { JavaScriptObfuscator } from "../../../src/JavaScriptObfuscator";
  4. const assert: Chai.AssertStatic = require('chai').assert;
  5. describe('VariableDeclarationObfuscator', () => {
  6. it('should obfuscate `variableDeclaration` node', () => {
  7. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  8. `
  9. function foo () {
  10. var test = 'abc';
  11. console.log(test);
  12. }
  13. `,
  14. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  15. );
  16. assert.match(obfuscationResult.getObfuscatedCode(), /var *_0x([a-z0-9]){6} *= *'\\x61\\x62\\x63';/);
  17. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){6}\);/);
  18. });
  19. it('should obfuscate variable call (`identifier` node) outside of block scope of node in which this variable was declared with `var` kind', () => {
  20. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  21. `
  22. if (true)
  23. {
  24. var test = 10;
  25. }
  26. console.log(test);
  27. `,
  28. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  29. );
  30. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){6}\);/);
  31. });
  32. it('should not obfuscate variable call (`identifier` node) outside of block scope of node in which this variable was declared with `let` kind', () => {
  33. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  34. `
  35. if (true)
  36. {
  37. let test = 10;
  38. }
  39. console.log(test);
  40. `,
  41. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  42. );
  43. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(test\);/);
  44. });
  45. describe(`variable calls before variable declaration`, () => {
  46. let obfuscationResult: IObfuscationResult;
  47. beforeEach(() => {
  48. obfuscationResult = JavaScriptObfuscator.obfuscate(
  49. `
  50. function foo () {
  51. function bar () {
  52. console.log(abc.item);
  53. }
  54. console.log(abc);
  55. var abc = {};
  56. abc.item = 15;
  57. bar();
  58. }
  59. `,
  60. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  61. );
  62. });
  63. it('should obfuscate variable call (`identifier` node) before variable declaration if this call is inside function body', () => {
  64. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){6}\['\\x69\\x74\\x65\\x6d'\]\);/);
  65. });
  66. it('should not obfuscate variable call (`identifier` node) before variable declaration', () => {
  67. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(abc\);/);
  68. });
  69. });
  70. });