asar-spec.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. const assert = require('assert')
  2. const ChildProcess = require('child_process')
  3. const { expect } = require('chai')
  4. const fs = require('fs')
  5. const path = require('path')
  6. const temp = require('temp').track()
  7. const util = require('util')
  8. const { closeWindow } = require('./window-helpers')
  9. const nativeImage = require('electron').nativeImage
  10. const remote = require('electron').remote
  11. const { ipcMain, BrowserWindow } = remote
  12. const features = process.electronBinding('features')
  13. async function expectToThrowErrorWithCode (func, code) {
  14. let error
  15. try {
  16. await func()
  17. } catch (e) {
  18. error = e
  19. }
  20. expect(error).is.an('Error')
  21. expect(error).to.have.property('code').which.equals(code)
  22. }
  23. describe('asar package', function () {
  24. const fixtures = path.join(__dirname, 'fixtures')
  25. const asarDir = path.join(fixtures, 'test.asar')
  26. describe('node api', function () {
  27. it('supports paths specified as a Buffer', function () {
  28. const file = Buffer.from(path.join(asarDir, 'a.asar', 'file1'))
  29. assert.strictEqual(fs.existsSync(file), true)
  30. })
  31. describe('fs.readFileSync', function () {
  32. it('does not leak fd', function () {
  33. let readCalls = 1
  34. while (readCalls <= 10000) {
  35. fs.readFileSync(path.join(process.resourcesPath, 'electron.asar', 'renderer', 'api', 'ipc-renderer.js'))
  36. readCalls++
  37. }
  38. })
  39. it('reads a normal file', function () {
  40. const file1 = path.join(asarDir, 'a.asar', 'file1')
  41. assert.strictEqual(fs.readFileSync(file1).toString().trim(), 'file1')
  42. const file2 = path.join(asarDir, 'a.asar', 'file2')
  43. assert.strictEqual(fs.readFileSync(file2).toString().trim(), 'file2')
  44. const file3 = path.join(asarDir, 'a.asar', 'file3')
  45. assert.strictEqual(fs.readFileSync(file3).toString().trim(), 'file3')
  46. })
  47. it('reads from a empty file', function () {
  48. const file = path.join(asarDir, 'empty.asar', 'file1')
  49. const buffer = fs.readFileSync(file)
  50. assert.strictEqual(buffer.length, 0)
  51. assert.strictEqual(buffer.toString(), '')
  52. })
  53. it('reads a linked file', function () {
  54. const p = path.join(asarDir, 'a.asar', 'link1')
  55. assert.strictEqual(fs.readFileSync(p).toString().trim(), 'file1')
  56. })
  57. it('reads a file from linked directory', function () {
  58. const p1 = path.join(asarDir, 'a.asar', 'link2', 'file1')
  59. assert.strictEqual(fs.readFileSync(p1).toString().trim(), 'file1')
  60. const p2 = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  61. assert.strictEqual(fs.readFileSync(p2).toString().trim(), 'file1')
  62. })
  63. it('throws ENOENT error when can not find file', function () {
  64. const p = path.join(asarDir, 'a.asar', 'not-exist')
  65. const throws = function () {
  66. fs.readFileSync(p)
  67. }
  68. assert.throws(throws, /ENOENT/)
  69. })
  70. it('passes ENOENT error to callback when can not find file', function () {
  71. const p = path.join(asarDir, 'a.asar', 'not-exist')
  72. let async = false
  73. fs.readFile(p, function (e) {
  74. assert(async)
  75. assert(/ENOENT/.test(e))
  76. })
  77. async = true
  78. })
  79. it('reads a normal file with unpacked files', function () {
  80. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  81. assert.strictEqual(fs.readFileSync(p).toString().trim(), 'a')
  82. })
  83. })
  84. describe('fs.readFile', function () {
  85. it('reads a normal file', function (done) {
  86. const p = path.join(asarDir, 'a.asar', 'file1')
  87. fs.readFile(p, function (err, content) {
  88. assert.strictEqual(err, null)
  89. assert.strictEqual(String(content).trim(), 'file1')
  90. done()
  91. })
  92. })
  93. it('reads from a empty file', function (done) {
  94. const p = path.join(asarDir, 'empty.asar', 'file1')
  95. fs.readFile(p, function (err, content) {
  96. assert.strictEqual(err, null)
  97. assert.strictEqual(String(content), '')
  98. done()
  99. })
  100. })
  101. it('reads from a empty file with encoding', function (done) {
  102. const p = path.join(asarDir, 'empty.asar', 'file1')
  103. fs.readFile(p, 'utf8', function (err, content) {
  104. assert.strictEqual(err, null)
  105. assert.strictEqual(content, '')
  106. done()
  107. })
  108. })
  109. it('reads a linked file', function (done) {
  110. const p = path.join(asarDir, 'a.asar', 'link1')
  111. fs.readFile(p, function (err, content) {
  112. assert.strictEqual(err, null)
  113. assert.strictEqual(String(content).trim(), 'file1')
  114. done()
  115. })
  116. })
  117. it('reads a file from linked directory', function (done) {
  118. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  119. fs.readFile(p, function (err, content) {
  120. assert.strictEqual(err, null)
  121. assert.strictEqual(String(content).trim(), 'file1')
  122. done()
  123. })
  124. })
  125. it('throws ENOENT error when can not find file', function (done) {
  126. const p = path.join(asarDir, 'a.asar', 'not-exist')
  127. fs.readFile(p, function (err) {
  128. assert.strictEqual(err.code, 'ENOENT')
  129. done()
  130. })
  131. })
  132. })
  133. describe('fs.promises.readFile', function () {
  134. it('reads a normal file', async function () {
  135. const p = path.join(asarDir, 'a.asar', 'file1')
  136. const content = await fs.promises.readFile(p)
  137. assert.strictEqual(String(content).trim(), 'file1')
  138. })
  139. it('reads from a empty file', async function () {
  140. const p = path.join(asarDir, 'empty.asar', 'file1')
  141. const content = await fs.promises.readFile(p)
  142. assert.strictEqual(String(content), '')
  143. })
  144. it('reads from a empty file with encoding', async function () {
  145. const p = path.join(asarDir, 'empty.asar', 'file1')
  146. const content = await fs.promises.readFile(p, 'utf8')
  147. assert.strictEqual(content, '')
  148. })
  149. it('reads a linked file', async function () {
  150. const p = path.join(asarDir, 'a.asar', 'link1')
  151. const content = await fs.promises.readFile(p)
  152. assert.strictEqual(String(content).trim(), 'file1')
  153. })
  154. it('reads a file from linked directory', async function () {
  155. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  156. const content = await fs.promises.readFile(p)
  157. assert.strictEqual(String(content).trim(), 'file1')
  158. })
  159. it('throws ENOENT error when can not find file', async function () {
  160. const p = path.join(asarDir, 'a.asar', 'not-exist')
  161. await expectToThrowErrorWithCode(() => fs.promises.readFile(p), 'ENOENT')
  162. })
  163. })
  164. describe('fs.copyFile', function () {
  165. it('copies a normal file', function (done) {
  166. const p = path.join(asarDir, 'a.asar', 'file1')
  167. const dest = temp.path()
  168. fs.copyFile(p, dest, function (err) {
  169. assert.strictEqual(err, null)
  170. assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
  171. done()
  172. })
  173. })
  174. it('copies a unpacked file', function (done) {
  175. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  176. const dest = temp.path()
  177. fs.copyFile(p, dest, function (err) {
  178. assert.strictEqual(err, null)
  179. assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
  180. done()
  181. })
  182. })
  183. })
  184. describe('fs.promises.copyFile', function () {
  185. it('copies a normal file', async function () {
  186. const p = path.join(asarDir, 'a.asar', 'file1')
  187. const dest = temp.path()
  188. await fs.promises.copyFile(p, dest)
  189. assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
  190. })
  191. it('copies a unpacked file', async function () {
  192. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  193. const dest = temp.path()
  194. await fs.promises.copyFile(p, dest)
  195. assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
  196. })
  197. })
  198. describe('fs.copyFileSync', function () {
  199. it('copies a normal file', function () {
  200. const p = path.join(asarDir, 'a.asar', 'file1')
  201. const dest = temp.path()
  202. fs.copyFileSync(p, dest)
  203. assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
  204. })
  205. it('copies a unpacked file', function () {
  206. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  207. const dest = temp.path()
  208. fs.copyFileSync(p, dest)
  209. assert(fs.readFileSync(p).equals(fs.readFileSync(dest)))
  210. })
  211. })
  212. describe('fs.lstatSync', function () {
  213. it('handles path with trailing slash correctly', function () {
  214. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  215. fs.lstatSync(p)
  216. fs.lstatSync(p + '/')
  217. })
  218. it('returns information of root', function () {
  219. const p = path.join(asarDir, 'a.asar')
  220. const stats = fs.lstatSync(p)
  221. assert.strictEqual(stats.isFile(), false)
  222. assert.strictEqual(stats.isDirectory(), true)
  223. assert.strictEqual(stats.isSymbolicLink(), false)
  224. assert.strictEqual(stats.size, 0)
  225. })
  226. it('returns information of root with stats as bigint', function () {
  227. const p = path.join(asarDir, 'a.asar')
  228. const stats = fs.lstatSync(p, { bigint: false })
  229. assert.strictEqual(stats.isFile(), false)
  230. assert.strictEqual(stats.isDirectory(), true)
  231. assert.strictEqual(stats.isSymbolicLink(), false)
  232. assert.strictEqual(stats.size, 0)
  233. })
  234. it('returns information of a normal file', function () {
  235. const ref2 = ['file1', 'file2', 'file3', path.join('dir1', 'file1'), path.join('link2', 'file1')]
  236. for (let j = 0, len = ref2.length; j < len; j++) {
  237. const file = ref2[j]
  238. const p = path.join(asarDir, 'a.asar', file)
  239. const stats = fs.lstatSync(p)
  240. assert.strictEqual(stats.isFile(), true)
  241. assert.strictEqual(stats.isDirectory(), false)
  242. assert.strictEqual(stats.isSymbolicLink(), false)
  243. assert.strictEqual(stats.size, 6)
  244. }
  245. })
  246. it('returns information of a normal directory', function () {
  247. const ref2 = ['dir1', 'dir2', 'dir3']
  248. for (let j = 0, len = ref2.length; j < len; j++) {
  249. const file = ref2[j]
  250. const p = path.join(asarDir, 'a.asar', file)
  251. const stats = fs.lstatSync(p)
  252. assert.strictEqual(stats.isFile(), false)
  253. assert.strictEqual(stats.isDirectory(), true)
  254. assert.strictEqual(stats.isSymbolicLink(), false)
  255. assert.strictEqual(stats.size, 0)
  256. }
  257. })
  258. it('returns information of a linked file', function () {
  259. const ref2 = ['link1', path.join('dir1', 'link1'), path.join('link2', 'link2')]
  260. for (let j = 0, len = ref2.length; j < len; j++) {
  261. const file = ref2[j]
  262. const p = path.join(asarDir, 'a.asar', file)
  263. const stats = fs.lstatSync(p)
  264. assert.strictEqual(stats.isFile(), false)
  265. assert.strictEqual(stats.isDirectory(), false)
  266. assert.strictEqual(stats.isSymbolicLink(), true)
  267. assert.strictEqual(stats.size, 0)
  268. }
  269. })
  270. it('returns information of a linked directory', function () {
  271. const ref2 = ['link2', path.join('dir1', 'link2'), path.join('link2', 'link2')]
  272. for (let j = 0, len = ref2.length; j < len; j++) {
  273. const file = ref2[j]
  274. const p = path.join(asarDir, 'a.asar', file)
  275. const stats = fs.lstatSync(p)
  276. assert.strictEqual(stats.isFile(), false)
  277. assert.strictEqual(stats.isDirectory(), false)
  278. assert.strictEqual(stats.isSymbolicLink(), true)
  279. assert.strictEqual(stats.size, 0)
  280. }
  281. })
  282. it('throws ENOENT error when can not find file', function () {
  283. const ref2 = ['file4', 'file5', path.join('dir1', 'file4')]
  284. for (let j = 0, len = ref2.length; j < len; j++) {
  285. const file = ref2[j]
  286. const p = path.join(asarDir, 'a.asar', file)
  287. const throws = function () {
  288. fs.lstatSync(p)
  289. }
  290. assert.throws(throws, /ENOENT/)
  291. }
  292. })
  293. })
  294. describe('fs.lstat', function () {
  295. it('handles path with trailing slash correctly', function (done) {
  296. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  297. fs.lstat(p + '/', done)
  298. })
  299. it('returns information of root', function (done) {
  300. const p = path.join(asarDir, 'a.asar')
  301. fs.lstat(p, function (err, stats) {
  302. assert.strictEqual(err, null)
  303. assert.strictEqual(stats.isFile(), false)
  304. assert.strictEqual(stats.isDirectory(), true)
  305. assert.strictEqual(stats.isSymbolicLink(), false)
  306. assert.strictEqual(stats.size, 0)
  307. done()
  308. })
  309. })
  310. it('returns information of root with stats as bigint', function (done) {
  311. const p = path.join(asarDir, 'a.asar')
  312. fs.lstat(p, { bigint: false }, function (err, stats) {
  313. assert.strictEqual(err, null)
  314. assert.strictEqual(stats.isFile(), false)
  315. assert.strictEqual(stats.isDirectory(), true)
  316. assert.strictEqual(stats.isSymbolicLink(), false)
  317. assert.strictEqual(stats.size, 0)
  318. done()
  319. })
  320. })
  321. it('returns information of a normal file', function (done) {
  322. const p = path.join(asarDir, 'a.asar', 'link2', 'file1')
  323. fs.lstat(p, function (err, stats) {
  324. assert.strictEqual(err, null)
  325. assert.strictEqual(stats.isFile(), true)
  326. assert.strictEqual(stats.isDirectory(), false)
  327. assert.strictEqual(stats.isSymbolicLink(), false)
  328. assert.strictEqual(stats.size, 6)
  329. done()
  330. })
  331. })
  332. it('returns information of a normal directory', function (done) {
  333. const p = path.join(asarDir, 'a.asar', 'dir1')
  334. fs.lstat(p, function (err, stats) {
  335. assert.strictEqual(err, null)
  336. assert.strictEqual(stats.isFile(), false)
  337. assert.strictEqual(stats.isDirectory(), true)
  338. assert.strictEqual(stats.isSymbolicLink(), false)
  339. assert.strictEqual(stats.size, 0)
  340. done()
  341. })
  342. })
  343. it('returns information of a linked file', function (done) {
  344. const p = path.join(asarDir, 'a.asar', 'link2', 'link1')
  345. fs.lstat(p, function (err, stats) {
  346. assert.strictEqual(err, null)
  347. assert.strictEqual(stats.isFile(), false)
  348. assert.strictEqual(stats.isDirectory(), false)
  349. assert.strictEqual(stats.isSymbolicLink(), true)
  350. assert.strictEqual(stats.size, 0)
  351. done()
  352. })
  353. })
  354. it('returns information of a linked directory', function (done) {
  355. const p = path.join(asarDir, 'a.asar', 'link2', 'link2')
  356. fs.lstat(p, function (err, stats) {
  357. assert.strictEqual(err, null)
  358. assert.strictEqual(stats.isFile(), false)
  359. assert.strictEqual(stats.isDirectory(), false)
  360. assert.strictEqual(stats.isSymbolicLink(), true)
  361. assert.strictEqual(stats.size, 0)
  362. done()
  363. })
  364. })
  365. it('throws ENOENT error when can not find file', function (done) {
  366. const p = path.join(asarDir, 'a.asar', 'file4')
  367. fs.lstat(p, function (err) {
  368. assert.strictEqual(err.code, 'ENOENT')
  369. done()
  370. })
  371. })
  372. })
  373. describe('fs.promises.lstat', function () {
  374. it('handles path with trailing slash correctly', async function () {
  375. const p = path.join(asarDir, 'a.asar', 'link2', 'link2', 'file1')
  376. await fs.promises.lstat(p + '/')
  377. })
  378. it('returns information of root', async function () {
  379. const p = path.join(asarDir, 'a.asar')
  380. const stats = await fs.promises.lstat(p)
  381. assert.strictEqual(stats.isFile(), false)
  382. assert.strictEqual(stats.isDirectory(), true)
  383. assert.strictEqual(stats.isSymbolicLink(), false)
  384. assert.strictEqual(stats.size, 0)
  385. })
  386. it('returns information of root with stats as bigint', async function () {
  387. const p = path.join(asarDir, 'a.asar')
  388. const stats = await fs.promises.lstat(p, { bigint: false })
  389. assert.strictEqual(stats.isFile(), false)
  390. assert.strictEqual(stats.isDirectory(), true)
  391. assert.strictEqual(stats.isSymbolicLink(), false)
  392. assert.strictEqual(stats.size, 0)
  393. })
  394. it('returns information of a normal file', async function () {
  395. const p = path.join(asarDir, 'a.asar', 'link2', 'file1')
  396. const stats = await fs.promises.lstat(p)
  397. assert.strictEqual(stats.isFile(), true)
  398. assert.strictEqual(stats.isDirectory(), false)
  399. assert.strictEqual(stats.isSymbolicLink(), false)
  400. assert.strictEqual(stats.size, 6)
  401. })
  402. it('returns information of a normal directory', async function () {
  403. const p = path.join(asarDir, 'a.asar', 'dir1')
  404. const stats = await fs.promises.lstat(p)
  405. assert.strictEqual(stats.isFile(), false)
  406. assert.strictEqual(stats.isDirectory(), true)
  407. assert.strictEqual(stats.isSymbolicLink(), false)
  408. assert.strictEqual(stats.size, 0)
  409. })
  410. it('returns information of a linked file', async function () {
  411. const p = path.join(asarDir, 'a.asar', 'link2', 'link1')
  412. const stats = await fs.promises.lstat(p)
  413. assert.strictEqual(stats.isFile(), false)
  414. assert.strictEqual(stats.isDirectory(), false)
  415. assert.strictEqual(stats.isSymbolicLink(), true)
  416. assert.strictEqual(stats.size, 0)
  417. })
  418. it('returns information of a linked directory', async function () {
  419. const p = path.join(asarDir, 'a.asar', 'link2', 'link2')
  420. const stats = await fs.promises.lstat(p)
  421. assert.strictEqual(stats.isFile(), false)
  422. assert.strictEqual(stats.isDirectory(), false)
  423. assert.strictEqual(stats.isSymbolicLink(), true)
  424. assert.strictEqual(stats.size, 0)
  425. })
  426. it('throws ENOENT error when can not find file', async function () {
  427. const p = path.join(asarDir, 'a.asar', 'file4')
  428. await expectToThrowErrorWithCode(() => fs.promises.lstat(p), 'ENOENT')
  429. })
  430. })
  431. describe('fs.realpathSync', () => {
  432. it('returns real path root', () => {
  433. const parent = fs.realpathSync(asarDir)
  434. const p = 'a.asar'
  435. const r = fs.realpathSync(path.join(parent, p))
  436. assert.strictEqual(r, path.join(parent, p))
  437. })
  438. it('returns real path of a normal file', () => {
  439. const parent = fs.realpathSync(asarDir)
  440. const p = path.join('a.asar', 'file1')
  441. const r = fs.realpathSync(path.join(parent, p))
  442. assert.strictEqual(r, path.join(parent, p))
  443. })
  444. it('returns real path of a normal directory', () => {
  445. const parent = fs.realpathSync(asarDir)
  446. const p = path.join('a.asar', 'dir1')
  447. const r = fs.realpathSync(path.join(parent, p))
  448. assert.strictEqual(r, path.join(parent, p))
  449. })
  450. it('returns real path of a linked file', () => {
  451. const parent = fs.realpathSync(asarDir)
  452. const p = path.join('a.asar', 'link2', 'link1')
  453. const r = fs.realpathSync(path.join(parent, p))
  454. assert.strictEqual(r, path.join(parent, 'a.asar', 'file1'))
  455. })
  456. it('returns real path of a linked directory', () => {
  457. const parent = fs.realpathSync(asarDir)
  458. const p = path.join('a.asar', 'link2', 'link2')
  459. const r = fs.realpathSync(path.join(parent, p))
  460. assert.strictEqual(r, path.join(parent, 'a.asar', 'dir1'))
  461. })
  462. it('returns real path of an unpacked file', () => {
  463. const parent = fs.realpathSync(asarDir)
  464. const p = path.join('unpack.asar', 'a.txt')
  465. const r = fs.realpathSync(path.join(parent, p))
  466. assert.strictEqual(r, path.join(parent, p))
  467. })
  468. it('throws ENOENT error when can not find file', () => {
  469. const parent = fs.realpathSync(asarDir)
  470. const p = path.join('a.asar', 'not-exist')
  471. const throws = () => fs.realpathSync(path.join(parent, p))
  472. assert.throws(throws, /ENOENT/)
  473. })
  474. })
  475. describe('fs.realpathSync.native', () => {
  476. it('returns real path root', () => {
  477. const parent = fs.realpathSync.native(asarDir)
  478. const p = 'a.asar'
  479. const r = fs.realpathSync.native(path.join(parent, p))
  480. assert.strictEqual(r, path.join(parent, p))
  481. })
  482. it('returns real path of a normal file', () => {
  483. const parent = fs.realpathSync.native(asarDir)
  484. const p = path.join('a.asar', 'file1')
  485. const r = fs.realpathSync.native(path.join(parent, p))
  486. assert.strictEqual(r, path.join(parent, p))
  487. })
  488. it('returns real path of a normal directory', () => {
  489. const parent = fs.realpathSync.native(asarDir)
  490. const p = path.join('a.asar', 'dir1')
  491. const r = fs.realpathSync.native(path.join(parent, p))
  492. assert.strictEqual(r, path.join(parent, p))
  493. })
  494. it('returns real path of a linked file', () => {
  495. const parent = fs.realpathSync.native(asarDir)
  496. const p = path.join('a.asar', 'link2', 'link1')
  497. const r = fs.realpathSync.native(path.join(parent, p))
  498. assert.strictEqual(r, path.join(parent, 'a.asar', 'file1'))
  499. })
  500. it('returns real path of a linked directory', () => {
  501. const parent = fs.realpathSync.native(asarDir)
  502. const p = path.join('a.asar', 'link2', 'link2')
  503. const r = fs.realpathSync.native(path.join(parent, p))
  504. assert.strictEqual(r, path.join(parent, 'a.asar', 'dir1'))
  505. })
  506. it('returns real path of an unpacked file', () => {
  507. const parent = fs.realpathSync.native(asarDir)
  508. const p = path.join('unpack.asar', 'a.txt')
  509. const r = fs.realpathSync.native(path.join(parent, p))
  510. assert.strictEqual(r, path.join(parent, p))
  511. })
  512. it('throws ENOENT error when can not find file', () => {
  513. const parent = fs.realpathSync.native(asarDir)
  514. const p = path.join('a.asar', 'not-exist')
  515. const throws = () => fs.realpathSync.native(path.join(parent, p))
  516. assert.throws(throws, /ENOENT/)
  517. })
  518. })
  519. describe('fs.realpath', () => {
  520. it('returns real path root', done => {
  521. const parent = fs.realpathSync(asarDir)
  522. const p = 'a.asar'
  523. fs.realpath(path.join(parent, p), (err, r) => {
  524. assert.strictEqual(err, null)
  525. assert.strictEqual(r, path.join(parent, p))
  526. done()
  527. })
  528. })
  529. it('returns real path of a normal file', done => {
  530. const parent = fs.realpathSync(asarDir)
  531. const p = path.join('a.asar', 'file1')
  532. fs.realpath(path.join(parent, p), (err, r) => {
  533. assert.strictEqual(err, null)
  534. assert.strictEqual(r, path.join(parent, p))
  535. done()
  536. })
  537. })
  538. it('returns real path of a normal directory', done => {
  539. const parent = fs.realpathSync(asarDir)
  540. const p = path.join('a.asar', 'dir1')
  541. fs.realpath(path.join(parent, p), (err, r) => {
  542. assert.strictEqual(err, null)
  543. assert.strictEqual(r, path.join(parent, p))
  544. done()
  545. })
  546. })
  547. it('returns real path of a linked file', done => {
  548. const parent = fs.realpathSync(asarDir)
  549. const p = path.join('a.asar', 'link2', 'link1')
  550. fs.realpath(path.join(parent, p), (err, r) => {
  551. assert.strictEqual(err, null)
  552. assert.strictEqual(r, path.join(parent, 'a.asar', 'file1'))
  553. done()
  554. })
  555. })
  556. it('returns real path of a linked directory', done => {
  557. const parent = fs.realpathSync(asarDir)
  558. const p = path.join('a.asar', 'link2', 'link2')
  559. fs.realpath(path.join(parent, p), (err, r) => {
  560. assert.strictEqual(err, null)
  561. assert.strictEqual(r, path.join(parent, 'a.asar', 'dir1'))
  562. done()
  563. })
  564. })
  565. it('returns real path of an unpacked file', done => {
  566. const parent = fs.realpathSync(asarDir)
  567. const p = path.join('unpack.asar', 'a.txt')
  568. fs.realpath(path.join(parent, p), (err, r) => {
  569. assert.strictEqual(err, null)
  570. assert.strictEqual(r, path.join(parent, p))
  571. done()
  572. })
  573. })
  574. it('throws ENOENT error when can not find file', done => {
  575. const parent = fs.realpathSync(asarDir)
  576. const p = path.join('a.asar', 'not-exist')
  577. fs.realpath(path.join(parent, p), err => {
  578. assert.strictEqual(err.code, 'ENOENT')
  579. done()
  580. })
  581. })
  582. })
  583. describe('fs.promises.realpath', () => {
  584. it('returns real path root', async () => {
  585. const parent = fs.realpathSync(asarDir)
  586. const p = 'a.asar'
  587. const r = await fs.promises.realpath(path.join(parent, p))
  588. assert.strictEqual(r, path.join(parent, p))
  589. })
  590. it('returns real path of a normal file', async () => {
  591. const parent = fs.realpathSync(asarDir)
  592. const p = path.join('a.asar', 'file1')
  593. const r = await fs.promises.realpath(path.join(parent, p))
  594. assert.strictEqual(r, path.join(parent, p))
  595. })
  596. it('returns real path of a normal directory', async () => {
  597. const parent = fs.realpathSync(asarDir)
  598. const p = path.join('a.asar', 'dir1')
  599. const r = await fs.promises.realpath(path.join(parent, p))
  600. assert.strictEqual(r, path.join(parent, p))
  601. })
  602. it('returns real path of a linked file', async () => {
  603. const parent = fs.realpathSync(asarDir)
  604. const p = path.join('a.asar', 'link2', 'link1')
  605. const r = await fs.promises.realpath(path.join(parent, p))
  606. assert.strictEqual(r, path.join(parent, 'a.asar', 'file1'))
  607. })
  608. it('returns real path of a linked directory', async () => {
  609. const parent = fs.realpathSync(asarDir)
  610. const p = path.join('a.asar', 'link2', 'link2')
  611. const r = await fs.promises.realpath(path.join(parent, p))
  612. assert.strictEqual(r, path.join(parent, 'a.asar', 'dir1'))
  613. })
  614. it('returns real path of an unpacked file', async () => {
  615. const parent = fs.realpathSync(asarDir)
  616. const p = path.join('unpack.asar', 'a.txt')
  617. const r = await fs.promises.realpath(path.join(parent, p))
  618. assert.strictEqual(r, path.join(parent, p))
  619. })
  620. it('throws ENOENT error when can not find file', async () => {
  621. const parent = fs.realpathSync(asarDir)
  622. const p = path.join('a.asar', 'not-exist')
  623. await expectToThrowErrorWithCode(() => fs.promises.realpath(path.join(parent, p)), 'ENOENT')
  624. })
  625. })
  626. describe('fs.realpath.native', () => {
  627. it('returns real path root', done => {
  628. const parent = fs.realpathSync.native(asarDir)
  629. const p = 'a.asar'
  630. fs.realpath.native(path.join(parent, p), (err, r) => {
  631. assert.strictEqual(err, null)
  632. assert.strictEqual(r, path.join(parent, p))
  633. done()
  634. })
  635. })
  636. it('returns real path of a normal file', done => {
  637. const parent = fs.realpathSync.native(asarDir)
  638. const p = path.join('a.asar', 'file1')
  639. fs.realpath.native(path.join(parent, p), (err, r) => {
  640. assert.strictEqual(err, null)
  641. assert.strictEqual(r, path.join(parent, p))
  642. done()
  643. })
  644. })
  645. it('returns real path of a normal directory', done => {
  646. const parent = fs.realpathSync.native(asarDir)
  647. const p = path.join('a.asar', 'dir1')
  648. fs.realpath.native(path.join(parent, p), (err, r) => {
  649. assert.strictEqual(err, null)
  650. assert.strictEqual(r, path.join(parent, p))
  651. done()
  652. })
  653. })
  654. it('returns real path of a linked file', done => {
  655. const parent = fs.realpathSync.native(asarDir)
  656. const p = path.join('a.asar', 'link2', 'link1')
  657. fs.realpath.native(path.join(parent, p), (err, r) => {
  658. assert.strictEqual(err, null)
  659. assert.strictEqual(r, path.join(parent, 'a.asar', 'file1'))
  660. done()
  661. })
  662. })
  663. it('returns real path of a linked directory', done => {
  664. const parent = fs.realpathSync.native(asarDir)
  665. const p = path.join('a.asar', 'link2', 'link2')
  666. fs.realpath.native(path.join(parent, p), (err, r) => {
  667. assert.strictEqual(err, null)
  668. assert.strictEqual(r, path.join(parent, 'a.asar', 'dir1'))
  669. done()
  670. })
  671. })
  672. it('returns real path of an unpacked file', done => {
  673. const parent = fs.realpathSync.native(asarDir)
  674. const p = path.join('unpack.asar', 'a.txt')
  675. fs.realpath.native(path.join(parent, p), (err, r) => {
  676. assert.strictEqual(err, null)
  677. assert.strictEqual(r, path.join(parent, p))
  678. done()
  679. })
  680. })
  681. it('throws ENOENT error when can not find file', done => {
  682. const parent = fs.realpathSync.native(asarDir)
  683. const p = path.join('a.asar', 'not-exist')
  684. fs.realpath.native(path.join(parent, p), err => {
  685. assert.strictEqual(err.code, 'ENOENT')
  686. done()
  687. })
  688. })
  689. })
  690. describe('fs.readdirSync', function () {
  691. it('reads dirs from root', function () {
  692. const p = path.join(asarDir, 'a.asar')
  693. const dirs = fs.readdirSync(p)
  694. assert.deepStrictEqual(dirs, ['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js'])
  695. })
  696. it('reads dirs from a normal dir', function () {
  697. const p = path.join(asarDir, 'a.asar', 'dir1')
  698. const dirs = fs.readdirSync(p)
  699. assert.deepStrictEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2'])
  700. })
  701. it('reads dirs from a linked dir', function () {
  702. const p = path.join(asarDir, 'a.asar', 'link2', 'link2')
  703. const dirs = fs.readdirSync(p)
  704. assert.deepStrictEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2'])
  705. })
  706. it('throws ENOENT error when can not find file', function () {
  707. const p = path.join(asarDir, 'a.asar', 'not-exist')
  708. const throws = function () {
  709. fs.readdirSync(p)
  710. }
  711. assert.throws(throws, /ENOENT/)
  712. })
  713. })
  714. describe('fs.readdir', function () {
  715. it('reads dirs from root', function (done) {
  716. const p = path.join(asarDir, 'a.asar')
  717. fs.readdir(p, function (err, dirs) {
  718. assert.strictEqual(err, null)
  719. assert.deepStrictEqual(dirs, ['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js'])
  720. done()
  721. })
  722. })
  723. it('reads dirs from a normal dir', function (done) {
  724. const p = path.join(asarDir, 'a.asar', 'dir1')
  725. fs.readdir(p, function (err, dirs) {
  726. assert.strictEqual(err, null)
  727. assert.deepStrictEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2'])
  728. done()
  729. })
  730. })
  731. it('reads dirs from a linked dir', function (done) {
  732. const p = path.join(asarDir, 'a.asar', 'link2', 'link2')
  733. fs.readdir(p, function (err, dirs) {
  734. assert.strictEqual(err, null)
  735. assert.deepStrictEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2'])
  736. done()
  737. })
  738. })
  739. it('throws ENOENT error when can not find file', function (done) {
  740. const p = path.join(asarDir, 'a.asar', 'not-exist')
  741. fs.readdir(p, function (err) {
  742. assert.strictEqual(err.code, 'ENOENT')
  743. done()
  744. })
  745. })
  746. })
  747. describe('fs.promises.readdir', function () {
  748. it('reads dirs from root', async function () {
  749. const p = path.join(asarDir, 'a.asar')
  750. const dirs = await fs.promises.readdir(p)
  751. assert.deepStrictEqual(dirs, ['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js'])
  752. })
  753. it('reads dirs from a normal dir', async function () {
  754. const p = path.join(asarDir, 'a.asar', 'dir1')
  755. const dirs = await fs.promises.readdir(p)
  756. assert.deepStrictEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2'])
  757. })
  758. it('reads dirs from a linked dir', async function () {
  759. const p = path.join(asarDir, 'a.asar', 'link2', 'link2')
  760. const dirs = await fs.promises.readdir(p)
  761. assert.deepStrictEqual(dirs, ['file1', 'file2', 'file3', 'link1', 'link2'])
  762. })
  763. it('throws ENOENT error when can not find file', async function () {
  764. const p = path.join(asarDir, 'a.asar', 'not-exist')
  765. await expectToThrowErrorWithCode(() => fs.promises.readdir(p), 'ENOENT')
  766. })
  767. })
  768. describe('fs.openSync', function () {
  769. it('opens a normal/linked/under-linked-directory file', function () {
  770. const ref2 = ['file1', 'link1', path.join('link2', 'file1')]
  771. for (let j = 0, len = ref2.length; j < len; j++) {
  772. const file = ref2[j]
  773. const p = path.join(asarDir, 'a.asar', file)
  774. const fd = fs.openSync(p, 'r')
  775. const buffer = Buffer.alloc(6)
  776. fs.readSync(fd, buffer, 0, 6, 0)
  777. assert.strictEqual(String(buffer).trim(), 'file1')
  778. fs.closeSync(fd)
  779. }
  780. })
  781. it('throws ENOENT error when can not find file', function () {
  782. const p = path.join(asarDir, 'a.asar', 'not-exist')
  783. const throws = function () {
  784. fs.openSync(p)
  785. }
  786. assert.throws(throws, /ENOENT/)
  787. })
  788. })
  789. describe('fs.open', function () {
  790. it('opens a normal file', function (done) {
  791. const p = path.join(asarDir, 'a.asar', 'file1')
  792. fs.open(p, 'r', function (err, fd) {
  793. assert.strictEqual(err, null)
  794. const buffer = Buffer.alloc(6)
  795. fs.read(fd, buffer, 0, 6, 0, function (err) {
  796. assert.strictEqual(err, null)
  797. assert.strictEqual(String(buffer).trim(), 'file1')
  798. fs.close(fd, done)
  799. })
  800. })
  801. })
  802. it('throws ENOENT error when can not find file', function (done) {
  803. const p = path.join(asarDir, 'a.asar', 'not-exist')
  804. fs.open(p, 'r', function (err) {
  805. assert.strictEqual(err.code, 'ENOENT')
  806. done()
  807. })
  808. })
  809. })
  810. describe('fs.promises.open', function () {
  811. it('opens a normal file', async function () {
  812. const p = path.join(asarDir, 'a.asar', 'file1')
  813. const fh = await fs.promises.open(p, 'r')
  814. const buffer = Buffer.alloc(6)
  815. await fh.read(buffer, 0, 6, 0)
  816. assert.strictEqual(String(buffer).trim(), 'file1')
  817. await fh.close()
  818. })
  819. it('throws ENOENT error when can not find file', async function () {
  820. const p = path.join(asarDir, 'a.asar', 'not-exist')
  821. await expectToThrowErrorWithCode(() => fs.promises.open(p, 'r'), 'ENOENT')
  822. })
  823. })
  824. describe('fs.mkdir', function () {
  825. it('throws error when calling inside asar archive', function (done) {
  826. const p = path.join(asarDir, 'a.asar', 'not-exist')
  827. fs.mkdir(p, function (err) {
  828. assert.strictEqual(err.code, 'ENOTDIR')
  829. done()
  830. })
  831. })
  832. })
  833. describe('fs.promises.mkdir', function () {
  834. it('throws error when calling inside asar archive', async function () {
  835. const p = path.join(asarDir, 'a.asar', 'not-exist')
  836. await expectToThrowErrorWithCode(() => fs.promises.mkdir(p), 'ENOTDIR')
  837. })
  838. })
  839. describe('fs.mkdirSync', function () {
  840. it('throws error when calling inside asar archive', function () {
  841. const p = path.join(asarDir, 'a.asar', 'not-exist')
  842. assert.throws(function () {
  843. fs.mkdirSync(p)
  844. }, new RegExp('ENOTDIR'))
  845. })
  846. })
  847. describe('fs.exists', function () {
  848. it('handles an existing file', function (done) {
  849. const p = path.join(asarDir, 'a.asar', 'file1')
  850. // eslint-disable-next-line
  851. fs.exists(p, function (exists) {
  852. assert.strictEqual(exists, true)
  853. done()
  854. })
  855. })
  856. it('handles a non-existent file', function (done) {
  857. const p = path.join(asarDir, 'a.asar', 'not-exist')
  858. // eslint-disable-next-line
  859. fs.exists(p, function (exists) {
  860. assert.strictEqual(exists, false)
  861. done()
  862. })
  863. })
  864. it('promisified version handles an existing file', (done) => {
  865. const p = path.join(asarDir, 'a.asar', 'file1')
  866. // eslint-disable-next-line
  867. util.promisify(fs.exists)(p).then(exists => {
  868. assert.strictEqual(exists, true)
  869. done()
  870. })
  871. })
  872. it('promisified version handles a non-existent file', function (done) {
  873. const p = path.join(asarDir, 'a.asar', 'not-exist')
  874. // eslint-disable-next-line
  875. util.promisify(fs.exists)(p).then(exists => {
  876. assert.strictEqual(exists, false)
  877. done()
  878. })
  879. })
  880. })
  881. describe('fs.existsSync', function () {
  882. it('handles an existing file', function () {
  883. const p = path.join(asarDir, 'a.asar', 'file1')
  884. assert.doesNotThrow(function () {
  885. assert.strictEqual(fs.existsSync(p), true)
  886. })
  887. })
  888. it('handles a non-existent file', function () {
  889. const p = path.join(asarDir, 'a.asar', 'not-exist')
  890. assert.doesNotThrow(function () {
  891. assert.strictEqual(fs.existsSync(p), false)
  892. })
  893. })
  894. })
  895. describe('fs.access', function () {
  896. it('accesses a normal file', function (done) {
  897. const p = path.join(asarDir, 'a.asar', 'file1')
  898. fs.access(p, function (err) {
  899. assert(err == null)
  900. done()
  901. })
  902. })
  903. it('throws an error when called with write mode', function (done) {
  904. const p = path.join(asarDir, 'a.asar', 'file1')
  905. fs.access(p, fs.constants.R_OK | fs.constants.W_OK, function (err) {
  906. assert.strictEqual(err.code, 'EACCES')
  907. done()
  908. })
  909. })
  910. it('throws an error when called on non-existent file', function (done) {
  911. const p = path.join(asarDir, 'a.asar', 'not-exist')
  912. fs.access(p, function (err) {
  913. assert.strictEqual(err.code, 'ENOENT')
  914. done()
  915. })
  916. })
  917. it('allows write mode for unpacked files', function (done) {
  918. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  919. fs.access(p, fs.constants.R_OK | fs.constants.W_OK, function (err) {
  920. assert(err == null)
  921. done()
  922. })
  923. })
  924. })
  925. describe('fs.promises.access', function () {
  926. it('accesses a normal file', async function () {
  927. const p = path.join(asarDir, 'a.asar', 'file1')
  928. await fs.promises.access(p)
  929. })
  930. it('throws an error when called with write mode', async function () {
  931. const p = path.join(asarDir, 'a.asar', 'file1')
  932. await expectToThrowErrorWithCode(() => fs.promises.access(p, fs.constants.R_OK | fs.constants.W_OK), 'EACCES')
  933. })
  934. it('throws an error when called on non-existent file', async function () {
  935. const p = path.join(asarDir, 'a.asar', 'not-exist')
  936. await expectToThrowErrorWithCode(() => fs.promises.access(p), 'ENOENT')
  937. })
  938. it('allows write mode for unpacked files', async function () {
  939. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  940. await fs.promises.access(p, fs.constants.R_OK | fs.constants.W_OK)
  941. })
  942. })
  943. describe('fs.accessSync', function () {
  944. it('accesses a normal file', function () {
  945. const p = path.join(asarDir, 'a.asar', 'file1')
  946. assert.doesNotThrow(function () {
  947. fs.accessSync(p)
  948. })
  949. })
  950. it('throws an error when called with write mode', function () {
  951. const p = path.join(asarDir, 'a.asar', 'file1')
  952. assert.throws(function () {
  953. fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK)
  954. }, /EACCES/)
  955. })
  956. it('throws an error when called on non-existent file', function () {
  957. const p = path.join(asarDir, 'a.asar', 'not-exist')
  958. assert.throws(function () {
  959. fs.accessSync(p)
  960. }, /ENOENT/)
  961. })
  962. it('allows write mode for unpacked files', function () {
  963. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  964. assert.doesNotThrow(function () {
  965. fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK)
  966. })
  967. })
  968. })
  969. describe('child_process.fork', function () {
  970. before(function () {
  971. if (!features.isRunAsNodeEnabled()) {
  972. this.skip()
  973. }
  974. })
  975. it('opens a normal js file', function (done) {
  976. const child = ChildProcess.fork(path.join(asarDir, 'a.asar', 'ping.js'))
  977. child.on('message', function (msg) {
  978. assert.strictEqual(msg, 'message')
  979. done()
  980. })
  981. child.send('message')
  982. })
  983. it('supports asar in the forked js', function (done) {
  984. const file = path.join(asarDir, 'a.asar', 'file1')
  985. const child = ChildProcess.fork(path.join(fixtures, 'module', 'asar.js'))
  986. child.on('message', function (content) {
  987. assert.strictEqual(content, fs.readFileSync(file).toString())
  988. done()
  989. })
  990. child.send(file)
  991. })
  992. })
  993. describe('child_process.exec', function () {
  994. const echo = path.join(asarDir, 'echo.asar', 'echo')
  995. it('should not try to extract the command if there is a reference to a file inside an .asar', function (done) {
  996. ChildProcess.exec('echo ' + echo + ' foo bar', function (error, stdout) {
  997. assert.strictEqual(error, null)
  998. assert.strictEqual(stdout.toString().replace(/\r/g, ''), echo + ' foo bar\n')
  999. done()
  1000. })
  1001. })
  1002. it('can be promisified', () => {
  1003. return util.promisify(ChildProcess.exec)('echo ' + echo + ' foo bar').then(({ stdout }) => {
  1004. assert.strictEqual(stdout.toString().replace(/\r/g, ''), echo + ' foo bar\n')
  1005. })
  1006. })
  1007. })
  1008. describe('child_process.execSync', function () {
  1009. const echo = path.join(asarDir, 'echo.asar', 'echo')
  1010. it('should not try to extract the command if there is a reference to a file inside an .asar', function (done) {
  1011. const stdout = ChildProcess.execSync('echo ' + echo + ' foo bar')
  1012. assert.strictEqual(stdout.toString().replace(/\r/g, ''), echo + ' foo bar\n')
  1013. done()
  1014. })
  1015. })
  1016. describe('child_process.execFile', function () {
  1017. const execFile = ChildProcess.execFile
  1018. const execFileSync = ChildProcess.execFileSync
  1019. const echo = path.join(asarDir, 'echo.asar', 'echo')
  1020. before(function () {
  1021. if (process.platform !== 'darwin') {
  1022. this.skip()
  1023. }
  1024. })
  1025. it('executes binaries', function (done) {
  1026. execFile(echo, ['test'], function (error, stdout) {
  1027. assert.strictEqual(error, null)
  1028. assert.strictEqual(stdout, 'test\n')
  1029. done()
  1030. })
  1031. })
  1032. it('executes binaries without callback', function (done) {
  1033. const process = execFile(echo, ['test'])
  1034. process.on('close', function (code) {
  1035. assert.strictEqual(code, 0)
  1036. done()
  1037. })
  1038. process.on('error', function () {
  1039. assert.fail()
  1040. done()
  1041. })
  1042. })
  1043. it('execFileSync executes binaries', function () {
  1044. const output = execFileSync(echo, ['test'])
  1045. assert.strictEqual(String(output), 'test\n')
  1046. })
  1047. it('can be promisified', () => {
  1048. return util.promisify(ChildProcess.execFile)(echo, ['test']).then(({ stdout }) => {
  1049. assert.strictEqual(stdout, 'test\n')
  1050. })
  1051. })
  1052. })
  1053. describe('internalModuleReadJSON', function () {
  1054. const internalModuleReadJSON = process.binding('fs').internalModuleReadJSON
  1055. it('read a normal file', function () {
  1056. const file1 = path.join(asarDir, 'a.asar', 'file1')
  1057. assert.strictEqual(internalModuleReadJSON(file1).toString().trim(), 'file1')
  1058. const file2 = path.join(asarDir, 'a.asar', 'file2')
  1059. assert.strictEqual(internalModuleReadJSON(file2).toString().trim(), 'file2')
  1060. const file3 = path.join(asarDir, 'a.asar', 'file3')
  1061. assert.strictEqual(internalModuleReadJSON(file3).toString().trim(), 'file3')
  1062. })
  1063. it('reads a normal file with unpacked files', function () {
  1064. const p = path.join(asarDir, 'unpack.asar', 'a.txt')
  1065. assert.strictEqual(internalModuleReadJSON(p).toString().trim(), 'a')
  1066. })
  1067. })
  1068. describe('util.promisify', function () {
  1069. it('can promisify all fs functions', function () {
  1070. const originalFs = require('original-fs')
  1071. const { hasOwnProperty } = Object.prototype
  1072. for (const [propertyName, originalValue] of Object.entries(originalFs)) {
  1073. // Some properties exist but have a value of `undefined` on some platforms.
  1074. // E.g. `fs.lchmod`, which in only available on MacOS, see
  1075. // https://nodejs.org/docs/latest-v10.x/api/fs.html#fs_fs_lchmod_path_mode_callback
  1076. // Also check for `null`s, `hasOwnProperty()` can't handle them.
  1077. if (typeof originalValue === 'undefined' || originalValue === null) continue
  1078. if (hasOwnProperty.call(originalValue, util.promisify.custom)) {
  1079. expect(fs).to.have.own.property(propertyName)
  1080. .that.has.own.property(util.promisify.custom)
  1081. }
  1082. }
  1083. })
  1084. })
  1085. describe('process.noAsar', function () {
  1086. const errorName = process.platform === 'win32' ? 'ENOENT' : 'ENOTDIR'
  1087. beforeEach(function () {
  1088. process.noAsar = true
  1089. })
  1090. afterEach(function () {
  1091. process.noAsar = false
  1092. })
  1093. it('disables asar support in sync API', function () {
  1094. const file = path.join(asarDir, 'a.asar', 'file1')
  1095. const dir = path.join(asarDir, 'a.asar', 'dir1')
  1096. assert.throws(function () {
  1097. fs.readFileSync(file)
  1098. }, new RegExp(errorName))
  1099. assert.throws(function () {
  1100. fs.lstatSync(file)
  1101. }, new RegExp(errorName))
  1102. assert.throws(function () {
  1103. fs.realpathSync(file)
  1104. }, new RegExp(errorName))
  1105. assert.throws(function () {
  1106. fs.readdirSync(dir)
  1107. }, new RegExp(errorName))
  1108. })
  1109. it('disables asar support in async API', function (done) {
  1110. const file = path.join(asarDir, 'a.asar', 'file1')
  1111. const dir = path.join(asarDir, 'a.asar', 'dir1')
  1112. fs.readFile(file, function (error) {
  1113. assert.strictEqual(error.code, errorName)
  1114. fs.lstat(file, function (error) {
  1115. assert.strictEqual(error.code, errorName)
  1116. fs.realpath(file, function (error) {
  1117. assert.strictEqual(error.code, errorName)
  1118. fs.readdir(dir, function (error) {
  1119. assert.strictEqual(error.code, errorName)
  1120. done()
  1121. })
  1122. })
  1123. })
  1124. })
  1125. })
  1126. it('treats *.asar as normal file', function () {
  1127. const originalFs = require('original-fs')
  1128. const asar = path.join(asarDir, 'a.asar')
  1129. const content1 = fs.readFileSync(asar)
  1130. const content2 = originalFs.readFileSync(asar)
  1131. assert.strictEqual(content1.compare(content2), 0)
  1132. assert.throws(function () {
  1133. fs.readdirSync(asar)
  1134. }, /ENOTDIR/)
  1135. })
  1136. it('is reset to its original value when execSync throws an error', function () {
  1137. process.noAsar = false
  1138. assert.throws(function () {
  1139. ChildProcess.execSync(path.join(__dirname, 'does-not-exist.txt'))
  1140. })
  1141. assert.strictEqual(process.noAsar, false)
  1142. })
  1143. })
  1144. describe('process.env.ELECTRON_NO_ASAR', function () {
  1145. before(function () {
  1146. if (!features.isRunAsNodeEnabled()) {
  1147. this.skip()
  1148. }
  1149. })
  1150. it('disables asar support in forked processes', function (done) {
  1151. const forked = ChildProcess.fork(path.join(__dirname, 'fixtures', 'module', 'no-asar.js'), [], {
  1152. env: {
  1153. ELECTRON_NO_ASAR: true
  1154. }
  1155. })
  1156. forked.on('message', function (stats) {
  1157. assert.strictEqual(stats.isFile, true)
  1158. assert.strictEqual(stats.size, 778)
  1159. done()
  1160. })
  1161. })
  1162. it('disables asar support in spawned processes', function (done) {
  1163. const spawned = ChildProcess.spawn(process.execPath, [path.join(__dirname, 'fixtures', 'module', 'no-asar.js')], {
  1164. env: {
  1165. ELECTRON_NO_ASAR: true,
  1166. ELECTRON_RUN_AS_NODE: true
  1167. }
  1168. })
  1169. let output = ''
  1170. spawned.stdout.on('data', function (data) {
  1171. output += data
  1172. })
  1173. spawned.stdout.on('close', function () {
  1174. const stats = JSON.parse(output)
  1175. assert.strictEqual(stats.isFile, true)
  1176. assert.strictEqual(stats.size, 778)
  1177. done()
  1178. })
  1179. })
  1180. })
  1181. })
  1182. describe('asar protocol', function () {
  1183. let w = null
  1184. afterEach(function () {
  1185. return closeWindow(w).then(function () { w = null })
  1186. })
  1187. it('can request a file in package', function (done) {
  1188. const p = path.resolve(asarDir, 'a.asar', 'file1')
  1189. $.get('file://' + p, function (data) {
  1190. assert.strictEqual(data.trim(), 'file1')
  1191. done()
  1192. })
  1193. })
  1194. it('can request a file in package with unpacked files', function (done) {
  1195. const p = path.resolve(asarDir, 'unpack.asar', 'a.txt')
  1196. $.get('file://' + p, function (data) {
  1197. assert.strictEqual(data.trim(), 'a')
  1198. done()
  1199. })
  1200. })
  1201. it('can request a linked file in package', function (done) {
  1202. const p = path.resolve(asarDir, 'a.asar', 'link2', 'link1')
  1203. $.get('file://' + p, function (data) {
  1204. assert.strictEqual(data.trim(), 'file1')
  1205. done()
  1206. })
  1207. })
  1208. it('can request a file in filesystem', function (done) {
  1209. const p = path.resolve(asarDir, 'file')
  1210. $.get('file://' + p, function (data) {
  1211. assert.strictEqual(data.trim(), 'file')
  1212. done()
  1213. })
  1214. })
  1215. it('gets 404 when file is not found', function (done) {
  1216. const p = path.resolve(asarDir, 'a.asar', 'no-exist')
  1217. $.ajax({
  1218. url: 'file://' + p,
  1219. error: function (err) {
  1220. assert.strictEqual(err.status, 404)
  1221. done()
  1222. }
  1223. })
  1224. })
  1225. it('sets __dirname correctly', function (done) {
  1226. after(function () {
  1227. ipcMain.removeAllListeners('dirname')
  1228. })
  1229. w = new BrowserWindow({
  1230. show: false,
  1231. width: 400,
  1232. height: 400,
  1233. webPreferences: {
  1234. nodeIntegration: true
  1235. }
  1236. })
  1237. const p = path.resolve(asarDir, 'web.asar', 'index.html')
  1238. ipcMain.once('dirname', function (event, dirname) {
  1239. assert.strictEqual(dirname, path.dirname(p))
  1240. done()
  1241. })
  1242. w.loadFile(p)
  1243. })
  1244. it('loads script tag in html', function (done) {
  1245. after(function () {
  1246. ipcMain.removeAllListeners('ping')
  1247. })
  1248. w = new BrowserWindow({
  1249. show: false,
  1250. width: 400,
  1251. height: 400,
  1252. webPreferences: {
  1253. nodeIntegration: true
  1254. }
  1255. })
  1256. const p = path.resolve(asarDir, 'script.asar', 'index.html')
  1257. w.loadFile(p)
  1258. ipcMain.once('ping', function (event, message) {
  1259. assert.strictEqual(message, 'pong')
  1260. done()
  1261. })
  1262. })
  1263. it('loads video tag in html', function (done) {
  1264. this.timeout(60000)
  1265. after(function () {
  1266. ipcMain.removeAllListeners('asar-video')
  1267. })
  1268. w = new BrowserWindow({
  1269. show: false,
  1270. width: 400,
  1271. height: 400,
  1272. webPreferences: {
  1273. nodeIntegration: true
  1274. }
  1275. })
  1276. const p = path.resolve(asarDir, 'video.asar', 'index.html')
  1277. w.loadFile(p)
  1278. ipcMain.on('asar-video', function (event, message, error) {
  1279. if (message === 'ended') {
  1280. assert(!error)
  1281. done()
  1282. } else if (message === 'error') {
  1283. done(error)
  1284. }
  1285. })
  1286. })
  1287. })
  1288. describe('original-fs module', function () {
  1289. const originalFs = require('original-fs')
  1290. it('treats .asar as file', function () {
  1291. const file = path.join(asarDir, 'a.asar')
  1292. const stats = originalFs.statSync(file)
  1293. assert(stats.isFile())
  1294. })
  1295. it('is available in forked scripts', function (done) {
  1296. if (!features.isRunAsNodeEnabled()) {
  1297. this.skip()
  1298. done()
  1299. }
  1300. const child = ChildProcess.fork(path.join(fixtures, 'module', 'original-fs.js'))
  1301. child.on('message', function (msg) {
  1302. assert.strictEqual(msg, 'object')
  1303. done()
  1304. })
  1305. child.send('message')
  1306. })
  1307. it('can be used with streams', () => {
  1308. originalFs.createReadStream(path.join(asarDir, 'a.asar'))
  1309. })
  1310. it('has the same APIs as fs', function () {
  1311. expect(Object.keys(require('fs'))).to.deep.equal(Object.keys(require('original-fs')))
  1312. expect(Object.keys(require('fs').promises)).to.deep.equal(Object.keys(require('original-fs').promises))
  1313. })
  1314. })
  1315. describe('graceful-fs module', function () {
  1316. const gfs = require('graceful-fs')
  1317. it('recognize asar archvies', function () {
  1318. const p = path.join(asarDir, 'a.asar', 'link1')
  1319. assert.strictEqual(gfs.readFileSync(p).toString().trim(), 'file1')
  1320. })
  1321. it('does not touch global fs object', function () {
  1322. assert.notStrictEqual(fs.readdir, gfs.readdir)
  1323. })
  1324. })
  1325. describe('mkdirp module', function () {
  1326. const mkdirp = require('mkdirp')
  1327. it('throws error when calling inside asar archive', function () {
  1328. const p = path.join(asarDir, 'a.asar', 'not-exist')
  1329. assert.throws(function () {
  1330. mkdirp.sync(p)
  1331. }, new RegExp('ENOTDIR'))
  1332. })
  1333. })
  1334. describe('native-image', function () {
  1335. it('reads image from asar archive', function () {
  1336. const p = path.join(asarDir, 'logo.asar', 'logo.png')
  1337. const logo = nativeImage.createFromPath(p)
  1338. assert.deepStrictEqual(logo.getSize(), {
  1339. width: 55,
  1340. height: 55
  1341. })
  1342. })
  1343. it('reads image from asar archive with unpacked files', function () {
  1344. const p = path.join(asarDir, 'unpack.asar', 'atom.png')
  1345. const logo = nativeImage.createFromPath(p)
  1346. assert.deepStrictEqual(logo.getSize(), {
  1347. width: 1024,
  1348. height: 1024
  1349. })
  1350. })
  1351. })
  1352. })