|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
"""Tests for Python APRS-IS Bindings.""" |
|
5
|
|
|
|
|
6
|
|
|
# These imports are for python3 compatability inside python2 |
|
7
|
|
|
from __future__ import absolute_import |
|
8
|
|
|
from __future__ import division |
|
9
|
|
|
from __future__ import print_function |
|
10
|
|
|
|
|
11
|
|
|
import sys |
|
12
|
|
|
import unittest |
|
13
|
|
|
|
|
14
|
|
|
import apex.aprs.aprs_internet_service |
|
15
|
|
|
import apex.aprs.constants |
|
16
|
|
|
|
|
17
|
|
|
if sys.version_info < (3, 0): |
|
18
|
|
|
import httpretty |
|
19
|
|
|
|
|
20
|
|
|
__author__ = 'Jeffrey Phillips Freeman (WI2ARD)' |
|
21
|
|
|
__maintainer__ = 'Jeffrey Phillips Freeman (WI2ARD)' |
|
22
|
|
|
__email__ = '[email protected]' |
|
23
|
|
|
__license__ = 'Apache License, Version 2.0' |
|
24
|
|
|
__copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors' |
|
25
|
|
|
__credits__ = [] |
|
26
|
|
|
|
|
27
|
|
|
ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
28
|
|
|
NUMBERS = '0123456789' |
|
29
|
|
|
POSITIVE_NUMBERS = NUMBERS[1:] |
|
30
|
|
|
ALPHANUM = ''.join([ALPHABET, NUMBERS]) |
|
31
|
|
|
|
|
32
|
|
|
DECODED_CALLSIGN = callsign = {'callsign': 'W2GMD', 'ssid': 1} |
|
33
|
|
|
ENCODED_CALLSIGN = [174, 100, 142, 154, 136, 64, 98] |
|
34
|
|
|
|
|
35
|
|
|
DECODED_CALLSIGN_DIGIPEATED = {'callsign': 'W2GMD*', 'ssid': 1} |
|
36
|
|
|
ENCODED_CALLSIGN_DIGIPEATED = [174, 100, 142, 154, 136, 64, 226] |
|
37
|
|
|
|
|
38
|
|
|
DECODED_FRAME = { |
|
39
|
|
|
'source': 'W2GMD-1', |
|
40
|
|
|
'destination': 'OMG', |
|
41
|
|
|
'path': ['WIDE1-1'], |
|
42
|
|
|
'text': 'test_encode_frame' |
|
43
|
|
|
} |
|
44
|
|
|
ENCODED_FRAME = [158, 154, 142, 64, 64, 64, 96, 174, 100, 142, 154, 136, 64, 98, 174, 146, 136, 138, 98, 64, 99, 3, 240, |
|
45
|
|
|
116, 101, 115, 116, 95, 101, 110, 99, 111, 100, 101, 95, 102, 114, 97, 109, 101] |
|
46
|
|
|
|
|
47
|
|
|
DECODED_FRAME_RECORDED = { |
|
48
|
|
|
'source': 'W2GMD-6', |
|
49
|
|
|
'destination': 'APRX24', |
|
50
|
|
|
'path': ['WIDE1-1'], |
|
51
|
|
|
'text': ('!3745.75NI12228.05W#W2GMD-6 Inner Sunset, ' |
|
52
|
|
|
'SF iGate/Digipeater http://w2gmd.org') |
|
53
|
|
|
} |
|
54
|
|
|
ENCODED_FRAME_RECORDED = [130, 160, 164, 176, 100, 104, 96, 174, 100, 142, 154, 136, 64, 108, 174, 146, 136, 138, 98, |
|
55
|
|
|
64, 99, 3, 240, 33, 51, 55, 52, 53, 46, 55, 53, 78, 73, 49, 50, 50, 50, 56, 46, 48, 53, 87, |
|
56
|
|
|
35, 87, 50, 71, 77, 68, 45, 54, 32, 73, 110, 110, 101, 114, 32, 83, 117, 110, 115, 101, 116, |
|
57
|
|
|
44, 32, 83, 70, 32, 105, 71, 97, 116, 101, 47, 68, 105, 103, 105, 112, 101, 97, 116, 101, 114, |
|
58
|
|
|
32, 104, 116, 116, 112, 58, 47, 47, 119, 50, 103, 109, 100, 46, 111, 114, 103] |
|
59
|
|
|
|
|
60
|
|
|
DECODED_FRAME_MULTIPATH = { |
|
61
|
|
|
'source': 'W2GMD-1', |
|
62
|
|
|
'destination': 'OMG', |
|
63
|
|
|
'path': ['WIDE1-1', 'WIDE2-2'], |
|
64
|
|
|
'text': 'test_encode_frame' |
|
65
|
|
|
} |
|
66
|
|
|
ENCODED_FRAME_MULTIPATH = [158, 154, 142, 64, 64, 64, 96, 174, 100, 142, 154, 136, 64, 98, 174, 146, 136, 138, 98, 64, |
|
67
|
|
|
98, 174, 146, 136, 138, 100, 64, 101, 3, 240, 116, 101, 115, 116, 95, 101, 110, 99, 111, 100, |
|
68
|
|
|
101, 95, 102, 114, 97, 109, 101] |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
class AprsTest(unittest.TestCase): # pylint: disable=R0904 |
|
72
|
|
|
"""Tests for Python APRS-IS Bindings.""" |
|
73
|
|
|
|
|
74
|
|
|
def setUp(self): # pylint: disable=C0103 |
|
75
|
|
|
self.fake_server = 'http://localhost:5567/' |
|
76
|
|
|
self.fake_callsign = 'KWN4YGH-5' |
|
77
|
|
|
|
|
78
|
|
|
def test_encode_callsign(self): |
|
79
|
|
|
""" |
|
80
|
|
|
Tests encoding a callsign. |
|
81
|
|
|
""" |
|
82
|
|
|
encoded_callsign = apex.aprs.Aprs._Aprs__encode_callsign(DECODED_CALLSIGN) |
|
83
|
|
|
self.assertEqual(ENCODED_CALLSIGN, encoded_callsign) |
|
84
|
|
|
|
|
85
|
|
|
def test_encode_callsign_digipeated(self): |
|
86
|
|
|
""" |
|
87
|
|
|
Tests encoding a digipeated callsign with |
|
88
|
|
|
`aprs.util.encode_callsign()`. |
|
89
|
|
|
""" |
|
90
|
|
|
encoded_callsign = apex.aprs.Aprs._Aprs__encode_callsign(DECODED_CALLSIGN_DIGIPEATED) |
|
91
|
|
|
self.assertEqual(ENCODED_CALLSIGN_DIGIPEATED, encoded_callsign) |
|
92
|
|
|
|
|
93
|
|
|
def test_decode_callsign(self): |
|
94
|
|
|
""" |
|
95
|
|
|
Tests extracting the callsign from a KISS-encoded APRS frame. |
|
96
|
|
|
""" |
|
97
|
|
|
decoded_callsign = apex.aprs.Aprs._Aprs__extract_callsign(ENCODED_CALLSIGN) |
|
98
|
|
|
self.assertEqual(DECODED_CALLSIGN, decoded_callsign) |
|
99
|
|
|
|
|
100
|
|
|
def test_decode_callsign_digipeated(self): |
|
101
|
|
|
""" |
|
102
|
|
|
Tests extracting the callsign from a KISS-encoded APRS frame. |
|
103
|
|
|
""" |
|
104
|
|
|
decoded_callsign = apex.aprs.Aprs._Aprs__extract_callsign(ENCODED_CALLSIGN_DIGIPEATED) |
|
105
|
|
|
self.assertEqual(DECODED_CALLSIGN, decoded_callsign) |
|
106
|
|
|
|
|
107
|
|
|
def test_encode_frame(self): |
|
108
|
|
|
""" |
|
109
|
|
|
Tests KISS-encoding an APRS. |
|
110
|
|
|
""" |
|
111
|
|
|
encoded_frame = apex.aprs.Aprs._Aprs__encode_frame(DECODED_FRAME) |
|
112
|
|
|
self.assertEqual(ENCODED_FRAME, encoded_frame) |
|
113
|
|
|
|
|
114
|
|
|
def test_encode_frame_recorded(self): |
|
115
|
|
|
""" |
|
116
|
|
|
Tests encoding a KISS-encoded APRS. |
|
117
|
|
|
""" |
|
118
|
|
|
encoded_frame = apex.aprs.Aprs._Aprs__encode_frame(DECODED_FRAME_RECORDED) |
|
119
|
|
|
self.assertEqual(ENCODED_FRAME_RECORDED, encoded_frame) |
|
120
|
|
|
|
|
121
|
|
|
def test_encode_frame_multipath(self): |
|
122
|
|
|
""" |
|
123
|
|
|
Tests encoding a KISS-encoded APRS. |
|
124
|
|
|
""" |
|
125
|
|
|
encoded_frame = apex.aprs.Aprs._Aprs__encode_frame(DECODED_FRAME_MULTIPATH) |
|
126
|
|
|
self.assertEqual(ENCODED_FRAME_MULTIPATH, encoded_frame) |
|
127
|
|
|
|
|
128
|
|
|
def test_decode_frame(self): |
|
129
|
|
|
""" |
|
130
|
|
|
Tests KISS-encoding an APRS |
|
131
|
|
|
""" |
|
132
|
|
|
decoded_frame = apex.aprs.Aprs._Aprs__decode_frame(ENCODED_FRAME) |
|
133
|
|
|
self.assertEqual(DECODED_FRAME, decoded_frame) |
|
134
|
|
|
|
|
135
|
|
|
def test_decode_frame_recorded(self): |
|
136
|
|
|
""" |
|
137
|
|
|
Tests decoding a KISS-encoded APRS frame |
|
138
|
|
|
""" |
|
139
|
|
|
decoded_frame = apex.aprs.Aprs._Aprs__decode_frame(ENCODED_FRAME_RECORDED) |
|
140
|
|
|
self.assertEqual(DECODED_FRAME_RECORDED, decoded_frame) |
|
141
|
|
|
|
|
142
|
|
|
def test_decode_frame_multipath(self): |
|
143
|
|
|
""" |
|
144
|
|
|
Tests decoding a KISS-encoded APRS frame |
|
145
|
|
|
""" |
|
146
|
|
|
decoded_frame = apex.aprs.Aprs._Aprs__decode_frame(ENCODED_FRAME_MULTIPATH) |
|
147
|
|
|
self.assertEqual(DECODED_FRAME_MULTIPATH, decoded_frame) |
|
148
|
|
|
|
|
149
|
|
|
def test_extract_path(self): |
|
150
|
|
|
""" |
|
151
|
|
|
Tests extracting the APRS path from a KISS-encoded. |
|
152
|
|
|
""" |
|
153
|
|
|
extracted_path = apex.aprs.Aprs._Aprs__extract_path(3, ENCODED_FRAME) |
|
154
|
|
|
self.assertEqual(DECODED_FRAME['path'][0], extracted_path[0]) |
|
155
|
|
|
|
|
156
|
|
|
def test_idwentity_as_string_with_ssid(self): |
|
157
|
|
|
""" |
|
158
|
|
|
Tests creating a full callsign string from a callsign+ssid dict using |
|
159
|
|
|
`aprs.util.full_callsign()`. |
|
160
|
|
|
""" |
|
161
|
|
|
callsign = { |
|
162
|
|
|
'callsign': 'W2GMD', |
|
163
|
|
|
'ssid': 1 |
|
164
|
|
|
} |
|
165
|
|
|
full_callsign = apex.aprs.Aprs._Aprs__identity_as_string(callsign) |
|
166
|
|
|
self.assertEqual(full_callsign, 'W2GMD-1') |
|
167
|
|
|
|
|
168
|
|
|
def test_identity_as_string_sans_ssid(self): |
|
169
|
|
|
""" |
|
170
|
|
|
Tests creating a full callsign string from a callsign dict using |
|
171
|
|
|
`aprs.util.full_callsign()`. |
|
172
|
|
|
""" |
|
173
|
|
|
callsign = { |
|
174
|
|
|
'callsign': 'W2GMD', |
|
175
|
|
|
'ssid': 0 |
|
176
|
|
|
} |
|
177
|
|
|
full_callsign = apex.aprs.Aprs._Aprs__identity_as_string(callsign) |
|
178
|
|
|
self.assertEqual(full_callsign, 'W2GMD') |
|
179
|
|
|
|
|
180
|
|
|
if sys.version_info < (3, 0): |
|
181
|
|
View Code Duplication |
@httpretty.httprettified |
|
|
|
|
|
|
182
|
|
|
def test_fake_good_auth(self): |
|
183
|
|
|
""" |
|
184
|
|
|
Tests authenticating against APRS-IS using a valid call+pass. |
|
185
|
|
|
""" |
|
186
|
|
|
httpretty.HTTPretty.register_uri( |
|
187
|
|
|
httpretty.HTTPretty.POST, |
|
188
|
|
|
self.fake_server, |
|
189
|
|
|
status=204 |
|
190
|
|
|
) |
|
191
|
|
|
|
|
192
|
|
|
aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService( |
|
193
|
|
|
user=self.fake_callsign, |
|
194
|
|
|
input_url=self.fake_server |
|
195
|
|
|
) |
|
196
|
|
|
aprs_conn.connect() |
|
197
|
|
|
|
|
198
|
|
|
msg = { |
|
199
|
|
|
'source': self.fake_callsign, |
|
200
|
|
|
'destination': 'APRS', |
|
201
|
|
|
'path': ['TCPIP*'], |
|
202
|
|
|
'text': '=3745.00N/12227.00W-Simulated Location' |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
result = aprs_conn.send(msg) |
|
206
|
|
|
|
|
207
|
|
|
self.assertTrue(result) |
|
208
|
|
|
|
|
209
|
|
View Code Duplication |
@httpretty.httprettified |
|
|
|
|
|
|
210
|
|
|
def test_fake_bad_auth_http(self): |
|
211
|
|
|
""" |
|
212
|
|
|
Tests authenticating against APRS-IS using an invalid call+pass. |
|
213
|
|
|
""" |
|
214
|
|
|
httpretty.HTTPretty.register_uri( |
|
215
|
|
|
httpretty.HTTPretty.POST, |
|
216
|
|
|
self.fake_server, |
|
217
|
|
|
status=401 |
|
218
|
|
|
) |
|
219
|
|
|
|
|
220
|
|
|
aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService( |
|
221
|
|
|
user=self.fake_callsign, |
|
222
|
|
|
input_url=self.fake_server |
|
223
|
|
|
) |
|
224
|
|
|
aprs_conn.connect() |
|
225
|
|
|
|
|
226
|
|
|
msg = { |
|
227
|
|
|
'source': self.fake_callsign, |
|
228
|
|
|
'destination': 'APRS', |
|
229
|
|
|
'path': ['TCPIP*'], |
|
230
|
|
|
'text': '=3745.00N/12227.00W-Simulated Location' |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
result = aprs_conn.send(msg, protocol='HTTP') |
|
234
|
|
|
|
|
235
|
|
|
self.assertFalse(result) |
|
236
|
|
|
|
|
237
|
|
|
@unittest.skip('Test only works with real server.') |
|
238
|
|
|
def test_more(self): |
|
239
|
|
|
""" |
|
240
|
|
|
Tests APRS-IS binding against a real APRS-IS server. |
|
241
|
|
|
""" |
|
242
|
|
|
aprs_conn = apex.aprs.aprs_internet_service.AprsInternetService( |
|
243
|
|
|
user=self.real_callsign, |
|
244
|
|
|
input_url=self.real_server |
|
245
|
|
|
) |
|
246
|
|
|
aprs_conn.connect() |
|
247
|
|
|
|
|
248
|
|
|
msg = { |
|
249
|
|
|
'source': self.fake_callsign, |
|
250
|
|
|
'destination': 'APRS', |
|
251
|
|
|
'path': ['TCPIP*'], |
|
252
|
|
|
'text': '=3745.00N/12227.00W-Simulated Location' |
|
253
|
|
|
} |
|
254
|
|
|
self.logger.debug(locals()) |
|
255
|
|
|
|
|
256
|
|
|
result = aprs_conn.send(msg) |
|
257
|
|
|
|
|
258
|
|
|
self.assertFalse(result) |
|
259
|
|
|
|