add test for partial frame stuffing
This commit is contained in:
parent
2dab308907
commit
1c51a6da90
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Frameable is an object that can be sent in an XBee Frame. An XBee Frame
|
// 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.
|
// FIXME: add bounds checking! this can panic.
|
||||||
var frameLen = int(binary.BigEndian.Uint16(data[startIdx+1:startIdx+3])) + 4
|
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 {
|
if len(data[startIdx:]) < frameLen {
|
||||||
// we got the length, but there's not enough data for the frame. we can trim the
|
// 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.
|
// data that came before the start, but not return a token.
|
||||||
return startIdx, nil, nil
|
return startIdx, nil, nil
|
||||||
}
|
}
|
||||||
// there is enough data to pull a frame, so return it.
|
// there is enough data to pull a frame, so return it.
|
||||||
|
// todo: is startDix + frameLen + 1?
|
||||||
return startIdx + frameLen, data[startIdx : startIdx+frameLen], nil
|
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
|
// we didn't find a start character in our data, so request more. trash everythign given to us
|
||||||
|
|
|
@ -2,11 +2,13 @@ package xbee
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_xbeeFrameSplit(t *testing.T) {
|
func Test_xbeeFrameSplit(t *testing.T) {
|
||||||
|
advTest, _ := hex.DecodeString("007E7E0012900013A20041B320")
|
||||||
type args struct {
|
type args struct {
|
||||||
data []byte
|
data []byte
|
||||||
atEOF bool
|
atEOF bool
|
||||||
|
@ -98,6 +100,17 @@ func Test_xbeeFrameSplit(t *testing.T) {
|
||||||
wantToken: nil,
|
wantToken: nil,
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "start delimiter inside partial packet",
|
||||||
|
args: args{
|
||||||
|
data: advTest,
|
||||||
|
atEOF: false,
|
||||||
|
},
|
||||||
|
wantAdvance: 2,
|
||||||
|
wantToken: nil,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
@ -116,6 +129,8 @@ func Test_xbeeFrameSplit(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func Test_parseFrame(t *testing.T) {
|
func Test_parseFrame(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
frame []byte
|
frame []byte
|
||||||
|
|
Loading…
Reference in a new issue