github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/inet/test_stack.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 inet
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"github.com/SagerNet/gvisor/pkg/tcpip"
    22  	"github.com/SagerNet/gvisor/pkg/tcpip/stack"
    23  )
    24  
    25  // TestStack is a dummy implementation of Stack for tests.
    26  type TestStack struct {
    27  	InterfacesMap     map[int32]Interface
    28  	InterfaceAddrsMap map[int32][]InterfaceAddr
    29  	RouteList         []Route
    30  	SupportsIPv6Flag  bool
    31  	TCPRecvBufSize    TCPBufferSize
    32  	TCPSendBufSize    TCPBufferSize
    33  	TCPSACKFlag       bool
    34  	Recovery          TCPLossRecovery
    35  	IPForwarding      bool
    36  }
    37  
    38  // NewTestStack returns a TestStack with no network interfaces. The value of
    39  // all other options is unspecified; tests that rely on specific values must
    40  // set them explicitly.
    41  func NewTestStack() *TestStack {
    42  	return &TestStack{
    43  		InterfacesMap:     make(map[int32]Interface),
    44  		InterfaceAddrsMap: make(map[int32][]InterfaceAddr),
    45  	}
    46  }
    47  
    48  // Interfaces implements Stack.Interfaces.
    49  func (s *TestStack) Interfaces() map[int32]Interface {
    50  	return s.InterfacesMap
    51  }
    52  
    53  // InterfaceAddrs implements Stack.InterfaceAddrs.
    54  func (s *TestStack) InterfaceAddrs() map[int32][]InterfaceAddr {
    55  	return s.InterfaceAddrsMap
    56  }
    57  
    58  // AddInterfaceAddr implements Stack.AddInterfaceAddr.
    59  func (s *TestStack) AddInterfaceAddr(idx int32, addr InterfaceAddr) error {
    60  	s.InterfaceAddrsMap[idx] = append(s.InterfaceAddrsMap[idx], addr)
    61  	return nil
    62  }
    63  
    64  // RemoveInterfaceAddr implements Stack.RemoveInterfaceAddr.
    65  func (s *TestStack) RemoveInterfaceAddr(idx int32, addr InterfaceAddr) error {
    66  	interfaceAddrs, ok := s.InterfaceAddrsMap[idx]
    67  	if !ok {
    68  		return fmt.Errorf("unknown idx: %d", idx)
    69  	}
    70  
    71  	var filteredAddrs []InterfaceAddr
    72  	for _, interfaceAddr := range interfaceAddrs {
    73  		if !bytes.Equal(interfaceAddr.Addr, addr.Addr) {
    74  			filteredAddrs = append(filteredAddrs, addr)
    75  		}
    76  	}
    77  	s.InterfaceAddrsMap[idx] = filteredAddrs
    78  
    79  	return nil
    80  }
    81  
    82  // SupportsIPv6 implements Stack.SupportsIPv6.
    83  func (s *TestStack) SupportsIPv6() bool {
    84  	return s.SupportsIPv6Flag
    85  }
    86  
    87  // TCPReceiveBufferSize implements Stack.TCPReceiveBufferSize.
    88  func (s *TestStack) TCPReceiveBufferSize() (TCPBufferSize, error) {
    89  	return s.TCPRecvBufSize, nil
    90  }
    91  
    92  // SetTCPReceiveBufferSize implements Stack.SetTCPReceiveBufferSize.
    93  func (s *TestStack) SetTCPReceiveBufferSize(size TCPBufferSize) error {
    94  	s.TCPRecvBufSize = size
    95  	return nil
    96  }
    97  
    98  // TCPSendBufferSize implements Stack.TCPSendBufferSize.
    99  func (s *TestStack) TCPSendBufferSize() (TCPBufferSize, error) {
   100  	return s.TCPSendBufSize, nil
   101  }
   102  
   103  // SetTCPSendBufferSize implements Stack.SetTCPSendBufferSize.
   104  func (s *TestStack) SetTCPSendBufferSize(size TCPBufferSize) error {
   105  	s.TCPSendBufSize = size
   106  	return nil
   107  }
   108  
   109  // TCPSACKEnabled implements Stack.TCPSACKEnabled.
   110  func (s *TestStack) TCPSACKEnabled() (bool, error) {
   111  	return s.TCPSACKFlag, nil
   112  }
   113  
   114  // SetTCPSACKEnabled implements Stack.SetTCPSACKEnabled.
   115  func (s *TestStack) SetTCPSACKEnabled(enabled bool) error {
   116  	s.TCPSACKFlag = enabled
   117  	return nil
   118  }
   119  
   120  // TCPRecovery implements Stack.TCPRecovery.
   121  func (s *TestStack) TCPRecovery() (TCPLossRecovery, error) {
   122  	return s.Recovery, nil
   123  }
   124  
   125  // SetTCPRecovery implements Stack.SetTCPRecovery.
   126  func (s *TestStack) SetTCPRecovery(recovery TCPLossRecovery) error {
   127  	s.Recovery = recovery
   128  	return nil
   129  }
   130  
   131  // Statistics implements inet.Stack.Statistics.
   132  func (s *TestStack) Statistics(stat interface{}, arg string) error {
   133  	return nil
   134  }
   135  
   136  // RouteTable implements Stack.RouteTable.
   137  func (s *TestStack) RouteTable() []Route {
   138  	return s.RouteList
   139  }
   140  
   141  // Resume implements Stack.Resume.
   142  func (s *TestStack) Resume() {}
   143  
   144  // RegisteredEndpoints implements inet.Stack.RegisteredEndpoints.
   145  func (s *TestStack) RegisteredEndpoints() []stack.TransportEndpoint {
   146  	return nil
   147  }
   148  
   149  // CleanupEndpoints implements inet.Stack.CleanupEndpoints.
   150  func (s *TestStack) CleanupEndpoints() []stack.TransportEndpoint {
   151  	return nil
   152  }
   153  
   154  // RestoreCleanupEndpoints implements inet.Stack.RestoreCleanupEndpoints.
   155  func (s *TestStack) RestoreCleanupEndpoints([]stack.TransportEndpoint) {}
   156  
   157  // SetForwarding implements inet.Stack.SetForwarding.
   158  func (s *TestStack) SetForwarding(protocol tcpip.NetworkProtocolNumber, enable bool) error {
   159  	s.IPForwarding = enable
   160  	return nil
   161  }
   162  
   163  // PortRange implements inet.Stack.PortRange.
   164  func (*TestStack) PortRange() (uint16, uint16) {
   165  	// Use the default Linux values per net/ipv4/af_inet.c:inet_init_net().
   166  	return 32768, 28232
   167  }
   168  
   169  // SetPortRange implements inet.Stack.SetPortRange.
   170  func (*TestStack) SetPortRange(start uint16, end uint16) error {
   171  	// No-op.
   172  	return nil
   173  }