gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/kernel/pipe/pipe_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 pipe 16 17 import ( 18 "bytes" 19 "testing" 20 21 "gvisor.dev/gvisor/pkg/context" 22 "gvisor.dev/gvisor/pkg/errors/linuxerr" 23 "gvisor.dev/gvisor/pkg/sentry/contexttest" 24 "gvisor.dev/gvisor/pkg/sentry/vfs" 25 "gvisor.dev/gvisor/pkg/usermem" 26 "gvisor.dev/gvisor/pkg/waiter" 27 ) 28 29 func runTest(t *testing.T, sizeBytes int64, tester func(ctx context.Context, r, w *vfs.FileDescription)) { 30 ctx := contexttest.Context(t) 31 vfsObj := &vfs.VirtualFilesystem{} 32 if err := vfsObj.Init(ctx); err != nil { 33 t.Fatalf("VFS init: %v", err) 34 } 35 vd := vfsObj.NewAnonVirtualDentry("pipe") 36 defer vd.DecRef(ctx) 37 38 vp := NewVFSPipe(false /* isNamed */, sizeBytes) 39 r, w, err := vp.ReaderWriterPair(ctx, vd.Mount(), vd.Dentry(), 0) 40 if err != nil { 41 t.Fatalf("ReaderWriterPair failed: %v", err) 42 } 43 defer r.DecRef(ctx) 44 defer w.DecRef(ctx) 45 46 tester(ctx, r, w) 47 } 48 49 func TestPipeRW(t *testing.T) { 50 runTest(t, 65536, func(ctx context.Context, r *vfs.FileDescription, w *vfs.FileDescription) { 51 msg := []byte("here's some bytes") 52 wantN := int64(len(msg)) 53 n, err := w.Write(ctx, usermem.BytesIOSequence(msg), vfs.WriteOptions{}) 54 if n != wantN || err != nil { 55 t.Fatalf("Writev: got (%d, %v), wanted (%d, nil)", n, err, wantN) 56 } 57 58 buf := make([]byte, len(msg)) 59 n, err = r.Read(ctx, usermem.BytesIOSequence(buf), vfs.ReadOptions{}) 60 if n != wantN || err != nil || !bytes.Equal(buf, msg) { 61 t.Fatalf("Readv: got (%d, %v) %q, wanted (%d, nil) %q", n, err, buf, wantN, msg) 62 } 63 }) 64 } 65 66 func TestPipeReadBlock(t *testing.T) { 67 runTest(t, 65536, func(ctx context.Context, r *vfs.FileDescription, w *vfs.FileDescription) { 68 n, err := r.Read(ctx, usermem.BytesIOSequence(make([]byte, 1)), vfs.ReadOptions{}) 69 if n != 0 || err != linuxerr.ErrWouldBlock { 70 t.Fatalf("Readv: got (%d, %v), wanted (0, %v)", n, err, linuxerr.ErrWouldBlock) 71 } 72 }) 73 } 74 75 func TestPipeWriteBlock(t *testing.T) { 76 const atomicIOBytes = 2 77 const capacity = MinimumPipeSize 78 79 runTest(t, capacity, func(ctx context.Context, r *vfs.FileDescription, w *vfs.FileDescription) { 80 msg := make([]byte, capacity+1) 81 n, err := w.Write(ctx, usermem.BytesIOSequence(msg), vfs.WriteOptions{}) 82 if wantN, wantErr := int64(capacity), linuxerr.ErrWouldBlock; n != wantN || err != wantErr { 83 t.Fatalf("Writev: got (%d, %v), wanted (%d, %v)", n, err, wantN, wantErr) 84 } 85 }) 86 } 87 88 func TestPipeWriteUntilEnd(t *testing.T) { 89 const atomicIOBytes = 2 90 runTest(t, atomicIOBytes, func(ctx context.Context, r *vfs.FileDescription, w *vfs.FileDescription) { 91 msg := []byte("here's some bytes") 92 93 wDone := make(chan struct{}, 0) 94 rDone := make(chan struct{}, 0) 95 defer func() { 96 // Signal the reader to stop and wait until it does so. 97 close(wDone) 98 <-rDone 99 }() 100 101 go func() { 102 defer close(rDone) 103 // Read from r until done is closed. 104 ctx := contexttest.Context(t) 105 buf := make([]byte, len(msg)+1) 106 dst := usermem.BytesIOSequence(buf) 107 e, ch := waiter.NewChannelEntry(waiter.ReadableEvents) 108 r.EventRegister(&e) 109 defer r.EventUnregister(&e) 110 for { 111 n, err := r.Read(ctx, dst, vfs.ReadOptions{}) 112 dst = dst.DropFirst64(n) 113 if err == linuxerr.ErrWouldBlock { 114 select { 115 case <-ch: 116 continue 117 case <-wDone: 118 // We expect to have 1 byte left in dst since len(buf) == 119 // len(msg)+1. 120 if dst.NumBytes() != 1 || !bytes.Equal(buf[:len(msg)], msg) { 121 t.Errorf("Reader: got %q (%d bytes remaining), wanted %q", buf, dst.NumBytes(), msg) 122 } 123 return 124 } 125 } 126 if err != nil { 127 t.Errorf("Readv: got unexpected error %v", err) 128 return 129 } 130 } 131 }() 132 133 src := usermem.BytesIOSequence(msg) 134 e, ch := waiter.NewChannelEntry(waiter.WritableEvents) 135 w.EventRegister(&e) 136 defer w.EventUnregister(&e) 137 for src.NumBytes() != 0 { 138 n, err := w.Write(ctx, src, vfs.WriteOptions{}) 139 src = src.DropFirst64(n) 140 if err == linuxerr.ErrWouldBlock { 141 <-ch 142 continue 143 } 144 if err != nil { 145 t.Fatalf("Writev: got (%d, %v)", n, err) 146 } 147 } 148 }) 149 }