test_disk_forensics_lesson.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # SPDX-License-Identifier: Apache-2.0
  2. """The disk forensics lesson: it is registered, honest about what was run, and safe to follow.
  3. docs/wiki/Disk-Forensics-with-Autopsy-and-The-Sleuth-Kit.md teaches imaging, hashing first and The Sleuth Kit commands
  4. on a practice image the reader builds. A broken link, a missing registration, a claim with no status, a real address,
  5. a missing authorization rule or a hash step placed after the examination would each teach the wrong thing.
  6. """
  7. import re
  8. from pathlib import Path
  9. ROOT = Path(__file__).resolve().parent.parent
  10. WIKI = ROOT / "docs" / "wiki"
  11. NAME = "Disk-Forensics-with-Autopsy-and-The-Sleuth-Kit"
  12. PAGE = WIKI / f"{NAME}.md"
  13. LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#[^)]*)?\)")
  14. ADDR = re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b")
  15. STATUS_WORDS = ("Read", "Observed", "Search summary only", "**est.**")
  16. def text() -> str:
  17. return PAGE.read_text(encoding="utf-8")
  18. def register_rows(body: str) -> list[list[str]]:
  19. reg = body.split("## Claims register")[1].split("Not verified, plainly")[0]
  20. rows = [line for line in reg.splitlines() if line.startswith("|") and not line.startswith("|---")]
  21. return [[c.strip() for c in r.strip("|").split("|")] for r in rows[1:]]
  22. def rows_missing_status(body: str) -> list[str]:
  23. return [r[0] for r in register_rows(body) if not any(w in r[-1] for w in STATUS_WORDS)]
  24. def test_the_page_says_what_was_and_was_not_run():
  25. body = text()
  26. assert body.startswith("# Disk forensics with Autopsy and The Sleuth Kit")
  27. assert "Status: unverified draft" in body
  28. assert "Autopsy itself was not installed or started" in body
  29. assert "**Not run:**" in body and "2026-11-07" in body
  30. assert "Not verified, plainly" in body
  31. def test_every_linked_wiki_page_exists_and_the_gap_check_links_are_present():
  32. links = set(LINK.findall(text()))
  33. missing = sorted(t for t in links if not (WIKI / f"{t}.md").exists())
  34. assert not missing, missing
  35. for needed in (
  36. "Hashing-Encryption-and-Keys",
  37. "Virtual-Machines-Hypervisors-Desktop-and-Datacenter-Options",
  38. "Terminal-and-Your-First-Script",
  39. ):
  40. assert needed in links, needed
  41. def test_sibling_forensics_lessons_are_plain_text_not_links():
  42. body = text()
  43. for issue in ("#474", "#476", "#477", "#478"):
  44. assert issue in body, issue
  45. assert not re.search(r"\]\((?:[^)]*)(Memory|Network|Toolkits|Foundations)[^)]*Forensic", body)
  46. def test_the_lesson_is_registered_everywhere():
  47. nav = (WIKI / "_nav.yaml").read_text(encoding="utf-8")
  48. line = nav.split(f"page: {NAME},")[1].split("\n")[0]
  49. assert 'status: "draft"' in line and 'subgroup: "Digital forensics"' in line
  50. for special in ("Contents.md", "_Sidebar.md", "Reading-Order.md"):
  51. assert f"{NAME}.md" in (WIKI / special).read_text(encoding="utf-8"), special
  52. rows = [l for l in (WIKI / "Glossary.md").read_text(encoding="utf-8").splitlines() if f"{NAME}.md" in l]
  53. assert len(rows) >= 4
  54. def test_glossary_rows_point_at_real_headings():
  55. heads = {re.sub(r"[^a-z0-9-]", "", re.sub(r"[^a-z0-9 -]", "", h.lower()).strip().replace(" ", "-")) for h in re.findall(r"^#+ (.+)$", text(), re.MULTILINE)}
  56. anchors = set()
  57. for line in (WIKI / "Glossary.md").read_text(encoding="utf-8").splitlines():
  58. anchors.update(re.findall(rf"{NAME}\.md#([a-z0-9-]+)", line))
  59. assert anchors, "the guard collected nothing"
  60. assert anchors <= heads, sorted(anchors - heads)
  61. def test_no_addresses_no_em_dashes_no_callsign_style_placeholders_missing():
  62. body = text()
  63. assert not ADDR.findall(body)
  64. assert "—" not in body
  65. def test_the_authorization_rule_is_stated_up_front():
  66. body = text()
  67. assert body.index("Examine only your own devices or data you are explicitly authorized to examine") < body.index("## What you will be able to do")
  68. assert "not legal advice" in body
  69. def test_the_hash_step_comes_before_the_examination_commands():
  70. body = text()
  71. assert body.index("sha256sum lab.dd > lab.dd.sha256") < body.index("mmls lab.dd\n fsstat")
  72. assert re.search(r"`[0-9a-f]{64}`|[0-9a-f]{64} lab\.dd", body)
  73. def test_every_claims_register_row_has_a_status():
  74. rows = register_rows(text())
  75. assert len(rows) >= 25, "the guard collected too few rows"
  76. assert not rows_missing_status(text())
  77. def test_the_register_guard_bites():
  78. bad = "## Claims register\n\n| Claim | Source | Status |\n|---|---|---|\n| X | y | maybe |\n\nNot verified, plainly"
  79. assert rows_missing_status(bad) == ["X"]
  80. def test_versions_and_licenses_are_dated():
  81. body = text()
  82. for needle in ("4.23.1", "4.15.0", "2026-05-07", "2026-04-15", "Apache License", "Common Public License", "October 2023"):
  83. assert needle in body, needle