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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
package org.traccar;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import org.traccar.helper.DataConverter;
import org.traccar.model.CellTower;
import org.traccar.model.Command;
import org.traccar.model.Position;
import org.traccar.model.WifiAccessPoint;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ProtocolTest extends BaseTest {
protected Position position(String time, boolean valid, double lat, double lon) throws ParseException {
Position position = new Position();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
position.setTime(dateFormat.parse(time));
position.setValid(valid);
position.setLatitude(lat);
position.setLongitude(lon);
return position;
}
private String concatenateStrings(String... strings) {
StringBuilder builder = new StringBuilder();
for (String s : strings) {
builder.append(s);
}
return builder.toString();
}
protected ByteBuf concatenateBuffers(ByteBuf... buffers) {
ByteBuf result = Unpooled.buffer();
for (ByteBuf buf : buffers) {
result.writeBytes(buf);
}
return result;
}
protected ByteBuf binary(String... data) {
return Unpooled.wrappedBuffer(DataConverter.parseHex(concatenateStrings(data)));
}
protected String text(String... data) {
return concatenateStrings(data);
}
protected ByteBuf buffer(String... data) {
return Unpooled.copiedBuffer(concatenateStrings(data), StandardCharsets.ISO_8859_1);
}
protected DefaultFullHttpRequest request(String url) {
return request(HttpMethod.GET, url);
}
protected DefaultFullHttpRequest request(HttpMethod method, String url) {
return new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, url);
}
protected DefaultFullHttpRequest request(HttpMethod method, String url, ByteBuf data) {
return new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, url, data);
}
protected DefaultFullHttpRequest request(HttpMethod method, String url, HttpHeaders headers) {
return new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, url, Unpooled.buffer(), headers, new DefaultHttpHeaders());
}
protected DefaultFullHttpResponse response(ByteBuf data) {
return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, data);
}
protected void verifyNotNull(BaseProtocolDecoder decoder, Object object) throws Exception {
assertNotNull(decoder.decode(null, null, object));
}
protected void verifyNull(Object object) {
assertNull(object);
}
protected void verifyNull(BaseProtocolDecoder decoder, Object object) throws Exception {
assertNull(decoder.decode(null, null, object));
}
protected void verifyAttribute(BaseProtocolDecoder decoder, Object object, String key, Object expected) throws Exception {
Object decodedObject = decoder.decode(null, null, object);
Position position;
if (decodedObject instanceof Collection) {
position = (Position) ((Collection<?>) decodedObject).iterator().next();
} else {
position = (Position) decodedObject;
}
switch (key) {
case "speed":
assertEquals(expected, position.getSpeed());
break;
case "course":
assertEquals(expected, position.getCourse());
break;
default:
assertEquals(expected, position.getAttributes().get(key));
break;
}
}
protected void verifyAttributes(BaseProtocolDecoder decoder, Object object) throws Exception {
verifyDecodedPosition(decoder.decode(null, null, object), false, true, null);
}
protected void verifyPosition(BaseProtocolDecoder decoder, Object object) throws Exception {
verifyDecodedPosition(decoder.decode(null, null, object), true, false, null);
}
protected void verifyPosition(BaseProtocolDecoder decoder, Object object, Position position) throws Exception {
verifyDecodedPosition(decoder.decode(null, null, object), true, false, position);
}
protected void verifyPositions(BaseProtocolDecoder decoder, Object object) throws Exception {
verifyDecodedList(decoder.decode(null, null, object), true, null);
}
protected void verifyPositions(BaseProtocolDecoder decoder, boolean checkLocation, Object object) throws Exception {
verifyDecodedList(decoder.decode(null, null, object), checkLocation, null);
}
protected void verifyPositions(BaseProtocolDecoder decoder, Object object, Position position) throws Exception {
verifyDecodedList(decoder.decode(null, null, object), true, position);
}
private void verifyDecodedList(Object decodedObject, boolean checkLocation, Position expected) {
assertNotNull(decodedObject, "list is null");
assertTrue(decodedObject instanceof List, "not a list");
assertFalse(((List<?>) decodedObject).isEmpty(), "list is empty");
for (Object item : (List<?>) decodedObject) {
verifyDecodedPosition(item, checkLocation, false, expected);
}
}
private void verifyDecodedPosition(Object decodedObject, boolean checkLocation, boolean checkAttributes, Position expected) {
assertNotNull(decodedObject, "position is null");
assertTrue(decodedObject instanceof Position, "not a position");
Position position = (Position) decodedObject;
if (checkLocation) {
if (expected != null) {
if (expected.getFixTime() != null) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
assertEquals("time", dateFormat.format(expected.getFixTime()), dateFormat.format(position.getFixTime()));
}
assertEquals(expected.getValid(), position.getValid(), "valid");
assertEquals(expected.getLatitude(), position.getLatitude(), 0.00001, "latitude");
assertEquals(expected.getLongitude(), position.getLongitude(), 0.00001, "longitude");
} else {
assertNotNull(position.getServerTime());
assertNotNull(position.getFixTime());
assertTrue(position.getFixTime().after(new Date(915148800000L)), "year > 1999");
assertTrue(position.getFixTime().getTime() < System.currentTimeMillis() + 25 * 3600000, "time < +25 h");
assertTrue(position.getLatitude() >= -90, "latitude >= -90");
assertTrue(position.getLatitude() <= 90, "latitude <= 90");
assertTrue(position.getLongitude() >= -180, "longitude >= -180");
assertTrue(position.getLongitude() <= 180, "longitude <= 180");
}
assertTrue(position.getAltitude() >= -12262, "altitude >= -12262");
assertTrue(position.getAltitude() <= 18000, "altitude <= 18000");
assertTrue(position.getSpeed() >= 0, "speed >= 0");
assertTrue(position.getSpeed() <= 869, "speed <= 869");
assertTrue(position.getCourse() >= 0, "course >= 0");
assertTrue(position.getCourse() <= 360, "course <= 360");
assertNotNull(position.getProtocol(), "protocol is null");
assertTrue(position.getDeviceId() > 0, "deviceId > 0");
}
Map<String, Object> attributes = position.getAttributes();
if (checkAttributes) {
assertFalse(attributes.isEmpty(), "no attributes");
}
if (attributes.containsKey(Position.KEY_INDEX)) {
assertTrue(attributes.get(Position.KEY_INDEX) instanceof Number);
}
if (attributes.containsKey(Position.KEY_HDOP)) {
assertTrue(attributes.get(Position.KEY_HDOP) instanceof Number);
}
if (attributes.containsKey(Position.KEY_VDOP)) {
assertTrue(attributes.get(Position.KEY_VDOP) instanceof Number);
}
if (attributes.containsKey(Position.KEY_PDOP)) {
assertTrue(attributes.get(Position.KEY_PDOP) instanceof Number);
}
if (attributes.containsKey(Position.KEY_SATELLITES)) {
assertTrue(attributes.get(Position.KEY_SATELLITES) instanceof Number);
}
if (attributes.containsKey(Position.KEY_SATELLITES_VISIBLE)) {
assertTrue(attributes.get(Position.KEY_SATELLITES_VISIBLE) instanceof Number);
}
if (attributes.containsKey(Position.KEY_RSSI)) {
assertTrue(attributes.get(Position.KEY_RSSI) instanceof Number);
}
if (attributes.containsKey(Position.KEY_ODOMETER)) {
assertTrue(attributes.get(Position.KEY_ODOMETER) instanceof Number);
}
if (attributes.containsKey(Position.KEY_RPM)) {
assertTrue(attributes.get(Position.KEY_RPM) instanceof Number);
}
if (attributes.containsKey(Position.KEY_FUEL_LEVEL)) {
assertTrue(attributes.get(Position.KEY_FUEL_LEVEL) instanceof Number);
}
if (attributes.containsKey(Position.KEY_POWER)) {
assertTrue(attributes.get(Position.KEY_POWER) instanceof Number);
}
if (attributes.containsKey(Position.KEY_BATTERY)) {
assertTrue(attributes.get(Position.KEY_BATTERY) instanceof Number);
}
if (attributes.containsKey(Position.KEY_BATTERY_LEVEL)) {
int batteryLevel = ((Number) attributes.get(Position.KEY_BATTERY_LEVEL)).intValue();
assertTrue(batteryLevel <= 100 && batteryLevel >= 0);
}
if (attributes.containsKey(Position.KEY_CHARGE)) {
assertTrue(attributes.get(Position.KEY_CHARGE) instanceof Boolean);
}
if (attributes.containsKey(Position.KEY_IGNITION)) {
assertTrue(attributes.get(Position.KEY_IGNITION) instanceof Boolean);
}
if (attributes.containsKey(Position.KEY_MOTION)) {
assertTrue(attributes.get(Position.KEY_MOTION) instanceof Boolean);
}
if (attributes.containsKey(Position.KEY_ARCHIVE)) {
assertTrue(attributes.get(Position.KEY_ARCHIVE) instanceof Boolean);
}
if (attributes.containsKey(Position.KEY_DRIVER_UNIQUE_ID)) {
assertTrue(attributes.get(Position.KEY_DRIVER_UNIQUE_ID) instanceof String);
}
if (attributes.containsKey(Position.KEY_STEPS)) {
assertTrue(attributes.get(Position.KEY_STEPS) instanceof Number);
}
if (attributes.containsKey(Position.KEY_ROAMING)) {
assertTrue(attributes.get(Position.KEY_ROAMING) instanceof Boolean);
}
if (attributes.containsKey(Position.KEY_HOURS)) {
assertTrue(attributes.get(Position.KEY_HOURS) instanceof Number);
}
if (attributes.containsKey(Position.KEY_RESULT)) {
assertTrue(attributes.get(Position.KEY_RESULT) instanceof String);
}
if (position.getNetwork() != null) {
if (position.getNetwork().getCellTowers() != null) {
for (CellTower cellTower : position.getNetwork().getCellTowers()) {
checkInteger(cellTower.getMobileCountryCode(), 0, 999);
checkInteger(cellTower.getMobileNetworkCode(), 0, 999);
checkInteger(cellTower.getLocationAreaCode(), 1, 65535);
checkInteger(cellTower.getCellId(), 0, 268435455);
}
}
if (position.getNetwork().getWifiAccessPoints() != null) {
for (WifiAccessPoint wifiAccessPoint : position.getNetwork().getWifiAccessPoints()) {
assertTrue(wifiAccessPoint.getMacAddress().matches("((\\p{XDigit}{2}):){5}(\\p{XDigit}{2})"));
}
}
}
}
private void checkInteger(Object value, int min, int max) {
assertNotNull(value, "value is null");
assertTrue(value instanceof Integer || value instanceof Long, "not int or long");
long number = ((Number) value).longValue();
assertTrue(number >= min, "value too low");
assertTrue(number <= max, "value too high");
}
protected void verifyCommand(
BaseProtocolEncoder encoder, Command command, ByteBuf expected) {
verifyFrame(expected, encoder.encodeCommand(command));
}
protected void verifyFrame(ByteBuf expected, Object object) {
assertNotNull(object, "buffer is null");
assertTrue(object instanceof ByteBuf, "not a buffer");
assertEquals(ByteBufUtil.hexDump(expected), ByteBufUtil.hexDump((ByteBuf) object));
}
}
|