github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric-config/protolator/variably_opaque_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package protolator
     8  
     9  import (
    10  	"bytes"
    11  	"github.com/hellobchain/third_party/hyperledger/fabric-config/protolator/testprotos"
    12  	"testing"
    13  
    14  	"github.com/golang/protobuf/proto"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  func extractNestedMsgPlainField(source []byte) string {
    19  	result := &testprotos.NestedMsg{}
    20  	err := proto.Unmarshal(source, result)
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  	return result.PlainNestedField.PlainField
    25  }
    26  
    27  func TestPlainVariablyOpaqueMsg(t *testing.T) {
    28  	gt := NewGomegaWithT(t)
    29  
    30  	fromPrefix := "from"
    31  	toPrefix := "to"
    32  	tppff := &testProtoPlainFieldFactory{
    33  		fromPrefix: fromPrefix,
    34  		toPrefix:   toPrefix,
    35  	}
    36  
    37  	fieldFactories = []protoFieldFactory{tppff}
    38  
    39  	pfValue := "foo"
    40  	startMsg := &testprotos.VariablyOpaqueMsg{
    41  		OpaqueType: "NestedMsg",
    42  		PlainOpaqueField: protoMarshalOrPanic(&testprotos.NestedMsg{
    43  			PlainNestedField: &testprotos.SimpleMsg{
    44  				PlainField: pfValue,
    45  			},
    46  		}),
    47  	}
    48  
    49  	var buffer bytes.Buffer
    50  	err := DeepMarshalJSON(&buffer, startMsg)
    51  	gt.Expect(err).NotTo(HaveOccurred())
    52  	newMsg := &testprotos.VariablyOpaqueMsg{}
    53  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
    54  	gt.Expect(err).NotTo(HaveOccurred())
    55  	gt.Expect(extractNestedMsgPlainField(newMsg.PlainOpaqueField)).NotTo(Equal(fromPrefix + toPrefix + extractNestedMsgPlainField(startMsg.PlainOpaqueField)))
    56  
    57  	fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}, variablyOpaqueFieldFactory{}}
    58  
    59  	buffer.Reset()
    60  	err = DeepMarshalJSON(&buffer, startMsg)
    61  	gt.Expect(err).NotTo(HaveOccurred())
    62  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
    63  	gt.Expect(err).NotTo(HaveOccurred())
    64  	gt.Expect(extractNestedMsgPlainField(newMsg.PlainOpaqueField)).To(Equal(fromPrefix + toPrefix + extractNestedMsgPlainField(startMsg.PlainOpaqueField)))
    65  }
    66  
    67  func TestMapVariablyOpaqueMsg(t *testing.T) {
    68  	gt := NewGomegaWithT(t)
    69  
    70  	fromPrefix := "from"
    71  	toPrefix := "to"
    72  	tppff := &testProtoPlainFieldFactory{
    73  		fromPrefix: fromPrefix,
    74  		toPrefix:   toPrefix,
    75  	}
    76  
    77  	fieldFactories = []protoFieldFactory{tppff}
    78  
    79  	pfValue := "foo"
    80  	mapKey := "bar"
    81  	startMsg := &testprotos.VariablyOpaqueMsg{
    82  		OpaqueType: "NestedMsg",
    83  		MapOpaqueField: map[string][]byte{
    84  			mapKey: protoMarshalOrPanic(&testprotos.NestedMsg{
    85  				PlainNestedField: &testprotos.SimpleMsg{
    86  					PlainField: pfValue,
    87  				},
    88  			}),
    89  		},
    90  	}
    91  
    92  	var buffer bytes.Buffer
    93  	err := DeepMarshalJSON(&buffer, startMsg)
    94  	gt.Expect(err).NotTo(HaveOccurred())
    95  	newMsg := &testprotos.VariablyOpaqueMsg{}
    96  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
    97  	gt.Expect(err).NotTo(HaveOccurred())
    98  	gt.Expect(extractNestedMsgPlainField(newMsg.MapOpaqueField[mapKey])).NotTo(Equal(fromPrefix + toPrefix + extractNestedMsgPlainField(startMsg.MapOpaqueField[mapKey])))
    99  
   100  	fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}, variablyOpaqueMapFieldFactory{}}
   101  
   102  	buffer.Reset()
   103  	err = DeepMarshalJSON(&buffer, startMsg)
   104  	gt.Expect(err).NotTo(HaveOccurred())
   105  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
   106  	gt.Expect(err).NotTo(HaveOccurred())
   107  	gt.Expect(extractNestedMsgPlainField(newMsg.MapOpaqueField[mapKey])).To(Equal(fromPrefix + toPrefix + extractNestedMsgPlainField(startMsg.MapOpaqueField[mapKey])))
   108  }
   109  
   110  func TestSliceVariablyOpaqueMsg(t *testing.T) {
   111  	gt := NewGomegaWithT(t)
   112  
   113  	fromPrefix := "from"
   114  	toPrefix := "to"
   115  	tppff := &testProtoPlainFieldFactory{
   116  		fromPrefix: fromPrefix,
   117  		toPrefix:   toPrefix,
   118  	}
   119  
   120  	fieldFactories = []protoFieldFactory{tppff}
   121  
   122  	pfValue := "foo"
   123  	startMsg := &testprotos.VariablyOpaqueMsg{
   124  		OpaqueType: "NestedMsg",
   125  		SliceOpaqueField: [][]byte{
   126  			protoMarshalOrPanic(&testprotos.NestedMsg{
   127  				PlainNestedField: &testprotos.SimpleMsg{
   128  					PlainField: pfValue,
   129  				},
   130  			}),
   131  		},
   132  	}
   133  
   134  	var buffer bytes.Buffer
   135  	err := DeepMarshalJSON(&buffer, startMsg)
   136  	gt.Expect(err).NotTo(HaveOccurred())
   137  	newMsg := &testprotos.VariablyOpaqueMsg{}
   138  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
   139  	gt.Expect(err).NotTo(HaveOccurred())
   140  	gt.Expect(extractNestedMsgPlainField(newMsg.SliceOpaqueField[0])).NotTo(Equal(fromPrefix + toPrefix + extractNestedMsgPlainField(startMsg.SliceOpaqueField[0])))
   141  
   142  	fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}, variablyOpaqueSliceFieldFactory{}}
   143  
   144  	buffer.Reset()
   145  	err = DeepMarshalJSON(&buffer, startMsg)
   146  	gt.Expect(err).NotTo(HaveOccurred())
   147  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
   148  	gt.Expect(err).NotTo(HaveOccurred())
   149  	gt.Expect(extractNestedMsgPlainField(newMsg.SliceOpaqueField[0])).To(Equal(fromPrefix + toPrefix + extractNestedMsgPlainField(startMsg.SliceOpaqueField[0])))
   150  }