def test_listen_on_zero_with_host(self): """ ``_tub_portlocation`` raises ``PortAssignmentRequired`` called with a listen address including port 0 and an interface. """ config_data = ("[node]\n" "tub.port = tcp:0:interface=127.0.0.1\n") config = config_from_string(self.basedir, "portnum", config_data) with self.assertRaises(PortAssignmentRequired): _tub_portlocation(config, None, None)
def test_empty_tub_location(self): """ location povided, but empty is an error """ config_data = ("[node]\n" "tub.location = \n") config = config_from_string(self.basedir, "portnum", config_data) with self.assertRaises(ValueError) as ctx: _tub_portlocation(config) self.assertIn("tub.location must not be empty", str(ctx.exception))
def test_disabled_tub_not_port(self): """ error to disable location but not port """ config_data = ("[node]\n" "tub.port = not_disabled\n" "tub.location = disabled\n") config = config_from_string(self.basedir, "portnum", config_data) with self.assertRaises(ValueError) as ctx: _tub_portlocation(config) self.assertIn("tub.location is disabled, but not tub.port", str(ctx.exception))
def test_empty_tub_port(self): """ port povided, but empty is an error """ config_data = ("[node]\n" "tub.port = \n") config = config_from_string(self.basedir, "portnum", config_data) with self.assertRaises(ValueError) as ctx: _tub_portlocation( config, _stub_get_local_addresses_sync, _stub_allocate_tcp_port, ) self.assertIn("tub.port must not be empty", str(ctx.exception))
def test_empty_tub_location(self): """ location povided, but empty is an error """ config_data = ( "[node]\n" "tub.location = \n" ) config = config_from_string(self.basedir, "portnum", config_data) with self.assertRaises(ValueError) as ctx: _tub_portlocation(config) self.assertIn( "tub.location must not be empty", str(ctx.exception) )
def test_disabled_port_not_tub(self): """ error to disable port but not location """ config_data = ("[node]\n" "tub.port = disabled\n" "tub.location = not_disabled\n") config = config_from_string(self.basedir, "portnum", config_data) with self.assertRaises(ValueError) as ctx: _tub_portlocation( config, _stub_get_local_addresses_sync, _stub_allocate_tcp_port, ) self.assertIn("tub.port is disabled, but not tub.location", str(ctx.exception))
def test_disabled_tub_not_port(self): """ error to disable location but not port """ config_data = ( "[node]\n" "tub.port = not_disabled\n" "tub.location = disabled\n" ) config = config_from_string(self.basedir, "portnum", config_data) with self.assertRaises(ValueError) as ctx: _tub_portlocation(config) self.assertIn( "tub.location is disabled, but not tub.port", str(ctx.exception) )
def test_tub_location_tcp(self): """ If ``reveal-IP-address`` is set to false and ``tub.location`` includes a **tcp** hint then ``_tub_portlocation`` raises `PrivacyError`` because TCP leaks IP addresses. """ config = config_from_string( "fake.port", "no-basedir", "[node]\nreveal-IP-address = false\ntub.location=tcp:hostname:1234\n", ) with self.assertRaises(PrivacyError) as ctx: _tub_portlocation( config, _stub_get_local_addresses_sync, _stub_allocate_tcp_port, ) self.assertEqual( str(ctx.exception), "tub.location includes tcp: hint", )
def test_parsing_location_complex(self): """ location with two options (including defaults) """ config_data = ("[node]\n" "tub.location = tcp:HOST:888,AUTO\n") config = config_from_string(self.basedir, "portnum", config_data) tubport, tublocation = _tub_portlocation( config, _stub_get_local_addresses_sync, _stub_allocate_tcp_port, ) self.assertEqual(tubport, "tcp:999") self.assertEqual(tublocation, b"tcp:HOST:888,tcp:LOCAL:999")
def test_parsing_defaults(self): """ parse empty config, check defaults """ config_data = ("[node]\n") config = config_from_string(self.basedir, "portnum", config_data) tubport, tublocation = _tub_portlocation( config, _stub_get_local_addresses_sync, _stub_allocate_tcp_port, ) self.assertEqual(tubport, "tcp:999") self.assertEqual(tublocation, b"tcp:LOCAL:999")
def test_tub_location_legacy_tcp(self): """ If ``reveal-IP-address`` is set to false and ``tub.location`` includes a "legacy" hint with no explicit type (which means it is a **tcp** hint) then the behavior is the same as for an explicit **tcp** hint. """ config = config_from_string( "fake.port", "no-basedir", "[node]\nreveal-IP-address = false\ntub.location=hostname:1234\n", ) with self.assertRaises(PrivacyError) as ctx: _tub_portlocation( config, _stub_get_local_addresses_sync, _stub_allocate_tcp_port, ) self.assertEqual( str(ctx.exception), "tub.location includes tcp: hint", )
def test_parsing_all_disabled(self): """ parse config with both port + location disabled """ config_data = ("[node]\n" "tub.port = disabled\n" "tub.location = disabled\n") config = config_from_string(self.basedir, "portnum", config_data) res = _tub_portlocation( config, _stub_get_local_addresses_sync, _stub_allocate_tcp_port, ) self.assertTrue(res is None)
def test_parsing_tcp(self): """ When ``tub.port`` is given and ``tub.location`` is **AUTO** the port number from ``tub.port`` is used as the port number for the value constructed for ``tub.location``. """ config_data = ("[node]\n" "tub.port = tcp:777\n" "tub.location = AUTO\n") config = config_from_string(self.basedir, "portnum", config_data) tubport, tublocation = _tub_portlocation( config, _stub_get_local_addresses_sync, _stub_allocate_tcp_port, ) self.assertEqual(tubport, "tcp:777") self.assertEqual(tublocation, b"tcp:LOCAL:777")
def test_parsing_location_complex(self): """ location with two options (including defaults) """ get_addr = mock.patch( "allmydata.util.iputil.get_local_addresses_sync", return_value=["LOCAL"], ) alloc_port = mock.patch( "allmydata.util.iputil.allocate_tcp_port", return_value=999, ) config_data = ("[node]\n" "tub.location = tcp:HOST:888,AUTO\n") config = config_from_string(self.basedir, "portnum", config_data) with get_addr, alloc_port: tubport, tublocation = _tub_portlocation(config) self.assertEqual(tubport, "tcp:999") self.assertEqual(tublocation, b"tcp:HOST:888,tcp:LOCAL:999")
def test_parsing_defaults(self): """ parse empty config, check defaults """ get_addr = mock.patch( "allmydata.util.iputil.get_local_addresses_sync", return_value=["LOCAL"], ) alloc_port = mock.patch( "allmydata.util.iputil.allocate_tcp_port", return_value=999, ) config_data = ("[node]\n") config = config_from_string(self.basedir, "portnum", config_data) with get_addr, alloc_port: tubport, tublocation = _tub_portlocation(config) self.assertEqual(tubport, "tcp:999") self.assertEqual(tublocation, b"tcp:LOCAL:999")
def test_parsing_all_disabled(self): """ parse config with both port + location disabled """ get_addr = mock.patch( "allmydata.util.iputil.get_local_addresses_sync", return_value=["LOCAL"], ) alloc_port = mock.patch( "allmydata.util.iputil.allocate_tcp_port", return_value=999, ) config_data = ("[node]\n" "tub.port = disabled\n" "tub.location = disabled\n") config = config_from_string(self.basedir, "portnum", config_data) with get_addr, alloc_port: res = _tub_portlocation(config) self.assertTrue(res is None)
def test_parsing_tcp(self): """ parse explicit tub.port with explicitly-default tub.location """ get_addr = mock.patch( "allmydata.util.iputil.get_local_addresses_sync", return_value=["LOCAL"], ) alloc_port = mock.patch( "allmydata.util.iputil.allocate_tcp_port", return_value=999, ) config_data = ("[node]\n" "tub.port = tcp:777\n" "tub.location = AUTO\n") config = config_from_string(self.basedir, "portnum", config_data) with get_addr, alloc_port: tubport, tublocation = _tub_portlocation(config) self.assertEqual(tubport, "tcp:777") self.assertEqual(tublocation, b"tcp:LOCAL:777")
def test_parsing_defaults(self): """ parse empty config, check defaults """ get_addr = mock.patch( "allmydata.util.iputil.get_local_addresses_sync", return_value=["LOCAL"], ) alloc_port = mock.patch( "allmydata.util.iputil.allocate_tcp_port", return_value=999, ) config_data = ( "[node]\n" ) config = config_from_string(self.basedir, "portnum", config_data) with get_addr, alloc_port: tubport, tublocation = _tub_portlocation(config) self.assertEqual(tubport, "tcp:999") self.assertEqual(tublocation, "tcp:LOCAL:999")
def test_parsing_location_complex(self): """ location with two options (including defaults) """ get_addr = mock.patch( "allmydata.util.iputil.get_local_addresses_sync", return_value=["LOCAL"], ) alloc_port = mock.patch( "allmydata.util.iputil.allocate_tcp_port", return_value=999, ) config_data = ( "[node]\n" "tub.location = tcp:HOST:888,AUTO\n" ) config = config_from_string(self.basedir, "portnum", config_data) with get_addr, alloc_port: tubport, tublocation = _tub_portlocation(config) self.assertEqual(tubport, "tcp:999") self.assertEqual(tublocation, "tcp:HOST:888,tcp:LOCAL:999")
def test_parsing_all_disabled(self): """ parse config with both port + location disabled """ get_addr = mock.patch( "allmydata.util.iputil.get_local_addresses_sync", return_value=["LOCAL"], ) alloc_port = mock.patch( "allmydata.util.iputil.allocate_tcp_port", return_value=999, ) config_data = ( "[node]\n" "tub.port = disabled\n" "tub.location = disabled\n" ) config = config_from_string(self.basedir, "portnum", config_data) with get_addr, alloc_port: res = _tub_portlocation(config) self.assertTrue(res is None)
def test_parsing_tcp(self): """ parse explicit tub.port with explicitly-default tub.location """ get_addr = mock.patch( "allmydata.util.iputil.get_local_addresses_sync", return_value=["LOCAL"], ) alloc_port = mock.patch( "allmydata.util.iputil.allocate_tcp_port", return_value=999, ) config_data = ( "[node]\n" "tub.port = tcp:777\n" "tub.location = AUTO\n" ) config = config_from_string(self.basedir, "portnum", config_data) with get_addr, alloc_port: tubport, tublocation = _tub_portlocation(config) self.assertEqual(tubport, "tcp:777") self.assertEqual(tublocation, "tcp:LOCAL:777")