github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/device/device_test.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 device 16 17 import ( 18 "testing" 19 ) 20 21 func TestMultiDevice(t *testing.T) { 22 device := &MultiDevice{} 23 24 // Check that Load fails to install virtual inodes that are 25 // uninitialized. 26 if device.Load(MultiDeviceKey{}, 0) { 27 t.Fatalf("got load of invalid virtual inode 0, want unsuccessful") 28 } 29 30 inode := device.Map(MultiDeviceKey{}) 31 32 // Assert that the same raw device and inode map to 33 // a consistent virtual inode. 34 if i := device.Map(MultiDeviceKey{}); i != inode { 35 t.Fatalf("got inode %d, want %d in %s", i, inode, device) 36 } 37 38 // Assert that a new inode or new device does not conflict. 39 if i := device.Map(MultiDeviceKey{Device: 0, Inode: 1}); i == inode { 40 t.Fatalf("got reused inode %d, want new distinct inode in %s", i, device) 41 } 42 last := device.Map(MultiDeviceKey{Device: 1, Inode: 0}) 43 if last == inode { 44 t.Fatalf("got reused inode %d, want new distinct inode in %s", last, device) 45 } 46 47 // Virtual is the virtual inode we want to load. 48 virtual := last + 1 49 50 // Assert that we can load a virtual inode at a new place. 51 if !device.Load(MultiDeviceKey{Device: 0, Inode: 2}, virtual) { 52 t.Fatalf("got load of virtual inode %d failed, want success in %s", virtual, device) 53 } 54 55 // Assert that the next inode skips over the loaded one. 56 if i := device.Map(MultiDeviceKey{Device: 0, Inode: 3}); i != virtual+1 { 57 t.Fatalf("got inode %d, want %d in %s", i, virtual+1, device) 58 } 59 }