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
|
// 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)
}
}
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)
}
}
|