-
Notifications
You must be signed in to change notification settings - Fork 42
/
read_write_test.go
942 lines (803 loc) · 31 KB
/
read_write_test.go
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
// Copyright 2017 Google Inc.
//
// 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 integration
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"syscall"
"testing"
"time"
"golang.org/x/sys/unix"
"github.com/bazelbuild/sandboxfs/integration/utils"
"github.com/bazelbuild/sandboxfs/internal/sandbox"
)
// The tests in this file verify the read/write mapping. In principle, they should ensure that the
// mapping is fully-functional, including in its read-only operations. However, as we know that
// read/write mappings are implemented in the same way as read-only mappings, we "cheat" and only
// test here for the write-specific behaviors.
// openAndDelete opens the given file with the given mode, deletes it, and returns the open file
// handle.
func openAndDelete(path string, mode int) (int, error) {
fd, err := syscall.Open(path, mode, 0)
if err != nil {
return -1, fmt.Errorf("failed to open %s: %v", path, err)
}
if err := os.Remove(path); err != nil {
return -1, fmt.Errorf("failed to remove %s: %v", path, err)
}
return fd, nil
}
func TestReadWrite_CreateFile(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
utils.MustWriteFile(t, state.RootPath("file"), 0644, "original content")
utils.MustMkdirAll(t, state.RootPath("subdir"), 0755)
utils.MustWriteFile(t, state.MountPath("subdir/file"), 0644, "new content")
if err := utils.FileEquals(state.MountPath("file"), "original content"); err != nil {
t.Error(err)
}
if err := utils.FileEquals(state.MountPath("subdir/file"), "new content"); err != nil {
t.Error(err)
}
}
func TestReadWrite_Remove(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%", "--mapping=rw:/mapped-dir:%ROOT%/mapped-dir", "--mapping=rw:/scaffold/dir:%ROOT%/scaffold-dir")
defer state.TearDown(t)
utils.MustMkdirAll(t, state.RootPath("dir"), 0755)
utils.MustWriteFile(t, state.RootPath("file"), 0644, "")
utils.MustMkdirAll(t, state.RootPath("mapped-dir"), 0755) // Clobbered by mapping.
t.Run("MappedDirCannotBeRemoved", func(t *testing.T) {
if err := os.Remove(state.MountPath("mapped-dir")); !os.IsPermission(err) {
t.Errorf("Want removal of mapped directory to return permission error; got %v", err)
}
if _, err := os.Lstat(state.MountPath("mapped-dir")); err != nil {
t.Errorf("Want mapped directory to remain within the mount point; got %v", err)
}
if _, err := os.Lstat(state.RootPath("mapped-dir")); err != nil {
t.Errorf("Want entry clobbered by mapping to remain on disk (no Lstat error); got %v", err)
}
})
t.Run("ScaffoldDirCannotBeRemoved", func(t *testing.T) {
if err := os.Remove(state.MountPath("scaffold")); !os.IsPermission(err) {
t.Errorf("Want removal of scaffold directory to return permission error; got %v", err)
}
if _, err := os.Lstat(state.MountPath("scaffold")); err != nil {
t.Errorf("Want scaffold directory to remain within the mount point; got %v", err)
}
})
t.Run("FileDoesNotExist", func(t *testing.T) {
if err := os.Remove(state.MountPath("non-existent")); !os.IsNotExist(err) {
t.Errorf("Want removal of non-existent file to return non-existence error; got %v", err)
}
})
t.Run("EntryExists", func(t *testing.T) {
for _, name := range []string{"dir", "file"} {
if err := os.Remove(state.MountPath(name)); err != nil {
t.Errorf("Want removal of existent file to succeed; got %v", err)
}
if _, err := os.Lstat(state.MountPath(name)); !os.IsNotExist(err) {
t.Errorf("Want stat of removed file within mount point to report non-existence error; got %v", err)
}
if _, err := os.Lstat(state.RootPath(name)); !os.IsNotExist(err) {
t.Errorf("Want stat of removed file in the underlying directory to report non-existence error; got %v", err)
}
}
})
}
func TestReadWrite_RewriteFile(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
utils.MustWriteFile(t, state.RootPath("file"), 0644, "original content")
if err := utils.FileEquals(state.MountPath("file"), "original content"); err != nil {
t.Error(err)
}
utils.MustWriteFile(t, state.MountPath("file"), 0644, "rewritten content")
if err := utils.FileEquals(state.MountPath("file"), "rewritten content"); err != nil {
t.Error(err)
}
}
func TestReadWrite_RewriteFileWithShorterContent(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
utils.MustWriteFile(t, state.MountPath("file"), 0644, "very long contents")
utils.MustWriteFile(t, state.MountPath("file"), 0644, "short")
if err := utils.FileEquals(state.MountPath("file"), "short"); err != nil {
t.Error(err)
}
}
func TestReadWrite_InodeReassignedAfterRecreation(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
path := state.MountPath("file")
utils.MustWriteFile(t, path, 0644, "")
fileInfo, err := os.Lstat(path)
if err != nil {
t.Fatalf("Failed to get inode number of file after first creation: %v", err)
}
originalInode := fileInfo.Sys().(*syscall.Stat_t).Ino
originalMode := fileInfo.Mode()
if err := os.Remove(path); err != nil {
t.Fatalf("Failed to remove file: %v", err)
}
utils.MustWriteFile(t, path, 0444, "")
fileInfo, err = os.Lstat(path)
if err != nil {
t.Fatalf("Failed to get inode number of file after recreation: %v", err)
}
recreatedInode := fileInfo.Sys().(*syscall.Stat_t).Ino
recreatedMode := fileInfo.Mode()
if originalInode == recreatedInode {
t.Errorf("Still got inode number %v; want it to change after file recreation", recreatedInode)
}
// Checking the mode's equality feels a bit out of scope, but it's not: an "inode" contains
// both the inode number and all other file metadata. Checking for mode equality is just a
// simple test to ensure that such metadata is not shared.
if originalMode == recreatedMode {
t.Errorf("Still got file mode %v; want it to change after file recreation with different permissions", recreatedMode)
}
}
func TestReadWrite_FstatOnDeletedNode(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
utils.MustMkdirAll(t, state.MountPath("dir"), 0755)
utils.MustWriteFile(t, state.MountPath("file"), 0644, "")
testData := []struct {
name string
relativePath string
}{
{"MappedDir", "dir"},
{"MappedFile", "file"},
}
for _, d := range testData {
t.Run(d.name, func(t *testing.T) {
path := state.MountPath(d.relativePath)
var wantStat syscall.Stat_t
if err := syscall.Stat(path, &wantStat); err != nil {
t.Fatalf("Fstat failed on golden entry: %v", err)
}
fd, err := openAndDelete(path, syscall.O_RDONLY)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(fd)
var stat syscall.Stat_t
if err := syscall.Fstat(fd, &stat); err != nil {
t.Fatalf("Fstat failed on deleted entry: %v", err)
}
// TODO(jmmv): It's not true that the stats should be fully equal. In
// particular, Nlink should have decreased to zero after deletion... but we
// currently do not explicitly do this and the behavior seems to be
// system-dependent. So, for now, just ignore that field.
stat.Nlink = 0
wantStat.Nlink = 0
if stat != wantStat {
t.Errorf("Got stat %v; want %v", stat, wantStat)
}
})
}
}
func TestReadWrite_Truncate(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
utils.MustWriteFile(t, state.MountPath("file"), 0644, "very long contents")
wantContent := "very"
if err := os.Truncate(state.MountPath("file"), int64(len(wantContent))); err != nil {
t.Fatalf("Truncate failed: %v", err)
}
if err := utils.FileEquals(state.MountPath("file"), wantContent); err != nil {
t.Error(err)
}
}
func TestReadWrite_FtruncateOnDeletedFile(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
originalContent := "very long contents"
utils.MustWriteFile(t, state.MountPath("file"), 0644, originalContent)
fd, err := openAndDelete(state.MountPath("file"), syscall.O_RDWR)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(fd)
wantContent := "very"
if err := syscall.Ftruncate(fd, int64(len(wantContent))); err != nil {
t.Fatalf("Ftruncate on deleted file failed: %v", err)
}
buf := make([]byte, len(originalContent))
n, err := syscall.Read(fd, buf)
if err != nil {
t.Fatalf("Failed to read from truncated file: %v", err)
}
if n != len(wantContent) {
t.Errorf("Got %d bytes from truncated file; want %d", n, len(wantContent))
}
buf = buf[:n]
if string(buf) != wantContent {
t.Errorf("Got content %s; want %s", string(buf), wantContent)
}
}
// equivalentStats compares two os.FileInfo objects and returns nil if they represent the same
// file; otherwise returns a descriptive error including the differences between the two.
// This equivalency is to be used during file move tess, to check if a file was actually moved
// instead of recreated.
func equivalentStats(stat1 os.FileInfo, stat2 os.FileInfo) error {
ino1 := stat1.Sys().(*syscall.Stat_t).Ino
ino2 := stat2.Sys().(*syscall.Stat_t).Ino
if stat1.Mode() != stat2.Mode() || stat1.ModTime() != stat2.ModTime() || ino1 != ino2 {
return fmt.Errorf("got mode=%v, mtime=%v, inode=%v; want mode=%v, mtime=%v, inode=%v", stat1.Mode(), stat1.ModTime(), ino1, stat2.Mode(), stat2.ModTime(), ino2)
}
return nil
}
// doRenameTest is a helper function for the tests that verify the file system-level rename
// operation. This takes the path of a file to be moved (the "old outer path"), the path of the
// rename target (the "new outer path"), and the corresponding paths within the mount point.
//
// Tests calling this function should only start a sandboxfs instance with the desired configuration
// and then immediately call this function.
func doRenameTest(t *testing.T, oldOuterPath, newOuterPath, oldInnerPath, newInnerPath string) {
utils.MustMkdirAll(t, filepath.Dir(oldOuterPath), 0755)
utils.MustMkdirAll(t, filepath.Dir(newOuterPath), 0755)
utils.MustMkdirAll(t, filepath.Dir(oldInnerPath), 0755)
utils.MustMkdirAll(t, filepath.Dir(newInnerPath), 0755)
utils.MustWriteFile(t, oldOuterPath, 0644, "some content")
lstatOrFatal := func(path string) os.FileInfo {
stat, err := os.Lstat(path)
if err != nil {
t.Fatalf("Failed to lstat %s: %v", path, err)
}
return stat
}
oldOuterStat := lstatOrFatal(oldOuterPath)
oldInnerStat := lstatOrFatal(oldInnerPath)
if err := os.Rename(oldInnerPath, newInnerPath); err != nil {
t.Fatalf("Failed to rename %s to %s: %v", oldInnerPath, newInnerPath, err)
}
newOuterStat := lstatOrFatal(newOuterPath)
newInnerStat := lstatOrFatal(newInnerPath)
if _, err := os.Lstat(oldOuterPath); os.IsExist(err) {
t.Fatalf("Old file name in root still present but should have disappeared: %s", oldOuterPath)
}
if _, err := os.Lstat(oldInnerPath); os.IsExist(err) {
t.Fatalf("Old file name in mount point still present but should have disappeared: %s", oldInnerPath)
}
if err := utils.FileEquals(newOuterPath, "some content"); err != nil {
t.Fatalf("New file name in root missing or with bad contents: %s: %v", newOuterPath, err)
}
if err := utils.FileEquals(newInnerPath, "some content"); err != nil {
t.Fatalf("New file name in mount point missing or with bad contents: %s: %v", newInnerPath, err)
}
if err := equivalentStats(oldOuterStat, newOuterStat); err != nil {
t.Errorf("Stats for %s and %s differ: %v", oldOuterPath, newOuterPath, err)
}
if err := equivalentStats(oldInnerStat, newInnerStat); err != nil {
t.Errorf("Stats for %s and %s differ: %v", oldInnerPath, newInnerPath, err)
}
}
func TestReadWrite_NestedMappingsInheritDirectoryProperties(t *testing.T) {
rootSetup := func(root string) error {
if err := os.MkdirAll(filepath.Join(root, "already/exist"), 0755); err != nil {
return err
}
return os.MkdirAll(filepath.Join(root, "dir"), 0755)
}
state := utils.MountSetupWithRootSetup(t, rootSetup,
"--mapping=rw:/:%ROOT%",
"--mapping=ro:/already/exist/dir:%ROOT%/dir")
defer state.TearDown(t)
for _, path := range []string{"already/foo", "already/exist/foo"} {
if err := ioutil.WriteFile(state.MountPath(path), []byte(""), 0644); err != nil {
t.Errorf("Cannot create %s; possible mapping interference: %v", path, err)
}
if _, err := os.Lstat(state.RootPath(path)); err != nil {
t.Errorf("Cannot find %s in underlying root location: %v", path, err)
}
}
if err := ioutil.WriteFile(state.MountPath("already/exist/dir/foo"), []byte(""), 0644); err == nil {
t.Errorf("Successfully created file in read-only mapping")
}
}
func TestReadWrite_NestedMappingsClobberFiles(t *testing.T) {
rootSetup := func(root string) error {
if err := os.MkdirAll(filepath.Join(root, "dir"), 0755); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(root, "file"), []byte(""), 0644); err != nil {
return err
}
return os.Symlink("/non-existent", filepath.Join(root, "symlink"))
}
state := utils.MountSetupWithRootSetup(t, rootSetup,
"--mapping=rw:/:%ROOT%",
"--mapping=ro:/file/nested-dir:%ROOT%/dir",
"--mapping=ro:/symlink/nested-dir:%ROOT%/dir")
defer state.TearDown(t)
for _, component := range []string{"file", "symlink"} {
fileInfo, err := os.Lstat(state.MountPath(component, "nested-dir"))
if err != nil {
t.Errorf("Cannot navigate into mapping %s/nested-dir; underlying entry interfered: %v", component, err)
}
if fileInfo.Mode()&os.ModeType != os.ModeDir {
t.Errorf("Got mode %v for mapping; want directory", fileInfo.Mode())
}
if err := os.Mkdir(state.MountPath(component, "other"), 0755); err == nil {
t.Errorf("Intermediate mapping directory %s was not read-only", component)
}
}
}
func TestReadWrite_RenameFile(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
oldOuterPath := state.RootPath("old-name")
newOuterPath := state.RootPath("new-name")
oldInnerPath := state.MountPath("old-name")
newInnerPath := state.MountPath("new-name")
doRenameTest(t, oldOuterPath, newOuterPath, oldInnerPath, newInnerPath)
}
func TestReadWrite_MoveFile(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
oldOuterPath := state.RootPath("dir1/dir2/old-name")
newOuterPath := state.RootPath("dir2/dir3/dir4/new-name")
oldInnerPath := state.MountPath("dir1/dir2/old-name")
newInnerPath := state.MountPath("dir2/dir3/dir4/new-name")
doRenameTest(t, oldOuterPath, newOuterPath, oldInnerPath, newInnerPath)
}
func TestReadWrite_Mknod(t *testing.T) {
utils.RequireRoot(t, "Requires root privileges to create arbitrary nodes")
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
// checkNode ensures that a given file is of the specified type and, if the type indicates
// that the file is a device, that the device number matches. This check is done on both
// the underlying file system and within the mount point.
checkNode := func(relPath string, wantMode os.FileMode, wantDev uint64) error {
for _, path := range []string{state.RootPath(relPath), state.MountPath(relPath)} {
fileInfo, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("failed to stat %s: %v", path, err)
}
stat := fileInfo.Sys().(*syscall.Stat_t)
if fileInfo.Mode() != wantMode {
return fmt.Errorf("got mode %v for %s, want %v", fileInfo.Mode(), path, wantMode)
}
if (wantMode&os.ModeType)&os.ModeDevice != 0 {
if uint64(stat.Rdev) != wantDev { // stat.Rdev size and sign are platform-specific.
return fmt.Errorf("got dev %v for %s, want %v", stat.Rdev, path, wantDev)
}
}
}
return nil
}
// findOS checks if the current OS appears in a list of acceptable OSes.
findOS := func(oses []string) bool {
for _, os := range oses {
if os == runtime.GOOS {
return true
}
}
return false
}
allOSes := []string{"darwin", "linux"}
if !findOS(allOSes) {
t.Fatalf("Don't know how this test behaves in this platform")
}
testData := []struct {
name string
filename string
perm uint32
mknodType uint32
dev int
statType os.FileMode
// The behavior of mknod(2) is operating-system specific. On Linux, we can create
// regular files with this call, and attempting to create a directory results in the
// wrong node being created. On macOS, attempting to create either of these fails.
//
// Instead of ignoring these cases as invalid, test specifically for the behavior we
// know should happen by "whitelisting" the systems on which each test is valid.
// This way, we verify that sandboxfs is properly delegating these calls to the
// underlying system.
wantOS []string
}{
{"RegularFile", "file", 0644, syscall.S_IFREG, 0, 0, []string{"linux"}},
{"Directory", "dir", 0755, syscall.S_IFDIR, 0, os.ModeDir, []string{}},
{"BlockDevice", "blkdev", 0400, syscall.S_IFBLK, 1234, os.ModeDevice, allOSes},
{"CharDevice", "chrdev", 0400, syscall.S_IFCHR, 5678, os.ModeDevice | os.ModeCharDevice, allOSes},
{"NamedPipe", "fifo", 0640, syscall.S_IFIFO, 0, os.ModeNamedPipe, allOSes},
}
for _, d := range testData {
t.Run(d.name, func(t *testing.T) {
path := state.MountPath(d.filename)
shouldHaveFailed := false
err := syscall.Mknod(path, d.perm|d.mknodType, d.dev)
if findOS(d.wantOS) {
if err != nil {
t.Fatalf("Failed to mknod %s: %v", path, err)
}
} else {
if err == nil {
shouldHaveFailed = true
}
}
err = checkNode(d.filename, (os.FileMode(d.perm)&os.ModePerm)|d.statType, uint64(d.dev))
if findOS(d.wantOS) {
if err != nil {
t.Error(err)
}
} else {
if err == nil {
shouldHaveFailed = true
}
}
if shouldHaveFailed {
t.Fatalf("Test was expected to fail on this platform due to behavioral differences in mknod(2) but succeeded")
}
})
}
}
func TestReadWrite_Chmod(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
// checkPerm ensures that the given file has the given permissions on the underlying file
// system and within the mount point.
checkPerm := func(relPath string, wantPerm os.FileMode) error {
for _, path := range []string{state.RootPath(relPath), state.MountPath(relPath)} {
fileInfo, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("failed to stat %s: %v", path, err)
}
perm := fileInfo.Mode() & os.ModePerm
if perm != wantPerm {
return fmt.Errorf("got permissions %v for %s, want %v", perm, path, wantPerm)
}
}
return nil
}
t.Run("Dir", func(t *testing.T) {
utils.MustMkdirAll(t, state.RootPath("dir"), 0755)
path := state.MountPath("dir")
if err := os.Chmod(path, 0500); err != nil {
t.Fatalf("Failed to chmod %s: %v", path, err)
}
if err := checkPerm("dir", 0500); err != nil {
t.Error(err)
}
})
t.Run("File", func(t *testing.T) {
utils.MustWriteFile(t, state.RootPath("file"), 0644, "new content")
path := state.MountPath("file")
if err := os.Chmod(path, 0440); err != nil {
t.Fatalf("Failed to chmod %s: %v", path, err)
}
if err := checkPerm("file", 0440); err != nil {
t.Error(err)
}
})
t.Run("Symlink", func(t *testing.T) {
utils.MustWriteFile(t, state.RootPath("target"), 0644, "")
utils.MustSymlink(t, "target", state.RootPath("symlink"))
path := state.MountPath("symlink")
linkFileInfo, err := os.Lstat(path)
if err != nil {
t.Fatalf("Failed to stat %s: %v", path, err)
}
if err := os.Chmod(path, 0200); err != nil {
t.Fatalf("Failed to chmod %s: %v", path, err)
}
if err := checkPerm("symlink", linkFileInfo.Mode()&os.ModePerm); err != nil {
t.Error(err)
}
if err := checkPerm("target", 0200); err != nil {
t.Errorf("Mode of symlink target was modified but shouldn't have been: %v", err)
}
})
}
func TestReadWrite_FchmodOnDeletedNode(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
utils.MustMkdirAll(t, state.MountPath("dir"), 0755)
utils.MustWriteFile(t, state.MountPath("file"), 0644, "")
testData := []struct {
name string
relativePath string
}{
{"MappedDir", "dir"},
{"MappedFile", "file"},
}
for _, d := range testData {
t.Run(d.name, func(t *testing.T) {
path := state.MountPath(d.relativePath)
fd, err := openAndDelete(path, syscall.O_RDONLY)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(fd)
if err := syscall.Fchmod(fd, 0444); err != nil {
t.Fatalf("Fchmod failed on deleted entry: %v", err)
}
var stat syscall.Stat_t
if err := syscall.Fstat(fd, &stat); err != nil {
t.Fatalf("Fstat failed on deleted entry: %v", err)
}
if stat.Mode&^syscall.S_IFMT != 0444 {
t.Errorf("Want file mode %o, got %o", 0444, stat.Mode&^syscall.S_IFMT)
}
})
}
}
func TestReadWrite_Chown(t *testing.T) {
utils.RequireRoot(t, "Requires root privileges to change test file ownership")
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
// checkOwners ensures that the given file is owned by the given user and group on the
// underlying file system and within the mount point.
checkOwners := func(relPath string, wantUID uint32, wantGID uint32) error {
for _, path := range []string{state.RootPath(relPath), state.MountPath(relPath)} {
fileInfo, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("failed to stat %s: %v", path, err)
}
stat := fileInfo.Sys().(*syscall.Stat_t)
if stat.Uid != wantUID {
return fmt.Errorf("got uid %v for %s, want %v", stat.Uid, path, wantUID)
}
if stat.Gid != wantGID {
return fmt.Errorf("got gid %v for %s, want %v", stat.Gid, path, wantGID)
}
}
return nil
}
utils.MustMkdirAll(t, state.RootPath("dir"), 0755)
utils.MustWriteFile(t, state.RootPath("file"), 0644, "new content")
utils.MustWriteFile(t, state.RootPath("target"), 0644, "")
utils.MustSymlink(t, "target", state.RootPath("symlink"))
targetFileInfo, err := os.Lstat(state.RootPath("target"))
if err != nil {
t.Fatalf("Failed to stat %s: %v", state.RootPath("target"), err)
}
targetStat := targetFileInfo.Sys().(*syscall.Stat_t)
testData := []struct {
name string
filename string
wantUID int
wantGID int
}{
{"Dir", "dir", 1, 2},
{"File", "file", 3, 4},
{"Symlink", "symlink", 7, 8},
}
for _, d := range testData {
t.Run(d.name, func(t *testing.T) {
path := state.MountPath(d.filename)
if err := os.Lchown(path, d.wantUID, d.wantGID); err != nil {
t.Fatalf("Failed to chown %s: %v", path, err)
}
if err := checkOwners(d.filename, uint32(d.wantUID), uint32(d.wantGID)); err != nil {
t.Error(err)
}
})
}
if err := checkOwners("target", targetStat.Uid, targetStat.Gid); err != nil {
t.Errorf("Ownership of symlink target was modified but shouldn't have been: %v", err)
}
}
func TestReadWrite_FchownOnDeletedNode(t *testing.T) {
utils.RequireRoot(t, "Requires root privileges to change test file ownership")
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
utils.MustMkdirAll(t, state.MountPath("dir"), 0755)
utils.MustWriteFile(t, state.MountPath("file"), 0644, "")
testData := []struct {
name string
relativePath string
}{
{"MappedDir", "dir"},
{"MappedFile", "file"},
}
for _, d := range testData {
t.Run(d.name, func(t *testing.T) {
path := state.MountPath(d.relativePath)
fd, err := openAndDelete(path, syscall.O_RDONLY)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(fd)
if err := syscall.Fchown(fd, 10, 20); err != nil {
t.Fatalf("Fchown failed on deleted entry: %v", err)
}
var stat syscall.Stat_t
if err := syscall.Fstat(fd, &stat); err != nil {
t.Fatalf("Fstat failed on deleted entry: %v", err)
}
if stat.Uid != 10 || stat.Gid != 20 {
t.Errorf("Want uid 10, gid 20; got uid %d, gid %d", stat.Uid, stat.Gid)
}
})
}
}
func TestReadWrite_Chtimes(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
// checkTimes ensures that the given file has the desired timing information on the
// underlying file system and within the mount point.
//
// wantAtime may be zero if the atime check should be skipped. wantMtime is always checked
// for equality. wantMinCtime indicates the minimum ctime that the file should have, as
// that's the most we can check for (because ctime cannot be explicitly set).
checkTimes := func(relPath string, wantAtime time.Time, wantMtime time.Time, wantMinCtime time.Time) error {
for _, path := range []string{state.RootPath(relPath), state.MountPath(relPath)} {
fileInfo, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("failed to stat %s: %v", path, err)
}
stat := fileInfo.Sys().(*syscall.Stat_t)
if !fileInfo.ModTime().Equal(wantMtime) {
return fmt.Errorf("got mtime %v for %s, want %v", fileInfo.ModTime(), path, wantMtime)
}
if !wantAtime.Equal(time.Unix(0, 0)) && !sandbox.Atime(stat).Equal(wantAtime) {
return fmt.Errorf("got atime %v for %s, want %v", sandbox.Atime(stat), path, wantAtime)
}
if sandbox.Ctime(stat).Before(wantMinCtime) {
return fmt.Errorf("got ctime %v for %s, want <= %v", sandbox.Ctime(stat), path, wantMinCtime)
}
}
return nil
}
// chtimes is a wrapper over os.Chtimes that updates the given file with the desired atime
// and mtime, but also computes a lower bound for the ctime of the touched file. This lower
// bound is returned and can later be fed to checkTimes.
chtimes := func(path string, atime time.Time, mtime time.Time) (time.Time, error) {
// We have no control on ctime updates so let some time pass before we modify our
// test file. This way, we can ensure that the ctime was set to, at least, the
// current updated time. All file systems should have a minimum of second-level
// granularity (I'm looking at you HFS+), so sleeping for a whole second should be
// sufficient to get this right. (Sleeps can pause for longer than specified, but
// that's perfectly fine.)
minCtime := time.Now()
time.Sleep(1 * time.Second)
if err := os.Chtimes(path, atime, mtime); err != nil {
return time.Unix(0, 0), fmt.Errorf("failed to chtimes %s: %v", path, err)
}
return minCtime, nil
}
someAtime := time.Date(2009, 5, 25, 9, 0, 0, 0, time.UTC)
someMtime := time.Date(1984, 8, 10, 19, 15, 0, 0, time.UTC)
t.Run("Dir", func(t *testing.T) {
utils.MustMkdirAll(t, state.RootPath("dir"), 0755)
wantMinCtime, err := chtimes(state.MountPath("dir"), someAtime, someMtime)
if err != nil {
t.Fatal(err)
}
if err := checkTimes("dir", someAtime, someMtime, wantMinCtime); err != nil {
t.Error(err)
}
})
t.Run("File", func(t *testing.T) {
utils.MustWriteFile(t, state.RootPath("file"), 0644, "new content")
wantMinCtime, err := chtimes(state.MountPath("file"), someAtime, someMtime)
if err != nil {
t.Fatal(err)
}
if err := checkTimes("file", someAtime, someMtime, wantMinCtime); err != nil {
t.Error(err)
}
})
t.Run("Symlink", func(t *testing.T) {
utils.MustWriteFile(t, state.RootPath("target"), 0644, "")
utils.MustSymlink(t, "target", state.RootPath("symlink"))
path := state.MountPath("symlink")
atimeTimespec, err := unix.TimeToTimespec(someAtime)
if err != nil {
t.Fatalf("Failed to convert %v to a timespec: %v", someAtime, err)
}
mtimeTimespec, err := unix.TimeToTimespec(someMtime)
if err != nil {
t.Fatalf("Failed to convert %v to a timespec: %v", someMtime, err)
}
if err = unix.UtimesNanoAt(unix.AT_FDCWD, path, []unix.Timespec{atimeTimespec, mtimeTimespec}, unix.AT_SYMLINK_NOFOLLOW); err == nil || err != syscall.EOPNOTSUPP {
t.Fatalf("Expected EOPNOTSUPP changing the times of a symlink; got %v", err)
}
})
}
func TestReadWrite_FutimesOnDeletedNode(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
utils.MustMkdirAll(t, state.MountPath("dir"), 0755)
utils.MustWriteFile(t, state.MountPath("file"), 0644, "")
someAtime := time.Date(2010, 2, 20, 10, 30, 0, 0, time.UTC)
someMtime := time.Date(1980, 3, 26, 12, 10, 0, 0, time.UTC)
testData := []struct {
name string
relativePath string
}{
{"MappedDir", "dir"},
{"MappedFile", "file"},
}
for _, d := range testData {
t.Run(d.name, func(t *testing.T) {
path := state.MountPath(d.relativePath)
fd, err := openAndDelete(path, syscall.O_RDONLY)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(fd)
tv := []syscall.Timeval{
{Sec: int64(someAtime.Unix())},
{Sec: int64(someMtime.Unix())},
}
if err := syscall.Futimes(fd, tv); err != nil {
t.Fatalf("Fchown failed on deleted entry: %v", err)
}
var stat syscall.Stat_t
if err := syscall.Fstat(fd, &stat); err != nil {
t.Fatalf("Fstat failed on deleted entry: %v", err)
}
if !someAtime.Equal(sandbox.Atime(&stat)) || !someMtime.Equal(sandbox.Mtime(&stat)) {
t.Errorf("Want atime %v, mtime %v; got atime %v, mtime %v", someAtime, someMtime, sandbox.Atime(&stat), sandbox.Mtime(&stat))
}
})
}
}
func TestReadWrite_HardLinksNotSupported(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%", "--mapping=rw:/dir:%ROOT%/dir", "--mapping=rw:/scaffold/name3:%ROOT%/dir2")
defer state.TearDown(t)
utils.MustWriteFile(t, state.RootPath("name1"), 0644, "")
utils.MustWriteFile(t, state.RootPath("dir/name2"), 0644, "")
testData := []struct {
name string
dir string // Directory on which to try the link operation.
entryName string // Name of the entry to link.
}{
{"Root", "", "name1"},
{"MappedDir", "dir", "name2"},
{"ScaffoldDir", "scaffold", "name3"},
}
for _, d := range testData {
t.Run(d.name, func(t *testing.T) {
path := state.MountPath(d.dir, d.entryName)
fileInfo, err := os.Lstat(path)
if err != nil {
t.Fatalf("Failed to stat %s before link attempt: %v", path, err)
}
wantNlink := fileInfo.Sys().(*syscall.Stat_t).Nlink
if err := os.Link(path, state.MountPath(d.dir, "new-name")); !os.IsPermission(err) {
t.Errorf("Want Link of %s to fail with permission error; got %v", path, err)
}
fileInfo, err = os.Lstat(path)
if err != nil {
t.Fatalf("Failed to stat %s after link attempt: %v", path, err)
}
stat := fileInfo.Sys().(*syscall.Stat_t)
if stat.Nlink != wantNlink {
t.Errorf("Want hard link count for %s to remain %d after failed link operation; got %d", path, wantNlink, stat.Nlink)
}
})
}
}
func TestReadWrite_SymlinkAndReadlink(t *testing.T) {
state := utils.MountSetup(t, "--mapping=rw:/:%ROOT%")
defer state.TearDown(t)
path := state.MountPath("symlink")
target := "some/random/dangling/target"
if err := os.Symlink(target, path); err != nil {
t.Fatalf("Failed to create symlink %s: %v", path, err)
}
gotTarget, err := os.Readlink(path)
if err != nil {
t.Fatalf("Failed to read symlink %s: %v", path, err)
}
if target != gotTarget {
t.Errorf("Want symlink target to be %s, got %s", target, gotTarget)
}
}