2020-12-25 16:45:28 -05:00
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
|
2021-08-23 12:14:19 -04:00
|
|
|
import pytz
|
|
|
|
|
2020-12-25 16:45:28 -05:00
|
|
|
import aprsd
|
2021-08-20 15:21:47 -04:00
|
|
|
from aprsd import messaging, packets, stats, utils
|
2020-12-25 16:45:28 -05:00
|
|
|
from aprsd.fuzzyclock import fuzzy
|
2021-01-09 09:58:56 -05:00
|
|
|
from aprsd.plugins import fortune as fortune_plugin
|
|
|
|
from aprsd.plugins import ping as ping_plugin
|
2021-01-11 12:09:29 -05:00
|
|
|
from aprsd.plugins import query as query_plugin
|
2021-01-09 09:58:56 -05:00
|
|
|
from aprsd.plugins import time as time_plugin
|
|
|
|
from aprsd.plugins import version as version_plugin
|
2020-12-25 16:45:28 -05:00
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
from . import fake
|
|
|
|
|
2020-12-25 16:45:28 -05:00
|
|
|
|
2021-01-06 17:50:02 -05:00
|
|
|
class TestPlugin(unittest.TestCase):
|
2020-12-25 16:45:28 -05:00
|
|
|
def setUp(self):
|
2021-08-20 15:21:47 -04:00
|
|
|
self.fromcall = fake.FAKE_FROM_CALLSIGN
|
2020-12-25 18:13:52 -05:00
|
|
|
self.ack = 1
|
2021-04-05 08:55:46 -04:00
|
|
|
self.config = utils.DEFAULT_CONFIG_DICT
|
|
|
|
self.config["ham"]["callsign"] = self.fromcall
|
2021-08-20 15:21:47 -04:00
|
|
|
self.config["aprs"]["login"] = fake.FAKE_TO_CALLSIGN
|
2021-04-05 08:55:46 -04:00
|
|
|
# Inintialize the stats object with the config
|
|
|
|
stats.APRSDStats(self.config)
|
2020-12-25 16:45:28 -05:00
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
@mock.patch.object(fake.FakeBaseNoThreadsPlugin, "process")
|
|
|
|
def test_base_plugin_no_threads(self, mock_process):
|
|
|
|
p = fake.FakeBaseNoThreadsPlugin(self.config)
|
|
|
|
|
|
|
|
expected = []
|
|
|
|
actual = p.create_threads()
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
expected = "1.0"
|
|
|
|
actual = p.version
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
expected = 0
|
|
|
|
actual = p.message_counter
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
expected = None
|
|
|
|
actual = p.filter(fake.fake_packet())
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
mock_process.assert_not_called()
|
|
|
|
|
|
|
|
@mock.patch.object(fake.FakeBaseThreadsPlugin, "create_threads")
|
|
|
|
def test_base_plugin_threads_created(self, mock_create):
|
|
|
|
fake.FakeBaseThreadsPlugin(self.config)
|
|
|
|
mock_create.assert_called_once()
|
|
|
|
|
|
|
|
def test_base_plugin_threads(self):
|
|
|
|
p = fake.FakeBaseThreadsPlugin(self.config)
|
|
|
|
actual = p.create_threads()
|
|
|
|
self.assertTrue(isinstance(actual, fake.FakeThread))
|
|
|
|
p.stop_threads()
|
|
|
|
|
|
|
|
@mock.patch.object(fake.FakeRegexCommandPlugin, "process")
|
|
|
|
def test_regex_base_not_called(self, mock_process):
|
|
|
|
p = fake.FakeRegexCommandPlugin(self.config)
|
|
|
|
packet = fake.fake_packet(message="a")
|
|
|
|
expected = None
|
|
|
|
actual = p.filter(packet)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
mock_process.assert_not_called()
|
|
|
|
|
|
|
|
packet = fake.fake_packet(tocall="notMe", message="f")
|
|
|
|
expected = None
|
|
|
|
actual = p.filter(packet)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
mock_process.assert_not_called()
|
2021-07-14 20:50:41 -04:00
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(
|
|
|
|
message="F",
|
|
|
|
message_format=packets.PACKET_TYPE_MICE,
|
|
|
|
)
|
|
|
|
expected = None
|
|
|
|
actual = p.filter(packet)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
mock_process.assert_not_called()
|
2021-07-14 20:50:41 -04:00
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(
|
|
|
|
message="f",
|
|
|
|
message_format=packets.PACKET_TYPE_ACK,
|
|
|
|
)
|
|
|
|
expected = None
|
|
|
|
actual = p.filter(packet)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
mock_process.assert_not_called()
|
2021-07-14 20:50:41 -04:00
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
@mock.patch.object(fake.FakeRegexCommandPlugin, "process")
|
|
|
|
def test_regex_base_assert_called(self, mock_process):
|
|
|
|
p = fake.FakeRegexCommandPlugin(self.config)
|
|
|
|
packet = fake.fake_packet(message="f")
|
|
|
|
p.filter(packet)
|
|
|
|
mock_process.assert_called_once()
|
|
|
|
|
|
|
|
def test_regex_base_process_called(self):
|
|
|
|
p = fake.FakeRegexCommandPlugin(self.config)
|
|
|
|
|
|
|
|
packet = fake.fake_packet(message="f")
|
|
|
|
expected = fake.FAKE_MESSAGE_TEXT
|
|
|
|
actual = p.filter(packet)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
packet = fake.fake_packet(message="F")
|
|
|
|
expected = fake.FAKE_MESSAGE_TEXT
|
|
|
|
actual = p.filter(packet)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
packet = fake.fake_packet(message="fake")
|
|
|
|
expected = fake.FAKE_MESSAGE_TEXT
|
|
|
|
actual = p.filter(packet)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
packet = fake.fake_packet(message="FAKE")
|
|
|
|
expected = fake.FAKE_MESSAGE_TEXT
|
|
|
|
actual = p.filter(packet)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
|
|
|
|
class TestFortunePlugin(TestPlugin):
|
2020-12-25 18:13:52 -05:00
|
|
|
@mock.patch("shutil.which")
|
|
|
|
def test_fortune_fail(self, mock_which):
|
2021-01-09 09:58:56 -05:00
|
|
|
fortune = fortune_plugin.FortunePlugin(self.config)
|
2020-12-25 18:13:52 -05:00
|
|
|
mock_which.return_value = None
|
|
|
|
expected = "Fortune command not installed"
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(message="fortune")
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = fortune.filter(packet)
|
2020-12-25 18:13:52 -05:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2021-01-11 14:29:02 -05:00
|
|
|
@mock.patch("subprocess.check_output")
|
2020-12-25 18:13:52 -05:00
|
|
|
@mock.patch("shutil.which")
|
2021-01-11 14:29:02 -05:00
|
|
|
def test_fortune_success(self, mock_which, mock_output):
|
2021-01-09 09:58:56 -05:00
|
|
|
fortune = fortune_plugin.FortunePlugin(self.config)
|
2020-12-25 18:13:52 -05:00
|
|
|
mock_which.return_value = "/usr/bin/games"
|
|
|
|
|
2021-01-11 14:29:02 -05:00
|
|
|
mock_output.return_value = "Funny fortune"
|
2020-12-25 18:13:52 -05:00
|
|
|
|
|
|
|
expected = "Funny fortune"
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(message="fortune")
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = fortune.filter(packet)
|
2020-12-25 18:13:52 -05:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
|
|
|
|
class TestQueryPlugin(TestPlugin):
|
2021-01-11 12:09:29 -05:00
|
|
|
@mock.patch("aprsd.messaging.MsgTrack.flush")
|
|
|
|
def test_query_flush(self, mock_flush):
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(message="!delete")
|
2021-01-11 12:09:29 -05:00
|
|
|
query = query_plugin.QueryPlugin(self.config)
|
|
|
|
|
2021-01-14 14:28:59 -05:00
|
|
|
expected = "Deleted ALL pending msgs."
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = query.filter(packet)
|
2021-01-11 12:09:29 -05:00
|
|
|
mock_flush.assert_called_once()
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
@mock.patch("aprsd.messaging.MsgTrack.restart_delayed")
|
|
|
|
def test_query_restart_delayed(self, mock_restart):
|
|
|
|
track = messaging.MsgTrack()
|
|
|
|
track.track = {}
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(message="!4")
|
2021-01-11 12:09:29 -05:00
|
|
|
query = query_plugin.QueryPlugin(self.config)
|
|
|
|
|
2021-01-14 15:06:57 -05:00
|
|
|
expected = "No pending msgs to resend"
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = query.filter(packet)
|
2021-01-11 12:09:29 -05:00
|
|
|
mock_restart.assert_not_called()
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
mock_restart.reset_mock()
|
|
|
|
|
|
|
|
# add a message
|
|
|
|
msg = messaging.TextMessage(self.fromcall, "testing", self.ack)
|
|
|
|
track.add(msg)
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = query.filter(packet)
|
2021-01-11 12:09:29 -05:00
|
|
|
mock_restart.assert_called_once()
|
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
|
|
|
|
class TestTimePlugins(TestPlugin):
|
2021-01-19 11:19:53 -05:00
|
|
|
@mock.patch("aprsd.plugins.time.TimePlugin._get_local_tz")
|
|
|
|
@mock.patch("aprsd.plugins.time.TimePlugin._get_utcnow")
|
|
|
|
def test_time(self, mock_utcnow, mock_localtz):
|
|
|
|
utcnow = pytz.datetime.datetime.utcnow()
|
|
|
|
mock_utcnow.return_value = utcnow
|
|
|
|
tz = pytz.timezone("US/Pacific")
|
|
|
|
mock_localtz.return_value = tz
|
|
|
|
|
|
|
|
gmt_t = pytz.utc.localize(utcnow)
|
|
|
|
local_t = gmt_t.astimezone(tz)
|
|
|
|
|
2020-12-25 16:45:28 -05:00
|
|
|
fake_time = mock.MagicMock()
|
2021-01-19 11:19:53 -05:00
|
|
|
h = int(local_t.strftime("%H"))
|
|
|
|
m = int(local_t.strftime("%M"))
|
|
|
|
fake_time.tm_sec = 13
|
2021-01-09 09:58:56 -05:00
|
|
|
time = time_plugin.TimePlugin(self.config)
|
2020-12-25 16:45:28 -05:00
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(
|
2021-07-14 20:50:41 -04:00
|
|
|
message="location",
|
|
|
|
msg_number=1,
|
|
|
|
)
|
2020-12-25 16:45:28 -05:00
|
|
|
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = time.filter(packet)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(None, actual)
|
|
|
|
|
|
|
|
cur_time = fuzzy(h, m, 1)
|
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(
|
2021-07-14 20:50:41 -04:00
|
|
|
message="time",
|
|
|
|
msg_number=1,
|
|
|
|
)
|
2021-01-19 11:19:53 -05:00
|
|
|
local_short_str = local_t.strftime("%H:%M %Z")
|
2021-01-20 10:19:49 -05:00
|
|
|
expected = "{} ({})".format(
|
2021-01-08 15:47:30 -05:00
|
|
|
cur_time,
|
2021-01-19 11:19:53 -05:00
|
|
|
local_short_str,
|
2020-12-25 16:45:28 -05:00
|
|
|
)
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = time.filter(packet)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
|
|
|
|
class TestPingPlugin(TestPlugin):
|
2020-12-25 16:45:28 -05:00
|
|
|
@mock.patch("time.localtime")
|
2021-01-06 17:50:02 -05:00
|
|
|
def test_ping(self, mock_time):
|
2020-12-25 16:45:28 -05:00
|
|
|
fake_time = mock.MagicMock()
|
|
|
|
h = fake_time.tm_hour = 16
|
|
|
|
m = fake_time.tm_min = 12
|
|
|
|
s = fake_time.tm_sec = 55
|
|
|
|
mock_time.return_value = fake_time
|
|
|
|
|
2021-01-09 09:58:56 -05:00
|
|
|
ping = ping_plugin.PingPlugin(self.config)
|
2020-12-25 16:45:28 -05:00
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(
|
2021-07-14 20:50:41 -04:00
|
|
|
message="location",
|
|
|
|
msg_number=1,
|
|
|
|
)
|
2020-12-25 16:45:28 -05:00
|
|
|
|
2021-08-19 11:39:29 -04:00
|
|
|
result = ping.filter(packet)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(None, result)
|
|
|
|
|
|
|
|
def ping_str(h, m, s):
|
|
|
|
return (
|
|
|
|
"Pong! "
|
|
|
|
+ str(h).zfill(2)
|
|
|
|
+ ":"
|
|
|
|
+ str(m).zfill(2)
|
|
|
|
+ ":"
|
|
|
|
+ str(s).zfill(2)
|
|
|
|
)
|
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(
|
2021-07-14 20:50:41 -04:00
|
|
|
message="Ping",
|
|
|
|
msg_number=1,
|
|
|
|
)
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = ping.filter(packet)
|
2020-12-25 16:45:28 -05:00
|
|
|
expected = ping_str(h, m, s)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(
|
2021-07-14 20:50:41 -04:00
|
|
|
message="ping",
|
|
|
|
msg_number=1,
|
|
|
|
)
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = ping.filter(packet)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
|
|
|
|
class TestVersionPlugin(TestPlugin):
|
2021-08-19 11:39:29 -04:00
|
|
|
@mock.patch("aprsd.plugin.PluginManager.get_plugins")
|
2021-06-17 16:27:35 -04:00
|
|
|
def test_version(self, mock_get_plugins):
|
2021-08-23 12:14:19 -04:00
|
|
|
expected = f"APRSD ver:{aprsd.__version__} uptime:0:0:0"
|
2021-01-09 09:58:56 -05:00
|
|
|
version = version_plugin.VersionPlugin(self.config)
|
2020-12-25 16:45:28 -05:00
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(
|
2021-07-14 20:50:41 -04:00
|
|
|
message="No",
|
|
|
|
msg_number=1,
|
|
|
|
)
|
2020-12-25 16:45:28 -05:00
|
|
|
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = version.filter(packet)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(None, actual)
|
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(
|
2021-07-14 20:50:41 -04:00
|
|
|
message="version",
|
|
|
|
msg_number=1,
|
|
|
|
)
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = version.filter(packet)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2021-08-20 15:21:47 -04:00
|
|
|
packet = fake.fake_packet(
|
2021-07-14 20:50:41 -04:00
|
|
|
message="Version",
|
|
|
|
msg_number=1,
|
|
|
|
)
|
2021-08-19 11:39:29 -04:00
|
|
|
actual = version.filter(packet)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(expected, actual)
|