tests/test_backends.py
| 1 | """Tests for attestation backends.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from pqc_hypervisor_attestation import ( |
| 8 | AMDSEVSNPBackend, |
| 9 | InMemoryBackend, |
| 10 | IntelTDXBackend, |
| 11 | MemoryRegion, |
| 12 | ) |
| 13 | from pqc_hypervisor_attestation.errors import BackendError, InvalidRegionError |
| 14 | |
| 15 | |
| 16 | def test_in_memory_register_list_snapshot() -> None: |
| 17 | be = InMemoryBackend() |
| 18 | region = MemoryRegion( |
| 19 | region_id="r1", |
| 20 | description="test", |
| 21 | address=0x0, |
| 22 | size=4, |
| 23 | protection="RO", |
| 24 | ) |
| 25 | be.register("w", region, b"abcd") |
| 26 | regions = be.list_regions("w") |
| 27 | assert regions == [region] |
| 28 | snap = be.snapshot(region) |
| 29 | assert snap.size == 4 |
| 30 | assert snap.content_hash |
| 31 | |
| 32 | |
| 33 | def test_in_memory_invalid_region_raises() -> None: |
| 34 | be = InMemoryBackend() |
| 35 | region = MemoryRegion( |
| 36 | region_id="missing", |
| 37 | description="nope", |
| 38 | address=0x0, |
| 39 | size=0, |
| 40 | protection="RO", |
| 41 | ) |
| 42 | with pytest.raises(InvalidRegionError): |
| 43 | be.snapshot(region) |
| 44 | with pytest.raises(InvalidRegionError): |
| 45 | be.update("missing", b"x") |
| 46 | |
| 47 | |
| 48 | def test_sev_snp_list_regions_raises() -> None: |
| 49 | backend = AMDSEVSNPBackend() |
| 50 | with pytest.raises(BackendError): |
| 51 | backend.list_regions("any") |
| 52 | |
| 53 | |
| 54 | def test_tdx_snapshot_raises() -> None: |
| 55 | backend = IntelTDXBackend() |
| 56 | region = MemoryRegion( |
| 57 | region_id="r1", |
| 58 | description="test", |
| 59 | address=0x0, |
| 60 | size=4, |
| 61 | protection="RO", |
| 62 | ) |
| 63 | with pytest.raises(BackendError): |
| 64 | backend.snapshot(region) |
| 65 | |
| 66 | |
| 67 | def test_workload_isolation() -> None: |
| 68 | be = InMemoryBackend() |
| 69 | region = MemoryRegion( |
| 70 | region_id="a", |
| 71 | description="test", |
| 72 | address=0x0, |
| 73 | size=4, |
| 74 | protection="RO", |
| 75 | ) |
| 76 | be.register("workload-a", region, b"abcd") |
| 77 | assert be.list_regions("workload-a") == [region] |
| 78 | assert be.list_regions("workload-b") == [] |
| 79 | |