gotelem/internal/xbee/api_frame_test.go

142 lines
2.9 KiB
Go
Raw Normal View History

2023-04-20 20:26:29 +00:00
package xbee
import (
"reflect"
"testing"
)
func Test_xbeeFrameSplit(t *testing.T) {
type args struct {
data []byte
atEOF bool
}
tests := []struct {
name string
args args
wantAdvance int
wantToken []byte
wantErr bool
}{
// TODO: Add test cases.
{
name: "empty data",
args: args{
data: []byte{},
atEOF: false,
},
wantAdvance: 0,
wantToken: nil,
wantErr: false,
},
{
name: "no start delimiter",
args: args{
data: []byte{0x11, 0x22, 0x23, 0x44, 0x44, 0x77, 0x33},
atEOF: false,
},
wantAdvance: 7,
wantToken: nil,
wantErr: false,
},
{
name: "incomplete packet",
args: args{
data: []byte{0x7E, 0x00, 0x02, 0x23, 0x11},
atEOF: false,
},
wantAdvance: 0,
wantToken: nil,
wantErr: false,
},
{
name: "valid packet",
args: args{
data: []byte{0x7E, 0x00, 0x02, 0x23, 0x11, 0xCB},
atEOF: false,
},
wantAdvance: 6,
wantToken: []byte{0x7E, 0x00, 0x02, 0x23, 0x11, 0xCB},
wantErr: false,
},
{
name: "valid packet w/ padding",
args: args{
data: []byte{0x00, 0x7E, 0x00, 0x02, 0x23, 0x11, 0xCB, 0x00},
atEOF: false,
},
wantAdvance: 7,
wantToken: []byte{0x7E, 0x00, 0x02, 0x23, 0x11, 0xCB},
wantErr: false,
},
2023-04-26 22:24:11 +00:00
{
name: "trailing start delimiter",
args: args{
data: []byte{0x53, 0x00, 0x02, 0x23, 0x11, 0x7E},
atEOF: false,
},
wantAdvance: 5,
wantToken: nil,
wantErr: false,
},
{
name: "incomplete length value",
args: args{
data: []byte{0x53, 0x00, 0x02, 0x23, 0x11, 0x7E, 0x00},
atEOF: false,
},
wantAdvance: 5,
wantToken: nil,
wantErr: false,
},
2023-04-20 20:26:29 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotAdvance, gotToken, err := xbeeFrameSplit(tt.args.data, tt.args.atEOF)
if (err != nil) != tt.wantErr {
t.Errorf("xbeeFrameSplit() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotAdvance != tt.wantAdvance {
t.Errorf("xbeeFrameSplit() gotAdvance = %v, want %v", gotAdvance, tt.wantAdvance)
}
if !reflect.DeepEqual(gotToken, tt.wantToken) {
t.Errorf("xbeeFrameSplit() gotToken = %v, want %v", gotToken, tt.wantToken)
}
})
}
}
2023-04-29 15:58:56 +00:00
func Test_parseFrame(t *testing.T) {
2023-04-20 20:26:29 +00:00
type args struct {
2023-04-29 15:58:56 +00:00
frame []byte
2023-04-20 20:26:29 +00:00
}
tests := []struct {
name string
args args
2023-04-29 15:58:56 +00:00
want []byte
2023-04-20 20:26:29 +00:00
wantErr bool
}{
// TODO: Add test cases.
2023-04-29 15:58:56 +00:00
{
name: "missing start delimiter",
args: args{
frame: []byte{0x00, 0x02, 0x03, 0x00, 0x3},
},
want: nil,
wantErr: true,
},
2023-04-20 20:26:29 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-04-29 15:58:56 +00:00
got, err := parseFrame(tt.args.frame)
2023-04-20 20:26:29 +00:00
if (err != nil) != tt.wantErr {
2023-04-29 15:58:56 +00:00
t.Errorf("parseFrame() error = %v, wantErr %v", err, tt.wantErr)
2023-04-20 20:26:29 +00:00
return
}
2023-04-29 15:58:56 +00:00
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseFrame() = %v, want %v", got, tt.want)
2023-04-20 20:26:29 +00:00
}
})
}
}