github.com/Hnampk/fabric@v2.1.1+incompatible/cmd/common/comm/client_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package comm
     8  
     9  import (
    10  	"fmt"
    11  	"io/ioutil"
    12  	"net"
    13  	"os"
    14  	"path/filepath"
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/hyperledger/fabric/internal/pkg/comm"
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestTLSClient(t *testing.T) {
    24  	srv, err := comm.NewGRPCServer("127.0.0.1:", comm.ServerConfig{
    25  		SecOpts: comm.SecureOptions{
    26  			UseTLS:      true,
    27  			Key:         loadFileOrDie(filepath.Join("testdata", "server", "key.pem")),
    28  			Certificate: loadFileOrDie(filepath.Join("testdata", "server", "cert.pem")),
    29  		},
    30  	})
    31  	assert.NoError(t, err)
    32  	go srv.Start()
    33  	defer srv.Stop()
    34  	conf := Config{
    35  		PeerCACertPath: filepath.Join("testdata", "server", "ca.pem"),
    36  	}
    37  	cl, err := NewClient(conf)
    38  	assert.NoError(t, err)
    39  	_, port, _ := net.SplitHostPort(srv.Address())
    40  	dial := cl.NewDialer(net.JoinHostPort("localhost", port))
    41  	conn, err := dial()
    42  	require.NoError(t, err)
    43  	conn.Close()
    44  }
    45  
    46  func TestDialBadEndpoint(t *testing.T) {
    47  	conf := Config{
    48  		PeerCACertPath: filepath.Join("testdata", "server", "ca.pem"),
    49  		Timeout:        100 * time.Millisecond,
    50  	}
    51  	cl, err := NewClient(conf)
    52  	assert.NoError(t, err)
    53  	dial := cl.NewDialer("non_existent_host.xyz.blabla:9999")
    54  	_, err = dial()
    55  	assert.Error(t, err)
    56  }
    57  
    58  func TestNonTLSClient(t *testing.T) {
    59  	srv, err := comm.NewGRPCServer("127.0.0.1:", comm.ServerConfig{
    60  		SecOpts: comm.SecureOptions{},
    61  	})
    62  	assert.NoError(t, err)
    63  	go srv.Start()
    64  	defer srv.Stop()
    65  	conf := Config{}
    66  	cl, err := NewClient(conf)
    67  	assert.NoError(t, err)
    68  	_, port, _ := net.SplitHostPort(srv.Address())
    69  	dial := cl.NewDialer(net.JoinHostPort("127.0.0.1", port))
    70  	conn, err := dial()
    71  	require.NoError(t, err)
    72  	conn.Close()
    73  }
    74  
    75  func TestClientBadConfig(t *testing.T) {
    76  	conf := Config{
    77  		PeerCACertPath: filepath.Join("testdata", "server", "non_existent_file"),
    78  	}
    79  	cl, err := NewClient(conf)
    80  	assert.Nil(t, cl)
    81  	assert.Contains(t, err.Error(), "open testdata/server/non_existent_file: no such file or directory")
    82  
    83  	conf = Config{
    84  		PeerCACertPath: filepath.Join("testdata", "server", "ca.pem"),
    85  		KeyPath:        "non_existent_file",
    86  		CertPath:       "non_existent_file",
    87  	}
    88  	cl, err = NewClient(conf)
    89  	assert.Nil(t, cl)
    90  	assert.Contains(t, err.Error(), "open non_existent_file: no such file or directory")
    91  
    92  	conf = Config{
    93  		PeerCACertPath: filepath.Join("testdata", "server", "ca.pem"),
    94  		KeyPath:        filepath.Join("testdata", "client", "key.pem"),
    95  		CertPath:       "non_existent_file",
    96  	}
    97  	cl, err = NewClient(conf)
    98  	assert.Nil(t, cl)
    99  	assert.Contains(t, err.Error(), "open non_existent_file: no such file or directory")
   100  }
   101  
   102  func loadFileOrDie(path string) []byte {
   103  	b, err := ioutil.ReadFile(path)
   104  	if err != nil {
   105  		fmt.Println("Failed opening file", path, ":", err)
   106  		os.Exit(1)
   107  	}
   108  	return b
   109  }