Enhance HostsManager to prevent modification and movement of default system entries; add is_default_entry method to HostEntry and update sorting methods to prioritize default entries.

This commit is contained in:
Philip Henning 2025-07-29 23:04:29 +02:00
parent 8c1cd2047e
commit 3084650c27
6 changed files with 122 additions and 45 deletions

View file

@ -224,20 +224,21 @@ class TestHostsFile:
assert inactive_entries[0] == inactive_entry
def test_sort_by_ip(self):
"""Test sorting entries by IP address."""
"""Test sorting entries by IP address with default entries on top."""
hosts_file = HostsFile()
entry1 = HostEntry(ip_address="192.168.1.1", hostnames=["router"])
entry2 = HostEntry(ip_address="127.0.0.1", hostnames=["localhost"])
entry2 = HostEntry(ip_address="127.0.0.1", hostnames=["localhost"]) # Default entry
entry3 = HostEntry(ip_address="10.0.0.1", hostnames=["test"])
hosts_file.add_entry(entry1)
hosts_file.add_entry(entry2)
hosts_file.add_entry(entry3)
hosts_file.sort_by_ip()
assert hosts_file.entries[0].ip_address == "10.0.0.1"
assert hosts_file.entries[1].ip_address == "127.0.0.1"
# Default entries should come first, then sorted non-default entries
assert hosts_file.entries[0].ip_address == "127.0.0.1" # Default entry first
assert hosts_file.entries[1].ip_address == "10.0.0.1" # Then sorted non-defaults
assert hosts_file.entries[2].ip_address == "192.168.1.1"
def test_sort_by_hostname(self):