diff options
| author | J08nY | 2023-04-21 15:10:56 +0200 |
|---|---|---|
| committer | J08nY | 2023-04-21 15:10:56 +0200 |
| commit | 5893352a41ce8e513dfbbb7c3e35cac3925966ea (patch) | |
| tree | 64b799f85667ead123c6bbada1db8fc1ed585f5d | |
| parent | 3728142d5fa9ec991b00cf16e9a04384b82f65d6 (diff) | |
| download | sec-certs-5893352a41ce8e513dfbbb7c3e35cac3925966ea.tar.gz sec-certs-5893352a41ce8e513dfbbb7c3e35cac3925966ea.tar.zst sec-certs-5893352a41ce8e513dfbbb7c3e35cac3925966ea.zip | |
Revert "Move FIPS test fixtures to conftest and share them."
This reverts commit 98c4f06c6ced66beab6f0c576542acbf15212416.
| -rw-r--r-- | tests/fips/__init__.py | 0 | ||||
| -rw-r--r-- | tests/fips/test_fips_algorithm_dataset.py | 23 | ||||
| -rw-r--r-- | tests/fips/test_fips_analysis.py | 153 | ||||
| -rw-r--r-- | tests/fips/test_fips_certificate.py | 35 | ||||
| -rw-r--r-- | tests/fips/test_fips_dataset.py | 35 | ||||
| -rw-r--r-- | tests/test_config.py | 7 |
6 files changed, 233 insertions, 20 deletions
diff --git a/tests/fips/__init__.py b/tests/fips/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/tests/fips/__init__.py +++ /dev/null diff --git a/tests/fips/test_fips_algorithm_dataset.py b/tests/fips/test_fips_algorithm_dataset.py index 137ac575..1d45e67c 100644 --- a/tests/fips/test_fips_algorithm_dataset.py +++ b/tests/fips/test_fips_algorithm_dataset.py @@ -1,8 +1,10 @@ from __future__ import annotations +from pathlib import Path from typing import Any import pytest +import tests.data.fips.dataset from sec_certs.dataset.fips_algorithm import FIPSAlgorithmDataset from sec_certs.sample.fips_algorithm import FIPSAlgorithm @@ -20,6 +22,27 @@ def test_alg_dset_from_web(tmp_path): assert len(dset) > 30000 +@pytest.fixture(scope="module") +def alg_dset_path() -> Path: + return Path(tests.data.fips.dataset.__path__[0]) / "alg_dataset.json" + + +@pytest.fixture(scope="module") +def alg_dset(alg_dset_path: Path) -> FIPSAlgorithmDataset: + return FIPSAlgorithmDataset.from_json(alg_dset_path) + + +@pytest.fixture(scope="module") +def alg_dict() -> dict[str, Any]: + return { + "alg_number": "2902", + "algorithm_type": "AES", + "vendor": "Hewlett-Packard Development Company, L.P.", + "implementation_name": "HP Secure Encryption Engine v1.0", + "validation_date": "7/10/2014", + } + + def test_alg_dset_lookup_dict(alg_dset: FIPSAlgorithmDataset): alg = alg_dset["AES2902"] assert alg_dset.alg_number_to_algs["2902"] == {alg} diff --git a/tests/fips/test_fips_analysis.py b/tests/fips/test_fips_analysis.py index 8183a63e..90de70f0 100644 --- a/tests/fips/test_fips_analysis.py +++ b/tests/fips/test_fips_analysis.py @@ -1,13 +1,162 @@ from __future__ import annotations +from pathlib import Path + import pytest +import tests.data.fips.dataset +from dateutil.parser import isoparse -from sec_certs.dataset.cve import CVEDataset +from sec_certs.dataset import CPEDataset, CVEDataset from sec_certs.dataset.fips import FIPSDataset -from sec_certs.sample.cpe import CPE +from sec_certs.sample.cpe import CPE, CPEConfiguration from sec_certs.sample.cve import CVE +@pytest.fixture(scope="module") +def data_dir() -> Path: + return Path(tests.data.fips.dataset.__path__[0]) + + +@pytest.fixture(scope="module") +def vulnerable_cpe() -> CPE: + return CPE("cpe:2.3:o:redhat:enterprise_linux:7.1:*:*:*:*:*:*:*", "Red Hat Enterprise Linux 7.1") + + +@pytest.fixture(scope="module") +def some_random_cpe() -> CPE: + return CPE( + "cpe:2.3:a:ibm:security_key_lifecycle_manager:2.6.0.1:*:*:*:*:*:*:*", + "IBM Security Key Lifecycle Manager 2.6.0.1", + ) + + +@pytest.fixture(scope="module") +def cve(vulnerable_cpe: CPE) -> CVE: + return CVE( + "CVE-1234-123456", + [vulnerable_cpe], + [], + CVE.Impact(10, "HIGH", 10, 10), + isoparse("2021-05-26T04:15Z"), + {"CWE-200"}, + ) + + +@pytest.fixture(scope="module") +def some_other_cve(some_random_cpe: CPE) -> CVE: + return CVE( + "CVE-2019-4513", + [some_random_cpe], + [], + CVE.Impact(8.2, "HIGH", 3.9, 4.2), + isoparse("2000-05-26T04:15Z"), + {"CVE-611"}, + ) + + +@pytest.fixture(scope="module") +def ibm_cpe_configuration() -> CPEConfiguration: + return CPEConfiguration( + CPE("cpe:2.3:o:ibm:zos:*:*:*:*:*:*:*:*"), + [ + CPE("cpe:2.3:a:ibm:websphere_application_server:7.0.0.1:*:*:*:*:*:*:*"), + CPE("cpe:2.3:a:ibm:websphere_application_server:7.0:*:*:*:*:*:*:*"), + CPE("cpe:2.3:a:ibm:websphere_application_server:7.0.0.2:*:*:*:*:*:*:*"), + CPE("cpe:2.3:a:ibm:websphere_application_server:7.0.0.3:*:*:*:*:*:*:*"), + CPE("cpe:2.3:a:ibm:websphere_application_server:7.0.0.4:*:*:*:*:*:*:*"), + CPE("cpe:2.3:a:ibm:websphere_application_server:7.0.0.5:*:*:*:*:*:*:*"), + CPE("cpe:2.3:a:ibm:websphere_application_server:7.0.0.6:*:*:*:*:*:*:*"), + CPE("cpe:2.3:a:ibm:websphere_application_server:7.0.0.7:*:*:*:*:*:*:*"), + CPE("cpe:2.3:a:ibm:websphere_application_server:7.0.0.8:*:*:*:*:*:*:*"), + CPE("cpe:2.3:a:ibm:websphere_application_server:7.0.0.9:*:*:*:*:*:*:*"), + CPE("cpe:2.3:a:ibm:websphere_application_server:*:*:*:*:*:*:*:*"), + ], + ) + + +@pytest.fixture(scope="module") +def cpes_ibm_websphere_app_with_platform() -> set[CPE]: + return { + CPE("cpe:2.3:o:ibm:zos:*:*:*:*:*:*:*:*", "IBM zOS"), + CPE("cpe:2.3:a:ibm:websphere_application_server:*:*:*:*:*:*:*:*", "IBM WebSphere Application Server"), + } + + +@pytest.fixture(scope="module") +def ibm_xss_cve(ibm_cpe_configuration: CPEConfiguration) -> CVE: + return CVE( + "CVE-2010-2325", + [], + [ibm_cpe_configuration], + CVE.Impact(4.3, "MEDIUM", 2.9, 8.6), + isoparse("2000-06-18T04:15Z"), + {"CWE-79"}, + ) + + +@pytest.fixture(scope="module") +def cpe_dataset( + vulnerable_cpe: CPE, some_random_cpe: CPE, cpes_ibm_websphere_app_with_platform: set[CPE] +) -> CPEDataset: + cpes = { + vulnerable_cpe, + some_random_cpe, + CPE( + "cpe:2.3:a:semperplugins:all_in_one_seo_pack:1.3.6.4:*:*:*:*:wordpress:*:*", + "Semper Plugins All in One SEO Pack 1.3.6.4 for WordPress", + ), + CPE( + "cpe:2.3:a:tracker-software:pdf-xchange_lite_printer:6.0.320.0:*:*:*:*:*:*:*", + "Tracker Software PDF-XChange Lite Printer 6.0.320.0", + ), + *cpes_ibm_websphere_app_with_platform, + } + + return CPEDataset(False, {x.uri: x for x in cpes}) + + +@pytest.fixture(scope="module") +def cve_dataset(cve: CVE, some_other_cve: CVE, ibm_xss_cve: CVE) -> CVEDataset: + cves = {cve, some_other_cve, ibm_xss_cve} + cve_dset = CVEDataset({x.cve_id: x for x in cves}) + cve_dset.build_lookup_dict(use_nist_mapping=False) + return cve_dset + + +@pytest.fixture(scope="module") +def toy_static_dataset(data_dir: Path) -> FIPSDataset: + return FIPSDataset.from_json(data_dir / "toy_dataset.json") + + +@pytest.fixture(scope="module") +def processed_dataset( + toy_static_dataset: FIPSDataset, cpe_dataset: CPEDataset, cve_dataset: CVEDataset, tmp_path_factory +) -> FIPSDataset: + tmp_dir = tmp_path_factory.mktemp("fips_dset") + toy_static_dataset.copy_dataset(tmp_dir) + + tested_certs = [ + toy_static_dataset["3095"], + toy_static_dataset["3093"], + toy_static_dataset["3197"], + toy_static_dataset["2441"], + ] + toy_static_dataset.certs = {x.dgst: x for x in tested_certs} + + toy_static_dataset.download_all_artifacts() + toy_static_dataset.convert_all_pdfs() + toy_static_dataset.extract_data() + toy_static_dataset._compute_references(keep_unknowns=True) + + toy_static_dataset.auxiliary_datasets.cpe_dset = cpe_dataset + toy_static_dataset.auxiliary_datasets.cve_dset = cve_dataset + toy_static_dataset.compute_cpe_heuristics() + toy_static_dataset.compute_related_cves() + toy_static_dataset._compute_transitive_vulnerabilities() + + return toy_static_dataset + + @pytest.mark.parametrize( "input_dgst, expected_refs", [ diff --git a/tests/fips/test_fips_certificate.py b/tests/fips/test_fips_certificate.py index e8f1c9c8..d6f10b09 100644 --- a/tests/fips/test_fips_certificate.py +++ b/tests/fips/test_fips_certificate.py @@ -1,11 +1,38 @@ from __future__ import annotations import json +import shutil 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 +@pytest.fixture(scope="module") +def data_dir() -> Path: + return Path(tests.data.fips.certificate.__path__[0]) + + +@pytest.fixture +def certificate(tmp_path_factory) -> FIPSCertificate: + tmp_dir = tmp_path_factory.mktemp("dset") + dset_json_path = Path(tests.data.fips.dataset.__path__[0]) / "toy_dataset.json" + data_dir_path = dset_json_path.parent + shutil.copytree(data_dir_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 @@ -24,18 +51,18 @@ def test_keyword_extraction(): pass -def test_cert_to_json(certificate: FIPSCertificate, tmp_path: Path, cert_data_dir: Path): +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 (cert_data_dir / "fictional_cert.json").open("r") as 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, cert_data_dir: Path): - crt = FIPSCertificate.from_json(cert_data_dir / "fictional_cert.json") +def test_cert_from_json(certificate: FIPSCertificate, data_dir: Path): + crt = FIPSCertificate.from_json(data_dir / "fictional_cert.json") assert certificate == crt diff --git a/tests/fips/test_fips_dataset.py b/tests/fips/test_fips_dataset.py index 8a28c322..443f18b7 100644 --- a/tests/fips/test_fips_dataset.py +++ b/tests/fips/test_fips_dataset.py @@ -6,19 +6,30 @@ from pathlib import Path from tempfile import TemporaryDirectory import pytest +import tests.data.fips.dataset from sec_certs import constants from sec_certs.dataset.fips import FIPSDataset from sec_certs.sample.fips import FIPSCertificate -def test_dataset_to_json(toy_dataset: FIPSDataset, dataset_data_dir: Path, tmp_path: Path): +@pytest.fixture(scope="module") +def data_dir() -> Path: + return Path(tests.data.fips.dataset.__path__[0]) + + +@pytest.fixture +def toy_dataset(data_dir: Path) -> FIPSDataset: + return FIPSDataset.from_json(data_dir / "toy_dataset.json") + + +def test_dataset_to_json(toy_dataset: FIPSDataset, data_dir: Path, tmp_path: Path): toy_dataset.to_json(tmp_path / "dset.json") with (tmp_path / "dset.json").open("r") as handle: data = json.load(handle) - with (dataset_data_dir / "toy_dataset.json").open("r") as handle: + with (data_dir / "toy_dataset.json").open("r") as handle: template_data = json.load(handle) del data["timestamp"] @@ -26,8 +37,8 @@ def test_dataset_to_json(toy_dataset: FIPSDataset, dataset_data_dir: Path, tmp_p assert data == template_data -def test_dataset_from_json(toy_dataset: FIPSDataset, dataset_data_dir: Path): - assert toy_dataset == FIPSDataset.from_json(dataset_data_dir / "toy_dataset.json") +def test_dataset_from_json(toy_dataset: FIPSDataset, data_dir: Path): + assert toy_dataset == FIPSDataset.from_json(data_dir / "toy_dataset.json") def test_build_empty_dataset(): @@ -40,19 +51,15 @@ def test_build_empty_dataset(): assert not dset.state.certs_analyzed -def test_build_dataset(dataset_data_dir: Path, toy_dataset: FIPSDataset): +def test_build_dataset(data_dir: Path, toy_dataset: FIPSDataset): with TemporaryDirectory() as tmp_dir: dataset_path = Path(tmp_dir) (dataset_path / "web").mkdir() + shutil.copyfile(data_dir / "fips_modules_active.html", dataset_path / "web" / "fips_modules_active.html") shutil.copyfile( - dataset_data_dir / "fips_modules_active.html", dataset_path / "web" / "fips_modules_active.html" - ) - shutil.copyfile( - dataset_data_dir / "fips_modules_historical.html", dataset_path / "web" / "fips_modules_historical.html" - ) - shutil.copyfile( - dataset_data_dir / "fips_modules_revoked.html", dataset_path / "web" / "fips_modules_revoked.html" + data_dir / "fips_modules_historical.html", dataset_path / "web" / "fips_modules_historical.html" ) + shutil.copyfile(data_dir / "fips_modules_revoked.html", dataset_path / "web" / "fips_modules_revoked.html") dset = FIPSDataset(root_dir=dataset_path) dset.get_certs_from_web(to_download=False) @@ -76,7 +83,7 @@ def test_download_meta_html_files(): assert (dset.web_dir / "fips_modules_revoked.html").stat().st_size > constants.MIN_FIPS_HTML_SIZE -def test_download_and_convert_artifacts(toy_dataset: FIPSDataset, dataset_data_dir: Path): +def test_download_and_convert_artifacts(toy_dataset: FIPSDataset, data_dir: Path): crt = toy_dataset["184097a88a9b4ad9"] toy_dataset.certs = {crt.dgst: crt} with TemporaryDirectory() as tmp_dir: @@ -93,7 +100,7 @@ def test_download_and_convert_artifacts(toy_dataset: FIPSDataset, dataset_data_d assert crt.state.policy_pdf_hash == "36b63890182f0aed29b305a0b4acc0d70b657262516f4be69138c70c2abdb1f1" assert crt.state.policy_txt_path.exists() - template_policy_txt_path = dataset_data_dir / "template_policy_184097a88a9b4ad9.txt" + template_policy_txt_path = data_dir / "template_policy_184097a88a9b4ad9.txt" assert abs(crt.state.policy_txt_path.stat().st_size - template_policy_txt_path.stat().st_size) < 1000 diff --git a/tests/test_config.py b/tests/test_config.py index cd673c15..39102111 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -8,6 +8,13 @@ import pytest import yaml import sec_certs.configuration as config_module +import tests.data + + +@pytest.fixture(autouse=True) +def load_test_config(): + pth = Path(tests.data.__path__[0]) / "settings_tests.yml" + config_module.config.load_from_yaml(pth) @pytest.fixture |
