Skip to content

Commit 467a2f1

Browse files
committed
[FAB-3890] Increase coverage for common/config
Coverage for common/config and common/config/msp is over 90%. Also removed a few dead code paths as well. Change-Id: Ia74527bf6d052be27083990c3779827e2844eb01 Signed-off-by: Gari Singh <[email protected]>
1 parent 65ee7b2 commit 467a2f1

9 files changed

+341
-15
lines changed

common/config/channel_test.go

+83-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package config
1818

1919
import (
2020
"math"
21+
"reflect"
2122
"testing"
2223

2324
"github.com/hyperledger/fabric/bccsp"
25+
"github.com/hyperledger/fabric/common/util"
2426
cb "github.com/hyperledger/fabric/protos/common"
2527

2628
logging "github.com/op/go-logging"
@@ -35,6 +37,55 @@ func TestInterface(t *testing.T) {
3537
_ = Channel(NewChannelGroup(nil))
3638
}
3739

40+
func TestChannelGroup(t *testing.T) {
41+
cg := NewChannelGroup(nil)
42+
assert.NotNil(t, cg, "ChannelGroup should not be nil")
43+
assert.Nil(t, cg.OrdererConfig(), "OrdererConfig should be nil")
44+
assert.Nil(t, cg.ApplicationConfig(), "ApplicationConfig should be nil")
45+
assert.Nil(t, cg.ConsortiumsConfig(), "ConsortiumsConfig should be nil")
46+
47+
_, err := cg.NewGroup(ApplicationGroupKey)
48+
assert.NoError(t, err, "Unexpected error for ApplicationGroupKey")
49+
_, err = cg.NewGroup(OrdererGroupKey)
50+
assert.NoError(t, err, "Unexpected error for OrdererGroupKey")
51+
_, err = cg.NewGroup(ConsortiumsGroupKey)
52+
assert.NoError(t, err, "Unexpected error for ConsortiumsGroupKey")
53+
_, err = cg.NewGroup("BadGroupKey")
54+
assert.Error(t, err, "Should have returned error for BadGroupKey")
55+
56+
}
57+
58+
func TestChannelConfig(t *testing.T) {
59+
cc := NewChannelConfig()
60+
assert.NotNil(t, cc, "ChannelConfig should not be nil")
61+
62+
cc.protos = &ChannelProtos{
63+
HashingAlgorithm: &cb.HashingAlgorithm{Name: bccsp.SHA256},
64+
BlockDataHashingStructure: &cb.BlockDataHashingStructure{Width: math.MaxUint32},
65+
OrdererAddresses: &cb.OrdererAddresses{Addresses: []string{"127.0.0.1:7050"}},
66+
}
67+
68+
ag := NewApplicationGroup(nil)
69+
og := NewOrdererGroup(nil)
70+
csg := NewConsortiumsGroup()
71+
good := make(map[string]ValueProposer)
72+
good[ApplicationGroupKey] = ag
73+
good[OrdererGroupKey] = og
74+
good[ConsortiumsGroupKey] = csg
75+
76+
err := cc.Validate(nil, good)
77+
assert.NoError(t, err, "Unexpected error validating good config groups")
78+
err = cc.Validate(nil, map[string]ValueProposer{ApplicationGroupKey: NewConsortiumsGroup()})
79+
assert.Error(t, err, "Expected error validating bad config group")
80+
err = cc.Validate(nil, map[string]ValueProposer{OrdererGroupKey: NewConsortiumsGroup()})
81+
assert.Error(t, err, "Expected error validating bad config group")
82+
err = cc.Validate(nil, map[string]ValueProposer{ConsortiumsGroupKey: NewOrdererGroup(nil)})
83+
assert.Error(t, err, "Expected error validating bad config group")
84+
err = cc.Validate(nil, map[string]ValueProposer{ConsortiumKey: NewConsortiumGroup(nil)})
85+
assert.Error(t, err, "Expected error validating bad config group")
86+
87+
}
88+
3889
func TestHashingAlgorithm(t *testing.T) {
3990
cc := &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{}}}
4091
assert.Error(t, cc.validateHashingAlgorithm(), "Must supply hashing algorithm")
@@ -43,7 +94,16 @@ func TestHashingAlgorithm(t *testing.T) {
4394
assert.Error(t, cc.validateHashingAlgorithm(), "Bad hashing algorithm supplied")
4495

4596
cc = &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{Name: bccsp.SHA256}}}
46-
assert.NoError(t, cc.validateHashingAlgorithm(), "Allowed hashing algorith supplied")
97+
assert.NoError(t, cc.validateHashingAlgorithm(), "Allowed hashing algorith SHA256 supplied")
98+
99+
assert.Equal(t, reflect.ValueOf(util.ComputeSHA256).Pointer(), reflect.ValueOf(cc.HashingAlgorithm()).Pointer(),
100+
"Unexpected hashing algorithm returned")
101+
102+
cc = &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{Name: bccsp.SHA3_256}}}
103+
assert.NoError(t, cc.validateHashingAlgorithm(), "Allowed hashing algorith SHA3_256 supplied")
104+
105+
assert.Equal(t, reflect.ValueOf(util.ComputeSHA3256).Pointer(), reflect.ValueOf(cc.HashingAlgorithm()).Pointer(),
106+
"Unexpected hashing algorithm returned")
47107
}
48108

49109
func TestBlockDataHashingStructure(t *testing.T) {
@@ -53,14 +113,34 @@ func TestBlockDataHashingStructure(t *testing.T) {
53113
cc = &ChannelConfig{protos: &ChannelProtos{BlockDataHashingStructure: &cb.BlockDataHashingStructure{Width: 7}}}
54114
assert.Error(t, cc.validateBlockDataHashingStructure(), "Invalid Merkle tree width supplied")
55115

56-
cc = &ChannelConfig{protos: &ChannelProtos{BlockDataHashingStructure: &cb.BlockDataHashingStructure{Width: math.MaxUint32}}}
116+
var width uint32
117+
width = math.MaxUint32
118+
cc = &ChannelConfig{protos: &ChannelProtos{BlockDataHashingStructure: &cb.BlockDataHashingStructure{Width: width}}}
57119
assert.NoError(t, cc.validateBlockDataHashingStructure(), "Valid Merkle tree width supplied")
120+
121+
assert.Equal(t, width, cc.BlockDataHashingStructureWidth(), "Unexpected width returned")
58122
}
59123

60124
func TestOrdererAddresses(t *testing.T) {
61125
cc := &ChannelConfig{protos: &ChannelProtos{OrdererAddresses: &cb.OrdererAddresses{}}}
62126
assert.Error(t, cc.validateOrdererAddresses(), "Must supply orderer addresses")
63127

64128
cc = &ChannelConfig{protos: &ChannelProtos{OrdererAddresses: &cb.OrdererAddresses{Addresses: []string{"127.0.0.1:7050"}}}}
65-
assert.NoError(t, cc.validateOrdererAddresses(), "Invalid Merkle tree width supplied")
129+
assert.NoError(t, cc.validateOrdererAddresses(), "Invalid orderer address supplied")
130+
131+
assert.Equal(t, "127.0.0.1:7050", cc.OrdererAddresses()[0], "Unexpected orderer address returned")
132+
}
133+
134+
func TestConsortiumName(t *testing.T) {
135+
cc := &ChannelConfig{protos: &ChannelProtos{Consortium: &cb.Consortium{Name: "TestConsortium"}}}
136+
assert.Equal(t, "TestConsortium", cc.ConsortiumName(), "Unexpected consortium name returned")
137+
}
138+
139+
func TestChannelUtils(t *testing.T) {
140+
// these functions all panic if marshaling fails so just executing them is sufficient
141+
_ = TemplateConsortium("test")
142+
_ = DefaultHashingAlgorithm()
143+
_ = DefaultBlockDataHashingStructure()
144+
_ = DefaultOrdererAddresses()
145+
66146
}

common/config/consortium_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package config
17+
18+
import (
19+
"testing"
20+
21+
cb "github.com/hyperledger/fabric/protos/common"
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestConsortiumGroup(t *testing.T) {
26+
27+
cg := NewConsortiumGroup(nil)
28+
og, err := cg.NewGroup("testGroup")
29+
assert.NoError(t, err, "NewGroup should not have returned error")
30+
assert.Equal(t, "testGroup", og.(*OrganizationGroup).Name(),
31+
"Unexpected group name returned")
32+
33+
cc := cg.Allocate()
34+
_, ok := cc.(*ConsortiumConfig)
35+
assert.Equal(t, true, ok, "Allocate should have returned a ConsortiumConfig")
36+
37+
_, _, err = cg.BeginValueProposals(t, []string{"testGroup"})
38+
assert.NoError(t, err, "BeginValueProposals should not have returned error")
39+
40+
err = cg.PreCommit(t)
41+
assert.NoError(t, err, "PreCommit should not have returned error")
42+
cg.CommitProposals(t)
43+
cg.RollbackProposals(t)
44+
45+
}
46+
47+
func TestConsortiumConfig(t *testing.T) {
48+
cg := NewConsortiumGroup(nil)
49+
cc := NewConsortiumConfig(cg)
50+
orgs := cc.Organizations()
51+
assert.Equal(t, 0, len(orgs))
52+
53+
policy := cc.ChannelCreationPolicy()
54+
assert.EqualValues(t, cb.Policy_UNKNOWN, policy.Type, "Expected policy type to be UNKNOWN")
55+
56+
cc.Commit()
57+
assert.Equal(t, cg.ConsortiumConfig, cc, "Error committing ConsortiumConfig")
58+
59+
og, _ := cg.NewGroup("testGroup")
60+
err := cc.Validate(t, map[string]ValueProposer{
61+
"testGroup": og,
62+
})
63+
assert.NoError(t, err, "Validate returned unexpected error")
64+
csg := NewConsortiumsGroup()
65+
err = cc.Validate(t, map[string]ValueProposer{
66+
"testGroup": csg,
67+
})
68+
assert.Error(t, err, "Validate should have failed")
69+
70+
}

common/config/consortiums_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package config
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestConsortiums(t *testing.T) {
25+
26+
csg := NewConsortiumsGroup()
27+
cg, err := csg.NewGroup("testCG")
28+
assert.NoError(t, err, "NewGroup shuld not have returned error")
29+
_, ok := cg.(*ConsortiumGroup)
30+
assert.Equal(t, true, ok, "NewGroup should have returned a ConsortiumGroup")
31+
32+
csc := csg.Allocate()
33+
_, ok = csc.(*ConsortiumsConfig)
34+
assert.Equal(t, true, ok, "Allocate should have returned a ConsortiumsConfig")
35+
36+
csc.Commit()
37+
assert.Equal(t, csc, csg.ConsortiumsConfig, "Failed to commit ConsortiumsConfig")
38+
39+
err = csc.Validate(t, map[string]ValueProposer{
40+
"badGroup": &OrganizationGroup{},
41+
})
42+
assert.Error(t, err, "Validate should have returned error for wrong type of ValueProposer")
43+
err = csc.Validate(t, map[string]ValueProposer{
44+
"testGroup": cg,
45+
})
46+
assert.NoError(t, err, "Validate should not have returned error")
47+
assert.Equal(t, cg, csc.(*ConsortiumsConfig).Consortiums()["testGroup"],
48+
"Expected consortium groups to be the same")
49+
50+
}

common/config/msp/config.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ func (bh *MSPConfigHandler) ProposeMSP(tx interface{}, mspConfig *mspprotos.MSPC
108108
}
109109

110110
// add the MSP to the map of pending MSPs
111-
mspID, err := mspInst.GetIdentifier()
112-
if err != nil {
113-
return nil, fmt.Errorf("Could not extract msp identifier, err %s", err)
114-
}
111+
mspID, _ := mspInst.GetIdentifier()
115112

116113
existingPendingMSPConfig, ok := pendingConfig.idMap[mspID]
117114
if ok && !reflect.DeepEqual(existingPendingMSPConfig.mspConfig, mspConfig) {

common/config/msp/config_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ package msp
1919
import (
2020
"testing"
2121

22+
"github.com/hyperledger/fabric/common/cauthdsl"
2223
"github.com/hyperledger/fabric/core/config"
2324
"github.com/hyperledger/fabric/msp"
25+
cb "github.com/hyperledger/fabric/protos/common"
2426
mspprotos "github.com/hyperledger/fabric/protos/msp"
27+
"github.com/hyperledger/fabric/protos/utils"
2528
"github.com/stretchr/testify/assert"
2629
)
2730

@@ -35,6 +38,17 @@ func TestMSPConfigManager(t *testing.T) {
3538

3639
// begin/propose/commit
3740
mspCH := NewMSPConfigHandler()
41+
42+
assert.Panics(t, func() {
43+
mspCH.PreCommit(t)
44+
}, "Expected panic calling PreCommit before beginning transaction")
45+
assert.Panics(t, func() {
46+
mspCH.CommitProposals(t)
47+
}, "Expected panic calling CommitProposals before beginning transaction")
48+
assert.Panics(t, func() {
49+
_, err = mspCH.ProposeMSP(t, conf)
50+
}, "Expected panic calling ProposeMSP before beginning transaction")
51+
3852
mspCH.BeginConfig(t)
3953
_, err = mspCH.ProposeMSP(t, conf)
4054
assert.NoError(t, err)
@@ -48,11 +62,49 @@ func TestMSPConfigManager(t *testing.T) {
4862
t.Fatalf("There are no MSPS in the manager")
4963
}
5064

65+
mspCH.BeginConfig(t)
66+
_, err = mspCH.ProposeMSP(t, conf)
67+
mspCH.RollbackProposals(t)
68+
5169
// test failure
5270
// begin/propose/commit
5371
mspCH.BeginConfig(t)
5472
_, err = mspCH.ProposeMSP(t, conf)
5573
assert.NoError(t, err)
5674
_, err = mspCH.ProposeMSP(t, &mspprotos.MSPConfig{Config: []byte("BARF!")})
5775
assert.Error(t, err)
76+
_, err = mspCH.ProposeMSP(t, &mspprotos.MSPConfig{Type: int32(10)})
77+
assert.Panics(t, func() {
78+
mspCH.BeginConfig(t)
79+
}, "Expected panic calling BeginConfig multiple times for same transaction")
80+
}
81+
82+
func TestTemplates(t *testing.T) {
83+
mspDir, err := config.GetDevMspDir()
84+
assert.NoError(t, err)
85+
mspConf, err := msp.GetLocalMspConfig(mspDir, nil, "DEFAULT")
86+
assert.NoError(t, err)
87+
88+
expectedMSPValue := &cb.ConfigValue{
89+
Value: utils.MarshalOrPanic(mspConf),
90+
}
91+
configGroup := TemplateGroupMSP([]string{"TestPath"}, mspConf)
92+
testGroup, ok := configGroup.Groups["TestPath"]
93+
assert.Equal(t, true, ok, "Failed to find group key")
94+
assert.Equal(t, expectedMSPValue, testGroup.Values[MSPKey], "MSPKey did not match expected value")
95+
96+
configGroup = TemplateGroupMSPWithAdminRolePrincipal([]string{"TestPath"}, mspConf, false)
97+
expectedPolicyValue := utils.MarshalOrPanic(cauthdsl.SignedByMspMember("DEFAULT"))
98+
actualPolicyValue := configGroup.Groups["TestPath"].Policies[AdminsPolicyKey].Policy.Policy
99+
assert.Equal(t, expectedPolicyValue, actualPolicyValue, "Expected SignedByMspMemberPolicy")
100+
101+
mspConf = &mspprotos.MSPConfig{}
102+
assert.Panics(t, func() {
103+
configGroup = TemplateGroupMSPWithAdminRolePrincipal([]string{"TestPath"}, mspConf, false)
104+
}, "Expected panic with bad msp config")
105+
mspConf.Type = int32(10)
106+
assert.Panics(t, func() {
107+
configGroup = TemplateGroupMSPWithAdminRolePrincipal([]string{"TestPath"}, mspConf, false)
108+
}, "Expected panic with bad msp config")
109+
58110
}

common/config/msp/config_util.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ func TemplateGroupMSPWithAdminRolePrincipal(configPath []string, mspConfig *mspp
6363
}
6464

6565
// add the MSP to the map of pending MSPs
66-
mspID, err := mspInst.GetIdentifier()
67-
if err != nil {
68-
logger.Panicf("Could not extract msp identifier, err %s", err)
69-
}
66+
mspID, _ := mspInst.GetIdentifier()
7067

7168
memberPolicy := &cb.ConfigPolicy{
7269
Policy: &cb.Policy{

common/config/organization.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@ func (oc *OrganizationConfig) validateMSP(tx interface{}) error {
115115
return err
116116
}
117117

118-
oc.mspID, err = oc.msp.GetIdentifier()
119-
if err != nil {
120-
return fmt.Errorf("Could not extract msp identifier for org %s: %s", oc.organizationGroup.name, err)
121-
}
118+
oc.mspID, _ = oc.msp.GetIdentifier()
122119

123120
if oc.mspID == "" {
124121
return fmt.Errorf("MSP for org %s has empty MSP ID", oc.organizationGroup.name)

0 commit comments

Comments
 (0)