github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/tools/protolator/variably_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 extractNestedMsgPlainField(source []byte) string {
    31  	result := &testprotos.NestedMsg{}
    32  	err := proto.Unmarshal(source, result)
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  	return result.PlainNestedField.PlainField
    37  }
    38  
    39  func TestPlainVariablyOpaqueMsg(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.VariablyOpaqueMsg{
    51  		OpaqueType: "NestedMsg",
    52  		PlainOpaqueField: utils.MarshalOrPanic(&testprotos.NestedMsg{
    53  			PlainNestedField: &testprotos.SimpleMsg{
    54  				PlainField: pfValue,
    55  			},
    56  		}),
    57  	}
    58  
    59  	var buffer bytes.Buffer
    60  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    61  	newMsg := &testprotos.VariablyOpaqueMsg{}
    62  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
    63  	assert.NotEqual(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.PlainOpaqueField), extractNestedMsgPlainField(newMsg.PlainOpaqueField))
    64  
    65  	fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}, variablyOpaqueFieldFactory{}}
    66  
    67  	buffer.Reset()
    68  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    69  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
    70  	assert.Equal(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.PlainOpaqueField), extractNestedMsgPlainField(newMsg.PlainOpaqueField))
    71  }
    72  
    73  func TestMapVariablyOpaqueMsg(t *testing.T) {
    74  	fromPrefix := "from"
    75  	toPrefix := "to"
    76  	tppff := &testProtoPlainFieldFactory{
    77  		fromPrefix: fromPrefix,
    78  		toPrefix:   toPrefix,
    79  	}
    80  
    81  	fieldFactories = []protoFieldFactory{tppff}
    82  
    83  	pfValue := "foo"
    84  	mapKey := "bar"
    85  	startMsg := &testprotos.VariablyOpaqueMsg{
    86  		OpaqueType: "NestedMsg",
    87  		MapOpaqueField: map[string][]byte{
    88  			mapKey: utils.MarshalOrPanic(&testprotos.NestedMsg{
    89  				PlainNestedField: &testprotos.SimpleMsg{
    90  					PlainField: pfValue,
    91  				},
    92  			}),
    93  		},
    94  	}
    95  
    96  	var buffer bytes.Buffer
    97  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    98  	newMsg := &testprotos.VariablyOpaqueMsg{}
    99  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
   100  	assert.NotEqual(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.MapOpaqueField[mapKey]), extractNestedMsgPlainField(newMsg.MapOpaqueField[mapKey]))
   101  
   102  	fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}, variablyOpaqueMapFieldFactory{}}
   103  
   104  	buffer.Reset()
   105  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
   106  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
   107  	assert.Equal(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.MapOpaqueField[mapKey]), extractNestedMsgPlainField(newMsg.MapOpaqueField[mapKey]))
   108  }
   109  
   110  func TestSliceVariablyOpaqueMsg(t *testing.T) {
   111  	fromPrefix := "from"
   112  	toPrefix := "to"
   113  	tppff := &testProtoPlainFieldFactory{
   114  		fromPrefix: fromPrefix,
   115  		toPrefix:   toPrefix,
   116  	}
   117  
   118  	fieldFactories = []protoFieldFactory{tppff}
   119  
   120  	pfValue := "foo"
   121  	startMsg := &testprotos.VariablyOpaqueMsg{
   122  		OpaqueType: "NestedMsg",
   123  		SliceOpaqueField: [][]byte{
   124  			utils.MarshalOrPanic(&testprotos.NestedMsg{
   125  				PlainNestedField: &testprotos.SimpleMsg{
   126  					PlainField: pfValue,
   127  				},
   128  			}),
   129  		},
   130  	}
   131  
   132  	var buffer bytes.Buffer
   133  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
   134  	newMsg := &testprotos.VariablyOpaqueMsg{}
   135  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
   136  	assert.NotEqual(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.SliceOpaqueField[0]), extractNestedMsgPlainField(newMsg.SliceOpaqueField[0]))
   137  
   138  	fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}, variablyOpaqueSliceFieldFactory{}}
   139  
   140  	buffer.Reset()
   141  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
   142  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
   143  	assert.Equal(t, fromPrefix+toPrefix+extractNestedMsgPlainField(startMsg.SliceOpaqueField[0]), extractNestedMsgPlainField(newMsg.SliceOpaqueField[0]))
   144  }