VariableDeclarationObfuscator.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. describe('wrong replacement', () => {
  71. it('shouldn\'t replace property node identifier', () => {
  72. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  73. `
  74. function foo () {
  75. var test = 'abc';
  76. var object = {
  77. test: 'cde'
  78. };
  79. console.log(test);
  80. }
  81. `,
  82. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  83. );
  84. assert.match(obfuscationResult.getObfuscatedCode(), /var _0x([a-z0-9]){6} *= *\{'\\x74\\x65\\x73\\x74/);
  85. });
  86. it('shouldn\'t replace computed member expression identifier', () => {
  87. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  88. `
  89. function foo () {
  90. var test = 'abc';
  91. var object = {
  92. 'test': 'cde'
  93. };
  94. console.log(test);
  95. console.log(object.test);
  96. }
  97. `,
  98. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  99. );
  100. assert.match(obfuscationResult.getObfuscatedCode(), /_0x([a-z0-9]){6}\['\\x74\\x65\\x73\\x74'\]/);
  101. });
  102. });
  103. });