github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/txmgmt/rwsetutil/rwset_test.go (about)

     1  /*
     2  Copyright hechain. 2022 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 rwsetutil
    18  
    19  import (
    20  	"io/ioutil"
    21  	"testing"
    22  
    23  	"github.com/davecgh/go-spew/spew"
    24  	"github.com/golang/protobuf/proto"
    25  	"github.com/hyperledger/fabric-protos-go/ledger/rwset"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  const rwsetV1ProtoBytesFile = "testdata/rwsetV1ProtoBytes"
    30  
    31  // TestRWSetV1BackwardCompatible passes if the 'RWSet' messgae declared in the latest version
    32  // is able to unmarshal the protobytes that are produced by the 'RWSet' proto message declared in
    33  // v1.0. This is to make sure that any incompatible changes does not go uncaught.
    34  func TestRWSetV1BackwardCompatible(t *testing.T) {
    35  	protoBytes, err := ioutil.ReadFile(rwsetV1ProtoBytesFile)
    36  	require.NoError(t, err)
    37  	rwset1 := &rwset.TxReadWriteSet{}
    38  	require.NoError(t, proto.Unmarshal(protoBytes, rwset1))
    39  	rwset2 := constructSampleRWSet()
    40  	t.Logf("rwset1=%s, rwset2=%s", spew.Sdump(rwset1), spew.Sdump(rwset2))
    41  	require.Equal(t, rwset2, rwset1)
    42  }
    43  
    44  // PrepareBinaryFileSampleRWSetV1 constructs a proto message for kvrwset and marshals its bytes to file 'rwsetV1ProtoBytes'.
    45  // this code should be run on fabric version 1.0 so as to produce a sample file of proto message declared in V1
    46  // In order to invoke this function on V1 code, copy this over on to V1 code, make the first letter as 'T', and finally invoke this function
    47  // using golang test framwork
    48  func PrepareBinaryFileSampleRWSetV1(t *testing.T) {
    49  	b, err := proto.Marshal(constructSampleRWSet())
    50  	require.NoError(t, err)
    51  	require.NoError(t, ioutil.WriteFile(rwsetV1ProtoBytesFile, b, 0o644))
    52  }
    53  
    54  func constructSampleRWSet() *rwset.TxReadWriteSet {
    55  	rwset1 := &rwset.TxReadWriteSet{}
    56  	rwset1.DataModel = rwset.TxReadWriteSet_KV
    57  	rwset1.NsRwset = []*rwset.NsReadWriteSet{
    58  		{Namespace: "ns-1", Rwset: []byte("ns-1-rwset")},
    59  		{Namespace: "ns-2", Rwset: []byte("ns-2-rwset")},
    60  	}
    61  	return rwset1
    62  }