Skip to content

Commit

Permalink
FAB-10441 Add missing GetConfigTree tests
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jason Yellick committed May 29, 2018
1 parent e53554d commit 12c609c
Show file tree
Hide file tree
Showing 5 changed files with 590 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/scc/cscc/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ func (e *PeerConfiger) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Error(fmt.Sprintf("Failed getting signed proposal from stub: [%s]", err))
}

return e.InvokeNoShim(args, sp)
}

func (e *PeerConfiger) InvokeNoShim(args [][]byte, sp *pb.SignedProposal) pb.Response {
var err error
fname := string(args[0])

switch fname {
case JoinChain:
if args[1] == nil {
Expand Down
74 changes: 74 additions & 0 deletions core/scc/cscc/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"time"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/config"
"github.com/hyperledger/fabric/common/configtx"
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/crypto/tlsgen"
"github.com/hyperledger/fabric/common/genesis"
Expand All @@ -22,6 +24,7 @@ import (
"github.com/hyperledger/fabric/common/tools/configtxgen/configtxgentest"
"github.com/hyperledger/fabric/common/tools/configtxgen/encoder"
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
"github.com/hyperledger/fabric/core/aclmgmt"
aclmocks "github.com/hyperledger/fabric/core/aclmgmt/mocks"
"github.com/hyperledger/fabric/core/aclmgmt/resources"
"github.com/hyperledger/fabric/core/chaincode"
Expand All @@ -37,6 +40,7 @@ import (
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/core/policy"
policymocks "github.com/hyperledger/fabric/core/policy/mocks"
"github.com/hyperledger/fabric/core/scc/cscc/mock"
"github.com/hyperledger/fabric/gossip/api"
"github.com/hyperledger/fabric/gossip/service"
"github.com/hyperledger/fabric/msp/mgmt"
Expand All @@ -51,6 +55,21 @@ import (
"github.com/stretchr/testify/assert"
)

//go:generate counterfeiter -o mock/config_manager.go --fake-name ConfigManager . configManager
type configManager interface {
config.Manager
}

//go:generate counterfeiter -o mock/acl_provider.go --fake-name ACLProvider . aclProvider
type aclProvider interface {
aclmgmt.ACLProvider
}

//go:generate counterfeiter -o mock/configtx_validator.go --fake-name ConfigtxValidator . configtxValidator
type configtxValidator interface {
configtx.Validator
}

type mockDeliveryClient struct {
}

Expand Down Expand Up @@ -329,6 +348,61 @@ func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
}
}

func TestGetConfigTree(t *testing.T) {
aclProvider := &mock.ACLProvider{}
configMgr := &mock.ConfigManager{}
pc := &PeerConfiger{
aclProvider: aclProvider,
configMgr: configMgr,
}

args := [][]byte{[]byte("GetConfigTree"), []byte("testchan")}

t.Run("Success", func(t *testing.T) {
ctxv := &mock.ConfigtxValidator{}
configMgr.GetChannelConfigReturns(ctxv)
testConfig := &cb.Config{
ChannelGroup: &cb.ConfigGroup{
Values: map[string]*cb.ConfigValue{
"foo": {
Value: []byte("bar"),
},
},
},
}
ctxv.ConfigProtoReturns(testConfig)
res := pc.InvokeNoShim(args, nil)
assert.Equal(t, int32(shim.OK), res.Status)
checkConfig := &pb.ConfigTree{}
err := proto.Unmarshal(res.Payload, checkConfig)
assert.NoError(t, err)
assert.True(t, proto.Equal(testConfig, checkConfig.ChannelConfig))
})

t.Run("MissingConfig", func(t *testing.T) {
ctxv := &mock.ConfigtxValidator{}
configMgr.GetChannelConfigReturns(ctxv)
res := pc.InvokeNoShim(args, nil)
assert.NotEqual(t, int32(shim.OK), res.Status)
assert.Equal(t, "Unknown chain ID, testchan", res.Message)
})

t.Run("NilChannel", func(t *testing.T) {
ctxv := &mock.ConfigtxValidator{}
configMgr.GetChannelConfigReturns(ctxv)
res := pc.InvokeNoShim([][]byte{[]byte("GetConfigTree"), nil}, nil)
assert.NotEqual(t, int32(shim.OK), res.Status)
assert.Equal(t, "Chain ID must not be nil", res.Message)
})

t.Run("BadACL", func(t *testing.T) {
aclProvider.CheckACLReturns(fmt.Errorf("fake-error"))
res := pc.InvokeNoShim(args, nil)
assert.NotEqual(t, int32(shim.OK), res.Status)
assert.Equal(t, "\"GetConfigTree\" request failed authorization check for channel [testchan]: [fake-error]", res.Message)
})
}

func TestPeerConfiger_SubmittingOrdererGenesis(t *testing.T) {
viper.Set("peer.fileSystemPath", "/tmp/hyperledgertest/")
os.Mkdir("/tmp/hyperledgertest", 0755)
Expand Down
98 changes: 98 additions & 0 deletions core/scc/cscc/mock/acl_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 96 additions & 0 deletions core/scc/cscc/mock/config_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 12c609c

Please sign in to comment.