@@ -8,6 +8,7 @@ package comm
88
99import (
1010 "crypto/tls"
11+ "crypto/x509"
1112 "fmt"
1213 "io/ioutil"
1314 "net"
@@ -17,11 +18,10 @@ import (
1718 "time"
1819
1920 testpb "github.com/hyperledger/fabric/core/comm/testdata/grpc"
20- "github.com/hyperledger/fabric/core/testutil"
21- "github.com/spf13/viper"
2221 "github.com/stretchr/testify/assert"
2322 "golang.org/x/net/context"
2423 "google.golang.org/grpc"
24+ "google.golang.org/grpc/credentials"
2525)
2626
2727const (
@@ -46,42 +46,89 @@ VQQLDAtIeXBlcmxlZGdlcjESMBAGA1UEAwwJbG9jYWxob3N0MFkwEwYHKoZIzj0C
4646-----END CERTIFICATE-----
4747`
4848
49- func TestConnection_Correct (t * testing.T ) {
50- testutil .SetupTestConfig ()
51- viper .Set ("ledger.blockchain.deploy-system-chaincode" , "false" )
52- peerAddress := GetPeerTestingAddress ("7051" )
53- var tmpConn * grpc.ClientConn
54- var err error
55- if TLSEnabled () {
56- tmpConn , err = NewClientConnectionWithAddress (peerAddress , true , true ,
57- InitTLSForPeer (), nil )
58- }
59- tmpConn , err = NewClientConnectionWithAddress (peerAddress , true , false ,
60- nil , nil )
61- if err != nil {
62- t .Fatalf ("error connection to server at host:port = %s\n " , peerAddress )
49+ func TestClientConnections (t * testing.T ) {
50+ t .Parallel ()
51+
52+ testPort := 9050
53+ //use Org1 test crypto material
54+ fileBase := "Org1"
55+ certPEMBlock , _ := ioutil .ReadFile (filepath .Join ("testdata" , "certs" , fileBase + "-server1-cert.pem" ))
56+ keyPEMBlock , _ := ioutil .ReadFile (filepath .Join ("testdata" , "certs" , fileBase + "-server1-key.pem" ))
57+ caPEMBlock , _ := ioutil .ReadFile (filepath .Join ("testdata" , "certs" , fileBase + "-cert.pem" ))
58+ certPool := x509 .NewCertPool ()
59+ certPool .AppendCertsFromPEM (caPEMBlock )
60+
61+ var tests = []struct {
62+ name string
63+ sc ServerConfig
64+ creds credentials.TransportCredentials
65+ clientPort int
66+ fail bool
67+ }{
68+ {
69+ name : "ValidConnection" ,
70+ sc : ServerConfig {
71+ SecOpts : & SecureOptions {
72+ UseTLS : false }},
73+ },
74+ {
75+ name : "InvalidConnection" ,
76+ sc : ServerConfig {
77+ SecOpts : & SecureOptions {
78+ UseTLS : false }},
79+ clientPort : 20040 ,
80+ fail : true ,
81+ },
82+ {
83+ name : "ValidConnectionTLS" ,
84+ sc : ServerConfig {
85+ SecOpts : & SecureOptions {
86+ UseTLS : true ,
87+ ServerCertificate : certPEMBlock ,
88+ ServerKey : keyPEMBlock }},
89+ creds : credentials .NewClientTLSFromCert (certPool , "" ),
90+ },
91+ {
92+ name : "InvalidConnectionTLS" ,
93+ sc : ServerConfig {
94+ SecOpts : & SecureOptions {
95+ UseTLS : true ,
96+ ServerCertificate : certPEMBlock ,
97+ ServerKey : keyPEMBlock }},
98+ creds : credentials .NewClientTLSFromCert (nil , "" ),
99+ fail : true ,
100+ },
63101 }
64102
65- tmpConn .Close ()
66- }
67-
68- func TestConnection_WrongAddress (t * testing.T ) {
69- testutil .SetupTestConfig ()
70- viper .Set ("ledger.blockchain.deploy-system-chaincode" , "false" )
71- //some random port
72- peerAddress := GetPeerTestingAddress ("10287" )
73- var tmpConn * grpc.ClientConn
74- var err error
75- if TLSEnabled () {
76- tmpConn , err = NewClientConnectionWithAddress (peerAddress , true , true ,
77- InitTLSForPeer (), nil )
78- }
79- tmpConn , err = NewClientConnectionWithAddress (peerAddress , true , false ,
80- nil , nil )
81- if err == nil {
82- fmt .Printf ("error connection to server - at host:port = %s\n " , peerAddress )
83- t .Error ("error connection to server - connection should fail" )
84- tmpConn .Close ()
103+ for _ , test := range tests {
104+ test := test
105+ t .Run (test .name , func (t * testing.T ) {
106+ t .Parallel ()
107+ t .Logf ("Running test %s ..." , test .name )
108+ testPort ++
109+ serverAddress := fmt .Sprintf ("localhost:%d" , testPort )
110+ clientAddress := serverAddress
111+ if test .clientPort > 0 {
112+ clientAddress = fmt .Sprintf ("localhost:%d" , test .clientPort )
113+ }
114+ srv , err := NewGRPCServer (serverAddress , test .sc )
115+ //check for error
116+ if err != nil {
117+ t .Fatalf ("Error [%s] creating test server for address [%s]" ,
118+ err , serverAddress )
119+ }
120+ //start the server
121+ go srv .Start ()
122+ defer srv .Stop ()
123+ testConn , err := NewClientConnectionWithAddress (clientAddress ,
124+ true , test .sc .SecOpts .UseTLS , test .creds , nil )
125+ if test .fail {
126+ assert .Error (t , err )
127+ } else {
128+ testConn .Close ()
129+ assert .NoError (t , err )
130+ }
131+ })
85132 }
86133}
87134
0 commit comments