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..efe4524 --- /dev/null +++ b/tests/protoSerialization_test.go @@ -0,0 +1,97 @@ +package tests + +import ( + "crypto/rand" + "reflect" + "testing" + + log "github.com/sirupsen/logrus" + "github.com/wework/grabbit/gbus/serialization" +) + +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) + + name := serializer.Name() + if name != "proto" { + t.Fatalf("incorrect serializer name. expected:proto actual:%s", name) + } + cmd := getProtoCommand() + schemaName := cmd.SchemaName() + + encodedBytes, encErr := serializer.Encode(cmd) + if encErr != nil { + 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.Fatalf("decoding expected to fail but did not return an error") + } + + serializer.Register(cmd) + + decodedMsg, noErr := serializer.Decode(encodedBytes, schemaName) + if noErr != nil { + 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)) + } + + 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") + } + +} + +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") + } + + serializer.Register(cmd) + //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") + } + +}