ArchiveManagerTests.js 19 KB

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