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