resolve.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 mocha = require('mocha');
  20. const expect = require('chai').expect;
  21. const path = require('path');
  22. const resolve = require('@joystream/storage-utils/fs/resolve');
  23. function tests(base)
  24. {
  25. it('resolves absolute paths relative to the base', function()
  26. {
  27. const resolved = resolve(base, '/foo');
  28. const relative = path.relative(base, resolved);
  29. expect(relative).to.equal('foo');
  30. });
  31. it('allows for relative paths that stay in the base', function()
  32. {
  33. const resolved = resolve(base, 'foo/../bar');
  34. const relative = path.relative(base, resolved);
  35. expect(relative).to.equal('bar');
  36. });
  37. it('prevents relative paths from breaking out of the base', function()
  38. {
  39. expect(() => resolve(base, '../foo')).to.throw();
  40. });
  41. it('prevents long relative paths from breaking out of the base', function()
  42. {
  43. expect(() => resolve(base, '../../../foo')).to.throw();
  44. });
  45. it('prevents sneaky relative paths from breaking out of the base', function()
  46. {
  47. expect(() => resolve(base, 'foo/../../../bar')).to.throw();
  48. });
  49. }
  50. describe('util/fs/resolve', function()
  51. {
  52. describe('slash base', function()
  53. {
  54. tests('/');
  55. });
  56. describe('empty base', function()
  57. {
  58. tests('');
  59. });
  60. describe('short base', function()
  61. {
  62. tests('/base');
  63. });
  64. describe('long base', function()
  65. {
  66. tests('/this/base/is/very/long/indeed');
  67. });
  68. });