ArchiveManager.test.mjs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. import { vi, expect } from 'vitest'
  2. // TODO: This file was created by bulk-decaffeinate.
  3. // Fix any style issues and re-enable lint.
  4. /*
  5. * decaffeinate suggestions:
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS206: Consider reworking classes to avoid initClass
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  9. */
  10. import sinon from 'sinon'
  11. import ArchiveErrors from '../../../../app/src/Features/Uploads/ArchiveErrors.mjs'
  12. import events from 'node:events'
  13. import { PassThrough } from 'node:stream'
  14. vi.mock('../../../../app/src/Features/Uploads/ArchiveErrors.js', () =>
  15. vi.importActual('../../../../app/src/Features/Uploads/ArchiveErrors.js')
  16. )
  17. const modulePath = '../../../../app/src/Features/Uploads/ArchiveManager.mjs'
  18. describe('ArchiveManager', function () {
  19. beforeEach(async function (ctx) {
  20. let Timer
  21. ctx.metrics = {
  22. Timer: (Timer = (function () {
  23. Timer = class Timer {
  24. static initClass() {
  25. this.prototype.done = sinon.stub()
  26. }
  27. }
  28. Timer.initClass()
  29. return Timer
  30. })()),
  31. }
  32. ctx.zipfile = new events.EventEmitter()
  33. ctx.zipfile.readEntry = sinon.stub()
  34. ctx.zipfile.close = sinon.stub()
  35. vi.doMock('@overleaf/settings', () => ({
  36. default: {},
  37. }))
  38. vi.doMock('yauzl', () => ({
  39. default: (ctx.yauzl = {
  40. open: sinon.stub().callsArgWith(2, null, ctx.zipfile),
  41. }),
  42. }))
  43. vi.doMock('@overleaf/metrics', () => ({
  44. default: ctx.metrics,
  45. }))
  46. ctx.fs = { mkdir: sinon.stub().yields(), stat: sinon.stub() }
  47. vi.doMock('fs', () => ({
  48. default: ctx.fs,
  49. }))
  50. vi.doMock(
  51. '../../../../app/src/Features/Uploads/ArchiveErrors',
  52. () => ArchiveErrors
  53. )
  54. ctx.ArchiveManager = (await import(modulePath)).default
  55. ctx.callback = sinon.stub()
  56. })
  57. describe('extractZipArchive', function () {
  58. beforeEach(function (ctx) {
  59. ctx.source = '/path/to/zip/source.zip'
  60. ctx.destination = '/path/to/zip/destination'
  61. ctx.ArchiveManager._isZipTooLarge = sinon
  62. .stub()
  63. .callsArgWith(1, null, false)
  64. })
  65. describe('successfully', function () {
  66. beforeEach(async function (ctx) {
  67. await new Promise(resolve => {
  68. ctx.readStream = new PassThrough()
  69. ctx.zipfile.openReadStream = sinon
  70. .stub()
  71. .callsArgWith(1, null, ctx.readStream)
  72. ctx.writeStream = new PassThrough()
  73. ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
  74. sinon.spy(ctx.writeStream, 'destroy')
  75. ctx.ArchiveManager.extractZipArchive(
  76. ctx.source,
  77. ctx.destination,
  78. resolve
  79. )
  80. // entry contains a single file
  81. ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
  82. ctx.readStream.end()
  83. ctx.zipfile.emit('end')
  84. })
  85. })
  86. it('should run yauzl', function (ctx) {
  87. ctx.yauzl.open.calledWith(ctx.source).should.equal(true)
  88. })
  89. it('should time the unzip', function (ctx) {
  90. ctx.metrics.Timer.prototype.done.called.should.equal(true)
  91. })
  92. })
  93. describe('with a zipfile containing an empty directory', function () {
  94. beforeEach(async function (ctx) {
  95. await new Promise(resolve => {
  96. ctx.readStream = new PassThrough()
  97. ctx.zipfile.openReadStream = sinon
  98. .stub()
  99. .callsArgWith(1, null, ctx.readStream)
  100. ctx.writeStream = new PassThrough()
  101. ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
  102. sinon.spy(ctx.writeStream, 'destroy')
  103. ctx.ArchiveManager.extractZipArchive(
  104. ctx.source,
  105. ctx.destination,
  106. error => {
  107. ctx.callback(error)
  108. resolve()
  109. }
  110. )
  111. // entry contains a single, empty directory
  112. ctx.zipfile.emit('entry', { fileName: 'testdir/' })
  113. ctx.readStream.end()
  114. ctx.zipfile.emit('end')
  115. })
  116. })
  117. it('should return the callback with an error', function (ctx) {
  118. sinon.assert.calledWithExactly(
  119. ctx.callback,
  120. sinon.match.instanceOf(ArchiveErrors.EmptyZipFileError)
  121. )
  122. })
  123. })
  124. describe('with an empty zipfile', function () {
  125. beforeEach(async function (ctx) {
  126. await new Promise(resolve => {
  127. ctx.ArchiveManager.extractZipArchive(
  128. ctx.source,
  129. ctx.destination,
  130. error => {
  131. ctx.callback(error)
  132. resolve()
  133. }
  134. )
  135. ctx.zipfile.emit('end')
  136. })
  137. })
  138. it('should return the callback with an error', function (ctx) {
  139. sinon.assert.calledWithExactly(
  140. ctx.callback,
  141. sinon.match.instanceOf(ArchiveErrors.EmptyZipFileError)
  142. )
  143. })
  144. })
  145. describe('with an error in the zip file header', function () {
  146. beforeEach(async function (ctx) {
  147. await new Promise(resolve => {
  148. ctx.yauzl.open = sinon
  149. .stub()
  150. .callsArgWith(2, new ArchiveErrors.InvalidZipFileError())
  151. ctx.ArchiveManager.extractZipArchive(
  152. ctx.source,
  153. ctx.destination,
  154. error => {
  155. ctx.callback(error)
  156. resolve()
  157. }
  158. )
  159. })
  160. })
  161. it('should return the callback with an error', function (ctx) {
  162. sinon.assert.calledWithExactly(
  163. ctx.callback,
  164. sinon.match.instanceOf(ArchiveErrors.InvalidZipFileError)
  165. )
  166. })
  167. })
  168. describe('with a zip that is too large', function () {
  169. beforeEach(async function (ctx) {
  170. await new Promise(resolve => {
  171. ctx.ArchiveManager._isZipTooLarge = sinon
  172. .stub()
  173. .callsArgWith(1, null, true)
  174. ctx.ArchiveManager.extractZipArchive(
  175. ctx.source,
  176. ctx.destination,
  177. error => {
  178. ctx.callback(error)
  179. resolve()
  180. }
  181. )
  182. })
  183. })
  184. it('should return the callback with an error', function (ctx) {
  185. sinon.assert.calledWithExactly(
  186. ctx.callback,
  187. sinon.match.instanceOf(ArchiveErrors.ZipContentsTooLargeError)
  188. )
  189. })
  190. it('should not call yauzl.open', function (ctx) {
  191. ctx.yauzl.open.called.should.equal(false)
  192. })
  193. })
  194. describe('with an error in the extracted files', function () {
  195. beforeEach(async function (ctx) {
  196. await new Promise(resolve => {
  197. ctx.ArchiveManager.extractZipArchive(
  198. ctx.source,
  199. ctx.destination,
  200. error => {
  201. ctx.callback(error)
  202. resolve()
  203. }
  204. )
  205. ctx.zipfile.emit('error', new Error('Something went wrong'))
  206. })
  207. })
  208. it('should return the callback with an error', function (ctx) {
  209. return ctx.callback.should.have.been.calledWithExactly(
  210. sinon.match
  211. .instanceOf(Error)
  212. .and(sinon.match.has('message', 'Something went wrong'))
  213. )
  214. })
  215. })
  216. describe('with a relative extracted file path', function () {
  217. beforeEach(async function (ctx) {
  218. await new Promise(resolve => {
  219. ctx.zipfile.openReadStream = sinon.stub()
  220. ctx.ArchiveManager.extractZipArchive(
  221. ctx.source,
  222. ctx.destination,
  223. error => {
  224. ctx.callback(error)
  225. return resolve()
  226. }
  227. )
  228. ctx.zipfile.emit('entry', { fileName: '../testfile.txt' })
  229. return ctx.zipfile.emit('end')
  230. })
  231. })
  232. it('should not write try to read the file entry', function (ctx) {
  233. return ctx.zipfile.openReadStream.called.should.equal(false)
  234. })
  235. })
  236. describe('with an unnormalized extracted file path', function () {
  237. beforeEach(async function (ctx) {
  238. await new Promise(resolve => {
  239. ctx.zipfile.openReadStream = sinon.stub()
  240. ctx.ArchiveManager.extractZipArchive(
  241. ctx.source,
  242. ctx.destination,
  243. error => {
  244. ctx.callback(error)
  245. return resolve()
  246. }
  247. )
  248. ctx.zipfile.emit('entry', { fileName: 'foo/./testfile.txt' })
  249. return ctx.zipfile.emit('end')
  250. })
  251. })
  252. it('should not try to read the file entry', function (ctx) {
  253. return ctx.zipfile.openReadStream.called.should.equal(false)
  254. })
  255. })
  256. describe('with backslashes in the path', function () {
  257. beforeEach(async function (ctx) {
  258. await new Promise(resolve => {
  259. ctx.readStream = new PassThrough()
  260. ctx.writeStream = new PassThrough()
  261. ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
  262. sinon.spy(ctx.writeStream, 'destroy')
  263. ctx.zipfile.openReadStream = sinon
  264. .stub()
  265. .callsArgWith(1, null, ctx.readStream)
  266. ctx.ArchiveManager.extractZipArchive(
  267. ctx.source,
  268. ctx.destination,
  269. error => {
  270. ctx.callback(error)
  271. return resolve()
  272. }
  273. )
  274. ctx.zipfile.emit('entry', { fileName: 'wombat\\foo.tex' })
  275. ctx.readStream.end()
  276. ctx.zipfile.emit('entry', { fileName: 'potato\\bar.tex' })
  277. ctx.readStream.end()
  278. return ctx.zipfile.emit('end')
  279. })
  280. })
  281. it('should read the file entry with its original path', function (ctx) {
  282. ctx.zipfile.openReadStream.should.be.calledWith({
  283. fileName: 'wombat\\foo.tex',
  284. })
  285. ctx.zipfile.openReadStream.should.be.calledWith({
  286. fileName: 'potato\\bar.tex',
  287. })
  288. })
  289. it('should treat the backslashes as a directory separator when creating the directory', function (ctx) {
  290. ctx.fs.mkdir.should.be.calledWith(`${ctx.destination}/wombat`, {
  291. recursive: true,
  292. })
  293. ctx.fs.mkdir.should.be.calledWith(`${ctx.destination}/potato`, {
  294. recursive: true,
  295. })
  296. })
  297. it('should treat the backslashes as a directory separator when creating the file', function (ctx) {
  298. ctx.fs.createWriteStream.should.be.calledWith(
  299. `${ctx.destination}/wombat/foo.tex`
  300. )
  301. ctx.fs.createWriteStream.should.be.calledWith(
  302. `${ctx.destination}/potato/bar.tex`
  303. )
  304. })
  305. })
  306. describe('with a directory entry', function () {
  307. beforeEach(async function (ctx) {
  308. await new Promise(resolve => {
  309. ctx.zipfile.openReadStream = sinon.stub()
  310. ctx.ArchiveManager.extractZipArchive(
  311. ctx.source,
  312. ctx.destination,
  313. error => {
  314. ctx.callback(error)
  315. resolve()
  316. }
  317. )
  318. ctx.zipfile.emit('entry', { fileName: 'testdir/' })
  319. ctx.zipfile.emit('end')
  320. })
  321. })
  322. it('should not try to read the entry', function (ctx) {
  323. ctx.zipfile.openReadStream.called.should.equal(false)
  324. })
  325. })
  326. describe('with an error opening the file read stream', function () {
  327. beforeEach(async function (ctx) {
  328. await new Promise(resolve => {
  329. ctx.zipfile.openReadStream = sinon
  330. .stub()
  331. .callsArgWith(1, new Error('Something went wrong'))
  332. ctx.writeStream = new PassThrough()
  333. ctx.ArchiveManager.extractZipArchive(
  334. ctx.source,
  335. ctx.destination,
  336. error => {
  337. ctx.callback(error)
  338. resolve()
  339. }
  340. )
  341. ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
  342. ctx.zipfile.emit('end')
  343. })
  344. })
  345. it('should return the callback with an error', function (ctx) {
  346. ctx.callback.should.have.been.calledWithExactly(
  347. sinon.match
  348. .instanceOf(Error)
  349. .and(sinon.match.has('message', 'Something went wrong'))
  350. )
  351. })
  352. it('should close the zipfile', function (ctx) {
  353. ctx.zipfile.close.called.should.equal(true)
  354. })
  355. })
  356. describe('with an error in the file read stream', function () {
  357. beforeEach(async function (ctx) {
  358. await new Promise(resolve => {
  359. ctx.readStream = new PassThrough()
  360. ctx.zipfile.openReadStream = sinon
  361. .stub()
  362. .callsArgWith(1, null, ctx.readStream)
  363. ctx.writeStream = new PassThrough()
  364. ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
  365. sinon.spy(ctx.writeStream, 'destroy')
  366. ctx.ArchiveManager.extractZipArchive(
  367. ctx.source,
  368. ctx.destination,
  369. error => {
  370. ctx.callback(error)
  371. return resolve()
  372. }
  373. )
  374. ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
  375. ctx.readStream.emit('error', new Error('Something went wrong'))
  376. })
  377. })
  378. it('should return the callback with an error', function (ctx) {
  379. ctx.callback.should.have.been.calledWithExactly(
  380. sinon.match
  381. .instanceOf(Error)
  382. .and(sinon.match.has('message', 'Something went wrong'))
  383. )
  384. })
  385. it('should close the zipfile', function (ctx) {
  386. ctx.zipfile.close.called.should.equal(true)
  387. })
  388. })
  389. describe('with an error in the file write stream', function () {
  390. beforeEach(async function (ctx) {
  391. await new Promise(resolve => {
  392. ctx.readStream = new PassThrough()
  393. sinon.spy(ctx.readStream, 'destroy')
  394. ctx.zipfile.openReadStream = sinon
  395. .stub()
  396. .callsArgWith(1, null, ctx.readStream)
  397. ctx.writeStream = new PassThrough()
  398. ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
  399. sinon.spy(ctx.writeStream, 'destroy')
  400. ctx.ArchiveManager.extractZipArchive(
  401. ctx.source,
  402. ctx.destination,
  403. error => {
  404. ctx.callback(error)
  405. return resolve()
  406. }
  407. )
  408. ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
  409. ctx.writeStream.emit('error', new Error('Something went wrong'))
  410. })
  411. })
  412. it('should return the callback with an error', function (ctx) {
  413. ctx.callback.should.have.been.calledWithExactly(
  414. sinon.match
  415. .instanceOf(Error)
  416. .and(sinon.match.has('message', 'Something went wrong'))
  417. )
  418. })
  419. it('should destroy the readstream', function (ctx) {
  420. ctx.readStream.destroy.called.should.equal(true)
  421. })
  422. it('should close the zipfile', function (ctx) {
  423. ctx.zipfile.close.called.should.equal(true)
  424. })
  425. })
  426. })
  427. describe('_isZipTooLarge', function () {
  428. it('should return false with small output', async function (ctx) {
  429. await new Promise(resolve => {
  430. ctx.ArchiveManager._isZipTooLarge(ctx.source, (error, isTooLarge) => {
  431. expect(error).not.to.exist
  432. isTooLarge.should.equal(false)
  433. resolve()
  434. })
  435. ctx.zipfile.emit('entry', { uncompressedSize: 109042 })
  436. ctx.zipfile.emit('end')
  437. })
  438. })
  439. it('should return true with large bytes', async function (ctx) {
  440. await new Promise(resolve => {
  441. ctx.ArchiveManager._isZipTooLarge(ctx.source, (error, isTooLarge) => {
  442. expect(error).not.to.exist
  443. isTooLarge.should.equal(true)
  444. resolve()
  445. })
  446. ctx.zipfile.emit('entry', { uncompressedSize: 109e16 })
  447. ctx.zipfile.emit('end')
  448. })
  449. })
  450. it('should return error on no data', async function (ctx) {
  451. await new Promise(resolve => {
  452. ctx.ArchiveManager._isZipTooLarge(ctx.source, (error, isTooLarge) => {
  453. expect(error).to.exist
  454. resolve()
  455. })
  456. ctx.zipfile.emit('entry', {})
  457. ctx.zipfile.emit('end')
  458. })
  459. })
  460. it("should return error if it didn't get a number", async function (ctx) {
  461. await new Promise(resolve => {
  462. ctx.ArchiveManager._isZipTooLarge(ctx.source, (error, isTooLarge) => {
  463. expect(error).to.exist
  464. resolve()
  465. })
  466. ctx.zipfile.emit('entry', { uncompressedSize: 'random-error' })
  467. ctx.zipfile.emit('end')
  468. })
  469. })
  470. it('should return error if there is no data', async function (ctx) {
  471. await new Promise(resolve => {
  472. ctx.ArchiveManager._isZipTooLarge(ctx.source, (error, isTooLarge) => {
  473. expect(error).to.exist
  474. resolve()
  475. })
  476. ctx.zipfile.emit('end')
  477. })
  478. })
  479. })
  480. describe('findTopLevelDirectory', function () {
  481. beforeEach(function (ctx) {
  482. ctx.fs.readdir = sinon.stub()
  483. ctx.fs.stat((ctx.directory = 'test/directory'))
  484. })
  485. describe('with multiple files', function () {
  486. beforeEach(function (ctx) {
  487. ctx.fs.readdir.callsArgWith(1, null, ['multiple', 'files'])
  488. ctx.ArchiveManager.findTopLevelDirectory(ctx.directory, ctx.callback)
  489. })
  490. it('should find the files in the directory', function (ctx) {
  491. ctx.fs.readdir.calledWith(ctx.directory).should.equal(true)
  492. })
  493. it('should return the original directory', function (ctx) {
  494. ctx.callback.calledWith(null, ctx.directory).should.equal(true)
  495. })
  496. })
  497. describe('with a single file (not folder)', function () {
  498. beforeEach(function (ctx) {
  499. ctx.fs.readdir.callsArgWith(1, null, ['foo.tex'])
  500. ctx.fs.stat.callsArgWith(1, null, {
  501. isDirectory() {
  502. return false
  503. },
  504. })
  505. ctx.ArchiveManager.findTopLevelDirectory(ctx.directory, ctx.callback)
  506. })
  507. it('should check if the file is a directory', function (ctx) {
  508. ctx.fs.stat.calledWith(ctx.directory + '/foo.tex').should.equal(true)
  509. })
  510. it('should return the original directory', function (ctx) {
  511. ctx.callback.calledWith(null, ctx.directory).should.equal(true)
  512. })
  513. })
  514. describe('with a single top-level folder', function () {
  515. beforeEach(function (ctx) {
  516. ctx.fs.readdir.callsArgWith(1, null, ['folder'])
  517. ctx.fs.stat.callsArgWith(1, null, {
  518. isDirectory() {
  519. return true
  520. },
  521. })
  522. ctx.ArchiveManager.findTopLevelDirectory(ctx.directory, ctx.callback)
  523. })
  524. it('should check if the file is a directory', function (ctx) {
  525. ctx.fs.stat.calledWith(ctx.directory + '/folder').should.equal(true)
  526. })
  527. it('should return the child directory', function (ctx) {
  528. ctx.callback
  529. .calledWith(null, ctx.directory + '/folder')
  530. .should.equal(true)
  531. })
  532. })
  533. })
  534. })