Skip to content

Commit f057c66

Browse files
committed
[FAB-3139] Increase test coverage for core/comm
Test coverage now over 80%. Some more work to do but this was a good point to commit Change-Id: Iaeaafcbc2cbe7c6cd980272571ac5a0eb481510d Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
1 parent c3009e6 commit f057c66

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

core/comm/connection_test.go

+91
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,39 @@ package comm
1818

1919
import (
2020
"fmt"
21+
"io/ioutil"
22+
"path/filepath"
2123
"testing"
2224

2325
"github.com/spf13/viper"
2426

2527
"github.com/hyperledger/fabric/core/config"
28+
"github.com/stretchr/testify/assert"
2629
"google.golang.org/grpc"
2730
)
2831

32+
const (
33+
numOrgs = 2
34+
numChildOrgs = 2
35+
)
36+
37+
//string for cert filenames
38+
var (
39+
orgCACert = filepath.Join("testdata", "certs", "Org%d-cert.pem")
40+
childCACert = filepath.Join("testdata", "certs", "Org%d-child%d-cert.pem")
41+
)
42+
43+
var badPEM = `-----BEGIN CERTIFICATE-----
44+
MIICRDCCAemgAwIBAgIJALwW//dz2ZBvMAoGCCqGSM49BAMCMH4xCzAJBgNVBAYT
45+
AlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1TYW4gRnJhbmNpc2Nv
46+
MRgwFgYDVQQKDA9MaW51eEZvdW5kYXRpb24xFDASBgNVBAsMC0h5cGVybGVkZ2Vy
47+
MRIwEAYDVQQDDAlsb2NhbGhvc3QwHhcNMTYxMjA0MjIzMDE4WhcNMjYxMjAyMjIz
48+
MDE4WjB+MQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQGA1UE
49+
BwwNU2FuIEZyYW5jaXNjbzEYMBYGA1UECgwPTGludXhGb3VuZGF0aW9uMRQwEgYD
50+
VQQLDAtIeXBlcmxlZGdlcjESMBAGA1UEAwwJbG9jYWxob3N0MFkwEwYHKoZIzj0C
51+
-----END CERTIFICATE-----
52+
`
53+
2954
func TestConnection_Correct(t *testing.T) {
3055
config.SetupTestConfig("./../../peer")
3156
viper.Set("ledger.blockchain.deploy-system-chaincode", "false")
@@ -59,3 +84,69 @@ func TestConnection_WrongAddress(t *testing.T) {
5984
tmpConn.Close()
6085
}
6186
}
87+
88+
// utility function to load up our test root certificates from testdata/certs
89+
func loadRootCAs() [][]byte {
90+
91+
rootCAs := [][]byte{}
92+
for i := 1; i <= numOrgs; i++ {
93+
root, err := ioutil.ReadFile(fmt.Sprintf(orgCACert, i))
94+
if err != nil {
95+
return [][]byte{}
96+
}
97+
rootCAs = append(rootCAs, root)
98+
for j := 1; j <= numChildOrgs; j++ {
99+
root, err := ioutil.ReadFile(fmt.Sprintf(childCACert, i, j))
100+
if err != nil {
101+
return [][]byte{}
102+
}
103+
rootCAs = append(rootCAs, root)
104+
}
105+
}
106+
return rootCAs
107+
}
108+
109+
func TestCASupport(t *testing.T) {
110+
111+
rootCAs := loadRootCAs()
112+
t.Logf("loaded %d root certificates", len(rootCAs))
113+
if len(rootCAs) != 6 {
114+
t.Fatalf("failed to load root certificates")
115+
}
116+
117+
cas := GetCASupport()
118+
cas.AppRootCAsByChain["channel1"] = [][]byte{rootCAs[0]}
119+
cas.AppRootCAsByChain["channel2"] = [][]byte{rootCAs[1]}
120+
cas.OrdererRootCAsByChain["channel1"] = [][]byte{(rootCAs[2])}
121+
cas.OrdererRootCAsByChain["channel2"] = [][]byte{rootCAs[3]}
122+
cas.ServerRootCAs = [][]byte{rootCAs[4]}
123+
cas.ClientRootCAs = [][]byte{rootCAs[4], rootCAs[5]}
124+
125+
appServerRoots, ordererServerRoots := cas.GetServerRootCAs()
126+
t.Logf("%d appServerRoots | %d ordererServerRoots", len(appServerRoots),
127+
len(ordererServerRoots))
128+
assert.Equal(t, 3, len(appServerRoots), "Expected 3 app server root CAs")
129+
assert.Equal(t, 3, len(ordererServerRoots), "Expected 3 orderer server root CAs")
130+
131+
appClientRoots, ordererClientRoots := cas.GetClientRootCAs()
132+
t.Logf("%d appClientRoots | %d ordererClientRoots", len(appClientRoots),
133+
len(ordererClientRoots))
134+
assert.Equal(t, 4, len(appClientRoots), "Expected 4 app server root CAs")
135+
assert.Equal(t, 4, len(ordererClientRoots), "Expected 4 orderer server root CAs")
136+
137+
// make sure we really have a singleton
138+
casClone := GetCASupport()
139+
assert.Exactly(t, casClone, cas, "Expected GetCASupport to be a singleton")
140+
141+
creds := cas.GetDeliverServiceCredentials()
142+
assert.Equal(t, "1.2", creds.Info().SecurityVersion,
143+
"Expected Security version to be 1.2")
144+
145+
// append some bad certs and make sure things still work
146+
cas.ServerRootCAs = append(cas.ServerRootCAs, []byte("badcert"))
147+
cas.ServerRootCAs = append(cas.ServerRootCAs, []byte(badPEM))
148+
creds = cas.GetDeliverServiceCredentials()
149+
assert.Equal(t, "1.2", creds.Info().SecurityVersion,
150+
"Expected Security version to be 1.2")
151+
152+
}

0 commit comments

Comments
 (0)