github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/common/channelconfig/channel_test.go (about) 1 /* 2 Copyright hechain. 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 "github.com/hechain20/hechain/bccsp" 15 "github.com/hechain20/hechain/bccsp/sw" 16 "github.com/hechain20/hechain/common/util" 17 cb "github.com/hyperledger/fabric-protos-go/common" 18 "github.com/stretchr/testify/require" 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 require.NoError(t, err) 28 29 cc, err := NewChannelConfig( 30 &cb.ConfigGroup{Groups: map[string]*cb.ConfigGroup{"UnknownGroupKey": {}}}, 31 cryptoProvider, 32 ) 33 require.Error(t, err) 34 require.Nil(t, cc) 35 } 36 37 func TestHashingAlgorithm(t *testing.T) { 38 cc := &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{}}} 39 require.Error(t, cc.validateHashingAlgorithm(), "Must supply hashing algorithm") 40 41 cc = &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{Name: "MD5"}}} 42 require.Error(t, cc.validateHashingAlgorithm(), "Bad hashing algorithm supplied") 43 44 cc = &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{Name: bccsp.SHA256}}} 45 require.NoError(t, cc.validateHashingAlgorithm(), "Allowed hashing algorithm SHA256 supplied") 46 47 require.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 require.NoError(t, cc.validateHashingAlgorithm(), "Allowed hashing algorithm SHA3_256 supplied") 52 53 require.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 require.Error(t, cc.validateBlockDataHashingStructure(), "Must supply block data hashing structure") 60 61 cc = &ChannelConfig{protos: &ChannelProtos{BlockDataHashingStructure: &cb.BlockDataHashingStructure{Width: 7}}} 62 require.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 require.NoError(t, cc.validateBlockDataHashingStructure(), "Valid Merkle tree width supplied") 67 68 require.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 require.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 require.NoError(t, cc.validateOrdererAddresses(), "Invalid orderer address supplied") 77 78 require.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 require.Equal(t, "TestConsortium", cc.ConsortiumName(), "Unexpected consortium name returned") 84 }