| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"runtime" |
| 6 |
|
| 7 |
. "github.com/go-git/go-billy/v5" |
| 8 |
"github.com/go-git/go-billy/v5/util" |
| 9 |
. "gopkg.in/check.v1" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
type FilesystemSuite struct { |
| 15 |
FS Filesystem |
| 16 |
|
| 17 |
BasicSuite |
| 18 |
DirSuite |
| 19 |
SymlinkSuite |
| 20 |
TempFileSuite |
| 21 |
ChrootSuite |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
func NewFilesystemSuite(fs Filesystem) FilesystemSuite { |
| 26 |
s := FilesystemSuite{FS: fs} |
| 27 |
s.BasicSuite.FS = s.FS |
| 28 |
s.DirSuite.FS = s.FS |
| 29 |
s.SymlinkSuite.FS = s.FS |
| 30 |
s.TempFileSuite.FS = s.FS |
| 31 |
s.ChrootSuite.FS = s.FS |
| 32 |
|
| 33 |
return s |
| 34 |
} |
| 35 |
|
| 36 |
func (s *FilesystemSuite) TestSymlinkToDir(c *C) { |
| 37 |
if runtime.GOOS == "plan9" { |
| 38 |
c.Skip("skipping on Plan 9; symlinks are not supported") |
| 39 |
} |
| 40 |
err := s.FS.MkdirAll("dir", 0755) |
| 41 |
c.Assert(err, IsNil) |
| 42 |
|
| 43 |
err = s.FS.Symlink("dir", "link") |
| 44 |
c.Assert(err, IsNil) |
| 45 |
|
| 46 |
fi, err := s.FS.Stat("link") |
| 47 |
c.Assert(err, IsNil) |
| 48 |
c.Assert(fi.Name(), Equals, "link") |
| 49 |
c.Assert(fi.IsDir(), Equals, true) |
| 50 |
} |
| 51 |
|
| 52 |
func (s *FilesystemSuite) TestSymlinkReadDir(c *C) { |
| 53 |
if runtime.GOOS == "plan9" { |
| 54 |
c.Skip("skipping on Plan 9; symlinks are not supported") |
| 55 |
} |
| 56 |
err := util.WriteFile(s.FS, "dir/file", []byte("foo"), 0644) |
| 57 |
c.Assert(err, IsNil) |
| 58 |
|
| 59 |
err = s.FS.Symlink("dir", "link") |
| 60 |
c.Assert(err, IsNil) |
| 61 |
|
| 62 |
info, err := s.FS.ReadDir("link") |
| 63 |
c.Assert(err, IsNil) |
| 64 |
c.Assert(info, HasLen, 1) |
| 65 |
|
| 66 |
c.Assert(info[0].Size(), Equals, int64(3)) |
| 67 |
c.Assert(info[0].IsDir(), Equals, false) |
| 68 |
c.Assert(info[0].Name(), Equals, "file") |
| 69 |
} |
| 70 |
|
| 71 |
func (s *FilesystemSuite) TestCreateWithExistantDir(c *C) { |
| 72 |
err := s.FS.MkdirAll("foo", 0644) |
| 73 |
c.Assert(err, IsNil) |
| 74 |
|
| 75 |
f, err := s.FS.Create("foo") |
| 76 |
c.Assert(err, NotNil) |
| 77 |
c.Assert(f, IsNil) |
| 78 |
} |
| 79 |
|
| 80 |
func (s *ChrootSuite) TestReadDirWithChroot(c *C) { |
| 81 |
files := []string{"foo", "bar", "qux/baz", "qux/qux"} |
| 82 |
for _, name := range files { |
| 83 |
err := util.WriteFile(s.FS, name, nil, 0644) |
| 84 |
c.Assert(err, IsNil) |
| 85 |
} |
| 86 |
|
| 87 |
qux, _ := s.FS.Chroot("/qux") |
| 88 |
|
| 89 |
info, err := qux.(Filesystem).ReadDir("/") |
| 90 |
c.Assert(err, IsNil) |
| 91 |
c.Assert(info, HasLen, 2) |
| 92 |
} |
| 93 |
|
| 94 |
func (s *FilesystemSuite) TestSymlinkWithChrootBasic(c *C) { |
| 95 |
if runtime.GOOS == "plan9" { |
| 96 |
c.Skip("skipping on Plan 9; symlinks are not supported") |
| 97 |
} |
| 98 |
qux, _ := s.FS.Chroot("/qux") |
| 99 |
|
| 100 |
err := util.WriteFile(qux, "file", nil, 0644) |
| 101 |
c.Assert(err, IsNil) |
| 102 |
|
| 103 |
err = qux.(Filesystem).Symlink("file", "link") |
| 104 |
c.Assert(err, IsNil) |
| 105 |
|
| 106 |
fi, err := qux.Stat("link") |
| 107 |
c.Assert(err, IsNil) |
| 108 |
c.Assert(fi.Name(), Equals, "link") |
| 109 |
|
| 110 |
fi, err = s.FS.Stat("qux/link") |
| 111 |
c.Assert(err, IsNil) |
| 112 |
c.Assert(fi.Name(), Equals, "link") |
| 113 |
} |
| 114 |
|
| 115 |
func (s *FilesystemSuite) TestSymlinkWithChrootCrossBounders(c *C) { |
| 116 |
if runtime.GOOS == "plan9" { |
| 117 |
c.Skip("skipping on Plan 9; symlinks are not supported") |
| 118 |
} |
| 119 |
qux, _ := s.FS.Chroot("/qux") |
| 120 |
util.WriteFile(s.FS, "file", []byte("foo"), customMode) |
| 121 |
|
| 122 |
err := qux.Symlink("../../file", "qux/link") |
| 123 |
c.Assert(err, Equals, nil) |
| 124 |
|
| 125 |
fi, err := qux.Lstat("qux/link") |
| 126 |
c.Assert(fi, NotNil) |
| 127 |
c.Assert(err, Equals, nil) |
| 128 |
|
| 129 |
fi, err = qux.Stat("qux/link") |
| 130 |
c.Assert(fi, IsNil) |
| 131 |
c.Assert(err, Equals, ErrCrossedBoundary) |
| 132 |
} |
| 133 |
|
| 134 |
func (s *FilesystemSuite) TestReadDirWithLink(c *C) { |
| 135 |
if runtime.GOOS == "plan9" { |
| 136 |
c.Skip("skipping on Plan 9; symlinks are not supported") |
| 137 |
} |
| 138 |
util.WriteFile(s.FS, "foo/bar", []byte("foo"), customMode) |
| 139 |
s.FS.Symlink("bar", "foo/qux") |
| 140 |
|
| 141 |
info, err := s.FS.ReadDir("/foo") |
| 142 |
c.Assert(err, IsNil) |
| 143 |
c.Assert(info, HasLen, 2) |
| 144 |
} |
| 145 |
|
| 146 |
func (s *FilesystemSuite) TestRemoveAllNonExistent(c *C) { |
| 147 |
c.Assert(util.RemoveAll(s.FS, "non-existent"), IsNil) |
| 148 |
} |
| 149 |
|
| 150 |
func (s *FilesystemSuite) TestRemoveAllEmptyDir(c *C) { |
| 151 |
c.Assert(s.FS.MkdirAll("empty", os.FileMode(0755)), IsNil) |
| 152 |
c.Assert(util.RemoveAll(s.FS, "empty"), IsNil) |
| 153 |
_, err := s.FS.Stat("empty") |
| 154 |
c.Assert(err, NotNil) |
| 155 |
c.Assert(os.IsNotExist(err), Equals, true) |
| 156 |
} |
| 157 |
|
| 158 |
func (s *FilesystemSuite) TestRemoveAll(c *C) { |
| 159 |
fnames := []string{ |
| 160 |
"foo/1", |
| 161 |
"foo/2", |
| 162 |
"foo/bar/1", |
| 163 |
"foo/bar/2", |
| 164 |
"foo/bar/baz/1", |
| 165 |
"foo/bar/baz/qux/1", |
| 166 |
"foo/bar/baz/qux/2", |
| 167 |
"foo/bar/baz/qux/3", |
| 168 |
} |
| 169 |
|
| 170 |
for _, fname := range fnames { |
| 171 |
err := util.WriteFile(s.FS, fname, nil, 0644) |
| 172 |
c.Assert(err, IsNil) |
| 173 |
} |
| 174 |
|
| 175 |
c.Assert(util.RemoveAll(s.FS, "foo"), IsNil) |
| 176 |
|
| 177 |
for _, fname := range fnames { |
| 178 |
_, err := s.FS.Stat(fname) |
| 179 |
comment := Commentf("not removed: %s %s", fname, err) |
| 180 |
c.Assert(os.IsNotExist(err), Equals, true, comment) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func (s *FilesystemSuite) TestRemoveAllRelative(c *C) { |
| 185 |
fnames := []string{ |
| 186 |
"foo/1", |
| 187 |
"foo/2", |
| 188 |
"foo/bar/1", |
| 189 |
"foo/bar/2", |
| 190 |
"foo/bar/baz/1", |
| 191 |
"foo/bar/baz/qux/1", |
| 192 |
"foo/bar/baz/qux/2", |
| 193 |
"foo/bar/baz/qux/3", |
| 194 |
} |
| 195 |
|
| 196 |
for _, fname := range fnames { |
| 197 |
err := util.WriteFile(s.FS, fname, nil, 0644) |
| 198 |
c.Assert(err, IsNil) |
| 199 |
} |
| 200 |
|
| 201 |
c.Assert(util.RemoveAll(s.FS, "foo/bar/.."), IsNil) |
| 202 |
|
| 203 |
for _, fname := range fnames { |
| 204 |
_, err := s.FS.Stat(fname) |
| 205 |
comment := Commentf("not removed: %s %s", fname, err) |
| 206 |
c.Assert(os.IsNotExist(err), Equals, true, comment) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func (s *FilesystemSuite) TestReadDir(c *C) { |
| 211 |
err := s.FS.MkdirAll("qux", 0755) |
| 212 |
c.Assert(err, IsNil) |
| 213 |
|
| 214 |
files := []string{"foo", "bar", "qux/baz", "qux/qux"} |
| 215 |
for _, name := range files { |
| 216 |
err := util.WriteFile(s.FS, name, nil, 0644) |
| 217 |
c.Assert(err, IsNil) |
| 218 |
} |
| 219 |
|
| 220 |
info, err := s.FS.ReadDir("/") |
| 221 |
c.Assert(err, IsNil) |
| 222 |
c.Assert(info, HasLen, 3) |
| 223 |
|
| 224 |
info, err = s.FS.ReadDir("/qux") |
| 225 |
c.Assert(err, IsNil) |
| 226 |
c.Assert(info, HasLen, 2) |
| 227 |
} |