Skip to content

Commit

Permalink
FAB-16437 Remove common/mocks/policies
Browse files Browse the repository at this point in the history
Change-Id: Id08ba203c662f43132980ede390d22fd473def33
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Nov 12, 2019
1 parent f876f2f commit f62a2c0
Show file tree
Hide file tree
Showing 15 changed files with 657 additions and 143 deletions.
62 changes: 0 additions & 62 deletions common/mocks/policies/policies.go

This file was deleted.

31 changes: 0 additions & 31 deletions common/mocks/policies/policies_test.go

This file was deleted.

11 changes: 11 additions & 0 deletions core/chaincode/chaincode_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hyperledger/fabric/common/channelconfig"
commonledger "github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/core/chaincode"
"github.com/hyperledger/fabric/core/common/privdata"
"github.com/hyperledger/fabric/core/container/ccintf"
Expand Down Expand Up @@ -135,3 +136,13 @@ type applicationCapabilities interface {
type applicationConfig interface {
channelconfig.Application
}

//go:generate counterfeiter -o mock/policy_manager.go -fake-name PolicyManager . policyManager
type policyManager interface {
policies.Manager
}

//go:generate counterfeiter -o mock/policy.go -fake-name Policy . policy
type policy interface {
policies.Policy
}
2 changes: 1 addition & 1 deletion core/chaincode/chaincode_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func initMockPeer(channelIDs ...string) (*peer.Peer, *ChaincodeSupport, func(),

globalBlockNum = make(map[string]uint64, len(channelIDs))
for _, id := range channelIDs {
if err := peer.CreateMockChannel(peerInstance, id); err != nil {
if err := peer.CreateMockChannel(peerInstance, id, &mock.PolicyManager{}); err != nil {
cleanup()
return nil, nil, func() {}, err
}
Expand Down
78 changes: 45 additions & 33 deletions core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/hyperledger/fabric/common/crypto/tlsgen"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/metrics/disabled"
mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"

"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/aclmgmt"
Expand All @@ -57,7 +57,7 @@ import (
cut "github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/core/policy"
"github.com/hyperledger/fabric/core/policy/mocks"
policymocks "github.com/hyperledger/fabric/core/policy/mocks"
"github.com/hyperledger/fabric/core/scc"
"github.com/hyperledger/fabric/core/scc/lscc"
"github.com/hyperledger/fabric/internal/peer/packaging"
Expand Down Expand Up @@ -211,17 +211,6 @@ func initPeer(channelIDs ...string) (*cm.Lifecycle, net.Listener, *ChaincodeSupp

scc.DeploySysCC(lsccImpl, chaincodeSupport)

for _, id := range channelIDs {
if err = peer.CreateMockChannel(peerInstance, id); err != nil {
closeListenerAndSleep(lis)
return nil, nil, nil, nil, err
}
// any channel other than the default testchannelid does not have a MSP set up -> create one
if id != "testchannelid" {
mspmgmt.XXXSetMSPManager(id, mspmgmt.GetManagerForChain("testchannelid"))
}
}

go grpcServer.Serve(lis)

cleanup := func() {
Expand Down Expand Up @@ -622,16 +611,33 @@ const (

// Test the execution of a chaincode that invokes another chaincode.
func TestChaincodeInvokeChaincode(t *testing.T) {
channel := "testchannelid"
channel2 := channel + "2"
ml, lis, chaincodeSupport, cleanup, err := initPeer(channel, channel2)
channel1 := "testchannelid"
channel2 := channel1 + "2"
ml, lis, chaincodeSupport, cleanup, err := initPeer(channel1, channel2)
if err != nil {
t.Fail()
t.Logf("Error creating peer: %s", err)
}
defer cleanup()
defer closeListenerAndSleep(lis)

mockPolicy := &mock.Policy{}
mockPolicy.EvaluateSignedDataReturns(nil)

polMgrChannel1 := &mock.PolicyManager{}
polMgrChannel1.GetPolicyReturns(mockPolicy, true)
err = peer.CreateMockChannel(chaincodeSupport.Peer, channel1, polMgrChannel1)
if err != nil {
t.Fatalf("Failed to create mock channel: %s", err)
}

polMgrChannel2 := &mock.PolicyManager{}
polMgrChannel2.GetPolicyReturns(mockPolicy, true)
err = peer.CreateMockChannel(chaincodeSupport.Peer, channel2, polMgrChannel2)
if err != nil {
t.Fatalf("Failed to create mock channel: %s", err)
}

peerInstance := chaincodeSupport.Peer

var nextBlockNumber1 uint64 = 1
Expand Down Expand Up @@ -661,7 +667,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
pb.ChaincodeSpec_GOLANG,
chaincodeExample02GolangPath,
util.ToChaincodeArgs("init", "a", strconv.Itoa(initialA), "b", strconv.Itoa(initialB)),
channel,
channel1,
nextBlockNumber1,
chaincodeSupport,
)
Expand All @@ -682,7 +688,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
chaincode2Type,
chaincode2Path,
util.ToChaincodeArgs("init"),
channel,
channel1,
nextBlockNumber1,
chaincodeSupport,
)
Expand All @@ -703,27 +709,21 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
Args: util.ToChaincodeArgs(ccContext1.Name, "invoke", "a", "b", "10", ""),
},
}
_, _, _, err = invoke(channel, chaincode2InvokeSpec, nextBlockNumber1, []byte("Alice"), chaincodeSupport)
_, _, _, err = invoke(channel1, chaincode2InvokeSpec, nextBlockNumber1, []byte("Alice"), chaincodeSupport)
require.NoErrorf(t, err, "error invoking %s: %s", chaincode2Name, err)
nextBlockNumber1++

// Check the state in the ledger
err = checkFinalState(peerInstance, channel, ccContext1, initialA-10, initialB+10)
err = checkFinalState(peerInstance, channel1, ccContext1, initialA-10, initialB+10)
require.NoErrorf(t, err, "incorrect final state after transaction for %s: %s", chaincode1Name, err)

// Change the policies of the two channels in such a way:
// 1. Alice has reader access to both the channels.
// 2. Bob has access only to chainID2.
// Therefore the chaincode invocation should fail.
pm := peerInstance.GetPolicyManager(channel)
pm.(*mockpolicies.Manager).PolicyMap = map[string]policies.Policy{
policies.ChannelApplicationWriters: &CreatorPolicy{Creators: [][]byte{[]byte("Alice")}},
}

pm = peerInstance.GetPolicyManager(channel2)
pm.(*mockpolicies.Manager).PolicyMap = map[string]policies.Policy{
policies.ChannelApplicationWriters: &CreatorPolicy{Creators: [][]byte{[]byte("Alice"), []byte("Bob")}},
}
polMgrChannel1.GetPolicyReturns(&CreatorPolicy{Creators: [][]byte{[]byte("Alice")}}, true)
polMgrChannel2.GetPolicyReturns(&CreatorPolicy{Creators: [][]byte{[]byte("Alice"), []byte("Bob")}}, true)

// deploy chaincode2 on channel2
_, ccContext3, err := deployChaincode(
Expand All @@ -748,18 +748,18 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
Version: chaincode2Version,
},
Input: &pb.ChaincodeInput{
Args: util.ToChaincodeArgs(ccContext1.Name, "invoke", "a", "b", "10", channel),
Args: util.ToChaincodeArgs(ccContext1.Name, "invoke", "a", "b", "10", channel1),
},
}

// as Bob, invoke chaincode2 on channel2 so that it invokes chaincode1 on channel
_, _, _, err = invoke(channel2, chaincode2InvokeSpec, nextBlockNumber2, []byte("Bob"), chaincodeSupport)
require.Errorf(t, err, "as Bob, invoking <%s/%s> via <%s/%s> should fail, but it succeeded.", ccContext1.Name, channel, chaincode2Name, channel2)
require.Errorf(t, err, "as Bob, invoking <%s/%s> via <%s/%s> should fail, but it succeeded.", ccContext1.Name, channel1, chaincode2Name, channel2)
assert.True(t, strings.Contains(err.Error(), "[Creator not recognized [Bob]]"))

// as Alice, invoke chaincode2 on channel2 so that it invokes chaincode1 on channel
_, _, _, err = invoke(channel2, chaincode2InvokeSpec, nextBlockNumber2, []byte("Alice"), chaincodeSupport)
require.NoError(t, err, "as Alice, invoking <%s/%s> via <%s/%s> should should of succeeded, but it failed: %s", ccContext1.Name, channel, chaincode2Name, channel2, err)
require.NoError(t, err, "as Alice, invoking <%s/%s> via <%s/%s> should should of succeeded, but it failed: %s", ccContext1.Name, channel1, chaincode2Name, channel2, err)
nextBlockNumber2++
}

Expand All @@ -779,6 +779,14 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) {
}
defer cleanup()

polMgr := &mock.PolicyManager{}
mockPolicy := &mock.Policy{}
mockPolicy.EvaluateIdentitiesReturns(nil)
mockPolicy.EvaluateSignedDataReturns(nil)
polMgr.GetPolicyReturns(mockPolicy, true)

peer.CreateMockChannel(chaincodeSupport.Peer, channelID, polMgr)

ml.ChaincodeEndorsementInfoStub = func(_, name string, _ ledger.SimpleQueryExecutor) (*lifecycle.ChaincodeEndorsementInfo, error) {
switch name {
case "lscc":
Expand Down Expand Up @@ -874,6 +882,8 @@ func TestChaincodeInit(t *testing.T) {

defer cleanup()

peer.CreateMockChannel(chaincodeSupport.Peer, channelID, nil)

url := "github.com/hyperledger/fabric/core/chaincode/testdata/src/chaincodes/init_private_data"
cID := &pb.ChaincodeID{Name: "init_pvtdata", Path: url, Version: "0"}

Expand Down Expand Up @@ -928,6 +938,8 @@ func TestQueries(t *testing.T) {

defer cleanup()

peer.CreateMockChannel(chaincodeSupport.Peer, channelID, nil)

url := "github.com/hyperledger/fabric/core/chaincode/testdata/src/chaincodes/map"
cID := &pb.ChaincodeID{Name: "tmap", Path: url, Version: "0"}

Expand Down Expand Up @@ -1271,10 +1283,10 @@ func (c *CreatorPolicy) EvaluateIdentities(identities []msp.Identity) error {
func newPolicyChecker(peerInstance *peer.Peer) policy.PolicyChecker {
return policy.NewPolicyChecker(
policies.PolicyManagerGetterFunc(peerInstance.GetPolicyManager),
&mocks.MockIdentityDeserializer{
&policymocks.MockIdentityDeserializer{
Identity: []byte("Admin"),
Msg: []byte("msg1"),
},
&mocks.MockMSPPrincipalGetter{Principal: []byte("Admin")},
&policymocks.MockMSPPrincipalGetter{Principal: []byte("Admin")},
)
}
3 changes: 3 additions & 0 deletions core/chaincode/executetransaction_pvtdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hyperledger/fabric-protos-go/common"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/peer"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
Expand All @@ -35,6 +36,8 @@ func TestQueriesPrivateData(t *testing.T) {

defer cleanup()

peer.CreateMockChannel(chaincodeSupport.Peer, channelID, nil)

url := "github.com/hyperledger/fabric/core/chaincode/testdata/src/chaincodes/map"
cID := &pb.ChaincodeID{Name: "tmap", Path: url, Version: "0"}

Expand Down
2 changes: 0 additions & 2 deletions core/chaincode/mock/collection_store.go

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

Loading

0 comments on commit f62a2c0

Please sign in to comment.