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