Files
alkane-benchmarks/ci/test_validate_site.py
Alkane CI ea160545c4
next-site-candidate / validate-and-candidate (push) Successful in 5s
Expose prepublication benchmark page guard
2026-10-16 08:44:49 -06:00

118 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""Privacy and shape checks for the generated Pages validator."""
import tempfile
import unittest
from pathlib import Path
import subprocess
import sys
from validate_site import validate
INDEX = """<!doctype html><html><head><title>Alkane benchmarks</title></head>
<body><h1>Alkane benchmarks</h1><a href="sdk/">SDK</a>
<a href="os/">OS</a><a href="toolkit/">Toolkit</a></body></html>"""
PAGE = """<!doctype html><html><head><title>Alkane benchmarks</title></head>
<body><h1>Alkane benchmarks</h1><table><tr><td>10 ns/op</td></tr></table></body></html>"""
class SiteValidationTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
(self.root / "index.html").write_text(INDEX)
for repo in ("sdk", "os", "toolkit"):
(self.root / repo).mkdir()
(self.root / repo / "index.html").write_text(PAGE)
def test_valid_data_only_site(self):
self.assertEqual(validate(self.root), [])
def test_single_page_prepublication_cli(self):
command = [sys.executable, str(Path(__file__).with_name("validate_site.py")),
"--page", "os", str(self.root / "os/index.html")]
self.assertEqual(subprocess.run(command, capture_output=True).returncode, 0)
(self.root / "os/index.html").write_text(PAGE + '<meta http-equiv="refresh" content="0">')
self.assertEqual(subprocess.run(command, capture_output=True).returncode, 1)
def test_rejects_active_content(self):
(self.root / "os/index.html").write_text(PAGE + "<script>alert(1)</script>")
self.assertTrue(any("forbidden tag" in error for error in validate(self.root)))
def test_rejects_external_source_and_credential_marker(self):
(self.root / "sdk/index.html").write_text(
PAGE + '<img src="https://example.com/a.png">GITEA_TOKEN'
)
errors = validate(self.root)
self.assertTrue(any("forbidden tag: img" in error for error in errors))
self.assertTrue(any("credential marker" in error for error in errors))
def test_allows_only_pinned_source_commit_links(self):
commit = "a" * 40
(self.root / "os/index.html").write_text(
PAGE + f'<a href="https://gitea.bluefla.re/blueflare/alkane-os/commit/{commit}">commit</a>'
)
self.assertEqual(validate(self.root), [])
(self.root / "os/index.html").write_text(
PAGE + '<a href="https://example.com/commit/secret">commit</a>'
)
self.assertTrue(any("external or active reference" in error for error in validate(self.root)))
def test_rejects_extra_files(self):
(self.root / "source.tar.gz").write_bytes(b"private source")
self.assertTrue(any("unexpected published files" in error for error in validate(self.root)))
def test_requires_all_three_reports(self):
(self.root / "toolkit/index.html").unlink()
self.assertTrue(any("missing regular page" in error for error in validate(self.root)))
def test_rejects_css_import(self):
(self.root / "os/index.html").write_text(PAGE + "<style>@import 'https://example.com/x.css';</style>")
self.assertTrue(any("external CSS reference" in error for error in validate(self.root)))
def test_rejects_css_escaped_import(self):
(self.root / "os/index.html").write_text(
PAGE + r"<style>@im\70ort 'https://example.com/x.css';</style>"
)
self.assertTrue(any("external CSS reference" in error for error in validate(self.root)))
def test_rejects_meta_refresh(self):
(self.root / "os/index.html").write_text(
PAGE + '<meta http-equiv="refresh" content="0;url=https://example.com/">'
)
self.assertTrue(any("unexpected meta directive" in error for error in validate(self.root)))
def test_rejects_base_and_unexpected_attributes(self):
(self.root / "os/index.html").write_text(
PAGE + '<base href="https://example.com/"><a href="../" ping="https://example.com/">Back</a>'
)
errors = validate(self.root)
self.assertTrue(any("forbidden tag: base" in error for error in errors))
self.assertTrue(any("forbidden attribute on a: ping" in error for error in errors))
def test_rejects_unlisted_local_link(self):
(self.root / "os/index.html").write_text(
PAGE + '<a href="../../private/">Source</a>'
)
self.assertTrue(any("external or active reference" in error for error in validate(self.root)))
def test_rejects_symlink(self):
(self.root / "private").symlink_to(self.root / "os/index.html")
self.assertTrue(any("contains a symlink" in error for error in validate(self.root)))
def test_requires_index_navigation(self):
(self.root / "index.html").write_text(INDEX.replace('<a href="toolkit/">Toolkit</a>', ""))
self.assertTrue(any("missing project links" in error for error in validate(self.root)))
def test_requires_measurements_or_pending_notice(self):
(self.root / "sdk/index.html").write_text(
"<!doctype html><html><head><title>Alkane</title></head><body><h1>Alkane</h1></body></html>"
)
self.assertTrue(any("no measurements" in error for error in validate(self.root)))
if __name__ == "__main__":
unittest.main()