github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/common/config/channel_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 config 18 19 import ( 20 "math" 21 "reflect" 22 "testing" 23 24 "github.com/inklabsfoundation/inkchain/bccsp" 25 "github.com/inklabsfoundation/inkchain/common/util" 26 cb "github.com/inklabsfoundation/inkchain/protos/common" 27 28 logging "github.com/op/go-logging" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func init() { 33 logging.SetLevel(logging.DEBUG, "") 34 } 35 36 func TestInterface(t *testing.T) { 37 _ = Channel(NewChannelGroup(nil)) 38 } 39 40 func TestChannelGroup(t *testing.T) { 41 cg := NewChannelGroup(nil) 42 assert.NotNil(t, cg, "ChannelGroup should not be nil") 43 assert.Nil(t, cg.OrdererConfig(), "OrdererConfig should be nil") 44 assert.Nil(t, cg.ApplicationConfig(), "ApplicationConfig should be nil") 45 assert.Nil(t, cg.ConsortiumsConfig(), "ConsortiumsConfig should be nil") 46 47 _, err := cg.NewGroup(ApplicationGroupKey) 48 assert.NoError(t, err, "Unexpected error for ApplicationGroupKey") 49 _, err = cg.NewGroup(OrdererGroupKey) 50 assert.NoError(t, err, "Unexpected error for OrdererGroupKey") 51 _, err = cg.NewGroup(ConsortiumsGroupKey) 52 assert.NoError(t, err, "Unexpected error for ConsortiumsGroupKey") 53 _, err = cg.NewGroup("BadGroupKey") 54 assert.Error(t, err, "Should have returned error for BadGroupKey") 55 56 } 57 58 func TestChannelConfig(t *testing.T) { 59 cc := NewChannelConfig() 60 assert.NotNil(t, cc, "ChannelConfig should not be nil") 61 62 cc.protos = &ChannelProtos{ 63 HashingAlgorithm: &cb.HashingAlgorithm{Name: bccsp.SHA256}, 64 BlockDataHashingStructure: &cb.BlockDataHashingStructure{Width: math.MaxUint32}, 65 OrdererAddresses: &cb.OrdererAddresses{Addresses: []string{"127.0.0.1:7050"}}, 66 } 67 68 ag := NewApplicationGroup(nil) 69 og := NewOrdererGroup(nil) 70 csg := NewConsortiumsGroup(nil) 71 good := make(map[string]ValueProposer) 72 good[ApplicationGroupKey] = ag 73 good[OrdererGroupKey] = og 74 good[ConsortiumsGroupKey] = csg 75 76 err := cc.Validate(nil, good) 77 assert.NoError(t, err, "Unexpected error validating good config groups") 78 err = cc.Validate(nil, map[string]ValueProposer{ApplicationGroupKey: NewConsortiumsGroup(nil)}) 79 assert.Error(t, err, "Expected error validating bad config group") 80 err = cc.Validate(nil, map[string]ValueProposer{OrdererGroupKey: NewConsortiumsGroup(nil)}) 81 assert.Error(t, err, "Expected error validating bad config group") 82 err = cc.Validate(nil, map[string]ValueProposer{ConsortiumsGroupKey: NewOrdererGroup(nil)}) 83 assert.Error(t, err, "Expected error validating bad config group") 84 err = cc.Validate(nil, map[string]ValueProposer{ConsortiumKey: NewConsortiumGroup(nil)}) 85 assert.Error(t, err, "Expected error validating bad config group") 86 87 } 88 89 func TestHashingAlgorithm(t *testing.T) { 90 cc := &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{}}} 91 assert.Error(t, cc.validateHashingAlgorithm(), "Must supply hashing algorithm") 92 93 cc = &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{Name: "MD5"}}} 94 assert.Error(t, cc.validateHashingAlgorithm(), "Bad hashing algorithm supplied") 95 96 cc = &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{Name: bccsp.SHA256}}} 97 assert.NoError(t, cc.validateHashingAlgorithm(), "Allowed hashing algorith SHA256 supplied") 98 99 assert.Equal(t, reflect.ValueOf(util.ComputeSHA256).Pointer(), reflect.ValueOf(cc.HashingAlgorithm()).Pointer(), 100 "Unexpected hashing algorithm returned") 101 102 cc = &ChannelConfig{protos: &ChannelProtos{HashingAlgorithm: &cb.HashingAlgorithm{Name: bccsp.SHA3_256}}} 103 assert.NoError(t, cc.validateHashingAlgorithm(), "Allowed hashing algorith SHA3_256 supplied") 104 105 assert.Equal(t, reflect.ValueOf(util.ComputeSHA3256).Pointer(), reflect.ValueOf(cc.HashingAlgorithm()).Pointer(), 106 "Unexpected hashing algorithm returned") 107 } 108 109 func TestBlockDataHashingStructure(t *testing.T) { 110 cc := &ChannelConfig{protos: &ChannelProtos{BlockDataHashingStructure: &cb.BlockDataHashingStructure{}}} 111 assert.Error(t, cc.validateBlockDataHashingStructure(), "Must supply block data hashing structure") 112 113 cc = &ChannelConfig{protos: &ChannelProtos{BlockDataHashingStructure: &cb.BlockDataHashingStructure{Width: 7}}} 114 assert.Error(t, cc.validateBlockDataHashingStructure(), "Invalid Merkle tree width supplied") 115 116 var width uint32 117 width = math.MaxUint32 118 cc = &ChannelConfig{protos: &ChannelProtos{BlockDataHashingStructure: &cb.BlockDataHashingStructure{Width: width}}} 119 assert.NoError(t, cc.validateBlockDataHashingStructure(), "Valid Merkle tree width supplied") 120 121 assert.Equal(t, width, cc.BlockDataHashingStructureWidth(), "Unexpected width returned") 122 } 123 124 func TestOrdererAddresses(t *testing.T) { 125 cc := &ChannelConfig{protos: &ChannelProtos{OrdererAddresses: &cb.OrdererAddresses{}}} 126 assert.Error(t, cc.validateOrdererAddresses(), "Must supply orderer addresses") 127 128 cc = &ChannelConfig{protos: &ChannelProtos{OrdererAddresses: &cb.OrdererAddresses{Addresses: []string{"127.0.0.1:7050"}}}} 129 assert.NoError(t, cc.validateOrdererAddresses(), "Invalid orderer address supplied") 130 131 assert.Equal(t, "127.0.0.1:7050", cc.OrdererAddresses()[0], "Unexpected orderer address returned") 132 } 133 134 func TestConsortiumName(t *testing.T) { 135 cc := &ChannelConfig{protos: &ChannelProtos{Consortium: &cb.Consortium{Name: "TestConsortium"}}} 136 assert.Equal(t, "TestConsortium", cc.ConsortiumName(), "Unexpected consortium name returned") 137 } 138 139 func TestChannelUtils(t *testing.T) { 140 // these functions all panic if marshaling fails so just executing them is sufficient 141 _ = TemplateConsortium("test") 142 _ = DefaultHashingAlgorithm() 143 _ = DefaultBlockDataHashingStructure() 144 _ = DefaultOrdererAddresses() 145 146 }