From 905f904464b40cf73ebf10eaa7abde0609986c41 Mon Sep 17 00:00:00 2001 From: Guy Baron Date: Thu, 9 May 2019 11:24:26 +0300 Subject: [PATCH 1/4] added tests for protobuf serlializer --- tests/protoMessages.pb.go | 86 ++++++++++++++++++++++++++++++++ tests/protoMessages.proto | 7 +++ tests/protoMessagesBase.go | 5 ++ tests/protoSerialization_test.go | 49 ++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 tests/protoMessages.pb.go create mode 100644 tests/protoMessages.proto create mode 100644 tests/protoMessagesBase.go create mode 100644 tests/protoSerialization_test.go diff --git a/tests/protoMessages.pb.go b/tests/protoMessages.pb.go new file mode 100644 index 0000000..37aedc5 --- /dev/null +++ b/tests/protoMessages.pb.go @@ -0,0 +1,86 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: protoMessages.proto + +package tests + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +// 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.ProtoPackageIsVersion3 // please upgrade the proto package + +type ProtoCommand struct { + SomeNumber int64 `protobuf:"varint,1,opt,name=some_number,json=someNumber,proto3" json:"some_number,omitempty"` + SomeData string `protobuf:"bytes,2,opt,name=some_data,json=someData,proto3" json:"some_data,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *ProtoCommand) Reset() { *m = ProtoCommand{} } +func (m *ProtoCommand) String() string { return proto.CompactTextString(m) } +func (*ProtoCommand) ProtoMessage() {} +func (*ProtoCommand) Descriptor() ([]byte, []int) { + return fileDescriptor_a1929c2ebf17cd0b, []int{0} +} + +func (m *ProtoCommand) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_ProtoCommand.Unmarshal(m, b) +} +func (m *ProtoCommand) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_ProtoCommand.Marshal(b, m, deterministic) +} +func (m *ProtoCommand) XXX_Merge(src proto.Message) { + xxx_messageInfo_ProtoCommand.Merge(m, src) +} +func (m *ProtoCommand) XXX_Size() int { + return xxx_messageInfo_ProtoCommand.Size(m) +} +func (m *ProtoCommand) XXX_DiscardUnknown() { + xxx_messageInfo_ProtoCommand.DiscardUnknown(m) +} + +var xxx_messageInfo_ProtoCommand proto.InternalMessageInfo + +func (m *ProtoCommand) GetSomeNumber() int64 { + if m != nil { + return m.SomeNumber + } + return 0 +} + +func (m *ProtoCommand) GetSomeData() string { + if m != nil { + return m.SomeData + } + return "" +} + +func init() { + proto.RegisterType((*ProtoCommand)(nil), "tests.ProtoCommand") +} + +func init() { proto.RegisterFile("protoMessages.proto", fileDescriptor_a1929c2ebf17cd0b) } + +var fileDescriptor_a1929c2ebf17cd0b = []byte{ + // 119 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0x28, 0xca, 0x2f, + 0xc9, 0xf7, 0x4d, 0x2d, 0x2e, 0x4e, 0x4c, 0x4f, 0x2d, 0xd6, 0x03, 0xf3, 0x84, 0x58, 0x4b, 0x52, + 0x8b, 0x4b, 0x8a, 0x95, 0x7c, 0xb8, 0x78, 0x02, 0x40, 0x7c, 0xe7, 0xfc, 0xdc, 0xdc, 0xc4, 0xbc, + 0x14, 0x21, 0x79, 0x2e, 0xee, 0xe2, 0xfc, 0xdc, 0xd4, 0xf8, 0xbc, 0xd2, 0xdc, 0xa4, 0xd4, 0x22, + 0x09, 0x46, 0x05, 0x46, 0x0d, 0xe6, 0x20, 0x2e, 0x90, 0x90, 0x1f, 0x58, 0x44, 0x48, 0x9a, 0x8b, + 0x13, 0xac, 0x20, 0x25, 0xb1, 0x24, 0x51, 0x82, 0x49, 0x81, 0x51, 0x83, 0x33, 0x88, 0x03, 0x24, + 0xe0, 0x92, 0x58, 0x92, 0x98, 0xc4, 0x06, 0x36, 0xdb, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0xe2, + 0x61, 0x85, 0xcd, 0x72, 0x00, 0x00, 0x00, +} diff --git a/tests/protoMessages.proto b/tests/protoMessages.proto new file mode 100644 index 0000000..bddad18 --- /dev/null +++ b/tests/protoMessages.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; +package tests; + +message ProtoCommand { + int64 some_number = 1; + string some_data = 2; +} diff --git a/tests/protoMessagesBase.go b/tests/protoMessagesBase.go new file mode 100644 index 0000000..16462ed --- /dev/null +++ b/tests/protoMessagesBase.go @@ -0,0 +1,5 @@ +package tests + +func (*ProtoCommand) SchemaName() string { + return "ProtoCommand" +} diff --git a/tests/protoSerialization_test.go b/tests/protoSerialization_test.go new file mode 100644 index 0000000..9e14a9c --- /dev/null +++ b/tests/protoSerialization_test.go @@ -0,0 +1,49 @@ +package tests + +import ( + "reflect" + "testing" + + log "github.com/sirupsen/logrus" + "github.com/wework/grabbit/gbus/serialization" +) + +func TestProtoSerialization(t *testing.T) { + + logger := log.WithField("test", "proto_serialization") + serializer := serialization.NewProtoSerializer(logger) + cmd := &ProtoCommand{} + schemaName := cmd.SchemaName() + cmd.SomeNumber = 15 + cmd.SomeData = "rhinof" + encodedBytes, encErr := serializer.Encode(cmd) + + if encErr != nil { + t.Errorf("encoding returned an error: %v", encErr) + } + + //Calling Decode with out first registering the schema should fail and return an error + _, decErr := serializer.Decode(encodedBytes, schemaName) + if decErr == nil { + t.Error("decoding expected to fail but did not return an error") + } + + serializer.Register(cmd) + decodedMsg, noErr := serializer.Decode(encodedBytes, schemaName) + if noErr != nil { + t.Errorf("decoding of message failed with error:%v", noErr) + } + + cmdCopy, ok := decodedMsg.(*ProtoCommand) + + if !ok { + t.Errorf("decoded message was of wrong type. expected:%v actual:%v", reflect.TypeOf(cmd), reflect.TypeOf(decodedMsg)) + } + + if cmdCopy.SomeNumber != cmd.SomeNumber || cmdCopy.SomeData != cmd.SomeData { + log.Infof("expected:%v\n", cmd) + log.Infof("actual:%v\n", cmdCopy) + t.Errorf("decoded message has unexpected or missing data") + } + +} From 9e5407bb675d5070df1155d3ac7a32737bafdd56 Mon Sep 17 00:00:00 2001 From: Guy Baron Date: Thu, 9 May 2019 17:52:04 +0300 Subject: [PATCH 2/4] added tests for protobuf serializer error scenarios --- tests/protoSerialization_test.go | 57 +++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/tests/protoSerialization_test.go b/tests/protoSerialization_test.go index 9e14a9c..f1e4b0b 100644 --- a/tests/protoSerialization_test.go +++ b/tests/protoSerialization_test.go @@ -8,34 +8,46 @@ import ( "github.com/wework/grabbit/gbus/serialization" ) -func TestProtoSerialization(t *testing.T) { +func getProtoCommand() *ProtoCommand { + return &ProtoCommand{ + SomeNumber: 15, + SomeData: "rhinof"} +} +func TestProtoSerialization(t *testing.T) { + // so the tests logs do not get littered with log entries from serlializer + log.SetLevel(log.PanicLevel) + defer log.SetLevel(log.InfoLevel) logger := log.WithField("test", "proto_serialization") + serializer := serialization.NewProtoSerializer(logger) - cmd := &ProtoCommand{} + + name := serializer.Name() + if name != "proto" { + t.Fatalf("incorrect serializer name. expected:proto actual:%s", name) + } + cmd := getProtoCommand() schemaName := cmd.SchemaName() - cmd.SomeNumber = 15 - cmd.SomeData = "rhinof" - encodedBytes, encErr := serializer.Encode(cmd) + encodedBytes, encErr := serializer.Encode(cmd) if encErr != nil { - t.Errorf("encoding returned an error: %v", encErr) + t.Fatalf("encoding returned an error: %v", encErr) } //Calling Decode with out first registering the schema should fail and return an error _, decErr := serializer.Decode(encodedBytes, schemaName) if decErr == nil { - t.Error("decoding expected to fail but did not return an error") + t.Fatalf("decoding expected to fail but did not return an error") } serializer.Register(cmd) + decodedMsg, noErr := serializer.Decode(encodedBytes, schemaName) if noErr != nil { - t.Errorf("decoding of message failed with error:%v", noErr) + t.Fatalf("decoding of message failed with error:%v", noErr) } cmdCopy, ok := decodedMsg.(*ProtoCommand) - if !ok { t.Errorf("decoded message was of wrong type. expected:%v actual:%v", reflect.TypeOf(cmd), reflect.TypeOf(decodedMsg)) } @@ -47,3 +59,30 @@ func TestProtoSerialization(t *testing.T) { } } + +func TestProtoSerializationErrors(t *testing.T) { + // so the tests logs do not get littered with log entries from serlializer + log.SetLevel(log.PanicLevel) + defer log.SetLevel(log.InfoLevel) + logger := log.WithField("test", "proto_serialization") + + serializer := serialization.NewProtoSerializer(logger) + + // test that encoding a non protobuf generated strcut fails and returns an error + _, encErr := serializer.Encode(Command1{}) + if encErr == nil { + t.Errorf("serializer expected to return an error for non proto generated messages but di not") + } + + cmd := getProtoCommand() + encodedBytes, encErr := serializer.Encode(cmd) + if encErr != nil { + t.Fatalf("encoding returned an error: %v", encErr) + } + + //decoding an unregistered schema fails and returns an error + if _, decErr := serializer.Decode(encodedBytes, "kong"); decErr == nil { + t.Errorf("decoding an unregistred schema is expected to return an error but did not") + } + +} From c1040a57f17cb9d8ecce65a3f35782c488064746 Mon Sep 17 00:00:00 2001 From: Guy Baron Date: Thu, 9 May 2019 18:01:07 +0300 Subject: [PATCH 3/4] added more error testing for proto serializer --- tests/protoSerialization_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/protoSerialization_test.go b/tests/protoSerialization_test.go index f1e4b0b..ef5c91e 100644 --- a/tests/protoSerialization_test.go +++ b/tests/protoSerialization_test.go @@ -1,6 +1,7 @@ package tests import ( + "crypto/rand" "reflect" "testing" @@ -85,4 +86,11 @@ func TestProtoSerializationErrors(t *testing.T) { t.Errorf("decoding an unregistred schema is expected to return an error but did not") } + //decoding junk fails and returns an error + junk := make([]byte, 16) + rand.Read(junk) + if _, decErr := serializer.Decode(junk, cmd.SchemaName()); decErr == nil { + t.Errorf("decoding junk is expected to return an error but did not") + } + } From 5490d3d5892f610bf84a4f9f2619a51dfd7ddd1b Mon Sep 17 00:00:00 2001 From: Guy Baron Date: Thu, 9 May 2019 18:10:54 +0300 Subject: [PATCH 4/4] fixed validation in proto serialization error tests --- tests/protoSerialization_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/protoSerialization_test.go b/tests/protoSerialization_test.go index ef5c91e..efe4524 100644 --- a/tests/protoSerialization_test.go +++ b/tests/protoSerialization_test.go @@ -86,6 +86,7 @@ func TestProtoSerializationErrors(t *testing.T) { t.Errorf("decoding an unregistred schema is expected to return an error but did not") } + serializer.Register(cmd) //decoding junk fails and returns an error junk := make([]byte, 16) rand.Read(junk)