|
| 1 | +import pytest |
| 2 | +from fastapi import HTTPException |
| 3 | +from cryptography import x509 |
| 4 | +from cryptography.hazmat.primitives.asymmetric import rsa, ec |
| 5 | + |
| 6 | +from main import validate_safe_name, parse_san_entry, generate_key |
| 7 | + |
| 8 | + |
| 9 | +class TestValidateSafeName: |
| 10 | + def test_valid_hostname(self): |
| 11 | + assert validate_safe_name("example.com") is True |
| 12 | + |
| 13 | + def test_valid_with_dash_and_underscore(self): |
| 14 | + assert validate_safe_name("my-host_01") is True |
| 15 | + |
| 16 | + def test_empty_string(self): |
| 17 | + assert validate_safe_name("") is False |
| 18 | + |
| 19 | + def test_path_traversal_dotdot(self): |
| 20 | + assert validate_safe_name("..") is False |
| 21 | + |
| 22 | + def test_path_traversal_embedded(self): |
| 23 | + assert validate_safe_name("foo/../bar") is False |
| 24 | + |
| 25 | + def test_slash(self): |
| 26 | + assert validate_safe_name("foo/bar") is False |
| 27 | + |
| 28 | + def test_space(self): |
| 29 | + assert validate_safe_name("foo bar") is False |
| 30 | + |
| 31 | + def test_semicolon(self): |
| 32 | + assert validate_safe_name("foo;bar") is False |
| 33 | + |
| 34 | + def test_wildcard(self): |
| 35 | + assert validate_safe_name("foo*bar") is False |
| 36 | + |
| 37 | + |
| 38 | +class TestParseSanEntry: |
| 39 | + def test_ipv4(self): |
| 40 | + result = parse_san_entry("192.168.1.1") |
| 41 | + assert isinstance(result, x509.IPAddress) |
| 42 | + |
| 43 | + def test_ipv6(self): |
| 44 | + result = parse_san_entry("::1") |
| 45 | + assert isinstance(result, x509.IPAddress) |
| 46 | + |
| 47 | + def test_dns_name(self): |
| 48 | + result = parse_san_entry("example.com") |
| 49 | + assert isinstance(result, x509.DNSName) |
| 50 | + |
| 51 | + def test_wildcard_dns(self): |
| 52 | + result = parse_san_entry("*.example.com") |
| 53 | + assert isinstance(result, x509.DNSName) |
| 54 | + |
| 55 | + |
| 56 | +class TestGenerateKey: |
| 57 | + def test_rsa2048(self): |
| 58 | + key = generate_key("rsa2048") |
| 59 | + assert isinstance(key, rsa.RSAPrivateKey) |
| 60 | + assert key.key_size == 2048 |
| 61 | + |
| 62 | + def test_rsa4096(self): |
| 63 | + key = generate_key("rsa4096") |
| 64 | + assert isinstance(key, rsa.RSAPrivateKey) |
| 65 | + assert key.key_size == 4096 |
| 66 | + |
| 67 | + def test_secp256r1(self): |
| 68 | + key = generate_key("secp256r1") |
| 69 | + assert isinstance(key, ec.EllipticCurvePrivateKey) |
| 70 | + assert key.curve.name == "secp256r1" |
| 71 | + |
| 72 | + def test_secp384r1(self): |
| 73 | + key = generate_key("secp384r1") |
| 74 | + assert isinstance(key, ec.EllipticCurvePrivateKey) |
| 75 | + assert key.curve.name == "secp384r1" |
| 76 | + |
| 77 | + def test_secp521r1(self): |
| 78 | + key = generate_key("secp521r1") |
| 79 | + assert isinstance(key, ec.EllipticCurvePrivateKey) |
| 80 | + assert key.curve.name == "secp521r1" |
| 81 | + |
| 82 | + def test_invalid_key_type_raises(self): |
| 83 | + with pytest.raises(HTTPException) as exc_info: |
| 84 | + generate_key("invalid") |
| 85 | + assert exc_info.value.status_code == 400 |
0 commit comments