github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/configupdate/configupdate_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 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 configupdate 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/hyperledger/fabric/common/configtx" 24 mockcrypto "github.com/hyperledger/fabric/common/mocks/crypto" 25 cb "github.com/hyperledger/fabric/protos/common" 26 "github.com/hyperledger/fabric/protos/utils" 27 28 "github.com/op/go-logging" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func init() { 33 logging.SetLevel(logging.DEBUG, "") 34 } 35 36 type mockSupport struct { 37 ProposeConfigUpdateVal *cb.ConfigEnvelope 38 } 39 40 func (ms *mockSupport) ProposeConfigUpdate(env *cb.Envelope) (*cb.ConfigEnvelope, error) { 41 var err error 42 if ms.ProposeConfigUpdateVal == nil { 43 err = fmt.Errorf("Nil result implies error in mock") 44 } 45 return ms.ProposeConfigUpdateVal, err 46 } 47 48 type mockSupportManager struct { 49 GetChainVal *mockSupport 50 } 51 52 func (msm *mockSupportManager) GetChain(chainID string) (Support, bool) { 53 return msm.GetChainVal, msm.GetChainVal != nil 54 } 55 56 func TestChannelID(t *testing.T) { 57 makeEnvelope := func(payload *cb.Payload) *cb.Envelope { 58 return &cb.Envelope{ 59 Payload: utils.MarshalOrPanic(payload), 60 } 61 } 62 63 testChannelID := "foo" 64 65 result, err := channelID(makeEnvelope(&cb.Payload{ 66 Header: &cb.Header{ 67 ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{ 68 ChannelId: testChannelID, 69 }), 70 }, 71 })) 72 assert.NoError(t, err, "Channel ID was present") 73 assert.Equal(t, testChannelID, result, "Channel ID was present") 74 75 _, err = channelID(makeEnvelope(&cb.Payload{ 76 Header: &cb.Header{ 77 ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{}), 78 }, 79 })) 80 assert.Error(t, err, "Channel ID was empty") 81 82 _, err = channelID(makeEnvelope(&cb.Payload{ 83 Header: &cb.Header{}, 84 })) 85 assert.Error(t, err, "ChannelHeader was missing") 86 87 _, err = channelID(makeEnvelope(&cb.Payload{})) 88 assert.Error(t, err, "Header was missing") 89 90 _, err = channelID(&cb.Envelope{}) 91 assert.Error(t, err, "Payload was missing") 92 } 93 94 const systemChannelID = "system_channel" 95 const testUpdateChannelID = "update_channel" 96 97 func newTestInstance() (*mockSupportManager, *Processor) { 98 msm := &mockSupportManager{} 99 return msm, New(systemChannelID, msm, mockcrypto.FakeLocalSigner) 100 } 101 102 func testConfigUpdate() *cb.Envelope { 103 ch := &cb.ChannelHeader{ 104 ChannelId: testUpdateChannelID, 105 } 106 107 return &cb.Envelope{ 108 Payload: utils.MarshalOrPanic(&cb.Payload{ 109 Header: &cb.Header{ 110 ChannelHeader: utils.MarshalOrPanic(ch), 111 }, 112 Data: utils.MarshalOrPanic(&cb.ConfigUpdateEnvelope{ 113 ConfigUpdate: utils.MarshalOrPanic(&cb.ConfigUpdate{ 114 Header: ch, 115 WriteSet: cb.NewConfigGroup(), 116 }), 117 }), 118 }), 119 } 120 } 121 122 func TestExistingChannel(t *testing.T) { 123 msm, p := newTestInstance() 124 125 testUpdate := testConfigUpdate() 126 127 dummyResult := &cb.ConfigEnvelope{LastUpdate: &cb.Envelope{Payload: []byte("DUMMY")}} 128 129 msm.GetChainVal = &mockSupport{ProposeConfigUpdateVal: dummyResult} 130 env, err := p.Process(testUpdate) 131 assert.NoError(t, err, "Valid config update") 132 _ = utils.UnmarshalPayloadOrPanic(env.Payload) 133 assert.Equal(t, dummyResult, configtx.UnmarshalConfigEnvelopeOrPanic(utils.UnmarshalPayloadOrPanic(env.Payload).Data), "Valid config update") 134 135 msm.GetChainVal = &mockSupport{} 136 _, err = p.Process(testUpdate) 137 assert.Error(t, err, "Invald ProposeUpdate result") 138 } 139 140 func TestNewChannel(t *testing.T) { 141 _, p := newTestInstance() 142 143 testUpdate := testConfigUpdate() 144 145 env, err := p.Process(testUpdate) 146 assert.NoError(t, err, "Valid config update") 147 148 resultChan, err := channelID(env) 149 assert.NoError(t, err, "Invalid envelope produced") 150 151 assert.Equal(t, systemChannelID, resultChan, "Wrapper TX should be bound for system channel") 152 153 chdr, err := utils.UnmarshalChannelHeader(utils.UnmarshalPayloadOrPanic(env.Payload).Header.ChannelHeader) 154 assert.NoError(t, err, "UnmarshalChannelHeader error") 155 156 assert.Equal(t, int32(cb.HeaderType_ORDERER_TRANSACTION), chdr.Type, "Wrong wrapper tx type") 157 }