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:
parent
8c1cd2047e
commit
3084650c27
6 changed files with 122 additions and 45 deletions
|
@ -321,7 +321,7 @@ class TestHostsManager:
|
|||
manager.edit_mode = True
|
||||
|
||||
hosts_file = HostsFile()
|
||||
entry = HostEntry("127.0.0.1", ["localhost"], is_active=True)
|
||||
entry = HostEntry("192.168.1.1", ["router"], is_active=True) # Non-default entry
|
||||
hosts_file.entries.append(entry)
|
||||
|
||||
success, message = manager.toggle_entry(hosts_file, 0)
|
||||
|
@ -363,7 +363,7 @@ class TestHostsManager:
|
|||
manager.edit_mode = True
|
||||
|
||||
hosts_file = HostsFile()
|
||||
entry1 = HostEntry("127.0.0.1", ["localhost"])
|
||||
entry1 = HostEntry("10.0.0.1", ["test1"]) # Non-default entries
|
||||
entry2 = HostEntry("192.168.1.1", ["router"])
|
||||
hosts_file.entries.extend([entry1, entry2])
|
||||
|
||||
|
@ -372,7 +372,7 @@ class TestHostsManager:
|
|||
assert success
|
||||
assert "moved up" in message
|
||||
assert hosts_file.entries[0].hostnames[0] == "router"
|
||||
assert hosts_file.entries[1].hostnames[0] == "localhost"
|
||||
assert hosts_file.entries[1].hostnames[0] == "test1"
|
||||
|
||||
def test_move_entry_up_invalid_index(self):
|
||||
"""Test moving entry up with invalid index."""
|
||||
|
@ -396,7 +396,7 @@ class TestHostsManager:
|
|||
manager.edit_mode = True
|
||||
|
||||
hosts_file = HostsFile()
|
||||
entry1 = HostEntry("127.0.0.1", ["localhost"])
|
||||
entry1 = HostEntry("10.0.0.1", ["test1"]) # Non-default entries
|
||||
entry2 = HostEntry("192.168.1.1", ["router"])
|
||||
hosts_file.entries.extend([entry1, entry2])
|
||||
|
||||
|
@ -405,7 +405,7 @@ class TestHostsManager:
|
|||
assert success
|
||||
assert "moved down" in message
|
||||
assert hosts_file.entries[0].hostnames[0] == "router"
|
||||
assert hosts_file.entries[1].hostnames[0] == "localhost"
|
||||
assert hosts_file.entries[1].hostnames[0] == "test1"
|
||||
|
||||
def test_move_entry_down_invalid_index(self):
|
||||
"""Test moving entry down with invalid index."""
|
||||
|
@ -429,7 +429,7 @@ class TestHostsManager:
|
|||
manager.edit_mode = True
|
||||
|
||||
hosts_file = HostsFile()
|
||||
entry = HostEntry("127.0.0.1", ["localhost"])
|
||||
entry = HostEntry("10.0.0.1", ["test"]) # Non-default entry
|
||||
hosts_file.entries.append(entry)
|
||||
|
||||
success, message = manager.update_entry(
|
||||
|
@ -449,7 +449,7 @@ class TestHostsManager:
|
|||
manager.edit_mode = True
|
||||
|
||||
hosts_file = HostsFile()
|
||||
entry = HostEntry("127.0.0.1", ["localhost"])
|
||||
entry = HostEntry("127.0.0.1", ["localhost"]) # Default entry - cannot be modified
|
||||
hosts_file.entries.append(entry)
|
||||
|
||||
success, message = manager.update_entry(
|
||||
|
@ -457,7 +457,7 @@ class TestHostsManager:
|
|||
)
|
||||
|
||||
assert not success
|
||||
assert "Invalid entry data" in message
|
||||
assert "Cannot modify default system entries" in message
|
||||
|
||||
@patch('tempfile.NamedTemporaryFile')
|
||||
@patch('subprocess.run')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue