add test for partial frame stuffing

This commit is contained in:
saji 2023-05-10 01:12:47 -05:00
parent 2dab308907
commit 1c51a6da90
2 changed files with 24 additions and 0 deletions

View file

@ -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

View file

@ -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