| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- /* eslint-disable
- handle-callback-err,
- max-len,
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS206: Consider reworking classes to avoid initClass
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const sinon = require('sinon')
- const { expect } = require('chai')
- const chai = require('chai')
- const should = chai.should()
- const modulePath = '../../../../app/src/Features/Uploads/ArchiveManager.js'
- const ArchiveErrors = require('../../../../app/src/Features/Uploads/ArchiveErrors')
- const SandboxedModule = require('sandboxed-module')
- const events = require('events')
- describe('ArchiveManager', function() {
- beforeEach(function() {
- let Timer
- this.logger = {
- error: sinon.stub(),
- warn: sinon.stub(),
- err() {},
- log: sinon.stub()
- }
- this.metrics = {
- Timer: (Timer = (function() {
- Timer = class Timer {
- static initClass() {
- this.prototype.done = sinon.stub()
- }
- }
- Timer.initClass()
- return Timer
- })())
- }
- this.zipfile = new events.EventEmitter()
- this.zipfile.readEntry = sinon.stub()
- this.zipfile.close = sinon.stub()
- this.ArchiveManager = SandboxedModule.require(modulePath, {
- globals: {
- console: console
- },
- requires: {
- 'settings-sharelatex': {},
- yauzl: (this.yauzl = {
- open: sinon.stub().callsArgWith(2, null, this.zipfile)
- }),
- 'logger-sharelatex': this.logger,
- '@overleaf/metrics': this.metrics,
- fs: (this.fs = {}),
- 'fs-extra': (this.fse = {}),
- './ArchiveErrors': ArchiveErrors
- }
- })
- return (this.callback = sinon.stub())
- })
- describe('extractZipArchive', function() {
- beforeEach(function() {
- this.source = '/path/to/zip/source.zip'
- this.destination = '/path/to/zip/destination'
- return (this.ArchiveManager._isZipTooLarge = sinon
- .stub()
- .callsArgWith(1, null, false))
- })
- describe('successfully', function() {
- beforeEach(function(done) {
- this.readStream = new events.EventEmitter()
- this.readStream.pipe = sinon.stub()
- this.zipfile.openReadStream = sinon
- .stub()
- .callsArgWith(1, null, this.readStream)
- this.writeStream = new events.EventEmitter()
- this.fs.createWriteStream = sinon.stub().returns(this.writeStream)
- this.fse.ensureDir = sinon.stub().callsArg(1)
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- done
- )
- // entry contains a single file
- this.zipfile.emit('entry', { fileName: 'testfile.txt' })
- this.readStream.emit('end')
- return this.zipfile.emit('end')
- })
- it('should run yauzl', function() {
- return this.yauzl.open.calledWith(this.source).should.equal(true)
- })
- it('should time the unzip', function() {
- return this.metrics.Timer.prototype.done.called.should.equal(true)
- })
- })
- describe('with a zipfile containing an empty directory', function() {
- beforeEach(function(done) {
- this.readStream = new events.EventEmitter()
- this.readStream.pipe = sinon.stub()
- this.zipfile.openReadStream = sinon
- .stub()
- .callsArgWith(1, null, this.readStream)
- this.writeStream = new events.EventEmitter()
- this.fs.createWriteStream = sinon.stub().returns(this.writeStream)
- this.fse.ensureDir = sinon.stub().callsArg(1)
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- done()
- }
- )
- // entry contains a single, empty directory
- this.zipfile.emit('entry', { fileName: 'testdir/' })
- this.readStream.emit('end')
- return this.zipfile.emit('end')
- })
- it('should return the callback with an error', function() {
- return sinon.assert.calledWithExactly(
- this.callback,
- sinon.match.instanceOf(ArchiveErrors.EmptyZipFileError)
- )
- })
- })
- describe('with an empty zipfile', function() {
- beforeEach(function(done) {
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- return this.zipfile.emit('end')
- })
- it('should return the callback with an error', function() {
- return sinon.assert.calledWithExactly(
- this.callback,
- sinon.match.instanceOf(ArchiveErrors.EmptyZipFileError)
- )
- })
- })
- describe('with an error in the zip file header', function() {
- beforeEach(function(done) {
- this.yauzl.open = sinon
- .stub()
- .callsArgWith(2, new ArchiveErrors.InvalidZipFileError())
- return this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- })
- it('should return the callback with an error', function() {
- return sinon.assert.calledWithExactly(
- this.callback,
- sinon.match.instanceOf(ArchiveErrors.InvalidZipFileError)
- )
- })
- })
- describe('with a zip that is too large', function() {
- beforeEach(function(done) {
- this.ArchiveManager._isZipTooLarge = sinon
- .stub()
- .callsArgWith(1, null, true)
- return this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- })
- it('should return the callback with an error', function() {
- return sinon.assert.calledWithExactly(
- this.callback,
- sinon.match.instanceOf(ArchiveErrors.ZipContentsTooLargeError)
- )
- })
- it('should not call yauzl.open', function() {
- return this.yauzl.open.called.should.equal(false)
- })
- })
- describe('with an error in the extracted files', function() {
- beforeEach(function(done) {
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- return this.zipfile.emit('error', new Error('Something went wrong'))
- })
- it('should return the callback with an error', function() {
- return this.callback.should.have.been.calledWithExactly(
- sinon.match
- .instanceOf(Error)
- .and(sinon.match.has('message', 'Something went wrong'))
- )
- })
- })
- describe('with a relative extracted file path', function() {
- beforeEach(function(done) {
- this.zipfile.openReadStream = sinon.stub()
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- this.zipfile.emit('entry', { fileName: '../testfile.txt' })
- return this.zipfile.emit('end')
- })
- it('should not write try to read the file entry', function() {
- return this.zipfile.openReadStream.called.should.equal(false)
- })
- })
- describe('with an unnormalized extracted file path', function() {
- beforeEach(function(done) {
- this.zipfile.openReadStream = sinon.stub()
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- this.zipfile.emit('entry', { fileName: 'foo/./testfile.txt' })
- return this.zipfile.emit('end')
- })
- it('should not try to read the file entry', function() {
- return this.zipfile.openReadStream.called.should.equal(false)
- })
- })
- describe('with backslashes in the path', function() {
- beforeEach(function(done) {
- this.readStream = new events.EventEmitter()
- this.readStream.pipe = sinon.stub()
- this.writeStream = new events.EventEmitter()
- this.fs.createWriteStream = sinon.stub().returns(this.writeStream)
- this.zipfile.openReadStream = sinon
- .stub()
- .callsArgWith(1, null, this.readStream)
- this.fse.ensureDir = sinon.stub().callsArg(1)
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- this.zipfile.emit('entry', { fileName: 'wombat\\foo.tex' })
- this.zipfile.emit('entry', { fileName: 'potato\\bar.tex' })
- return this.zipfile.emit('end')
- })
- it('should read the file entry with its original path', function() {
- this.zipfile.openReadStream.should.be.calledWith({
- fileName: 'wombat\\foo.tex'
- })
- return this.zipfile.openReadStream.should.be.calledWith({
- fileName: 'potato\\bar.tex'
- })
- })
- it('should treat the backslashes as a directory separator when creating the directory', function() {
- this.fse.ensureDir.should.be.calledWith(`${this.destination}/wombat`)
- return this.fse.ensureDir.should.be.calledWith(
- `${this.destination}/potato`
- )
- })
- it('should treat the backslashes as a directory separator when creating the file', function() {
- this.fs.createWriteStream.should.be.calledWith(
- `${this.destination}/wombat/foo.tex`
- )
- return this.fs.createWriteStream.should.be.calledWith(
- `${this.destination}/potato/bar.tex`
- )
- })
- })
- describe('with a directory entry', function() {
- beforeEach(function(done) {
- this.zipfile.openReadStream = sinon.stub()
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- this.zipfile.emit('entry', { fileName: 'testdir/' })
- return this.zipfile.emit('end')
- })
- it('should not try to read the entry', function() {
- return this.zipfile.openReadStream.called.should.equal(false)
- })
- })
- describe('with an error opening the file read stream', function() {
- beforeEach(function(done) {
- this.zipfile.openReadStream = sinon
- .stub()
- .callsArgWith(1, new Error('Something went wrong'))
- this.writeStream = new events.EventEmitter()
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- this.zipfile.emit('entry', { fileName: 'testfile.txt' })
- return this.zipfile.emit('end')
- })
- it('should return the callback with an error', function() {
- return this.callback.should.have.been.calledWithExactly(
- sinon.match
- .instanceOf(Error)
- .and(sinon.match.has('message', 'Something went wrong'))
- )
- })
- it('should close the zipfile', function() {
- return this.zipfile.close.called.should.equal(true)
- })
- })
- describe('with an error in the file read stream', function() {
- beforeEach(function(done) {
- this.readStream = new events.EventEmitter()
- this.readStream.pipe = sinon.stub()
- this.zipfile.openReadStream = sinon
- .stub()
- .callsArgWith(1, null, this.readStream)
- this.writeStream = new events.EventEmitter()
- this.fs.createWriteStream = sinon.stub().returns(this.writeStream)
- this.fse.ensureDir = sinon.stub().callsArg(1)
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- this.zipfile.emit('entry', { fileName: 'testfile.txt' })
- this.readStream.emit('error', new Error('Something went wrong'))
- return this.zipfile.emit('end')
- })
- it('should return the callback with an error', function() {
- return this.callback.should.have.been.calledWithExactly(
- sinon.match
- .instanceOf(Error)
- .and(sinon.match.has('message', 'Something went wrong'))
- )
- })
- it('should close the zipfile', function() {
- return this.zipfile.close.called.should.equal(true)
- })
- })
- describe('with an error in the file write stream', function() {
- beforeEach(function(done) {
- this.readStream = new events.EventEmitter()
- this.readStream.pipe = sinon.stub()
- this.readStream.unpipe = sinon.stub()
- this.readStream.destroy = sinon.stub()
- this.zipfile.openReadStream = sinon
- .stub()
- .callsArgWith(1, null, this.readStream)
- this.writeStream = new events.EventEmitter()
- this.fs.createWriteStream = sinon.stub().returns(this.writeStream)
- this.fse.ensureDir = sinon.stub().callsArg(1)
- this.ArchiveManager.extractZipArchive(
- this.source,
- this.destination,
- error => {
- this.callback(error)
- return done()
- }
- )
- this.zipfile.emit('entry', { fileName: 'testfile.txt' })
- this.writeStream.emit('error', new Error('Something went wrong'))
- return this.zipfile.emit('end')
- })
- it('should return the callback with an error', function() {
- return this.callback.should.have.been.calledWithExactly(
- sinon.match
- .instanceOf(Error)
- .and(sinon.match.has('message', 'Something went wrong'))
- )
- })
- it('should unpipe from the readstream', function() {
- return this.readStream.unpipe.called.should.equal(true)
- })
- it('should destroy the readstream', function() {
- return this.readStream.destroy.called.should.equal(true)
- })
- it('should close the zipfile', function() {
- return this.zipfile.close.called.should.equal(true)
- })
- })
- })
- describe('_isZipTooLarge', function() {
- it('should return false with small output', function(done) {
- this.ArchiveManager._isZipTooLarge(this.source, (error, isTooLarge) => {
- isTooLarge.should.equal(false)
- return done()
- })
- this.zipfile.emit('entry', { uncompressedSize: 109042 })
- return this.zipfile.emit('end')
- })
- it('should return true with large bytes', function(done) {
- this.ArchiveManager._isZipTooLarge(this.source, (error, isTooLarge) => {
- isTooLarge.should.equal(true)
- return done()
- })
- this.zipfile.emit('entry', { uncompressedSize: 1090000000000000042 })
- return this.zipfile.emit('end')
- })
- it('should return error on no data', function(done) {
- this.ArchiveManager._isZipTooLarge(this.source, (error, isTooLarge) => {
- expect(error).to.exist
- return done()
- })
- this.zipfile.emit('entry', {})
- return this.zipfile.emit('end')
- })
- it("should return error if it didn't get a number", function(done) {
- this.ArchiveManager._isZipTooLarge(this.source, (error, isTooLarge) => {
- expect(error).to.exist
- return done()
- })
- this.zipfile.emit('entry', { uncompressedSize: 'random-error' })
- return this.zipfile.emit('end')
- })
- it('should return error if there is no data', function(done) {
- this.ArchiveManager._isZipTooLarge(this.source, (error, isTooLarge) => {
- expect(error).to.exist
- return done()
- })
- return this.zipfile.emit('end')
- })
- })
- describe('findTopLevelDirectory', function() {
- beforeEach(function() {
- this.fs.readdir = sinon.stub()
- this.fs.stat = sinon.stub()
- return (this.directory = 'test/directory')
- })
- describe('with multiple files', function() {
- beforeEach(function() {
- this.fs.readdir.callsArgWith(1, null, ['multiple', 'files'])
- return this.ArchiveManager.findTopLevelDirectory(
- this.directory,
- this.callback
- )
- })
- it('should find the files in the directory', function() {
- return this.fs.readdir.calledWith(this.directory).should.equal(true)
- })
- it('should return the original directory', function() {
- return this.callback.calledWith(null, this.directory).should.equal(true)
- })
- })
- describe('with a single file (not folder)', function() {
- beforeEach(function() {
- this.fs.readdir.callsArgWith(1, null, ['foo.tex'])
- this.fs.stat.callsArgWith(1, null, {
- isDirectory() {
- return false
- }
- })
- return this.ArchiveManager.findTopLevelDirectory(
- this.directory,
- this.callback
- )
- })
- it('should check if the file is a directory', function() {
- return this.fs.stat
- .calledWith(this.directory + '/foo.tex')
- .should.equal(true)
- })
- it('should return the original directory', function() {
- return this.callback.calledWith(null, this.directory).should.equal(true)
- })
- })
- describe('with a single top-level folder', function() {
- beforeEach(function() {
- this.fs.readdir.callsArgWith(1, null, ['folder'])
- this.fs.stat.callsArgWith(1, null, {
- isDirectory() {
- return true
- }
- })
- return this.ArchiveManager.findTopLevelDirectory(
- this.directory,
- this.callback
- )
- })
- it('should check if the file is a directory', function() {
- return this.fs.stat
- .calledWith(this.directory + '/folder')
- .should.equal(true)
- })
- it('should return the child directory', function() {
- return this.callback
- .calledWith(null, this.directory + '/folder')
- .should.equal(true)
- })
- })
- })
- })
|