ObfuscatedCodeWriter.spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. import { assert } from 'chai';
  2. import * as fs from 'fs';
  3. import * as mkdirp from 'mkdirp';
  4. import * as path from 'path';
  5. import * as rimraf from 'rimraf';
  6. import { ObfuscatedCodeWriter } from '../../../../src/cli/utils/ObfuscatedCodeWriter';
  7. describe('ObfuscatedCodeWriter', () => {
  8. const tmpDirectoryPath: string = 'test/tmp';
  9. describe('getOutputCodePath', () => {
  10. before(() => {
  11. mkdirp.sync(path.join(tmpDirectoryPath, 'input', 'nested',));
  12. fs.writeFileSync(
  13. path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js'),
  14. 'var foo = 1;'
  15. );
  16. });
  17. describe('Variant #1: raw input path is a file path, raw output path is a file path', () => {
  18. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  19. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  20. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  21. const expectedOutputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  22. let outputCodePath: string;
  23. before(() => {
  24. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  25. rawInputPath,
  26. {
  27. output: rawOutputPath
  28. }
  29. );
  30. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  31. });
  32. it('should return output path that equals to passed output file path', () => {
  33. assert.equal(outputCodePath, expectedOutputCodePath);
  34. });
  35. });
  36. describe('Variant #2: raw input path is a file path, raw output path is a directory path', () => {
  37. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  38. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  39. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output');
  40. const expectedOutputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-input.js');
  41. let outputCodePath: string;
  42. before(() => {
  43. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  44. rawInputPath,
  45. {
  46. output: rawOutputPath
  47. }
  48. );
  49. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  50. });
  51. it('should return output path that equals to passed output directory with file name from actual file path', () => {
  52. assert.equal(outputCodePath, expectedOutputCodePath);
  53. });
  54. });
  55. describe('Variant #3: raw input path is a directory path, raw output path is a file path', () => {
  56. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  57. const rawInputPath: string = path.join(tmpDirectoryPath, 'input');
  58. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  59. let testFunc: () => string;
  60. before(() => {
  61. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  62. rawInputPath,
  63. {
  64. output: rawOutputPath
  65. }
  66. );
  67. testFunc = () => obfuscatedCodeWriter.getOutputCodePath(inputPath);
  68. });
  69. it('should throw an error if output path is a file path', () => {
  70. assert.throws(testFunc, Error);
  71. });
  72. });
  73. describe('Variant #4: raw input path is a directory path, raw output path is a directory path', () => {
  74. describe('Variant #1: base directory name', () => {
  75. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  76. const rawInputPath: string = path.join(tmpDirectoryPath, 'input');
  77. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output');
  78. const expectedOutputCodePath: string = path.join(
  79. tmpDirectoryPath,
  80. 'output',
  81. 'test-input.js'
  82. );
  83. let outputCodePath: string;
  84. before(() => {
  85. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  86. rawInputPath,
  87. {
  88. output: rawOutputPath
  89. }
  90. );
  91. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  92. });
  93. it('should return output path that contains raw output path and actual file input path', () => {
  94. assert.equal(outputCodePath, expectedOutputCodePath);
  95. });
  96. });
  97. describe('Variant #2: base nested directory name', () => {
  98. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  99. const rawInputPath: string = path.join(tmpDirectoryPath, 'input');
  100. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output');
  101. const expectedOutputCodePath: string = path.join(
  102. tmpDirectoryPath,
  103. 'output',
  104. 'nested',
  105. 'test-input.js'
  106. );
  107. let outputCodePath: string;
  108. before(() => {
  109. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  110. rawInputPath,
  111. {
  112. output: rawOutputPath
  113. }
  114. );
  115. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  116. });
  117. it('should return output path that contains raw output path and actual file input path', () => {
  118. assert.equal(outputCodePath, expectedOutputCodePath);
  119. });
  120. });
  121. describe('Variant #3: directory name with dot', () => {
  122. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  123. const rawInputPath: string = path.join(tmpDirectoryPath, 'input');
  124. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'foo.bar');
  125. const expectedOutputCodePath: string = path.join(
  126. tmpDirectoryPath,
  127. 'output',
  128. 'foo.bar',
  129. 'nested',
  130. 'test-input.js'
  131. );
  132. let outputCodePath: string;
  133. before(() => {
  134. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  135. rawInputPath,
  136. {
  137. output: rawOutputPath
  138. }
  139. );
  140. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  141. });
  142. it('should return output path that contains raw output path and actual file input path', () => {
  143. assert.equal(outputCodePath, expectedOutputCodePath);
  144. });
  145. });
  146. });
  147. describe('Variant #5: Win32 environment', () => {
  148. const baseDirnamePath: string = __dirname;
  149. before(() => {
  150. mkdirp.sync(path.join(baseDirnamePath, tmpDirectoryPath, 'input', 'nested'));
  151. fs.writeFileSync(
  152. path.join(baseDirnamePath, tmpDirectoryPath, 'input', 'nested', 'test-input.js'),
  153. 'var foo = 1;'
  154. );
  155. });
  156. describe('Variant #1: raw input absolute path is a directory path, raw output absolute path is a directory path', () => {
  157. describe('Variant #1: base directory name', () => {
  158. const inputPath: string = path.join(baseDirnamePath, tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  159. const rawInputPath: string = path.join(baseDirnamePath, tmpDirectoryPath, 'input');
  160. const rawOutputPath: string = path.join(baseDirnamePath, tmpDirectoryPath, 'output');
  161. const expectedOutputCodePath: string = path.join(
  162. baseDirnamePath,
  163. tmpDirectoryPath,
  164. 'output',
  165. 'nested',
  166. 'test-input.js'
  167. );
  168. let outputCodePath: string;
  169. before(() => {
  170. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  171. rawInputPath,
  172. {
  173. output: rawOutputPath
  174. }
  175. );
  176. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  177. });
  178. it('should return output path that contains raw output path and actual file input path', () => {
  179. assert.equal(outputCodePath, expectedOutputCodePath);
  180. });
  181. });
  182. });
  183. after(() => {
  184. rimraf.sync(path.join(baseDirnamePath, tmpDirectoryPath));
  185. });
  186. });
  187. after(() => {
  188. rimraf.sync(tmpDirectoryPath);
  189. });
  190. });
  191. describe('getOutputSourceMapPath', () => {
  192. describe('Variant #1: output code path is a file path', () => {
  193. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  194. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  195. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  196. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js.map');
  197. let outputSourceMapPath: string;
  198. before(() => {
  199. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  200. rawInputPath,
  201. {
  202. output: rawOutputPath
  203. }
  204. );
  205. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath);
  206. });
  207. it('should return output path for source map', () => {
  208. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  209. });
  210. });
  211. describe('Variant #2: output code path is a directory path and source map file name is not set', () => {
  212. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  213. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  214. const outputCodePath: string = path.join(tmpDirectoryPath, 'output');
  215. let testFunc: () => string;
  216. before(() => {
  217. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  218. rawInputPath,
  219. {
  220. output: rawOutputPath
  221. }
  222. );
  223. testFunc = () => obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath);
  224. });
  225. it('should throw an error if output code path is a directory path and source map file name is not set', () => {
  226. assert.throws(testFunc, Error);
  227. });
  228. });
  229. describe('Variant #3: output code path with dot', () => {
  230. const rawInputPath: string = path.join(tmpDirectoryPath, 'input.with.dot', 'test-input.js');
  231. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js');
  232. const outputCodePath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js');
  233. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js.map');
  234. let outputSourceMapPath: string;
  235. before(() => {
  236. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  237. rawInputPath,
  238. {
  239. output: rawOutputPath
  240. }
  241. );
  242. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath);
  243. });
  244. it('should return output path for source map', () => {
  245. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  246. });
  247. });
  248. describe('Variant #4: source map file name without extension is set', () => {
  249. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  250. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  251. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  252. const sourceMapFileName: string = 'foo';
  253. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  254. let outputSourceMapPath: string;
  255. before(() => {
  256. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  257. rawInputPath,
  258. {
  259. output: rawOutputPath
  260. }
  261. );
  262. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  263. });
  264. it('should return output path for source map', () => {
  265. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  266. });
  267. });
  268. describe('Variant #5: output code path is a directory path and source map file name without extension is set', () => {
  269. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  270. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  271. const outputCodePath: string = path.join(tmpDirectoryPath, 'output');
  272. const sourceMapFileName: string = 'foo';
  273. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  274. let outputSourceMapPath: string;
  275. before(() => {
  276. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  277. rawInputPath,
  278. {
  279. output: rawOutputPath
  280. }
  281. );
  282. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  283. });
  284. it('should return output path for source map', () => {
  285. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  286. });
  287. });
  288. describe('Variant #6: source map file name with wrong extension is set', () => {
  289. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  290. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  291. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  292. const sourceMapFileName: string = 'foo.js';
  293. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  294. let outputSourceMapPath: string;
  295. before(() => {
  296. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  297. rawInputPath,
  298. {
  299. output: rawOutputPath
  300. }
  301. );
  302. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  303. });
  304. it('should return output path for source map', () => {
  305. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  306. });
  307. });
  308. describe('Variant #7: source map file name with valid extension is set', () => {
  309. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  310. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  311. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  312. const sourceMapFileName: string = 'foo.js.map';
  313. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  314. let outputSourceMapPath: string;
  315. before(() => {
  316. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  317. rawInputPath,
  318. {
  319. output: rawOutputPath
  320. }
  321. );
  322. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  323. });
  324. it('should return output path for source map', () => {
  325. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  326. });
  327. });
  328. describe('Variant #8: source map file name is a path', () => {
  329. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  330. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  331. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  332. const sourceMapFileName: string = path.join('parent', 'foo.js.map');
  333. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'parent', 'foo.js.map');
  334. let outputSourceMapPath: string;
  335. before(() => {
  336. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  337. rawInputPath,
  338. {
  339. output: rawOutputPath
  340. }
  341. );
  342. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  343. });
  344. it('should return output path for source map', () => {
  345. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  346. });
  347. });
  348. describe('Variant #9: Win32 environment', () => {
  349. describe('Variant #1: output code path is a directory path', () => {
  350. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  351. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  352. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output');
  353. const sourceMapFileName: string = path.join('foo');
  354. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'foo.js.map');
  355. let outputSourceMapPath: string;
  356. before(() => {
  357. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  358. rawInputPath,
  359. {
  360. output: rawOutputPath
  361. }
  362. );
  363. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  364. });
  365. it('should return output path for source map', () => {
  366. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  367. });
  368. });
  369. describe('Variant #2: source map file name is a file name without extension', () => {
  370. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  371. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  372. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  373. const sourceMapFileName: string = path.join('foo');
  374. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'foo.js.map');
  375. let outputSourceMapPath: string;
  376. before(() => {
  377. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  378. rawInputPath,
  379. {
  380. output: rawOutputPath
  381. }
  382. );
  383. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  384. });
  385. it('should return output path for source map', () => {
  386. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  387. });
  388. });
  389. describe('Variant #3: source map file name is a file name with an extension', () => {
  390. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  391. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  392. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  393. const sourceMapFileName: string = path.join('foo.js.map');
  394. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'foo.js.map');
  395. let outputSourceMapPath: string;
  396. before(() => {
  397. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  398. rawInputPath,
  399. {
  400. output: rawOutputPath
  401. }
  402. );
  403. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  404. });
  405. it('should return output path for source map', () => {
  406. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  407. });
  408. });
  409. describe('Variant #4: output path and win32 path in source map file name', () => {
  410. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  411. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  412. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  413. const sourceMapFileName: string = path.join('C:\\', 'parent', 'foo.js.map');
  414. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'parent', 'foo.js.map');
  415. let outputSourceMapPath: string;
  416. before(() => {
  417. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  418. rawInputPath,
  419. {
  420. output: rawOutputPath
  421. }
  422. );
  423. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  424. });
  425. it('should return output path for source map', () => {
  426. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  427. });
  428. });
  429. });
  430. describe('Variant #10: empty paths', () => {
  431. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  432. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  433. let testFunc: () => string;
  434. before(() => {
  435. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  436. rawInputPath,
  437. {
  438. output: rawOutputPath
  439. }
  440. );
  441. testFunc = () => obfuscatedCodeWriter.getOutputSourceMapPath('', '');
  442. });
  443. it('should throw an error if output code path is empty', () => {
  444. assert.throws(testFunc, Error);
  445. });
  446. });
  447. });
  448. });