forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
264 lines (243 loc) · 5.29 KB
/
writer_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package frame
import (
"bytes"
"encoding/binary"
"testing"
"time"
"bou.ke/monkey"
"github.com/stretchr/testify/require"
"github.com/bluenviron/gomavlib/v2/pkg/dialect"
"github.com/bluenviron/gomavlib/v2/pkg/message"
)
func TestWriterNewErrors(t *testing.T) {
_, err := NewWriter(WriterConf{
OutVersion: V2,
OutSystemID: 1,
})
require.EqualError(t, err, "Writer not provided")
var buf bytes.Buffer
_, err = NewWriter(WriterConf{
Writer: &buf,
OutSystemID: 1,
})
require.EqualError(t, err, "OutVersion not provided")
_, err = NewWriter(WriterConf{
Writer: &buf,
OutVersion: V2,
})
require.EqualError(t, err, "OutSystemID must be greater than one")
_, err = NewWriter(WriterConf{
Writer: &buf,
OutVersion: V1,
OutSystemID: 1,
OutKey: NewV2Key(bytes.Repeat([]byte("\x4F"), 32)),
})
require.EqualError(t, err, "OutKey requires V2 frames")
}
func TestWriterWriteFrame(t *testing.T) {
for _, ca := range casesReadWrite {
t.Run(ca.name, func(t *testing.T) {
var buf bytes.Buffer
writer, err := NewWriter(WriterConf{
Writer: &buf,
OutVersion: V2,
OutSystemID: 1,
DialectRW: ca.dialectRW,
})
require.NoError(t, err)
err = writer.WriteFrame(ca.frame)
require.NoError(t, err)
require.Equal(t, ca.raw, buf.Bytes())
})
}
}
func TestWriterWriteFrameErrors(t *testing.T) {
for _, ca := range []struct {
name string
dialectRW *dialect.ReadWriter
frame Frame
err string
}{
{
"nil message",
nil,
&V1Frame{
SequenceID: 0x01,
SystemID: 0x02,
ComponentID: 0x03,
Message: nil,
Checksum: 0x0807,
},
"message is nil",
},
{
"nil dialect",
nil,
&V1Frame{
SequenceID: 0x27,
SystemID: 0x01,
ComponentID: 0x02,
Message: &MessageTest5{
'\x10',
binary.LittleEndian.Uint32([]byte("\x10\x10\x10\x10")),
},
Checksum: 0x66e5,
},
"dialect is nil",
},
{
"not in dialect",
testDialectRW,
&V1Frame{
SequenceID: 0x27,
SystemID: 0x01,
ComponentID: 0x02,
Message: &MessageTest8{15, 7},
Checksum: 0x66e5,
},
"message is not in the dialect",
},
{
"frame encode error",
testDialectRW,
&V1Frame{
SequenceID: 0x27,
SystemID: 0x01,
ComponentID: 0x02,
Message: &MessageTest9{},
Checksum: 0x66e5,
},
"cannot send a message with an ID greater than 255 with a V1 frame",
},
} {
t.Run(ca.name, func(t *testing.T) {
var buf bytes.Buffer
writer, err := NewWriter(WriterConf{
Writer: &buf,
OutVersion: V2,
OutSystemID: 1,
DialectRW: ca.dialectRW,
})
require.NoError(t, err)
err = writer.WriteFrame(ca.frame)
require.EqualError(t, err, ca.err)
})
}
}
func TestWriterWriteMessage(t *testing.T) {
// fake current time in order to obtain deterministic signatures
wayback := time.Date(2019, time.May, 18, 1, 2, 3, 4, time.UTC)
patch := monkey.Patch(time.Now, func() time.Time { return wayback })
defer patch.Unpatch()
for _, c := range []struct {
name string
ver WriterOutVersion
key *V2Key
msg message.Message
raw []byte
}{
{
"v1 frame",
V1,
nil,
&MessageTest5{
'\x10',
binary.LittleEndian.Uint32([]byte("\x10\x10\x10\x10")),
},
[]byte("\xFE\x05\x00\x01\x01\x05\x10\x10\x10\x10\x10\x75\x84"),
},
{
"v2 frame, signed",
V2,
NewV2Key(bytes.Repeat([]byte("\x4F"), 32)),
&MessageHeartbeat{
Type: 1,
Autopilot: 2,
BaseMode: 3,
CustomMode: 4,
SystemStatus: 5,
MavlinkVersion: 3,
},
[]byte("\xFD\x09\x01\x00\x00\x01\x01\x00" +
"\x00\x00\x04\x00\x00\x00\x01\x02" +
"\x03\x05\x03\x19\xe7\x00\xe0\xf8" +
"\xd4\xb6\x8e\x0c\xe7\x5d\x07\x46" +
"\x81\xd4"),
},
} {
t.Run(c.name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
writer, err := NewWriter(WriterConf{
Writer: buf,
DialectRW: testDialectRW,
OutVersion: c.ver,
OutSystemID: 1,
OutKey: c.key,
})
require.NoError(t, err)
err = writer.WriteMessage(c.msg)
require.NoError(t, err)
require.Equal(t, c.raw, buf.Bytes())
buf.Next(len(c.raw))
})
}
}
type MessageTest10 struct {
TestByte1 byte
TestUint1 uint32
}
func (m *MessageTest10) GetID() uint32 {
return 301
}
func TestWriterWriteMessageErrors(t *testing.T) {
for _, ca := range []struct {
name string
dialectRW *dialect.ReadWriter
message message.Message
err string
}{
{
"nil message",
nil,
nil,
"message is nil",
},
{
"nil dialect",
nil,
&MessageTest8{15, 7},
"dialect is nil",
},
{
"not in dialect",
testDialectRW,
&MessageTest10{15, 7},
"message is not in the dialect",
},
} {
t.Run(ca.name, func(t *testing.T) {
var buf bytes.Buffer
writer, err := NewWriter(WriterConf{
Writer: &buf,
OutVersion: V2,
OutSystemID: 1,
DialectRW: ca.dialectRW,
})
require.NoError(t, err)
err = writer.WriteMessage(ca.message)
require.EqualError(t, err, ca.err)
})
}
}
func TestWriterWriteFrameNilMsg(t *testing.T) {
writer, err := NewWriter(WriterConf{
Writer: bytes.NewBuffer(nil),
DialectRW: nil,
OutVersion: V2,
OutSystemID: 1,
})
require.NoError(t, err)
f := &V1Frame{Message: nil}
err = writer.WriteFrame(f)
require.Error(t, err)
}