summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/mount_test.go
blob: a3d10770b18c3f32989ab3fda50bd178f6eaedfa (plain)
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
// 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

import (
	"fmt"
	"testing"

	"gvisor.dev/gvisor/pkg/sentry/contexttest"
)

// cacheReallyContains iterates through the dirent cache to determine whether
// it contains the given dirent.
func cacheReallyContains(cache *DirentCache, d *Dirent) bool {
	for i := cache.list.Front(); i != nil; i = i.Next() {
		if i == d {
			return true
		}
	}
	return false
}

func mountPathsAre(root *Dirent, got []*Mount, want ...string) error {
	gotPaths := make(map[string]struct{}, len(got))
	gotStr := make([]string, len(got))
	for i, g := range got {
		if groot := g.Root(); groot != nil {
			name, _ := groot.FullName(root)
			groot.DecRef()
			gotStr[i] = name
			gotPaths[name] = struct{}{}
		}
	}
	if len(got) != len(want) {
		return fmt.Errorf("mount paths are different, got: %q, want: %q", gotStr, want)
	}
	for _, w := range want {
		if _, ok := gotPaths[w]; !ok {
			return fmt.Errorf("no mount with path %q found", w)
		}
	}
	return nil
}

// TestMountSourceOnlyCachedOnce tests that a Dirent that is mounted over only ends
// up in a single Dirent Cache. NOTE(b/63848693): Having a dirent in multiple
// caches causes major consistency issues.
func TestMountSourceOnlyCachedOnce(t *testing.T) {
	ctx := contexttest.Context(t)

	rootCache := NewDirentCache(100)
	rootInode := NewMockInode(ctx, NewMockMountSource(rootCache), StableAttr{
		Type: Directory,
	})
	mm, err := NewMountNamespace(ctx, rootInode)
	if err != nil {
		t.Fatalf("NewMountNamespace failed: %v", err)
	}
	rootDirent := mm.Root()
	defer rootDirent.DecRef()

	// Get a child of the root which we will mount over.  Note that the
	// MockInodeOperations causes Walk to always succeed.
	child, err := rootDirent.Walk(ctx, rootDirent, "child")
	if err != nil {
		t.Fatalf("failed to walk to child dirent: %v", err)
	}
	child.maybeExtendReference() // Cache.

	// Ensure that the root cache contains the child.
	if !cacheReallyContains(rootCache, child) {
		t.Errorf("wanted rootCache to contain child dirent, but it did not")
	}

	// Create a new cache and inode, and mount it over child.
	submountCache := NewDirentCache(100)
	submountInode := NewMockInode(ctx, NewMockMountSource(submountCache), StableAttr{
		Type: Directory,
	})
	if err := mm.Mount(ctx, child, submountInode); err != nil {
		t.Fatalf("failed to mount over child: %v", err)
	}

	// Walk to the child again.
	child2, err := rootDirent.Walk(ctx, rootDirent, "child")
	if err != nil {
		t.Fatalf("failed to walk to child dirent: %v", err)
	}

	// Should have a different Dirent than before.
	if child == child2 {
		t.Fatalf("expected %v not equal to %v, but they are the same", child, child2)
	}

	// Neither of the caches should no contain the child.
	if cacheReallyContains(rootCache, child) {
		t.Errorf("wanted rootCache not to contain child dirent, but it did")
	}
	if cacheReallyContains(submountCache, child) {
		t.Errorf("wanted submountCache not to contain child dirent, but it did")
	}
}

func TestAllMountsUnder(t *testing.T) {
	ctx := contexttest.Context(t)

	rootCache := NewDirentCache(100)
	rootInode := NewMockInode(ctx, NewMockMountSource(rootCache), StableAttr{
		Type: Directory,
	})
	mm, err := NewMountNamespace(ctx, rootInode)
	if err != nil {
		t.Fatalf("NewMountNamespace failed: %v", err)
	}
	rootDirent := mm.Root()
	defer rootDirent.DecRef()

	// Add mounts at the following paths:
	paths := []string{
		"/foo",
		"/foo/bar",
		"/foo/bar/baz",
		"/foo/qux",
		"/waldo",
	}

	var maxTraversals uint
	for _, p := range paths {
		maxTraversals = 0
		d, err := mm.FindLink(ctx, rootDirent, nil, p, &maxTraversals)
		if err != nil {
			t.Fatalf("could not find path %q in mount manager: %v", p, err)
		}

		submountInode := NewMockInode(ctx, NewMockMountSource(nil), StableAttr{
			Type: Directory,
		})
		if err := mm.Mount(ctx, d, submountInode); err != nil {
			t.Fatalf("could not mount at %q: %v", p, err)
		}
		d.DecRef()
	}

	// mm root should contain all submounts (and does not include the root mount).
	rootMnt := mm.FindMount(rootDirent)
	submounts := mm.AllMountsUnder(rootMnt)
	allPaths := append(paths, "/")
	if err := mountPathsAre(rootDirent, submounts, allPaths...); err != nil {
		t.Error(err)
	}

	// Each mount should have a unique ID.
	foundIDs := make(map[uint64]struct{})
	for _, m := range submounts {
		if _, ok := foundIDs[m.ID]; ok {
			t.Errorf("got multiple mounts with id %d", m.ID)
		}
		foundIDs[m.ID] = struct{}{}
	}

	// Root mount should have no parent.
	if p := rootMnt.ParentID; p != invalidMountID {
		t.Errorf("root.Parent got %v wanted nil", p)
	}

	// Check that "foo" mount has 3 children.
	maxTraversals = 0
	d, err := mm.FindLink(ctx, rootDirent, nil, "/foo", &maxTraversals)
	if err != nil {
		t.Fatalf("could not find path %q in mount manager: %v", "/foo", err)
	}
	defer d.DecRef()
	submounts = mm.AllMountsUnder(mm.FindMount(d))
	if err := mountPathsAre(rootDirent, submounts, "/foo", "/foo/bar", "/foo/qux", "/foo/bar/baz"); err != nil {
		t.Error(err)
	}

	// "waldo" mount should have no children.
	maxTraversals = 0
	waldo, err := mm.FindLink(ctx, rootDirent, nil, "/waldo", &maxTraversals)
	if err != nil {
		t.Fatalf("could not find path %q in mount manager: %v", "/waldo", err)
	}
	defer waldo.DecRef()
	submounts = mm.AllMountsUnder(mm.FindMount(waldo))
	if err := mountPathsAre(rootDirent, submounts, "/waldo"); err != nil {
		t.Error(err)
	}
}

func TestUnmount(t *testing.T) {
	ctx := contexttest.Context(t)

	rootCache := NewDirentCache(100)
	rootInode := NewMockInode(ctx, NewMockMountSource(rootCache), StableAttr{
		Type: Directory,
	})
	mm, err := NewMountNamespace(ctx, rootInode)
	if err != nil {
		t.Fatalf("NewMountNamespace failed: %v", err)
	}
	rootDirent := mm.Root()
	defer rootDirent.DecRef()

	// Add mounts at the following paths:
	paths := []string{
		"/foo",
		"/foo/bar",
		"/foo/bar/goo",
		"/foo/bar/goo/abc",
		"/foo/abc",
		"/foo/def",
		"/waldo",
		"/wally",
	}

	var maxTraversals uint
	for _, p := range paths {
		maxTraversals = 0
		d, err := mm.FindLink(ctx, rootDirent, nil, p, &maxTraversals)
		if err != nil {
			t.Fatalf("could not find path %q in mount manager: %v", p, err)
		}

		submountInode := NewMockInode(ctx, NewMockMountSource(nil), StableAttr{
			Type: Directory,
		})
		if err := mm.Mount(ctx, d, submountInode); err != nil {
			t.Fatalf("could not mount at %q: %v", p, err)
		}
		d.DecRef()
	}

	allPaths := make([]string, len(paths)+1)
	allPaths[0] = "/"
	copy(allPaths[1:], paths)

	rootMnt := mm.FindMount(rootDirent)
	for i := len(paths) - 1; i >= 0; i-- {
		maxTraversals = 0
		p := paths[i]
		d, err := mm.FindLink(ctx, rootDirent, nil, p, &maxTraversals)
		if err != nil {
			t.Fatalf("could not find path %q in mount manager: %v", p, err)
		}

		if err := mm.Unmount(ctx, d, false); err != nil {
			t.Fatalf("could not unmount at %q: %v", p, err)
		}
		d.DecRef()

		// Remove the path that has been unmounted and the check that the remaining
		// mounts are still there.
		allPaths = allPaths[:len(allPaths)-1]
		submounts := mm.AllMountsUnder(rootMnt)
		if err := mountPathsAre(rootDirent, submounts, allPaths...); err != nil {
			t.Error(err)
		}
	}
}