resolve.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict'
  19. const expect = require('chai').expect
  20. const path = require('path')
  21. const resolve = require('@joystream/storage-utils/fs/resolve')
  22. function tests(base) {
  23. it('resolves absolute paths relative to the base', function () {
  24. const resolved = resolve(base, '/foo')
  25. const relative = path.relative(base, resolved)
  26. expect(relative).to.equal('foo')
  27. })
  28. it('allows for relative paths that stay in the base', function () {
  29. const resolved = resolve(base, 'foo/../bar')
  30. const relative = path.relative(base, resolved)
  31. expect(relative).to.equal('bar')
  32. })
  33. it('prevents relative paths from breaking out of the base', function () {
  34. expect(() => resolve(base, '../foo')).to.throw()
  35. })
  36. it('prevents long relative paths from breaking out of the base', function () {
  37. expect(() => resolve(base, '../../../foo')).to.throw()
  38. })
  39. it('prevents sneaky relative paths from breaking out of the base', function () {
  40. expect(() => resolve(base, 'foo/../../../bar')).to.throw()
  41. })
  42. }
  43. describe('util/fs/resolve', function () {
  44. describe('slash base', function () {
  45. tests('/')
  46. })
  47. describe('empty base', function () {
  48. tests('')
  49. })
  50. describe('short base', function () {
  51. tests('/base')
  52. })
  53. describe('long base', function () {
  54. tests('/this/base/is/very/long/indeed')
  55. })
  56. })