test_privacy_vs_security_lesson.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # SPDX-License-Identifier: Apache-2.0
  2. """The privacy-versus-security lesson: its citations exist, it is registered and honest about what was read.
  3. docs/wiki/Privacy-vs-Security-Blinds-and-Locks-Physical-Security-and-Monero.md has no script. It quotes the Monero
  4. project, the European Commission, NIST and two lockpicking-hobby pages, and it gives lockpicking rules. A missing
  5. registration, a source with no date, a real address, a wallet address, a lockpicking how-to or an overclaim about
  6. what Monero can defeat would each teach the wrong thing.
  7. """
  8. import re
  9. from pathlib import Path
  10. from wikitext import glossary_text, nav_text, page_text
  11. ROOT = Path(__file__).resolve().parent.parent
  12. WIKI = ROOT / "docs" / "wiki"
  13. NAME = "Privacy-vs-Security-Blinds-and-Locks-Physical-Security-and-Monero"
  14. PAGE = WIKI / f"{NAME}.md"
  15. LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#[^)]*)?\)")
  16. ADDR = re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b")
  17. # A Monero standard address is 95 base58 characters starting with 4 or 8.
  18. XMR_ADDR = re.compile(r"\b[48][1-9A-HJ-NP-Za-km-z]{94}\b")
  19. PATH_TOKEN = re.compile(r"`((?:modules|stacks|scripts|\.github|\.spacelift|tests)/[A-Za-z0-9_./-]+|\.gitignore)`")
  20. STATUS_WORDS = ("Read", "Not read", "Not claimed", "**est.**", "Nothing run")
  21. HOWTO_WORDS = ("tension wrench", "raking", "single pin picking", "how to pick", "bump key how", "shim a")
  22. def text() -> str:
  23. return page_text(PAGE) # without the generated navigation blocks (issue #417)
  24. def section(body: str, start: str, end: str) -> str:
  25. return body.split(start)[1].split(end)[0]
  26. def table_rows(block: str) -> list[list[str]]:
  27. rows = [line for line in block.splitlines() if line.startswith("|") and not line.startswith("|---")]
  28. return [[c.strip() for c in r.strip("|").split("|")] for r in rows[1:]]
  29. def register_rows(body: str) -> list[list[str]]:
  30. return table_rows(body.split("## Claims register")[1])
  31. def rows_missing_status(rows: list[list[str]]) -> list[str]:
  32. return [r[0] for r in rows if not any(w in r[-1] for w in STATUS_WORDS)]
  33. def rows_missing_date(rows: list[list[str]]) -> list[str]:
  34. """A row that points at a URL must carry the read date, or say it was not read."""
  35. return [r[0] for r in rows if "http" in r[1] and "2026-10-31" not in r[-1] and "Not read" not in r[-1]]
  36. def howto_hits(body: str) -> list[str]:
  37. low = body.lower()
  38. return [w for w in HOWTO_WORDS if w in low]
  39. def test_the_banner_says_what_was_and_was_not_read():
  40. body = text()
  41. assert body.startswith("# Privacy versus security")
  42. assert "Status: unverified draft" in body
  43. assert "**Not read:**" in body and "**Not run:**" in body and "2026-10-31" in body
  44. assert "Nothing here is legal advice" in body
  45. def test_every_linked_wiki_page_exists():
  46. links = set(LINK.findall(text()))
  47. assert len(links) >= 10, "the guard collected too few links"
  48. missing = sorted(t for t in links if not (WIKI / f"{t}.md").exists())
  49. assert not missing, missing
  50. def test_cited_repository_paths_exist():
  51. for p in PATH_TOKEN.findall(text()):
  52. assert (ROOT / p).exists(), p
  53. def test_the_path_guard_bites():
  54. assert PATH_TOKEN.findall("see `scripts/does_not_exist.py`") == ["scripts/does_not_exist.py"]
  55. assert not (ROOT / "scripts/does_not_exist.py").exists()
  56. def test_the_blast_radius_lesson_is_not_linked_so_its_link_cap_holds():
  57. # tests/test_blast_radius_lesson.py caps how many pages may link that lesson, and the cap is full.
  58. assert "Plan-for-Failure-Blast-Radius" not in text()
  59. def test_the_sibling_lessons_are_linked_now_that_they_exist():
  60. body = text()
  61. links = set(LINK.findall(body))
  62. tor = "Tor-I2P-and-the-Darknet-Without-the-Myths"
  63. crypto = "Cryptography-Fundamentals-Symmetric-Asymmetric-Hashing-Certificates-and-Keys"
  64. assert tor in links and crypto in links
  65. assert (WIKI / f"{tor}.md").is_file() and (WIKI / f"{crypto}.md").is_file()
  66. # The issue numbers stay where they carry meaning; the stale statements are gone.
  67. assert "issue 358" in body and "issue 360" in body
  68. assert "not merged yet" not in body and "Not yet. The lesson on Tor" not in body
  69. def test_the_lesson_is_registered_in_contents_reading_order_and_the_sidebar():
  70. nav = nav_text(WIKI)
  71. entry = nav.split(f"page: {NAME},")[1].split("\n")[0]
  72. assert 'status: "draft"' in entry
  73. assert "sidebar" not in entry
  74. for special in ("Contents.md", "Reading-Order.md"):
  75. assert f"{NAME}.md" in (WIKI / special).read_text(encoding="utf-8"), special
  76. # Issue #378: the sidebar lists every page, so the lesson is in it (it is still not pinned to the top).
  77. assert NAME in (WIKI / "_Sidebar.md").read_text(encoding="utf-8")
  78. def test_glossary_rows_point_at_real_headings():
  79. heads = {re.sub(r"[^a-z0-9-]", "", h.lower().replace(" ", "-")) for h in re.findall(r"^#+ (.+)$", text(), re.MULTILINE)}
  80. anchors = set()
  81. rows = 0
  82. for line in glossary_text(WIKI).splitlines():
  83. if f"{NAME}.md" in line:
  84. rows += 1
  85. anchors.update(re.findall(rf"{NAME}\.md#([a-z0-9-]+)", line))
  86. assert rows >= 6, "expected glossary rows for the new terms"
  87. assert anchors and anchors <= heads, sorted(anchors - heads)
  88. def test_no_addresses_wallet_addresses_or_em_dashes():
  89. body = text()
  90. assert not ADDR.findall(body)
  91. assert not XMR_ADDR.findall(body)
  92. assert "—" not in body
  93. def test_the_address_guards_bite():
  94. assert ADDR.findall("ping 192.0.2.7")
  95. assert XMR_ADDR.findall("4" + "A" * 94)
  96. def test_the_picture_overlaps_and_tensions_are_present():
  97. body = text()
  98. assert "Privacy is the blinds and drapes" in body and "Security is the locks" in body
  99. over = section(body, "## Where they overlap", "## Where they pull apart")
  100. for item in ("Encryption", "Access control", "Data minimization"):
  101. assert item in over, item
  102. apart = section(body, "## Where they pull apart", "## Monero")
  103. for item in ("A lock with no blind", "A blind with no lock", "Anonymity against accountability"):
  104. assert item in apart, item
  105. def test_monero_is_sourced_from_the_project_and_makes_no_overclaim():
  106. body = text()
  107. monero = section(body, "## Monero: a worked example", "## Privacy law basics")
  108. assert monero.count("https://www.getmonero.org/") >= 6
  109. assert "makes no claim about what Monero can or cannot defeat" in monero
  110. for prop in ("Key custody", "The view key", "Node trust", "Wallet and device security", "Software integrity"):
  111. assert prop in monero, prop
  112. for banned in ("untraceable by", "cannot be broken", "impossible to trace", "guarantees anonymity"):
  113. assert banned not in body.lower(), banned
  114. def test_privacy_law_is_high_level_dated_and_not_advice():
  115. law = section(text(), "## Privacy law basics", "## The physical side")
  116. for needle in ("Not legal advice", "Check your own jurisdiction", "Purpose limitation", "Data minimization", "Storage limitation", "integrity and confidentiality"):
  117. assert needle in law, needle
  118. assert "2026-10-31" in law and "commission.europa.eu" in law
  119. def test_every_physical_control_names_its_nist_control_and_a_privacy_cost():
  120. phys = section(text(), "## The physical side of security", "### Secure disposal")
  121. rows = table_rows("| Control idea" + section(phys, "| Control idea", "Two observations"))
  122. names = [r[0] for r in rows]
  123. for want in ("Locks and key control", "Doors and access control", "Tailgating", "Alarms", "Cameras", "Visitors", "Secure disposal", "Clean desk"):
  124. assert any(want in n for n in names), want
  125. assert len(rows) >= 9, "the guard collected too few rows"
  126. assert all(len(r) == 4 and r[2] and r[3] for r in rows)
  127. for control in ("PE-3(8)", "PE-6(1)", "PE-6(3)", "PE-8", "MP-6"):
  128. assert control in phys, control
  129. def test_disposal_names_the_current_edition_and_the_withdrawn_one():
  130. disposal = section(text(), "### Secure disposal", "## Why lockpicking")
  131. assert "Rev. 2" in disposal and "withdrawn on September 26, 2025" in disposal
  132. for m in ("clear", "purge", "destroy"):
  133. assert m in disposal
  134. def test_lockpicking_puts_the_rules_first_and_gives_no_how_to():
  135. pick = section(text(), "## Why lockpicking is a common hobby in the field", "## Free exercise")
  136. first_rules = pick.index("The rules come before anything else")
  137. assert first_rules < pick.index("What the hobby teaches defenders")
  138. for needle in ("Only locks you own", "Check your local law", "no step-by-step technique", "no bypass guide"):
  139. assert needle in pick, needle
  140. assert "toool.us" in pick and "2026-10-31" in pick
  141. assert not howto_hits(text())
  142. def test_the_how_to_guard_bites():
  143. assert howto_hits("Use a tension wrench and raking motions")
  144. assert not howto_hits("Never pick a lock you do not own")
  145. def test_the_exercise_has_a_table_to_fill_and_a_checklist():
  146. ex = section(text(), "## Free exercise", "## Checkpoint")
  147. header = next(line for line in ex.splitlines() if line.startswith("| Item"))
  148. assert header.count("|") >= 8 and "Blind, lock or both" in header
  149. blank = [line for line in ex.splitlines() if re.fullmatch(r"(\| *)+\|", line)]
  150. assert len(blank) >= 5, "the table needs empty rows to fill in"
  151. assert ex.count("- [ ]") >= 8
  152. for cell in ("Predict first", "**Check.**", "**Undo.**"):
  153. assert cell in ex, cell
  154. def test_the_page_has_no_script():
  155. assert not re.search(r"^```(?:bash|sh|python|powershell)", text(), re.MULTILINE)
  156. def test_every_claims_register_row_has_a_status_and_a_date():
  157. rows = register_rows(text())
  158. assert len(rows) >= 20, "the guard collected too few rows"
  159. assert not rows_missing_status(rows), rows_missing_status(rows)
  160. assert not rows_missing_date(rows), rows_missing_date(rows)
  161. assert any("getmonero.org" in r[1] for r in rows) and any("toool.us" in r[1] for r in rows)
  162. assert any("Not read" in r[-1] for r in rows), "the register must admit what was not read"
  163. def test_the_register_guards_bite():
  164. assert rows_missing_status([["X", "y", "maybe"]]) == ["X"]
  165. assert rows_missing_date([["X", "https://example.org/a", "Read sometime"]]) == ["X"]
  166. assert not rows_missing_date([["X", "https://example.org/a", "Not read"]])