gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/fsimpl/eventfd/eventfd_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 eventfd 16 17 import ( 18 "testing" 19 20 "gvisor.dev/gvisor/pkg/abi/linux" 21 "gvisor.dev/gvisor/pkg/sentry/contexttest" 22 "gvisor.dev/gvisor/pkg/sentry/vfs" 23 "gvisor.dev/gvisor/pkg/usermem" 24 "gvisor.dev/gvisor/pkg/waiter" 25 ) 26 27 func TestEventFD(t *testing.T) { 28 initVals := []uint64{ 29 0, 30 // Using a non-zero initial value verifies that writing to an 31 // eventfd signals when the eventfd's counter was already 32 // non-zero. 33 343, 34 } 35 36 for _, initVal := range initVals { 37 ctx := contexttest.Context(t) 38 vfsObj := &vfs.VirtualFilesystem{} 39 if err := vfsObj.Init(ctx); err != nil { 40 t.Fatalf("VFS init: %v", err) 41 } 42 43 // Make a new eventfd that is writable. 44 eventfd, err := New(ctx, vfsObj, initVal, false, linux.O_RDWR) 45 if err != nil { 46 t.Fatalf("New() failed: %v", err) 47 } 48 defer eventfd.DecRef(ctx) 49 50 // Register a callback for a write event. 51 w, ch := waiter.NewChannelEntry(waiter.ReadableEvents) 52 if err := eventfd.EventRegister(&w); err != nil { 53 t.Fatalf("EventRegister(): %v", err) 54 } 55 defer eventfd.EventUnregister(&w) 56 57 data := []byte("00000124") 58 // Create and submit a write request. 59 n, err := eventfd.Write(ctx, usermem.BytesIOSequence(data), vfs.WriteOptions{}) 60 if err != nil { 61 t.Fatal(err) 62 } 63 if n != 8 { 64 t.Errorf("eventfd.write wrote %d bytes, not full int64", n) 65 } 66 67 // Check if the callback fired due to the write event. 68 select { 69 case <-ch: 70 default: 71 t.Errorf("Didn't get notified of EventIn after write") 72 } 73 } 74 } 75 76 func TestEventFDStat(t *testing.T) { 77 ctx := contexttest.Context(t) 78 vfsObj := &vfs.VirtualFilesystem{} 79 if err := vfsObj.Init(ctx); err != nil { 80 t.Fatalf("VFS init: %v", err) 81 } 82 83 // Make a new eventfd that is writable. 84 eventfd, err := New(ctx, vfsObj, 0, false, linux.O_RDWR) 85 if err != nil { 86 t.Fatalf("New() failed: %v", err) 87 } 88 defer eventfd.DecRef(ctx) 89 90 statx, err := eventfd.Stat(ctx, vfs.StatOptions{ 91 Mask: linux.STATX_BASIC_STATS, 92 }) 93 if err != nil { 94 t.Fatalf("eventfd.Stat failed: %v", err) 95 } 96 if statx.Size != 0 { 97 t.Errorf("eventfd size should be 0") 98 } 99 }