github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/cmd/common/comm/client_test.go (about)

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