From 0cf6ca86fb9df9ca9d14d584baf10f29fb1afd7a Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Thu, 29 Oct 2020 10:26:14 -0400 Subject: [PATCH] Improve test coverage. --- src/allmydata/test/test_node.py | 114 ++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/allmydata/test/test_node.py b/src/allmydata/test/test_node.py index 0d6c915b6..a1ecedfd5 100644 --- a/src/allmydata/test/test_node.py +++ b/src/allmydata/test/test_node.py @@ -17,6 +17,7 @@ import sys import time import mock from textwrap import dedent +import configparser from unittest import skipIf @@ -38,6 +39,8 @@ from allmydata.node import ( MissingConfigEntry, _tub_portlocation, formatTimeTahoeStyle, + UnescapedHashError, + PrivacyError, ) from allmydata.introducer.server import create_introducer from allmydata import client @@ -178,6 +181,43 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): config = read_config(basedir, "") self.failUnless(config.nickname == nickname) + def test_hash_in_furl(self): + """ + Hashes in furl options are not allowed, resulting in exception. + """ + escaped = "lalal\\#onohash" + basedir = self.mktemp() + fileutil.make_dirs(basedir) + with open(os.path.join(basedir, 'tahoe.cfg'), 'wt') as f: + f.write("[node]\n") + f.write("log_gatherer.furl = lalal#onohash\n") + + config = read_config(basedir, "") + with self.assertRaises(UnescapedHashError): + config.get_config("node", "log_gatherer.furl") + + with open(os.path.join(basedir, 'tahoe.cfg'), 'wt') as f: + f.write("[node]\n") + f.write("log_gatherer.furl = %s\n" % (escaped,)) + self.assertEquals(config.get_config("node", "log_gatherer.furl"), escaped) + + def test_missing_config_item(self): + """ + If a config item is missing: + + 1. Given a default, return default. + 2. Otherwise, raise MissingConfigEntry. + """ + basedir = self.mktemp() + fileutil.make_dirs(basedir) + with open(os.path.join(basedir, 'tahoe.cfg'), 'wt') as f: + f.write("[node]\n") + config = read_config(basedir, "") + + self.assertEquals(config.get_config("node", "log_gatherer.furl", "def"), "def") + with self.assertRaises(MissingConfigEntry): + config.get_config("node", "log_gatherer.furl") + def test_config_required(self): """ Asking for missing (but required) configuration is an error @@ -210,6 +250,31 @@ class TestCase(testutil.SignalMixin, unittest.TestCase): ("timeout.disconnect", "12"), ], ) + self.assertEqual( + config.items("node", [("unnecessary", "default")]), + [("nickname", "foo"), + ("timeout.disconnect", "12"), + ], + ) + + + def test_config_items_missing_section(self): + """ + If a default is given for a missing section, the default is used. + + Lacking both default and section, an error is raised. + """ + basedir = self.mktemp() + create_node_dir(basedir, "testing") + + with open(os.path.join(basedir, 'tahoe.cfg'), 'wt') as f: + f.write("") + + config = read_config(basedir, "portnum") + with self.assertRaises(configparser.NoSectionError): + config.items("nosuch") + default = [("hello", "world")] + self.assertEqual(config.items("nosuch", default), default) @skipIf( "win32" in sys.platform.lower() or "cygwin" in sys.platform.lower(), @@ -756,3 +821,52 @@ class Configuration(unittest.TestCase): "invalid section", str(ctx.exception), ) + + + +class FakeProvider(object): + """Emulate Tor and I2P providers.""" + + def get_tor_handler(self): + return "TORHANDLER!" + + def get_i2p_handler(self): + return "I2PHANDLER!" + + +class CreateConnectionHandlers(unittest.TestCase): + """ + Tests for create_connection_handlers(). + """ + + def test_tcp_disabled(self): + """ + If tcp is set to disabled, no TCP handler is set. + """ + config = config_from_string("", "", dedent(""" + [connections] + tcp = disabled + """)) + reactor = object() # it's not actually used?! + provider = FakeProvider() + default_handlers, _ = create_connection_handlers( + reactor, config, provider, provider + ) + self.assertIs(default_handlers["tcp"], None) + + def test_reveal_ip_tcp(self): + """ + If reveal IP setting is false, TCP handler is not allowed. + """ + config = config_from_string("", "", dedent(""" + [node] + reveal-IP-address = False + + [connections] + tcp = tcp + """)) + reactor = object() # it's not actually used?! + provider = FakeProvider() + with self.assertRaises(PrivacyError): + create_connection_handlers(reactor, config, provider, provider) +