Skip to content

Commit 12c609c

Browse files
author
Jason Yellick
committedMay 29, 2018
FAB-10441 Add missing GetConfigTree tests
Discovered during an audit of the ACL unit test coverage, there are no tests at all for the GetConfigTree function of CSCC. This CR adds tests for this function, including ACL tests. Change-Id: I12c7e8154737cd35281585ad13d5c56cbb623a7b Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent e53554d commit 12c609c

File tree

5 files changed

+590
-0
lines changed

5 files changed

+590
-0
lines changed
 

‎core/scc/cscc/configure.go

+7
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ func (e *PeerConfiger) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
110110
return shim.Error(fmt.Sprintf("Failed getting signed proposal from stub: [%s]", err))
111111
}
112112

113+
return e.InvokeNoShim(args, sp)
114+
}
115+
116+
func (e *PeerConfiger) InvokeNoShim(args [][]byte, sp *pb.SignedProposal) pb.Response {
117+
var err error
118+
fname := string(args[0])
119+
113120
switch fname {
114121
case JoinChain:
115122
if args[1] == nil {

‎core/scc/cscc/configure_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"time"
1414

1515
"github.com/golang/protobuf/proto"
16+
"github.com/hyperledger/fabric/common/config"
17+
"github.com/hyperledger/fabric/common/configtx"
1618
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
1719
"github.com/hyperledger/fabric/common/crypto/tlsgen"
1820
"github.com/hyperledger/fabric/common/genesis"
@@ -22,6 +24,7 @@ import (
2224
"github.com/hyperledger/fabric/common/tools/configtxgen/configtxgentest"
2325
"github.com/hyperledger/fabric/common/tools/configtxgen/encoder"
2426
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
27+
"github.com/hyperledger/fabric/core/aclmgmt"
2528
aclmocks "github.com/hyperledger/fabric/core/aclmgmt/mocks"
2629
"github.com/hyperledger/fabric/core/aclmgmt/resources"
2730
"github.com/hyperledger/fabric/core/chaincode"
@@ -37,6 +40,7 @@ import (
3740
"github.com/hyperledger/fabric/core/peer"
3841
"github.com/hyperledger/fabric/core/policy"
3942
policymocks "github.com/hyperledger/fabric/core/policy/mocks"
43+
"github.com/hyperledger/fabric/core/scc/cscc/mock"
4044
"github.com/hyperledger/fabric/gossip/api"
4145
"github.com/hyperledger/fabric/gossip/service"
4246
"github.com/hyperledger/fabric/msp/mgmt"
@@ -51,6 +55,21 @@ import (
5155
"github.com/stretchr/testify/assert"
5256
)
5357

58+
//go:generate counterfeiter -o mock/config_manager.go --fake-name ConfigManager . configManager
59+
type configManager interface {
60+
config.Manager
61+
}
62+
63+
//go:generate counterfeiter -o mock/acl_provider.go --fake-name ACLProvider . aclProvider
64+
type aclProvider interface {
65+
aclmgmt.ACLProvider
66+
}
67+
68+
//go:generate counterfeiter -o mock/configtx_validator.go --fake-name ConfigtxValidator . configtxValidator
69+
type configtxValidator interface {
70+
configtx.Validator
71+
}
72+
5473
type mockDeliveryClient struct {
5574
}
5675

@@ -329,6 +348,61 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
329348
}
330349
}
331350

351+
func TestGetConfigTree(t *testing.T) {
352+
aclProvider := &mock.ACLProvider{}
353+
configMgr := &mock.ConfigManager{}
354+
pc := &PeerConfiger{
355+
aclProvider: aclProvider,
356+
configMgr: configMgr,
357+
}
358+
359+
args := [][]byte{[]byte("GetConfigTree"), []byte("testchan")}
360+
361+
t.Run("Success", func(t *testing.T) {
362+
ctxv := &mock.ConfigtxValidator{}
363+
configMgr.GetChannelConfigReturns(ctxv)
364+
testConfig := &cb.Config{
365+
ChannelGroup: &cb.ConfigGroup{
366+
Values: map[string]*cb.ConfigValue{
367+
"foo": {
368+
Value: []byte("bar"),
369+
},
370+
},
371+
},
372+
}
373+
ctxv.ConfigProtoReturns(testConfig)
374+
res := pc.InvokeNoShim(args, nil)
375+
assert.Equal(t, int32(shim.OK), res.Status)
376+
checkConfig := &pb.ConfigTree{}
377+
err := proto.Unmarshal(res.Payload, checkConfig)
378+
assert.NoError(t, err)
379+
assert.True(t, proto.Equal(testConfig, checkConfig.ChannelConfig))
380+
})
381+
382+
t.Run("MissingConfig", func(t *testing.T) {
383+
ctxv := &mock.ConfigtxValidator{}
384+
configMgr.GetChannelConfigReturns(ctxv)
385+
res := pc.InvokeNoShim(args, nil)
386+
assert.NotEqual(t, int32(shim.OK), res.Status)
387+
assert.Equal(t, "Unknown chain ID, testchan", res.Message)
388+
})
389+
390+
t.Run("NilChannel", func(t *testing.T) {
391+
ctxv := &mock.ConfigtxValidator{}
392+
configMgr.GetChannelConfigReturns(ctxv)
393+
res := pc.InvokeNoShim([][]byte{[]byte("GetConfigTree"), nil}, nil)
394+
assert.NotEqual(t, int32(shim.OK), res.Status)
395+
assert.Equal(t, "Chain ID must not be nil", res.Message)
396+
})
397+
398+
t.Run("BadACL", func(t *testing.T) {
399+
aclProvider.CheckACLReturns(fmt.Errorf("fake-error"))
400+
res := pc.InvokeNoShim(args, nil)
401+
assert.NotEqual(t, int32(shim.OK), res.Status)
402+
assert.Equal(t, "\"GetConfigTree\" request failed authorization check for channel [testchan]: [fake-error]", res.Message)
403+
})
404+
}
405+
332406
func TestPeerConfiger_SubmittingOrdererGenesis(t *testing.T) {
333407
viper.Set("peer.fileSystemPath", "/tmp/hyperledgertest/")
334408
os.Mkdir("/tmp/hyperledgertest", 0755)

‎core/scc/cscc/mock/acl_provider.go

+98
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/scc/cscc/mock/config_manager.go

+96
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/scc/cscc/mock/configtx_validator.go

+315
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.