1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
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")
def test_error_response_contains_only_status_code(self):
handler = object.__new__(ip_echo.AddressHandler)
handler.command = "GET"
handler.wfile = mock.Mock()
handler.send_response = mock.Mock()
handler.send_header = mock.Mock()
handler.end_headers = mock.Mock()
handler.send_error(404, "ignored")
handler.send_response.assert_called_once_with(404)
handler.send_header.assert_has_calls([
mock.call("Content-Type", "text/plain; charset=us-ascii"),
mock.call("Content-Length", "4"),
])
handler.end_headers.assert_called_once_with()
handler.wfile.write.assert_called_once_with(b"404\n")
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_initialize_state_discovers_both_families_without_he(self, discover, update):
discover.side_effect = [
ipaddress.ip_address("192.0.2.60"),
ipaddress.ip_address("2001:db8::60"),
]
args = self.parser.parse_args([
"home.example", "--initialize-state", "--state-file", self.state_path
])
he_dyndns.run(args, self.parser)
self.assertEqual(
[call.args[1] for call in discover.call_args_list],
[4, 6],
)
update.assert_not_called()
with open(self.state_path, encoding="utf-8") as state_file:
self.assertEqual(
json.load(state_file)["home.example"],
{"4": "192.0.2.60", "6": "2001:db8::60"},
)
@mock.patch.object(he_dyndns, "update_he")
@mock.patch.object(he_dyndns, "discover_address")
def test_initialize_failure_does_not_change_existing_state(self, discover, update):
existing = {"home.example": {"4": "192.0.2.10", "6": "2001:db8::10"}}
he_dyndns.write_state(self.state_path, existing)
discover.side_effect = [
ipaddress.ip_address("192.0.2.60"),
RuntimeError("IPv6 unavailable"),
]
args = self.parser.parse_args([
"home.example", "--initialize-state", "--state-file", self.state_path
])
with self.assertRaisesRegex(RuntimeError, "IPv6 unavailable"):
he_dyndns.run(args, self.parser)
update.assert_not_called()
with open(self.state_path, encoding="utf-8") as state_file:
self.assertEqual(json.load(state_file), existing)
@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()
|