JavaScriptObfuscator.spec.ts 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. import { assert } from 'chai';
  2. import { TDictionary } from '../../../src/types/TDictionary';
  3. import { TIdentifierNamesCache } from '../../../src/types/TIdentifierNamesCache';
  4. import { TInputOptions } from '../../../src/types/options/TInputOptions';
  5. import { TOptionsPreset } from '../../../src/types/options/TOptionsPreset';
  6. import { TTypeFromEnum } from '../../../src/types/utils/TTypeFromEnum';
  7. import { IObfuscationResult } from '../../../src/interfaces/source-code/IObfuscationResult';
  8. import { SourceMapMode } from '../../../src/enums/source-map/SourceMapMode';
  9. import { StringArrayEncoding } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
  10. import { StringArrayIndexesType } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayIndexesType';
  11. import { StringArrayWrappersType } from '../../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';
  12. import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscatorFacade';
  13. import { HIGH_OBFUSCATION_PRESET } from '../../../src/options/presets/HighObfuscation';
  14. import { NO_ADDITIONAL_NODES_PRESET } from '../../../src/options/presets/NoCustomNodes';
  15. import { IdentifierNamesGenerator } from '../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  16. import { OptionsPreset } from '../../../src/enums/options/presets/OptionsPreset';
  17. import { buildLargeCode } from '../../helpers/buildLargeCode';
  18. import { getRegExpMatch } from '../../helpers/getRegExpMatch';
  19. import { readFileAsString } from '../../helpers/readFileAsString';
  20. describe('JavaScriptObfuscator', () => {
  21. describe('obfuscate', () => {
  22. describe('correct source code', () => {
  23. let obfuscatedCode: string,
  24. sourceMap: string;
  25. beforeEach(() => {
  26. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  27. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  28. code,
  29. {
  30. ...NO_ADDITIONAL_NODES_PRESET
  31. }
  32. );
  33. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  34. sourceMap = obfuscationResult.getSourceMap();
  35. });
  36. it('should return correct obfuscated code', () => {
  37. assert.isOk(obfuscatedCode);
  38. });
  39. it('should return empty source map', () => {
  40. assert.isNotOk(sourceMap);
  41. });
  42. });
  43. describe('Empty or invalid source code', () => {
  44. describe('empty source code', () => {
  45. let obfuscatedCode: string;
  46. beforeEach(() => {
  47. const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
  48. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  49. code,
  50. ).getObfuscatedCode();
  51. });
  52. it('should return an empty obfuscated code', () => {
  53. assert.isNotOk(obfuscatedCode);
  54. });
  55. });
  56. describe('empty source code with comments', () => {
  57. let obfuscatedCode: string;
  58. beforeEach(() => {
  59. const code: string = readFileAsString(__dirname + '/fixtures/comments-only.js');
  60. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  61. code,
  62. {
  63. controlFlowFlattening: true,
  64. deadCodeInjection: true
  65. }
  66. ).getObfuscatedCode();
  67. });
  68. it('should return an empty obfuscated code', () => {
  69. assert.isNotOk(obfuscatedCode);
  70. });
  71. });
  72. describe('invalid source code type', () => {
  73. let obfuscatedCode: string;
  74. beforeEach(() => {
  75. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  76. 1 as unknown as string
  77. ).getObfuscatedCode();
  78. });
  79. it('should return an empty obfuscated code', () => {
  80. assert.isNotOk(obfuscatedCode);
  81. });
  82. });
  83. });
  84. describe('`sourceMap` option is `true`', () => {
  85. describe('`sourceMapMode` is `separate`', () => {
  86. let obfuscatedCode: string,
  87. sourceMap: string;
  88. beforeEach(() => {
  89. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  90. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  91. code,
  92. {
  93. ...NO_ADDITIONAL_NODES_PRESET,
  94. sourceMap: true
  95. }
  96. );
  97. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  98. sourceMap = JSON.parse(obfuscationResult.getSourceMap()).mappings;
  99. });
  100. it('should return correct obfuscated code', () => {
  101. assert.isOk(obfuscatedCode);
  102. });
  103. it('should return correct source map', () => {
  104. assert.isOk(sourceMap);
  105. });
  106. });
  107. describe('`sourceMapMode` is `inline`', () => {
  108. const regExp: RegExp = /sourceMappingURL=data:application\/json;base64/;
  109. let obfuscatedCode: string,
  110. sourceMap: string;
  111. beforeEach(() => {
  112. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  113. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  114. code,
  115. {
  116. ...NO_ADDITIONAL_NODES_PRESET,
  117. sourceMap: true,
  118. sourceMapMode: SourceMapMode.Inline
  119. }
  120. );
  121. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  122. sourceMap = JSON.parse(obfuscationResult.getSourceMap()).mappings;
  123. });
  124. it('should return correct obfuscated code', () => {
  125. assert.isOk(obfuscatedCode);
  126. });
  127. it('should return obfuscated code with inline source map as Base64 string', () => {
  128. assert.match(obfuscatedCode, regExp);
  129. });
  130. it('should return correct source map', () => {
  131. assert.isOk(sourceMap);
  132. });
  133. });
  134. describe('empty source code', () => {
  135. let obfuscatedCode: string,
  136. sourceMapNames: string[],
  137. sourceMapSources: string[],
  138. sourceMapMappings: string;
  139. beforeEach(() => {
  140. const code: string = readFileAsString(__dirname + '/fixtures/empty-input.js');
  141. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  142. code,
  143. {
  144. sourceMap: true
  145. }
  146. );
  147. obfuscatedCode = obfuscationResult.getObfuscatedCode();
  148. const sourceMapObject: any = JSON.parse(obfuscationResult.getSourceMap());
  149. sourceMapNames = sourceMapObject.names;
  150. sourceMapSources = sourceMapObject.sources;
  151. sourceMapMappings = sourceMapObject.mappings;
  152. });
  153. it('should return empty obfuscated code', () => {
  154. assert.isNotOk(obfuscatedCode);
  155. });
  156. it('should return empty source map property `names`', () => {
  157. assert.deepEqual(sourceMapNames, []);
  158. });
  159. it('should return empty source map property `sources`', () => {
  160. assert.deepEqual(sourceMapSources, []);
  161. });
  162. it('should return empty source map property `mappings`', () => {
  163. assert.isNotOk(sourceMapMappings);
  164. });
  165. });
  166. });
  167. describe('variable inside global scope', () => {
  168. describe('Variant #1: without `renameGlobals` option', () => {
  169. const regExp: RegExp = /^var test *= *0x\d+;$/;
  170. let obfuscatedCode: string;
  171. beforeEach(() => {
  172. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  173. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  174. code,
  175. {
  176. ...NO_ADDITIONAL_NODES_PRESET
  177. }
  178. ).getObfuscatedCode();
  179. });
  180. it('should return correct obfuscated code', () => {
  181. assert.match(obfuscatedCode, regExp);
  182. });
  183. });
  184. describe('Variant #2: with `renameGlobals` option', () => {
  185. const regExp: RegExp = /^var _0x(\w){4,6} *= *0x\d+;$/;
  186. let obfuscatedCode: string;
  187. beforeEach(() => {
  188. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  189. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  190. code,
  191. {
  192. ...NO_ADDITIONAL_NODES_PRESET,
  193. renameGlobals: true
  194. }
  195. ).getObfuscatedCode();
  196. });
  197. it('should return correct obfuscated code', () => {
  198. assert.match(obfuscatedCode, regExp);
  199. });
  200. });
  201. describe('Variant #3: with `renameGlobals` and `identifiersPrefix` options', () => {
  202. const regExp: RegExp = /^var foo_0x(\w){4,6} *= *0x\d+;$/;
  203. let obfuscatedCode: string;
  204. beforeEach(() => {
  205. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  206. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  207. code,
  208. {
  209. ...NO_ADDITIONAL_NODES_PRESET,
  210. renameGlobals: true,
  211. identifiersPrefix: 'foo'
  212. }
  213. ).getObfuscatedCode();
  214. });
  215. it('should return correct obfuscated code', () => {
  216. assert.match(obfuscatedCode, regExp);
  217. });
  218. });
  219. describe('Variant #4: with `stringArray`, `renameGlobals` and `identifiersPrefix` options', () => {
  220. const stringArrayRegExp: RegExp = /^var foo_0x(\w){4} *= *\['abc'\];/;
  221. const stringArrayCallRegExp: RegExp = /var foo_0x(\w){4,6} *= *foo_0x(\w){4}\(0x0\);$/;
  222. let obfuscatedCode: string;
  223. beforeEach(() => {
  224. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  225. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  226. code,
  227. {
  228. ...NO_ADDITIONAL_NODES_PRESET,
  229. renameGlobals: true,
  230. identifiersPrefix: 'foo',
  231. stringArray: true,
  232. stringArrayThreshold: 1
  233. }
  234. ).getObfuscatedCode();
  235. });
  236. it('match #1: should return correct obfuscated code', () => {
  237. assert.match(obfuscatedCode, stringArrayRegExp);
  238. });
  239. it('match #2: should return correct obfuscated code', () => {
  240. assert.match(obfuscatedCode, stringArrayCallRegExp);
  241. });
  242. });
  243. });
  244. describe('variable inside block scope', () => {
  245. const regExp: RegExp = /^\(function *\(\) *\{ *var _0x[\w]+ *= *0x\d+; *\}(\(\)\)|\)\(\));?$/;
  246. let obfuscatedCode: string;
  247. beforeEach(() => {
  248. const code: string = readFileAsString(__dirname + '/fixtures/block-scope.js');
  249. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  250. code,
  251. {
  252. ...NO_ADDITIONAL_NODES_PRESET
  253. }
  254. ).getObfuscatedCode();
  255. });
  256. it('should return correct obfuscated code', () => {
  257. assert.match(obfuscatedCode, regExp);
  258. });
  259. });
  260. describe('variables inside global and block scopes', () => {
  261. describe('Variant #1: with `renameGlobals` and `identifiersPrefix` options', () => {
  262. const variableDeclaration1: RegExp = /var foo_0x(\w){4,6} *= *0x1;/;
  263. const variableDeclaration2: RegExp = /var foo_0x(\w){4,6} *= *0x2;/;
  264. const variableDeclaration3: RegExp = /var _0x(\w){4,6} *= *foo_0x(\w){4,6} *\+ *foo_0x(\w){4,6}/;
  265. const functionDeclaration: RegExp = /var foo_0x(\w){4,6} *= *function/;
  266. let obfuscatedCode: string;
  267. beforeEach(() => {
  268. const code: string = readFileAsString(__dirname + '/fixtures/identifiers-prefix.js');
  269. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  270. code,
  271. {
  272. ...NO_ADDITIONAL_NODES_PRESET,
  273. renameGlobals: true,
  274. identifiersPrefix: 'foo'
  275. }
  276. ).getObfuscatedCode();
  277. });
  278. it('match #1: should return correct obfuscated code', () => {
  279. assert.match(obfuscatedCode, variableDeclaration1);
  280. });
  281. it('match #2: should return correct obfuscated code', () => {
  282. assert.match(obfuscatedCode, variableDeclaration2);
  283. });
  284. it('match #3: should return correct obfuscated code', () => {
  285. assert.match(obfuscatedCode, variableDeclaration3);
  286. });
  287. it('match #4: should return correct obfuscated code', () => {
  288. assert.match(obfuscatedCode, functionDeclaration);
  289. });
  290. });
  291. });
  292. describe('latin literal variable value', () => {
  293. const stringArrayLatinRegExp: RegExp = /^var _0x(\w){4} *= *\['abc'\];/;
  294. const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\(0x0\);$/;
  295. let obfuscatedCode: string;
  296. beforeEach(() => {
  297. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  298. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  299. code,
  300. {
  301. ...NO_ADDITIONAL_NODES_PRESET,
  302. stringArray: true,
  303. stringArrayThreshold: 1
  304. }
  305. ).getObfuscatedCode();
  306. });
  307. it('match #1: should return correct obfuscated code', () => {
  308. assert.match(obfuscatedCode, stringArrayLatinRegExp);
  309. });
  310. it('match #2: should return correct obfuscated code', () => {
  311. assert.match(obfuscatedCode, stringArrayCallRegExp);
  312. });
  313. });
  314. describe('cyrillic literal variable value', () => {
  315. const stringArrayCyrillicRegExp: RegExp = /^var _0x(\w){4} *= *\['абц'\];/;
  316. const stringArrayCallRegExp: RegExp = /var test *= *_0x(\w){4}\(0x0\);$/;
  317. let obfuscatedCode: string;
  318. beforeEach(() => {
  319. const code: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
  320. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  321. code,
  322. {
  323. ...NO_ADDITIONAL_NODES_PRESET,
  324. stringArray: true,
  325. stringArrayThreshold: 1
  326. }
  327. ).getObfuscatedCode();
  328. });
  329. it('match #1: should return correct obfuscated code', () => {
  330. assert.match(obfuscatedCode, stringArrayCyrillicRegExp);
  331. });
  332. it('match #2: should return correct obfuscated code', () => {
  333. assert.match(obfuscatedCode, stringArrayCallRegExp);
  334. });
  335. });
  336. describe('seed', function () {
  337. this.timeout(60000);
  338. describe('same seed on each run', () => {
  339. const code: string = readFileAsString('./test/fixtures/sample.js');
  340. const samples: number = 100;
  341. let obfuscatedCode1: string,
  342. obfuscatedCode2: string,
  343. seed: number = 12345,
  344. equalsCount: number = 0;
  345. beforeEach(() => {
  346. for (let i: number = 0; i < samples; i++) {
  347. if (i % 20 === 0) {
  348. seed++;
  349. }
  350. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  351. code,
  352. {
  353. seed: seed
  354. }
  355. ).getObfuscatedCode();
  356. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  357. code,
  358. {
  359. seed: seed
  360. }
  361. ).getObfuscatedCode();
  362. if (obfuscatedCode1 === obfuscatedCode2) {
  363. equalsCount++;
  364. }
  365. }
  366. });
  367. it('should return same code every time with same `seed`', () => {
  368. assert.equal(equalsCount, samples);
  369. });
  370. });
  371. describe('Variant #1: different seed on each run', () => {
  372. const code: string = readFileAsString('./test/fixtures/sample.js');
  373. let obfuscatedCode1: string,
  374. obfuscatedCode2: string;
  375. beforeEach(() => {
  376. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  377. code,
  378. {
  379. seed: 12345
  380. }
  381. ).getObfuscatedCode();
  382. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  383. code,
  384. {
  385. seed: 12346
  386. }
  387. ).getObfuscatedCode();
  388. });
  389. it('should return different obfuscated code with different `seed` option value', () => {
  390. assert.notEqual(obfuscatedCode1, obfuscatedCode2);
  391. });
  392. });
  393. describe('Variant #2: different seed on each run', () => {
  394. const code: string = readFileAsString('./test/fixtures/sample.js');
  395. let obfuscatedCode1: string,
  396. obfuscatedCode2: string;
  397. beforeEach(() => {
  398. obfuscatedCode1 = JavaScriptObfuscator.obfuscate(
  399. code,
  400. {
  401. seed: 0
  402. }
  403. ).getObfuscatedCode();
  404. obfuscatedCode2 = JavaScriptObfuscator.obfuscate(
  405. code,
  406. {
  407. seed: 0
  408. }
  409. ).getObfuscatedCode();
  410. });
  411. it('should return different obfuscated code with different `seed` option value', () => {
  412. assert.notEqual(obfuscatedCode1, obfuscatedCode2);
  413. });
  414. });
  415. describe('Variant #3: same seed for different source code', () => {
  416. const code1: string = readFileAsString(__dirname + '/fixtures/simple-input-cyrillic.js');
  417. const code2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  418. const regExp: RegExp = /var (_0x(\w){4}) *= *\['.*'\];/;
  419. let match1: string,
  420. match2: string;
  421. beforeEach(() => {
  422. const obfuscatedCode1: string = JavaScriptObfuscator.obfuscate(
  423. code1,
  424. {
  425. seed: 123,
  426. stringArrayThreshold: 1
  427. }
  428. ).getObfuscatedCode();
  429. const obfuscatedCode2: string = JavaScriptObfuscator.obfuscate(
  430. code2,
  431. {
  432. seed: 123,
  433. stringArrayThreshold: 1
  434. }
  435. ).getObfuscatedCode();
  436. match1 = getRegExpMatch(obfuscatedCode1, regExp);
  437. match2 = getRegExpMatch(obfuscatedCode2, regExp);
  438. });
  439. it('should return different String Array names for different source code with same seed', () => {
  440. assert.notEqual(match1, match2);
  441. });
  442. });
  443. });
  444. /**
  445. * https://github.com/estools/escodegen/pull/408
  446. */
  447. describe('`ObjectPattern` with single `RestElement`', () => {
  448. const regExp: RegExp = /const {\.\.\.foo} *= *{};/;
  449. let obfuscatedCode: string;
  450. beforeEach(() => {
  451. const code: string = readFileAsString(__dirname + '/fixtures/object-pattern-single-rest-element.js');
  452. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  453. code,
  454. {
  455. ...NO_ADDITIONAL_NODES_PRESET
  456. }
  457. ).getObfuscatedCode();
  458. });
  459. it('should not break on `ObjectPattern` with single `RestElement`', () => {
  460. assert.match(obfuscatedCode, regExp);
  461. });
  462. });
  463. /**
  464. * https://github.com/estools/escodegen/pull/415
  465. */
  466. describe('Precedence of `SequenceExpression` in computed property', () => {
  467. const regExp: RegExp = /class Foo *{ *\[\(bar, *baz\)]\(\) *{ *} * *}/;
  468. let obfuscatedCode: string;
  469. beforeEach(() => {
  470. const code: string = readFileAsString(__dirname + '/fixtures/precedence-of-sequence-expression-in-computed-property.js');
  471. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  472. code,
  473. {
  474. ...NO_ADDITIONAL_NODES_PRESET
  475. }
  476. ).getObfuscatedCode();
  477. });
  478. it('should generate a valid js code', () => {
  479. assert.match(obfuscatedCode, regExp);
  480. });
  481. });
  482. describe('new.target MetaProperty', () => {
  483. const regExp: RegExp = /new\.target *=== *Foo/;
  484. let obfuscatedCode: string;
  485. beforeEach(() => {
  486. const code: string = readFileAsString(__dirname + '/fixtures/new-target.js');
  487. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  488. code,
  489. {
  490. ...NO_ADDITIONAL_NODES_PRESET
  491. }
  492. ).getObfuscatedCode();
  493. });
  494. it('should keep new.target MetaProperty', () => {
  495. assert.match(obfuscatedCode, regExp);
  496. });
  497. });
  498. describe('import.meta support', () => {
  499. const regExp: RegExp = /console\['log']\(import\.meta\['url']\);/;
  500. let obfuscatedCode: string;
  501. beforeEach(() => {
  502. const code: string = readFileAsString(__dirname + '/fixtures/import-meta.js');
  503. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  504. code,
  505. {
  506. ...NO_ADDITIONAL_NODES_PRESET
  507. }
  508. ).getObfuscatedCode();
  509. });
  510. it('should support `import.meta`', () => {
  511. assert.match(obfuscatedCode, regExp);
  512. });
  513. });
  514. /**
  515. * https://github.com/javascript-obfuscator/javascript-obfuscator/issues/710
  516. */
  517. describe('export * as', () => {
  518. const regExp: RegExp = /export *\* *as foo from *'bar';/;
  519. let obfuscatedCode: string;
  520. beforeEach(() => {
  521. const code: string = readFileAsString(__dirname + '/fixtures/export-all-named-support.js');
  522. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  523. code,
  524. {
  525. ...NO_ADDITIONAL_NODES_PRESET
  526. }
  527. ).getObfuscatedCode();
  528. });
  529. it('should support `export * as` syntax', () => {
  530. assert.match(obfuscatedCode, regExp);
  531. });
  532. });
  533. /**
  534. * https://github.com/estools/escodegen/pull/407
  535. */
  536. describe('valid exponentiation operator precedence', () => {
  537. const regExp: RegExp = /var foo *= *\( *0x1 *- *0x2 *\) *\*\* *0x2;/;
  538. let obfuscatedCode: string;
  539. beforeEach(() => {
  540. const code: string = readFileAsString(__dirname + '/fixtures/exponentiation-operator-precedence.js');
  541. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  542. code,
  543. {
  544. ...NO_ADDITIONAL_NODES_PRESET
  545. }
  546. ).getObfuscatedCode();
  547. });
  548. it('should support exponentiation operator', () => {
  549. assert.match(obfuscatedCode, regExp);
  550. });
  551. });
  552. describe('BigInt support', () => {
  553. const regExp: RegExp = /return 0x20000000000001n *\+ *0xan *\+ *0xan;/;
  554. let obfuscatedCode: string;
  555. beforeEach(() => {
  556. const code: string = readFileAsString(__dirname + '/fixtures/bigint-support.js');
  557. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  558. code,
  559. {
  560. ...NO_ADDITIONAL_NODES_PRESET
  561. }
  562. ).getObfuscatedCode();
  563. });
  564. it('should support BigInt', () => {
  565. assert.match(obfuscatedCode, regExp);
  566. });
  567. });
  568. describe('Optional chaining support', () => {
  569. const regExp: RegExp = new RegExp(
  570. 'const _0x(\\w){4,6} *= *{ *' +
  571. '\'bar\': *\\(\\) *=> *{} *' +
  572. '}; *' +
  573. '_0x(\\w){4,6}\\?\\.\\[\'bar\']\\?\\.\\(\\);'
  574. );
  575. let obfuscatedCode: string;
  576. beforeEach(() => {
  577. const code: string = readFileAsString(__dirname + '/fixtures/optional-chaining-support.js');
  578. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  579. code,
  580. {
  581. ...NO_ADDITIONAL_NODES_PRESET
  582. }
  583. ).getObfuscatedCode();
  584. });
  585. it('should support optional chaining', () => {
  586. assert.match(obfuscatedCode, regExp);
  587. });
  588. });
  589. describe('Nullish coalescing support', () => {
  590. const regExp: RegExp = /\(foo *\?\? *bar\) *&& *baz;/;
  591. let obfuscatedCode: string;
  592. beforeEach(() => {
  593. const code: string = readFileAsString(__dirname + '/fixtures/nullish-coalescing-support.js');
  594. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  595. code,
  596. {
  597. ...NO_ADDITIONAL_NODES_PRESET
  598. }
  599. ).getObfuscatedCode();
  600. });
  601. it('should support nullish coalescing operator', () => {
  602. assert.match(obfuscatedCode, regExp);
  603. });
  604. });
  605. describe('Numeric separators support', () => {
  606. const regExp: RegExp = /const foo *= *0x64;/;
  607. let obfuscatedCode: string;
  608. beforeEach(() => {
  609. const code: string = readFileAsString(__dirname + '/fixtures/numeric-separators-support.js');
  610. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  611. code,
  612. {
  613. ...NO_ADDITIONAL_NODES_PRESET
  614. }
  615. ).getObfuscatedCode();
  616. });
  617. it('should support numeric separators', () => {
  618. assert.match(obfuscatedCode, regExp);
  619. });
  620. });
  621. describe('Top-level await support', () => {
  622. const regExp: RegExp = /await 0x1;/;
  623. let obfuscatedCode: string;
  624. beforeEach(() => {
  625. const code: string = readFileAsString(__dirname + '/fixtures/top-level-await-support.js');
  626. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  627. code,
  628. {
  629. ...NO_ADDITIONAL_NODES_PRESET
  630. }
  631. ).getObfuscatedCode();
  632. });
  633. it('should support top-level await', () => {
  634. assert.match(obfuscatedCode, regExp);
  635. });
  636. });
  637. describe('mangled identifier names generator', () => {
  638. const regExp: RegExp = /var c *= *0x1/;
  639. let obfuscatedCode: string;
  640. beforeEach(() => {
  641. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  642. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  643. code,
  644. {
  645. ...NO_ADDITIONAL_NODES_PRESET,
  646. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  647. stringArray: true,
  648. stringArrayThreshold: 1
  649. }
  650. ).getObfuscatedCode();
  651. });
  652. it('should mangle obfuscated code', () => {
  653. assert.match(obfuscatedCode, regExp);
  654. });
  655. });
  656. describe('mangled shuffled identifier names generator', () => {
  657. const regExp: RegExp = /var [a-zA-Z] *= *0x1/;
  658. let obfuscatedCode: string;
  659. beforeEach(() => {
  660. const code: string = readFileAsString(__dirname + '/fixtures/mangle.js');
  661. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  662. code,
  663. {
  664. identifierNamesGenerator: IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator
  665. }
  666. ).getObfuscatedCode();
  667. });
  668. it('should mangle obfuscated code', () => {
  669. assert.match(obfuscatedCode, regExp);
  670. });
  671. });
  672. describe('dictionary identifier names generator', () => {
  673. const regExp1: RegExp = /var [abc] *= *0x1; *var [abc] *= *0x2; *var [abc] *= *0x3;/;
  674. const regExp2: RegExp = /var [ABC] *= *0x4; *var [ABC] *= *0x5; *var [ABC] *= *0x6;/;
  675. let obfuscatedCode: string;
  676. beforeEach(() => {
  677. const code: string = readFileAsString(__dirname + '/fixtures/dictionary-identifiers.js');
  678. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  679. code,
  680. {
  681. ...NO_ADDITIONAL_NODES_PRESET,
  682. identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  683. identifiersDictionary: ['a', 'b', 'c']
  684. }
  685. ).getObfuscatedCode();
  686. });
  687. it('Match #1: should generate identifier based on the dictionary', () => {
  688. assert.match(obfuscatedCode, regExp1);
  689. });
  690. it('Match #2: should generate identifier based on the dictionary', () => {
  691. assert.match(obfuscatedCode, regExp2);
  692. });
  693. });
  694. describe('parse module', () => {
  695. describe('Variant #1: import', () => {
  696. const importRegExp: RegExp = /import *{foo} *from *'.\/foo';/;
  697. const variableDeclarationRegExp: RegExp = /var test *= *0x1/;
  698. let obfuscatedCode: string;
  699. beforeEach(() => {
  700. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-1.js');
  701. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  702. });
  703. it('Match #1: should correctly obfuscate a import', () => {
  704. assert.match(obfuscatedCode, importRegExp);
  705. });
  706. it('Match #2: should correctly obfuscate a module', () => {
  707. assert.match(obfuscatedCode, variableDeclarationRegExp);
  708. });
  709. });
  710. describe('Variant #2: export', () => {
  711. const regExp: RegExp = /export *const foo *= *0x1;/;
  712. let obfuscatedCode: string;
  713. beforeEach(() => {
  714. const code: string = readFileAsString(__dirname + '/fixtures/parse-module-2.js');
  715. obfuscatedCode = JavaScriptObfuscator.obfuscate(code).getObfuscatedCode();
  716. });
  717. it('should correctly obfuscate a module', () => {
  718. assert.match(obfuscatedCode, regExp);
  719. });
  720. });
  721. });
  722. describe('identifier names cache generation', () => {
  723. describe('Variant #1: `identifierNamesCache` and `renameGlobal` options are enabled. Existing cache is passed', () => {
  724. const expectedIdentifierNamesCache: TIdentifierNamesCache = {
  725. globalIdentifiers: {
  726. foo: 'a',
  727. bar: 'b',
  728. baz: 'baz_value_from_cache'
  729. },
  730. propertyIdentifiers: {}
  731. };
  732. let identifierNamesCache: TIdentifierNamesCache;
  733. beforeEach(() => {
  734. const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
  735. identifierNamesCache = JavaScriptObfuscator.obfuscate(
  736. code,
  737. {
  738. ...NO_ADDITIONAL_NODES_PRESET,
  739. renameGlobals: true,
  740. identifierNamesCache: {
  741. globalIdentifiers: {
  742. baz: 'baz_value_from_cache'
  743. },
  744. propertyIdentifiers: {}
  745. },
  746. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  747. }
  748. ).getIdentifierNamesCache();
  749. });
  750. it('Match #1: should correctly generate identifier names cache', () => {
  751. assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
  752. });
  753. });
  754. describe('Variant #2: `identifierNamesCache` and `renameGlobal` options are enabled', () => {
  755. const expectedIdentifierNamesCache: TIdentifierNamesCache = {
  756. globalIdentifiers: {
  757. foo: 'a',
  758. bar: 'b'
  759. },
  760. propertyIdentifiers: {}
  761. };
  762. let identifierNamesCache: TIdentifierNamesCache;
  763. beforeEach(() => {
  764. const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
  765. identifierNamesCache = JavaScriptObfuscator.obfuscate(
  766. code,
  767. {
  768. ...NO_ADDITIONAL_NODES_PRESET,
  769. renameGlobals: true,
  770. identifierNamesCache: {
  771. globalIdentifiers: {},
  772. propertyIdentifiers: {}
  773. },
  774. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  775. }
  776. ).getIdentifierNamesCache();
  777. });
  778. it('Match #1: should correctly generate identifier names cache', () => {
  779. assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
  780. });
  781. });
  782. describe('Variant #3: `identifierNamesCache` and `renameGlobal` options are enabled. Source code without global variables', () => {
  783. const expectedIdentifierNamesCache: TIdentifierNamesCache = {
  784. globalIdentifiers: {},
  785. propertyIdentifiers: {}
  786. };
  787. let identifierNamesCache: TIdentifierNamesCache;
  788. beforeEach(() => {
  789. const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-2.js');
  790. identifierNamesCache = JavaScriptObfuscator.obfuscate(
  791. code,
  792. {
  793. ...NO_ADDITIONAL_NODES_PRESET,
  794. renameGlobals: true,
  795. identifierNamesCache: {
  796. globalIdentifiers: {},
  797. propertyIdentifiers: {}
  798. },
  799. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  800. }
  801. ).getIdentifierNamesCache();
  802. });
  803. it('Match #1: should correctly generate identifier names cache', () => {
  804. assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
  805. });
  806. });
  807. describe('Variant #4: `identifierNamesCache` option is disabled', () => {
  808. const expectedIdentifierNamesCache: TIdentifierNamesCache = null;
  809. let identifierNamesCache: TIdentifierNamesCache;
  810. beforeEach(() => {
  811. const code: string = readFileAsString(__dirname + '/fixtures/identifier-names-cache-1.js');
  812. identifierNamesCache = JavaScriptObfuscator.obfuscate(
  813. code,
  814. {
  815. ...NO_ADDITIONAL_NODES_PRESET,
  816. renameGlobals: true,
  817. identifierNamesCache: null,
  818. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  819. }
  820. ).getIdentifierNamesCache();
  821. });
  822. it('Match #1: should correctly generate identifier names cache', () => {
  823. assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
  824. });
  825. });
  826. });
  827. describe('3.5k variables', function () {
  828. this.timeout(200000);
  829. const expectedValue: number = 3500;
  830. let result: number;
  831. beforeEach(() => {
  832. const code: string = buildLargeCode(expectedValue);
  833. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  834. code,
  835. {
  836. compact: true,
  837. controlFlowFlattening: true,
  838. controlFlowFlatteningThreshold: 1,
  839. deadCodeInjection: true,
  840. deadCodeInjectionThreshold: 1,
  841. disableConsoleOutput: false,
  842. numbersToExpressions: true,
  843. simplify: true,
  844. renameProperties: true,
  845. rotateStringArray: true,
  846. stringArray: true,
  847. stringArrayEncoding: [
  848. StringArrayEncoding.Base64,
  849. StringArrayEncoding.Rc4
  850. ],
  851. stringArrayIndexesType: [
  852. StringArrayIndexesType.HexadecimalNumber,
  853. StringArrayIndexesType.HexadecimalNumericString
  854. ],
  855. stringArrayIndexShift: true,
  856. stringArrayWrappersChainedCalls: true,
  857. stringArrayWrappersCount: 10,
  858. stringArrayWrappersParametersMaxCount: 5,
  859. stringArrayWrappersType: StringArrayWrappersType.Function,
  860. stringArrayThreshold: 1,
  861. transformObjectKeys: true,
  862. unicodeEscapeSequence: false
  863. }
  864. ).getObfuscatedCode();
  865. result = eval(obfuscatedCode);
  866. });
  867. it('should correctly obfuscate 3.5k variables', () => {
  868. assert.equal(result, expectedValue);
  869. });
  870. });
  871. describe('Eval `Hello World`', function () {
  872. this.timeout(20000);
  873. const samplesCount: number = 100;
  874. const expectedEvaluationResult: string = 'Hello World';
  875. let isEvaluationSuccessful: boolean = true;
  876. before(() => {
  877. const code: string = readFileAsString(__dirname + '/fixtures/eval-hello-world.js');
  878. for (let i = 0; i < samplesCount; i++) {
  879. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
  880. code,
  881. {
  882. ...NO_ADDITIONAL_NODES_PRESET,
  883. compact: false,
  884. controlFlowFlattening: true,
  885. controlFlowFlatteningThreshold: 1,
  886. deadCodeInjection: true,
  887. deadCodeInjectionThreshold: 1,
  888. disableConsoleOutput: true,
  889. identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  890. renameProperties: true,
  891. simplify: false,
  892. stringArray: true,
  893. stringArrayThreshold: 1,
  894. stringArrayWrappersChainedCalls: true,
  895. stringArrayWrappersCount: 1,
  896. stringArrayWrappersType: StringArrayWrappersType.Variable
  897. }
  898. ).getObfuscatedCode();
  899. const evaluationResult: string = eval(obfuscatedCode);
  900. if (evaluationResult !== expectedEvaluationResult) {
  901. isEvaluationSuccessful = false;
  902. break;
  903. }
  904. }
  905. });
  906. it('should correctly evaluate obfuscated code', () => {
  907. assert.equal(isEvaluationSuccessful, true);
  908. });
  909. });
  910. describe('Identifier names collision between base code and appended string array nodes', function () {
  911. this.timeout(10000);
  912. const samplesCount: number = 30;
  913. let areCollisionsExists: boolean = false;
  914. let obfuscateFunc: (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => string;
  915. before(() => {
  916. const code: string = readFileAsString(__dirname + '/fixtures/custom-nodes-identifier-names-collision.js');
  917. obfuscateFunc = (identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
  918. const obfuscatedCode = JavaScriptObfuscator.obfuscate(
  919. code,
  920. {
  921. identifierNamesGenerator,
  922. compact: false,
  923. renameGlobals: true,
  924. identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'foozmos', 'cow', 'chikago'],
  925. stringArray: true
  926. }
  927. ).getObfuscatedCode();
  928. return obfuscatedCode;
  929. };
  930. [
  931. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  932. IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  933. ].forEach((identifierNamesGenerator: TTypeFromEnum<typeof IdentifierNamesGenerator>) => {
  934. for (let i = 0; i < samplesCount; i++) {
  935. try {
  936. eval(obfuscateFunc(identifierNamesGenerator));
  937. } catch {
  938. areCollisionsExists = true;
  939. break;
  940. }
  941. }
  942. });
  943. });
  944. it('It does not create identifier names collision', () => {
  945. assert.equal(areCollisionsExists, false);
  946. });
  947. });
  948. describe('Prevailing kind of variables', () => {
  949. const baseParams: TInputOptions = {
  950. compact: true,
  951. controlFlowFlattening: true,
  952. controlFlowFlatteningThreshold: 1,
  953. deadCodeInjection: true,
  954. deadCodeInjectionThreshold: 1,
  955. debugProtection: true,
  956. debugProtectionInterval: true,
  957. disableConsoleOutput: false,
  958. rotateStringArray: true,
  959. selfDefending: true,
  960. stringArray: true,
  961. stringArrayThreshold: 1,
  962. transformObjectKeys: true,
  963. unicodeEscapeSequence: false
  964. };
  965. describe('`var` kind', function () {
  966. let obfuscatedCode: string;
  967. before(() => {
  968. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
  969. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  970. code,
  971. {
  972. ...NO_ADDITIONAL_NODES_PRESET,
  973. ...baseParams,
  974. stringArrayEncoding: [StringArrayEncoding.Rc4]
  975. }
  976. ).getObfuscatedCode();
  977. });
  978. it('does not break on run', () => {
  979. assert.doesNotThrow(() => eval(obfuscatedCode));
  980. });
  981. });
  982. describe('`const` kind', function () {
  983. describe('Variant #1: StringArrayEncoding: rc4', () => {
  984. let obfuscatedCode: string;
  985. before(() => {
  986. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  987. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  988. code,
  989. {
  990. ...NO_ADDITIONAL_NODES_PRESET,
  991. ...baseParams,
  992. stringArrayEncoding: [StringArrayEncoding.Rc4]
  993. }
  994. ).getObfuscatedCode();
  995. });
  996. it('does not break on run', () => {
  997. assert.doesNotThrow(() => eval(obfuscatedCode));
  998. });
  999. });
  1000. describe('Variant #2: StringArrayEncoding: base64', () => {
  1001. let obfuscatedCode: string;
  1002. before(() => {
  1003. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  1004. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1005. code,
  1006. {
  1007. ...NO_ADDITIONAL_NODES_PRESET,
  1008. ...baseParams,
  1009. stringArrayEncoding: [StringArrayEncoding.Rc4]
  1010. }
  1011. ).getObfuscatedCode();
  1012. });
  1013. it('does not break on run', () => {
  1014. assert.doesNotThrow(() => eval(obfuscatedCode));
  1015. });
  1016. });
  1017. });
  1018. describe('`let` kind', function () {
  1019. describe('Variant #1: StringArrayEncoding: rc4', () => {
  1020. let obfuscatedCode: string;
  1021. before(() => {
  1022. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  1023. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1024. code,
  1025. {
  1026. ...NO_ADDITIONAL_NODES_PRESET,
  1027. ...baseParams,
  1028. stringArrayEncoding: [StringArrayEncoding.Rc4]
  1029. }
  1030. ).getObfuscatedCode();
  1031. });
  1032. it('does not break on run', () => {
  1033. assert.doesNotThrow(() => eval(obfuscatedCode));
  1034. });
  1035. });
  1036. describe('Variant #2: StringArrayEncoding: base64', () => {
  1037. let obfuscatedCode: string;
  1038. before(() => {
  1039. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  1040. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  1041. code,
  1042. {
  1043. ...NO_ADDITIONAL_NODES_PRESET,
  1044. ...baseParams,
  1045. stringArrayEncoding: [StringArrayEncoding.Base64]
  1046. }
  1047. ).getObfuscatedCode();
  1048. });
  1049. it('does not break on run', () => {
  1050. assert.doesNotThrow(() => eval(obfuscatedCode));
  1051. });
  1052. });
  1053. });
  1054. });
  1055. });
  1056. describe('obfuscateMultiple', () => {
  1057. describe('multiple source codes', () => {
  1058. const regExp1: RegExp = /var a0_0x(\w){4,6} *= *0x1;/;
  1059. const regExp2: RegExp = /var a1_0x(\w){4,6} *= *'abc';/;
  1060. let obfuscatedCode1: string;
  1061. let obfuscatedCode2: string;
  1062. beforeEach(() => {
  1063. const sourceCode1: string = readFileAsString(__dirname + '/fixtures/simple-input-1.js');
  1064. const sourceCode2: string = readFileAsString(__dirname + '/fixtures/simple-input-2.js');
  1065. const obfuscationResultsObject = JavaScriptObfuscator.obfuscateMultiple(
  1066. {
  1067. sourceCode1,
  1068. sourceCode2
  1069. },
  1070. {
  1071. ...NO_ADDITIONAL_NODES_PRESET,
  1072. renameGlobals: true
  1073. }
  1074. );
  1075. obfuscatedCode1 = obfuscationResultsObject.sourceCode1.getObfuscatedCode();
  1076. obfuscatedCode2 = obfuscationResultsObject.sourceCode2.getObfuscatedCode();
  1077. });
  1078. it('Match #1: should return correct obfuscated code', () => {
  1079. assert.match(obfuscatedCode1, regExp1);
  1080. });
  1081. it('Match #2: should return correct obfuscated code', () => {
  1082. assert.match(obfuscatedCode2, regExp2);
  1083. });
  1084. });
  1085. describe('invalid source codes object', () => {
  1086. let testFunc: () => TDictionary<IObfuscationResult>;
  1087. beforeEach(() => {
  1088. testFunc = () => JavaScriptObfuscator.obfuscateMultiple(
  1089. 'foo' as any,
  1090. {
  1091. ...NO_ADDITIONAL_NODES_PRESET,
  1092. renameGlobals: true
  1093. }
  1094. );
  1095. });
  1096. it('Should throw an error if source codes object is not a plain object', () => {
  1097. assert.throw(testFunc, Error);
  1098. });
  1099. });
  1100. });
  1101. describe('getOptionsByPreset', () => {
  1102. describe('Variant #1: base behaviour', () => {
  1103. const optionsPresetName: TOptionsPreset = OptionsPreset.HighObfuscation;
  1104. let options: TInputOptions;
  1105. before(() => {
  1106. options = JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
  1107. });
  1108. it('Should return options for passed options preset name', () => {
  1109. assert.deepEqual(options, HIGH_OBFUSCATION_PRESET);
  1110. });
  1111. });
  1112. describe('Variant #2: unknown options preset name', () => {
  1113. const optionsPresetName: TOptionsPreset = 'foobar' as TOptionsPreset;
  1114. let testFunc: () => TInputOptions;
  1115. before(() => {
  1116. testFunc = () => JavaScriptObfuscator.getOptionsByPreset(optionsPresetName);
  1117. });
  1118. it('Should throws an error when unknown option preset is passed', () => {
  1119. assert.throws(testFunc, 'Options for preset name `foobar` are not found');
  1120. });
  1121. });
  1122. });
  1123. });