JavaScriptObfuscatorCLI.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. import * as fs from 'fs';
  2. import * as mkdirp from 'mkdirp';
  3. import * as sinon from 'sinon';
  4. import { assert } from 'chai';
  5. import { StdoutWriteMock } from '../../mocks/StdoutWriteMock';
  6. import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscator';
  7. describe('JavaScriptObfuscatorCLI', function (): void {
  8. this.timeout(100000);
  9. const fixturesDirName: string = 'test/fixtures';
  10. const fixtureFileName: string = 'sample.js';
  11. const fixtureFilePath: string = `${fixturesDirName}/${fixtureFileName}`;
  12. const outputDirName: string = 'test/tmp';
  13. const outputFileName: string = 'sample-obfuscated.js';
  14. const outputFilePath: string = `${outputDirName}/${outputFileName}`;
  15. const configDirName: string = 'test/fixtures';
  16. const configFileName: string = 'config.js';
  17. const configFilePath: string = `${configDirName}/${configFileName}`;
  18. describe('run (): void', () => {
  19. before(() => {
  20. mkdirp.sync(outputDirName);
  21. });
  22. describe('`--output` option is set', () => {
  23. let isFileExist: boolean;
  24. before(() => {
  25. JavaScriptObfuscator.runCLI([
  26. 'node',
  27. 'javascript-obfuscator',
  28. fixtureFilePath,
  29. '--output',
  30. outputFilePath,
  31. '--compact',
  32. 'true',
  33. '--selfDefending',
  34. '0'
  35. ]);
  36. isFileExist = fs.existsSync(outputFilePath);
  37. });
  38. it('should create file with obfuscated code in `--output` directory', () => {
  39. assert.equal(isFileExist, true);
  40. });
  41. after(() => {
  42. fs.unlinkSync(outputFilePath);
  43. });
  44. });
  45. describe('`--output` option isn\'t set', () => {
  46. describe('variant #1: default behaviour', () => {
  47. let outputFixturesFilePath: string,
  48. isFileExist: boolean;
  49. before(() => {
  50. outputFixturesFilePath = `${fixturesDirName}/${outputFileName}`;
  51. JavaScriptObfuscator.runCLI([
  52. 'node',
  53. 'javascript-obfuscator',
  54. fixtureFilePath
  55. ]);
  56. isFileExist = fs.existsSync(outputFixturesFilePath);
  57. });
  58. it(`should create file \`${outputFileName}\` with obfuscated code in \`${fixturesDirName}\` directory`, () => {
  59. assert.equal(isFileExist, true);
  60. });
  61. after(() => {
  62. fs.unlinkSync(outputFixturesFilePath);
  63. });
  64. });
  65. describe('variant #2: invalid input file path', () => {
  66. const expectedError: ReferenceErrorConstructor = ReferenceError;
  67. let testFunc: () => void;
  68. before(() => {
  69. testFunc = () => JavaScriptObfuscator.runCLI([
  70. 'node',
  71. 'javascript-obfuscator',
  72. 'wrong/file/path'
  73. ]);
  74. });
  75. it(`should throw an error`, () => {
  76. assert.throws(testFunc, expectedError);
  77. });
  78. });
  79. describe('variant #3: input file extension isn\'t `.js`', () => {
  80. const expectedError: ReferenceErrorConstructor = ReferenceError;
  81. const outputFileName: string = 'sample-obfuscated.ts';
  82. const outputFilePath: string = `${outputDirName}/${outputFileName}`;
  83. let testFunc: () => void;
  84. before(() => {
  85. fs.writeFileSync(outputFilePath, 'data');
  86. testFunc = () => JavaScriptObfuscator.runCLI([
  87. 'node',
  88. 'javascript-obfuscator',
  89. outputFilePath
  90. ]);
  91. });
  92. it(`should throw an error`, () => {
  93. assert.throws(testFunc, expectedError);
  94. });
  95. after(() => {
  96. fs.unlinkSync(outputFilePath);
  97. });
  98. });
  99. });
  100. describe('`--sourceMap` option is set', () => {
  101. const outputSourceMapPath: string = `${outputFilePath}.map`;
  102. describe('variant #1: `--sourceMapMode` option value is `separate`', () => {
  103. describe('variant #1: default behaviour', () => {
  104. let isFileExist: boolean,
  105. sourceMapObject: any;
  106. before(() => {
  107. JavaScriptObfuscator.runCLI([
  108. 'node',
  109. 'javascript-obfuscator',
  110. fixtureFilePath,
  111. '--output',
  112. outputFilePath,
  113. '--compact',
  114. 'true',
  115. '--selfDefending',
  116. '0',
  117. '--sourceMap',
  118. 'true'
  119. ]);
  120. try {
  121. const content: string = fs.readFileSync(outputSourceMapPath, { encoding: 'utf8' });
  122. isFileExist = true;
  123. sourceMapObject = JSON.parse(content);
  124. } catch (e) {
  125. isFileExist = false;
  126. }
  127. });
  128. it('should create file with source map in the same directory as output file', () => {
  129. assert.equal(isFileExist, true);
  130. });
  131. it('source map from created file should contains property `version`', () => {
  132. assert.property(sourceMapObject, 'version');
  133. });
  134. it('source map from created file should contains property `sources`', () => {
  135. assert.property(sourceMapObject, 'sources');
  136. });
  137. it('source map from created file should contains property `names`', () => {
  138. assert.property(sourceMapObject, 'names');
  139. });
  140. after(() => {
  141. fs.unlinkSync(outputFilePath);
  142. fs.unlinkSync(outputSourceMapPath);
  143. });
  144. });
  145. describe('variant #2: `sourceMapBaseUrl` option is set', () => {
  146. let isFileExist: boolean,
  147. sourceMapObject: any;
  148. before(() => {
  149. JavaScriptObfuscator.runCLI([
  150. 'node',
  151. 'javascript-obfuscator',
  152. fixtureFilePath,
  153. '--output',
  154. outputFilePath,
  155. '--compact',
  156. 'true',
  157. '--selfDefending',
  158. '0',
  159. '--sourceMap',
  160. 'true',
  161. '--sourceMapBaseUrl',
  162. 'http://localhost:9000/'
  163. ]);
  164. try {
  165. const content: string = fs.readFileSync(outputSourceMapPath, { encoding: 'utf8' });
  166. isFileExist = true;
  167. sourceMapObject = JSON.parse(content);
  168. } catch (e) {
  169. isFileExist = false;
  170. }
  171. });
  172. it('should create file with source map in the same directory as output file', () => {
  173. assert.equal(isFileExist, true);
  174. });
  175. it('source map from created file should contains property `version`', () => {
  176. assert.property(sourceMapObject, 'version');
  177. });
  178. it('source map from created file should contains property `sources`', () => {
  179. assert.property(sourceMapObject, 'sources');
  180. });
  181. it('source map from created file should contains property `names`', () => {
  182. assert.property(sourceMapObject, 'names');
  183. });
  184. after(() => {
  185. fs.unlinkSync(outputFilePath);
  186. fs.unlinkSync(outputSourceMapPath);
  187. });
  188. });
  189. describe('variant #3: `--sourceMapFileName` option is set', () => {
  190. const sourceMapFileName: string = 'test';
  191. const sourceMapFilePath: string = `${sourceMapFileName}.js.map`;
  192. const outputSourceMapFilePath: string = `${outputDirName}/${sourceMapFilePath}`;
  193. let isFileExist: boolean,
  194. sourceMapObject: any;
  195. before(() => {
  196. JavaScriptObfuscator.runCLI([
  197. 'node',
  198. 'javascript-obfuscator',
  199. fixtureFilePath,
  200. '--output',
  201. outputFilePath,
  202. '--compact',
  203. 'true',
  204. '--selfDefending',
  205. '0',
  206. '--sourceMap',
  207. 'true',
  208. '--sourceMapFileName',
  209. sourceMapFileName
  210. ]);
  211. try {
  212. const content: string = fs.readFileSync(outputSourceMapFilePath, { encoding: 'utf8' });
  213. isFileExist = true;
  214. sourceMapObject = JSON.parse(content);
  215. } catch (e) {
  216. isFileExist = false;
  217. }
  218. });
  219. it('should create source map file with given name in the same directory as output file', () => {
  220. assert.equal(isFileExist, true);
  221. });
  222. it('source map from created file should contains property `version`', () => {
  223. assert.property(sourceMapObject, 'version');
  224. });
  225. it('source map from created file should contains property `sources`', () => {
  226. assert.property(sourceMapObject, 'sources');
  227. });
  228. it('source map from created file should contains property `names`', () => {
  229. assert.property(sourceMapObject, 'names');
  230. });
  231. after(() => {
  232. fs.unlinkSync(outputFilePath);
  233. fs.unlinkSync(outputSourceMapFilePath);
  234. });
  235. });
  236. });
  237. describe('variant #2: `--sourceMapMode` option is `inline`', () => {
  238. let isFileExist: boolean;
  239. before(() => {
  240. JavaScriptObfuscator.runCLI([
  241. 'node',
  242. 'javascript-obfuscator',
  243. fixtureFilePath,
  244. '--output',
  245. outputFilePath,
  246. '--compact',
  247. 'true',
  248. '--selfDefending',
  249. '0',
  250. '--sourceMap',
  251. 'true',
  252. '--sourceMapMode',
  253. 'inline'
  254. ]);
  255. isFileExist = fs.existsSync(outputSourceMapPath);
  256. });
  257. it('shouldn\'t create file with source map', () => {
  258. assert.equal(isFileExist, false);
  259. });
  260. after(() => {
  261. fs.unlinkSync(outputFilePath);
  262. });
  263. });
  264. });
  265. describe('help output', () => {
  266. let callback: sinon.SinonSpy,
  267. stdoutWriteMock: StdoutWriteMock;
  268. beforeEach(() => {
  269. callback = sinon.spy(console, 'log');
  270. stdoutWriteMock = new StdoutWriteMock(process.stdout.write);
  271. });
  272. describe('`--help` option is set', () => {
  273. let isConsoleLogCalled: boolean;
  274. beforeEach(() => {
  275. stdoutWriteMock.mute();
  276. JavaScriptObfuscator.runCLI([
  277. 'node',
  278. 'javascript-obfuscator',
  279. fixtureFilePath,
  280. '--help'
  281. ]);
  282. stdoutWriteMock.restore();
  283. isConsoleLogCalled = callback.called;
  284. });
  285. it('should print `console.log` help', () => {
  286. assert.equal(isConsoleLogCalled, true);
  287. });
  288. });
  289. describe('no arguments passed', () => {
  290. let isConsoleLogCalled: boolean;
  291. beforeEach(() => {
  292. stdoutWriteMock.mute();
  293. JavaScriptObfuscator.runCLI([
  294. 'node',
  295. 'javascript-obfuscator'
  296. ]);
  297. stdoutWriteMock.restore();
  298. isConsoleLogCalled = callback.called;
  299. });
  300. it('should print `console.log` help', () => {
  301. assert.equal(isConsoleLogCalled, true);
  302. });
  303. });
  304. afterEach(() => {
  305. callback.restore();
  306. });
  307. });
  308. describe('`--config` option is set', () => {
  309. const outputSourceMapPath: string = `${outputFilePath}.map`;
  310. let isFileExist: boolean,
  311. sourceMapObject: any;
  312. before(() => {
  313. JavaScriptObfuscator.runCLI([
  314. 'node',
  315. 'javascript-obfuscator',
  316. fixtureFilePath,
  317. '--output',
  318. outputFilePath,
  319. '--config',
  320. configFilePath
  321. ]);
  322. try {
  323. const content: string = fs.readFileSync(outputSourceMapPath, {encoding: 'utf8'});
  324. isFileExist = true;
  325. sourceMapObject = JSON.parse(content);
  326. } catch (e) {
  327. isFileExist = false;
  328. }
  329. });
  330. it('should create file with source map in the same directory as output file', () => {
  331. assert.equal(isFileExist, true);
  332. });
  333. it('source map from created file should contains property `version`', () => {
  334. assert.property(sourceMapObject, 'version');
  335. });
  336. it('source map from created file should contains property `sources`', () => {
  337. assert.property(sourceMapObject, 'sources');
  338. });
  339. it('source map from created file should contains property `names`', () => {
  340. assert.property(sourceMapObject, 'names');
  341. });
  342. after(() => {
  343. fs.unlinkSync(outputFilePath);
  344. fs.unlinkSync(outputSourceMapPath);
  345. });
  346. });
  347. describe('`--config` option is set but overridden by CLI option', () => {
  348. const outputSourceMapPath: string = `${outputFilePath}.map`;
  349. let isFileExist: boolean,
  350. sourceMapObject: any;
  351. before(() => {
  352. JavaScriptObfuscator.runCLI([
  353. 'node',
  354. 'javascript-obfuscator',
  355. fixtureFilePath,
  356. '--output',
  357. outputFilePath,
  358. '--config',
  359. configFilePath,
  360. '--sourceMap',
  361. 'false',
  362. ]);
  363. try {
  364. const content: string = fs.readFileSync(outputSourceMapPath, {encoding: 'utf8'});
  365. isFileExist = true;
  366. sourceMapObject = JSON.parse(content);
  367. } catch (e) {
  368. isFileExist = false;
  369. }
  370. });
  371. it('should create file without source map in the same directory as output file', () => {
  372. assert.equal(isFileExist, false);
  373. });
  374. after(() => {
  375. fs.unlinkSync(outputFilePath);
  376. });
  377. });
  378. after(() => {
  379. fs.rmdirSync(outputDirName);
  380. });
  381. });
  382. });