tests/test_scanner_javascript.py
| 1 | """Scanner tests for JavaScript 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_node_crypto_rsa(scan_tmpdir): |
| 14 | p = scan_tmpdir / "s.js" |
| 15 | _write(p, ( |
| 16 | "const crypto = require('crypto');\n" |
| 17 | "crypto.generateKeyPair('rsa', { modulusLength: 2048 });\n" |
| 18 | )) |
| 19 | report = Scanner().scan_path(str(scan_tmpdir)) |
| 20 | ids = {f.rule_id for f in report.findings} |
| 21 | assert "PQC001" in ids |
| 22 | |
| 23 | |
| 24 | def test_web_crypto_ecdsa(scan_tmpdir): |
| 25 | p = scan_tmpdir / "s.ts" |
| 26 | _write(p, ( |
| 27 | "await crypto.subtle.generateKey(\n" |
| 28 | " { name: 'ECDSA', namedCurve: 'P-256' },\n" |
| 29 | " true, ['sign','verify'],\n" |
| 30 | ");\n" |
| 31 | )) |
| 32 | report = Scanner().scan_path(str(scan_tmpdir)) |
| 33 | ids = {f.rule_id for f in report.findings} |
| 34 | assert "PQC002" in ids |
| 35 | |
| 36 | |
| 37 | def test_node_crypto_sha1(scan_tmpdir): |
| 38 | p = scan_tmpdir / "h.js" |
| 39 | _write(p, "const crypto = require('crypto');\ncrypto.createHash('sha1');\n") |
| 40 | report = Scanner().scan_path(str(scan_tmpdir)) |
| 41 | ids = {f.rule_id for f in report.findings} |
| 42 | assert "PQC302" in ids |
| 43 | |