asar-spec.js 53 KB

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