Debian Codegraph @

test/chroot.go

raw symbols SCIP
1 package test
2
3 import (
4 "os"
5 "path/filepath"
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 // ChrootSuite is a convenient test suite to validate any implementation of
13 // billy.Chroot
14 type ChrootSuite struct {
15 FS interface {
16 Basic
17 Chroot
18 }
19 }
20
21 func (s *ChrootSuite) TestCreateWithChroot(c *C) {
22 fs, _ := s.FS.Chroot("foo")
23 f, err := fs.Create("bar")
24 c.Assert(err, IsNil)
25 c.Assert(f.Close(), IsNil)
26 c.Assert(f.Name(), Equals, "bar")
27
28 f, err = s.FS.Open("foo/bar")
29 c.Assert(err, IsNil)
30 c.Assert(f.Name(), Equals, s.FS.Join("foo", "bar"))
31 c.Assert(f.Close(), IsNil)
32 }
33
34 func (s *ChrootSuite) TestCreateOutOffBoundaryVariants(c *C) {
35 tests := []string{
36 "..",
37 "foo/../..",
38 "/../foo",
39 "/foo/../..",
40 "/foo/../../bar",
41 "foo" + string(filepath.Separator) + "../..",
42 "/foo" + string(filepath.Separator) + "../..",
43 }
44
45 fs, _ := s.FS.Chroot("foo")
46 for _, path := range tests {
47 f, err := fs.Create(path)
48 c.Assert(err, Equals, ErrCrossedBoundary, Commentf("path: %q", path))
49 c.Assert(f, IsNil, Commentf("path: %q", path))
50 }
51 }
52
53 func (s *ChrootSuite) TestOpenWithChroot(c *C) {
54 fs, _ := s.FS.Chroot("foo")
55 f, err := fs.Create("bar")
56 c.Assert(err, IsNil)
57 c.Assert(f.Close(), IsNil)
58 c.Assert(f.Name(), Equals, "bar")
59
60 f, err = fs.Open("bar")
61 c.Assert(err, IsNil)
62 c.Assert(f.Name(), Equals, "bar")
63 c.Assert(f.Close(), IsNil)
64 }
65
66 func (s *ChrootSuite) TestOpenOutOffBoundary(c *C) {
67 err := util.WriteFile(s.FS, "bar", nil, 0644)
68 c.Assert(err, IsNil)
69
70 fs, _ := s.FS.Chroot("foo")
71 f, err := fs.Open("../bar")
72 c.Assert(err, Equals, ErrCrossedBoundary)
73 c.Assert(f, IsNil)
74 }
75
76 func (s *ChrootSuite) TestStatOutOffBoundary(c *C) {
77 err := util.WriteFile(s.FS, "bar", nil, 0644)
78 c.Assert(err, IsNil)
79
80 fs, _ := s.FS.Chroot("foo")
81 f, err := fs.Stat("../bar")
82 c.Assert(err, Equals, ErrCrossedBoundary)
83 c.Assert(f, IsNil)
84 }
85
86 func (s *ChrootSuite) TestStatWithChroot(c *C) {
87 files := []string{"foo", "bar", "qux/baz", "qux/qux"}
88 for _, name := range files {
89 err := util.WriteFile(s.FS, name, nil, 0644)
90 c.Assert(err, IsNil)
91 }
92
93 // Some implementations detect directories based on a prefix
94 // for all files; it's easy to miss path separator handling there.
95 fi, err := s.FS.Stat("qu")
96 c.Assert(os.IsNotExist(err), Equals, true, Commentf("error: %s", err))
97 c.Assert(fi, IsNil)
98
99 fi, err = s.FS.Stat("qux")
100 c.Assert(err, IsNil)
101 c.Assert(fi.Name(), Equals, "qux")
102 c.Assert(fi.IsDir(), Equals, true)
103
104 qux, _ := s.FS.Chroot("qux")
105
106 fi, err = qux.Stat("baz")
107 c.Assert(err, IsNil)
108 c.Assert(fi.Name(), Equals, "baz")
109 c.Assert(fi.IsDir(), Equals, false)
110
111 fi, err = qux.Stat("/baz")
112 c.Assert(err, IsNil)
113 c.Assert(fi.Name(), Equals, "baz")
114 c.Assert(fi.IsDir(), Equals, false)
115 }
116
117 func (s *ChrootSuite) TestRenameOutOffBoundary(c *C) {
118 err := util.WriteFile(s.FS, "foo/foo", nil, 0644)
119 c.Assert(err, IsNil)
120
121 err = util.WriteFile(s.FS, "bar", nil, 0644)
122 c.Assert(err, IsNil)
123
124 fs, _ := s.FS.Chroot("foo")
125 err = fs.Rename("../bar", "foo")
126 c.Assert(err, Equals, ErrCrossedBoundary)
127
128 err = fs.Rename("foo", "../bar")
129 c.Assert(err, Equals, ErrCrossedBoundary)
130 }
131
132 func (s *ChrootSuite) TestRemoveOutOffBoundary(c *C) {
133 err := util.WriteFile(s.FS, "bar", nil, 0644)
134 c.Assert(err, IsNil)
135
136 fs, _ := s.FS.Chroot("foo")
137 err = fs.Remove("../bar")
138 c.Assert(err, Equals, ErrCrossedBoundary)
139 }
140
141 func (s *FilesystemSuite) TestRoot(c *C) {
142 c.Assert(s.FS.Root(), Not(Equals), "")
143 }