|
| 1 | +# @file |
| 2 | +# |
| 3 | +# Copyright (c) Microsoft Corporation. |
| 4 | +# SPDX-License-Identifier: BSD-2-Clause-Patent |
| 5 | +## |
| 6 | +"""Test the validate_dbx_references.py script. |
| 7 | +
|
| 8 | +This module contains unit tests for the DBX certificate reference validation functionality. |
| 9 | +""" |
| 10 | +import json |
| 11 | +import pathlib |
| 12 | +import tempfile |
| 13 | + |
| 14 | +import pytest |
| 15 | +from validate_dbx_references import validate_certificate_references |
| 16 | + |
| 17 | + |
| 18 | +def test_validate_certificate_references_no_certificates_section() -> None: |
| 19 | + """Test validation when JSON has no certificates section.""" |
| 20 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 21 | + temp_path = pathlib.Path(temp_dir) |
| 22 | + |
| 23 | + # Create JSON file without certificates section |
| 24 | + json_file = temp_path / "dbx_info_msft_01_01_24.json" |
| 25 | + with json_file.open("w") as f: |
| 26 | + json.dump({"images": {"x64": []}}, f) |
| 27 | + |
| 28 | + # Create empty certificates directory |
| 29 | + certs_dir = temp_path / "Certificates" |
| 30 | + certs_dir.mkdir() |
| 31 | + |
| 32 | + # Should pass validation |
| 33 | + errors = validate_certificate_references(json_file, certs_dir) |
| 34 | + assert errors == [] |
| 35 | + |
| 36 | + |
| 37 | +def test_validate_certificate_references_empty_certificates() -> None: |
| 38 | + """Test validation when certificates section is empty.""" |
| 39 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 40 | + temp_path = pathlib.Path(temp_dir) |
| 41 | + |
| 42 | + # Create JSON file with empty certificates section |
| 43 | + json_file = temp_path / "dbx_info_msft_01_01_24.json" |
| 44 | + with json_file.open("w") as f: |
| 45 | + json.dump({"certificates": []}, f) |
| 46 | + |
| 47 | + # Create empty certificates directory |
| 48 | + certs_dir = temp_path / "Certificates" |
| 49 | + certs_dir.mkdir() |
| 50 | + |
| 51 | + # Should pass validation |
| 52 | + errors = validate_certificate_references(json_file, certs_dir) |
| 53 | + assert errors == [] |
| 54 | + |
| 55 | + |
| 56 | +def test_validate_certificate_references_valid_certificates() -> None: |
| 57 | + """Test validation when all certificate references are valid.""" |
| 58 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 59 | + temp_path = pathlib.Path(temp_dir) |
| 60 | + |
| 61 | + # Create certificates directory with test files |
| 62 | + certs_dir = temp_path / "Certificates" |
| 63 | + certs_dir.mkdir() |
| 64 | + (certs_dir / "cert1.cer").touch() |
| 65 | + (certs_dir / "cert2.der").touch() |
| 66 | + |
| 67 | + # Create JSON file referencing these certificates |
| 68 | + json_file = temp_path / "dbx_info_msft_01_01_24.json" |
| 69 | + json_data = { |
| 70 | + "certificates": [ |
| 71 | + { |
| 72 | + "value": "cert1.cer", |
| 73 | + "subjectName": "Test Subject 1", |
| 74 | + "issuerName": "Test Issuer 1", |
| 75 | + "thumbprint": "abc123", |
| 76 | + "description": "Test certificate 1", |
| 77 | + "dateOfAddition": "2024-01-01" |
| 78 | + }, |
| 79 | + { |
| 80 | + "value": "cert2.der", |
| 81 | + "subjectName": "Test Subject 2", |
| 82 | + "issuerName": "Test Issuer 2", |
| 83 | + "thumbprint": "def456", |
| 84 | + "description": "Test certificate 2", |
| 85 | + "dateOfAddition": "2024-01-01" |
| 86 | + } |
| 87 | + ] |
| 88 | + } |
| 89 | + with json_file.open("w") as f: |
| 90 | + json.dump(json_data, f) |
| 91 | + |
| 92 | + # Should pass validation |
| 93 | + errors = validate_certificate_references(json_file, certs_dir) |
| 94 | + assert errors == [] |
| 95 | + |
| 96 | + |
| 97 | +def test_validate_certificate_references_missing_certificates() -> None: |
| 98 | + """Test validation when some certificate references are missing.""" |
| 99 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 100 | + temp_path = pathlib.Path(temp_dir) |
| 101 | + |
| 102 | + # Create certificates directory with only one file |
| 103 | + certs_dir = temp_path / "Certificates" |
| 104 | + certs_dir.mkdir() |
| 105 | + (certs_dir / "cert1.cer").touch() |
| 106 | + |
| 107 | + # Create JSON file referencing missing certificate |
| 108 | + json_file = temp_path / "dbx_info_msft_01_01_24.json" |
| 109 | + json_data = { |
| 110 | + "certificates": [ |
| 111 | + { |
| 112 | + "value": "cert1.cer", |
| 113 | + "subjectName": "Test Subject 1", |
| 114 | + "issuerName": "Test Issuer 1", |
| 115 | + "thumbprint": "abc123", |
| 116 | + "description": "Test certificate 1", |
| 117 | + "dateOfAddition": "2024-01-01" |
| 118 | + }, |
| 119 | + { |
| 120 | + "value": "missing_cert.cer", |
| 121 | + "subjectName": "Test Subject 2", |
| 122 | + "issuerName": "Test Issuer 2", |
| 123 | + "thumbprint": "def456", |
| 124 | + "description": "Test certificate 2", |
| 125 | + "dateOfAddition": "2024-01-01" |
| 126 | + } |
| 127 | + ] |
| 128 | + } |
| 129 | + with json_file.open("w") as f: |
| 130 | + json.dump(json_data, f) |
| 131 | + |
| 132 | + # Should fail validation |
| 133 | + errors = validate_certificate_references(json_file, certs_dir) |
| 134 | + assert len(errors) == 1 |
| 135 | + assert "missing_cert.cer" in errors[0] |
| 136 | + assert "not found" in errors[0] |
| 137 | + |
| 138 | + |
| 139 | +def test_validate_certificate_references_missing_value_field() -> None: |
| 140 | + """Test validation when certificate entry is missing value field.""" |
| 141 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 142 | + temp_path = pathlib.Path(temp_dir) |
| 143 | + |
| 144 | + # Create certificates directory |
| 145 | + certs_dir = temp_path / "Certificates" |
| 146 | + certs_dir.mkdir() |
| 147 | + |
| 148 | + # Create JSON file with malformed certificate entry |
| 149 | + json_file = temp_path / "dbx_info_msft_01_01_24.json" |
| 150 | + json_data = { |
| 151 | + "certificates": [ |
| 152 | + { |
| 153 | + "subjectName": "Test Subject", |
| 154 | + "issuerName": "Test Issuer", |
| 155 | + "thumbprint": "abc123", |
| 156 | + "description": "Test certificate", |
| 157 | + "dateOfAddition": "2024-01-01" |
| 158 | + # Missing "value" field |
| 159 | + } |
| 160 | + ] |
| 161 | + } |
| 162 | + with json_file.open("w") as f: |
| 163 | + json.dump(json_data, f) |
| 164 | + |
| 165 | + # Should fail validation |
| 166 | + errors = validate_certificate_references(json_file, certs_dir) |
| 167 | + assert len(errors) == 1 |
| 168 | + assert "missing 'value' field" in errors[0] |
| 169 | + |
| 170 | + |
| 171 | +def test_validate_certificate_references_file_not_found() -> None: |
| 172 | + """Test validation when JSON file doesn't exist.""" |
| 173 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 174 | + temp_path = pathlib.Path(temp_dir) |
| 175 | + |
| 176 | + # Create certificates directory |
| 177 | + certs_dir = temp_path / "Certificates" |
| 178 | + certs_dir.mkdir() |
| 179 | + |
| 180 | + # Reference non-existent JSON file |
| 181 | + json_file = temp_path / "nonexistent.json" |
| 182 | + |
| 183 | + # Should raise FileNotFoundError |
| 184 | + with pytest.raises(FileNotFoundError): |
| 185 | + validate_certificate_references(json_file, certs_dir) |
| 186 | + |
| 187 | + |
| 188 | +def test_validate_certificate_references_invalid_json() -> None: |
| 189 | + """Test validation when JSON file is malformed.""" |
| 190 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 191 | + temp_path = pathlib.Path(temp_dir) |
| 192 | + |
| 193 | + # Create certificates directory |
| 194 | + certs_dir = temp_path / "Certificates" |
| 195 | + certs_dir.mkdir() |
| 196 | + |
| 197 | + # Create malformed JSON file |
| 198 | + json_file = temp_path / "dbx_info_msft_01_01_24.json" |
| 199 | + with json_file.open("w") as f: |
| 200 | + f.write("{ invalid json }") |
| 201 | + |
| 202 | + # Should raise json.JSONDecodeError |
| 203 | + with pytest.raises(json.JSONDecodeError): |
| 204 | + validate_certificate_references(json_file, certs_dir) |
0 commit comments