test_blast_radius_lesson.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. """The blast radius lesson: its exercise script runs, and its claims register, sections and links hold.
  2. docs/wiki/Plan-for-Failure-Blast-Radius-and-Assuming-Breach.md teaches a mindset and a small Python
  3. exercise that computes what one compromised node reaches. A script that does not run, or numbers on
  4. the page that the script does not produce, teach the wrong thing. The test pulls the script out of
  5. the page, runs it, and checks the output shown on the page against the real output.
  6. """
  7. import re
  8. import subprocess
  9. import sys
  10. from pathlib import Path
  11. import pytest
  12. from wikitext import GLOSSARY_FILES, glossary_text, page_text
  13. WIKI = Path(__file__).resolve().parent.parent / "docs" / "wiki"
  14. PAGE = WIKI / "Plan-for-Failure-Blast-Radius-and-Assuming-Breach.md"
  15. FENCE = re.compile(r"^```(\w*)\n(.*?)^```$", re.MULTILINE | re.DOTALL)
  16. LINK = re.compile(r"\]\(([A-Za-z0-9_-]+\.md)(?:#[^)]*)?\)")
  17. SECTIONS = [
  18. "## What you will be able to do",
  19. "## The maintainer's framing, in his words",
  20. "## In plain words",
  21. "## Three ideas, defined once",
  22. "## Why defenders have to be right every time",
  23. "## Contain, detect, recover, and prevent",
  24. "## Where this shows up in this project",
  25. "## What does not count as protection",
  26. "## Honest limits",
  27. "## Free path: a lab with nothing to lose",
  28. "## Checkpoint",
  29. "## Common mistakes",
  30. "## Claims register",
  31. ]
  32. def text() -> str:
  33. return PAGE.read_text(encoding="utf-8")
  34. def script() -> str:
  35. found = [b for tag, b in FENCE.findall(text()) if tag == "python" and "radius.py - what does" in b]
  36. assert len(found) == 1, f"expected one radius.py block, found {len(found)}"
  37. return found[0]
  38. def run(path: Path, *args: str) -> subprocess.CompletedProcess:
  39. return subprocess.run([sys.executable, "-I", str(path), *args], capture_output=True, text=True, timeout=60, check=False)
  40. @pytest.fixture()
  41. def radius(tmp_path):
  42. p = tmp_path / "radius.py"
  43. p.write_text(script())
  44. return p
  45. def test_default_run_matches_the_output_shown_on_the_page(radius):
  46. done = run(radius)
  47. assert done.returncode == 0, done.stderr
  48. shown = [b for tag, b in FENCE.findall(text()) if tag == "text" and b.startswith("Before:")]
  49. assert len(shown) == 1
  50. assert done.stdout.strip() == shown[0].strip()
  51. def test_cutting_the_edge_shrinks_the_radius_and_states_a_cost(radius):
  52. out = run(radius).stdout
  53. assert "web compromised: reach went from 1 to 0." in out
  54. assert "What it costs:" in out
  55. def test_a_second_cut_changes_the_laptop_not_the_web_server(radius):
  56. out = run(radius, "laptop", "vault").stdout
  57. assert "laptop compromised: reach went from 3 to 2." in out
  58. def test_a_missing_edge_exits_two(radius):
  59. done = run(radius, "x", "y")
  60. assert done.returncode == 2 and "usage" in done.stderr
  61. def test_required_sections_are_present_in_order():
  62. body = text()
  63. positions = [body.index(s) for s in SECTIONS]
  64. assert positions == sorted(positions)
  65. def test_the_maintainers_framing_is_quoted_and_attributed():
  66. body = text()
  67. assert "we will fail to protect from those attacks" in body
  68. assert "they only have to be right once" in body
  69. assert "ChiefGyk3D" in body and "Grammar fixes only" in body
  70. def test_the_three_questions_box_is_present():
  71. body = text()
  72. for q in ("what can it reach", "How will I know", "How do I recover"):
  73. assert q in body
  74. def test_claims_register_marks_unrun_and_estimates():
  75. reg = text().split("## Claims register")[1]
  76. assert "2026-10-22" in reg
  77. assert "est." in reg and "Not run" in reg
  78. assert "**Observed**" in reg
  79. def test_no_em_dashes_in_the_lesson():
  80. assert "—" not in text()
  81. def test_no_machine_addresses_in_the_lesson():
  82. assert not re.search(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", text())
  83. def test_every_linked_wiki_page_exists():
  84. missing = sorted({t for t in LINK.findall(text()) if not (WIKI / t).exists()})
  85. assert not missing, missing
  86. def test_glossary_has_exactly_one_blast_radius_row_and_it_links_the_lesson():
  87. rows = [l for l in glossary_text(WIKI).splitlines() if l.startswith("| **Blast radius**")]
  88. assert len(rows) == 1
  89. assert PAGE.name in rows[0]
  90. # both earlier references are kept
  91. assert "WordPress-at-Scale-CDN-WAF-and-CI.md#tenant-isolation" in rows[0]
  92. assert "Build-and-Destroy-Stack-Per-Service-Part-1-Why-and-How-Stacks-Are-Split-and-the-Lab-Script.md#why-one-stack-per-service" in rows[0]
  93. def test_glossary_defines_assume_breach_and_defense_in_depth():
  94. g = glossary_text(WIKI)
  95. assert "| **Assume breach** |" in g and "| **Defense in depth** |" in g
  96. def test_at_least_four_existing_pages_link_the_lesson_at_first_use():
  97. # Issue #417: the generated navigation blocks link neighbors and are not the author's first-use links, so
  98. # each page is read without them.
  99. users = [p.name for p in WIKI.glob("*.md") if p != PAGE and p.name not in {*GLOSSARY_FILES, "_Sidebar.md", "Reading-Order.md", "Home.md"} and PAGE.name in page_text(p)]
  100. # Home.md is excluded like the other navigation pages: since issue #378 its generated map links every page.
  101. # 9, not 8: issue #230 added the required links from Droplet hardening and the threats lesson.
  102. assert 4 <= len(users) <= 9, users
  103. def test_the_lesson_is_in_contents_and_reading_order():
  104. assert PAGE.name in (WIKI / "Contents.md").read_text()
  105. assert PAGE.name in (WIKI / "Reading-Order.md").read_text()
  106. def test_powershell_text_validates_the_edge_before_printing():
  107. ps = [b for tag, b in FENCE.findall(text()) if tag == "powershell" and "radius.ps1" in b]
  108. assert len(ps) == 1
  109. body = ps[0]
  110. assert body.index("exit 2") < body.index('Show-Table "Before:"')
  111. assert "usage:" in body
  112. def test_credential_scope_is_called_out_as_a_separate_boundary():
  113. body = text()
  114. assert "Credentials are a separate boundary" in body
  115. assert "reaches only what is in that stack's state" in body