JavaScriptObfuscatorRuntime.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { assert } from 'chai';
  2. import { TInputOptions } from '../../src/types/options/TInputOptions';
  3. import { StringArrayEncoding } from '../../src/enums/StringArrayEncoding';
  4. import { readFileAsString } from '../helpers/readFileAsString';
  5. import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscatorFacade';
  6. describe('JavaScriptObfuscator runtime eval', function () {
  7. const options: TInputOptions = {
  8. controlFlowFlattening: true,
  9. controlFlowFlatteningThreshold: 1,
  10. deadCodeInjection: true,
  11. deadCodeInjectionThreshold: 1,
  12. debugProtection: true,
  13. selfDefending: true,
  14. splitStrings: true,
  15. splitStringsChunkLength: 5,
  16. stringArray: true,
  17. rotateStringArray: true,
  18. stringArrayEncoding: StringArrayEncoding.Rc4,
  19. transformObjectKeys: true,
  20. unicodeEscapeSequence: true
  21. };
  22. this.timeout(100000);
  23. describe('Astring', () => {
  24. let obfuscatedCode: string;
  25. before(() => {
  26. const code: string = readFileAsString(__dirname + '/fixtures/astring.js');
  27. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  28. code,
  29. options
  30. ).getObfuscatedCode();
  31. });
  32. it('should obfuscate code without any runtime errors after obfuscation: Variant #1 astring', () => {
  33. assert.equal(
  34. eval(`
  35. ${obfuscatedCode}
  36. const code = generate({
  37. "type": "Program",
  38. "body": [
  39. {
  40. "type": "FunctionDeclaration",
  41. "id": {
  42. "type": "Identifier",
  43. "name": "test",
  44. "range": [
  45. 9,
  46. 13
  47. ]
  48. },
  49. "params": [],
  50. "body": {
  51. "type": "BlockStatement",
  52. "body": [
  53. {
  54. "type": "ReturnStatement",
  55. "argument": {
  56. "type": "Literal",
  57. "value": "foo",
  58. "raw": "'foo'",
  59. "range": [
  60. 30,
  61. 35
  62. ]
  63. },
  64. "range": [
  65. 23,
  66. 36
  67. ]
  68. }
  69. ],
  70. "range": [
  71. 17,
  72. 38
  73. ]
  74. },
  75. "generator": false,
  76. "expression": false,
  77. "async": false,
  78. "range": [
  79. 0,
  80. 38
  81. ]
  82. }
  83. ],
  84. "sourceType": "module",
  85. "range": [
  86. 0,
  87. 38
  88. ],
  89. "comments": []
  90. });
  91. eval(\`\${code} test();\`);
  92. `),
  93. 'foo'
  94. );
  95. });
  96. });
  97. describe('Sha256', () => {
  98. let obfuscatedCode: string;
  99. before(() => {
  100. const code: string = readFileAsString(__dirname + '/fixtures/sha256.js');
  101. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  102. code,
  103. options
  104. ).getObfuscatedCode();
  105. });
  106. it('should obfuscate code without any runtime errors after obfuscation: Variant #2 sha256', () => {
  107. assert.equal(
  108. eval(`${obfuscatedCode} sha256('test');`),
  109. '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
  110. );
  111. });
  112. });
  113. });