Skip to content

Commit

Permalink
[FAB-7099] Fix core/comm tests
Browse files Browse the repository at this point in the history
Prior to this change, running
go test for the core/comm package
required the TestEnv.  With this
change you can now simply run
go test locally.

Change-Id: If75456c85330cdda30aa6f11c449c2585f6b6110
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Dec 1, 2017
1 parent 8e8a8d8 commit a205bf3
Showing 1 changed file with 83 additions and 36 deletions.
119 changes: 83 additions & 36 deletions core/comm/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package comm

import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
Expand All @@ -17,11 +18,10 @@ import (
"time"

testpb "github.com/hyperledger/fabric/core/comm/testdata/grpc"
"github.com/hyperledger/fabric/core/testutil"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

const (
Expand All @@ -46,42 +46,89 @@ VQQLDAtIeXBlcmxlZGdlcjESMBAGA1UEAwwJbG9jYWxob3N0MFkwEwYHKoZIzj0C
-----END CERTIFICATE-----
`

func TestConnection_Correct(t *testing.T) {
testutil.SetupTestConfig()
viper.Set("ledger.blockchain.deploy-system-chaincode", "false")
peerAddress := GetPeerTestingAddress("7051")
var tmpConn *grpc.ClientConn
var err error
if TLSEnabled() {
tmpConn, err = NewClientConnectionWithAddress(peerAddress, true, true,
InitTLSForPeer(), nil)
}
tmpConn, err = NewClientConnectionWithAddress(peerAddress, true, false,
nil, nil)
if err != nil {
t.Fatalf("error connection to server at host:port = %s\n", peerAddress)
func TestClientConnections(t *testing.T) {
t.Parallel()

testPort := 9050
//use Org1 test crypto material
fileBase := "Org1"
certPEMBlock, _ := ioutil.ReadFile(filepath.Join("testdata", "certs", fileBase+"-server1-cert.pem"))
keyPEMBlock, _ := ioutil.ReadFile(filepath.Join("testdata", "certs", fileBase+"-server1-key.pem"))
caPEMBlock, _ := ioutil.ReadFile(filepath.Join("testdata", "certs", fileBase+"-cert.pem"))
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(caPEMBlock)

var tests = []struct {
name string
sc ServerConfig
creds credentials.TransportCredentials
clientPort int
fail bool
}{
{
name: "ValidConnection",
sc: ServerConfig{
SecOpts: &SecureOptions{
UseTLS: false}},
},
{
name: "InvalidConnection",
sc: ServerConfig{
SecOpts: &SecureOptions{
UseTLS: false}},
clientPort: 20040,
fail: true,
},
{
name: "ValidConnectionTLS",
sc: ServerConfig{
SecOpts: &SecureOptions{
UseTLS: true,
ServerCertificate: certPEMBlock,
ServerKey: keyPEMBlock}},
creds: credentials.NewClientTLSFromCert(certPool, ""),
},
{
name: "InvalidConnectionTLS",
sc: ServerConfig{
SecOpts: &SecureOptions{
UseTLS: true,
ServerCertificate: certPEMBlock,
ServerKey: keyPEMBlock}},
creds: credentials.NewClientTLSFromCert(nil, ""),
fail: true,
},
}

tmpConn.Close()
}

func TestConnection_WrongAddress(t *testing.T) {
testutil.SetupTestConfig()
viper.Set("ledger.blockchain.deploy-system-chaincode", "false")
//some random port
peerAddress := GetPeerTestingAddress("10287")
var tmpConn *grpc.ClientConn
var err error
if TLSEnabled() {
tmpConn, err = NewClientConnectionWithAddress(peerAddress, true, true,
InitTLSForPeer(), nil)
}
tmpConn, err = NewClientConnectionWithAddress(peerAddress, true, false,
nil, nil)
if err == nil {
fmt.Printf("error connection to server - at host:port = %s\n", peerAddress)
t.Error("error connection to server - connection should fail")
tmpConn.Close()
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
t.Logf("Running test %s ...", test.name)
testPort++
serverAddress := fmt.Sprintf("localhost:%d", testPort)
clientAddress := serverAddress
if test.clientPort > 0 {
clientAddress = fmt.Sprintf("localhost:%d", test.clientPort)
}
srv, err := NewGRPCServer(serverAddress, test.sc)
//check for error
if err != nil {
t.Fatalf("Error [%s] creating test server for address [%s]",
err, serverAddress)
}
//start the server
go srv.Start()
defer srv.Stop()
testConn, err := NewClientConnectionWithAddress(clientAddress,
true, test.sc.SecOpts.UseTLS, test.creds, nil)
if test.fail {
assert.Error(t, err)
} else {
testConn.Close()
assert.NoError(t, err)
}
})
}
}

Expand Down

0 comments on commit a205bf3

Please sign in to comment.