From 1c51a6da9063e38855be3559bacebd9965198863 Mon Sep 17 00:00:00 2001 From: saji <9110284+kschamplin@users.noreply.github.com> Date: Wed, 10 May 2023 01:12:47 -0500 Subject: [PATCH] add test for partial frame stuffing --- xbee/api_frame.go | 9 +++++++++ xbee/api_frame_test.go | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/xbee/api_frame.go b/xbee/api_frame.go index 0243603..e7e6ac8 100644 --- a/xbee/api_frame.go +++ b/xbee/api_frame.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "errors" "io" + ) // Frameable is an object that can be sent in an XBee Frame. An XBee Frame @@ -103,12 +104,20 @@ func xbeeFrameSplit(data []byte, atEOF bool) (advance int, token []byte, err err } // FIXME: add bounds checking! this can panic. var frameLen = int(binary.BigEndian.Uint16(data[startIdx+1:startIdx+3])) + 4 + + + // if the value of frameLen is > 0x100, we know that it's screwed up. + // this helps keep error duration lowered. + if frameLen > 0x100 { + return startIdx + 1, nil, nil + } if len(data[startIdx:]) < frameLen { // we got the length, but there's not enough data for the frame. we can trim the // data that came before the start, but not return a token. return startIdx, nil, nil } // there is enough data to pull a frame, so return it. + // todo: is startDix + frameLen + 1? return startIdx + frameLen, data[startIdx : startIdx+frameLen], nil } // we didn't find a start character in our data, so request more. trash everythign given to us diff --git a/xbee/api_frame_test.go b/xbee/api_frame_test.go index 482b47a..8d6e7d6 100644 --- a/xbee/api_frame_test.go +++ b/xbee/api_frame_test.go @@ -2,11 +2,13 @@ package xbee import ( "bytes" + "encoding/hex" "reflect" "testing" ) func Test_xbeeFrameSplit(t *testing.T) { + advTest, _ := hex.DecodeString("007E7E0012900013A20041B320") type args struct { data []byte atEOF bool @@ -98,6 +100,17 @@ func Test_xbeeFrameSplit(t *testing.T) { wantToken: nil, wantErr: false, }, + { + name: "start delimiter inside partial packet", + args: args{ + data: advTest, + atEOF: false, + }, + wantAdvance: 2, + wantToken: nil, + wantErr: false, + }, + } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -116,6 +129,8 @@ func Test_xbeeFrameSplit(t *testing.T) { } } + + func Test_parseFrame(t *testing.T) { type args struct { frame []byte