1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
package local
import (
"context"
"io"
"io/fs"
"os"
"path/filepath"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValidatePath(t *testing.T) {
ctx := context.Background()
rootVFS, err := localVFS.Root(ctx, ".", "")
require.NoError(t, err)
root := rootVFS.(*Root)
wd, err := os.Getwd()
require.NoError(t, err)
tests := map[string]struct {
path string
expectedFullPath string
expectedVFSPath string
expectedInvalidPath bool
}{
"a valid path": {
path: "testdata/link",
expectedFullPath: filepath.Join(wd, "testdata", "link"),
expectedVFSPath: filepath.Join("testdata", "link"),
},
"a path outside of root directory": {
path: "testdata/../../link",
expectedInvalidPath: true,
},
"an absolute path": {
// we don't support absolute paths, thus the `wd` will be preprended to `path`
path: filepath.Join(wd, "testdata", "link"),
expectedFullPath: filepath.Join(wd, wd, "testdata", "link"),
expectedVFSPath: filepath.Join(wd, "testdata", "link")[1:], // strip leading `/`
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
fullPath, vfsPath, err := root.validatePath(test.path)
if test.expectedInvalidPath {
require.IsType(t, &invalidPathError{}, err, "InvalidPath")
return
}
require.NoError(t, err, "validatePath")
assert.Equal(t, test.expectedFullPath, fullPath, "FullPath")
assert.Equal(t, test.expectedVFSPath, vfsPath, "VFSPath")
})
}
}
func TestReadlink(t *testing.T) {
ctx := context.Background()
root, err := localVFS.Root(ctx, ".", "")
require.NoError(t, err)
tests := map[string]struct {
path string
expectedTarget string
expectedErr error
expectedInvalidPath bool
}{
"a valid link": {
path: "testdata/link",
expectedTarget: "file",
},
"a file": {
path: "testdata/file",
// TODO: use fs.ErrInvalid once https://github.com/golang/go/issues/30322 is fixed
expectedErr: syscall.EINVAL,
},
"a path outside of root directory": {
path: "testdata/../../link",
expectedInvalidPath: true,
},
"a non-existing link": {
path: "non-existing",
expectedErr: fs.ErrNotExist,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
target, err := root.Readlink(ctx, test.path)
if test.expectedInvalidPath {
require.IsType(t, &invalidPathError{}, err, "InvalidPath")
return
}
if test.expectedErr != nil {
require.ErrorIs(t, err, test.expectedErr, "Readlink")
return
}
require.NoError(t, err, "Readlink")
assert.Equal(t, test.expectedTarget, target, "target")
})
}
}
func TestReadlinkAbsolutePath(t *testing.T) {
// create structure as:
// /tmp/dir: directory
// /tmp/dir/symlink: points to `/tmp/file` outside of the `/tmp/dir`
// /tmp/dir/symlink2: points to `/tmp/dir/file`
tmpDir := tmpDir(t)
dirPath := filepath.Join(tmpDir, "dir")
err := os.Mkdir(dirPath, 0755)
require.NoError(t, err)
symlinkPath := filepath.Join(dirPath, "symlink")
filePath := filepath.Join(tmpDir, "file")
err = os.Symlink(filePath, symlinkPath)
require.NoError(t, err)
symlinkPath = filepath.Join(dirPath, "symlink2")
dirFilePath := filepath.Join(dirPath, "file")
err = os.Symlink(dirFilePath, symlinkPath)
require.NoError(t, err)
root, err := localVFS.Root(context.Background(), dirPath, "")
require.NoError(t, err)
tests := map[string]struct {
path string
expectedTarget string
}{
"the absolute path is returned for file outside of `/tmp/dir": {
path: "symlink",
expectedTarget: filePath,
},
"the relative path is returned for file inside the `/tmp/dir": {
path: "symlink2",
expectedTarget: "file",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
targetPath, err := root.Readlink(context.Background(), test.path)
require.NoError(t, err)
assert.Equal(t, test.expectedTarget, targetPath)
})
}
}
func TestLstat(t *testing.T) {
ctx := context.Background()
root, err := localVFS.Root(ctx, ".", "")
require.NoError(t, err)
tests := map[string]struct {
path string
modePerm os.FileMode
modeType os.FileMode
expectedInvalidPath bool
expectedErr error
}{
"a directory": {
path: "testdata",
modeType: os.ModeDir,
modePerm: 0755,
},
"a file": {
path: "testdata/file",
modeType: os.FileMode(0),
modePerm: 0644,
},
"a link": {
path: "testdata/link",
modeType: os.ModeSymlink,
// modePerm: Permissions of symlinks are platform dependent
},
"a path outside of root directory": {
path: "testdata/../../link",
expectedInvalidPath: true,
},
"a non-existing link": {
path: "non-existing",
expectedErr: fs.ErrNotExist,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if test.modePerm > 0 {
require.NoError(t, os.Chmod(test.path, test.modePerm), "preparation: deterministic permissions")
}
fi, err := root.Lstat(ctx, test.path)
if test.expectedErr != nil {
require.ErrorIs(t, err, test.expectedErr)
return
}
if test.expectedInvalidPath {
require.IsType(t, &invalidPathError{}, err, "InvalidPath")
return
}
require.NoError(t, err, "Lstat")
require.Equal(t, test.modeType, fi.Mode()&os.ModeType, "file mode: type")
if test.modePerm > 0 {
require.Equal(t, test.modePerm, fi.Mode()&os.ModePerm, "file mode: permissions")
}
})
}
}
func TestOpen(t *testing.T) {
ctx := context.Background()
root, err := localVFS.Root(ctx, ".", "")
require.NoError(t, err)
tests := map[string]struct {
path string
expectedInvalidPath bool
expectedContent string
expectedErr error
}{
"a file": {
path: "testdata/file",
expectedContent: "hello\n",
},
"a directory": {
path: "testdata",
expectedErr: errNotFile,
},
"a link": {
path: "testdata/link",
expectedErr: syscall.ELOOP,
},
"a path outside of root directory": {
path: "testdata/../../link",
expectedInvalidPath: true,
},
"a non-existing file": {
path: "non-existing",
expectedErr: fs.ErrNotExist,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
file, err := root.Open(ctx, test.path)
if file != nil {
defer file.Close()
}
if test.expectedErr != nil {
require.ErrorIs(t, err, test.expectedErr, "Open")
return
}
if test.expectedInvalidPath {
require.IsType(t, &invalidPathError{}, err, "InvalidPath")
return
}
require.NoError(t, err, "Open")
data, err := io.ReadAll(file)
require.NoError(t, err, "ReadAll")
require.Equal(t, test.expectedContent, string(data), "ReadAll")
})
}
}
|