github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/filetest/filetest.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 filetest provides a test implementation of an fs.File.
    16  package filetest
    17  
    18  import (
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/SagerNet/gvisor/pkg/context"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/contexttest"
    24  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    25  	"github.com/SagerNet/gvisor/pkg/sentry/fs/anon"
    26  	"github.com/SagerNet/gvisor/pkg/sentry/fs/fsutil"
    27  	"github.com/SagerNet/gvisor/pkg/usermem"
    28  	"github.com/SagerNet/gvisor/pkg/waiter"
    29  )
    30  
    31  // TestFileOperations is an implementation of the File interface. It provides all
    32  // required methods.
    33  type TestFileOperations struct {
    34  	fsutil.FileNoopRelease          `state:"nosave"`
    35  	fsutil.FilePipeSeek             `state:"nosave"`
    36  	fsutil.FileNotDirReaddir        `state:"nosave"`
    37  	fsutil.FileNoFsync              `state:"nosave"`
    38  	fsutil.FileNoopFlush            `state:"nosave"`
    39  	fsutil.FileNoMMap               `state:"nosave"`
    40  	fsutil.FileNoIoctl              `state:"nosave"`
    41  	fsutil.FileNoSplice             `state:"nosave"`
    42  	fsutil.FileUseInodeUnstableAttr `state:"nosave"`
    43  	waiter.AlwaysReady              `state:"nosave"`
    44  }
    45  
    46  // NewTestFile creates and initializes a new test file.
    47  func NewTestFile(tb testing.TB) *fs.File {
    48  	ctx := contexttest.Context(tb)
    49  	dirent := fs.NewDirent(ctx, anon.NewInode(ctx), "test")
    50  	return fs.NewFile(ctx, dirent, fs.FileFlags{}, &TestFileOperations{})
    51  }
    52  
    53  // Read just fails the request.
    54  func (*TestFileOperations) Read(context.Context, *fs.File, usermem.IOSequence, int64) (int64, error) {
    55  	return 0, fmt.Errorf("TestFileOperations.Read not implemented")
    56  }
    57  
    58  // Write just fails the request.
    59  func (*TestFileOperations) Write(context.Context, *fs.File, usermem.IOSequence, int64) (int64, error) {
    60  	return 0, fmt.Errorf("TestFileOperations.Write not implemented")
    61  }