github.com/ewagmig/fabric@v2.1.1+incompatible/common/channelconfig/channel_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channelconfig
     8  
     9  import (
    10  	"math"
    11  	"reflect"
    12  	"testing"
    13  
    14  	cb "github.com/hyperledger/fabric-protos-go/common"
    15  	"github.com/hyperledger/fabric/bccsp"
    16  	"github.com/hyperledger/fabric/bccsp/sw"
    17  	"github.com/hyperledger/fabric/common/util"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func TestInterface(t *testing.T) {
    22  	_ = Channel(&ChannelConfig{})
    23  }
    24  
    25  func TestChannelConfig(t *testing.T) {
    26  	cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore())
    27  	assert.NoError(t, err)
    28  
    29  	cc, err := NewChannelConfig(
    30  		&cb.ConfigGroup{Groups: map[string]*cb.ConfigGroup{"UnknownGroupKey": {}}},
    31  		cryptoProvider,
    32  	)
    33  	assert.Error(t, err)
    34  	assert.Nil(t, cc)
    35  }
    36  
    37  func TestHashingAlgorithm(t *testing.T) {
    38  	cc := &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{}}}
    39  	assert.Error(t, cc.validateHashingAlgorithm(), "Must supply hashing algorithm")
    40  
    41  	cc = &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{Name: "MD5"}}}
    42  	assert.Error(t, cc.validateHashingAlgorithm(), "Bad hashing algorithm supplied")
    43  
    44  	cc = &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{Name: bccsp.SHA256}}}
    45  	assert.NoError(t, cc.validateHashingAlgorithm(), "Allowed hashing algorith SHA256 supplied")
    46  
    47  	assert.Equal(t, reflect.ValueOf(util.ComputeSHA256).Pointer(), reflect.ValueOf(cc.HashingAlgorithm()).Pointer(),
    48  		"Unexpected hashing algorithm returned")
    49  
    50  	cc = &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{Name: bccsp.SHA3_256}}}
    51  	assert.NoError(t, cc.validateHashingAlgorithm(), "Allowed hashing algorith SHA3_256 supplied")
    52  
    53  	assert.Equal(t, reflect.ValueOf(util.ComputeSHA3256).Pointer(), reflect.ValueOf(cc.HashingAlgorithm()).Pointer(),
    54  		"Unexpected hashing algorithm returned")
    55  }
    56  
    57  func TestBlockDataHashingStructure(t *testing.T) {
    58  	cc := &ChannelConfig{protos: &ChannelProtos{BlockDataHashingStructure: &cb.BlockDataHashingStructure{}}}
    59  	assert.Error(t, cc.validateBlockDataHashingStructure(), "Must supply block data hashing structure")
    60  
    61  	cc = &ChannelConfig{protos: &ChannelProtos{BlockDataHashingStructure: &cb.BlockDataHashingStructure{Width: 7}}}
    62  	assert.Error(t, cc.validateBlockDataHashingStructure(), "Invalid Merkle tree width supplied")
    63  
    64  	var width uint32 = math.MaxUint32
    65  	cc = &ChannelConfig{protos: &ChannelProtos{BlockDataHashingStructure: &cb.BlockDataHashingStructure{Width: width}}}
    66  	assert.NoError(t, cc.validateBlockDataHashingStructure(), "Valid Merkle tree width supplied")
    67  
    68  	assert.Equal(t, width, cc.BlockDataHashingStructureWidth(), "Unexpected width returned")
    69  }
    70  
    71  func TestOrdererAddresses(t *testing.T) {
    72  	cc := &ChannelConfig{protos: &ChannelProtos{OrdererAddresses: &cb.OrdererAddresses{}}}
    73  	assert.Error(t, cc.validateOrdererAddresses(), "Must supply orderer addresses")
    74  
    75  	cc = &ChannelConfig{protos: &ChannelProtos{OrdererAddresses: &cb.OrdererAddresses{Addresses: []string{"127.0.0.1:7050"}}}}
    76  	assert.NoError(t, cc.validateOrdererAddresses(), "Invalid orderer address supplied")
    77  
    78  	assert.Equal(t, "127.0.0.1:7050", cc.OrdererAddresses()[0], "Unexpected orderer address returned")
    79  }
    80  
    81  func TestConsortiumName(t *testing.T) {
    82  	cc := &ChannelConfig{protos: &ChannelProtos{Consortium: &cb.Consortium{Name: "TestConsortium"}}}
    83  	assert.Equal(t, "TestConsortium", cc.ConsortiumName(), "Unexpected consortium name returned")
    84  }