gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/tcpip/stack/addressable_endpoint_state_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 stack_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"gvisor.dev/gvisor/pkg/tcpip"
    21  	"gvisor.dev/gvisor/pkg/tcpip/stack"
    22  )
    23  
    24  // TestAddressableEndpointStateCleanup tests that cleaning up an addressable
    25  // endpoint state removes permanent addresses and leaves groups.
    26  func TestAddressableEndpointStateCleanup(t *testing.T) {
    27  	var ep fakeNetworkEndpoint
    28  	if err := ep.Enable(); err != nil {
    29  		t.Fatalf("ep.Enable(): %s", err)
    30  	}
    31  
    32  	var s stack.AddressableEndpointState
    33  	s.Init(&ep, stack.AddressableEndpointStateOptions{HiddenWhileDisabled: false})
    34  
    35  	addr := tcpip.AddressWithPrefix{
    36  		Address:   tcpip.AddrFromSlice([]byte("\x01\x00\x00\x00")),
    37  		PrefixLen: 32,
    38  	}
    39  
    40  	{
    41  		properties := stack.AddressProperties{PEB: stack.NeverPrimaryEndpoint}
    42  		ep, err := s.AddAndAcquirePermanentAddress(addr, properties)
    43  		if err != nil {
    44  			t.Fatalf("s.AddAndAcquirePermanentAddress(%s, %+v): %s", addr, properties, err)
    45  		}
    46  		// We don't need the address endpoint.
    47  		ep.DecRef()
    48  	}
    49  	{
    50  		ep := s.AcquireAssignedAddress(addr.Address, false /* allowTemp */, stack.NeverPrimaryEndpoint, true /* readOnly */)
    51  		if ep == nil {
    52  			t.Fatalf("got s.AcquireAssignedAddress(%s, false, NeverPrimaryEndpoint) = nil, want = non-nil", addr.Address)
    53  		}
    54  	}
    55  
    56  	s.Cleanup()
    57  	if ep := s.AcquireAssignedAddress(addr.Address, false /* allowTemp */, stack.NeverPrimaryEndpoint, true /* readOnly */); ep != nil {
    58  		t.Fatalf("got s.AcquireAssignedAddress(%s, false, NeverPrimaryEndpoint) = %s, want = nil", addr.Address, ep.AddressWithPrefix())
    59  	}
    60  }
    61  
    62  func TestAddressDispatcherExpiredToAssigned(t *testing.T) {
    63  	var networkEp fakeNetworkEndpoint
    64  	if err := networkEp.Enable(); err != nil {
    65  		t.Fatalf("ep.Enable(): %s", err)
    66  	}
    67  
    68  	var s stack.AddressableEndpointState
    69  	s.Init(&networkEp, stack.AddressableEndpointStateOptions{HiddenWhileDisabled: false})
    70  
    71  	addr := tcpip.AddressWithPrefix{
    72  		Address:   tcpip.AddrFromSlice([]byte("\x01\x00\x00\x00")),
    73  		PrefixLen: 32,
    74  	}
    75  
    76  	ep, err := s.AddAndAcquirePermanentAddress(addr, stack.AddressProperties{})
    77  	if err != nil {
    78  		t.Fatalf("s.AddAndAcquirePermanentAddress(%s, {}): %s", addr, err)
    79  	}
    80  	defer ep.DecRef()
    81  	if !ep.TryIncRef() {
    82  		t.Fatalf("failed to increase ref count of address endpoint")
    83  	}
    84  
    85  	if err := s.RemovePermanentEndpoint(ep, stack.AddressRemovalManualAction); err != nil {
    86  		ep.DecRef()
    87  		t.Fatalf("s.RemovePermanentEndpoint(ep, stack.AddressRemovalManualAction): %s", err)
    88  	}
    89  
    90  	addrDisp := &addressDispatcher{
    91  		changedCh: make(chan addressChangedEvent, 1),
    92  		removedCh: make(chan stack.AddressRemovalReason, 1),
    93  		addr:      addr,
    94  	}
    95  	properties := stack.AddressProperties{Disp: addrDisp}
    96  	readdedEp, err := s.AddAndAcquirePermanentAddress(addr, properties)
    97  	if err != nil {
    98  		t.Fatalf("s.AddAndAcquirePermanentAddress(%s, %+v): %s", addr, properties, err)
    99  	}
   100  	defer readdedEp.DecRef()
   101  	if err := addrDisp.expectChanged(stack.AddressLifetimes{}, stack.AddressAssigned); err != nil {
   102  		t.Fatalf("expect to observe address added: %s", err)
   103  	}
   104  }