From 95a0d28436e235721395028de83fc42e46d9b855 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Sat, 31 Aug 2019 11:05:44 +0300 Subject: [PATCH] Add HSFrame tests --- pkg/app2/hsframe_test.go | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 pkg/app2/hsframe_test.go diff --git a/pkg/app2/hsframe_test.go b/pkg/app2/hsframe_test.go new file mode 100644 index 0000000000..ab66e376f9 --- /dev/null +++ b/pkg/app2/hsframe_test.go @@ -0,0 +1,69 @@ +package app2 + +import ( + "encoding/binary" + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestHSFrame(t *testing.T) { + t.Run("ok", func(t *testing.T) { + body := struct { + Test string `json:"test"` + }{ + Test: "some string", + } + + bodyBytes, err := json.Marshal(body) + require.NoError(t, err) + + procID := ProcID(1) + frameType := HSFrameTypeDMSGListen + bodyLen := len(bodyBytes) + + hsFrame, err := NewHSFrame(procID, frameType, body) + require.NoError(t, err) + + require.Equal(t, len(hsFrame), HSFrameHeaderLen+len(bodyBytes)) + + gotProcID := ProcID(binary.BigEndian.Uint16(hsFrame)) + require.Equal(t, gotProcID, procID) + + gotFrameType := HSFrameType(hsFrame[HSFrameProcIDLen]) + require.Equal(t, gotFrameType, frameType) + + gotBodyLen := int(binary.BigEndian.Uint16(hsFrame[HSFrameProcIDLen+HSFrameTypeLen:])) + require.Equal(t, gotBodyLen, bodyLen) + + require.Equal(t, bodyBytes, []byte(hsFrame[HSFrameProcIDLen+HSFrameTypeLen+HSFrameBodyLenLen:])) + + gotProcID = hsFrame.ProcID() + require.Equal(t, gotProcID, procID) + + gotFrameType = hsFrame.FrameType() + require.Equal(t, gotFrameType, frameType) + + gotBodyLen = hsFrame.BodyLen() + require.Equal(t, gotBodyLen, bodyLen) + }) + + t.Run("fail - too large body", func(t *testing.T) { + body := struct { + Test string `json:"test"` + }{ + Test: "some string", + } + + for len(body.Test) <= HSFrameMaxBodyLen { + body.Test += body.Test + } + + procID := ProcID(1) + frameType := HSFrameTypeDMSGListen + + _, err := NewHSFrame(procID, frameType, body) + require.Equal(t, err, ErrHSFrameBodyTooLarge) + }) +}