-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathreencode_test.go
337 lines (280 loc) · 6.83 KB
/
reencode_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package ais
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"testing"
)
func binToString(bin []byte) string {
var out string
for _, m := range bin {
if m > 0 {
out += "1"
} else {
out += "0"
}
}
return out
}
func tryFile(t *testing.T, x *Codec, msgID int) {
f, err := os.Open(fmt.Sprintf("testmsg/%d.msg", msgID))
if err != nil {
t.Error("Failed to open file", msgID)
return
}
defer f.Close()
r := bufio.NewReader(f)
for index := 0; true; index++ {
line, err := r.ReadString('\n')
if err != nil {
break
}
line = line[:len(line)-2]
source := []byte(line)
/* Convert ascii '0' and '1' to real 0 and 1 */
for i := 0; i < len(source); i++ {
source[i] -= '0'
}
/* Decode the packet */
decoded := x.DecodePacket(source)
if decoded == nil {
/* Failed to decode... */
t.Error("Could not decode", msgID, index)
continue
}
encoded := x.EncodePacket(decoded)
/* Check if the bitstream is identical */
if len(encoded) < len(source) {
/* Output is too short */
t.Error("Output too short", msgID, index, len(encoded), len(source))
continue
}
if !bytes.Equal(encoded[:len(source)], source) {
t.Error("Bitstream does not match", msgID, index, binToString(source), binToString(encoded[:len(source)]))
t.Errorf("%+v", decoded)
}
}
}
func TestReEncode(t *testing.T) {
x := CodecNew(false, false)
/* Convenience conversion disabled to avoid float inaccuracies */
x.FloatWithoutConversion = true
/* Test all message types */
for i := 1; i <= 27; i++ {
log.Println("Working on message type", i)
tryFile(t, x, i)
}
}
func checkFloat(a float64, b float64) bool {
diff := a - b
if diff < 0 {
diff = -diff
}
return diff < 0.002
}
func TestFloatReEncoder(t *testing.T) {
x := CodecNew(true, true)
x.DecoderCheckFixedValues = true
packet := PositionReport{
Valid: true,
Longitude: 12.345,
Latitude: 1.2345,
Cog: 123.2,
}
packet.Header = Header{
MessageID: 2,
UserID: 1337}
encoded := x.EncodePacket(packet)
if encoded == nil {
t.Error("Failed to encode position report")
return
}
switch newPacket := x.DecodePacket(encoded).(type) {
case PositionReport:
if !checkFloat(float64(packet.Latitude), float64(newPacket.Latitude)) ||
!checkFloat(float64(packet.Longitude), float64(newPacket.Longitude)) ||
!checkFloat(float64(packet.Cog), float64(newPacket.Cog)) {
t.Error("Packet has a floating point error")
}
default:
t.Error("Packet was returned as a different type")
}
packet2 := LongRangeAisBroadcastMessage{
Valid: true,
Longitude: 12.345,
Latitude: 1.2345,
}
packet2.Header = Header{
MessageID: 27,
UserID: 1337}
encoded2 := x.EncodePacket(packet2)
if encoded2 == nil {
t.Error("Failed to encode position report")
return
}
switch newPacket := x.DecodePacket(encoded2).(type) {
case LongRangeAisBroadcastMessage:
if !checkFloat(float64(packet.Latitude), float64(newPacket.Latitude)) ||
!checkFloat(float64(packet.Longitude), float64(newPacket.Longitude)) {
t.Error("Packet has a floating point error")
}
default:
t.Error("Packet was returned as a different type")
}
}
func testInterfacecInternal(p Packet, t *testing.T) (bool, int, uint32) {
x := CodecNew(false, false)
encoded := x.EncodePacket(p)
if encoded == nil {
t.Error("Failed to encode packet")
}
decoded := x.DecodePacket(encoded)
if decoded.GetHeader().MessageID != p.GetHeader().MessageID {
t.Error("GetHeader failed")
}
switch newPacket := decoded.(type) {
case HasCommunicationState:
return true, newPacket.IsItdma(), newPacket.GetState()
default:
return false, 0, 0
}
}
func TestInterfaceAccess(t *testing.T) {
packet := MultiSlotBinaryMessage{
Valid: true,
}
packet.Header = Header{MessageID: 26}
packet.CommunicationStateIsItdma = true
packet.CommunicationState = 0x1234
a, b, c := testInterfacecInternal(packet, t)
if !a {
t.Error("Comm state not found")
}
if b != 1 || c != 0x1234 {
t.Error("Comm state decoding error", b, c)
}
packet.CommunicationStateIsItdma = false
packet.CommunicationState = 0xCAFE
a, b, c = testInterfacecInternal(packet, t)
if !a {
t.Error("Comm state not found")
}
if b != 0 || c != 0xCAFE {
t.Error("Comm state decoding error", b, c)
}
packet2 := PositionReport{
Valid: true,
}
packet2.Header = Header{MessageID: 1}
packet2.CommunicationState = 0x4321
a, b, c = testInterfacecInternal(packet2, t)
if !a {
t.Error("Comm state not found")
}
if b != -1 || c != 0x4321 {
t.Error("Comm state decoding error", b, c)
}
}
func testEmptyArray(t *testing.T, slotValid bool) bool {
x := CodecNew(true, true)
packet := BinaryAcknowledge{
Valid: true,
}
packet.Header = Header{
MessageID: 7,
UserID: 1337}
packet.Destinations[0].Valid = true
encoded := x.EncodePacket(packet)
if encoded == nil {
t.Error("Failed to encode BinaryAcknowledge")
return false
}
/* Truncate message to remove the payload */
if !slotValid {
encoded = encoded[:40]
}
decoded := x.DecodePacket(encoded)
if decoded == nil {
return false
}
switch decoded.(type) {
case BinaryAcknowledge:
return true
default:
t.Error("Packet was returned as a different type")
return false
}
}
func TestEmptyPayloadInArrayMessages(t *testing.T) {
if testEmptyArray(t, false) {
t.Error("Could decode empty acknowledge")
}
if !testEmptyArray(t, true) {
t.Error("Could not decode non-empty acknowledge")
}
}
func testValidateFailCorrupt(t *testing.T, corruptSpare bool, packetType bool) bool {
x := CodecNew(true, true)
x.DecoderCheckFixedValues = true
if !packetType {
packet := StaticDataReport{
Valid: true,
PartNumber: true,
}
packet.Header = Header{
MessageID: 24,
UserID: 1337}
packet.ReportB.Valid = true
encoded := x.EncodePacket(packet)
if encoded == nil {
t.Error("Failed to encode StaticDataReport")
return false
}
if corruptSpare {
encoded[167] = 1
}
decoded := x.DecodePacket(encoded)
if decoded == nil {
return false
}
} else {
packet2 := Interrogation{
Valid: true,
}
packet2.Header = Header{
MessageID: 15,
UserID: 1337}
packet2.Station1Msg1.Valid = true
packet2.Station2.Valid = true
packet2.Station2.MessageID = 2
encoded2 := x.EncodePacket(packet2)
if encoded2 == nil {
t.Error("Failed to encode Interrogation")
return false
}
if corruptSpare {
encoded2[88] = 1
}
decoded2 := x.DecodePacket(encoded2)
if decoded2 == nil {
return false
}
}
return true
}
func TestValidateFailCorrupt(t *testing.T) {
if testValidateFailCorrupt(t, true, false) {
t.Error("Could decode corrupted packet")
}
if !testValidateFailCorrupt(t, false, false) {
t.Error("Could not decode non-corrupt packet")
}
if testValidateFailCorrupt(t, true, true) {
t.Error("Could decode corrupted packet")
}
if !testValidateFailCorrupt(t, false, true) {
t.Error("Could not decode non-corrupt packet")
}
}