github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fsimpl/sys/sys_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 sys_test 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/google/go-cmp/cmp" 22 "github.com/SagerNet/gvisor/pkg/abi/linux" 23 "github.com/SagerNet/gvisor/pkg/sentry/fsimpl/sys" 24 "github.com/SagerNet/gvisor/pkg/sentry/fsimpl/testutil" 25 "github.com/SagerNet/gvisor/pkg/sentry/kernel" 26 "github.com/SagerNet/gvisor/pkg/sentry/kernel/auth" 27 "github.com/SagerNet/gvisor/pkg/sentry/vfs" 28 ) 29 30 func newTestSystem(t *testing.T) *testutil.System { 31 k, err := testutil.Boot() 32 if err != nil { 33 t.Fatalf("Failed to create test kernel: %v", err) 34 } 35 ctx := k.SupervisorContext() 36 creds := auth.CredentialsFromContext(ctx) 37 k.VFS().MustRegisterFilesystemType(sys.Name, sys.FilesystemType{}, &vfs.RegisterFilesystemTypeOptions{ 38 AllowUserMount: true, 39 }) 40 41 mns, err := k.VFS().NewMountNamespace(ctx, creds, "", sys.Name, &vfs.MountOptions{}) 42 if err != nil { 43 t.Fatalf("Failed to create new mount namespace: %v", err) 44 } 45 return testutil.NewSystem(ctx, t, k.VFS(), mns) 46 } 47 48 func TestReadCPUFile(t *testing.T) { 49 s := newTestSystem(t) 50 defer s.Destroy() 51 k := kernel.KernelFromContext(s.Ctx) 52 maxCPUCores := k.ApplicationCores() 53 54 expected := fmt.Sprintf("0-%d\n", maxCPUCores-1) 55 56 for _, fname := range []string{"online", "possible", "present"} { 57 pop := s.PathOpAtRoot(fmt.Sprintf("devices/system/cpu/%s", fname)) 58 fd, err := s.VFS.OpenAt(s.Ctx, s.Creds, pop, &vfs.OpenOptions{}) 59 if err != nil { 60 t.Fatalf("OpenAt(pop:%+v) = %+v failed: %v", pop, fd, err) 61 } 62 defer fd.DecRef(s.Ctx) 63 content, err := s.ReadToEnd(fd) 64 if err != nil { 65 t.Fatalf("Read failed: %v", err) 66 } 67 if diff := cmp.Diff(expected, content); diff != "" { 68 t.Fatalf("Read returned unexpected data:\n--- want\n+++ got\n%v", diff) 69 } 70 } 71 } 72 73 func TestSysRootContainsExpectedEntries(t *testing.T) { 74 s := newTestSystem(t) 75 defer s.Destroy() 76 pop := s.PathOpAtRoot("/") 77 s.AssertAllDirentTypes(s.ListDirents(pop), map[string]testutil.DirentType{ 78 "block": linux.DT_DIR, 79 "bus": linux.DT_DIR, 80 "class": linux.DT_DIR, 81 "dev": linux.DT_DIR, 82 "devices": linux.DT_DIR, 83 "firmware": linux.DT_DIR, 84 "fs": linux.DT_DIR, 85 "kernel": linux.DT_DIR, 86 "module": linux.DT_DIR, 87 "power": linux.DT_DIR, 88 }) 89 }