StringArrayCallsWrapperCodeHelper.spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { assert } from 'chai';
  2. import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  3. import { StringArrayEncoding } from '../../../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
  4. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
  5. import { readFileAsString } from '../../../helpers/readFileAsString';
  6. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
  7. describe('StringArrayCallsWrapperCodeHelper', () => {
  8. const stringCallsWrapperRegExp: RegExp = /function _0x([a-f0-9]){4} *\(_0x([a-f0-9]){4,6} *,_0x([a-f0-9]){4,6}\)/;
  9. describe('`stringArray` option is set', () => {
  10. const samplesCount: number = 100;
  11. const stringArrayCallsWrapperAtFirstPositionRegExp: RegExp = new RegExp(`^${stringCallsWrapperRegExp.source}`);
  12. const variableDeclarationAtFirstPositionRegExp: RegExp = /^var test *= *_0x([a-f0-9]){4}\(0x0\);/;
  13. let obfuscatedCode: string;
  14. let stringArrayCallsWrapperAtFirstPositionMatchesCount: number = 0;
  15. let variableDeclarationAtFirstPositionMatchesCount: number = 0;
  16. before(() => {
  17. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  18. for (let i = 0; i < samplesCount; i++) {
  19. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  20. code,
  21. {
  22. ...NO_ADDITIONAL_NODES_PRESET,
  23. stringArray: true,
  24. stringArrayThreshold: 1
  25. }
  26. ).getObfuscatedCode();
  27. if (obfuscatedCode.match(stringArrayCallsWrapperAtFirstPositionRegExp)) {
  28. stringArrayCallsWrapperAtFirstPositionMatchesCount++;
  29. }
  30. if (obfuscatedCode.match(variableDeclarationAtFirstPositionRegExp)) {
  31. variableDeclarationAtFirstPositionMatchesCount++;
  32. }
  33. }
  34. });
  35. it('Match #1: should correctly append code helper into the obfuscated code at random index', () => {
  36. assert.isAbove(stringArrayCallsWrapperAtFirstPositionMatchesCount, 1);
  37. });
  38. it('Match #2: should correctly append code helper into the obfuscated code at random index', () => {
  39. assert.isAbove(variableDeclarationAtFirstPositionMatchesCount, 1);
  40. });
  41. });
  42. describe('`stringArray` option is set', () => {
  43. let obfuscatedCode: string;
  44. before(() => {
  45. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  46. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  47. code,
  48. {
  49. ...NO_ADDITIONAL_NODES_PRESET,
  50. stringArray: true,
  51. stringArrayThreshold: 1
  52. }
  53. ).getObfuscatedCode();
  54. });
  55. it('should correctly append code helper into the obfuscated code', () => {
  56. assert.match(obfuscatedCode, stringCallsWrapperRegExp);
  57. });
  58. });
  59. describe('`stringArray` option isn\'t set', () => {
  60. let obfuscatedCode: string;
  61. before(() => {
  62. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  63. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  64. code,
  65. {
  66. ...NO_ADDITIONAL_NODES_PRESET,
  67. stringArray: false
  68. }
  69. ).getObfuscatedCode();
  70. });
  71. it('shouldn\'t append code helper into the obfuscated code', () => {
  72. assert.notMatch(obfuscatedCode, stringCallsWrapperRegExp);
  73. });
  74. });
  75. describe('Preserve string array name', () => {
  76. const callsWrapperRegExp: RegExp = new RegExp(`` +
  77. `function *b *\\(c, *d\\) *{ *` +
  78. `var e *= *a\\(\\); *` +
  79. `b *= *function *\\(f, *g\\) *{` +
  80. `f *= *f *- *0x0; *` +
  81. `var h *= *e\\[f]; *` +
  82. ``);
  83. let obfuscatedCode: string;
  84. before(() => {
  85. const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
  86. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  87. code,
  88. {
  89. ...NO_ADDITIONAL_NODES_PRESET,
  90. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  91. stringArray: true,
  92. stringArrayThreshold: 1,
  93. stringArrayEncoding: [StringArrayEncoding.Base64]
  94. }
  95. ).getObfuscatedCode();
  96. });
  97. it('should preserve string array name', () => {
  98. assert.match(obfuscatedCode, callsWrapperRegExp);
  99. });
  100. });
  101. });