ArchiveManager.test.mjs 19 KB

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