asar-spec.js 42 KB

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