trpc.group/trpc-go/trpc-go@v1.0.3/transport/tnet/client_transport_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  //go:build linux || freebsd || dragonfly || darwin
    15  // +build linux freebsd dragonfly darwin
    16  
    17  package tnet_test
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	"trpc.group/trpc-go/trpc-go/errs"
    27  	"trpc.group/trpc-go/trpc-go/pool/connpool"
    28  	tnettrans "trpc.group/trpc-go/trpc-go/transport/tnet"
    29  )
    30  
    31  func TestDial(t *testing.T) {
    32  	l, err := net.Listen("tcp", "localhost:0")
    33  	assert.Nil(t, err)
    34  	defer l.Close()
    35  
    36  	tests := []struct {
    37  		name    string
    38  		opts    *connpool.DialOptions
    39  		want    net.Conn
    40  		wantErr assert.ErrorAssertionFunc
    41  	}{
    42  		{
    43  			name: "empty CACertFile and Network",
    44  			opts: &connpool.DialOptions{
    45  				CACertFile: "",
    46  				Network:    "",
    47  			},
    48  			want: nil,
    49  			wantErr: func(t assert.TestingT, err error, msg ...interface{}) bool {
    50  				return assert.Contains(t, err.Error(), "unknown network")
    51  			},
    52  		},
    53  		{
    54  			name: "wrong CACertFile and TLSServerName ",
    55  			opts: &connpool.DialOptions{
    56  				CACertFile:    "xxx",
    57  				TLSServerName: "xxx",
    58  			},
    59  			want: nil,
    60  			wantErr: func(t assert.TestingT, err error, msg ...interface{}) bool {
    61  				return assert.Equal(t, errs.RetClientDecodeFail, errs.Code(err)) &&
    62  					assert.Contains(t, err.Error(), "client dial tnet tls fail")
    63  			},
    64  		},
    65  	}
    66  	for _, tt := range tests {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			got, err := tnettrans.Dial(tt.opts)
    69  			assert.True(t, tt.wantErr(t, err, fmt.Sprintf("Dial(%v)", tt.opts)))
    70  			assert.Equalf(t, tt.want, got, "Dial(%v)", tt.opts)
    71  		})
    72  	}
    73  }