ObfuscatedCodeWriter.spec.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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', 'nested', '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. 'nested',
  82. 'test-input.js'
  83. );
  84. let outputCodePath: string;
  85. before(() => {
  86. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  87. rawInputPath,
  88. {
  89. output: rawOutputPath
  90. }
  91. );
  92. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  93. });
  94. it('should return output path that contains raw output path and actual file input path', () => {
  95. assert.equal(outputCodePath, expectedOutputCodePath);
  96. });
  97. });
  98. describe('Variant #2: directory name with dot', () => {
  99. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  100. const rawInputPath: string = path.join(tmpDirectoryPath, 'input');
  101. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'foo.bar');
  102. const expectedOutputCodePath: string = path.join(
  103. tmpDirectoryPath,
  104. 'output',
  105. 'foo.bar',
  106. 'nested',
  107. 'test-input.js'
  108. );
  109. let outputCodePath: string;
  110. before(() => {
  111. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  112. rawInputPath,
  113. {
  114. output: rawOutputPath
  115. }
  116. );
  117. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  118. });
  119. it('should return output path that contains raw output path and actual file input path', () => {
  120. assert.equal(outputCodePath, expectedOutputCodePath);
  121. });
  122. });
  123. });
  124. describe('Variant #5: Win32 environment', () => {
  125. const baseDirnamePath: string = __dirname;
  126. before(() => {
  127. mkdirp.sync(path.join(baseDirnamePath, tmpDirectoryPath, 'input', 'nested'));
  128. fs.writeFileSync(
  129. path.join(baseDirnamePath, tmpDirectoryPath, 'input', 'nested', 'test-input.js'),
  130. 'var foo = 1;'
  131. );
  132. });
  133. describe('Variant #1: raw input absolute path is a directory path, raw output absolute path is a directory path', () => {
  134. describe('Variant #1: base directory name', () => {
  135. const inputPath: string = path.join(baseDirnamePath, tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  136. const rawInputPath: string = path.join(baseDirnamePath, tmpDirectoryPath, 'input');
  137. const rawOutputPath: string = path.join(baseDirnamePath, tmpDirectoryPath, 'output');
  138. const expectedOutputCodePath: string = path.join(
  139. baseDirnamePath,
  140. tmpDirectoryPath,
  141. 'output',
  142. 'nested',
  143. 'test-input.js'
  144. );
  145. let outputCodePath: string;
  146. before(() => {
  147. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  148. rawInputPath,
  149. {
  150. output: rawOutputPath
  151. }
  152. );
  153. outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
  154. });
  155. it('should return output path that contains raw output path and actual file input path', () => {
  156. assert.equal(outputCodePath, expectedOutputCodePath);
  157. });
  158. });
  159. });
  160. after(() => {
  161. rimraf.sync(path.join(baseDirnamePath, tmpDirectoryPath));
  162. });
  163. });
  164. after(() => {
  165. rimraf.sync(tmpDirectoryPath);
  166. });
  167. });
  168. describe('getOutputSourceMapPath', () => {
  169. describe('Variant #1: output code path is a file path', () => {
  170. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  171. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  172. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  173. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js.map');
  174. let outputSourceMapPath: string;
  175. before(() => {
  176. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  177. rawInputPath,
  178. {
  179. output: rawOutputPath
  180. }
  181. );
  182. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath);
  183. });
  184. it('should return output path for source map', () => {
  185. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  186. });
  187. });
  188. describe('Variant #2: output code path is a directory path and source map file name is not set', () => {
  189. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  190. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  191. const outputCodePath: string = path.join(tmpDirectoryPath, 'output');
  192. let testFunc: () => string;
  193. before(() => {
  194. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  195. rawInputPath,
  196. {
  197. output: rawOutputPath
  198. }
  199. );
  200. testFunc = () => obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath);
  201. });
  202. it('should throw an error if output code path is a directory path and source map file name is not set', () => {
  203. assert.throws(testFunc, Error);
  204. });
  205. });
  206. describe('Variant #3: output code path with dot', () => {
  207. const rawInputPath: string = path.join(tmpDirectoryPath, 'input.with.dot', 'test-input.js');
  208. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js');
  209. const outputCodePath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js');
  210. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js.map');
  211. let outputSourceMapPath: string;
  212. before(() => {
  213. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  214. rawInputPath,
  215. {
  216. output: rawOutputPath
  217. }
  218. );
  219. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath);
  220. });
  221. it('should return output path for source map', () => {
  222. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  223. });
  224. });
  225. describe('Variant #4: source map file name without extension is set', () => {
  226. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  227. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  228. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  229. const sourceMapFileName: string = 'foo';
  230. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  231. let outputSourceMapPath: string;
  232. before(() => {
  233. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  234. rawInputPath,
  235. {
  236. output: rawOutputPath
  237. }
  238. );
  239. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  240. });
  241. it('should return output path for source map', () => {
  242. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  243. });
  244. });
  245. describe('Variant #5: output code path is a directory path and source map file name without extension is set', () => {
  246. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  247. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  248. const outputCodePath: string = path.join(tmpDirectoryPath, 'output');
  249. const sourceMapFileName: string = 'foo';
  250. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  251. let outputSourceMapPath: string;
  252. before(() => {
  253. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  254. rawInputPath,
  255. {
  256. output: rawOutputPath
  257. }
  258. );
  259. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  260. });
  261. it('should return output path for source map', () => {
  262. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  263. });
  264. });
  265. describe('Variant #6: source map file name with wrong extension is set', () => {
  266. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  267. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  268. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  269. const sourceMapFileName: string = 'foo.js';
  270. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  271. let outputSourceMapPath: string;
  272. before(() => {
  273. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  274. rawInputPath,
  275. {
  276. output: rawOutputPath
  277. }
  278. );
  279. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  280. });
  281. it('should return output path for source map', () => {
  282. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  283. });
  284. });
  285. describe('Variant #7: source map file name with valid extension is set', () => {
  286. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  287. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  288. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  289. const sourceMapFileName: string = 'foo.js.map';
  290. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  291. let outputSourceMapPath: string;
  292. before(() => {
  293. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  294. rawInputPath,
  295. {
  296. output: rawOutputPath
  297. }
  298. );
  299. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  300. });
  301. it('should return output path for source map', () => {
  302. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  303. });
  304. });
  305. describe('Variant #8: source map file name is a path', () => {
  306. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  307. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  308. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  309. const sourceMapFileName: string = path.join('parent', 'foo.js.map');
  310. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'parent', 'foo.js.map');
  311. let outputSourceMapPath: string;
  312. before(() => {
  313. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  314. rawInputPath,
  315. {
  316. output: rawOutputPath
  317. }
  318. );
  319. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  320. });
  321. it('should return output path for source map', () => {
  322. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  323. });
  324. });
  325. describe('Variant #9: Win32 environment', () => {
  326. describe('Variant #1: output code path is a directory path', () => {
  327. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  328. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  329. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output');
  330. const sourceMapFileName: string = path.join('foo');
  331. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'foo.js.map');
  332. let outputSourceMapPath: string;
  333. before(() => {
  334. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  335. rawInputPath,
  336. {
  337. output: rawOutputPath
  338. }
  339. );
  340. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  341. });
  342. it('should return output path for source map', () => {
  343. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  344. });
  345. });
  346. describe('Variant #2: source map file name is a file name without extension', () => {
  347. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  348. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  349. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  350. const sourceMapFileName: string = path.join('foo');
  351. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'foo.js.map');
  352. let outputSourceMapPath: string;
  353. before(() => {
  354. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  355. rawInputPath,
  356. {
  357. output: rawOutputPath
  358. }
  359. );
  360. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  361. });
  362. it('should return output path for source map', () => {
  363. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  364. });
  365. });
  366. describe('Variant #3: source map file name is a file name with an extension', () => {
  367. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  368. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  369. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  370. const sourceMapFileName: string = path.join('foo.js.map');
  371. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'foo.js.map');
  372. let outputSourceMapPath: string;
  373. before(() => {
  374. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  375. rawInputPath,
  376. {
  377. output: rawOutputPath
  378. }
  379. );
  380. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  381. });
  382. it('should return output path for source map', () => {
  383. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  384. });
  385. });
  386. describe('Variant #4: output path and win32 path in source map file name', () => {
  387. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  388. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  389. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  390. const sourceMapFileName: string = path.join('C:\\', 'parent', 'foo.js.map');
  391. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'parent', 'foo.js.map');
  392. let outputSourceMapPath: string;
  393. before(() => {
  394. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  395. rawInputPath,
  396. {
  397. output: rawOutputPath
  398. }
  399. );
  400. outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  401. });
  402. it('should return output path for source map', () => {
  403. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  404. });
  405. });
  406. });
  407. describe('Variant #10: empty paths', () => {
  408. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  409. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  410. let testFunc: () => string;
  411. before(() => {
  412. const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
  413. rawInputPath,
  414. {
  415. output: rawOutputPath
  416. }
  417. );
  418. testFunc = () => obfuscatedCodeWriter.getOutputSourceMapPath('', '');
  419. });
  420. it('should throw an error if output code path is empty', () => {
  421. assert.throws(testFunc, Error);
  422. });
  423. });
  424. });
  425. });