File: test_IS.py

package info (click to toggle)
python-aprslib 0.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: python: 2,973; makefile: 216
file content (449 lines) | stat: -rw-r--r-- 16,286 bytes parent folder | download
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import unittest
import socket
import sys
import os

import aprslib
from mox3 import mox


# byte shim for testing in both py2 and py3

class TC_IS(unittest.TestCase):
    def setUp(self):
        self.ais = aprslib.IS("LZ1DEV-99", "testpwd", "127.0.0.1", "11111")
        self.m = mox.Mox()

    def tearDown(self):
        self.m.UnsetStubs()

    def test_initilization(self):
        self.assertFalse(self.ais._connected)
        self.assertEqual(self.ais.buf, b'')
        self.assertIsNone(self.ais.sock)
        self.assertEqual(self.ais.callsign, "LZ1DEV-99")
        self.assertEqual(self.ais.passwd, "testpwd")
        self.assertEqual(self.ais.server, ("127.0.0.1", "11111"))

    def test_close(self):
        self.ais._connected = True
        self.ais.sock = mox.MockAnything()
        self.ais.sock.close()
        mox.Replay(self.ais.sock)

        self.ais.close()

        mox.Verify(self.ais.sock)
        self.assertFalse(self.ais._connected)
        self.assertEqual(self.ais.buf, b'')

    def test_open_socket(self):
        with self.assertRaises(socket.error):
            self.ais._open_socket()

    def test_socket_readlines(self):
        fdr, fdw = os.pipe()
        f = os.fdopen(fdw, 'w')
        f.write("something")
        f.close()

        class BreakBlocking(Exception):
            pass

        self.m.ReplayAll()
        self.ais.sock = mox.MockAnything()
        # part 1 - conn drop before setblocking
        self.ais.sock.setblocking(0).AndRaise(socket.error)
        # part 2 - conn drop trying to recv
        self.ais.sock.setblocking(0)
        self.ais.sock.fileno().AndReturn(fdr)
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b'')
        # part 3 - nothing to read
        self.ais.sock.setblocking(0)
        self.ais.sock.fileno().AndReturn(fdr)
        self.ais.sock.recv(mox.IgnoreArg()).AndRaise(
            socket.error("Resource temporarily unavailable"))
        # part 4 - yield 3 lines (blocking False)
        self.ais.sock.setblocking(0)
        self.ais.sock.fileno().AndReturn(fdr)
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"a\r\n"*3)
        self.ais.sock.fileno().AndReturn(fdr)
        self.ais.sock.recv(mox.IgnoreArg()).AndRaise(
            socket.error("Resource temporarily unavailable"))
        # part 5 - yield 3 lines 2 times (blocking True)
        self.ais.sock.setblocking(0)
        self.ais.sock.fileno().AndReturn(fdr)
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"b\r\n"*3)
        self.ais.sock.fileno().AndReturn(fdr)
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"b\r\n"*3)
        self.ais.sock.fileno().AndReturn(fdr)
        self.ais.sock.recv(mox.IgnoreArg()).AndRaise(BreakBlocking)
        mox.Replay(self.ais.sock)

        next_method = '__next__' if sys.version_info[0] >= 3 else 'next'

        # part 1
        with self.assertRaises(aprslib.exceptions.ConnectionDrop):
            getattr(self.ais._socket_readlines(), next_method)()
        # part 2
        with self.assertRaises(aprslib.exceptions.ConnectionDrop):
            getattr(self.ais._socket_readlines(), next_method)()
        # part 3
        with self.assertRaises(StopIteration):
            getattr(self.ais._socket_readlines(), next_method)()
        # part 4
        for line in self.ais._socket_readlines():
            self.assertEqual(line, b'a')
        # part 5
        with self.assertRaises(BreakBlocking):
            for line in self.ais._socket_readlines(blocking=True):
                self.assertEqual(line, b'b')

        mox.Verify(self.ais.sock)

    def test_send_login(self):
        self.ais.sock = mox.MockAnything()
        self.m.StubOutWithMock(self.ais, "close")
        self.m.StubOutWithMock(self.ais, "_sendall")
        # part 1 - raises
        self.ais._sendall(mox.IgnoreArg())
        self.ais.sock.settimeout(mox.IgnoreArg())
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"invalidreply")
        self.ais.close()
        # part 2 - raises (empty callsign)
        self.ais._sendall(mox.IgnoreArg())
        self.ais.sock.settimeout(mox.IgnoreArg())
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# logresp  verified, xx")
        self.ais.close()
        # part 3 - raises (callsign doesn't match
        self.ais._sendall(mox.IgnoreArg())
        self.ais.sock.settimeout(mox.IgnoreArg())
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# logresp NOMATCH verified, xx")
        self.ais.close()
        # part 4 - raises (unverified, but pass is not -1)
        self.ais._sendall(mox.IgnoreArg())
        self.ais.sock.settimeout(mox.IgnoreArg())
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# logresp CALL unverified, xx")
        self.ais.close()
        # part 5 - normal, receive only
        self.ais._sendall(mox.IgnoreArg())
        self.ais.sock.settimeout(mox.IgnoreArg())
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# logresp CALL unverified, xx")
        # part 6 - normal, correct pass
        self.ais._sendall(mox.IgnoreArg())
        self.ais.sock.settimeout(mox.IgnoreArg())
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# logresp CALL verified, xx")
        mox.Replay(self.ais.sock)
        self.m.ReplayAll()

        # part 1
        self.ais.set_login("CALL", "-1")
        self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login)
        # part 2
        self.ais.set_login("CALL", "-1")
        self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login)
        # part 3
        self.ais.set_login("CALL", "-1")
        self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login)
        # part 4
        self.ais.set_login("CALL", "99999")
        self.assertRaises(aprslib.exceptions.LoginError, self.ais._send_login)
        # part 5
        self.ais.set_login("CALL", "-1")
        self.ais._send_login()
        # part 6
        self.ais.set_login("CALL", "99999")
        self.ais._send_login()

        mox.Verify(self.ais.sock)
        self.m.VerifyAll()

    def test_connect(self):
        self.ais.sock = mox.MockAnything()
        self.m.StubOutWithMock(self.ais, "_open_socket")
        self.m.StubOutWithMock(self.ais, "close")
        # part 1 - socket creation errors
        self.ais._open_socket().AndRaise(socket.timeout("timed out"))
        self.ais.close()
        self.ais._open_socket().AndRaise(socket.error('any'))
        self.ais.close()
        # part 2 - invalid banner from server
        self.ais._open_socket()
        self.ais.sock.getpeername().AndReturn((1, 2))
        self.ais.sock.setblocking(mox.IgnoreArg())
        self.ais.sock.settimeout(mox.IgnoreArg())
        self.ais.sock.setsockopt(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg())
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"junk")
        self.ais.close()
        # part 3 - everything going well
        self.ais._open_socket()
        self.ais.sock.getpeername().AndReturn((1, 2))
        self.ais.sock.setblocking(mox.IgnoreArg())
        self.ais.sock.settimeout(mox.IgnoreArg())
        self.ais.sock.setsockopt(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg())
        self.ais.sock.recv(mox.IgnoreArg()).AndReturn(b"# server banner")
        mox.Replay(self.ais.sock)
        self.m.ReplayAll()

        # part 1
        self.assertRaises(aprslib.exceptions.ConnectionError, self.ais._connect)
        self.assertFalse(self.ais._connected)
        self.assertRaises(aprslib.exceptions.ConnectionError, self.ais._connect)
        self.assertFalse(self.ais._connected)
        # part 2
        self.assertRaises(aprslib.exceptions.ConnectionError, self.ais._connect)
        self.assertFalse(self.ais._connected)
        # part 3
        self.ais._connect()
        self.assertTrue(self.ais._connected)

        mox.Verify(self.ais.sock)
        self.m.VerifyAll()

    def test_filter(self):
        testFilter = 'x/CALLSIGN'

        self.ais._connected = True
        self.ais.sock = mox.MockAnything()
        self.ais.sock.sendall(b'#filter ' + testFilter.encode('ascii') + b'\r\n')
        mox.Replay(self.ais.sock)

        self.ais.set_filter(testFilter)
        self.assertEqual(self.ais.filter, testFilter)

        mox.Verify(self.ais.sock)

    def test_connect_from_notconnected(self):
        self.m.StubOutWithMock(self.ais, "_connect")
        self.m.StubOutWithMock(self.ais, "_send_login")
        self.ais._connect()
        self.ais._send_login()
        self.m.ReplayAll()

        self.ais.connect()

        self.m.VerifyAll()

    def test_connect_from_connected(self):
        self.m.StubOutWithMock(self.ais, "_connect")
        self.m.StubOutWithMock(self.ais, "_send_login")
        self.ais._connect()
        self.ais._send_login()
        self.m.ReplayAll()

        self.ais._connected = True
        self.ais.connect()

        self.assertRaises(mox.ExpectedMethodCallsError, self.m.VerifyAll)

    def test_connect_raising_exception(self):
        self.m.StubOutWithMock(self.ais, "_connect")
        self.ais._connect().AndRaise(Exception("anything"))
        self.m.ReplayAll()

        self.assertRaises(Exception, self.ais.connect)

        self.m.VerifyAll()

    def test_connect_raising_exceptions(self):
        self.m.StubOutWithMock(self.ais, "_connect")
        self.m.StubOutWithMock(self.ais, "_send_login")
        self.ais._connect().AndRaise(aprslib.exceptions.ConnectionError("first"))
        self.ais._connect()
        self.ais._send_login().AndRaise(aprslib.exceptions.LoginError("second"))
        self.ais._connect()
        self.ais._send_login()
        self.m.ReplayAll()

        self.ais.connect(blocking=True, retry=0)

        self.m.VerifyAll()

    def test_sendall_type_exception(self):
        for testType in [5, 0.5, dict, list]:
            with self.assertRaises(TypeError):
                self.ais.sendall(testType)

    def test_sendall_not_connected(self):
        self.ais._connected = False
        with self.assertRaises(aprslib.ConnectionError):
            self.ais.sendall("test")

    def test_sendall_socketerror(self):
        self.ais.sock = mox.MockAnything()
        self.m.StubOutWithMock(self.ais, "close")

        # setup
        self.ais.sock.setblocking(mox.IgnoreArg())
        self.ais.sock.settimeout(mox.IgnoreArg())
        self.ais.sock.sendall(mox.IgnoreArg()).AndRaise(socket.error)
        self.ais.close()

        mox.Replay(self.ais.sock)
        self.m.ReplayAll()

        # test
        self.ais._connected = True
        with self.assertRaises(aprslib.ConnectionError):
            self.ais.sendall("test")

        # verify
        mox.Verify(self.ais.sock)
        self.m.VerifyAll()

    def test_sendall_empty_input(self):
        self.ais._connected = True
        self.ais.sendall("")

    def test_sendall_passing_to_socket(self):
        self.ais.sock = mox.MockAnything()
        self.m.StubOutWithMock(self.ais, "close")
        self.m.StubOutWithMock(self.ais, "_sendall")

        # rest
        _unicode = str if sys.version_info[0] >= 3 else unicode

        self.ais._connected = True
        for line in [
                "test",
                "test\r\n",
                _unicode("test"),
                _unicode("test\r\n"),
                ]:
            # setup
            self.ais.sock = mox.MockAnything()
            self.ais.sock.setblocking(mox.IgnoreArg())
            self.ais.sock.settimeout(mox.IgnoreArg())
            self.ais._sendall(b"%c" + line.rstrip('\r\n').encode('ascii') + b'\r\n').AndReturn(None)
            mox.Replay(self.ais.sock)

            self.ais.sendall(line)

            mox.Verify(self.ais.sock)


class TC_IS_consumer(unittest.TestCase):
    def setUp(self):
        self.ais = aprslib.IS("LZ1DEV-99")
        self.ais._connected = True
        self.m = mox.Mox()
        self.m.StubOutWithMock(self.ais, "_socket_readlines")
        self.m.StubOutWithMock(self.ais, "_parse")
        self.m.StubOutWithMock(self.ais, "connect")
        self.m.StubOutWithMock(self.ais, "close")

    def tearDown(self):
        self.m.UnsetStubs()

    def test_consumer_notconnected(self):
        self.ais._connected = False

        with self.assertRaises(aprslib.exceptions.ConnectionError):
            self.ais.consumer(callback=lambda: None, blocking=False)

    def test_consumer_raw(self):
        self.ais._socket_readlines(False).AndReturn([b"line1"])
        self.m.ReplayAll()

        def testcallback(line):
            self.assertEqual(line, b"line1")

        self.ais.consumer(callback=testcallback, blocking=False, raw=True)

        self.m.VerifyAll()

    def test_consumer_blocking(self):
        self.ais._socket_readlines(True).AndReturn([b"line1"])
        self.ais._socket_readlines(True).AndReturn([b"line1"] * 5)
        self.ais._socket_readlines(True).AndRaise(StopIteration)
        self.m.ReplayAll()

        def testcallback(line):
            self.assertEqual(line, b"line1")

        self.ais.consumer(callback=testcallback, blocking=True, raw=True)

        self.m.VerifyAll()

    def test_consumer_parsed(self):
        self.ais._socket_readlines(False).AndReturn([b"line1"])
        self.ais._parse(b"line1").AndReturn([])
        self.m.ReplayAll()

        def testcallback(line):
            self.assertEqual(line, [])

        self.ais.consumer(callback=testcallback, blocking=False, raw=False)

        self.m.VerifyAll()

    def test_consumer_serverline(self):
        self.ais._socket_readlines(False).AndReturn([b"# serverline"])
        self.m.ReplayAll()

        def testcallback(line):
            self.fail("callback shouldn't be called")

        self.ais.consumer(callback=testcallback, blocking=False, raw=False)

        self.m.VerifyAll()

    def test_consumer_exceptions(self):
        self.ais._socket_readlines(False).AndRaise(SystemExit)
        self.ais._socket_readlines(False).AndRaise(KeyboardInterrupt)
        self.ais._socket_readlines(False).AndRaise(Exception("random"))
        self.ais._socket_readlines(False).AndRaise(aprslib.exceptions.ParseError('x'))
        self.ais._socket_readlines(False).AndRaise(aprslib.exceptions.UnknownFormat('x'))
        self.ais._socket_readlines(False).AndRaise(aprslib.exceptions.LoginError('x'))
        self.ais._socket_readlines(False).AndRaise(aprslib.exceptions.GenericError('x'))
        self.ais._socket_readlines(False).AndRaise(StopIteration)
        self.m.ReplayAll()

        def testcallback(line):
            pass

        # should raise
        for e in [
                SystemExit,
                KeyboardInterrupt,
                Exception
                ]:
            with self.assertRaises(e):
                self.ais.consumer(callback=testcallback, blocking=False, raw=False)

        # non raising
        for e in [
                aprslib.exceptions.ParseError,
                aprslib.exceptions.UnknownFormat,
                aprslib.exceptions.LoginError,
                aprslib.exceptions.GenericError,
                StopIteration
                ]:
            self.ais.consumer(callback=testcallback, blocking=False, raw=False)

        self.m.VerifyAll()

    def test_consumer_close(self):
        # importal = False
        self.ais._socket_readlines(False).AndRaise(aprslib.exceptions.ConnectionDrop(''))
        self.ais.close()
        self.ais._socket_readlines(False).AndRaise(aprslib.exceptions.ConnectionError(''))
        self.ais.close()
        # importal = True
        self.ais._socket_readlines(False).AndRaise(aprslib.exceptions.ConnectionDrop(''))
        self.ais.close()
        self.ais.connect(blocking=False)
        self.ais._socket_readlines(False).AndRaise(aprslib.exceptions.ConnectionError(''))
        self.ais.close()
        self.ais.connect(blocking=False)
        self.ais._socket_readlines(False).AndRaise(StopIteration)
        self.m.ReplayAll()

        with self.assertRaises(aprslib.exceptions.ConnectionDrop):
            self.ais.consumer(callback=lambda: None, blocking=False, raw=False)
        with self.assertRaises(aprslib.exceptions.ConnectionError):
            self.ais.consumer(callback=lambda: None, blocking=False, raw=False)

        self.ais.consumer(callback=lambda: None, blocking=False, raw=False, immortal=True)

        self.m.VerifyAll()