go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/transport/tcp/tcp_test.go (about)

     1  // Copyright 2019 The Mangos Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use 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 tcp
    16  
    17  import (
    18  	"net"
    19  	"testing"
    20  	"time"
    21  
    22  	"go.nanomsg.org/mangos/v3"
    23  	. "go.nanomsg.org/mangos/v3/internal/test"
    24  )
    25  
    26  var tran = Transport
    27  
    28  func TestTcpRecvMax(t *testing.T) {
    29  	TranVerifyMaxRecvSize(t, tran, nil, nil)
    30  }
    31  
    32  func TestTcpOptions(t *testing.T) {
    33  	TranVerifyInvalidOption(t, tran)
    34  	TranVerifyIntOption(t, tran, mangos.OptionMaxRecvSize)
    35  	TranVerifyNoDelayOption(t, tran)
    36  	TranVerifyKeepAliveOption(t, tran)
    37  }
    38  
    39  func TestTcpScheme(t *testing.T) {
    40  	TranVerifyScheme(t, tran)
    41  }
    42  func TestTcpAcceptWithoutListen(t *testing.T) {
    43  	TranVerifyAcceptWithoutListen(t, tran)
    44  }
    45  func TestTcpListenAndAccept(t *testing.T) {
    46  	TranVerifyListenAndAccept(t, tran, nil, nil)
    47  }
    48  func TestTcpDuplicateListen(t *testing.T) {
    49  	TranVerifyDuplicateListen(t, tran, nil)
    50  }
    51  func TestTcpConnectionRefused(t *testing.T) {
    52  	TranVerifyConnectionRefused(t, tran, nil)
    53  }
    54  func TestTcpHandshake(t *testing.T) {
    55  	TranVerifyHandshakeFail(t, tran, nil, nil)
    56  }
    57  func TestTcpSendRecv(t *testing.T) {
    58  	TranVerifySendRecv(t, tran, nil, nil)
    59  }
    60  func TestTcpAnonymousPort(t *testing.T) {
    61  	TranVerifyAnonymousPort(t, "tcp://127.0.0.1:0", nil, nil)
    62  }
    63  func TestTcpInvalidDomain(t *testing.T) {
    64  	TranVerifyBadAddress(t, "tcp://invalid.invalid", nil, nil)
    65  }
    66  func TestTcpInvalidLocalIP(t *testing.T) {
    67  	TranVerifyBadLocalAddress(t, "tcp://1.1.1.1:80", nil)
    68  }
    69  func TestTcpBroadcastIP(t *testing.T) {
    70  	TranVerifyBadAddress(t, "tcp://255.255.255.255:80", nil, nil)
    71  }
    72  func TestTcpListenerClosed(t *testing.T) {
    73  	TranVerifyListenerClosed(t, tran, nil)
    74  }
    75  
    76  func TestTcpResolverChange(t *testing.T) {
    77  	sock := GetMockSocket()
    78  	defer MustClose(t, sock)
    79  
    80  	addr := AddrTestTCP()
    81  	MustSucceed(t, sock.Listen(addr))
    82  
    83  	d, e := tran.NewDialer(addr, sock)
    84  	MustSucceed(t, e)
    85  	td := d.(*dialer)
    86  	addr = td.addr
    87  	td.addr = "tcp://invalid.invalid:80"
    88  	p, e := d.Dial()
    89  	MustFail(t, e)
    90  	MustBeTrue(t, p == nil)
    91  
    92  	td.addr = addr
    93  	p, e = d.Dial()
    94  	MustSucceed(t, e)
    95  	MustSucceed(t, p.Close())
    96  }
    97  
    98  func TestTcpAcceptAbort(t *testing.T) {
    99  	sock := GetMockSocket()
   100  	defer MustClose(t, sock)
   101  
   102  	addr := AddrTestTCP()
   103  	l, e := tran.NewListener(addr, sock)
   104  	MustSucceed(t, e)
   105  	MustSucceed(t, l.Listen())
   106  	_ = l.(*listener).l.Close()
   107  	// This will make the accept loop spin hard, but nothing much
   108  	// we can do about it.
   109  	time.Sleep(time.Millisecond * 50)
   110  }
   111  
   112  func TestTcpMessageSize(t *testing.T) {
   113  	TranVerifyMessageSizes(t, tran, nil, nil)
   114  }
   115  func TestTcpMessageHeader(t *testing.T) {
   116  	TranVerifyMessageHeader(t, tran, nil, nil)
   117  }
   118  func TestTcpVerifyPipeAddresses(t *testing.T) {
   119  	TranVerifyPipeAddresses(t, tran, nil, nil)
   120  }
   121  func TestTcpVerifyPipeOptions(t *testing.T) {
   122  	TranVerifyPipeOptions2(t, tran, nil, nil)
   123  }
   124  
   125  type testAddr string
   126  
   127  func (a testAddr) testDial() (net.Conn, error) {
   128  	return net.Dial("tcp", string(a)[len("tcp://"):])
   129  }
   130  
   131  func TestTcpAbortHandshake(t *testing.T) {
   132  	sock := GetMockSocket()
   133  	defer MustClose(t, sock)
   134  	addr := AddrTestTCP()
   135  	l, e := sock.NewListener(addr, nil)
   136  	MustSucceed(t, e)
   137  	MustSucceed(t, l.Listen())
   138  	c, e := testAddr(addr).testDial()
   139  	MustSucceed(t, e)
   140  	MustSucceed(t, c.Close())
   141  }
   142  
   143  func TestTcpBadHandshake(t *testing.T) {
   144  	sock := GetMockSocket()
   145  	defer MustClose(t, sock)
   146  	addr := AddrTestTCP()
   147  	l, e := sock.NewListener(addr, nil)
   148  	MustSucceed(t, e)
   149  	MustSucceed(t, l.Listen())
   150  	TranSendConnBadHandshakes(t, testAddr(addr).testDial)
   151  }
   152  
   153  func TestTcpBadRecv(t *testing.T) {
   154  	sock := GetMockSocket()
   155  	defer MustClose(t, sock)
   156  	addr := AddrTestTCP()
   157  	l, e := sock.NewListener(addr, nil)
   158  	MustSucceed(t, e)
   159  	MustSucceed(t, l.Listen())
   160  	TranSendBadMessages(t, sock.Info().Peer, false, testAddr(addr).testDial)
   161  }
   162  
   163  func TestTcpSendAbort(t *testing.T) {
   164  	sock := GetMockSocket()
   165  	defer MustClose(t, sock)
   166  	addr := AddrTestTCP()
   167  	l, e := sock.NewListener(addr, nil)
   168  	MustSucceed(t, e)
   169  	MustSucceed(t, l.Listen())
   170  	c, e := testAddr(addr).testDial()
   171  	MustSucceed(t, e)
   172  	TranConnHandshake(t, c, sock.Info().Peer)
   173  	MustSend(t, sock, make([]byte, 2*1024*1024)) // TCP window size is 64k
   174  	time.Sleep(time.Millisecond * 100)
   175  	MustSend(t, sock, make([]byte, 2*1024*1024)) // TCP window size is 64k
   176  	MustSucceed(t, c.Close())
   177  }