github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/tools/protolator/dynamic_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/stretchr/testify/assert"
    27  )
    28  
    29  func TestPlainDynamicMsg(t *testing.T) {
    30  	fromPrefix := "from"
    31  	toPrefix := "to"
    32  	tppff := &testProtoPlainFieldFactory{
    33  		fromPrefix: fromPrefix,
    34  		toPrefix:   toPrefix,
    35  	}
    36  
    37  	fieldFactories = []protoFieldFactory{tppff, variablyOpaqueFieldFactory{}}
    38  
    39  	pfValue := "foo"
    40  	startMsg := &testprotos.DynamicMsg{
    41  		DynamicType: "SimpleMsg",
    42  		PlainDynamicField: &testprotos.ContextlessMsg{
    43  			OpaqueField: utils.MarshalOrPanic(&testprotos.SimpleMsg{
    44  				PlainField: pfValue,
    45  			}),
    46  		},
    47  	}
    48  
    49  	var buffer bytes.Buffer
    50  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    51  	newMsg := &testprotos.DynamicMsg{}
    52  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
    53  	assert.NotEqual(t, fromPrefix+toPrefix+extractSimpleMsgPlainField(startMsg.PlainDynamicField.OpaqueField), extractSimpleMsgPlainField(newMsg.PlainDynamicField.OpaqueField))
    54  
    55  	fieldFactories = []protoFieldFactory{tppff, variablyOpaqueFieldFactory{}, dynamicFieldFactory{}}
    56  
    57  	buffer.Reset()
    58  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    59  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
    60  	assert.Equal(t, fromPrefix+toPrefix+extractSimpleMsgPlainField(startMsg.PlainDynamicField.OpaqueField), extractSimpleMsgPlainField(newMsg.PlainDynamicField.OpaqueField))
    61  }
    62  
    63  func TestMapDynamicMsg(t *testing.T) {
    64  	fromPrefix := "from"
    65  	toPrefix := "to"
    66  	tppff := &testProtoPlainFieldFactory{
    67  		fromPrefix: fromPrefix,
    68  		toPrefix:   toPrefix,
    69  	}
    70  
    71  	fieldFactories = []protoFieldFactory{tppff, variablyOpaqueFieldFactory{}}
    72  
    73  	pfValue := "foo"
    74  	mapKey := "bar"
    75  	startMsg := &testprotos.DynamicMsg{
    76  		DynamicType: "SimpleMsg",
    77  		MapDynamicField: map[string]*testprotos.ContextlessMsg{
    78  			mapKey: &testprotos.ContextlessMsg{
    79  				OpaqueField: utils.MarshalOrPanic(&testprotos.SimpleMsg{
    80  					PlainField: pfValue,
    81  				}),
    82  			},
    83  		},
    84  	}
    85  
    86  	var buffer bytes.Buffer
    87  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    88  	newMsg := &testprotos.DynamicMsg{}
    89  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
    90  	assert.NotEqual(t, fromPrefix+toPrefix+extractSimpleMsgPlainField(startMsg.MapDynamicField[mapKey].OpaqueField), extractSimpleMsgPlainField(newMsg.MapDynamicField[mapKey].OpaqueField))
    91  
    92  	fieldFactories = []protoFieldFactory{tppff, variablyOpaqueFieldFactory{}, dynamicMapFieldFactory{}}
    93  
    94  	buffer.Reset()
    95  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    96  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
    97  	assert.Equal(t, fromPrefix+toPrefix+extractSimpleMsgPlainField(startMsg.MapDynamicField[mapKey].OpaqueField), extractSimpleMsgPlainField(newMsg.MapDynamicField[mapKey].OpaqueField))
    98  }
    99  
   100  func TestSliceDynamicMsg(t *testing.T) {
   101  	fromPrefix := "from"
   102  	toPrefix := "to"
   103  	tppff := &testProtoPlainFieldFactory{
   104  		fromPrefix: fromPrefix,
   105  		toPrefix:   toPrefix,
   106  	}
   107  
   108  	fieldFactories = []protoFieldFactory{tppff, variablyOpaqueFieldFactory{}}
   109  
   110  	pfValue := "foo"
   111  	startMsg := &testprotos.DynamicMsg{
   112  		DynamicType: "SimpleMsg",
   113  		SliceDynamicField: []*testprotos.ContextlessMsg{
   114  			&testprotos.ContextlessMsg{
   115  				OpaqueField: utils.MarshalOrPanic(&testprotos.SimpleMsg{
   116  					PlainField: pfValue,
   117  				}),
   118  			},
   119  		},
   120  	}
   121  
   122  	var buffer bytes.Buffer
   123  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
   124  	newMsg := &testprotos.DynamicMsg{}
   125  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
   126  	assert.NotEqual(t, fromPrefix+toPrefix+extractSimpleMsgPlainField(startMsg.SliceDynamicField[0].OpaqueField), extractSimpleMsgPlainField(newMsg.SliceDynamicField[0].OpaqueField))
   127  
   128  	fieldFactories = []protoFieldFactory{tppff, variablyOpaqueFieldFactory{}, dynamicSliceFieldFactory{}}
   129  
   130  	buffer.Reset()
   131  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
   132  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
   133  	assert.Equal(t, fromPrefix+toPrefix+extractSimpleMsgPlainField(startMsg.SliceDynamicField[0].OpaqueField), extractSimpleMsgPlainField(newMsg.SliceDynamicField[0].OpaqueField))
   134  }