diff --git a/test/protobuffer/Makefile b/test/protobuffer/Makefile new file mode 100644 index 0000000000..00e39f73f4 --- /dev/null +++ b/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 diff --git a/test/protobuffer/protobuffer.pb.go b/test/protobuffer/protobuffer.pb.go new file mode 100644 index 0000000000..ae8df3a673 --- /dev/null +++ b/test/protobuffer/protobuffer.pb.go @@ -0,0 +1,220 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: protobuffer.proto + +package protobuffer + +import ( + fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +type PBuffMarshal struct { + Field1 []byte `protobuf:"bytes,1,opt,name=Field1" json:"Field1,omitempty"` + Field2 *int32 `protobuf:"varint,2,opt,name=Field2" json:"Field2,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *PBuffMarshal) Reset() { *m = PBuffMarshal{} } +func (m *PBuffMarshal) String() string { return proto.CompactTextString(m) } +func (*PBuffMarshal) ProtoMessage() {} +func (*PBuffMarshal) Descriptor() ([]byte, []int) { + return fileDescriptor_12798c2215df1728, []int{0} +} +func (m *PBuffMarshal) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_PBuffMarshal.Unmarshal(m, b) +} +func (m *PBuffMarshal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_PBuffMarshal.Marshal(b, m, deterministic) +} +func (m *PBuffMarshal) XXX_Merge(src proto.Message) { + xxx_messageInfo_PBuffMarshal.Merge(m, src) +} +func (m *PBuffMarshal) XXX_Size() int { + return xxx_messageInfo_PBuffMarshal.Size(m) +} +func (m *PBuffMarshal) XXX_DiscardUnknown() { + xxx_messageInfo_PBuffMarshal.DiscardUnknown(m) +} + +var xxx_messageInfo_PBuffMarshal proto.InternalMessageInfo + +func (m *PBuffMarshal) GetField1() []byte { + if m != nil { + return m.Field1 + } + return nil +} + +func (m *PBuffMarshal) GetField2() int32 { + if m != nil && m.Field2 != nil { + return *m.Field2 + } + return 0 +} + +type PBuffMarshaler struct { + Field1 []byte `protobuf:"bytes,1,opt,name=Field1" json:"Field1,omitempty"` + Field2 *int32 `protobuf:"varint,2,opt,name=Field2" json:"Field2,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *PBuffMarshaler) Reset() { *m = PBuffMarshaler{} } +func (m *PBuffMarshaler) String() string { return proto.CompactTextString(m) } +func (*PBuffMarshaler) ProtoMessage() {} +func (*PBuffMarshaler) Descriptor() ([]byte, []int) { + return fileDescriptor_12798c2215df1728, []int{1} +} +func (m *PBuffMarshaler) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_PBuffMarshaler.Unmarshal(m, b) +} +func (m *PBuffMarshaler) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_PBuffMarshaler.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *PBuffMarshaler) XXX_Merge(src proto.Message) { + xxx_messageInfo_PBuffMarshaler.Merge(m, src) +} +func (m *PBuffMarshaler) XXX_Size() int { + return m.Size() +} +func (m *PBuffMarshaler) XXX_DiscardUnknown() { + xxx_messageInfo_PBuffMarshaler.DiscardUnknown(m) +} + +var xxx_messageInfo_PBuffMarshaler proto.InternalMessageInfo + +func (m *PBuffMarshaler) GetField1() []byte { + if m != nil { + return m.Field1 + } + return nil +} + +func (m *PBuffMarshaler) GetField2() int32 { + if m != nil && m.Field2 != nil { + return *m.Field2 + } + return 0 +} + +func init() { + proto.RegisterType((*PBuffMarshal)(nil), "protobuffer.PBuffMarshal") + proto.RegisterType((*PBuffMarshaler)(nil), "protobuffer.PBuffMarshaler") +} + +func init() { proto.RegisterFile("protobuffer.proto", fileDescriptor_12798c2215df1728) } + +var fileDescriptor_12798c2215df1728 = []byte{ + // 144 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2c, 0x28, 0xca, 0x2f, + 0xc9, 0x4f, 0x2a, 0x4d, 0x4b, 0x4b, 0x2d, 0xd2, 0x03, 0xb3, 0x85, 0xb8, 0x91, 0x84, 0xa4, 0x74, + 0xd3, 0x33, 0x4b, 0x32, 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, 0xf5, 0xd3, 0xf3, 0xd3, 0xf3, 0xf5, + 0x61, 0x92, 0x60, 0x1e, 0x98, 0x03, 0x66, 0x41, 0xf4, 0x2a, 0x79, 0x70, 0xf1, 0x04, 0x38, 0x95, + 0xa6, 0xa5, 0xf9, 0x26, 0x16, 0x15, 0x67, 0x24, 0xe6, 0x08, 0x89, 0x71, 0xb1, 0xb9, 0x65, 0xa6, + 0xe6, 0xa4, 0x18, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x04, 0x41, 0x79, 0x70, 0x71, 0x23, 0x09, + 0x26, 0x05, 0x46, 0x0d, 0x56, 0xa8, 0xb8, 0x91, 0x15, 0x47, 0xc7, 0x42, 0x79, 0x86, 0x05, 0x0b, + 0xe5, 0x19, 0x94, 0xbc, 0xb8, 0xf8, 0x90, 0x4d, 0x4a, 0x2d, 0x22, 0xcb, 0x2c, 0xc6, 0x05, 0x0b, + 0xe5, 0x19, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xc0, 0x9d, 0x92, 0xe0, 0xe5, 0x00, 0x00, 0x00, +} + +func (m *PBuffMarshaler) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *PBuffMarshaler) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PBuffMarshaler) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } + if m.Field2 != nil { + i = encodeVarintProtobuffer(dAtA, i, uint64(*m.Field2)) + i-- + dAtA[i] = 0x10 + } + if m.Field1 != nil { + i -= len(m.Field1) + copy(dAtA[i:], m.Field1) + i = encodeVarintProtobuffer(dAtA, i, uint64(len(m.Field1))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintProtobuffer(dAtA []byte, offset int, v uint64) int { + offset -= sovProtobuffer(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *PBuffMarshaler) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Field1 != nil { + l = len(m.Field1) + n += 1 + l + sovProtobuffer(uint64(l)) + } + if m.Field2 != nil { + n += 1 + sovProtobuffer(uint64(*m.Field2)) + } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } + return n +} + +func sovProtobuffer(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozProtobuffer(x uint64) (n int) { + return sovProtobuffer(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} diff --git a/test/protobuffer/protobuffer.proto b/test/protobuffer/protobuffer.proto new file mode 100644 index 0000000000..43743cda07 --- /dev/null +++ b/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; +} diff --git a/test/protobuffer/protobuffer_test.go b/test/protobuffer/protobuffer_test.go new file mode 100644 index 0000000000..ac5bb1e6ee --- /dev/null +++ b/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) + } +}