github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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  	"github.com/SagerNet/gvisor/pkg/tcpip"
    21  	"github.com/SagerNet/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)
    34  
    35  	addr := tcpip.AddressWithPrefix{
    36  		Address:   "\x01",
    37  		PrefixLen: 8,
    38  	}
    39  
    40  	{
    41  		ep, err := s.AddAndAcquirePermanentAddress(addr, stack.NeverPrimaryEndpoint, stack.AddressConfigStatic, false /* deprecated */)
    42  		if err != nil {
    43  			t.Fatalf("s.AddAndAcquirePermanentAddress(%s, %d, %d, false): %s", addr, stack.NeverPrimaryEndpoint, stack.AddressConfigStatic, err)
    44  		}
    45  		// We don't need the address endpoint.
    46  		ep.DecRef()
    47  	}
    48  	{
    49  		ep := s.AcquireAssignedAddress(addr.Address, false /* allowTemp */, stack.NeverPrimaryEndpoint)
    50  		if ep == nil {
    51  			t.Fatalf("got s.AcquireAssignedAddress(%s, false, NeverPrimaryEndpoint) = nil, want = non-nil", addr.Address)
    52  		}
    53  		ep.DecRef()
    54  	}
    55  
    56  	s.Cleanup()
    57  	if ep := s.AcquireAssignedAddress(addr.Address, false /* allowTemp */, stack.NeverPrimaryEndpoint); ep != nil {
    58  		ep.DecRef()
    59  		t.Fatalf("got s.AcquireAssignedAddress(%s, false, NeverPrimaryEndpoint) = %s, want = nil", addr.Address, ep.AddressWithPrefix())
    60  	}
    61  }