import importlib.machinery import importlib.util import ipaddress import json import pathlib import tempfile import unittest from unittest import mock ROOT = pathlib.Path(__file__).resolve().parents[1] def load_script(name): loader = importlib.machinery.SourceFileLoader(name, str(ROOT / name)) spec = importlib.util.spec_from_loader(name, loader) module = importlib.util.module_from_spec(spec) loader.exec_module(module) return module he_dyndns = load_script("he-dyndns") ip_echo = load_script("ip-echo-daemon") class EchoTests(unittest.TestCase): def test_accepts_address_from_loopback_proxy(self): self.assertEqual( ip_echo.proxied_address("127.0.0.1", "2001:db8::123"), ipaddress.ip_address("2001:db8::123"), ) def test_rejects_header_from_non_loopback_peer(self): with self.assertRaisesRegex(ValueError, "loopback"): ip_echo.proxied_address("192.0.2.10", "198.51.100.20") class DiscoveryTests(unittest.TestCase): @mock.patch.object(he_dyndns, "http_get") def test_ipv6_discovery_pins_family_and_validates_response(self, http_get): http_get.return_value = (200, b"2001:db8::5\n") result = he_dyndns.discover_address("https://ip.mfamily.org/", 6) self.assertEqual(result, ipaddress.ip_address("2001:db8::5")) self.assertEqual(http_get.call_args.args[3], he_dyndns.socket.AF_INET6) self.assertTrue(http_get.call_args.kwargs["use_tls"]) @mock.patch.object(he_dyndns, "http_get") def test_rejects_wrong_address_family(self, http_get): http_get.return_value = (200, b"192.0.2.1\n") with self.assertRaisesRegex(ValueError, "IPv6"): he_dyndns.discover_address("https://ip.mfamily.org/", 6) class HeUpdateTests(unittest.TestCase): @mock.patch.object(he_dyndns, "http_get") def test_update_uses_family_pinned_https(self, http_get): http_get.return_value = (200, b"good 192.0.2.9\n") he_dyndns.update_he( "home.example", "secret", ipaddress.ip_address("192.0.2.9") ) self.assertEqual(http_get.call_args.args[1], 443) self.assertEqual(http_get.call_args.args[3], he_dyndns.socket.AF_INET) self.assertTrue(http_get.call_args.kwargs["use_tls"]) class StateTests(unittest.TestCase): def setUp(self): self.directory = tempfile.TemporaryDirectory() self.state_path = str(pathlib.Path(self.directory.name) / "state.json") self.parser = he_dyndns.make_parser() def tearDown(self): self.directory.cleanup() @mock.patch.object(he_dyndns, "update_he") @mock.patch.object(he_dyndns, "discover_address") def test_unchanged_discovered_address_skips_update(self, discover, update): address = ipaddress.ip_address("192.0.2.44") discover.return_value = address he_dyndns.write_state(self.state_path, {"home.example": {"4": str(address)}}) args = self.parser.parse_args([ "home.example", "--key", "secret", "--state-file", self.state_path ]) he_dyndns.run(args, self.parser) update.assert_not_called() @mock.patch.object(he_dyndns, "update_he") @mock.patch.object(he_dyndns, "discover_address") def test_changed_address_updates_then_writes_state(self, discover, update): address = ipaddress.ip_address("2001:db8::44") discover.return_value = address args = self.parser.parse_args([ "home.example", "--v6", "--key", "secret", "--state-file", self.state_path, ]) he_dyndns.run(args, self.parser) update.assert_called_once_with("home.example", "secret", address, timeout=10) with open(self.state_path, encoding="utf-8") as state_file: self.assertEqual(json.load(state_file)["home.example"]["6"], str(address)) @mock.patch.object(he_dyndns, "update_he") @mock.patch.object(he_dyndns, "discover_address") def test_failed_update_does_not_write_state(self, discover, update): discover.return_value = ipaddress.ip_address("192.0.2.55") update.side_effect = RuntimeError("failed") args = self.parser.parse_args([ "home.example", "--key", "secret", "--state-file", self.state_path ]) with self.assertRaisesRegex(RuntimeError, "failed"): he_dyndns.run(args, self.parser) self.assertFalse(pathlib.Path(self.state_path).exists()) @mock.patch.object(he_dyndns, "update_he") @mock.patch.object(he_dyndns, "discover_address") def test_explicit_address_bypasses_discovery_and_state(self, discover, update): args = self.parser.parse_args([ "home.example", "--addr4", "192.0.2.88", "--key", "secret", "--state-file", self.state_path, ]) he_dyndns.run(args, self.parser) discover.assert_not_called() update.assert_called_once() self.assertFalse(pathlib.Path(self.state_path).exists()) if __name__ == "__main__": unittest.main()