From 0b93725709e7299c9715cfa98e3e29da04842d4d Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Tue, 7 Nov 2017 21:21:04 -0600 Subject: [PATCH] more test coverage --- .../test/test_config.py | 23 +++++++++++ .../test/test_service.py | 39 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/wormhole_transit_relay/test/test_config.py create mode 100644 src/wormhole_transit_relay/test/test_service.py diff --git a/src/wormhole_transit_relay/test/test_config.py b/src/wormhole_transit_relay/test/test_config.py new file mode 100644 index 0000000..02e3fee --- /dev/null +++ b/src/wormhole_transit_relay/test/test_config.py @@ -0,0 +1,23 @@ +from __future__ import unicode_literals, print_function +from twisted.trial import unittest +from .. import server_tap + +class Config(unittest.TestCase): + def test_defaults(self): + o = server_tap.Options() + o.parseOptions([]) + self.assertEqual(o, {"blur-usage": None, "log-fd": None, + "usage-db": None, "port": "tcp:4001"}) + def test_blur(self): + o = server_tap.Options() + o.parseOptions(["--blur-usage=60"]) + self.assertEqual(o, {"blur-usage": 60, "log-fd": None, + "usage-db": None, "port": "tcp:4001"}) + + def test_string(self): + o = server_tap.Options() + s = str(o) + self.assertIn("This plugin sets up a 'Transit Relay'", s) + self.assertIn("--blur-usage=", s) + self.assertIn("blur timestamps and data sizes in logs", s) + diff --git a/src/wormhole_transit_relay/test/test_service.py b/src/wormhole_transit_relay/test/test_service.py new file mode 100644 index 0000000..dac642c --- /dev/null +++ b/src/wormhole_transit_relay/test/test_service.py @@ -0,0 +1,39 @@ +from __future__ import unicode_literals, print_function +from twisted.trial import unittest +import mock +from twisted.application.service import MultiService +from .. import server_tap + +class Service(unittest.TestCase): + def test_defaults(self): + o = server_tap.Options() + o.parseOptions([]) + with mock.patch("wormhole_transit_relay.server_tap.transit_server.Transit") as t: + s = server_tap.makeService(o) + self.assertEqual(t.mock_calls, + [mock.call(blur_usage=None, + log_file=None, usage_db=None)]) + self.assertIsInstance(s, MultiService) + + def test_blur(self): + o = server_tap.Options() + o.parseOptions(["--blur-usage=60"]) + with mock.patch("wormhole_transit_relay.server_tap.transit_server.Transit") as t: + server_tap.makeService(o) + self.assertEqual(t.mock_calls, + [mock.call(blur_usage=60, + log_file=None, usage_db=None)]) + + def test_log_fd(self): + o = server_tap.Options() + o.parseOptions(["--log-fd=99"]) + fd = object() + with mock.patch("wormhole_transit_relay.server_tap.transit_server.Transit") as t: + with mock.patch("wormhole_transit_relay.server_tap.os.fdopen", + return_value=fd) as f: + server_tap.makeService(o) + self.assertEqual(f.mock_calls, [mock.call(99, "w")]) + self.assertEqual(t.mock_calls, + [mock.call(blur_usage=None, + log_file=fd, usage_db=None)]) +