gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/fsimpl/tmpfs/tmpfs_test.go (about) 1 // Copyright 2019 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 tmpfs 16 17 import ( 18 "fmt" 19 "testing" 20 21 "gvisor.dev/gvisor/pkg/abi/linux" 22 "gvisor.dev/gvisor/pkg/atomicbitops" 23 "gvisor.dev/gvisor/pkg/context" 24 "gvisor.dev/gvisor/pkg/fspath" 25 "gvisor.dev/gvisor/pkg/sentry/kernel/auth" 26 "gvisor.dev/gvisor/pkg/sentry/vfs" 27 ) 28 29 // nextFileID is used to generate unique file names. 30 var nextFileID atomicbitops.Int64 31 32 // newTmpfsRoot creates a new tmpfs mount, and returns the root. If the error 33 // is not nil, then cleanup should be called when the root is no longer needed. 34 func newTmpfsRoot(ctx context.Context) (*vfs.VirtualFilesystem, vfs.VirtualDentry, func(), error) { 35 creds := auth.CredentialsFromContext(ctx) 36 37 vfsObj := &vfs.VirtualFilesystem{} 38 if err := vfsObj.Init(ctx); err != nil { 39 return nil, vfs.VirtualDentry{}, nil, fmt.Errorf("VFS init: %v", err) 40 } 41 42 vfsObj.MustRegisterFilesystemType("tmpfs", FilesystemType{}, &vfs.RegisterFilesystemTypeOptions{ 43 AllowUserMount: true, 44 }) 45 mntns, err := vfsObj.NewMountNamespace(ctx, creds, "", "tmpfs", &vfs.MountOptions{}, nil) 46 if err != nil { 47 return nil, vfs.VirtualDentry{}, nil, fmt.Errorf("failed to create tmpfs root mount: %v", err) 48 } 49 root := mntns.Root(ctx) 50 return vfsObj, root, func() { 51 root.DecRef(ctx) 52 mntns.DecRef(ctx) 53 }, nil 54 } 55 56 // newFileFD creates a new file in a new tmpfs mount, and returns the FD. If 57 // the returned err is not nil, then cleanup should be called when the FD is no 58 // longer needed. 59 func newFileFD(ctx context.Context, mode linux.FileMode) (*vfs.FileDescription, func(), error) { 60 creds := auth.CredentialsFromContext(ctx) 61 vfsObj, root, cleanup, err := newTmpfsRoot(ctx) 62 if err != nil { 63 return nil, nil, err 64 } 65 66 filename := fmt.Sprintf("tmpfs-test-file-%d", nextFileID.Add(1)) 67 68 // Create the file that will be write/read. 69 fd, err := vfsObj.OpenAt(ctx, creds, &vfs.PathOperation{ 70 Root: root, 71 Start: root, 72 Path: fspath.Parse(filename), 73 }, &vfs.OpenOptions{ 74 Flags: linux.O_RDWR | linux.O_CREAT | linux.O_EXCL, 75 Mode: linux.ModeRegular | mode, 76 }) 77 if err != nil { 78 cleanup() 79 return nil, nil, fmt.Errorf("failed to create file %q: %v", filename, err) 80 } 81 82 return fd, cleanup, nil 83 } 84 85 // newDirFD is like newFileFD, but for directories. 86 func newDirFD(ctx context.Context, mode linux.FileMode) (*vfs.FileDescription, func(), error) { 87 creds := auth.CredentialsFromContext(ctx) 88 vfsObj, root, cleanup, err := newTmpfsRoot(ctx) 89 if err != nil { 90 return nil, nil, err 91 } 92 93 dirname := fmt.Sprintf("tmpfs-test-dir-%d", nextFileID.Add(1)) 94 95 // Create the dir. 96 if err := vfsObj.MkdirAt(ctx, creds, &vfs.PathOperation{ 97 Root: root, 98 Start: root, 99 Path: fspath.Parse(dirname), 100 }, &vfs.MkdirOptions{ 101 Mode: linux.ModeDirectory | mode, 102 }); err != nil { 103 cleanup() 104 return nil, nil, fmt.Errorf("failed to create directory %q: %v", dirname, err) 105 } 106 107 // Open the dir and return it. 108 fd, err := vfsObj.OpenAt(ctx, creds, &vfs.PathOperation{ 109 Root: root, 110 Start: root, 111 Path: fspath.Parse(dirname), 112 }, &vfs.OpenOptions{ 113 Flags: linux.O_RDONLY | linux.O_DIRECTORY, 114 }) 115 if err != nil { 116 cleanup() 117 return nil, nil, fmt.Errorf("failed to open directory %q: %v", dirname, err) 118 } 119 120 return fd, cleanup, nil 121 } 122 123 // newPipeFD is like newFileFD, but for pipes. 124 func newPipeFD(ctx context.Context, mode linux.FileMode) (*vfs.FileDescription, func(), error) { 125 creds := auth.CredentialsFromContext(ctx) 126 vfsObj, root, cleanup, err := newTmpfsRoot(ctx) 127 if err != nil { 128 return nil, nil, err 129 } 130 131 name := fmt.Sprintf("tmpfs-test-%d", nextFileID.Add(1)) 132 133 if err := vfsObj.MknodAt(ctx, creds, &vfs.PathOperation{ 134 Root: root, 135 Start: root, 136 Path: fspath.Parse(name), 137 }, &vfs.MknodOptions{ 138 Mode: linux.ModeNamedPipe | mode, 139 }); err != nil { 140 cleanup() 141 return nil, nil, fmt.Errorf("failed to create pipe %q: %v", name, err) 142 } 143 144 fd, err := vfsObj.OpenAt(ctx, creds, &vfs.PathOperation{ 145 Root: root, 146 Start: root, 147 Path: fspath.Parse(name), 148 }, &vfs.OpenOptions{ 149 Flags: linux.O_RDWR, 150 }) 151 if err != nil { 152 cleanup() 153 return nil, nil, fmt.Errorf("failed to open pipe %q: %v", name, err) 154 } 155 156 return fd, cleanup, nil 157 } 158 159 func TestParseSize(t *testing.T) { 160 var tests = []struct { 161 s string 162 want uint64 163 wantError bool 164 }{ 165 {"500", 500, false}, 166 {"5k", (5 * 1024), false}, 167 {"5m", (5 * 1024 * 1024), false}, 168 {"5G", (5 * 1024 * 1024 * 1024), false}, 169 {"5t", (5 * 1024 * 1024 * 1024 * 1024), false}, 170 {"5P", (5 * 1024 * 1024 * 1024 * 1024 * 1024), false}, 171 {"5e", (5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024), false}, 172 {"5e3", 0, true}, 173 {"", 0, true}, 174 {"9999999999999999P", 0, true}, 175 } 176 for _, tt := range tests { 177 testname := fmt.Sprintf("%s", tt.s) 178 t.Run(testname, func(t *testing.T) { 179 size, err := parseSize(tt.s) 180 if tt.wantError && err == nil { 181 t.Errorf("Invalid input: %v parsed", tt.s) 182 } 183 if !tt.wantError { 184 if err != nil { 185 t.Errorf("Couldn't parse size, Error: %v", err) 186 } 187 if size != tt.want { 188 t.Errorf("got: %v, want %v", size, tt.want) 189 } 190 } 191 }) 192 } 193 }