tests/test_scanner_go.py
| 1 | """Scanner tests for Go fixtures.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pqc_lint.scanner import Scanner |
| 6 | |
| 7 | |
| 8 | def _write(path, content): |
| 9 | path.parent.mkdir(parents=True, exist_ok=True) |
| 10 | path.write_text(content, encoding="utf-8") |
| 11 | |
| 12 | |
| 13 | def test_go_rsa_import(scan_tmpdir): |
| 14 | p = scan_tmpdir / "s.go" |
| 15 | _write(p, ( |
| 16 | "package main\n\n" |
| 17 | "import (\n" |
| 18 | " \"crypto/rand\"\n" |
| 19 | " \"crypto/rsa\"\n" |
| 20 | ")\n\n" |
| 21 | "func main() { _, _ = rsa.GenerateKey(rand.Reader, 2048) }\n" |
| 22 | )) |
| 23 | report = Scanner().scan_path(str(scan_tmpdir)) |
| 24 | ids = {f.rule_id for f in report.findings} |
| 25 | assert "PQC001" in ids |
| 26 | |
| 27 | |
| 28 | def test_go_ecdsa_generate(scan_tmpdir): |
| 29 | p = scan_tmpdir / "s.go" |
| 30 | _write(p, ( |
| 31 | "package main\n\n" |
| 32 | "import (\n" |
| 33 | " \"crypto/ecdsa\"\n" |
| 34 | " \"crypto/elliptic\"\n" |
| 35 | " \"crypto/rand\"\n" |
| 36 | ")\n\n" |
| 37 | "func main() { _, _ = ecdsa.GenerateKey(elliptic.P256(), rand.Reader) }\n" |
| 38 | )) |
| 39 | report = Scanner().scan_path(str(scan_tmpdir)) |
| 40 | ids = {f.rule_id for f in report.findings} |
| 41 | assert "PQC002" in ids |
| 42 | |
| 43 | |
| 44 | def test_go_md5_package_import(scan_tmpdir): |
| 45 | p = scan_tmpdir / "s.go" |
| 46 | _write(p, ( |
| 47 | "package main\n" |
| 48 | "import \"crypto/md5\"\n" |
| 49 | "func main() { _ = md5.New() }\n" |
| 50 | )) |
| 51 | report = Scanner().scan_path(str(scan_tmpdir)) |
| 52 | ids = {f.rule_id for f in report.findings} |
| 53 | assert "PQC301" in ids |
| 54 | |