github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/contexttest"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/vfs"
    23  	"github.com/SagerNet/gvisor/pkg/usermem"
    24  	"github.com/SagerNet/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(nil)
    52  		eventfd.EventRegister(&w, waiter.ReadableEvents)
    53  		defer eventfd.EventUnregister(&w)
    54  
    55  		data := []byte("00000124")
    56  		// Create and submit a write request.
    57  		n, err := eventfd.Write(ctx, usermem.BytesIOSequence(data), vfs.WriteOptions{})
    58  		if err != nil {
    59  			t.Fatal(err)
    60  		}
    61  		if n != 8 {
    62  			t.Errorf("eventfd.write wrote %d bytes, not full int64", n)
    63  		}
    64  
    65  		// Check if the callback fired due to the write event.
    66  		select {
    67  		case <-ch:
    68  		default:
    69  			t.Errorf("Didn't get notified of EventIn after write")
    70  		}
    71  	}
    72  }
    73  
    74  func TestEventFDStat(t *testing.T) {
    75  	ctx := contexttest.Context(t)
    76  	vfsObj := &vfs.VirtualFilesystem{}
    77  	if err := vfsObj.Init(ctx); err != nil {
    78  		t.Fatalf("VFS init: %v", err)
    79  	}
    80  
    81  	// Make a new eventfd that is writable.
    82  	eventfd, err := New(ctx, vfsObj, 0, false, linux.O_RDWR)
    83  	if err != nil {
    84  		t.Fatalf("New() failed: %v", err)
    85  	}
    86  	defer eventfd.DecRef(ctx)
    87  
    88  	statx, err := eventfd.Stat(ctx, vfs.StatOptions{
    89  		Mask: linux.STATX_BASIC_STATS,
    90  	})
    91  	if err != nil {
    92  		t.Fatalf("eventfd.Stat failed: %v", err)
    93  	}
    94  	if statx.Size != 0 {
    95  		t.Errorf("eventfd size should be 0")
    96  	}
    97  }