github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/protos/utils/blockutils_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 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 // This package provides unit tests for blocks 18 package utils_test 19 20 import ( 21 "testing" 22 23 "github.com/golang/protobuf/proto" 24 configtxtest "github.com/hyperledger/fabric/common/configtx/test" 25 "github.com/hyperledger/fabric/protos/common" 26 cb "github.com/hyperledger/fabric/protos/common" 27 "github.com/hyperledger/fabric/protos/utils" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 var testChainID = "myuniquetestchainid" 32 33 func TestGetChainIDFromBlockBytes(t *testing.T) { 34 gb, err := configtxtest.MakeGenesisBlock(testChainID) 35 assert.NoError(t, err, "Failed to create test configuration block") 36 bytes, err := proto.Marshal(gb) 37 cid, err := utils.GetChainIDFromBlockBytes(bytes) 38 assert.NoError(t, err) 39 assert.Equal(t, testChainID, cid, "Failed to return expected chain ID") 40 41 // bad block bytes 42 _, err = utils.GetChainIDFromBlockBytes([]byte("bad block")) 43 assert.Error(t, err, "Expected error with malformed block bytes") 44 } 45 46 func TestGetChainIDFromBlock(t *testing.T) { 47 var err error 48 var gb *common.Block 49 var cid string 50 51 // nil block 52 _, err = utils.GetChainIDFromBlock(gb) 53 assert.Error(t, err, "Expected error getting channel id from nil block") 54 55 gb, err = configtxtest.MakeGenesisBlock(testChainID) 56 assert.NoError(t, err, "Failed to create test configuration block") 57 58 cid, err = utils.GetChainIDFromBlock(gb) 59 assert.NoError(t, err, "Failed to get chain ID from block") 60 assert.Equal(t, testChainID, cid, "Failed to return expected chain ID") 61 62 // missing data 63 badBlock := gb 64 badBlock.Data = nil 65 _, err = utils.GetChainIDFromBlock(badBlock) 66 assert.Error(t, err, "Expected error with missing block data") 67 68 // no envelope 69 badBlock = &cb.Block{ 70 Data: &cb.BlockData{ 71 Data: [][]byte{[]byte("bad envelope")}, 72 }, 73 } 74 _, err = utils.GetChainIDFromBlock(badBlock) 75 assert.Error(t, err, "Expected error with no envelope in data") 76 77 // bad payload 78 env, _ := proto.Marshal(&cb.Envelope{ 79 Payload: []byte("bad payload"), 80 }) 81 badBlock = &cb.Block{ 82 Data: &cb.BlockData{ 83 Data: [][]byte{env}, 84 }, 85 } 86 _, err = utils.GetChainIDFromBlock(badBlock) 87 assert.Error(t, err, "Expected error - malformed payload") 88 89 // bad channel header 90 payload, _ := proto.Marshal(&cb.Payload{ 91 Header: &cb.Header{ 92 ChannelHeader: []byte("bad header"), 93 }, 94 }) 95 env, _ = proto.Marshal(&cb.Envelope{ 96 Payload: payload, 97 }) 98 badBlock = &cb.Block{ 99 Data: &cb.BlockData{ 100 Data: [][]byte{env}, 101 }, 102 } 103 _, err = utils.GetChainIDFromBlock(badBlock) 104 assert.Error(t, err, "Expected error with malformed channel header") 105 106 // nil payload header 107 payload, _ = proto.Marshal(&cb.Payload{}) 108 env, _ = proto.Marshal(&cb.Envelope{ 109 Payload: payload, 110 }) 111 badBlock = &cb.Block{ 112 Data: &cb.BlockData{ 113 Data: [][]byte{env}, 114 }, 115 } 116 _, err = utils.GetChainIDFromBlock(badBlock) 117 assert.Error(t, err, "Expected error when payload header is nil") 118 } 119 120 func TestGetBlockFromBlockBytes(t *testing.T) { 121 testChainID := "myuniquetestchainid" 122 gb, err := configtxtest.MakeGenesisBlock(testChainID) 123 assert.NoError(t, err, "Failed to create test configuration block") 124 blockBytes, err := utils.Marshal(gb) 125 assert.NoError(t, err, "Failed to marshal block") 126 _, err = utils.GetBlockFromBlockBytes(blockBytes) 127 assert.NoError(t, err, "to get block from block bytes") 128 129 // bad block bytes 130 _, err = utils.GetBlockFromBlockBytes([]byte("bad block")) 131 assert.Error(t, err, "Expected error for malformed block bytes") 132 } 133 134 func TestGetMetadataFromNewBlock(t *testing.T) { 135 block := common.NewBlock(0, nil) 136 md, err := utils.GetMetadataFromBlock(block, cb.BlockMetadataIndex_ORDERER) 137 assert.NoError(t, err, "Unexpected error extracting metadata from new block") 138 assert.Nil(t, md.Value, "Expected metadata field value to be nil") 139 assert.Equal(t, 0, len(md.Value), "Expected length of metadata field value to be 0") 140 md = utils.GetMetadataFromBlockOrPanic(block, cb.BlockMetadataIndex_ORDERER) 141 assert.NotNil(t, md, "Expected to get metadata from block") 142 143 // malformed metadata 144 block.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = []byte("bad metadata") 145 _, err = utils.GetMetadataFromBlock(block, cb.BlockMetadataIndex_ORDERER) 146 assert.Error(t, err, "Expected error with malformed metadata") 147 assert.Panics(t, func() { 148 _ = utils.GetMetadataFromBlockOrPanic(block, cb.BlockMetadataIndex_ORDERER) 149 }, "Expected panic with malformed metadata") 150 } 151 152 func TestInitBlockMeta(t *testing.T) { 153 // block with no metadata 154 block := &cb.Block{} 155 utils.InitBlockMetadata(block) 156 // should have 3 entries 157 assert.Equal(t, 3, len(block.Metadata.Metadata), "Expected block to have 3 metadata entries") 158 159 // block with a single entry 160 block = &cb.Block{ 161 Metadata: &cb.BlockMetadata{}, 162 } 163 block.Metadata.Metadata = append(block.Metadata.Metadata, []byte{}) 164 utils.InitBlockMetadata(block) 165 // should have 3 entries 166 assert.Equal(t, 3, len(block.Metadata.Metadata), "Expected block to have 3 metadata entries") 167 } 168 169 func TestCopyBlockMetadata(t *testing.T) { 170 srcBlock := common.NewBlock(0, nil) 171 dstBlock := &cb.Block{} 172 173 metadata, _ := proto.Marshal(&cb.Metadata{ 174 Value: []byte("orderer metadata"), 175 }) 176 srcBlock.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = metadata 177 utils.CopyBlockMetadata(srcBlock, dstBlock) 178 179 // check that the copy worked 180 assert.Equal(t, len(srcBlock.Metadata.Metadata), len(dstBlock.Metadata.Metadata), 181 "Expected target block to have same number of metadata entries after copy") 182 assert.Equal(t, metadata, dstBlock.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER], 183 "Unexpected metadata from target block") 184 } 185 186 func TestGetLastConfigIndexFromBlock(t *testing.T) { 187 block := common.NewBlock(0, nil) 188 index := uint64(2) 189 lc, _ := proto.Marshal(&cb.LastConfig{ 190 Index: index, 191 }) 192 metadata, _ := proto.Marshal(&cb.Metadata{ 193 Value: lc, 194 }) 195 block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = metadata 196 result, err := utils.GetLastConfigIndexFromBlock(block) 197 assert.NoError(t, err, "Unexpected error returning last config index") 198 assert.Equal(t, index, result, "Unexpected last config index returned from block") 199 result = utils.GetLastConfigIndexFromBlockOrPanic(block) 200 assert.Equal(t, index, result, "Unexpected last config index returned from block") 201 202 // malformed metadata 203 block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = []byte("bad metadata") 204 _, err = utils.GetLastConfigIndexFromBlock(block) 205 assert.Error(t, err, "Expected error with malformed metadata") 206 207 // malformed last config 208 metadata, _ = proto.Marshal(&cb.Metadata{ 209 Value: []byte("bad last config"), 210 }) 211 block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = metadata 212 _, err = utils.GetLastConfigIndexFromBlock(block) 213 assert.Error(t, err, "Expected error with malformed last config metadata") 214 assert.Panics(t, func() { 215 _ = utils.GetLastConfigIndexFromBlockOrPanic(block) 216 }, "Expected panic with malformed last config metadata") 217 }