github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/ledger/blkstorage/fsblkstorage/block_serialization_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  package fsblkstorage
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/golang/protobuf/proto"
    23  	"github.com/hyperledger/fabric/common/ledger/testutil"
    24  	putils "github.com/hyperledger/fabric/protos/utils"
    25  )
    26  
    27  func TestBlockSerialization(t *testing.T) {
    28  	block := testutil.ConstructTestBlock(t, 1, 10, 100)
    29  	bb, _, err := serializeBlock(block)
    30  	testutil.AssertNoError(t, err, "")
    31  	deserializedBlock, err := deserializeBlock(bb)
    32  	testutil.AssertNoError(t, err, "")
    33  	testutil.AssertEquals(t, deserializedBlock, block)
    34  }
    35  
    36  func TestExtractTxid(t *testing.T) {
    37  	txEnv, txid, _ := testutil.ConstructTransaction(t, testutil.ConstructRandomBytes(t, 50), false)
    38  	txEnvBytes, _ := putils.GetBytesEnvelope(txEnv)
    39  	extractedTxid, err := extractTxID(txEnvBytes)
    40  	testutil.AssertNoError(t, err, "")
    41  	testutil.AssertEquals(t, extractedTxid, txid)
    42  }
    43  
    44  func TestSerializedBlockInfo(t *testing.T) {
    45  	block := testutil.ConstructTestBlock(t, 1, 10, 100)
    46  	bb, info, err := serializeBlock(block)
    47  	testutil.AssertNoError(t, err, "")
    48  	infoFromBB, err := extractSerializedBlockInfo(bb)
    49  	testutil.AssertNoError(t, err, "")
    50  	testutil.AssertEquals(t, infoFromBB, info)
    51  	testutil.AssertEquals(t, len(info.txOffsets), len(block.Data.Data))
    52  	for txIndex, txEnvBytes := range block.Data.Data {
    53  		txid, err := extractTxID(txEnvBytes)
    54  		testutil.AssertNoError(t, err, "")
    55  
    56  		indexInfo := info.txOffsets[txIndex]
    57  		indexTxID := indexInfo.txID
    58  		indexOffset := indexInfo.loc
    59  
    60  		testutil.AssertEquals(t, txid, indexTxID)
    61  		b := bb[indexOffset.offset:]
    62  		len, num := proto.DecodeVarint(b)
    63  		txEnvBytesFromBB := b[num : num+int(len)]
    64  		testutil.AssertEquals(t, txEnvBytesFromBB, txEnvBytes)
    65  	}
    66  }