fix off by one in api_frame and add more tests

This commit is contained in:
saji 2023-05-03 14:00:26 -05:00
parent 9c21a330e8
commit 771ac243ac
2 changed files with 59 additions and 1 deletions

View file

@ -136,7 +136,7 @@ func parseFrame(frame []byte) ([]byte, error) {
return nil, errors.New("incorrect start delimiter") return nil, errors.New("incorrect start delimiter")
} }
fsize := len(frame) fsize := len(frame)
if calculateChecksum(frame[3:fsize-1]) != frame[fsize] { if calculateChecksum(frame[3:fsize-1]) != frame[fsize-1] {
return nil, errors.New("checksum mismatch") return nil, errors.New("checksum mismatch")
} }
return frame[3 : fsize-1], nil return frame[3 : fsize-1], nil

View file

@ -1,6 +1,7 @@
package xbee package xbee
import ( import (
"bytes"
"reflect" "reflect"
"testing" "testing"
) )
@ -28,6 +29,16 @@ func Test_xbeeFrameSplit(t *testing.T) {
wantToken: nil, wantToken: nil,
wantErr: false, wantErr: false,
}, },
{
name: "EOF and empty data",
args: args{
data: []byte{},
atEOF: true,
},
wantAdvance: 0,
wantToken: nil,
wantErr: false,
},
{ {
name: "no start delimiter", name: "no start delimiter",
args: args{ args: args{
@ -125,6 +136,22 @@ func Test_parseFrame(t *testing.T) {
want: nil, want: nil,
wantErr: true, wantErr: true,
}, },
{
name: "checksum mismatch",
args: args{
frame: []byte{0x7E, 0x00, 0x02, 0x23, 0x11, 0xCA},
},
want: nil,
wantErr: true,
},
{
name: "valid packet",
args: args{
frame: []byte{0x7E, 0x00, 0x02, 0x23, 0x11, 0xCB},
},
want: []byte{0x23, 0x11},
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) {
@ -139,3 +166,34 @@ func Test_parseFrame(t *testing.T) {
}) })
} }
} }
func Test_writeXBeeFrame(t *testing.T) {
type args struct {
data []byte
}
tests := []struct {
name string
args args
wantN int
wantW string
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
gotN, err := writeXBeeFrame(w, tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("writeXBeeFrame() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotN != tt.wantN {
t.Errorf("writeXBeeFrame() = %v, want %v", gotN, tt.wantN)
}
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("writeXBeeFrame() = %v, want %v", gotW, tt.wantW)
}
})
}
}