nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/transport/tlstcp/tlstcp_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 tlstcp
    16  
    17  import (
    18  	"crypto/tls"
    19  	"testing"
    20  	"time"
    21  
    22  	"nanomsg.org/go/mangos/v2"
    23  	"nanomsg.org/go/mangos/v2/internal/test"
    24  )
    25  
    26  var tran = Transport
    27  var lOpts map[string]interface{}
    28  var dOpts map[string]interface{}
    29  
    30  func init() {
    31  	dOpts = make(map[string]interface{})
    32  	lOpts = make(map[string]interface{})
    33  	var t *testing.T
    34  	dOpts[mangos.OptionTLSConfig] = test.GetTLSConfig(t, false)
    35  	lOpts[mangos.OptionTLSConfig] = test.GetTLSConfig(t, true)
    36  }
    37  
    38  func TestTlsTcpRecvMax(t *testing.T) {
    39  	test.TranVerifyMaxRecvSize(t, tran, dOpts, lOpts)
    40  }
    41  
    42  func TestTlsTcpOptions(t *testing.T) {
    43  	test.TranVerifyInvalidOption(t, tran)
    44  	test.TranVerifyBoolOption(t, tran, mangos.OptionKeepAlive)
    45  	test.TranVerifyIntOption(t, tran, mangos.OptionMaxRecvSize)
    46  	test.TranVerifyDurationOption(t, tran, mangos.OptionKeepAliveTime)
    47  	test.TranVerifyNoDelayOption(t, tran)
    48  	test.TranVerifyKeepAliveOption(t, tran)
    49  	test.TranVerifyTLSConfigOption(t, tran)
    50  }
    51  
    52  func TestTlsTcpScheme(t *testing.T) {
    53  	test.TranVerifyScheme(t, tran)
    54  }
    55  func TestTlsTcpAcceptWithoutListen(t *testing.T) {
    56  	test.TranVerifyAcceptWithoutListen(t, tran)
    57  }
    58  func TestTcpListenAndAccept(t *testing.T) {
    59  	test.TranVerifyListenAndAccept(t, tran, dOpts, lOpts)
    60  }
    61  func TestTlsTcpDuplicateListen(t *testing.T) {
    62  	test.TranVerifyDuplicateListen(t, tran, lOpts)
    63  }
    64  func TestTlsTcpConnectionRefused(t *testing.T) {
    65  	test.TranVerifyConnectionRefused(t, tran, dOpts)
    66  }
    67  func TestTlsTcpHandshake(t *testing.T) {
    68  	test.TranVerifyHandshakeFail(t, tran, dOpts, lOpts)
    69  }
    70  func TestTlsTcpSendRecv(t *testing.T) {
    71  	test.TranVerifySendRecv(t, tran, dOpts, lOpts)
    72  }
    73  func TestTlsTcpAnonymousPort(t *testing.T) {
    74  	test.TranVerifyAnonymousPort(t, "tls+tcp://127.0.0.1:0", dOpts, lOpts)
    75  }
    76  func TestTlsTcpInvalidDomain(t *testing.T) {
    77  	test.TranVerifyBadAddress(t, "tls+tcp://invalid.invalid", dOpts, lOpts)
    78  }
    79  func TestTlsTcpInvalidLocalIP(t *testing.T) {
    80  	test.TranVerifyBadLocalAddress(t, "tls+tcp://1.1.1.1:80", lOpts)
    81  }
    82  func TestTlsTcpBroadcastIP(t *testing.T) {
    83  	test.TranVerifyBadAddress(t, "tls+tcp://255.255.255.255:80", dOpts, lOpts)
    84  }
    85  
    86  func TestTlsTcpListenerClosed(t *testing.T) {
    87  	test.TranVerifyListenerClosed(t, tran, lOpts)
    88  }
    89  
    90  func TestTlsTcpResolverChange(t *testing.T) {
    91  	sock := test.GetMockSocket()
    92  	defer test.MustClose(t, sock)
    93  
    94  	addr := test.AddrTestTLS()
    95  	test.MustSucceed(t, sock.ListenOptions(addr, lOpts))
    96  
    97  	d, e := tran.NewDialer(addr, sock)
    98  	test.MustSucceed(t, e)
    99  	for key, val := range dOpts {
   100  		test.MustSucceed(t, d.SetOption(key, val))
   101  	}
   102  
   103  	td := d.(*dialer)
   104  	addr = td.addr
   105  	td.addr = "tls+tcp://invalid.invalid:80"
   106  	p, e := d.Dial()
   107  	test.MustFail(t, e)
   108  	test.MustBeTrue(t, p == nil)
   109  
   110  	td.addr = addr
   111  	p, e = d.Dial()
   112  	test.MustSucceed(t, e)
   113  	test.MustSucceed(t, p.Close())
   114  }
   115  
   116  func TestTlsTcpAcceptAbort(t *testing.T) {
   117  	sock := test.GetMockSocket()
   118  	defer test.MustClose(t, sock)
   119  
   120  	addr := test.AddrTestTLS()
   121  	l, e := tran.NewListener(addr, sock)
   122  	test.MustSucceed(t, e)
   123  
   124  	for key, val := range lOpts {
   125  		test.MustSucceed(t, l.SetOption(key, val))
   126  	}
   127  	test.MustSucceed(t, l.Listen())
   128  	_ = l.(*listener).l.Close()
   129  	// This will make the accept loop spin hard, but nothing much
   130  	// we can do about it.
   131  	time.Sleep(time.Millisecond * 50)
   132  }
   133  
   134  func TestTlsListenNoCert(t *testing.T) {
   135  	sock := test.GetMockSocket()
   136  	defer test.MustClose(t, sock)
   137  
   138  	addr := test.AddrTestTLS()
   139  	test.MustBeError(t, sock.ListenOptions(addr, nil), mangos.ErrTLSNoConfig)
   140  
   141  	cfg := &tls.Config{}
   142  	opts := make(map[string]interface{})
   143  	opts[mangos.OptionTLSConfig] = cfg
   144  	test.MustBeError(t, sock.ListenOptions(addr, opts), mangos.ErrTLSNoCert)
   145  }
   146  
   147  func TestTlsDialNoCert(t *testing.T) {
   148  	test.TranVerifyDialNoCert(t, tran)
   149  }
   150  
   151  func TestTlsDialInsecure(t *testing.T) {
   152  	test.TranVerifyDialInsecure(t, tran)
   153  }
   154  
   155  func TestTlsMessageSize(t *testing.T) {
   156  	test.TranVerifyMessageSizes(t, tran, dOpts, lOpts)
   157  }
   158  
   159  func TestTlsMessageHeader(t *testing.T) {
   160  	test.TranVerifyMessageHeader(t, tran, dOpts, lOpts)
   161  }