JavaScriptObfuscator.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { IObfuscationResult } from "../../src/interfaces/IObfuscationResult";
  2. import { JavaScriptObfuscator } from "../../src/JavaScriptObfuscator";
  3. import { NO_CUSTOM_NODES_PRESET } from "../../src/preset-options/NoCustomNodesPreset";
  4. const assert: Chai.AssertStatic = require('chai').assert;
  5. describe('JavaScriptObfuscator', () => {
  6. describe('obfuscate (sourceCode: string, customOptions?: IOptionsPreset): IObfuscationResult', () => {
  7. describe('if `sourceMap` option is `false`', () => {
  8. it('should returns object with obfuscated code and empty source map', () => {
  9. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  10. `var test = 1;`,
  11. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  12. );
  13. assert.isOk(obfuscationResult.getObfuscatedCode());
  14. assert.isNotOk(obfuscationResult.getSourceMap());
  15. });
  16. });
  17. describe('if `sourceMap` option is `true`', () => {
  18. it('should returns object with obfuscated code and source map', () => {
  19. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  20. `var test = 1;`,
  21. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  22. sourceMap: true
  23. })
  24. );
  25. assert.isOk(obfuscationResult.getObfuscatedCode());
  26. assert.isOk(JSON.parse(obfuscationResult.getSourceMap()).mappings);
  27. });
  28. it('should returns object with obfuscated code with inline source map and empty `sourceMap` if `sourceMapMode` is `inline', () => {
  29. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  30. `var test = 1;`,
  31. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  32. sourceMap: true,
  33. sourceMapMode: 'inline'
  34. })
  35. );
  36. assert.isOk(obfuscationResult.getObfuscatedCode());
  37. assert.match(
  38. obfuscationResult.getObfuscatedCode(),
  39. /sourceMappingURL=data:application\/json;base64/
  40. );
  41. assert.isNotOk(obfuscationResult.getSourceMap());
  42. });
  43. it('should returns object with empty obfuscated code and source map with empty data if source code is empty', () => {
  44. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  45. '',
  46. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  47. sourceMap: true
  48. })
  49. );
  50. assert.isNotOk(obfuscationResult.getObfuscatedCode());
  51. assert.deepEqual(JSON.parse(obfuscationResult.getSourceMap()).names, []);
  52. assert.deepEqual(JSON.parse(obfuscationResult.getSourceMap()).sources, []);
  53. assert.isNotOk(JSON.parse(obfuscationResult.getSourceMap()).mappings);
  54. });
  55. });
  56. it('should returns empty string if source code is empty', () => {
  57. assert.isNotOk(
  58. JavaScriptObfuscator.obfuscate(
  59. '',
  60. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  61. ).getObfuscatedCode()
  62. );
  63. });
  64. it('should obfuscate simple code with variable inside global scope', () => {
  65. assert.match(
  66. JavaScriptObfuscator.obfuscate(
  67. `var test = 1;`,
  68. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  69. ).getObfuscatedCode(),
  70. /^var *test *= *0x\d+;$/
  71. );
  72. });
  73. it('should obfuscate simple code with variable inside block-scope', () => {
  74. assert.match(
  75. JavaScriptObfuscator.obfuscate(
  76. `(function () {var test = 1;})()`,
  77. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  78. ).getObfuscatedCode(),
  79. /^\(function *\(\) *\{ *var *_0x[\w]+ *= *0x\d+; *\}(\(\)\)|\)\(\));?$/
  80. );
  81. });
  82. it('should obfuscate simple code with literal variable value', () => {
  83. let pattern: RegExp = /^var _0x(\w){4} *= *\['(\\[x|u]\d+)+'\]; *var *test *= *_0x(\w){4}\[0x0\];$/;
  84. assert.match(
  85. JavaScriptObfuscator.obfuscate(
  86. `var test = 'abc';`,
  87. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  88. unicodeArray: true,
  89. unicodeArrayThreshold: 1
  90. })
  91. ).getObfuscatedCode(),
  92. pattern
  93. );
  94. assert.match(
  95. JavaScriptObfuscator.obfuscate(
  96. `var test = 'абц';`,
  97. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  98. unicodeArray: true,
  99. unicodeArrayThreshold: 1
  100. })
  101. ).getObfuscatedCode(),
  102. pattern
  103. );
  104. });
  105. });
  106. });