-
Notifications
You must be signed in to change notification settings - Fork 42
/
header_test.go
103 lines (99 loc) · 2.31 KB
/
header_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package fit
import (
"bytes"
"errors"
"testing"
"github.com/tormoder/fit/dyncrc16"
)
var decodeHeaderTests = []struct {
in []byte
err error
h Header
}{
{[]byte{11}, errHeaderSize, Header{}},
{[]byte{12}, errReadData, Header{}},
{[]byte{13}, errHeaderSize, Header{}},
{[]byte{14}, errReadData, Header{}},
{[]byte{15}, errHeaderSize, Header{}},
{[]byte{12, 0}, errReadData, Header{}},
{[]byte{14, 0}, errReadData, Header{}},
{
[]byte{14, 0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
NotSupportedError("protocol version 3.0 not supported by sdk protocol version 2.0"),
Header{},
},
{[]byte{14, 0, 0, 0, 0, 0, 0, 0, '.', 'G', 'I', 'T', 0, 0}, errNotFit, Header{}},
{
[]byte{14, 0, 0, 0, 0, 0, 0, 0, '.', 'F', 'I', 'T', 0, 0},
nil,
Header{
Size: 14,
DataType: [4]byte{'.', 'F', 'I', 'T'},
},
},
{
[]byte{14, 0x10, 0, 0, 0, 0, 0, 0, '.', 'F', 'I', 'T', 0, 0},
nil,
Header{
Size: 14,
ProtocolVersion: 0x10,
DataType: [4]byte{'.', 'F', 'I', 'T'},
},
},
{
[]byte{14, 0x10, 0x57, 0x04, 0, 0, 0, 0, '.', 'F', 'I', 'T', 0, 0},
nil,
Header{
Size: 14,
ProtocolVersion: 0x10,
ProfileVersion: 1111,
DataType: [4]byte{'.', 'F', 'I', 'T'},
},
},
{
[]byte{14, 0x10, 0x57, 0x04, 0x63, 0x6f, 0x01, 0x00, '.', 'F', 'I', 'T', 0, 0},
nil,
Header{
Size: 14,
ProtocolVersion: 0x10,
ProfileVersion: 1111,
DataSize: 94051,
DataType: [4]byte{'.', 'F', 'I', 'T'},
},
},
{
[]byte{14, 0x10, 0x57, 0x04, 0x63, 0x6f, 0x01, 0x00, '.', 'F', 'I', 'T', 0x3b, 0x34},
nil,
Header{
Size: 14,
ProtocolVersion: 0x10,
ProfileVersion: 1111,
DataSize: 94051,
DataType: [4]byte{'.', 'F', 'I', 'T'},
CRC: 13371,
},
},
{
[]byte{14, 0x10, 0x57, 0x04, 0x63, 0x6f, 0x01, 0x00, '.', 'F', 'I', 'T', 0x3b, 0x35},
errHdrCRC,
Header{},
},
}
func TestDecodeHeader(t *testing.T) {
for i, dht := range decodeHeaderTests {
var dec decoder
dec.r = bytes.NewReader(dht.in)
dec.crc = dyncrc16.New()
err := dec.decodeHeader()
if !errors.Is(err, dht.err) {
t.Errorf("%d: got error: %v, want: %v", i, err, dht.err)
continue
}
if dht.err != nil {
continue
}
if dec.h != dht.h {
t.Errorf("%d:\ngot header:\n%v\nwant header\n%v", i, dec.h, dht.h)
}
}
}