test_tor_i2p_lesson.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # SPDX-License-Identifier: Apache-2.0
  2. """The Tor, I2P and darknet lesson: registered, honest about what was run, and safe to read.
  3. docs/wiki/Tor-I2P-and-the-Darknet-Without-the-Myths.md teaches two anonymity networks and four tools, and uses a
  4. Monero node as its worked example. A banner that claims more than was run, a dead link, a missing registration,
  5. a real address, a claim with no status, or text about a security review beyond its bare existence would each teach
  6. or reveal the wrong thing.
  7. """
  8. import re
  9. from pathlib import Path
  10. ROOT = Path(__file__).resolve().parent.parent
  11. WIKI = ROOT / "docs" / "wiki"
  12. NAME = "Tor-I2P-and-the-Darknet-Without-the-Myths"
  13. PAGE = WIKI / f"{NAME}.md"
  14. LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#[^)]*)?\)")
  15. ADDR = re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b")
  16. ONION = re.compile(r"\b[a-z2-7]{16,56}\.(?:onion|b32\.i2p)\b")
  17. STATUS_WORDS = ("Read", "Observed", "General knowledge", "**est.**")
  18. AUDIT_WORDS = re.compile(r"\b(audit|vulnerab|CVE|finding|exploit|severity|weakness)", re.IGNORECASE)
  19. def text() -> str:
  20. return PAGE.read_text(encoding="utf-8")
  21. def register_rows(body: str) -> list[list[str]]:
  22. reg = body.split("## Claims register")[1]
  23. rows = [line for line in reg.splitlines() if line.startswith("|") and not line.startswith("|---")]
  24. return [[c.strip() for c in r.strip("|").split("|")] for r in rows[1:]]
  25. def rows_missing_status(body: str) -> list[str]:
  26. return [r[0] for r in register_rows(body) if not any(w in r[-1] for w in STATUS_WORDS)]
  27. def test_the_banner_says_it_was_written_from_reading_and_nothing_was_run():
  28. body = text()
  29. assert body.startswith("# Tor, I2P and the darknet without the myths")
  30. assert "Status: unverified draft, written from reading" in body
  31. assert "Nothing on this page was run" in body
  32. assert "**Not run:**" in body and "2026-10-31" in body
  33. assert "## Not verified, plainly" in body
  34. def test_every_linked_wiki_page_exists():
  35. links = set(LINK.findall(text()))
  36. assert len(links) >= 8, "the guard collected too few links"
  37. missing = sorted(t for t in links if not (WIKI / f"{t}.md").exists())
  38. assert not missing, missing
  39. def test_the_lesson_is_registered_in_contents_reading_order_and_nav():
  40. nav = (WIKI / "_nav.yaml").read_text(encoding="utf-8")
  41. assert f"page: {NAME}," in nav
  42. for special in ("Contents.md", "Reading-Order.md"):
  43. assert f"{NAME}.md" in (WIKI / special).read_text(encoding="utf-8"), special
  44. sidebar_line = next(l for l in nav.splitlines() if f"page: {NAME}," in l)
  45. assert "sidebar: true" not in sidebar_line, "the sidebar is a short opt-in list"
  46. def test_glossary_rows_point_at_real_headings():
  47. heads = {re.sub(r"[^a-z0-9-]", "", re.sub(r"[^a-z0-9 -]", "", h.lower()).strip().replace(" ", "-"))
  48. for h in re.findall(r"^#+ (.+)$", text(), re.MULTILINE)}
  49. anchors = set()
  50. rows = 0
  51. for line in (WIKI / "Glossary.md").read_text(encoding="utf-8").splitlines():
  52. if f"{NAME}.md" in line:
  53. rows += 1
  54. anchors.update(re.findall(rf"{NAME}\.md#([a-z0-9-]+)", line))
  55. assert rows >= 7, "expected glossary rows for the new terms"
  56. assert anchors and anchors <= heads, sorted(anchors - heads)
  57. def test_no_addresses_onion_names_or_em_dashes():
  58. body = text()
  59. found = set(ADDR.findall(body))
  60. assert found <= {"127.0.0.1"}, found
  61. assert "127.0.0.1" in found, "the guard collected nothing"
  62. assert not ONION.findall(body)
  63. assert "—" not in body
  64. def test_the_address_guard_bites():
  65. assert set(ADDR.findall("host 192.0.2.7")) != {"127.0.0.1"}
  66. assert ONION.findall("go to " + "a" * 56 + ".onion")
  67. def test_every_claims_register_row_has_a_status():
  68. rows = register_rows(text())
  69. assert len(rows) >= 20, "the guard collected too few rows"
  70. assert not rows_missing_status(text())
  71. def test_the_register_guard_bites():
  72. bad = "## Claims register\n\n| Claim | Source | Status |\n|---|---|---|\n| X | y | maybe |\n"
  73. assert rows_missing_status(bad) == ["X"]
  74. def test_the_sources_are_cited_with_urls_and_a_date():
  75. reg = text().split("## Claims register")[1]
  76. for host in ("support.torproject.org", "community.torproject.org", "geti2p.net", "tails.net", "whonix.org",
  77. "qubes-os.org", "getmonero.org", "raw.githubusercontent.com/monero-project",
  78. "github.com/shermand100/PiNodeXMR"):
  79. assert host in reg, host
  80. assert reg.count("2026-10-31") >= 20
  81. def test_no_audit_detail_about_pinodexmr():
  82. for line in text().splitlines():
  83. if "PiNodeXMR" in line:
  84. assert not re.search(r"\b(audit|vulnerab|CVE|severity)", line, re.IGNORECASE), line
  85. def test_the_audit_guard_bites():
  86. assert AUDIT_WORDS.search("The audit found a weakness")
  87. assert not AUDIT_WORDS.search("One body of recent work is a security review.")
  88. def test_the_page_makes_no_marketplace_or_how_to_commit_crime_content():
  89. body = text().lower()
  90. assert "no marketplace walkthroughs" in body
  91. for banned in ("buy drugs", "vendor shop", "how to buy", "escrow"):
  92. assert banned not in body
  93. def test_the_law_paragraph_tells_learners_to_check_their_jurisdiction():
  94. law = text().split("## The law in one paragraph")[1].split("## Exercises")[0]
  95. assert "Check your own jurisdiction" in law
  96. assert "nothing here is legal advice" in law.lower()
  97. MYTH_HEADING = '## The myth: "Tor is a government backdoor, because the government paid for it"'
  98. URL = re.compile(r"\]\(https://[^)\s]+\)")
  99. DATE_READ = "2026-11-01"
  100. SENTENCES = re.compile(r"(?<=\.)\s+")
  101. SNOWDEN_NEGATION = re.compile(r"\b(no source|does not say|not say)\b")
  102. # Names that would identify the maintainer's contacts if they appeared in his attributed statement.
  103. IDENTIFYING = re.compile(
  104. r"\b(iran|iraq|syria|egypt|lebanon|jordan|saudi|yemen|turkey|dubai|emirates|qatar|"
  105. r"telegram|signal|matrix|discord|whatsapp|session|briar|ricochet|xmpp)\b|\d",
  106. re.IGNORECASE,
  107. )
  108. def myth_section(body: str) -> str:
  109. return body.split(MYTH_HEADING)[1].split("\n## ")[0]
  110. def maintainer_sentence(section: str) -> str:
  111. return next(s for s in SENTENCES.split(section) if "personally spoken with LGBTQ people" in s)
  112. def myth_rows(body: str) -> list[list[str]]:
  113. return [r for r in register_rows(body) if r[0].startswith("Myth section:")]
  114. def test_the_myth_section_exists_after_who_uses_these_networks_and_has_its_parts():
  115. body = text()
  116. assert MYTH_HEADING in body
  117. assert body.index("## Who uses these networks") < body.index(MYTH_HEADING) < body.index("## The tools and what each is for")
  118. section = myth_section(body)
  119. for part in ("### What is true", "### Why funding does not show a backdoor", "### The cover argument",
  120. "### Who relies on it", "### Documented de-anonymizations", "### What is known, and what cannot be proven"):
  121. assert part in section, part
  122. assert "No one can prove a negative" in section
  123. def test_every_myth_claim_has_a_register_row_with_a_url_and_the_read_date():
  124. rows = myth_rows(text())
  125. assert len(rows) >= 18, "the guard collected too few myth rows"
  126. for row in rows:
  127. assert URL.search(row[1]) or "maintainer" in row[1], row[0]
  128. assert DATE_READ in row[2] or "Read in the issue" in row[2], row[0]
  129. sources = " ".join(r[1] for r in rows)
  130. for host in ("torproject.org", "freehaven.net", "bbc.com", "propublica.org", "securedrop.org", "freedom.press",
  131. "wired.com", "theguardian.com", "freedom-to-tinker.com"):
  132. assert host in sources, host
  133. assert sum(DATE_READ in r[2] for r in rows) >= 18
  134. def test_the_myth_section_prints_no_onion_address_or_funding_percentage():
  135. section = myth_section(text())
  136. assert not ONION.findall(section)
  137. assert ".onion" not in section, "name where to find an address instead of printing one"
  138. assert "no figure or percentage" in section
  139. assert not re.search(r"\d+\s*(%|percent)\s+(of (its|their|the)\s+)?(money|funding|budget|revenue)", section)
  140. def test_the_maintainer_statement_is_attributed_without_detail():
  141. sentence = maintainer_sentence(myth_section(text()))
  142. assert "In his own words, the maintainer" in sentence
  143. assert "LGBTQ people in the Middle East" in sentence
  144. assert "the only safe place to be open" in sentence
  145. assert not IDENTIFYING.search(sentence.replace("LGBTQ people in the Middle East", "")), sentence
  146. def test_the_maintainer_guard_bites():
  147. assert IDENTIFYING.search("He spoke with 12 people in Egypt on Telegram.")
  148. assert not IDENTIFYING.search("In his own words, the maintainer has personally spoken with people.")
  149. def test_the_page_never_says_the_snowden_documents_went_through_securedrop():
  150. section = myth_section(text())
  151. checked = 0
  152. for paragraph in section.split("\n\n"):
  153. if "Snowden" in paragraph and "SecureDrop" in paragraph:
  154. checked += 1
  155. assert SNOWDEN_NEGATION.search(paragraph), paragraph
  156. assert checked >= 1, "the guard collected nothing"
  157. def test_the_snowden_guard_bites():
  158. bad = "Snowden shared the documents through SecureDrop."
  159. assert "Snowden" in bad and "SecureDrop" in bad and not SNOWDEN_NEGATION.search(bad)
  160. def test_the_carnegie_mellon_funding_claim_stays_attributed_to_the_tor_project():
  161. section = myth_section(text())
  162. sentence = next(s for s in SENTENCES.split(section) if "apparently paid by the FBI" in s)
  163. assert "Tor Project" in sentence
  164. assert "independent report" in section
  165. def test_the_sibling_lessons_are_linked_now_that_they_exist():
  166. body = text()
  167. privacy = "Privacy-vs-Security-Blinds-and-Locks-Physical-Security-and-Monero"
  168. crypto = "Cryptography-Fundamentals-Symmetric-Asymmetric-Hashing-Certificates-and-Keys"
  169. links = set(LINK.findall(body))
  170. assert privacy in links and crypto in links
  171. assert (WIKI / f"{privacy}.md").is_file() and (WIKI / f"{crypto}.md").is_file()
  172. assert "issues 359 and 360" not in body and "not written yet" not in body