github.com/anjalikarhana/fabric@v2.1.1+incompatible/orderer/common/msgprocessor/standardchannel_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package msgprocessor 8 9 import ( 10 "fmt" 11 "testing" 12 13 cb "github.com/hyperledger/fabric-protos-go/common" 14 "github.com/hyperledger/fabric-protos-go/orderer" 15 "github.com/hyperledger/fabric/bccsp/sw" 16 "github.com/hyperledger/fabric/common/channelconfig" 17 "github.com/hyperledger/fabric/internal/pkg/identity" 18 "github.com/hyperledger/fabric/orderer/common/msgprocessor/mocks" 19 "github.com/hyperledger/fabric/protoutil" 20 "github.com/stretchr/testify/assert" 21 ) 22 23 const testChannelID = "foo" 24 25 type mockSystemChannelFilterSupport struct { 26 ProposeConfigUpdateVal *cb.ConfigEnvelope 27 ProposeConfigUpdateErr error 28 SequenceVal uint64 29 OrdererConfigVal channelconfig.Orderer 30 } 31 32 func (ms *mockSystemChannelFilterSupport) ProposeConfigUpdate(env *cb.Envelope) (*cb.ConfigEnvelope, error) { 33 return ms.ProposeConfigUpdateVal, ms.ProposeConfigUpdateErr 34 } 35 36 func (ms *mockSystemChannelFilterSupport) Sequence() uint64 { 37 return ms.SequenceVal 38 } 39 40 func (ms *mockSystemChannelFilterSupport) Signer() identity.SignerSerializer { 41 return nil 42 } 43 44 func (ms *mockSystemChannelFilterSupport) ChannelID() string { 45 return testChannelID 46 } 47 48 func (ms *mockSystemChannelFilterSupport) OrdererConfig() (channelconfig.Orderer, bool) { 49 if ms.OrdererConfigVal == nil { 50 return nil, false 51 } 52 53 return ms.OrdererConfigVal, true 54 } 55 56 func TestClassifyMsg(t *testing.T) { 57 t.Run("ConfigUpdate", func(t *testing.T) { 58 class := (&StandardChannel{}).ClassifyMsg(&cb.ChannelHeader{Type: int32(cb.HeaderType_CONFIG_UPDATE)}) 59 assert.Equal(t, class, ConfigUpdateMsg) 60 }) 61 t.Run("OrdererTx", func(t *testing.T) { 62 class := (&StandardChannel{}).ClassifyMsg(&cb.ChannelHeader{Type: int32(cb.HeaderType_ORDERER_TRANSACTION)}) 63 assert.Equal(t, class, ConfigMsg) 64 }) 65 t.Run("ConfigTx", func(t *testing.T) { 66 class := (&StandardChannel{}).ClassifyMsg(&cb.ChannelHeader{Type: int32(cb.HeaderType_CONFIG)}) 67 assert.Equal(t, class, ConfigMsg) 68 }) 69 t.Run("EndorserTx", func(t *testing.T) { 70 class := (&StandardChannel{}).ClassifyMsg(&cb.ChannelHeader{Type: int32(cb.HeaderType_ENDORSER_TRANSACTION)}) 71 assert.Equal(t, class, NormalMsg) 72 }) 73 } 74 75 func TestProcessNormalMsg(t *testing.T) { 76 t.Run("Normal", func(t *testing.T) { 77 ms := &mockSystemChannelFilterSupport{ 78 SequenceVal: 7, 79 OrdererConfigVal: newMockOrdererConfig(true, orderer.ConsensusType_STATE_NORMAL), 80 } 81 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 82 assert.NoError(t, err) 83 cs, err := NewStandardChannel(ms, NewRuleSet([]Rule{AcceptRule}), cryptoProvider).ProcessNormalMsg(nil) 84 assert.Equal(t, cs, ms.SequenceVal) 85 assert.Nil(t, err) 86 }) 87 t.Run("Maintenance", func(t *testing.T) { 88 ms := &mockSystemChannelFilterSupport{ 89 SequenceVal: 7, 90 OrdererConfigVal: newMockOrdererConfig(true, orderer.ConsensusType_STATE_MAINTENANCE), 91 } 92 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 93 assert.NoError(t, err) 94 _, err = NewStandardChannel(ms, NewRuleSet([]Rule{AcceptRule}), cryptoProvider).ProcessNormalMsg(nil) 95 assert.EqualError(t, err, "normal transactions are rejected: maintenance mode") 96 }) 97 } 98 99 func TestConfigUpdateMsg(t *testing.T) { 100 t.Run("BadUpdate", func(t *testing.T) { 101 ms := &mockSystemChannelFilterSupport{ 102 ProposeConfigUpdateVal: &cb.ConfigEnvelope{}, 103 ProposeConfigUpdateErr: fmt.Errorf("An error"), 104 OrdererConfigVal: &mocks.OrdererConfig{}, 105 } 106 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 107 assert.NoError(t, err) 108 config, cs, err := NewStandardChannel(ms, NewRuleSet(nil), cryptoProvider).ProcessConfigUpdateMsg(&cb.Envelope{}) 109 assert.Nil(t, config) 110 assert.Equal(t, uint64(0), cs) 111 assert.EqualError(t, err, "error applying config update to existing channel 'foo': An error") 112 }) 113 t.Run("BadMsg", func(t *testing.T) { 114 ms := &mockSystemChannelFilterSupport{ 115 ProposeConfigUpdateVal: &cb.ConfigEnvelope{}, 116 ProposeConfigUpdateErr: fmt.Errorf("An error"), 117 OrdererConfigVal: &mocks.OrdererConfig{}, 118 } 119 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 120 assert.NoError(t, err) 121 config, cs, err := NewStandardChannel(ms, NewRuleSet([]Rule{EmptyRejectRule}), cryptoProvider).ProcessConfigUpdateMsg(&cb.Envelope{}) 122 assert.Nil(t, config) 123 assert.Equal(t, uint64(0), cs) 124 assert.NotNil(t, err) 125 }) 126 t.Run("SignedEnvelopeFailure", func(t *testing.T) { 127 ms := &mockSystemChannelFilterSupport{ 128 OrdererConfigVal: &mocks.OrdererConfig{}, 129 } 130 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 131 assert.NoError(t, err) 132 config, cs, err := NewStandardChannel(ms, NewRuleSet([]Rule{AcceptRule}), cryptoProvider).ProcessConfigUpdateMsg(nil) 133 assert.Nil(t, config) 134 assert.Equal(t, uint64(0), cs) 135 assert.NotNil(t, err) 136 assert.Regexp(t, "Marshal called with nil", err) 137 }) 138 t.Run("Success", func(t *testing.T) { 139 ms := &mockSystemChannelFilterSupport{ 140 SequenceVal: 7, 141 ProposeConfigUpdateVal: &cb.ConfigEnvelope{}, 142 OrdererConfigVal: newMockOrdererConfig(true, orderer.ConsensusType_STATE_NORMAL), 143 } 144 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 145 assert.NoError(t, err) 146 stdChan := NewStandardChannel(ms, NewRuleSet([]Rule{AcceptRule}), cryptoProvider) 147 stdChan.maintenanceFilter = AcceptRule 148 config, cs, err := stdChan.ProcessConfigUpdateMsg(nil) 149 assert.NotNil(t, config) 150 assert.Equal(t, cs, ms.SequenceVal) 151 assert.Nil(t, err) 152 }) 153 } 154 155 func TestProcessConfigMsg(t *testing.T) { 156 t.Run("WrongType", func(t *testing.T) { 157 ms := &mockSystemChannelFilterSupport{ 158 SequenceVal: 7, 159 ProposeConfigUpdateVal: &cb.ConfigEnvelope{}, 160 OrdererConfigVal: &mocks.OrdererConfig{}, 161 } 162 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 163 assert.NoError(t, err) 164 _, _, err = NewStandardChannel(ms, NewRuleSet([]Rule{AcceptRule}), cryptoProvider).ProcessConfigMsg(&cb.Envelope{ 165 Payload: protoutil.MarshalOrPanic(&cb.Payload{ 166 Header: &cb.Header{ 167 ChannelHeader: protoutil.MarshalOrPanic(&cb.ChannelHeader{ 168 ChannelId: testChannelID, 169 Type: int32(cb.HeaderType_ORDERER_TRANSACTION), 170 }), 171 }, 172 }), 173 }) 174 assert.Error(t, err) 175 }) 176 177 t.Run("Success", func(t *testing.T) { 178 ms := &mockSystemChannelFilterSupport{ 179 SequenceVal: 7, 180 ProposeConfigUpdateVal: &cb.ConfigEnvelope{}, 181 OrdererConfigVal: newMockOrdererConfig(true, orderer.ConsensusType_STATE_NORMAL), 182 } 183 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 184 assert.NoError(t, err) 185 stdChan := NewStandardChannel(ms, NewRuleSet([]Rule{AcceptRule}), cryptoProvider) 186 stdChan.maintenanceFilter = AcceptRule 187 config, cs, err := stdChan.ProcessConfigMsg(&cb.Envelope{ 188 Payload: protoutil.MarshalOrPanic(&cb.Payload{ 189 Header: &cb.Header{ 190 ChannelHeader: protoutil.MarshalOrPanic(&cb.ChannelHeader{ 191 ChannelId: testChannelID, 192 Type: int32(cb.HeaderType_CONFIG), 193 }), 194 }, 195 }), 196 }) 197 assert.NotNil(t, config) 198 assert.Equal(t, cs, ms.SequenceVal) 199 assert.Nil(t, err) 200 hdr, err := protoutil.ChannelHeader(config) 201 assert.Equal( 202 t, 203 int32(cb.HeaderType_CONFIG), 204 hdr.Type, 205 "Expect type of returned envelope to be %d, but got %d", cb.HeaderType_CONFIG, hdr.Type) 206 }) 207 }