| 1 | package test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "testing" |
| 7 | |
| 8 | . "github.com/go-git/go-billy/v6" //nolint |
| 9 | "github.com/go-git/go-billy/v6/osfs" |
| 10 | "github.com/go-git/go-billy/v6/util" |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | ) |
| 14 | |
| 15 | type chrootFS interface { |
| 16 | Basic |
| 17 | Chroot |
| 18 | } |
| 19 | |
| 20 | func eachChrootFS(t *testing.T, test func(t *testing.T, fs chrootFS)) { |
| 21 | t.Helper() |
| 22 | for _, fs := range allFS(t.TempDir) { |
| 23 | t.Run(fmt.Sprintf("%T", fs), func(t *testing.T) { |
| 24 | test(t, fs) |
| 25 | }) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestCreateWithChroot(t *testing.T) { |
| 30 | eachChrootFS(t, func(t *testing.T, fs chrootFS) { |
| 31 | t.Helper() |
| 32 | chroot, _ := fs.Chroot("foo") |
| 33 | f, err := chroot.Create("bar") |
| 34 | require.NoError(t, err) |
| 35 | require.NoError(t, f.Close()) |
| 36 | assert.Equal(t, f.Name(), "bar") |
| 37 | |
| 38 | f, err = fs.Open("foo/bar") |
| 39 | require.NoError(t, err) |
| 40 | assert.Equal(t, f.Name(), fs.Join("foo", "bar")) |
| 41 | require.NoError(t, f.Close()) |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | func TestOpenWithChroot(t *testing.T) { |
| 46 | eachChrootFS(t, func(t *testing.T, fs chrootFS) { |
| 47 | t.Helper() |
| 48 | chroot, _ := fs.Chroot("foo") |
| 49 | f, err := chroot.Create("bar") |
| 50 | require.NoError(t, err) |
| 51 | require.NoError(t, f.Close()) |
| 52 | assert.Equal(t, f.Name(), "bar") |
| 53 | |
| 54 | f, err = chroot.Open("bar") |
| 55 | require.NoError(t, err) |
| 56 | assert.Equal(t, f.Name(), "bar") |
| 57 | require.NoError(t, f.Close()) |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | func TestOpenOutOffBoundary(t *testing.T) { |
| 62 | eachChrootFS(t, func(t *testing.T, fs chrootFS) { |
| 63 | t.Helper() |
| 64 | err := util.WriteFile(fs, "bar", []byte("root"), 0o644) |
| 65 | require.NoError(t, err) |
| 66 | err = util.WriteFile(fs, "foo/bar", []byte("chroot"), 0o644) |
| 67 | require.NoError(t, err) |
| 68 | |
| 69 | chroot, _ := fs.Chroot("foo") |
| 70 | f, err := chroot.Open("../bar") |
| 71 | if _, ok := chroot.(*osfs.BoundOS); !ok { |
| 72 | require.ErrorIs(t, err, ErrCrossedBoundary) |
| 73 | assert.Nil(t, f) |
| 74 | assertFileContents(t, fs, "bar", []byte("root")) |
| 75 | assertFileContents(t, fs, "foo/bar", []byte("chroot")) |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | require.NoError(t, err) |
| 80 | require.NoError(t, f.Close()) |
| 81 | data, err := util.ReadFile(chroot, "../bar") |
| 82 | require.NoError(t, err) |
| 83 | assert.Equal(t, []byte("chroot"), data) |
| 84 | assertFileContents(t, fs, "bar", []byte("root")) |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | func TestStatOutOffBoundary(t *testing.T) { |
| 89 | eachChrootFS(t, func(t *testing.T, fs chrootFS) { |
| 90 | t.Helper() |
| 91 | err := util.WriteFile(fs, "bar", []byte("root"), 0o644) |
| 92 | require.NoError(t, err) |
| 93 | err = util.WriteFile(fs, "foo/bar", []byte("chroot"), 0o644) |
| 94 | require.NoError(t, err) |
| 95 | |
| 96 | chroot, _ := fs.Chroot("foo") |
| 97 | f, err := chroot.Stat("../bar") |
| 98 | if _, ok := chroot.(*osfs.BoundOS); !ok { |
| 99 | require.ErrorIs(t, err, ErrCrossedBoundary) |
| 100 | assert.Nil(t, f) |
| 101 | assertFileContents(t, fs, "bar", []byte("root")) |
| 102 | assertFileContents(t, fs, "foo/bar", []byte("chroot")) |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | require.NoError(t, err) |
| 107 | assert.Equal(t, int64(len("chroot")), f.Size()) |
| 108 | assertFileContents(t, fs, "bar", []byte("root")) |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | func TestStatWithChroot(t *testing.T) { |
| 113 | eachChrootFS(t, func(t *testing.T, fs chrootFS) { |
| 114 | t.Helper() |
| 115 | files := []string{"foo", "bar", "qux/baz", "qux/qux"} |
| 116 | for _, name := range files { |
| 117 | err := util.WriteFile(fs, name, nil, 0o644) |
| 118 | require.NoError(t, err) |
| 119 | } |
| 120 | |
| 121 | // Some implementations detect directories based on a prefix |
| 122 | // for all files; it's easy to miss path separator handling there. |
| 123 | fi, err := fs.Stat("qu") |
| 124 | assert.ErrorIs(t, err, os.ErrNotExist) |
| 125 | assert.Nil(t, fi) |
| 126 | |
| 127 | fi, err = fs.Stat("qux") |
| 128 | require.NoError(t, err) |
| 129 | assert.Equal(t, fi.Name(), "qux") |
| 130 | assert.True(t, fi.IsDir()) |
| 131 | |
| 132 | qux, _ := fs.Chroot("qux") |
| 133 | |
| 134 | fi, err = qux.Stat("baz") |
| 135 | require.NoError(t, err) |
| 136 | assert.Equal(t, fi.Name(), "baz") |
| 137 | assert.False(t, fi.IsDir()) |
| 138 | |
| 139 | fi, err = qux.Stat("/baz") |
| 140 | require.NoError(t, err) |
| 141 | assert.Equal(t, fi.Name(), "baz") |
| 142 | assert.False(t, fi.IsDir()) |
| 143 | }) |
| 144 | } |
| 145 | |
| 146 | func TestRenameOutOffBoundary(t *testing.T) { |
| 147 | eachChrootFS(t, func(t *testing.T, fs chrootFS) { |
| 148 | t.Helper() |
| 149 | err := util.WriteFile(fs, "bar", []byte("root"), 0o644) |
| 150 | require.NoError(t, err) |
| 151 | err = util.WriteFile(fs, "foo/bar", []byte("chroot-from"), 0o644) |
| 152 | require.NoError(t, err) |
| 153 | |
| 154 | chroot, _ := fs.Chroot("foo") |
| 155 | err = chroot.Rename("../bar", "renamed") |
| 156 | if _, ok := chroot.(*osfs.BoundOS); !ok { |
| 157 | require.ErrorIs(t, err, ErrCrossedBoundary) |
| 158 | assertFileContents(t, fs, "bar", []byte("root")) |
| 159 | assertFileContents(t, fs, "foo/bar", []byte("chroot-from")) |
| 160 | return |
| 161 | } |
| 162 | |
| 163 | require.NoError(t, err) |
| 164 | assertFileContents(t, fs, "bar", []byte("root")) |
| 165 | assertFileContents(t, fs, "foo/renamed", []byte("chroot-from")) |
| 166 | }) |
| 167 | } |
| 168 | |
| 169 | func TestRenameIntoParentBoundary(t *testing.T) { |
| 170 | eachChrootFS(t, func(t *testing.T, fs chrootFS) { |
| 171 | t.Helper() |
| 172 | err := util.WriteFile(fs, "bar", []byte("root"), 0o644) |
| 173 | require.NoError(t, err) |
| 174 | err = util.WriteFile(fs, "foo/file", []byte("chroot-to"), 0o644) |
| 175 | require.NoError(t, err) |
| 176 | |
| 177 | chroot, _ := fs.Chroot("foo") |
| 178 | err = chroot.Rename("file", "../bar") |
| 179 | if _, ok := chroot.(*osfs.BoundOS); ok { |
| 180 | require.NoError(t, err) |
| 181 | assertFileContents(t, fs, "bar", []byte("root")) |
| 182 | assertFileContents(t, fs, "foo/bar", []byte("chroot-to")) |
| 183 | return |
| 184 | } |
| 185 | |
| 186 | require.ErrorIs(t, err, ErrCrossedBoundary) |
| 187 | assertFileContents(t, fs, "bar", []byte("root")) |
| 188 | assertFileContents(t, fs, "foo/file", []byte("chroot-to")) |
| 189 | }) |
| 190 | } |
| 191 | |
| 192 | func TestRemoveOutOffBoundary(t *testing.T) { |
| 193 | eachChrootFS(t, func(t *testing.T, fs chrootFS) { |
| 194 | t.Helper() |
| 195 | err := util.WriteFile(fs, "bar", []byte("root"), 0o644) |
| 196 | require.NoError(t, err) |
| 197 | err = util.WriteFile(fs, "foo/bar", []byte("chroot"), 0o644) |
| 198 | require.NoError(t, err) |
| 199 | |
| 200 | chroot, _ := fs.Chroot("foo") |
| 201 | err = chroot.Remove("../bar") |
| 202 | if _, ok := chroot.(*osfs.BoundOS); ok { |
| 203 | require.NoError(t, err) |
| 204 | _, err = fs.Open("foo/bar") |
| 205 | assert.ErrorIs(t, err, os.ErrNotExist) |
| 206 | } else { |
| 207 | require.ErrorIs(t, err, ErrCrossedBoundary) |
| 208 | assertFileContents(t, fs, "foo/bar", []byte("chroot")) |
| 209 | } |
| 210 | assertFileContents(t, fs, "bar", []byte("root")) |
| 211 | }) |
| 212 | } |
| 213 | |
| 214 | func assertFileContents(t *testing.T, fs Basic, path string, want []byte) { |
| 215 | t.Helper() |
| 216 | data, err := util.ReadFile(fs, path) |
| 217 | require.NoError(t, err) |
| 218 | assert.Equal(t, want, data) |
| 219 | } |
| 220 | |
| 221 | func TestRoot(t *testing.T) { |
| 222 | eachChrootFS(t, func(t *testing.T, fs chrootFS) { |
| 223 | t.Helper() |
| 224 | assert.NotEmpty(t, fs.Root()) |
| 225 | }) |
| 226 | } |