ArchiveManagerTests.js 18 KB

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