"""Tests for naming/address resolution.""" import os import base64 import pytest class TestLookupResult: def test_success(self): from i2p_client.naming import LookupResult dest_data = os.urandom(387) r = LookupResult(success=True, destination_data=dest_data) assert r.success assert r.destination_data == dest_data assert r.error_code is None def test_failure(self): from i2p_client.naming import LookupResult r = LookupResult(success=False, error_code=1) assert not r.success assert r.destination_data is None assert r.error_code == 1 class TestHostsTxtService: def _make_hosts_entry(self, hostname, dest_data): """Create a hosts.txt line: hostname=base64(dest_data)""" b64 = base64.b64encode(dest_data).decode("ascii") return f"{hostname}={b64}" def test_lookup_found(self): from i2p_client.naming import HostsTxtService dest_data = os.urandom(387) hosts_txt = self._make_hosts_entry("test.i2p", dest_data) svc = HostsTxtService.from_string(hosts_txt) result = svc.lookup("test.i2p") assert result.success assert result.destination_data == dest_data def test_lookup_not_found(self): from i2p_client.naming import HostsTxtService svc = HostsTxtService.from_string("") result = svc.lookup("missing.i2p") assert not result.success def test_multiple_entries(self): from i2p_client.naming import HostsTxtService d1 = os.urandom(387) d2 = os.urandom(387) lines = "\n".join([ self._make_hosts_entry("a.i2p", d1), self._make_hosts_entry("b.i2p", d2), ]) svc = HostsTxtService.from_string(lines) assert svc.lookup("a.i2p").destination_data == d1 assert svc.lookup("b.i2p").destination_data == d2 def test_comment_lines_ignored(self): from i2p_client.naming import HostsTxtService d = os.urandom(387) lines = f"# comment\n{self._make_hosts_entry('x.i2p', d)}\n# another comment" svc = HostsTxtService.from_string(lines) assert svc.lookup("x.i2p").success def test_blank_lines_ignored(self): from i2p_client.naming import HostsTxtService d = os.urandom(387) lines = f"\n\n{self._make_hosts_entry('y.i2p', d)}\n\n" svc = HostsTxtService.from_string(lines) assert svc.lookup("y.i2p").success def test_case_insensitive(self): from i2p_client.naming import HostsTxtService d = os.urandom(387) lines = self._make_hosts_entry("MyHost.I2P", d) svc = HostsTxtService.from_string(lines) assert svc.lookup("myhost.i2p").success assert svc.lookup("MYHOST.I2P").success def test_list_all(self): from i2p_client.naming import HostsTxtService d1 = os.urandom(387) d2 = os.urandom(387) lines = "\n".join([ self._make_hosts_entry("a.i2p", d1), self._make_hosts_entry("b.i2p", d2), ]) svc = HostsTxtService.from_string(lines) all_hosts = svc.list_all() assert len(all_hosts) == 2 assert "a.i2p" in all_hosts assert "b.i2p" in all_hosts class TestNamingService: def test_abstract_lookup(self): from i2p_client.naming import NamingService # NamingService is abstract with pytest.raises(TypeError): NamingService()