github.com/kaituanwang/hyperledger@v2.0.1+incompatible/common/tools/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 "testing" 12 13 "github.com/golang/protobuf/proto" 14 "github.com/hyperledger/fabric/common/tools/protolator/testprotos" 15 "github.com/hyperledger/fabric/protoutil" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func extractNestedMsgPlainField(source []byte) string { 20 result := &testprotos.NestedMsg{} 21 err := proto.Unmarshal(source, result) 22 if err != nil { 23 panic(err) 24 } 25 return result.PlainNestedField.PlainField 26 } 27 28 func TestPlainVariablyOpaqueMsg(t *testing.T) { 29 fromPrefix := "from" 30 toPrefix := "to" 31 tppff := &testProtoPlainFieldFactory{ 32 fromPrefix: fromPrefix, 33 toPrefix: toPrefix, 34 } 35 36 fieldFactories = []protoFieldFactory{tppff} 37 38 pfValue := "foo" 39 startMsg := &testprotos.VariablyOpaqueMsg{ 40 OpaqueType: "NestedMsg", 41 PlainOpaqueField: protoutil.MarshalOrPanic(&testprotos.NestedMsg{ 42 PlainNestedField: &testprotos.SimpleMsg{ 43 PlainField: pfValue, 44 }, 45 }), 46 } 47 48 var buffer bytes.Buffer 49 assert.NoError(t, DeepMarshalJSON(&buffer, startMsg)) 50 newMsg := &testprotos.VariablyOpaqueMsg{} 51 assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)) 52 assert.NotEqual(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.PlainOpaqueField), extractNestedMsgPlainField(newMsg.PlainOpaqueField)) 53 54 fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}, variablyOpaqueFieldFactory{}} 55 56 buffer.Reset() 57 assert.NoError(t, DeepMarshalJSON(&buffer, startMsg)) 58 assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)) 59 assert.Equal(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.PlainOpaqueField), extractNestedMsgPlainField(newMsg.PlainOpaqueField)) 60 } 61 62 func TestMapVariablyOpaqueMsg(t *testing.T) { 63 fromPrefix := "from" 64 toPrefix := "to" 65 tppff := &testProtoPlainFieldFactory{ 66 fromPrefix: fromPrefix, 67 toPrefix: toPrefix, 68 } 69 70 fieldFactories = []protoFieldFactory{tppff} 71 72 pfValue := "foo" 73 mapKey := "bar" 74 startMsg := &testprotos.VariablyOpaqueMsg{ 75 OpaqueType: "NestedMsg", 76 MapOpaqueField: map[string][]byte{ 77 mapKey: protoutil.MarshalOrPanic(&testprotos.NestedMsg{ 78 PlainNestedField: &testprotos.SimpleMsg{ 79 PlainField: pfValue, 80 }, 81 }), 82 }, 83 } 84 85 var buffer bytes.Buffer 86 assert.NoError(t, DeepMarshalJSON(&buffer, startMsg)) 87 newMsg := &testprotos.VariablyOpaqueMsg{} 88 assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)) 89 assert.NotEqual(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.MapOpaqueField[mapKey]), extractNestedMsgPlainField(newMsg.MapOpaqueField[mapKey])) 90 91 fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}, variablyOpaqueMapFieldFactory{}} 92 93 buffer.Reset() 94 assert.NoError(t, DeepMarshalJSON(&buffer, startMsg)) 95 assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)) 96 assert.Equal(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.MapOpaqueField[mapKey]), extractNestedMsgPlainField(newMsg.MapOpaqueField[mapKey])) 97 } 98 99 func TestSliceVariablyOpaqueMsg(t *testing.T) { 100 fromPrefix := "from" 101 toPrefix := "to" 102 tppff := &testProtoPlainFieldFactory{ 103 fromPrefix: fromPrefix, 104 toPrefix: toPrefix, 105 } 106 107 fieldFactories = []protoFieldFactory{tppff} 108 109 pfValue := "foo" 110 startMsg := &testprotos.VariablyOpaqueMsg{ 111 OpaqueType: "NestedMsg", 112 SliceOpaqueField: [][]byte{ 113 protoutil.MarshalOrPanic(&testprotos.NestedMsg{ 114 PlainNestedField: &testprotos.SimpleMsg{ 115 PlainField: pfValue, 116 }, 117 }), 118 }, 119 } 120 121 var buffer bytes.Buffer 122 assert.NoError(t, DeepMarshalJSON(&buffer, startMsg)) 123 newMsg := &testprotos.VariablyOpaqueMsg{} 124 assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)) 125 assert.NotEqual(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.SliceOpaqueField[0]), extractNestedMsgPlainField(newMsg.SliceOpaqueField[0])) 126 127 fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}, variablyOpaqueSliceFieldFactory{}} 128 129 buffer.Reset() 130 assert.NoError(t, DeepMarshalJSON(&buffer, startMsg)) 131 assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)) 132 assert.Equal(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.SliceOpaqueField[0]), extractNestedMsgPlainField(newMsg.SliceOpaqueField[0])) 133 }