gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/fsimpl/fuse/utils_test.go (about)

     1  // Copyright 2020 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 fuse
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"gvisor.dev/gvisor/pkg/abi/linux"
    22  	"gvisor.dev/gvisor/pkg/sentry/fsimpl/testutil"
    23  	"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
    24  	"gvisor.dev/gvisor/pkg/sentry/vfs"
    25  )
    26  
    27  func setup(t *testing.T) *testutil.System {
    28  	k, err := testutil.Boot()
    29  	if err != nil {
    30  		t.Fatalf("Error creating kernel: %v", err)
    31  	}
    32  
    33  	ctx := k.SupervisorContext()
    34  	creds := auth.CredentialsFromContext(ctx)
    35  
    36  	k.VFS().MustRegisterFilesystemType(Name, &FilesystemType{}, &vfs.RegisterFilesystemTypeOptions{
    37  		AllowUserList:  true,
    38  		AllowUserMount: true,
    39  	})
    40  
    41  	mntns, err := k.VFS().NewMountNamespace(ctx, creds, "", "tmpfs", &vfs.MountOptions{}, nil)
    42  	if err != nil {
    43  		t.Fatalf("NewMountNamespace(): %v", err)
    44  	}
    45  
    46  	return testutil.NewSystem(ctx, t, k.VFS(), mntns)
    47  }
    48  
    49  // newTestConnection creates a fuse connection that the sentry can communicate with
    50  // and the FD for the server to communicate with.
    51  func newTestConnection(system *testutil.System, maxActiveRequests uint64) (*connection, *vfs.FileDescription, error) {
    52  	fuseDev := &DeviceFD{}
    53  
    54  	vd := system.VFS.NewAnonVirtualDentry("fuse")
    55  	defer vd.DecRef(system.Ctx)
    56  	if err := fuseDev.vfsfd.Init(fuseDev, linux.O_RDWR, vd.Mount(), vd.Dentry(), &vfs.FileDescriptionOptions{}); err != nil {
    57  		return nil, nil, err
    58  	}
    59  
    60  	fsopts := filesystemOptions{
    61  		maxActiveRequests: maxActiveRequests,
    62  	}
    63  	fuseDev.mu.Lock()
    64  	conn, err := newFUSEConnection(system.Ctx, fuseDev, &fsopts)
    65  	if err != nil {
    66  		return nil, nil, err
    67  	}
    68  	fuseDev.conn = conn
    69  	fuseDev.mu.Unlock()
    70  
    71  	// Fake the connection being properly initialized for testing purposes.
    72  	conn.mu.Lock()
    73  	conn.connInitSuccess = true
    74  	conn.mu.Unlock()
    75  	return conn, &fuseDev.vfsfd, nil
    76  }
    77  
    78  // newTestFilesystem creates a filesystem that the sentry can communicate with
    79  // and the FD for the server to communicate with.
    80  func newTestFilesystem(system *testutil.System, fd *vfs.FileDescription, maxActiveRequests uint64) (*filesystem, error) {
    81  	fuseFD, ok := fd.Impl().(*DeviceFD)
    82  	if !ok {
    83  		return nil, fmt.Errorf("newTestFilesystem: FD is %T, not a FUSE device", fd)
    84  	}
    85  	fsopts := filesystemOptions{
    86  		maxActiveRequests: maxActiveRequests,
    87  	}
    88  
    89  	fuseFD.mu.Lock()
    90  	defer fuseFD.mu.Unlock()
    91  	fs, err := newFUSEFilesystem(system.Ctx, system.VFS, &FilesystemType{}, fuseFD, 0, &fsopts)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return fs, nil
    96  }