github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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 configtxtest "github.com/hyperledger/fabric/common/configtx/test" 24 "github.com/hyperledger/fabric/protos/common" 25 cb "github.com/hyperledger/fabric/protos/common" 26 "github.com/hyperledger/fabric/protos/utils" 27 ) 28 29 func TestGetChainIDFromBlock(t *testing.T) { 30 var err error 31 var gb *common.Block 32 var cid string 33 34 testChainID := "myuniquetestchainid" 35 36 gb, err = configtxtest.MakeGenesisBlock(testChainID) 37 if err != nil { 38 t.Fatalf("failed to create test configuration block: %s", err) 39 } 40 cid, err = utils.GetChainIDFromBlock(gb) 41 if err != nil { 42 t.Fatalf("failed to get chain ID from block: %s", err) 43 } 44 if testChainID != cid { 45 t.Fatalf("failed with wrong chain ID: Actual=%s; Got=%s", testChainID, cid) 46 } 47 48 badBlock := gb 49 badBlock.Data = nil 50 _, err = utils.GetChainIDFromBlock(badBlock) 51 // We should get error 52 if err == nil { 53 t.Fatalf("error is expected -- the block must not be marshallable") 54 } 55 } 56 57 func TestGetBlockFromBlockBytes(t *testing.T) { 58 testChainID := "myuniquetestchainid" 59 gb, err := configtxtest.MakeGenesisBlock(testChainID) 60 if err != nil { 61 t.Fatalf("failed to create test configuration block: %s", err) 62 } 63 blockBytes, err := utils.Marshal(gb) 64 if err != nil { 65 t.Fatalf("failed to marshal block: %s", err) 66 } 67 _, err = utils.GetBlockFromBlockBytes(blockBytes) 68 if err != nil { 69 t.Fatalf("failed to get block from block bytes: %s", err) 70 } 71 } 72 73 func TestGetMetadataFromNewBlock(t *testing.T) { 74 block := common.NewBlock(0, nil) 75 md, err := utils.GetMetadataFromBlock(block, cb.BlockMetadataIndex_ORDERER) 76 if err != nil { 77 t.Fatal("Expected no error when extracting metadata from new block") 78 } 79 if md.Value != nil { 80 t.Fatal("Expected metadata field value to be nil, got", md.Value) 81 } 82 if len(md.Value) > 0 { 83 t.Fatal("Expected length of metadata field value to be 0, got", len(md.Value)) 84 } 85 86 }