aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/fips/test_fips_certificate.py
blob: f6adb74d3df71d12ab4175581b8409f146903966 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from __future__ import annotations

import json
import shutil
from collections.abc import Generator
from importlib import resources
from pathlib import Path

import pytest
import tests.data.fips.certificate
import tests.data.fips.dataset

from sec_certs.dataset.fips import FIPSDataset
from sec_certs.sample.fips import FIPSCertificate
from sec_certs.serialization.schemas import validator


@pytest.fixture(scope="module")
def data_dir() -> Generator[Path, None, None]:
    with resources.path(tests.data.fips.certificate, "") as path:
        yield path


@pytest.fixture
def certificate(tmp_path_factory) -> FIPSCertificate:
    tmp_dir = tmp_path_factory.mktemp("dset")

    with resources.path(tests.data.fips.dataset, "") as dataset_path:
        shutil.copytree(dataset_path, tmp_dir, dirs_exist_ok=True)

    fips_dset = FIPSDataset.from_json(tmp_dir / "toy_dataset.json")

    crt = fips_dset["184097a88a9b4ad9"]
    fips_dset.certs = {crt.dgst: crt}
    fips_dset.download_all_artifacts()
    fips_dset.convert_all_pdfs()

    return crt


def test_extract_metadata(certificate: FIPSCertificate):
    pass


def test_extract_module(certificate: FIPSCertificate):
    certificate.state.module_extract_ok = True
    FIPSCertificate.parse_html_module(certificate)
    assert certificate.state.module_extract_ok


def test_extract_frontpage():
    pass


def test_keyword_extraction():
    pass


def test_cert_to_json(certificate: FIPSCertificate, tmp_path: Path, data_dir: Path):
    certificate.to_json(tmp_path / "crt.json")

    with (tmp_path / "crt.json").open("r") as handle:
        data = json.load(handle)

    with (data_dir / "fictional_cert.json").open("r") as handle:
        template_data = json.load(handle)

    assert template_data == data


def test_cert_from_json(certificate: FIPSCertificate, data_dir: Path):
    crt = FIPSCertificate.from_json(data_dir / "fictional_cert.json")
    assert certificate == crt


def test_schema_validation(data_dir: Path):
    with (data_dir / "fictional_cert.json").open("r") as cert:
        v = validator("http://sec-certs.org/schemas/fips_certificate.json")
        v.validate(json.load(cert))