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
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fs_test
import (
"reflect"
"testing"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/sentry/fs"
"gvisor.dev/gvisor/pkg/sentry/fs/fsutil"
"gvisor.dev/gvisor/pkg/sentry/fs/ramfs"
"gvisor.dev/gvisor/pkg/sentry/kernel/contexttest"
)
func TestReaddir(t *testing.T) {
ctx := contexttest.Context(t)
ctx = &rootContext{
Context: ctx,
root: fs.NewDirent(ctx, newTestRamfsDir(ctx, nil, nil), "root"),
}
for _, test := range []struct {
// Test description.
desc string
// Lookup parameters.
dir *fs.Inode
// Want from lookup.
err error
names []string
}{
{
desc: "no upper, lower has entries",
dir: fs.NewTestOverlayDir(ctx,
nil, /* upper */
newTestRamfsDir(ctx, []dirContent{
{name: "a"},
{name: "b"},
}, nil), /* lower */
false /* revalidate */),
names: []string{".", "..", "a", "b"},
},
{
desc: "upper has entries, no lower",
dir: fs.NewTestOverlayDir(ctx,
newTestRamfsDir(ctx, []dirContent{
{name: "a"},
{name: "b"},
}, nil), /* upper */
nil, /* lower */
false /* revalidate */),
names: []string{".", "..", "a", "b"},
},
{
desc: "upper and lower, entries combine",
dir: fs.NewTestOverlayDir(ctx,
newTestRamfsDir(ctx, []dirContent{
{name: "a"},
}, nil), /* upper */
newTestRamfsDir(ctx, []dirContent{
{name: "b"},
}, nil), /* lower */
false /* revalidate */),
names: []string{".", "..", "a", "b"},
},
{
desc: "upper and lower, entries combine, none are masked",
dir: fs.NewTestOverlayDir(ctx,
newTestRamfsDir(ctx, []dirContent{
{name: "a"},
}, []string{"b"}), /* upper */
newTestRamfsDir(ctx, []dirContent{
{name: "c"},
}, nil), /* lower */
false /* revalidate */),
names: []string{".", "..", "a", "c"},
},
{
desc: "upper and lower, entries combine, upper masks some of lower",
dir: fs.NewTestOverlayDir(ctx,
newTestRamfsDir(ctx, []dirContent{
{name: "a"},
}, []string{"b"}), /* upper */
newTestRamfsDir(ctx, []dirContent{
{name: "b"}, /* will be masked */
{name: "c"},
}, nil), /* lower */
false /* revalidate */),
names: []string{".", "..", "a", "c"},
},
} {
t.Run(test.desc, func(t *testing.T) {
openDir, err := test.dir.GetFile(ctx, fs.NewDirent(ctx, test.dir, "stub"), fs.FileFlags{Read: true})
if err != nil {
t.Fatalf("GetFile got error %v, want nil", err)
}
stubSerializer := &fs.CollectEntriesSerializer{}
err = openDir.Readdir(ctx, stubSerializer)
if err != test.err {
t.Fatalf("Readdir got error %v, want nil", err)
}
if err != nil {
return
}
if !reflect.DeepEqual(stubSerializer.Order, test.names) {
t.Errorf("Readdir got names %v, want %v", stubSerializer.Order, test.names)
}
})
}
}
func TestReaddirRevalidation(t *testing.T) {
ctx := contexttest.Context(t)
ctx = &rootContext{
Context: ctx,
root: fs.NewDirent(ctx, newTestRamfsDir(ctx, nil, nil), "root"),
}
// Create an overlay with two directories, each with one file.
upper := newTestRamfsDir(ctx, []dirContent{{name: "a"}}, nil)
lower := newTestRamfsDir(ctx, []dirContent{{name: "b"}}, nil)
overlay := fs.NewTestOverlayDir(ctx, upper, lower, true /* revalidate */)
// Get a handle to the dirent in the upper filesystem so that we can
// modify it without going through the dirent.
upperDir := upper.InodeOperations.(*dir).InodeOperations.(*ramfs.Dir)
// Check that overlay returns the files from both upper and lower.
openDir, err := overlay.GetFile(ctx, fs.NewDirent(ctx, overlay, "stub"), fs.FileFlags{Read: true})
if err != nil {
t.Fatalf("GetFile got error %v, want nil", err)
}
ser := &fs.CollectEntriesSerializer{}
if err := openDir.Readdir(ctx, ser); err != nil {
t.Fatalf("Readdir got error %v, want nil", err)
}
got, want := ser.Order, []string{".", "..", "a", "b"}
if !reflect.DeepEqual(got, want) {
t.Errorf("Readdir got names %v, want %v", got, want)
}
// Remove "a" from the upper and add "c".
if err := upperDir.Remove(ctx, upper, "a"); err != nil {
t.Fatalf("error removing child: %v", err)
}
upperDir.AddChild(ctx, "c", fs.NewInode(ctx, fsutil.NewSimpleFileInode(ctx, fs.RootOwner, fs.FilePermissions{}, 0),
upper.MountSource, fs.StableAttr{Type: fs.RegularFile}))
// Seek to beginning of the directory and do the readdir again.
if _, err := openDir.Seek(ctx, fs.SeekSet, 0); err != nil {
t.Fatalf("error seeking to beginning of dir: %v", err)
}
ser = &fs.CollectEntriesSerializer{}
if err := openDir.Readdir(ctx, ser); err != nil {
t.Fatalf("Readdir got error %v, want nil", err)
}
// Readdir should return the updated children.
got, want = ser.Order, []string{".", "..", "b", "c"}
if !reflect.DeepEqual(got, want) {
t.Errorf("Readdir got names %v, want %v", got, want)
}
}
// TestReaddirOverlayFrozen tests that calling Readdir on an overlay file with
// a frozen dirent tree does not make Readdir calls to the underlying files.
func TestReaddirOverlayFrozen(t *testing.T) {
ctx := contexttest.Context(t)
// Create an overlay with two directories, each with two files.
upper := newTestRamfsDir(ctx, []dirContent{{name: "upper-file1"}, {name: "upper-file2"}}, nil)
lower := newTestRamfsDir(ctx, []dirContent{{name: "lower-file1"}, {name: "lower-file2"}}, nil)
overlayInode := fs.NewTestOverlayDir(ctx, upper, lower, false)
// Set that overlay as the root.
root := fs.NewDirent(ctx, overlayInode, "root")
ctx = &rootContext{
Context: ctx,
root: root,
}
// Check that calling Readdir on the root now returns all 4 files (2
// from each layer in the overlay).
rootFile, err := root.Inode.GetFile(ctx, root, fs.FileFlags{Read: true})
if err != nil {
t.Fatalf("root.Inode.GetFile failed: %v", err)
}
defer rootFile.DecRef()
ser := &fs.CollectEntriesSerializer{}
if err := rootFile.Readdir(ctx, ser); err != nil {
t.Fatalf("rootFile.Readdir failed: %v", err)
}
if got, want := ser.Order, []string{".", "..", "lower-file1", "lower-file2", "upper-file1", "upper-file2"}; !reflect.DeepEqual(got, want) {
t.Errorf("Readdir got names %v, want %v", got, want)
}
// Readdir should have been called on upper and lower.
upperDir := upper.InodeOperations.(*dir)
lowerDir := lower.InodeOperations.(*dir)
if !upperDir.ReaddirCalled {
t.Errorf("upperDir.ReaddirCalled got %v, want true", upperDir.ReaddirCalled)
}
if !lowerDir.ReaddirCalled {
t.Errorf("lowerDir.ReaddirCalled got %v, want true", lowerDir.ReaddirCalled)
}
// Reset.
upperDir.ReaddirCalled = false
lowerDir.ReaddirCalled = false
// Take references on "upper-file1" and "lower-file1", pinning them in
// the dirent tree.
for _, name := range []string{"upper-file1", "lower-file1"} {
if _, err := root.Walk(ctx, root, name); err != nil {
t.Fatalf("root.Walk(%q) failed: %v", name, err)
}
// Don't drop a reference on the returned dirent so that it
// will stay in the tree.
}
// Freeze the dirent tree.
root.Freeze()
// Seek back to the beginning of the file.
if _, err := rootFile.Seek(ctx, fs.SeekSet, 0); err != nil {
t.Fatalf("error seeking to beginning of directory: %v", err)
}
// Calling Readdir on the root now will return only the pinned
// children.
ser = &fs.CollectEntriesSerializer{}
if err := rootFile.Readdir(ctx, ser); err != nil {
t.Fatalf("rootFile.Readdir failed: %v", err)
}
if got, want := ser.Order, []string{".", "..", "lower-file1", "upper-file1"}; !reflect.DeepEqual(got, want) {
t.Errorf("Readdir got names %v, want %v", got, want)
}
// Readdir should NOT have been called on upper or lower.
if upperDir.ReaddirCalled {
t.Errorf("upperDir.ReaddirCalled got %v, want false", upperDir.ReaddirCalled)
}
if lowerDir.ReaddirCalled {
t.Errorf("lowerDir.ReaddirCalled got %v, want false", lowerDir.ReaddirCalled)
}
}
type rootContext struct {
context.Context
root *fs.Dirent
}
// Value implements context.Context.
func (r *rootContext) Value(key interface{}) interface{} {
switch key {
case fs.CtxRoot:
r.root.IncRef()
return r.root
default:
return r.Context.Value(key)
}
}
|