github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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 "io" 19 "testing" 20 21 "github.com/SagerNet/gvisor/pkg/abi/linux" 22 "github.com/SagerNet/gvisor/pkg/marshal" 23 "github.com/SagerNet/gvisor/pkg/sentry/fsimpl/testutil" 24 "github.com/SagerNet/gvisor/pkg/sentry/kernel" 25 "github.com/SagerNet/gvisor/pkg/sentry/kernel/auth" 26 "github.com/SagerNet/gvisor/pkg/sentry/vfs" 27 28 "github.com/SagerNet/gvisor/pkg/hostarch" 29 ) 30 31 func setup(t *testing.T) *testutil.System { 32 k, err := testutil.Boot() 33 if err != nil { 34 t.Fatalf("Error creating kernel: %v", err) 35 } 36 37 ctx := k.SupervisorContext() 38 creds := auth.CredentialsFromContext(ctx) 39 40 k.VFS().MustRegisterFilesystemType(Name, &FilesystemType{}, &vfs.RegisterFilesystemTypeOptions{ 41 AllowUserList: true, 42 AllowUserMount: true, 43 }) 44 45 mntns, err := k.VFS().NewMountNamespace(ctx, creds, "", "tmpfs", &vfs.MountOptions{}) 46 if err != nil { 47 t.Fatalf("NewMountNamespace(): %v", err) 48 } 49 50 return testutil.NewSystem(ctx, t, k.VFS(), mntns) 51 } 52 53 // newTestConnection creates a fuse connection that the sentry can communicate with 54 // and the FD for the server to communicate with. 55 func newTestConnection(system *testutil.System, k *kernel.Kernel, maxActiveRequests uint64) (*connection, *vfs.FileDescription, error) { 56 fuseDev := &DeviceFD{} 57 58 vd := system.VFS.NewAnonVirtualDentry("fuse") 59 defer vd.DecRef(system.Ctx) 60 if err := fuseDev.vfsfd.Init(fuseDev, linux.O_RDWR, vd.Mount(), vd.Dentry(), &vfs.FileDescriptionOptions{}); err != nil { 61 return nil, nil, err 62 } 63 64 fsopts := filesystemOptions{ 65 maxActiveRequests: maxActiveRequests, 66 } 67 fs, err := newFUSEFilesystem(system.Ctx, system.VFS, &FilesystemType{}, fuseDev, 0, &fsopts) 68 if err != nil { 69 return nil, nil, err 70 } 71 return fs.conn, &fuseDev.vfsfd, nil 72 } 73 74 type testPayload struct { 75 marshal.StubMarshallable 76 data uint32 77 } 78 79 // SizeBytes implements marshal.Marshallable.SizeBytes. 80 func (t *testPayload) SizeBytes() int { 81 return 4 82 } 83 84 // MarshalBytes implements marshal.Marshallable.MarshalBytes. 85 func (t *testPayload) MarshalBytes(dst []byte) { 86 hostarch.ByteOrder.PutUint32(dst[:4], t.data) 87 } 88 89 // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. 90 func (t *testPayload) UnmarshalBytes(src []byte) { 91 *t = testPayload{data: hostarch.ByteOrder.Uint32(src[:4])} 92 } 93 94 // Packed implements marshal.Marshallable.Packed. 95 func (t *testPayload) Packed() bool { 96 return true 97 } 98 99 // MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe. 100 func (t *testPayload) MarshalUnsafe(dst []byte) { 101 t.MarshalBytes(dst) 102 } 103 104 // UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe. 105 func (t *testPayload) UnmarshalUnsafe(src []byte) { 106 t.UnmarshalBytes(src) 107 } 108 109 // CopyOutN implements marshal.Marshallable.CopyOutN. 110 func (t *testPayload) CopyOutN(task marshal.CopyContext, addr hostarch.Addr, limit int) (int, error) { 111 panic("not implemented") 112 } 113 114 // CopyOut implements marshal.Marshallable.CopyOut. 115 func (t *testPayload) CopyOut(task marshal.CopyContext, addr hostarch.Addr) (int, error) { 116 panic("not implemented") 117 } 118 119 // CopyIn implements marshal.Marshallable.CopyIn. 120 func (t *testPayload) CopyIn(task marshal.CopyContext, addr hostarch.Addr) (int, error) { 121 panic("not implemented") 122 } 123 124 // WriteTo implements io.WriterTo.WriteTo. 125 func (t *testPayload) WriteTo(w io.Writer) (int64, error) { 126 panic("not implemented") 127 }