Debian Codegraph @

filesystem/mountpoint_test.go

raw
1 /*
2 * mountpoint_test.go - Tests for reading information about all mountpoints.
3 *
4 * Copyright 2017 Google Inc.
5 * Author: Joe Richey ([email protected])
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
17 * the License.
18 */
19
20 // Note: these tests assume the existence of some well-known directories: /mnt,
21 // /home, and /tmp. This is because the mountpoint loading code only retains
22 // mountpoints on valid directories.
23
24 package filesystem
25
26 import (
27 "fmt"
28 "os"
29 "path/filepath"
30 "reflect"
31 "strings"
32 "testing"
33 )
34
35 func TestLoadMountInfo(t *testing.T) {
36 if err := UpdateMountInfo(); err != nil {
37 t.Error(err)
38 }
39 }
40
41 // Lock the mount maps so that concurrent tests don't interfere with each other.
42 func beginLoadMountInfoTest() {
43 mountMutex.Lock()
44 }
45
46 func endLoadMountInfoTest() {
47 // Invalidate the fake mount information in case a test runs later which
48 // needs the real mount information.
49 mountsInitialized = false
50 mountMutex.Unlock()
51 }
52
53 func loadMountInfoFromString(str string) {
54 readMountInfo(strings.NewReader(str))
55 }
56
57 func mountForDevice(deviceNumberStr string) *Mount {
58 deviceNumber, _ := newDeviceNumberFromString(deviceNumberStr)
59 return mountsByDevice[deviceNumber]
60 }
61
62 // Test basic loading of a single mountpoint.
63 func TestLoadMountInfoBasic(t *testing.T) {
64 var mountinfo = `
65 15 0 259:3 / / rw,relatime shared:1 - ext4 /dev/root rw,data=ordered
66 `
67 beginLoadMountInfoTest()
68 defer endLoadMountInfoTest()
69 loadMountInfoFromString(mountinfo)
70 if len(mountsByDevice) != 1 {
71 t.Error("Loaded wrong number of mounts")
72 }
73 mnt := mountForDevice("259:3")
74 if mnt == nil {
75 t.Fatal("Failed to load mount")
76 }
77 if mnt.Path != "/" {
78 t.Error("Wrong path")
79 }
80 if mnt.FilesystemType != "ext4" {
81 t.Error("Wrong filesystem type")
82 }
83 if mnt.DeviceNumber.String() != "259:3" {
84 t.Error("Wrong device number")
85 }
86 if mnt.Subtree != "/" {
87 t.Error("Wrong subtree")
88 }
89 if mnt.ReadOnly {
90 t.Error("Wrong readonly flag")
91 }
92 if len(mountsByPath) != 1 {
93 t.Error("mountsByPath doesn't contain exactly one entry")
94 }
95 if mountsByPath[mnt.Path] != mnt {
96 t.Error("mountsByPath doesn't contain the correct entry")
97 }
98 }
99
100 // Test that Mount.Device is set to the mountpoint's source device if
101 // applicable, otherwise it is set to the empty string.
102 func TestLoadSourceDevice(t *testing.T) {
103 // The mountinfo parser ignores devices that don't exist. For the valid
104 // device, try /dev/loop0. If it doesn't exist, skip the test.
105 if _, err := os.Stat("/dev/loop0"); err != nil {
106 t.Skip("/dev/loop0 does not exist, skipping test")
107 }
108 var mountinfo = `
109 15 0 7:0 / / rw shared:1 - foo /dev/loop0 rw,data=ordered
110 31 15 0:27 / /tmp rw,nosuid,nodev shared:17 - tmpfs tmpfs rw
111 `
112 beginLoadMountInfoTest()
113 defer endLoadMountInfoTest()
114 loadMountInfoFromString(mountinfo)
115 mnt := mountForDevice("7:0")
116 if mnt.Device != "/dev/loop0" {
117 t.Error("mnt.Device wasn't set to source device")
118 }
119 mnt = mountForDevice("0:27")
120 if mnt.Device != "" {
121 t.Error("mnt.Device wasn't set to empty string for an invalid device")
122 }
123 }
124
125 // Test that non-directory mounts are ignored.
126 func TestNondirectoryMountsIgnored(t *testing.T) {
127 beginLoadMountInfoTest()
128 defer endLoadMountInfoTest()
129 file, err := os.CreateTemp("", "fscrypt_regfile")
130 if err != nil {
131 t.Fatal(err)
132 }
133 file.Close()
134 defer os.Remove(file.Name())
135
136 mountinfo := fmt.Sprintf("15 0 259:3 /foo %s rw,relatime shared:1 - ext4 /dev/root rw", file.Name())
137 loadMountInfoFromString(mountinfo)
138 if len(mountsByDevice) != 0 {
139 t.Error("Non-directory mount wasn't ignored")
140 }
141 }
142
143 // Test that when multiple mounts are on one directory, the last is the one
144 // which is kept.
145 func TestNonLatestMountsIgnored(t *testing.T) {
146 mountinfo := `
147 15 0 259:3 / / rw shared:1 - ext4 /dev/root rw
148 15 0 259:3 / / rw shared:1 - f2fs /dev/root rw
149 15 0 259:3 / / rw shared:1 - ubifs /dev/root rw
150 `
151 beginLoadMountInfoTest()
152 defer endLoadMountInfoTest()
153 loadMountInfoFromString(mountinfo)
154 mnt := mountForDevice("259:3")
155 if mnt.FilesystemType != "ubifs" {
156 t.Error("Last mount didn't supersede previous ones")
157 }
158 }
159
160 // Test that escape sequences in the mountinfo file are unescaped correctly.
161 func TestLoadMountWithSpecialCharacters(t *testing.T) {
162 tempDir, err := os.MkdirTemp("", "fscrypt")
163 if err != nil {
164 t.Fatal(err)
165 }
166 defer os.RemoveAll(tempDir)
167 tempDir, err = filepath.Abs(tempDir)
168 if err != nil {
169 t.Fatal(err)
170 }
171 mountpoint := filepath.Join(tempDir, "/My Directory\t\n\\")
172 if err := os.Mkdir(mountpoint, 0700); err != nil {
173 t.Fatal(err)
174 }
175 mountinfo := fmt.Sprintf("15 0 259:3 / %s/My\\040Directory\\011\\012\\134 rw shared:1 - ext4 /dev/root rw", tempDir)
176
177 beginLoadMountInfoTest()
178 defer endLoadMountInfoTest()
179 loadMountInfoFromString(mountinfo)
180 mnt := mountForDevice("259:3")
181 if mnt.Path != mountpoint {
182 t.Fatal("Wrong mountpoint")
183 }
184 }
185
186 // Tests the EscapeString() and unescapeString() functions.
187 func TestStringEscaping(t *testing.T) {
188 charsNeedEscaping := " \t\n\\"
189 charsDontNeedEscaping := "ABCDEF\u2603\xff\xff\v"
190
191 orig := charsNeedEscaping + charsDontNeedEscaping
192 escaped := `\040\011\012\134` + charsDontNeedEscaping
193 if EscapeString(orig) != escaped {
194 t.Fatal("EscapeString gave wrong result")
195 }
196 if unescapeString(escaped) != orig {
197 t.Fatal("unescapeString gave wrong result")
198 }
199 }
200
201 // Test parsing some invalid mountinfo lines.
202 func TestLoadBadMountInfo(t *testing.T) {
203 mountinfos := []string{"a",
204 "a a a a a a a a a a a a a a a",
205 "a a a a a a a a a a a a - a a",
206 "15 0 BAD:3 / / rw,relatime shared:1 - ext4 /dev/root rw,data=ordered"}
207 beginLoadMountInfoTest()
208 defer endLoadMountInfoTest()
209 for _, mountinfo := range mountinfos {
210 loadMountInfoFromString(mountinfo)
211 if len(mountsByDevice) != 0 {
212 t.Error("Loaded mount from invalid mountinfo line")
213 }
214 }
215 }
216
217 // Test that the ReadOnly flag is set if the mount is readonly, even if the
218 // filesystem is read-write.
219 func TestLoadReadOnlyMount(t *testing.T) {
220 mountinfo := `
221 222 15 259:3 / /mnt ro,relatime shared:1 - ext4 /dev/root rw,data=ordered
222 `
223 beginLoadMountInfoTest()
224 defer endLoadMountInfoTest()
225 loadMountInfoFromString(mountinfo)
226 mnt := mountForDevice("259:3")
227 if !mnt.ReadOnly {
228 t.Error("Wrong readonly flag")
229 }
230 }
231
232 // Test that a read-write mount is preferred over a read-only mount.
233 func TestReadWriteMountIsPreferredOverReadOnlyMount(t *testing.T) {
234 mountinfo := `
235 222 15 259:3 / /home ro shared:1 - ext4 /dev/root rw
236 222 15 259:3 / /mnt rw shared:1 - ext4 /dev/root rw
237 222 15 259:3 / /tmp ro shared:1 - ext4 /dev/root rw
238 `
239 beginLoadMountInfoTest()
240 defer endLoadMountInfoTest()
241 loadMountInfoFromString(mountinfo)
242 mnt := mountForDevice("259:3")
243 if mnt.Path != "/mnt" {
244 t.Error("Wrong mount was chosen")
245 }
246 }
247
248 // Test that a mount of the full filesystem is preferred over mounts of non-root
249 // subtrees, given independent mountpoints.
250 func TestRootSubtreeIsPreferred(t *testing.T) {
251 mountinfo := `
252 222 15 259:3 /subtree1 /home rw shared:1 - ext4 /dev/root rw
253 222 15 259:3 / /mnt rw shared:1 - ext4 /dev/root rw
254 222 15 259:3 /subtree2 /tmp rw shared:1 - ext4 /dev/root rw
255 `
256 beginLoadMountInfoTest()
257 defer endLoadMountInfoTest()
258 loadMountInfoFromString(mountinfo)
259 mnt := mountForDevice("259:3")
260 if mnt.Subtree != "/" {
261 t.Error("Wrong mount was chosen")
262 }
263 }
264
265 // Test that a mount that is not of the full filesystem but still contains all
266 // other mounted subtrees is preferred, given independent mountpoints.
267 func TestHighestSubtreeIsPreferred(t *testing.T) {
268 mountinfo := `
269 222 15 259:3 /foo/bar /mnt rw shared:1 - ext4 /dev/root rw
270 222 15 259:3 /foo /tmp rw shared:1 - ext4 /dev/root rw
271 222 15 259:3 /foo/baz /home rw shared:1 - ext4 /dev/root rw
272 `
273 beginLoadMountInfoTest()
274 defer endLoadMountInfoTest()
275 loadMountInfoFromString(mountinfo)
276 deviceNumber, _ := newDeviceNumberFromString("259:3")
277 mnt := mountsByDevice[deviceNumber]
278 if mnt.Subtree != "/foo" {
279 t.Error("Wrong mount was chosen")
280 }
281 }
282
283 // Test that mountpoint "/" is preferred, given independent subtrees.
284 func TestRootMountpointIsPreferred(t *testing.T) {
285 mountinfo := `
286 222 15 259:3 /var/cache/pacman/pkg /mnt rw shared:1 - ext4 /dev/root rw
287 222 15 259:3 /var/lib/lxc/base/rootfs / rw shared:1 - ext4 /dev/root rw
288 222 15 259:3 /srv/repo/x86_64 /home rw shared:1 - ext4 /dev/root rw
289 `
290 beginLoadMountInfoTest()
291 defer endLoadMountInfoTest()
292 loadMountInfoFromString(mountinfo)
293 deviceNumber, _ := newDeviceNumberFromString("259:3")
294 mnt := mountsByDevice[deviceNumber]
295 if mnt.Subtree != "/var/lib/lxc/base/rootfs" {
296 t.Error("Wrong mount was chosen")
297 }
298 }
299
300 // Test that a mountpoint that is not "/" but still contains all other
301 // mountpoints is preferred, given independent subtrees.
302 func TestHighestMountpointIsPreferred(t *testing.T) {
303 tempDir, err := os.MkdirTemp("", "fscrypt")
304 if err != nil {
305 t.Fatal(err)
306 }
307 defer os.RemoveAll(tempDir)
308 tempDir, err = filepath.Abs(tempDir)
309 if err != nil {
310 t.Fatal(err)
311 }
312 if err := os.MkdirAll(tempDir+"/a/b", 0700); err != nil {
313 t.Fatal(err)
314 }
315 if err := os.Mkdir(tempDir+"/a/c", 0700); err != nil {
316 t.Fatal(err)
317 }
318 mountinfo := fmt.Sprintf(`
319 222 15 259:3 /0 %s rw shared:1 - ext4 /dev/root rw
320 222 15 259:3 /1 %s rw shared:1 - ext4 /dev/root rw
321 222 15 259:3 /2 %s rw shared:1 - ext4 /dev/root rw
322 `, tempDir+"/a/b", tempDir+"/a", tempDir+"/a/c")
323
324 beginLoadMountInfoTest()
325 defer endLoadMountInfoTest()
326 loadMountInfoFromString(mountinfo)
327 deviceNumber, _ := newDeviceNumberFromString("259:3")
328 mnt := mountsByDevice[deviceNumber]
329 if mnt.Subtree != "/1" {
330 t.Error("Wrong mount was chosen")
331 }
332 }
333
334 // Test that if some subtrees are contained in other subtrees, *and* some
335 // mountpoints are contained in other mountpoints, the chosen Mount is the root
336 // of a tree of mountpoints whose mounted subtrees contain all mounted subtrees.
337 func TestLoadContainedSubtreesAndMountpoints(t *testing.T) {
338 tempDir, err := os.MkdirTemp("", "fscrypt")
339 if err != nil {
340 t.Fatal(err)
341 }
342 defer os.RemoveAll(tempDir)
343 tempDir, err = filepath.Abs(tempDir)
344 if err != nil {
345 t.Fatal(err)
346 }
347 if err := os.MkdirAll(tempDir+"/a/b", 0700); err != nil {
348 t.Fatal(err)
349 }
350 if err := os.Mkdir(tempDir+"/a/c", 0700); err != nil {
351 t.Fatal(err)
352 }
353 if err := os.Mkdir(tempDir+"/d", 0700); err != nil {
354 t.Fatal(err)
355 }
356 if err := os.Mkdir(tempDir+"/e", 0700); err != nil {
357 t.Fatal(err)
358 }
359 // The first three mounts form a tree of mountpoints. The rest have
360 // independent mountpoints but have mounted subtrees contained in the
361 // mounted subtrees of the first mountpoint tree.
362 mountinfo := fmt.Sprintf(`
363 222 15 259:3 /0 %s rw shared:1 - ext4 /dev/root rw
364 222 15 259:3 /1 %s rw shared:1 - ext4 /dev/root rw
365 222 15 259:3 /2 %s rw shared:1 - ext4 /dev/root rw
366 222 15 259:3 /1/3 %s rw shared:1 - ext4 /dev/root rw
367 222 15 259:3 /2/4 %s rw shared:1 - ext4 /dev/root rw
368 `, tempDir+"/a/b", tempDir+"/a", tempDir+"/a/c",
369 tempDir+"/d", tempDir+"/e")
370
371 beginLoadMountInfoTest()
372 defer endLoadMountInfoTest()
373 loadMountInfoFromString(mountinfo)
374 deviceNumber, _ := newDeviceNumberFromString("259:3")
375 mnt := mountsByDevice[deviceNumber]
376 if mnt.Subtree != "/1" {
377 t.Error("Wrong mount was chosen")
378 }
379 }
380
381 // Test loading mounts with independent subtrees *and* independent mountpoints.
382 // This case is ambiguous, so an explicit nil entry should be stored.
383 func TestLoadAmbiguousMounts(t *testing.T) {
384 mountinfo := `
385 222 15 259:3 /foo /mnt rw shared:1 - ext4 /dev/root rw
386 222 15 259:3 /bar /tmp rw shared:1 - ext4 /dev/root rw
387 `
388 beginLoadMountInfoTest()
389 defer endLoadMountInfoTest()
390 loadMountInfoFromString(mountinfo)
391 deviceNumber, _ := newDeviceNumberFromString("259:3")
392 mnt, ok := mountsByDevice[deviceNumber]
393 if !ok {
394 t.Error("Entry should exist")
395 }
396 if mnt != nil {
397 t.Error("Entry should be nil")
398 }
399 }
400
401 // Test making a filesystem link and following it, and test that leading and
402 // trailing whitespace in the link is ignored.
403 func TestGetMountFromLink(t *testing.T) {
404 mnt, err := getTestMount(t)
405 if err != nil {
406 t.Skip(err)
407 }
408 link, err := makeLink(mnt)
409 if err != nil {
410 t.Fatal(err)
411 }
412 linkedMnt, err := getMountFromLink(link)
413 if err != nil {
414 t.Fatal(err)
415 }
416 if linkedMnt != mnt {
417 t.Fatal("Link doesn't point to the same Mount")
418 }
419 if linkedMnt, err = getMountFromLink(link + "\n"); err != nil {
420 t.Fatal(err)
421 }
422 if linkedMnt != mnt {
423 t.Fatal("Link doesn't point to the same Mount")
424 }
425 if linkedMnt, err = getMountFromLink(" " + link + " \r\n"); err != nil {
426 t.Fatal(err)
427 }
428 if linkedMnt != mnt {
429 t.Fatal("Link doesn't point to the same Mount")
430 }
431 }
432
433 // Test that makeLink() is including the expected information in links.
434 func TestMakeLink(t *testing.T) {
435 mnt, err := getTestMount(t)
436 if err != nil {
437 t.Skip(err)
438 }
439 link, err := makeLink(mnt)
440 if err != nil {
441 t.Fatal(err)
442 }
443
444 // Normally, both UUID and PATH should be included.
445 if !strings.Contains(link, "UUID=") {
446 t.Fatal("Link doesn't contain UUID")
447 }
448 if !strings.Contains(link, "PATH=") {
449 t.Fatal("Link doesn't contain PATH")
450 }
451
452 // Without a valid device number, only PATH should be included.
453 mntCopy := *mnt
454 mntCopy.DeviceNumber = 0
455 link, err = makeLink(&mntCopy)
456 if err != nil {
457 t.Fatal(err)
458 }
459 if strings.Contains(link, "UUID=") {
460 t.Fatal("Link shouldn't contain UUID")
461 }
462 if !strings.Contains(link, "PATH=") {
463 t.Fatal("Link doesn't contain PATH")
464 }
465 }
466
467 // Test that old filesystem links that contain a UUID only still work.
468 func TestGetMountFromLegacyLink(t *testing.T) {
469 mnt, err := getTestMount(t)
470 if err != nil {
471 t.Skip(err)
472 }
473 uuid, err := mnt.getFilesystemUUID()
474 if uuid == "" || err != nil {
475 t.Fatal("Can't get UUID of test filesystem")
476 }
477
478 link := fmt.Sprintf("UUID=%s", uuid)
479 linkedMnt, err := getMountFromLink(link)
480 if err != nil {
481 t.Fatal(err)
482 }
483 if linkedMnt != mnt {
484 t.Fatal("Link doesn't point to the same Mount")
485 }
486 }
487
488 // Test that if the UUID in a filesystem link doesn't work, then the PATH is
489 // used instead, and vice versa.
490 func TestGetMountFromLinkFallback(t *testing.T) {
491 mnt, err := getTestMount(t)
492 if err != nil {
493 t.Skip(err)
494 }
495 badUUID := "00000000-0000-0000-0000-000000000000"
496 badPath := "/NONEXISTENT_MOUNT"
497 goodUUID, err := mnt.getFilesystemUUID()
498 if goodUUID == "" || err != nil {
499 t.Fatal("Can't get UUID of test filesystem")
500 }
501
502 // only PATH valid (should succeed)
503 link := fmt.Sprintf("UUID=%s\nPATH=%s\n", badUUID, mnt.Path)
504 linkedMnt, err := getMountFromLink(link)
505 if err != nil {
506 t.Fatal(err)
507 }
508 if linkedMnt != mnt {
509 t.Fatal("Link doesn't point to the same Mount")
510 }
511
512 // only PATH given at all (should succeed)
513 link = fmt.Sprintf("PATH=%s\n", mnt.Path)
514 linkedMnt, err = getMountFromLink(link)
515 if err != nil {
516 t.Fatal(err)
517 }
518 if linkedMnt != mnt {
519 t.Fatal("Link doesn't point to the same Mount")
520 }
521
522 // only UUID valid (should succeed)
523 link = fmt.Sprintf("UUID=%s\nPATH=%s\n", goodUUID, badPath)
524 if linkedMnt, err = getMountFromLink(link); err != nil {
525 t.Fatal(err)
526 }
527 if linkedMnt != mnt {
528 t.Fatal("Link doesn't point to the same Mount")
529 }
530
531 // neither valid (should fail)
532 link = fmt.Sprintf("UUID=%s\nPATH=%s\n", badUUID, badPath)
533 linkedMnt, err = getMountFromLink(link)
534 if linkedMnt != nil || err == nil {
535 t.Fatal("Following a bad link succeeded")
536 }
537 }
538
539 // Benchmarks how long it takes to update the mountpoint data
540 func BenchmarkLoadFirst(b *testing.B) {
541 for n := 0; n < b.N; n++ {
542 err := UpdateMountInfo()
543 if err != nil {
544 b.Fatal(err)
545 }
546 }
547 }
548
549 // Test mount comparison by values instead of by reference,
550 // as the map mountsByDevice gets recreated on each load.
551 // This ensures that concurrent mounts work properly.
552 func TestMountComparison(t *testing.T) {
553 var mountinfo = `
554 15 0 259:3 / /home rw,relatime shared:1 - ext4 /dev/root rw,data=ordered
555 `
556 beginLoadMountInfoTest()
557 defer endLoadMountInfoTest()
558 loadMountInfoFromString(mountinfo)
559 firstMnt := mountForDevice("259:3")
560 loadMountInfoFromString(mountinfo)
561 secondMnt := mountForDevice("259:3")
562 if !reflect.DeepEqual(firstMnt, secondMnt) {
563 t.Errorf("Mount comparison does not work")
564 }
565 }