github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/multichain/systemchain_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 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 17 package multichain 18 19 import ( 20 "testing" 21 22 "github.com/hyperledger/fabric/common/configtx" 23 "github.com/hyperledger/fabric/common/configtx/tool/provisional" 24 configvaluesapi "github.com/hyperledger/fabric/common/configvalues" 25 mockconfigvaluesorderer "github.com/hyperledger/fabric/common/mocks/configvalues/channel/orderer" 26 mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" 27 "github.com/hyperledger/fabric/common/policies" 28 "github.com/hyperledger/fabric/orderer/common/filter" 29 cb "github.com/hyperledger/fabric/protos/common" 30 31 "github.com/stretchr/testify/assert" 32 ) 33 34 type mockSupport struct { 35 mpm *mockpolicies.Manager 36 msc *mockconfigvaluesorderer.SharedConfig 37 } 38 39 func newMockSupport(chainID string) *mockSupport { 40 return &mockSupport{ 41 mpm: &mockpolicies.Manager{}, 42 msc: &mockconfigvaluesorderer.SharedConfig{}, 43 } 44 } 45 46 func (ms *mockSupport) PolicyManager() policies.Manager { 47 return ms.mpm 48 } 49 50 func (ms *mockSupport) SharedConfig() configvaluesapi.Orderer { 51 return ms.msc 52 } 53 54 type mockChainCreator struct { 55 newChains []*cb.Envelope 56 ms *mockSupport 57 } 58 59 func newMockChainCreator() *mockChainCreator { 60 mcc := &mockChainCreator{ 61 ms: newMockSupport(provisional.TestChainID), 62 } 63 return mcc 64 } 65 66 func (mcc *mockChainCreator) newChain(configTx *cb.Envelope) { 67 mcc.newChains = append(mcc.newChains, configTx) 68 } 69 70 func TestGoodProposal(t *testing.T) { 71 newChainID := "NewChainID" 72 73 mcc := newMockChainCreator() 74 mcc.ms.msc.ChainCreationPolicyNamesVal = []string{provisional.AcceptAllPolicyKey} 75 mcc.ms.mpm.Policy = &mockpolicies.Policy{} 76 77 configEnv, err := configtx.NewChainCreationTemplate(provisional.AcceptAllPolicyKey, configtx.NewCompositeTemplate()).Envelope(newChainID) 78 if err != nil { 79 t.Fatalf("Error constructing configtx") 80 } 81 ingressTx := makeConfigTxFromConfigUpdateEnvelope(newChainID, configEnv) 82 wrapped := wrapConfigTx(ingressTx) 83 84 sysFilter := newSystemChainFilter(mcc.ms, mcc) 85 action, committer := sysFilter.Apply(wrapped) 86 87 assert.EqualValues(t, action, filter.Accept, "Did not accept valid transaction") 88 assert.True(t, committer.Isolated(), "Channel creation belong in its own block") 89 90 committer.Commit() 91 assert.Len(t, mcc.newChains, 1, "Proposal should only have created 1 new chain") 92 93 assert.Equal(t, ingressTx, mcc.newChains[0], "New chain should have been created with ingressTx") 94 } 95 96 func TestProposalWithBadPolicy(t *testing.T) { 97 newChainID := "NewChainID" 98 99 mcc := newMockChainCreator() 100 mcc.ms.mpm.Policy = &mockpolicies.Policy{} 101 102 configEnv, err := configtx.NewChainCreationTemplate(provisional.AcceptAllPolicyKey, configtx.NewCompositeTemplate()).Envelope(newChainID) 103 if err != nil { 104 t.Fatalf("Error constructing configtx") 105 } 106 ingressTx := makeConfigTxFromConfigUpdateEnvelope(newChainID, configEnv) 107 wrapped := wrapConfigTx(ingressTx) 108 109 sysFilter := newSystemChainFilter(mcc.ms, mcc) 110 action, _ := sysFilter.Apply(wrapped) 111 112 assert.EqualValues(t, action, filter.Reject, "Transaction creation policy was not authorized") 113 } 114 115 func TestProposalWithMissingPolicy(t *testing.T) { 116 newChainID := "NewChainID" 117 118 mcc := newMockChainCreator() 119 mcc.ms.msc.ChainCreationPolicyNamesVal = []string{provisional.AcceptAllPolicyKey} 120 121 configEnv, err := configtx.NewChainCreationTemplate(provisional.AcceptAllPolicyKey, configtx.NewCompositeTemplate()).Envelope(newChainID) 122 if err != nil { 123 t.Fatalf("Error constructing configtx") 124 } 125 ingressTx := makeConfigTxFromConfigUpdateEnvelope(newChainID, configEnv) 126 wrapped := wrapConfigTx(ingressTx) 127 128 sysFilter := newSystemChainFilter(mcc.ms, mcc) 129 action, _ := sysFilter.Apply(wrapped) 130 131 assert.EqualValues(t, filter.Reject, action, "Transaction had missing policy") 132 }