gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/tcpip/link/nested/nested_test.go (about)

     1  // Copyright 2020 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 nested_test
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  
    21  	"gvisor.dev/gvisor/pkg/refs"
    22  	"gvisor.dev/gvisor/pkg/tcpip"
    23  	"gvisor.dev/gvisor/pkg/tcpip/header"
    24  	"gvisor.dev/gvisor/pkg/tcpip/link/nested"
    25  	"gvisor.dev/gvisor/pkg/tcpip/stack"
    26  )
    27  
    28  type parentEndpoint struct {
    29  	nested.Endpoint
    30  }
    31  
    32  var _ stack.LinkEndpoint = (*parentEndpoint)(nil)
    33  var _ stack.NetworkDispatcher = (*parentEndpoint)(nil)
    34  
    35  type childEndpoint struct {
    36  	stack.LinkEndpoint
    37  	dispatcher stack.NetworkDispatcher
    38  }
    39  
    40  var _ stack.LinkEndpoint = (*childEndpoint)(nil)
    41  
    42  func (c *childEndpoint) Attach(dispatcher stack.NetworkDispatcher) {
    43  	c.dispatcher = dispatcher
    44  }
    45  
    46  func (c *childEndpoint) IsAttached() bool {
    47  	return c.dispatcher != nil
    48  }
    49  
    50  type counterDispatcher struct {
    51  	count int
    52  }
    53  
    54  var _ stack.NetworkDispatcher = (*counterDispatcher)(nil)
    55  
    56  func (d *counterDispatcher) DeliverNetworkPacket(tcpip.NetworkProtocolNumber, *stack.PacketBuffer) {
    57  	d.count++
    58  }
    59  
    60  func (*counterDispatcher) DeliverLinkPacket(tcpip.NetworkProtocolNumber, *stack.PacketBuffer) {
    61  	panic("not implemented")
    62  }
    63  
    64  func TestNestedLinkEndpoint(t *testing.T) {
    65  	var (
    66  		childEP  childEndpoint
    67  		nestedEP parentEndpoint
    68  		disp     counterDispatcher
    69  	)
    70  	nestedEP.Endpoint.Init(&childEP, &nestedEP)
    71  
    72  	if childEP.IsAttached() {
    73  		t.Error("On init, childEP.IsAttached() = true, want = false")
    74  	}
    75  	if nestedEP.IsAttached() {
    76  		t.Error("On init, nestedEP.IsAttached() = true, want = false")
    77  	}
    78  
    79  	nestedEP.Attach(&disp)
    80  	if disp.count != 0 {
    81  		t.Fatalf("After attach, got disp.count = %d, want = 0", disp.count)
    82  	}
    83  	if !childEP.IsAttached() {
    84  		t.Error("After attach, childEP.IsAttached() = false, want = true")
    85  	}
    86  	if !nestedEP.IsAttached() {
    87  		t.Error("After attach, nestedEP.IsAttached() = false, want = true")
    88  	}
    89  
    90  	{
    91  		p := stack.NewPacketBuffer(stack.PacketBufferOptions{})
    92  		nestedEP.DeliverNetworkPacket(header.IPv4ProtocolNumber, p)
    93  		p.DecRef()
    94  		if disp.count != 1 {
    95  			t.Errorf("After first packet with dispatcher attached, got disp.count = %d, want = 1", disp.count)
    96  		}
    97  	}
    98  
    99  	nestedEP.Attach(nil)
   100  	if childEP.IsAttached() {
   101  		t.Error("After detach, childEP.IsAttached() = true, want = false")
   102  	}
   103  	if nestedEP.IsAttached() {
   104  		t.Error("After detach, nestedEP.IsAttached() = true, want = false")
   105  	}
   106  
   107  	{
   108  		disp.count = 0
   109  		p := stack.NewPacketBuffer(stack.PacketBufferOptions{})
   110  		nestedEP.DeliverNetworkPacket(header.IPv4ProtocolNumber, p)
   111  		p.DecRef()
   112  		if disp.count != 0 {
   113  			t.Errorf("After second packet with dispatcher detached, got disp.count = %d, want = 0", disp.count)
   114  		}
   115  	}
   116  }
   117  
   118  func TestMain(m *testing.M) {
   119  	refs.SetLeakMode(refs.LeaksPanic)
   120  	code := m.Run()
   121  	refs.DoLeakCheck()
   122  	os.Exit(code)
   123  }