diff --git a/.github/workflows/scripts-test.yml b/.github/workflows/scripts-test.yml index 4bec3cba72..05af993466 100644 --- a/.github/workflows/scripts-test.yml +++ b/.github/workflows/scripts-test.yml @@ -2,6 +2,9 @@ name: scripts-test on: workflow_dispatch: + push: + branches: [main] + pull_request: permissions: contents: read @@ -33,3 +36,5 @@ jobs: run: python3 -m unittest discover -s scripts/tests -p 'test_worker_preflight.py' -v - name: Run bitwarden-load-secrets multiline contract tests (#5650) run: python3 -m unittest discover -s scripts/tests -p 'test_bitwarden_load_secrets.py' -v + - name: Run power-guard timing validation tests (#6477) + run: python3 -m unittest discover -s nixos/services/power-guard -p 'test_*.py' -v diff --git a/nixos/services/power-guard/power-guard.py b/nixos/services/power-guard/power-guard.py index 4b3f13bdb2..4eb2d178f4 100644 --- a/nixos/services/power-guard/power-guard.py +++ b/nixos/services/power-guard/power-guard.py @@ -27,19 +27,70 @@ POWER_GUARD_EDGE_CONFIRM. """ +import math import os import subprocess import sys import time SYSFS = os.environ.get("POWER_GUARD_SYSFS", "/sys/class/power_supply") -POLL = float(os.environ.get("POWER_GUARD_POLL", "15")) + +# Largest interval time.sleep can represent: C PyTime_t is a signed +# 64-bit count of nanoseconds, so a finite value beyond ~292 years +# still overflows time.sleep at runtime (OverflowError) mid-loop. +MAX_TIMING_SECONDS = (2**63 - 1) / 1e9 + + +def parse_timing(name, raw): + """Parse one timing override (seconds). + + Invariant (the daemon's reason to exist, #1687/#6477): a timing + override is only meaningful as a finite, strictly positive number + of seconds no larger than MAX_TIMING_SECONDS (the largest interval + time.sleep can represent). Zero or negative confirmation would fire + on the first sample; a non-positive poll cannot pace the loop. + Raises ValueError naming the variable and the offending value. + """ + try: + value = float(raw) + except (TypeError, ValueError): + raise ValueError( + f"{name}={raw!r} is not a number; " + "it must be a finite positive number of seconds" + ) from None + if not math.isfinite(value) or value <= 0: + raise ValueError( + f"{name}={raw!r} must be a finite positive number of seconds" + ) + if value > MAX_TIMING_SECONDS: + raise ValueError( + f"{name}={raw!r} exceeds the largest interval time.sleep " + f"can represent ({MAX_TIMING_SECONDS:.0f} seconds)" + ) + return value + + +def timing_from_env(name, default): + """Read a timing override, failing closed at startup on bad input. + + On an invalid value, emit one named error (no traceback) and exit + non-zero before any polling, so a systemd unit shows as failed + instead of crash-looping or silently running unguarded. + """ + raw = os.environ.get(name, default) + try: + return parse_timing(name, raw) + except ValueError as exc: + print(f"power-guard: error: {exc}", file=sys.stderr, flush=True) + raise SystemExit(2) from None + +POLL = timing_from_env("POWER_GUARD_POLL", "15") LOW_PCT = 20 # engage the block at/below this (UPower PercentageLow) CRITICAL_PCT = 5 # UPower PercentageCritical EDGE_PCT = 2 # UPower PercentageAction CRITICAL_TTE = 300 # seconds-to-empty treated as critical -CONFIRM = float(os.environ.get("POWER_GUARD_CONFIRM", "300")) -EDGE_CONFIRM = float(os.environ.get("POWER_GUARD_EDGE_CONFIRM", "60")) +CONFIRM = timing_from_env("POWER_GUARD_CONFIRM", "300") +EDGE_CONFIRM = timing_from_env("POWER_GUARD_EDGE_CONFIRM", "60") NO_INHIBIT = os.environ.get("POWER_GUARD_NO_INHIBIT") == "1" SLEEP_CMD = os.environ.get("POWER_GUARD_SLEEP_CMD", "systemctl hybrid-sleep") diff --git a/nixos/services/power-guard/test_power_guard.py b/nixos/services/power-guard/test_power_guard.py new file mode 100644 index 0000000000..752bade4fa --- /dev/null +++ b/nixos/services/power-guard/test_power_guard.py @@ -0,0 +1,250 @@ +#!/usr/bin/env python3 +"""Tests for power-guard timing-override validation (#6477). + +Oracle: the acceptance criteria of #6477 plus the daemon's defining +invariant from #1687 — hybrid-sleep may only fire after a *sustained* +critical state, so a timing override is valid only as a finite, +strictly positive number of seconds no larger than the largest +interval time.sleep can represent. Invalid overrides are decided by +the parser contract (named startup error, non-zero exit, before +polling, marker file from the stubbed sleep command never written); +valid behaviour is decided by the fake-sysfs probe scenarios A-E from +the issue, run against the real daemon subprocess through its +documented POWER_GUARD_SYSFS / POWER_GUARD_NO_INHIBIT / +POWER_GUARD_SLEEP_CMD hooks. +""" + +import importlib.util +import math +import os +import random +import subprocess +import sys +import tempfile +import time +import unittest +from pathlib import Path + +HERE = Path(__file__).resolve().parent +DAEMON = HERE / "power-guard.py" + +TIMING_VARS = ("POWER_GUARD_POLL", "POWER_GUARD_CONFIRM", "POWER_GUARD_EDGE_CONFIRM") +BAD_VALUES = ["abc", "", "nan", "NaN", "inf", "-inf", "0", "0.0", "-1", "-5", "1e10", "1e100"] + + +def load_daemon(): + """Import power-guard.py with a clean, valid timing environment.""" + env_backup = {k: os.environ.get(k) for k in TIMING_VARS} + for k in TIMING_VARS: + os.environ.pop(k, None) + try: + spec = importlib.util.spec_from_file_location("power_guard", DAEMON) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + finally: + for k, v in env_backup.items(): + if v is None: + os.environ.pop(k, None) + else: + os.environ[k] = v + + +pg = load_daemon() + + +class ParseTimingTests(unittest.TestCase): + def test_valid_values(self): + for raw, want in [("15", 15.0), ("0.2", 0.2), ("300", 300.0), ("1e2", 100.0)]: + with self.subTest(raw=raw): + self.assertEqual(pg.parse_timing("POWER_GUARD_POLL", raw), want) + + def test_defaults_are_valid_and_unchanged(self): + self.assertEqual(pg.POLL, 15.0) + self.assertEqual(pg.CONFIRM, 300.0) + self.assertEqual(pg.EDGE_CONFIRM, 60.0) + + def test_bad_values_rejected_naming_variable_and_value(self): + for var in TIMING_VARS: + for raw in BAD_VALUES: + with self.subTest(var=var, raw=raw): + with self.assertRaises(ValueError) as ctx: + pg.parse_timing(var, raw) + self.assertIn(var, str(ctx.exception)) + self.assertIn(repr(raw), str(ctx.exception)) + + +class ParseTimingPropertyTests(unittest.TestCase): + """Property-based coverage of the parser invariant (Codex #6485). + + Oracle: the acceptance predicate itself, evaluated + independently of parse_timing's control flow — a generated input + is acceptable iff float() parses it to a finite value > 0 and + no larger than MAX_TIMING_SECONDS (the time.sleep bound). + Generation is seeded, so failures reproduce exactly. + """ + + def oracle_accepts(self, raw): + try: + value = float(raw) + except (TypeError, ValueError): + return False + return math.isfinite(value) and 0 < value <= pg.MAX_TIMING_SECONDS + + @staticmethod + def generated_inputs(rng): + cases = [] + for _ in range(200): # plausible valid timings, many formats + v = rng.choice( + [ + rng.uniform(0.001, 100000), + rng.randint(1, 100000), + rng.uniform(0.001, 1), + ] + ) + cases.append(rng.choice([repr(v), f"{v:.6f}", f"{v:e}", f" {v} "])) + for _ in range(200): # invalid: zero/negative/non-finite/junk + cases.append( + rng.choice( + [ + repr(-rng.uniform(0, 100000)), + "0", + "-0.0", + rng.choice(["nan", "NaN", "inf", "-inf", "infinity"]), + "".join(rng.choices("abcxyz_=!@#", k=rng.randint(1, 8))), + f"{rng.uniform(2, 9)}e{rng.randint(10, 100)}", + f"{rng.uniform(1, 9)}x{rng.randint(0, 9)}", + "", + ] + ) + ) + return cases + + def test_generated_inputs_match_finite_positive_predicate(self): + rng = random.Random(6477) + for raw in self.generated_inputs(rng): + with self.subTest(raw=raw): + if self.oracle_accepts(raw): + self.assertEqual( + pg.parse_timing("POWER_GUARD_CONFIRM", raw), float(raw) + ) + else: + with self.assertRaises(ValueError) as ctx: + pg.parse_timing("POWER_GUARD_CONFIRM", raw) + self.assertIn("POWER_GUARD_CONFIRM", str(ctx.exception)) + self.assertIn(repr(raw), str(ctx.exception)) + + +def make_fake_sysfs(root: Path, capacity=3, status="Discharging", ac_online=0): + ac = root / "ACAD" + ac.mkdir(parents=True) + (ac / "type").write_text("Mains\n") + (ac / "online").write_text(f"{ac_online}\n") + bat = root / "BAT0" + bat.mkdir() + (bat / "type").write_text("Battery\n") + (bat / "capacity").write_text(f"{capacity}\n") + (bat / "status").write_text(f"{status}\n") + + +class DaemonSubprocessTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + base = Path(self.tmp.name) + self.sysfs = base / "sysfs" + make_fake_sysfs(self.sysfs) + self.marker = base / "slept" + + def daemon_env(self, **timing): + env = dict(os.environ) + for k in TIMING_VARS: + env.pop(k, None) + env.update( + POWER_GUARD_SYSFS=str(self.sysfs), + POWER_GUARD_NO_INHIBIT="1", + POWER_GUARD_SLEEP_CMD=f"touch {self.marker}", + ) + env.update(timing) + return env + + def run_daemon(self, env, seconds): + proc = subprocess.Popen( + [sys.executable, str(DAEMON)], + env=env, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + try: + out, err = proc.communicate(timeout=seconds) + return proc.returncode, out, err, True + except subprocess.TimeoutExpired: + proc.terminate() + out, err = proc.communicate(timeout=10) + return proc.returncode, out, err, False + + def test_invalid_overrides_exit_nonzero_named_no_traceback_no_fire(self): + cases = [ + {"POWER_GUARD_CONFIRM": "0"}, + {"POWER_GUARD_CONFIRM": "-5"}, + {"POWER_GUARD_CONFIRM": "abc"}, + {"POWER_GUARD_EDGE_CONFIRM": "0"}, + {"POWER_GUARD_EDGE_CONFIRM": "nan"}, + {"POWER_GUARD_POLL": "abc"}, + {"POWER_GUARD_POLL": "-1"}, + {"POWER_GUARD_POLL": "inf"}, + {"POWER_GUARD_POLL": "1e100"}, + ] + for timing in cases: + var = next(iter(timing)) + with self.subTest(**timing): + rc, out, err, exited = self.run_daemon(self.daemon_env(**timing), 10) + self.assertTrue(exited, "daemon kept polling on an invalid override") + self.assertNotEqual(rc, 0) + self.assertIn(var, err) + self.assertIn(repr(timing[var]), err) + self.assertNotIn("Traceback", err) + self.assertFalse(self.marker.exists(), "sleep command fired") + + def test_valid_confirm_300_does_not_fire_on_first_samples(self): + # Issue probe B (control): sustained requirement intact. + env = self.daemon_env(POWER_GUARD_POLL="0.05", POWER_GUARD_CONFIRM="300") + rc, out, err, exited = self.run_daemon(env, 1.0) + self.assertFalse(exited, "daemon should still be polling") + self.assertIn("CRITICAL state entered", out) + self.assertFalse(self.marker.exists()) + + def test_defaults_do_not_fire_and_report_documented_timings(self): + rc, out, err, exited = self.run_daemon(self.daemon_env(), 1.0) + self.assertFalse(exited, "daemon should still be polling") + self.assertIn("poll=15.0s", out) + self.assertIn("confirm=300.0s", out) + self.assertIn("edge_confirm=60.0s", out) + self.assertFalse(self.marker.exists()) + + def test_valid_small_confirm_still_fires_after_sustained_critical(self): + # Valid overrides behave as before: a genuinely sustained + # critical state (CONFIRM=0.3s of continuous 3% samples) fires. + env = self.daemon_env(POWER_GUARD_POLL="0.05", POWER_GUARD_CONFIRM="0.3") + deadline = time.time() + 10 + proc = subprocess.Popen( + [sys.executable, str(DAEMON)], + env=env, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + try: + while time.time() < deadline and not self.marker.exists(): + if proc.poll() is not None: + break + time.sleep(0.05) + finally: + proc.terminate() + proc.communicate(timeout=10) + self.assertTrue(self.marker.exists(), "sustained critical state never fired") + + +if __name__ == "__main__": + unittest.main()