DomainLockCodeHelper.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { assert } from 'chai';
  2. import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  3. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  4. import { readFileAsString } from '../../../helpers/readFileAsString';
  5. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
  6. describe('DomainLockCodeHelper', () => {
  7. describe('`domainLock` option is set', () => {
  8. describe('Variant #1: base behaviour', () => {
  9. const regExp: RegExp = /var _0x([a-f0-9]){4,6} *= *new RegExp/;
  10. let obfuscatedCode: string;
  11. before(() => {
  12. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  13. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  14. code,
  15. {
  16. ...NO_ADDITIONAL_NODES_PRESET,
  17. domainLock: ['.example.com']
  18. }
  19. ).getObfuscatedCode();
  20. });
  21. it('should correctly append code helper into the obfuscated code', () => {
  22. assert.match(obfuscatedCode, regExp);
  23. });
  24. });
  25. describe('Variant #2: identifier names when appended inside global scope', () => {
  26. const regExp: RegExp = /var foo[a-z] *= *foo[a-z]\(this, *function *\(\) *{/;
  27. let obfuscatedCode: string;
  28. before(() => {
  29. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  30. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  31. code,
  32. {
  33. ...NO_ADDITIONAL_NODES_PRESET,
  34. domainLock: ['.example.com'],
  35. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  36. identifiersPrefix: 'foo'
  37. }
  38. ).getObfuscatedCode();
  39. });
  40. it('should add prefix to the helper identifiers inside global scope', () => {
  41. assert.match(obfuscatedCode, regExp);
  42. });
  43. });
  44. describe('Variant #3: identifier names when appended inside function scope', () => {
  45. const regExp: RegExp = /var [a-z] *= *[a-z]\(this, *function *\(\) *{/;
  46. let obfuscatedCode: string;
  47. before(() => {
  48. const code: string = readFileAsString(__dirname + '/fixtures/append-inside-function-scope.js');
  49. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  50. code,
  51. {
  52. ...NO_ADDITIONAL_NODES_PRESET,
  53. domainLock: ['.example.com'],
  54. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  55. identifiersPrefix: 'foo'
  56. }
  57. ).getObfuscatedCode();
  58. });
  59. it('should not add prefix to the helper identifiers inside global scope', () => {
  60. assert.match(obfuscatedCode, regExp);
  61. });
  62. });
  63. });
  64. describe('`domainLock` option isn\'t set', () => {
  65. const regExp: RegExp = /var _0x([a-f0-9]){4,6} *= *new RegExp/;
  66. let obfuscatedCode: string;
  67. before(() => {
  68. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  69. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  70. code,
  71. {
  72. ...NO_ADDITIONAL_NODES_PRESET,
  73. domainLock: []
  74. }
  75. ).getObfuscatedCode();
  76. });
  77. it('shouldn\'t append code helper into the obfuscated code', () => {
  78. assert.notMatch(obfuscatedCode, regExp);
  79. });
  80. });
  81. });