asar-spec.js 33 KB

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