github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric-config/protolator/statically_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 extractSimpleMsgPlainField(source []byte) string {
    19  	result := &testprotos.SimpleMsg{}
    20  	err := proto.Unmarshal(source, result)
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  	return result.PlainField
    25  }
    26  
    27  func TestPlainStaticallyOpaqueMsg(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.StaticallyOpaqueMsg{
    41  		PlainOpaqueField: protoMarshalOrPanic(&testprotos.SimpleMsg{
    42  			PlainField: pfValue,
    43  		}),
    44  	}
    45  
    46  	var buffer bytes.Buffer
    47  	err := DeepMarshalJSON(&buffer, startMsg)
    48  	gt.Expect(err).NotTo(HaveOccurred())
    49  	newMsg := &testprotos.StaticallyOpaqueMsg{}
    50  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
    51  	gt.Expect(err).NotTo(HaveOccurred())
    52  	gt.Expect(extractSimpleMsgPlainField(newMsg.PlainOpaqueField)).NotTo(Equal(fromPrefix + toPrefix + extractSimpleMsgPlainField(startMsg.PlainOpaqueField)))
    53  
    54  	fieldFactories = []protoFieldFactory{tppff, staticallyOpaqueFieldFactory{}}
    55  
    56  	buffer.Reset()
    57  	err = DeepMarshalJSON(&buffer, startMsg)
    58  	gt.Expect(err).NotTo(HaveOccurred())
    59  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
    60  	gt.Expect(err).NotTo(HaveOccurred())
    61  	gt.Expect(extractSimpleMsgPlainField(newMsg.PlainOpaqueField)).To(Equal(fromPrefix + toPrefix + extractSimpleMsgPlainField(startMsg.PlainOpaqueField)))
    62  }
    63  
    64  func TestMapStaticallyOpaqueMsg(t *testing.T) {
    65  	gt := NewGomegaWithT(t)
    66  
    67  	fromPrefix := "from"
    68  	toPrefix := "to"
    69  	tppff := &testProtoPlainFieldFactory{
    70  		fromPrefix: fromPrefix,
    71  		toPrefix:   toPrefix,
    72  	}
    73  
    74  	fieldFactories = []protoFieldFactory{tppff}
    75  
    76  	pfValue := "foo"
    77  	mapKey := "bar"
    78  	startMsg := &testprotos.StaticallyOpaqueMsg{
    79  		MapOpaqueField: map[string][]byte{
    80  			mapKey: protoMarshalOrPanic(&testprotos.SimpleMsg{
    81  				PlainField: pfValue,
    82  			}),
    83  		},
    84  	}
    85  
    86  	var buffer bytes.Buffer
    87  	err := DeepMarshalJSON(&buffer, startMsg)
    88  	gt.Expect(err).NotTo(HaveOccurred())
    89  	newMsg := &testprotos.StaticallyOpaqueMsg{}
    90  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
    91  	gt.Expect(err).NotTo(HaveOccurred())
    92  	gt.Expect(extractSimpleMsgPlainField(newMsg.MapOpaqueField[mapKey])).NotTo(Equal(fromPrefix + toPrefix + extractSimpleMsgPlainField(startMsg.MapOpaqueField[mapKey])))
    93  
    94  	fieldFactories = []protoFieldFactory{tppff, staticallyOpaqueMapFieldFactory{}}
    95  
    96  	buffer.Reset()
    97  	err = DeepMarshalJSON(&buffer, startMsg)
    98  	gt.Expect(err).NotTo(HaveOccurred())
    99  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
   100  	gt.Expect(err).NotTo(HaveOccurred())
   101  	gt.Expect(extractSimpleMsgPlainField(newMsg.MapOpaqueField[mapKey])).To(Equal(fromPrefix + toPrefix + extractSimpleMsgPlainField(startMsg.MapOpaqueField[mapKey])))
   102  }
   103  
   104  func TestSliceStaticallyOpaqueMsg(t *testing.T) {
   105  	gt := NewGomegaWithT(t)
   106  
   107  	fromPrefix := "from"
   108  	toPrefix := "to"
   109  	tppff := &testProtoPlainFieldFactory{
   110  		fromPrefix: fromPrefix,
   111  		toPrefix:   toPrefix,
   112  	}
   113  
   114  	fieldFactories = []protoFieldFactory{tppff}
   115  
   116  	pfValue := "foo"
   117  	startMsg := &testprotos.StaticallyOpaqueMsg{
   118  		SliceOpaqueField: [][]byte{
   119  			protoMarshalOrPanic(&testprotos.SimpleMsg{
   120  				PlainField: pfValue,
   121  			}),
   122  		},
   123  	}
   124  
   125  	var buffer bytes.Buffer
   126  	err := DeepMarshalJSON(&buffer, startMsg)
   127  	gt.Expect(err).NotTo(HaveOccurred())
   128  	newMsg := &testprotos.StaticallyOpaqueMsg{}
   129  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
   130  	gt.Expect(err).NotTo(HaveOccurred())
   131  	gt.Expect(extractSimpleMsgPlainField(newMsg.SliceOpaqueField[0])).NotTo(Equal(fromPrefix + toPrefix + extractSimpleMsgPlainField(startMsg.SliceOpaqueField[0])))
   132  
   133  	fieldFactories = []protoFieldFactory{tppff, staticallyOpaqueSliceFieldFactory{}}
   134  
   135  	buffer.Reset()
   136  	err = DeepMarshalJSON(&buffer, startMsg)
   137  	gt.Expect(err).NotTo(HaveOccurred())
   138  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
   139  	gt.Expect(err).NotTo(HaveOccurred())
   140  	gt.Expect(extractSimpleMsgPlainField(newMsg.SliceOpaqueField[0])).To(Equal(fromPrefix + toPrefix + extractSimpleMsgPlainField(startMsg.SliceOpaqueField[0])))
   141  }
   142  
   143  func TestIgnoredNilFields(t *testing.T) {
   144  	gt := NewGomegaWithT(t)
   145  
   146  	_ = StaticallyOpaqueFieldProto(&testprotos.UnmarshalableDeepFields{})
   147  	_ = StaticallyOpaqueMapFieldProto(&testprotos.UnmarshalableDeepFields{})
   148  	_ = StaticallyOpaqueSliceFieldProto(&testprotos.UnmarshalableDeepFields{})
   149  
   150  	fieldFactories = []protoFieldFactory{
   151  		staticallyOpaqueFieldFactory{},
   152  		staticallyOpaqueMapFieldFactory{},
   153  		staticallyOpaqueSliceFieldFactory{},
   154  	}
   155  
   156  	err := DeepMarshalJSON(&bytes.Buffer{}, &testprotos.UnmarshalableDeepFields{
   157  		PlainOpaqueField: []byte("fake"),
   158  	})
   159  	gt.Expect(err).To(MatchError("*testprotos.UnmarshalableDeepFields: error in PopulateTo for field plain_opaque_field for message *testprotos.UnmarshalableDeepFields: intentional error"))
   160  	err = DeepMarshalJSON(&bytes.Buffer{}, &testprotos.UnmarshalableDeepFields{
   161  		MapOpaqueField: map[string][]byte{"foo": []byte("bar")},
   162  	})
   163  	gt.Expect(err).To(MatchError("*testprotos.UnmarshalableDeepFields: error in PopulateTo for map field map_opaque_field and key foo for message *testprotos.UnmarshalableDeepFields: intentional error"))
   164  	err = DeepMarshalJSON(&bytes.Buffer{}, &testprotos.UnmarshalableDeepFields{
   165  		SliceOpaqueField: [][]byte{[]byte("bar")},
   166  	})
   167  	gt.Expect(err).To(MatchError("*testprotos.UnmarshalableDeepFields: error in PopulateTo for slice field slice_opaque_field at index 0 for message *testprotos.UnmarshalableDeepFields: intentional error"))
   168  	err = DeepMarshalJSON(&bytes.Buffer{}, &testprotos.UnmarshalableDeepFields{})
   169  	gt.Expect(err).NotTo(HaveOccurred())
   170  }
   171  
   172  // protoMarshalOrPanic serializes a protobuf message and panics if this
   173  // operation fails
   174  func protoMarshalOrPanic(pb proto.Message) []byte {
   175  	data, err := proto.Marshal(pb)
   176  	if err != nil {
   177  		panic(err)
   178  	}
   179  
   180  	return data
   181  }