You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chroot.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package test
  2. import (
  3. "os"
  4. . "gopkg.in/check.v1"
  5. . "gopkg.in/src-d/go-billy.v4"
  6. "gopkg.in/src-d/go-billy.v4/util"
  7. )
  8. // ChrootSuite is a convenient test suite to validate any implementation of
  9. // billy.Chroot
  10. type ChrootSuite struct {
  11. FS interface {
  12. Basic
  13. Chroot
  14. }
  15. }
  16. func (s *ChrootSuite) TestCreateWithChroot(c *C) {
  17. fs, _ := s.FS.Chroot("foo")
  18. f, err := fs.Create("bar")
  19. c.Assert(err, IsNil)
  20. c.Assert(f.Close(), IsNil)
  21. c.Assert(f.Name(), Equals, "bar")
  22. f, err = s.FS.Open("foo/bar")
  23. c.Assert(err, IsNil)
  24. c.Assert(f.Name(), Equals, s.FS.Join("foo", "bar"))
  25. c.Assert(f.Close(), IsNil)
  26. }
  27. func (s *ChrootSuite) TestOpenWithChroot(c *C) {
  28. fs, _ := s.FS.Chroot("foo")
  29. f, err := fs.Create("bar")
  30. c.Assert(err, IsNil)
  31. c.Assert(f.Close(), IsNil)
  32. c.Assert(f.Name(), Equals, "bar")
  33. f, err = fs.Open("bar")
  34. c.Assert(err, IsNil)
  35. c.Assert(f.Name(), Equals, "bar")
  36. c.Assert(f.Close(), IsNil)
  37. }
  38. func (s *ChrootSuite) TestOpenOutOffBoundary(c *C) {
  39. err := util.WriteFile(s.FS, "bar", nil, 0644)
  40. c.Assert(err, IsNil)
  41. fs, _ := s.FS.Chroot("foo")
  42. f, err := fs.Open("../bar")
  43. c.Assert(err, Equals, ErrCrossedBoundary)
  44. c.Assert(f, IsNil)
  45. }
  46. func (s *ChrootSuite) TestStatOutOffBoundary(c *C) {
  47. err := util.WriteFile(s.FS, "bar", nil, 0644)
  48. c.Assert(err, IsNil)
  49. fs, _ := s.FS.Chroot("foo")
  50. f, err := fs.Stat("../bar")
  51. c.Assert(err, Equals, ErrCrossedBoundary)
  52. c.Assert(f, IsNil)
  53. }
  54. func (s *ChrootSuite) TestStatWithChroot(c *C) {
  55. files := []string{"foo", "bar", "qux/baz", "qux/qux"}
  56. for _, name := range files {
  57. err := util.WriteFile(s.FS, name, nil, 0644)
  58. c.Assert(err, IsNil)
  59. }
  60. // Some implementations detect directories based on a prefix
  61. // for all files; it's easy to miss path separator handling there.
  62. fi, err := s.FS.Stat("qu")
  63. c.Assert(os.IsNotExist(err), Equals, true, Commentf("error: %s", err))
  64. c.Assert(fi, IsNil)
  65. fi, err = s.FS.Stat("qux")
  66. c.Assert(err, IsNil)
  67. c.Assert(fi.Name(), Equals, "qux")
  68. c.Assert(fi.IsDir(), Equals, true)
  69. qux, _ := s.FS.Chroot("qux")
  70. fi, err = qux.Stat("baz")
  71. c.Assert(err, IsNil)
  72. c.Assert(fi.Name(), Equals, "baz")
  73. c.Assert(fi.IsDir(), Equals, false)
  74. fi, err = qux.Stat("/baz")
  75. c.Assert(err, IsNil)
  76. c.Assert(fi.Name(), Equals, "baz")
  77. c.Assert(fi.IsDir(), Equals, false)
  78. }
  79. func (s *ChrootSuite) TestRenameOutOffBoundary(c *C) {
  80. err := util.WriteFile(s.FS, "foo/foo", nil, 0644)
  81. c.Assert(err, IsNil)
  82. err = util.WriteFile(s.FS, "bar", nil, 0644)
  83. c.Assert(err, IsNil)
  84. fs, _ := s.FS.Chroot("foo")
  85. err = fs.Rename("../bar", "foo")
  86. c.Assert(err, Equals, ErrCrossedBoundary)
  87. err = fs.Rename("foo", "../bar")
  88. c.Assert(err, Equals, ErrCrossedBoundary)
  89. }
  90. func (s *ChrootSuite) TestRemoveOutOffBoundary(c *C) {
  91. err := util.WriteFile(s.FS, "bar", nil, 0644)
  92. c.Assert(err, IsNil)
  93. fs, _ := s.FS.Chroot("foo")
  94. err = fs.Remove("../bar")
  95. c.Assert(err, Equals, ErrCrossedBoundary)
  96. }
  97. func (s *FilesystemSuite) TestRoot(c *C) {
  98. c.Assert(s.FS.Root(), Not(Equals), "")
  99. }