github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/mock.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fs
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/context"
    19  	"github.com/SagerNet/gvisor/pkg/errors/linuxerr"
    20  )
    21  
    22  // MockInodeOperations implements InodeOperations for testing Inodes.
    23  type MockInodeOperations struct {
    24  	InodeOperations
    25  
    26  	UAttr UnstableAttr
    27  
    28  	createCalled          bool
    29  	createDirectoryCalled bool
    30  	createLinkCalled      bool
    31  	renameCalled          bool
    32  	walkCalled            bool
    33  }
    34  
    35  // NewMockInode returns a mock *Inode using MockInodeOperations.
    36  func NewMockInode(ctx context.Context, msrc *MountSource, sattr StableAttr) *Inode {
    37  	return NewInode(ctx, NewMockInodeOperations(ctx), msrc, sattr)
    38  }
    39  
    40  // NewMockInodeOperations returns a *MockInodeOperations.
    41  func NewMockInodeOperations(ctx context.Context) *MockInodeOperations {
    42  	return &MockInodeOperations{
    43  		UAttr: WithCurrentTime(ctx, UnstableAttr{
    44  			Perms: FilePermsFromMode(0777),
    45  		}),
    46  	}
    47  }
    48  
    49  // MockMountSourceOps implements fs.MountSourceOperations.
    50  type MockMountSourceOps struct {
    51  	MountSourceOperations
    52  	keep       bool
    53  	revalidate bool
    54  }
    55  
    56  // NewMockMountSource returns a new *MountSource using MockMountSourceOps.
    57  func NewMockMountSource(cache *DirentCache) *MountSource {
    58  	var keep bool
    59  	if cache != nil {
    60  		keep = cache.maxSize > 0
    61  	}
    62  	return &MountSource{
    63  		MountSourceOperations: &MockMountSourceOps{keep: keep},
    64  		fscache:               cache,
    65  	}
    66  }
    67  
    68  // Revalidate implements fs.MountSourceOperations.Revalidate.
    69  func (n *MockMountSourceOps) Revalidate(context.Context, string, *Inode, *Inode) bool {
    70  	return n.revalidate
    71  }
    72  
    73  // Keep implements fs.MountSourceOperations.Keep.
    74  func (n *MockMountSourceOps) Keep(dirent *Dirent) bool {
    75  	return n.keep
    76  }
    77  
    78  // CacheReaddir implements fs.MountSourceOperations.CacheReaddir.
    79  func (n *MockMountSourceOps) CacheReaddir() bool {
    80  	// Common case: cache readdir results if there is a dirent cache.
    81  	return n.keep
    82  }
    83  
    84  // WriteOut implements fs.InodeOperations.WriteOut.
    85  func (n *MockInodeOperations) WriteOut(context.Context, *Inode) error {
    86  	return nil
    87  }
    88  
    89  // UnstableAttr implements fs.InodeOperations.UnstableAttr.
    90  func (n *MockInodeOperations) UnstableAttr(context.Context, *Inode) (UnstableAttr, error) {
    91  	return n.UAttr, nil
    92  }
    93  
    94  // IsVirtual implements fs.InodeOperations.IsVirtual.
    95  func (n *MockInodeOperations) IsVirtual() bool {
    96  	return false
    97  }
    98  
    99  // Lookup implements fs.InodeOperations.Lookup.
   100  func (n *MockInodeOperations) Lookup(ctx context.Context, dir *Inode, p string) (*Dirent, error) {
   101  	n.walkCalled = true
   102  	return NewDirent(ctx, NewInode(ctx, &MockInodeOperations{}, dir.MountSource, StableAttr{}), p), nil
   103  }
   104  
   105  // SetPermissions implements fs.InodeOperations.SetPermissions.
   106  func (n *MockInodeOperations) SetPermissions(context.Context, *Inode, FilePermissions) bool {
   107  	return false
   108  }
   109  
   110  // SetOwner implements fs.InodeOperations.SetOwner.
   111  func (*MockInodeOperations) SetOwner(context.Context, *Inode, FileOwner) error {
   112  	return linuxerr.EINVAL
   113  }
   114  
   115  // SetTimestamps implements fs.InodeOperations.SetTimestamps.
   116  func (n *MockInodeOperations) SetTimestamps(context.Context, *Inode, TimeSpec) error {
   117  	return nil
   118  }
   119  
   120  // Create implements fs.InodeOperations.Create.
   121  func (n *MockInodeOperations) Create(ctx context.Context, dir *Inode, p string, flags FileFlags, perms FilePermissions) (*File, error) {
   122  	n.createCalled = true
   123  	d := NewDirent(ctx, NewInode(ctx, &MockInodeOperations{}, dir.MountSource, StableAttr{}), p)
   124  	return &File{Dirent: d}, nil
   125  }
   126  
   127  // CreateLink implements fs.InodeOperations.CreateLink.
   128  func (n *MockInodeOperations) CreateLink(_ context.Context, dir *Inode, oldname string, newname string) error {
   129  	n.createLinkCalled = true
   130  	return nil
   131  }
   132  
   133  // CreateDirectory implements fs.InodeOperations.CreateDirectory.
   134  func (n *MockInodeOperations) CreateDirectory(context.Context, *Inode, string, FilePermissions) error {
   135  	n.createDirectoryCalled = true
   136  	return nil
   137  }
   138  
   139  // Rename implements fs.InodeOperations.Rename.
   140  func (n *MockInodeOperations) Rename(ctx context.Context, inode *Inode, oldParent *Inode, oldName string, newParent *Inode, newName string, replacement bool) error {
   141  	n.renameCalled = true
   142  	return nil
   143  }
   144  
   145  // Check implements fs.InodeOperations.Check.
   146  func (n *MockInodeOperations) Check(ctx context.Context, inode *Inode, p PermMask) bool {
   147  	return ContextCanAccessFile(ctx, inode, p)
   148  }
   149  
   150  // Release implements fs.InodeOperations.Release.
   151  func (n *MockInodeOperations) Release(context.Context) {}
   152  
   153  // Truncate implements fs.InodeOperations.Truncate.
   154  func (n *MockInodeOperations) Truncate(ctx context.Context, inode *Inode, size int64) error {
   155  	return nil
   156  }
   157  
   158  // Allocate implements fs.InodeOperations.Allocate.
   159  func (n *MockInodeOperations) Allocate(ctx context.Context, inode *Inode, offset, length int64) error {
   160  	return nil
   161  }
   162  
   163  // Remove implements fs.InodeOperations.Remove.
   164  func (n *MockInodeOperations) Remove(context.Context, *Inode, string) error {
   165  	return nil
   166  }
   167  
   168  // RemoveDirectory implements fs.InodeOperations.RemoveDirectory.
   169  func (n *MockInodeOperations) RemoveDirectory(context.Context, *Inode, string) error {
   170  	return nil
   171  }
   172  
   173  // Getlink implements fs.InodeOperations.Getlink.
   174  func (n *MockInodeOperations) Getlink(context.Context, *Inode) (*Dirent, error) {
   175  	return nil, linuxerr.ENOLINK
   176  }