Skip to content

Commit

Permalink
tests. added test case for issue (#619). Compare marshaling between a…
Browse files Browse the repository at this point in the history
… proto.Buffer and proto.Marshal with different proto.Buffer sizes.
  • Loading branch information
jmarais committed Sep 7, 2019
1 parent 23325ce commit f6b4f34
Show file tree
Hide file tree
Showing 4 changed files with 365 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/protobuffer/Makefile
@@ -0,0 +1,31 @@
# Protocol Buffers for Go with Gadgets
#
# Copyright (c) 2019, The GoGo Authors. All rights reserved.
# http://github.com/gogo/protobuf
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

regenerate:
go install github.com/gogo/protobuf/protoc-gen-gogo
protoc --gogo_out=. --proto_path=../../../../../:../../protobuf/:. *.proto
220 changes: 220 additions & 0 deletions test/protobuffer/protobuffer.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions test/protobuffer/protobuffer.proto
@@ -0,0 +1,47 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2019, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

syntax = "proto2";

package protobuffer;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";

message PBuffMarshal {
option (gogoproto.marshaler) = false;
option (gogoproto.sizer) = false;
optional bytes Field1 = 1;
optional int32 Field2 = 2;
}

message PBuffMarshaler {
option (gogoproto.marshaler) = true;
option (gogoproto.sizer) = true;
optional bytes Field1 = 1;
optional int32 Field2 = 2;
}
67 changes: 67 additions & 0 deletions test/protobuffer/protobuffer_test.go
@@ -0,0 +1,67 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2019, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package protobuffer

import (
"bytes"
"testing"

proto "github.com/gogo/protobuf/proto"
)

func TestProtoBufferMarshal12(t *testing.T) {
i := int32(33)
m := &PBuffMarshal{Field1: []byte(string("Tester123")), Field2: &i}
mer := &PBuffMarshaler{Field1: []byte(string("Tester123")), Field2: &i}
testCases := []struct {
name string
size int
m proto.Message
}{
{"MarshalLarge", 1024, m},
{"MarshalSmall", 0, m},
{"MarshalerLarge", 1024, mer},
{"MarshalerSmall", 0, mer},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
marshalCheck(t, tc.m, tc.size)
})
}
}

func marshalCheck(t *testing.T, m proto.Message, size int) {
buf := proto.NewBuffer(make([]byte, 0, size))
buf.Marshal(m)
bufferBytes := buf.Bytes()
protoBytes, _ := proto.Marshal(m)
if !bytes.Equal(bufferBytes, protoBytes) {
t.Fatalf("proto.Buffer Marshal != proto.Marshal (%v != %v)\n", bufferBytes, protoBytes)
}
}

0 comments on commit f6b4f34

Please sign in to comment.