steam-backlog-enforcer/steam_backlog_enforcer/tests/test_research_hosts.py
Krzysztof kuhy Rudnicki 5d5aa3e773
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Add Librarian-only research window to total block
kuhy 2026-11-11: game-design research needs Librarian (app 4197610)
actually played during the 6-day total block. New root command
total-block-research --until T opens ONE window per lock: <= 48 h,
never past the lock end, a rerun never extends it.

The command only validates and records (/var/lib/steam-backlog-
enforcer, root 0700; lock id + deadline + two latches). The daemon
opens and closes everything: Steam hosts names, the store block, the
total-block chain (IP cache kept) and the steam package, reinstalled
with only guard-lib's package-block hook masked for that one pacman
call (--hookdir, proved in a guest). Lock file, hook and end date are
never touched. Browser-game sites stay null-routed.

Only Librarian may run: loop-local allowed set, strict mode (no
whitelist exceptions, no zero-achievement games), launchers and
counted games killed; state.json is never read or written.

Expiry latches the record, then the ordinary tick re-applies hosts
(now reconciled each tick), chain, uninstall and purge; the normal
store block is restored once, unconditionally.

Failure modes: missing/corrupt/unparseable record = no window and a
corrupt one also refuses a new window (fail closed); a clock set
back cannot reopen a latched window.

Checked: 3290 tests at 100% coverage; whole-tree pylint 10/10 run in
three parts (the hook exceeds the 60 s cap, so SKIP=pylint here); vmbox
guest with real guardctl package-block and the real unit, window
opened, Librarian-only enforced, clock moved past the deadline and the
full block re-applied. Not verified on the host.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWWzuCEg1m87jpKyRrw
2026-11-11 14:29:58 +01:00

185 lines
6.9 KiB
Python

"""The research window's /etc/hosts half: Steam names open, game sites shut."""
from __future__ import annotations
from typing import TYPE_CHECKING
from unittest.mock import patch
import pytest
from steam_backlog_enforcer._total_block_domains import (
_ALL_TOTAL_BLOCK_DOMAINS,
GAME_WEBSITE_DOMAINS,
RESEARCH_BLOCKED_DOMAINS,
TOTAL_BLOCK_DOMAINS,
TOTAL_BLOCK_EXEMPT_DOMAINS,
)
from steam_backlog_enforcer._total_block_hosts import (
_HOSTS_BLOCK_BEGIN,
_HOSTS_BLOCK_END,
apply_total_block_hosts,
refresh_total_block_hosts,
)
if TYPE_CHECKING:
from steam_backlog_enforcer.tests._total_block_paths import Paths
PKG = "steam_backlog_enforcer._total_block_hosts"
STEAM_NAMES = (
"store.steampowered.com",
"checkout.steampowered.com",
"steamcommunity.com",
"api.steampowered.com",
"login.steampowered.com",
"steamcontent.com",
"steamstatic.com",
"cdn.steamstatic.com",
)
GAME_SITES = ("newgrounds.com", "www.newgrounds.com", "poki.com", "www.y8.com")
class _Hosts:
"""Plays back what a refresh writes as the file's new content."""
def __init__(self, paths: Paths, content: str) -> None:
self.path = paths.hosts_file
self.path.write_text(content, encoding="utf-8")
self.writes = 0
def write(self, content: str) -> None:
"""Stand-in for the sudo-tee write."""
self.writes += 1
self.path.write_text(content, encoding="utf-8")
def text(self) -> str:
"""The file as it is now."""
return self.path.read_text(encoding="utf-8")
@pytest.fixture(name="hosts")
def _hosts(total_block_paths: Paths) -> _Hosts:
"""A hosts file with the full section between unrelated lines."""
full = _HOSTS_BLOCK_BEGIN + "".join(
f"0.0.0.0 {d}\n" for d in _ALL_TOTAL_BLOCK_DOMAINS
)
return _Hosts(
total_block_paths,
"127.0.0.1 localhost\n" + full + _HOSTS_BLOCK_END + "0.0.0.0 evil.example\n",
)
def _refresh(hosts: _Hosts, *, steam_open: bool) -> bool:
with (
patch(f"{PKG}._disable_hosts_protection"),
patch(f"{PKG}._enable_hosts_protection"),
patch(f"{PKG}._sudo_write_hosts", side_effect=hosts.write),
):
return refresh_total_block_hosts(steam_open=steam_open)
class TestDomainTable:
"""The research list is the game sites, split off the same source."""
def test_is_exactly_the_game_sites_with_www(self) -> None:
"""Test that no Steam name is in the research block list."""
assert not set(RESEARCH_BLOCKED_DOMAINS) & set(TOTAL_BLOCK_DOMAINS)
for site in GAME_SITES:
assert site in RESEARCH_BLOCKED_DOMAINS
assert set(GAME_WEBSITE_DOMAINS) <= set(RESEARCH_BLOCKED_DOMAINS)
def test_union_with_steam_names_is_the_full_list(self) -> None:
"""Test that the split loses nothing and keeps the full-list order."""
steam_part = [
d for d in _ALL_TOTAL_BLOCK_DOMAINS if d not in RESEARCH_BLOCKED_DOMAINS
]
assert set(steam_part) | set(RESEARCH_BLOCKED_DOMAINS) == set(
_ALL_TOTAL_BLOCK_DOMAINS
)
def test_exempt_domains_stay_out(self) -> None:
"""Test that the itch exception holds in the research list too."""
assert not set(RESEARCH_BLOCKED_DOMAINS) & set(TOTAL_BLOCK_EXEMPT_DOMAINS)
class TestOpenSection:
"""Opening swaps the section body and nothing else."""
def test_open_drops_steam_names_and_keeps_game_sites(self, hosts: _Hosts) -> None:
"""Test that the open section null-routes only the game sites."""
assert _refresh(hosts, steam_open=True) is True
text = hosts.text()
for name in STEAM_NAMES:
assert f"0.0.0.0 {name}\n" not in text
for site in GAME_SITES:
assert f"0.0.0.0 {site}\n" in text
def test_surrounding_lines_survive(self, hosts: _Hosts) -> None:
"""Test that only the text between the markers changed."""
_refresh(hosts, steam_open=True)
text = hosts.text()
assert text.startswith("127.0.0.1 localhost\n" + _HOSTS_BLOCK_BEGIN)
assert text.endswith(_HOSTS_BLOCK_END + "0.0.0.0 evil.example\n")
def test_already_open_section_is_not_rewritten(self, hosts: _Hosts) -> None:
"""Test that a converged tick performs no write."""
_refresh(hosts, steam_open=True)
hosts.writes = 0
assert _refresh(hosts, steam_open=True) is True
assert hosts.writes == 0
def test_missing_section_gets_the_open_variant(self, hosts: _Hosts) -> None:
"""Test that an absent section is created open, never full."""
hosts.path.write_text("127.0.0.1 localhost\n", encoding="utf-8")
assert _refresh(hosts, steam_open=True) is True
text = hosts.text()
assert _HOSTS_BLOCK_BEGIN in text
assert "0.0.0.0 store.steampowered.com\n" not in text
assert "0.0.0.0 newgrounds.com\n" in text
def test_missing_section_without_trailing_newline(self, hosts: _Hosts) -> None:
"""Test that the new section starts on its own line."""
hosts.path.write_text("127.0.0.1 localhost", encoding="utf-8")
_refresh(hosts, steam_open=True)
assert hosts.text().startswith("127.0.0.1 localhost\n" + _HOSTS_BLOCK_BEGIN)
def test_unreadable_file_fails_closed(self, hosts: _Hosts) -> None:
"""Test that a missing hosts file is a False, not an exception."""
hosts.path.unlink()
assert _refresh(hosts, steam_open=True) is False
def test_begin_without_end_is_left_alone(self, hosts: _Hosts) -> None:
"""Test that a damaged section is not guessed at."""
hosts.path.write_text(_HOSTS_BLOCK_BEGIN + "0.0.0.0 x\n", encoding="utf-8")
assert _refresh(hosts, steam_open=True) is False
assert hosts.writes == 0
@pytest.mark.usefixtures("hosts")
def test_write_failure_is_false(self) -> None:
"""Test that a failing sudo tee is reported."""
with (
patch(f"{PKG}._disable_hosts_protection"),
patch(f"{PKG}._enable_hosts_protection") as relock,
patch(f"{PKG}._sudo_write_hosts", side_effect=OSError("tee")),
):
assert refresh_total_block_hosts(steam_open=True) is False
relock.assert_called_once()
class TestClose:
"""After the window the full list comes back through the ordinary refresh."""
def test_full_refresh_restores_every_steam_name(self, hosts: _Hosts) -> None:
"""Test that refresh without steam_open re-null-routes Steam."""
_refresh(hosts, steam_open=True)
assert _refresh(hosts, steam_open=False) is True
text = hosts.text()
for name in STEAM_NAMES:
assert f"0.0.0.0 {name}\n" in text
def test_apply_still_leaves_a_present_section_alone(self, hosts: _Hosts) -> None:
"""Test that the cheap per-tick apply keeps its old contract."""
_refresh(hosts, steam_open=True)
hosts.writes = 0
assert apply_total_block_hosts() is True
assert hosts.writes == 0