From 0f731471ab258d9e01658f0ab70f8317fd0fb89c Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Wed, 12 Sep 2018 17:38:36 +0200 Subject: Recover possible panic in EncodeMsg and improve debug logs Change-Id: I771c171ae30a957f4436e7f4ba834d8a38d02f80 Signed-off-by: Ondrej Fabry --- codec/msg_codec_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 codec/msg_codec_test.go (limited to 'codec/msg_codec_test.go') diff --git a/codec/msg_codec_test.go b/codec/msg_codec_test.go new file mode 100644 index 0000000..cd1240e --- /dev/null +++ b/codec/msg_codec_test.go @@ -0,0 +1,63 @@ +package codec + +import ( + "bytes" + "testing" + + "git.fd.io/govpp.git/api" +) + +type MyMsg struct { + Index uint16 + Label []byte `struc:"[16]byte"` + Port uint16 +} + +func (*MyMsg) GetMessageName() string { + return "my_msg" +} +func (*MyMsg) GetCrcString() string { + return "xxxxx" +} +func (*MyMsg) GetMessageType() api.MessageType { + return api.OtherMessage +} + +func TestEncode(t *testing.T) { + tests := []struct { + name string + msg api.Message + msgID uint16 + expData []byte + }{ + {name: "basic", + msg: &MyMsg{Index: 1, Label: []byte("Abcdef"), Port: 1000}, + msgID: 100, + expData: []byte{0x00, 0x64, 0x00, 0x01, 0x41, 0x62, 0x63, 0x64, 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE8}, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + c := &MsgCodec{} + + data, err := c.EncodeMsg(test.msg, test.msgID) + if err != nil { + t.Fatalf("expected nil error, got: %v", err) + } + if !bytes.Equal(data, test.expData) { + t.Fatalf("expected data: % 0X, got: % 0X", test.expData, data) + } + }) + } +} + +func TestEncodePanic(t *testing.T) { + c := &MsgCodec{} + + msg := &MyMsg{Index: 1, Label: []byte("thisIsLongerThan16Bytes"), Port: 1000} + + _, err := c.EncodeMsg(msg, 100) + if err == nil { + t.Fatalf("expected non-nil error, got: %v", err) + } +} -- cgit 1.2.3-korg