github.com/lzy4123/fabric@v2.1.1+incompatible/common/tools/protolator/protoext/decorate_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package protoext
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/golang/protobuf/proto"
    13  
    14  	"github.com/hyperledger/fabric-protos-go/common"
    15  	"github.com/hyperledger/fabric-protos-go/ledger/rwset"
    16  	"github.com/hyperledger/fabric-protos-go/msp"
    17  	"github.com/hyperledger/fabric-protos-go/orderer"
    18  	"github.com/hyperledger/fabric-protos-go/peer"
    19  	"github.com/hyperledger/fabric/common/tools/protolator/protoext/commonext"
    20  	"github.com/hyperledger/fabric/common/tools/protolator/protoext/ledger/rwsetext"
    21  	"github.com/hyperledger/fabric/common/tools/protolator/protoext/mspext"
    22  	"github.com/hyperledger/fabric/common/tools/protolator/protoext/ordererext"
    23  	"github.com/hyperledger/fabric/common/tools/protolator/protoext/peerext"
    24  
    25  	. "github.com/onsi/gomega"
    26  )
    27  
    28  type GenericProtoMessage struct {
    29  	GenericField string
    30  }
    31  
    32  func (g *GenericProtoMessage) Reset() {
    33  	panic("not implemented")
    34  }
    35  
    36  func (g *GenericProtoMessage) String() string {
    37  	return "not implemented"
    38  }
    39  
    40  func (g *GenericProtoMessage) ProtoMessage() {
    41  	panic("not implemented")
    42  }
    43  
    44  func TestDecorate(t *testing.T) {
    45  	tests := []struct {
    46  		testSpec       string
    47  		msg            proto.Message
    48  		expectedReturn proto.Message
    49  	}{
    50  		{
    51  			testSpec: "common.BlockData",
    52  			msg: &common.BlockData{
    53  				Data: [][]byte{
    54  					[]byte("data-bytes"),
    55  				}},
    56  			expectedReturn: &commonext.BlockData{
    57  				BlockData: &common.BlockData{
    58  					Data: [][]byte{
    59  						[]byte("data-bytes"),
    60  					},
    61  				},
    62  			},
    63  		},
    64  		{
    65  			testSpec: "common.Config",
    66  			msg: &common.Config{
    67  				Sequence: 5,
    68  			},
    69  			expectedReturn: &commonext.Config{
    70  				Config: &common.Config{
    71  					Sequence: 5,
    72  				},
    73  			},
    74  		},
    75  		{
    76  			testSpec: "common.ConfigSignature",
    77  			msg: &common.ConfigSignature{
    78  				SignatureHeader: []byte("signature-header-bytes"),
    79  			},
    80  			expectedReturn: &commonext.ConfigSignature{
    81  				ConfigSignature: &common.ConfigSignature{
    82  					SignatureHeader: []byte("signature-header-bytes"),
    83  				},
    84  			},
    85  		},
    86  		{
    87  			testSpec: "common.ConfigUpdate",
    88  			msg: &common.ConfigUpdate{
    89  				ChannelId: "testchannel",
    90  			},
    91  			expectedReturn: &commonext.ConfigUpdate{
    92  				ConfigUpdate: &common.ConfigUpdate{
    93  					ChannelId: "testchannel",
    94  				},
    95  			},
    96  		},
    97  		{
    98  			testSpec: "common.ConfigUpdateEnvelope",
    99  			msg: &common.ConfigUpdateEnvelope{
   100  				ConfigUpdate: []byte("config-update-bytes"),
   101  			},
   102  			expectedReturn: &commonext.ConfigUpdateEnvelope{
   103  				ConfigUpdateEnvelope: &common.ConfigUpdateEnvelope{
   104  					ConfigUpdate: []byte("config-update-bytes"),
   105  				},
   106  			},
   107  		},
   108  		{
   109  			testSpec: "common.Envelope",
   110  			msg: &common.Envelope{
   111  				Payload: []byte("payload-bytes"),
   112  			},
   113  			expectedReturn: &commonext.Envelope{
   114  				Envelope: &common.Envelope{
   115  					Payload: []byte("payload-bytes"),
   116  				},
   117  			},
   118  		},
   119  		{
   120  			testSpec: "common.Header",
   121  			msg: &common.Header{
   122  				ChannelHeader: []byte("channel-header-bytes"),
   123  			},
   124  			expectedReturn: &commonext.Header{
   125  				Header: &common.Header{
   126  					ChannelHeader: []byte("channel-header-bytes"),
   127  				},
   128  			},
   129  		},
   130  		{
   131  			testSpec: "common.ChannelHeader",
   132  			msg: &common.ChannelHeader{
   133  				Type: 5,
   134  			},
   135  			expectedReturn: &commonext.ChannelHeader{
   136  				ChannelHeader: &common.ChannelHeader{
   137  					Type: 5,
   138  				},
   139  			},
   140  		},
   141  		{
   142  			testSpec: "common.SignatureHeader",
   143  			msg: &common.SignatureHeader{
   144  				Creator: []byte("creator-bytes"),
   145  			},
   146  			expectedReturn: &commonext.SignatureHeader{
   147  				SignatureHeader: &common.SignatureHeader{
   148  					Creator: []byte("creator-bytes"),
   149  				},
   150  			},
   151  		},
   152  		{
   153  			testSpec: "common.Payload",
   154  			msg: &common.Payload{
   155  				Header: &common.Header{ChannelHeader: []byte("channel-header-bytes")},
   156  			},
   157  			expectedReturn: &commonext.Payload{
   158  				Payload: &common.Payload{
   159  					Header: &common.Header{ChannelHeader: []byte("channel-header-bytes")},
   160  				},
   161  			},
   162  		},
   163  		{
   164  			testSpec: "common.Policy",
   165  			msg: &common.Policy{
   166  				Type: 5,
   167  			},
   168  			expectedReturn: &commonext.Policy{
   169  				Policy: &common.Policy{
   170  					Type: 5,
   171  				},
   172  			},
   173  		},
   174  		{
   175  			testSpec: "msp.MSPConfig",
   176  			msg: &msp.MSPConfig{
   177  				Type: 5,
   178  			},
   179  			expectedReturn: &mspext.MSPConfig{
   180  				MSPConfig: &msp.MSPConfig{
   181  					Type: 5,
   182  				},
   183  			},
   184  		},
   185  		{
   186  			testSpec: "msp.MSPPrincipal",
   187  			msg: &msp.MSPPrincipal{
   188  				Principal: []byte("principal-bytes"),
   189  			},
   190  			expectedReturn: &mspext.MSPPrincipal{
   191  				MSPPrincipal: &msp.MSPPrincipal{
   192  					Principal: []byte("principal-bytes"),
   193  				},
   194  			},
   195  		},
   196  		{
   197  			testSpec: "orderer.ConsensusType",
   198  			msg: &orderer.ConsensusType{
   199  				Type: "etcdraft",
   200  			},
   201  			expectedReturn: &ordererext.ConsensusType{
   202  				ConsensusType: &orderer.ConsensusType{
   203  					Type: "etcdraft",
   204  				},
   205  			},
   206  		},
   207  		{
   208  			testSpec: "peer.ChaincodeAction",
   209  			msg: &peer.ChaincodeAction{
   210  				Results: []byte("results-bytes"),
   211  			},
   212  			expectedReturn: &peerext.ChaincodeAction{
   213  				ChaincodeAction: &peer.ChaincodeAction{
   214  					Results: []byte("results-bytes"),
   215  				},
   216  			},
   217  		},
   218  		{
   219  			testSpec: "peer.ChaincodeActionPayload",
   220  			msg: &peer.ChaincodeActionPayload{
   221  				ChaincodeProposalPayload: []byte("chaincode-proposal-payload-bytes"),
   222  			},
   223  			expectedReturn: &peerext.ChaincodeActionPayload{
   224  				ChaincodeActionPayload: &peer.ChaincodeActionPayload{
   225  					ChaincodeProposalPayload: []byte("chaincode-proposal-payload-bytes"),
   226  				},
   227  			},
   228  		},
   229  		{
   230  			testSpec: "peer.ChaincodeEndorsedAction",
   231  			msg: &peer.ChaincodeEndorsedAction{
   232  				ProposalResponsePayload: []byte("proposal-response-payload-bytes"),
   233  			},
   234  			expectedReturn: &peerext.ChaincodeEndorsedAction{
   235  				ChaincodeEndorsedAction: &peer.ChaincodeEndorsedAction{
   236  					ProposalResponsePayload: []byte("proposal-response-payload-bytes"),
   237  				},
   238  			},
   239  		},
   240  		{
   241  			testSpec: "peer.ChaincodeProposalPayload",
   242  			msg: &peer.ChaincodeProposalPayload{
   243  				Input: []byte("input-bytes"),
   244  			},
   245  			expectedReturn: &peerext.ChaincodeProposalPayload{
   246  				ChaincodeProposalPayload: &peer.ChaincodeProposalPayload{
   247  					Input: []byte("input-bytes"),
   248  				},
   249  			},
   250  		},
   251  		{
   252  			testSpec: "peer.ProposalResponsePayload",
   253  			msg: &peer.ProposalResponsePayload{
   254  				ProposalHash: []byte("proposal-hash-bytes"),
   255  			},
   256  			expectedReturn: &peerext.ProposalResponsePayload{
   257  				ProposalResponsePayload: &peer.ProposalResponsePayload{
   258  					ProposalHash: []byte("proposal-hash-bytes"),
   259  				},
   260  			},
   261  		},
   262  		{
   263  			testSpec: "peer.TransactionAction",
   264  			msg: &peer.TransactionAction{
   265  				Header: []byte("header-bytes"),
   266  			},
   267  			expectedReturn: &peerext.TransactionAction{
   268  				TransactionAction: &peer.TransactionAction{
   269  					Header: []byte("header-bytes"),
   270  				},
   271  			},
   272  		},
   273  		{
   274  			testSpec: "rwset.TxReadWriteSet",
   275  			msg: &rwset.TxReadWriteSet{
   276  				NsRwset: []*rwset.NsReadWriteSet{
   277  					{
   278  						Namespace: "namespace",
   279  					},
   280  				},
   281  			},
   282  			expectedReturn: &rwsetext.TxReadWriteSet{
   283  				TxReadWriteSet: &rwset.TxReadWriteSet{
   284  					NsRwset: []*rwset.NsReadWriteSet{
   285  						{
   286  							Namespace: "namespace",
   287  						},
   288  					},
   289  				},
   290  			},
   291  		},
   292  		{
   293  			testSpec: "default",
   294  			msg: &GenericProtoMessage{
   295  				GenericField: "test",
   296  			},
   297  			expectedReturn: &GenericProtoMessage{
   298  				GenericField: "test",
   299  			},
   300  		},
   301  	}
   302  
   303  	for _, tt := range tests {
   304  		t.Run(tt.testSpec, func(t *testing.T) {
   305  			gt := NewGomegaWithT(t)
   306  			decoratedMsg := Decorate(tt.msg)
   307  			gt.Expect(proto.Equal(decoratedMsg, tt.expectedReturn)).To(BeTrue())
   308  		})
   309  	}
   310  }