A Python port of the Invisible Internet Project (I2P)
1"""Tests for SusiDNS — TDD: tests before implementation."""
2
3import pytest
4from pathlib import Path
5from i2p_apps.susidns.manager import AddressbookManager, AddressbookEntry
6
7
8class TestAddressbookEntry:
9 def test_creation(self):
10 entry = AddressbookEntry(hostname="test.i2p", destination="AAAA" * 86)
11 assert entry.hostname == "test.i2p"
12
13 def test_str(self):
14 entry = AddressbookEntry(hostname="test.i2p", destination="DEST")
15 assert str(entry) == "test.i2p=DEST"
16
17
18class TestAddressbookManager:
19 def test_creation(self):
20 mgr = AddressbookManager()
21 assert mgr is not None
22
23 def test_add_entry(self):
24 mgr = AddressbookManager()
25 mgr.add("example.i2p", "DESTDATA")
26 assert mgr.lookup("example.i2p") == "DESTDATA"
27
28 def test_remove_entry(self):
29 mgr = AddressbookManager()
30 mgr.add("example.i2p", "DESTDATA")
31 mgr.remove("example.i2p")
32 assert mgr.lookup("example.i2p") is None
33
34 def test_list_entries(self):
35 mgr = AddressbookManager()
36 mgr.add("a.i2p", "DEST_A")
37 mgr.add("b.i2p", "DEST_B")
38 entries = mgr.list_entries()
39 assert len(entries) == 2
40
41 def test_search(self):
42 mgr = AddressbookManager()
43 mgr.add("alice.i2p", "DEST_A")
44 mgr.add("bob.i2p", "DEST_B")
45 mgr.add("alicia.i2p", "DEST_C")
46 results = mgr.search("ali")
47 assert len(results) == 2
48
49 def test_load_from_file(self, tmp_path: Path):
50 hosts = tmp_path / "hosts.txt"
51 hosts.write_text("test.i2p=DESTDATA\nother.i2p=DESTOTHER\n")
52 mgr = AddressbookManager.from_file(hosts)
53 assert mgr.lookup("test.i2p") == "DESTDATA"
54 assert mgr.lookup("other.i2p") == "DESTOTHER"
55
56 def test_save_to_file(self, tmp_path: Path):
57 hosts = tmp_path / "hosts.txt"
58 mgr = AddressbookManager()
59 mgr.add("test.i2p", "DEST")
60 mgr.save(hosts)
61 content = hosts.read_text()
62 assert "test.i2p=DEST" in content