summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/gofer/gofer_test.go
blob: 764b530cbbdb342065bc1ef19325c9fbdbd2c0b8 (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
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
// Copyright 2018 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 gofer

import (
	"fmt"
	"io"
	"syscall"
	"testing"
	"time"

	"gvisor.googlesource.com/gvisor/pkg/log"
	"gvisor.googlesource.com/gvisor/pkg/p9"
	"gvisor.googlesource.com/gvisor/pkg/p9/p9test"
	"gvisor.googlesource.com/gvisor/pkg/sentry/context"
	"gvisor.googlesource.com/gvisor/pkg/sentry/context/contexttest"
	"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
	ktime "gvisor.googlesource.com/gvisor/pkg/sentry/kernel/time"
	"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
	"gvisor.googlesource.com/gvisor/pkg/unet"
)

// goodMockFile returns a file that can be Walk'ed to and created.
func goodMockFile(mode p9.FileMode, size uint64) *p9test.FileMock {
	return &p9test.FileMock{
		GetAttrMock: p9test.GetAttrMock{
			Attr:  p9.Attr{Mode: mode, Size: size, RDev: 0},
			Valid: p9.AttrMaskAll(),
		},
	}
}

func newClosedSocket() (*unet.Socket, error) {
	fd, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
	if err != nil {
		return nil, err
	}

	s, err := unet.NewSocket(fd)
	if err != nil {
		syscall.Close(fd)
		return nil, err
	}

	return s, s.Close()
}

// root returns a p9 file mock and an fs.InodeOperations created from that file.  Any
// functions performed on fs.InodeOperations will use the p9 file mock.
func root(ctx context.Context, cp cachePolicy, mode p9.FileMode, size uint64) (*p9test.FileMock, *fs.Inode, error) {
	sock, err := newClosedSocket()
	if err != nil {
		return nil, nil, err
	}

	// Construct a dummy session that we can destruct.
	s := &session{
		conn:        sock,
		mounter:     fs.RootOwner,
		cachePolicy: cp,
		client:      &p9.Client{},
	}

	rootFile := goodMockFile(mode, size)
	sattr, rootInodeOperations := newInodeOperations(ctx, s, contextFile{file: rootFile}, p9.QID{}, rootFile.GetAttrMock.Valid, rootFile.GetAttrMock.Attr)
	m := fs.NewMountSource(s, &filesystem{}, fs.MountSourceFlags{})
	return rootFile, fs.NewInode(rootInodeOperations, m, sattr), nil
}

func TestLookup(t *testing.T) {
	// Test parameters.
	type lookupTest struct {
		// Name of the test.
		name string

		// Function input parameters.
		fileName string

		// Expected return value.
		want error
	}

	tests := []lookupTest{
		{
			name:     "mock Walk passes (function succeeds)",
			fileName: "ppp",
			want:     nil,
		},
		{
			name:     "mock Walk fails (function fails)",
			fileName: "ppp",
			want:     syscall.ENOENT,
		},
	}

	ctx := contexttest.Context(t)
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// Set up mock.
			rootFile, rootInode, err := root(ctx, cacheNone, p9.PermissionsMask, 0)
			if err != nil {
				t.Fatalf("error creating root: %v", err)
			}

			rootFile.WalkGetAttrMock.QIDs = []p9.QID{{}}
			rootFile.WalkGetAttrMock.Err = test.want
			rootFile.WalkGetAttrMock.File = goodMockFile(p9.PermissionsMask, 0)

			// Call function.
			dirent, err := rootInode.Lookup(ctx, test.fileName)

			// Unwrap the InodeOperations.
			var newInodeOperations fs.InodeOperations
			if dirent != nil {
				if dirent.IsNegative() {
					err = syscall.ENOENT
				} else {
					newInodeOperations = dirent.Inode.InodeOperations
				}
			}

			// Check return values.
			if err != test.want {
				t.Errorf("Lookup got err %v, want %v", err, test.want)
			}
			if err == nil && newInodeOperations == nil {
				t.Errorf("Lookup got non-nil err and non-nil node, wanted at least one non-nil")
			}

			// Check mock parameters.
			if !rootFile.WalkGetAttrMock.Called {
				t.Errorf("GetAttr not called; error: %v", err)
			} else if rootFile.WalkGetAttrMock.Names[0] != test.fileName {
				t.Errorf("file name not set")
			}
		})
	}
}

func TestRevalidation(t *testing.T) {
	tests := []struct {
		cachePolicy               cachePolicy
		preModificationWantReval  bool
		postModificationWantReval bool
	}{
		{
			// Policy cacheNone causes Revalidate to always return
			// true.
			cachePolicy:               cacheNone,
			preModificationWantReval:  true,
			postModificationWantReval: true,
		},
		{
			// Policy cacheAll causes Revalidate to always return
			// false.
			cachePolicy:               cacheAll,
			preModificationWantReval:  false,
			postModificationWantReval: false,
		},
		{
			// Policy cacheAllWritethrough causes Revalidate to
			// always return false.
			cachePolicy:               cacheAllWritethrough,
			preModificationWantReval:  false,
			postModificationWantReval: false,
		},
		{
			// Policy cacheRemoteRevalidating causes Revalidate to
			// always return true.
			//
			// TODO: The cacheRemoteRevalidating
			// policy should only return true if the remote file's
			// attributes have changed.
			cachePolicy:               cacheRemoteRevalidating,
			preModificationWantReval:  true,
			postModificationWantReval: true,
		},
	}

	ctx := contexttest.Context(t)
	for _, test := range tests {
		name := fmt.Sprintf("cachepolicy=%s", test.cachePolicy)
		t.Run(name, func(t *testing.T) {
			// Set up mock.
			rootFile, rootInode, err := root(ctx, test.cachePolicy, p9.ModeDirectory|p9.PermissionsMask, 0)
			if err != nil {
				t.Fatalf("error creating root: %v", err)
			}

			rootDir := fs.NewDirent(rootInode, "root")

			// Create a mock file that we will walk to from the root.
			const (
				name = "foo"
				mode = p9.PermissionsMask
			)
			file := goodMockFile(mode, 0)
			file.GetAttrMock.Valid = p9.AttrMaskAll()

			// Tell the root mock how to walk to this file.
			rootFile.WalkGetAttrMock.QIDs = []p9.QID{{}}
			rootFile.WalkGetAttrMock.File = file
			rootFile.WalkGetAttrMock.Attr = file.GetAttrMock.Attr
			rootFile.WalkGetAttrMock.Valid = file.GetAttrMock.Valid

			// Do the walk.
			dirent, err := rootDir.Walk(ctx, rootDir, name)
			if err != nil {
				t.Fatalf("Lookup(%q) failed: %v", name, err)
			}

			// Walk again. Depending on the cache policy, we may get a new
			// dirent.
			newDirent, err := rootDir.Walk(ctx, rootDir, name)
			if err != nil {
				t.Fatalf("Lookup(%q) failed: %v", name, err)
			}
			if test.preModificationWantReval && dirent == newDirent {
				t.Errorf("Lookup(%q) with cachePolicy=%s got old dirent %v, wanted a new dirent", name, test.cachePolicy, dirent)
			}
			if !test.preModificationWantReval && dirent != newDirent {
				t.Errorf("Lookup(%q) with cachePolicy=%s got new dirent %v, wanted old dirent %v", name, test.cachePolicy, newDirent, dirent)
			}

			// Modify the underlying mocked file's modification time.
			file.GetAttrMock.Attr.MTimeSeconds = uint64(time.Now().Unix())

			// Walk again. Depending on the cache policy, we may get a new
			// dirent.
			newDirent, err = rootDir.Walk(ctx, rootDir, name)
			if err != nil {
				t.Fatalf("Lookup(%q) failed: %v", name, err)
			}
			if test.postModificationWantReval && dirent == newDirent {
				t.Errorf("Lookup(%q) with cachePolicy=%s got old dirent %v, wanted a new dirent", name, test.cachePolicy, dirent)
			}
			if !test.postModificationWantReval && dirent != newDirent {
				t.Errorf("Lookup(%q) with cachePolicy=%s got new dirent %v, wanted old dirent %v", name, test.cachePolicy, newDirent, dirent)
			}
		})
	}
}

func TestSetTimestamps(t *testing.T) {
	// Test parameters.
	type setTimestampsTest struct {
		// Name of the test.
		name string

		// Function input parameters.
		ts fs.TimeSpec
	}

	ctx := contexttest.Context(t)
	now := ktime.NowFromContext(ctx)
	tests := []setTimestampsTest{
		{
			name: "mock SetAttr passes (function succeeds)",
			ts: fs.TimeSpec{
				ATime: now,
				MTime: now,
			},
		},
		{
			name: "mock SetAttr passes, times are 0 (function succeeds)",
			ts:   fs.TimeSpec{},
		},
		{
			name: "mock SetAttr passes, times are 0 and not system time (function succeeds)",
			ts: fs.TimeSpec{
				ATimeSetSystemTime: false,
				MTimeSetSystemTime: false,
			},
		},
		{
			name: "mock SetAttr passes, times are set to system time (function succeeds)",
			ts: fs.TimeSpec{
				ATimeSetSystemTime: true,
				MTimeSetSystemTime: true,
			},
		},
		{
			name: "mock SetAttr passes, times are omitted (function succeeds)",
			ts: fs.TimeSpec{
				ATimeOmit: true,
				MTimeOmit: true,
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// Set up mock.
			rootFile, rootInode, err := root(ctx, cacheNone, p9.PermissionsMask, 0)
			if err != nil {
				t.Fatalf("error creating root: %v", err)
			}

			// Call function.
			err = rootInode.SetTimestamps(ctx, nil /* Dirent */, test.ts)

			// Check return values.
			if err != nil {
				t.Errorf("SetTimestamps failed: got error %v, want nil", err)
			}

			// Check mock parameters.
			if !(test.ts.ATimeOmit && test.ts.MTimeOmit) && !rootFile.SetAttrMock.Called {
				t.Errorf("TestSetTimestamps failed: SetAttr not called")
				return
			}

			// Check what was passed to the mock function.
			attr := rootFile.SetAttrMock.Attr
			atimeGiven := ktime.FromUnix(int64(attr.ATimeSeconds), int64(attr.ATimeNanoSeconds))
			if test.ts.ATimeOmit {
				if rootFile.SetAttrMock.Valid.ATime {
					t.Errorf("ATime got set true in mask, wanted false")
				}
			} else {
				if got, want := rootFile.SetAttrMock.Valid.ATimeNotSystemTime, !test.ts.ATimeSetSystemTime; got != want {
					t.Errorf("got ATimeNotSystemTime %v, want %v", got, want)
				}
				if !test.ts.ATimeSetSystemTime && !test.ts.ATime.Equal(atimeGiven) {
					t.Errorf("ATime got %v, want %v", atimeGiven, test.ts.ATime)
				}
			}

			mtimeGiven := ktime.FromUnix(int64(attr.MTimeSeconds), int64(attr.MTimeNanoSeconds))
			if test.ts.MTimeOmit {
				if rootFile.SetAttrMock.Valid.MTime {
					t.Errorf("MTime got set true in mask, wanted false")
				}
			} else {
				if got, want := rootFile.SetAttrMock.Valid.MTimeNotSystemTime, !test.ts.MTimeSetSystemTime; got != want {
					t.Errorf("got MTimeNotSystemTime %v, want %v", got, want)
				}
				if !test.ts.MTimeSetSystemTime && !test.ts.MTime.Equal(mtimeGiven) {
					t.Errorf("MTime got %v, want %v", mtimeGiven, test.ts.MTime)
				}
			}
		})
	}
}

func TestSetPermissions(t *testing.T) {
	// Test parameters.
	type setPermissionsTest struct {
		// Name of the test.
		name string

		// SetPermissions input parameters.
		perms fs.FilePermissions

		// Error that SetAttr mock should return.
		setAttrErr error

		// Expected return value.
		want bool
	}

	tests := []setPermissionsTest{
		{
			name:       "SetAttr mock succeeds (function succeeds)",
			perms:      fs.FilePermissions{User: fs.PermMask{Read: true, Write: true, Execute: true}},
			want:       true,
			setAttrErr: nil,
		},
		{
			name:       "SetAttr mock fails (function fails)",
			perms:      fs.FilePermissions{User: fs.PermMask{Read: true, Write: true}},
			want:       false,
			setAttrErr: syscall.ENOENT,
		},
	}

	ctx := contexttest.Context(t)
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// Set up mock.
			rootFile, rootInode, err := root(ctx, cacheNone, 0, 0)
			if err != nil {
				t.Fatalf("error creating root: %v", err)
			}
			rootFile.SetAttrMock.Err = test.setAttrErr

			ok := rootInode.SetPermissions(ctx, nil /* Dirent */, test.perms)

			// Check return value.
			if ok != test.want {
				t.Errorf("SetPermissions got %v, want %v", ok, test.want)
			}

			// Check mock parameters.
			pattr := rootFile.SetAttrMock.Attr
			if !rootFile.SetAttrMock.Called {
				t.Errorf("SetAttr not called")
				return
			}
			if !rootFile.SetAttrMock.Valid.Permissions {
				t.Errorf("SetAttr did not get right request (got false, expected SetAttrMask.Permissions true)")
			}
			if got := fs.FilePermsFromP9(pattr.Permissions); got != test.perms {
				t.Errorf("SetAttr did not get right permissions -- got %v, want %v", got, test.perms)
			}
		})
	}
}

func TestClose(t *testing.T) {
	ctx := contexttest.Context(t)
	// Set up mock.
	rootFile, rootInode, err := root(ctx, cacheNone, p9.PermissionsMask, 0)
	if err != nil {
		t.Fatalf("error creating root: %v", err)
	}

	// Call function.
	rootInode.InodeOperations.Release(ctx)

	// Check mock parameters.
	if !rootFile.CloseMock.Called {
		t.Errorf("TestClose failed: Close not called")
	}
}

func TestRename(t *testing.T) {
	// Test parameters.
	type renameTest struct {
		// Name of the test.
		name string

		// Input parameters.
		newParent *fs.Inode
		newName   string

		// Rename mock parameters.
		renameErr    error
		renameCalled bool

		// Error want to return given the parameters. (Same as what
		// we expect and tell rename to return.)
		want error
	}
	ctx := contexttest.Context(t)
	rootFile, rootInode, err := root(ctx, cacheNone, p9.PermissionsMask, 0)
	if err != nil {
		t.Fatalf("error creating root: %v", err)
	}

	tests := []renameTest{
		{
			name:         "mock Rename succeeds (function succeeds)",
			newParent:    rootInode,
			newName:      "foo2",
			want:         nil,
			renameErr:    nil,
			renameCalled: true,
		},
		{
			name:         "mock Rename fails (function fails)",
			newParent:    rootInode,
			newName:      "foo2",
			want:         syscall.ENOENT,
			renameErr:    syscall.ENOENT,
			renameCalled: true,
		},
		{
			name:         "newParent is not inodeOperations but should be (function fails)",
			newParent:    fs.NewMockInode(ctx, fs.NewMockMountSource(nil), fs.StableAttr{Type: fs.Directory}),
			newName:      "foo2",
			want:         syscall.EXDEV,
			renameErr:    nil,
			renameCalled: false,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			mockFile := goodMockFile(p9.PermissionsMask, 0)
			rootFile.WalkGetAttrMock.QIDs = []p9.QID{{}}
			rootFile.WalkGetAttrMock.File = mockFile

			dirent, err := rootInode.Lookup(ctx, "foo")
			if err != nil {
				t.Fatalf("root.Walk failed: %v", err)
			}
			mockFile.RenameMock.Err = test.renameErr
			mockFile.RenameMock.Called = false

			// Use a dummy oldParent to acquire write access to that directory.
			oldParent := &inodeOperations{
				readdirCache: fs.NewSortedDentryMap(nil),
			}
			oldInode := fs.NewInode(oldParent, fs.NewMockMountSource(nil), fs.StableAttr{Type: fs.Directory})

			// Call function.
			err = dirent.Inode.InodeOperations.Rename(ctx, oldInode, "", test.newParent, test.newName)

			// Check return value.
			if err != test.want {
				t.Errorf("Rename got %v, want %v", err, test.want)
			}

			// Check mock parameters.
			if got, want := mockFile.RenameMock.Called, test.renameCalled; got != want {
				t.Errorf("renameCalled got %v want %v", got, want)
			}
		})
	}
}

// This file is read from in TestPreadv.
type readAtFileFake struct {
	p9test.FileMock

	// Parameters for faking ReadAt.
	FileLength int
	Err        error
	ChunkSize  int
	Called     bool
	LengthRead int
}

func (r *readAtFileFake) ReadAt(p []byte, offset uint64) (int, error) {
	r.Called = true
	log.Warningf("ReadAt fake: length read so far = %d, len(p) = %d, offset = %d", r.LengthRead, len(p), offset)
	if int(offset) != r.LengthRead {
		return 0, fmt.Errorf("offset got %d; expected %d", offset, r.LengthRead)
	}

	if r.Err != nil {
		return 0, r.Err
	}

	if r.LengthRead >= r.FileLength {
		return 0, io.EOF
	}

	// Read at most ChunkSize and read at most what's left in the file.
	toBeRead := len(p)
	if r.LengthRead+toBeRead >= r.FileLength {
		toBeRead = r.FileLength - int(offset)
	}
	if toBeRead > r.ChunkSize {
		toBeRead = r.ChunkSize
	}

	r.LengthRead += toBeRead
	if r.LengthRead == r.FileLength {
		return toBeRead, io.EOF
	}
	return toBeRead, nil
}

func TestPreadv(t *testing.T) {
	// Test parameters.
	type preadvTest struct {
		// Name of the test.
		name string

		// Mock parameters
		mode p9.FileMode

		// Buffer to read into.
		buffer    [512]byte
		sliceSize int

		// How much readAt returns at a time.
		chunkSize int

		// Whether or not we expect ReadAt to be called.
		readAtCalled bool
		readAtErr    error

		// Expected return values.
		want error
	}

	tests := []preadvTest{
		{
			name:         "fake ReadAt succeeds, 512 bytes requested, 512 byte chunks (function succeeds)",
			want:         nil,
			readAtErr:    nil,
			mode:         p9.PermissionsMask,
			readAtCalled: true,
			sliceSize:    512,
			chunkSize:    512,
		},
		{
			name:         "fake ReadAt succeeds, 512 bytes requested, 200 byte chunks (function succeeds)",
			want:         nil,
			readAtErr:    nil,
			mode:         p9.PermissionsMask,
			readAtCalled: true,
			sliceSize:    512,
			chunkSize:    200,
		},
		{
			name:         "fake ReadAt succeeds, 0 bytes requested (function succeeds)",
			want:         nil,
			readAtErr:    nil,
			mode:         p9.PermissionsMask,
			readAtCalled: false,
			sliceSize:    0,
			chunkSize:    100,
		},
		{
			name:         "fake ReadAt returns 0 bytes and EOF (function fails)",
			want:         io.EOF,
			readAtErr:    io.EOF,
			mode:         p9.PermissionsMask,
			readAtCalled: true,
			sliceSize:    512,
			chunkSize:    512,
		},
	}

	ctx := contexttest.Context(t)
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// Set up mock.
			rootFile, rootInode, err := root(ctx, cacheNone, test.mode, 1024)
			if err != nil {
				t.Fatalf("error creating root: %v", err)
			}

			// Set up the read buffer.
			dst := usermem.BytesIOSequence(test.buffer[:test.sliceSize])

			// This file will be read from.
			openFile := &readAtFileFake{
				Err:        test.readAtErr,
				FileLength: test.sliceSize,
				ChunkSize:  test.chunkSize,
			}
			rootFile.WalkGetAttrMock.File = openFile
			rootFile.WalkGetAttrMock.Attr.Mode = test.mode
			rootFile.WalkGetAttrMock.Valid.Mode = true

			f := NewFile(
				ctx,
				fs.NewDirent(rootInode, ""),
				"",
				fs.FileFlags{Read: true},
				rootInode.InodeOperations.(*inodeOperations),
				&handles{File: contextFile{file: openFile}},
			)

			// Call function.
			_, err = f.Preadv(ctx, dst, 0)

			// Check return value.
			if err != test.want {
				t.Errorf("Preadv got %v, want %v", err, test.want)
			}

			// Check mock parameters.
			if test.readAtCalled != openFile.Called {
				t.Errorf("ReadAt called: %v, but expected opposite", openFile.Called)
			}
		})
	}
}

func TestReadlink(t *testing.T) {
	// Test parameters.
	type readlinkTest struct {
		// Name of the test.
		name string

		// Mock parameters
		mode p9.FileMode

		// Whether or not we expect ReadAt to be called and what error
		// it shall return.
		readlinkCalled bool
		readlinkErr    error

		// Expected return values.
		want error
	}

	tests := []readlinkTest{
		{
			name:           "file is not symlink (function fails)",
			want:           syscall.ENOLINK,
			mode:           p9.PermissionsMask,
			readlinkCalled: false,
			readlinkErr:    nil,
		},
		{
			name:           "mock Readlink succeeds (function succeeds)",
			want:           nil,
			mode:           p9.PermissionsMask | p9.ModeSymlink,
			readlinkCalled: true,
			readlinkErr:    nil,
		},
		{
			name:           "mock Readlink fails (function fails)",
			want:           syscall.ENOENT,
			mode:           p9.PermissionsMask | p9.ModeSymlink,
			readlinkCalled: true,
			readlinkErr:    syscall.ENOENT,
		},
	}

	ctx := contexttest.Context(t)
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// Set up mock.
			rootFile, rootInode, err := root(ctx, cacheNone, test.mode, 0)
			if err != nil {
				t.Fatalf("error creating root: %v", err)
			}

			openFile := goodMockFile(test.mode, 0)
			rootFile.WalkMock.File = openFile
			rootFile.ReadlinkMock.Err = test.readlinkErr

			// Call function.
			_, err = rootInode.Readlink(ctx)

			// Check return value.
			if err != test.want {
				t.Errorf("Readlink got %v, want %v", err, test.want)
			}

			// Check mock parameters.
			if test.readlinkCalled && !rootFile.ReadlinkMock.Called {
				t.Errorf("Readlink not called")
			}
		})
	}
}

// This file is write from in TestPwritev.
type writeAtFileFake struct {
	p9test.FileMock

	// Parameters for faking WriteAt.
	Err           error
	ChunkSize     int
	Called        bool
	LengthWritten int
}

func (r *writeAtFileFake) WriteAt(p []byte, offset uint64) (int, error) {
	r.Called = true
	log.Warningf("WriteAt fake: length written so far = %d, len(p) = %d, offset = %d", r.LengthWritten, len(p), offset)
	if int(offset) != r.LengthWritten {
		return 0, fmt.Errorf("offset got %d; want %d", offset, r.LengthWritten)
	}

	if r.Err != nil {
		return 0, r.Err
	}

	// Write at most ChunkSize.
	toBeWritten := len(p)
	if toBeWritten > r.ChunkSize {
		toBeWritten = r.ChunkSize
	}
	r.LengthWritten += toBeWritten
	return toBeWritten, nil
}

func TestPwritev(t *testing.T) {
	// Test parameters.
	type pwritevTest struct {
		// Name of the test.
		name string

		// Mock parameters
		mode p9.FileMode

		allowWrite bool

		// Buffer to write into.
		buffer    [512]byte
		sliceSize int
		chunkSize int

		// Whether or not we expect writeAt to be called.
		writeAtCalled bool
		writeAtErr    error

		// Expected return values.
		want error
	}

	tests := []pwritevTest{
		{
			name:          "fake writeAt succeeds, one chunk (function succeeds)",
			want:          nil,
			writeAtErr:    nil,
			mode:          p9.PermissionsMask,
			allowWrite:    true,
			writeAtCalled: true,
			sliceSize:     512,
			chunkSize:     512,
		},
		{
			name:          "fake writeAt fails, short write (function fails)",
			want:          io.ErrShortWrite,
			writeAtErr:    nil,
			mode:          p9.PermissionsMask,
			allowWrite:    true,
			writeAtCalled: true,
			sliceSize:     512,
			chunkSize:     200,
		},
		{
			name:          "fake writeAt succeeds, len 0 (function succeeds)",
			want:          nil,
			writeAtErr:    nil,
			mode:          p9.PermissionsMask,
			allowWrite:    true,
			writeAtCalled: false,
			sliceSize:     0,
			chunkSize:     0,
		},
		{
			name:          "writeAt can still write despite file permissions read only (function succeeds)",
			want:          nil,
			writeAtErr:    nil,
			mode:          p9.PermissionsMask,
			allowWrite:    false,
			writeAtCalled: true,
			sliceSize:     512,
			chunkSize:     512,
		},
	}

	ctx := contexttest.Context(t)
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// Set up mock.
			_, rootInode, err := root(ctx, cacheNone, test.mode, 0)
			if err != nil {
				t.Fatalf("error creating root: %v", err)
			}

			src := usermem.BytesIOSequence(test.buffer[:test.sliceSize])

			// This is the file that will be used for writing.
			openFile := &writeAtFileFake{
				Err:       test.writeAtErr,
				ChunkSize: test.chunkSize,
			}

			f := NewFile(
				ctx,
				fs.NewDirent(rootInode, ""),
				"",
				fs.FileFlags{Write: true},
				rootInode.InodeOperations.(*inodeOperations),
				&handles{File: contextFile{file: openFile}},
			)

			// Call function.
			_, err = f.Pwritev(ctx, src, 0)

			// Check return value.
			if err != test.want {
				t.Errorf("Pwritev got %v, want %v", err, test.want)
			}

			// Check mock parameters.
			if test.writeAtCalled != openFile.Called {
				t.Errorf("WriteAt called: %v, but expected opposite", openFile.Called)
				return
			}
			if openFile.Called && test.writeAtErr != nil && openFile.LengthWritten != test.sliceSize {
				t.Errorf("wrote %d bytes, expected %d bytes written", openFile.LengthWritten, test.sliceSize)
			}
		})
	}
}