forked from BertoldVdb/go-ais
-
Notifications
You must be signed in to change notification settings - Fork 2
/
encode_fail_test.go
191 lines (148 loc) · 3.75 KB
/
encode_fail_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
package ais
import "testing"
func tryEncodeTooLong(len int) bool {
x := CodecNew(false, false)
packet := SingleSlotBinaryMessage{
Valid: true,
DestinationIDValid: false,
ApplicationIDValid: false,
}
packet.Header = Header{
MessageID: 25,
UserID: 1337}
packet.Payload = make([]byte, len)
return x.EncodePacket(packet) != nil
}
func TestEncodeFailTooLong(t *testing.T) {
if tryEncodeTooLong(129) {
t.Error("Could encode overlength packet")
}
if !tryEncodeTooLong(128) {
t.Error("Acceptable length packet rejected")
}
}
func tryEncodeWithString(s string) bool {
x := CodecNew(false, false)
packet := SafetyBroadcastMessage{
Valid: true,
Text: s,
}
packet.Header = Header{
MessageID: 14,
UserID: 1337}
return x.EncodePacket(packet) != nil
}
func TestEncodeFailIllegalChar(t *testing.T) {
if tryEncodeWithString("ILLeGAL") {
t.Error("Could encode string with illegal char")
}
if !tryEncodeWithString("LEGAL") {
t.Error("Could not encode valid string")
}
}
func TestEncodeNumberTooLarge(t *testing.T) {
_, ok := encodeNumber([]byte{}, false, 8, 256)
if ok {
t.Error("Could encode 256 in 8 bits")
}
_, ok = encodeNumber([]byte{}, false, 8, 255)
if !ok {
t.Error("Could not encode 255 in 8 bits")
}
_, ok = encodeNumber([]byte{}, true, 8, 128)
if ok {
t.Error("Could encode 128 in 8 bits signed")
}
_, ok = encodeNumber([]byte{}, true, 8, 127)
if !ok {
t.Error("Could not encode 127 in 8 bits signed")
}
_, ok = encodeNumber([]byte{}, true, 8, -128)
if !ok {
t.Error("Could not encode -128 in 8 bits signed")
}
_, ok = encodeNumber([]byte{}, true, 8, -129)
if ok {
t.Error("Could encode -129 in 8 bits signed")
}
}
func TestEncodeFailNoUsefulData(t *testing.T) {
x := CodecNew(false, false)
packet := BinaryAcknowledge{
Valid: true,
}
packet.Header = Header{
MessageID: 7,
UserID: 1337}
if x.EncodePacket(packet) != nil {
t.Error("Could encode packet that contained no data")
}
/* Add some data */
packet.Destinations[0].Valid = true
if x.EncodePacket(packet) == nil {
t.Error("Could not encode packet although data was added")
}
}
func tryEncodeWithMessageID(mID uint8) bool {
x := CodecNew(false, false)
packet := PositionReport{
Valid: true,
}
packet.Header = Header{
MessageID: mID,
UserID: 1337}
return x.EncodePacket(packet) != nil
}
func TestEncodeFailWrongMsgID(t *testing.T) {
if !tryEncodeWithMessageID(1) {
t.Error("Could not encode position report with msgID==1")
}
if !tryEncodeWithMessageID(2) {
t.Error("Could not encode position report with msgID==2")
}
if !tryEncodeWithMessageID(3) {
t.Error("Could not encode position report with msgID==3")
}
if tryEncodeWithMessageID(4) {
t.Error("Could encode position report with msgID==4")
}
if tryEncodeWithMessageID(0) {
t.Error("Could encode position report with msgID==0")
}
if tryEncodeWithMessageID(28) {
t.Error("Could encode position report with msgID==28")
}
}
func tryEncodeTooLargeNumber(number uint16) bool {
x := CodecNew(false, false)
packet := ShipStaticData{
Valid: true,
}
packet.Header = Header{
MessageID: 5,
UserID: 1337}
packet.Dimension.A = number
return x.EncodePacket(packet) != nil
}
func TestEncodeTooLargeNumber(t *testing.T) {
if !tryEncodeTooLargeNumber(1) {
t.Error("Could not encode ShipStaticData with DimensionA=1")
}
if tryEncodeTooLargeNumber(65535) {
t.Error("Could encode ShipStaticData with DimensionA=65535")
}
}
func TestEncodeFloatOutOfRange(t *testing.T) {
staticData := ShipStaticData{
Valid: true,
MaximumStaticDraught: 1000,
}
staticData.Header = Header{
MessageID: 5,
UserID: 1337,
}
x := CodecNew(false, false)
if x.EncodePacket(staticData) != nil {
t.Error("Encoded oversized float")
}
}