|
4 | 4 | import unittest
|
5 | 5 | from pathlib import Path
|
6 | 6 |
|
| 7 | +import pytest |
| 8 | + |
7 | 9 | from cve_bin_tool.util import CVE, CVEData, ProductInfo, Remarks
|
8 | 10 | from cve_bin_tool.vex_manager.generate import VEXGenerate
|
| 11 | +from cve_bin_tool.vex_manager.parse import VEXParse |
9 | 12 |
|
10 | 13 | TEST_DIR = Path(__file__).parent.resolve()
|
11 | 14 | VEX_PATH = TEST_DIR / "vex"
|
@@ -113,6 +116,147 @@ def test_output_cyclonedx(self):
|
113 | 116 |
|
114 | 117 | Path("generated_cyclonedx_vex.json").unlink()
|
115 | 118 |
|
| 119 | + def test_output_openvex(self): |
| 120 | + """Test VEX output generation""" |
| 121 | + |
| 122 | + vexgen = VEXGenerate( |
| 123 | + "dummy-product", |
| 124 | + "1.0", |
| 125 | + "dummy-vendor", |
| 126 | + "generated_openvex_vex.json", |
| 127 | + "openvex", |
| 128 | + self.FORMATTED_DATA, |
| 129 | + ) |
| 130 | + vexgen.generate_vex() |
| 131 | + |
| 132 | + with open("generated_openvex_vex.json") as f: |
| 133 | + json_data = json.load(f) |
| 134 | + # remove dynamic fields such as timestamp and id |
| 135 | + json_data.pop("@id", None) |
| 136 | + json_data.pop("timestamp", None) |
| 137 | + for statement in json_data.get("statements", []): |
| 138 | + statement.pop("timestamp", None) |
| 139 | + statement.pop("action_statement_timestamp", None) |
| 140 | + |
| 141 | + with open(str(VEX_PATH / "test_openvex_vex.json")) as f: |
| 142 | + expected_json = json.load(f) |
| 143 | + # remove dynamic fields such as timestamp and id |
| 144 | + expected_json.pop("@id", None) |
| 145 | + expected_json.pop("timestamp", None) |
| 146 | + for statement in expected_json.get("statements", []): |
| 147 | + statement.pop("timestamp", None) |
| 148 | + statement.pop("action_statement_timestamp", None) |
| 149 | + |
| 150 | + assert json_data == expected_json |
| 151 | + |
| 152 | + Path("generated_openvex_vex.json").unlink() |
| 153 | + |
| 154 | + |
| 155 | +class TestVexParse: |
| 156 | + PARSED_DATA_WITH_PURL = { |
| 157 | + ProductInfo( |
| 158 | + vendor="vendor0", |
| 159 | + product="product0", |
| 160 | + version="1.0", |
| 161 | + location="location/to/product", |
| 162 | + purl="pkg:generic/vendor0/[email protected]", |
| 163 | + ): { |
| 164 | + "CVE-1234-1004": { |
| 165 | + "remarks": Remarks.NewFound, |
| 166 | + "comments": "", |
| 167 | + "response": [], |
| 168 | + }, |
| 169 | + "CVE-1234-1005": { |
| 170 | + "remarks": Remarks.NotAffected, |
| 171 | + "comments": "", |
| 172 | + "response": [], |
| 173 | + }, |
| 174 | + "paths": {}, |
| 175 | + }, |
| 176 | + ProductInfo( |
| 177 | + vendor="vendor0", |
| 178 | + product="product0", |
| 179 | + version="2.8.6", |
| 180 | + location="location/to/product", |
| 181 | + purl="pkg:generic/vendor0/[email protected]", |
| 182 | + ): { |
| 183 | + "CVE-1234-1007": { |
| 184 | + "remarks": Remarks.Mitigated, |
| 185 | + "comments": "", |
| 186 | + "response": [], |
| 187 | + }, |
| 188 | + "CVE-1234-1008": { |
| 189 | + "remarks": Remarks.NewFound, |
| 190 | + "comments": "", |
| 191 | + "response": [], |
| 192 | + }, |
| 193 | + "paths": {}, |
| 194 | + }, |
| 195 | + } |
| 196 | + PARSED_DATA_WITHOUT_PURL = { |
| 197 | + ProductInfo( |
| 198 | + vendor="vendor0", |
| 199 | + product="product0", |
| 200 | + version="1.0", |
| 201 | + location="location/to/product", |
| 202 | + ): { |
| 203 | + "CVE-1234-1004": { |
| 204 | + "remarks": Remarks.NewFound, |
| 205 | + "comments": "", |
| 206 | + "response": [], |
| 207 | + }, |
| 208 | + "CVE-1234-1005": { |
| 209 | + "remarks": Remarks.NotAffected, |
| 210 | + "comments": "", |
| 211 | + "response": [], |
| 212 | + "justification": "code_not_reachable", |
| 213 | + }, |
| 214 | + "paths": {}, |
| 215 | + }, |
| 216 | + ProductInfo( |
| 217 | + vendor="vendor0", |
| 218 | + product="product0", |
| 219 | + version="2.8.6", |
| 220 | + location="location/to/product", |
| 221 | + ): { |
| 222 | + "CVE-1234-1007": { |
| 223 | + "remarks": Remarks.Mitigated, |
| 224 | + "comments": "", |
| 225 | + "response": [], |
| 226 | + }, |
| 227 | + "CVE-1234-1008": { |
| 228 | + "remarks": Remarks.NewFound, |
| 229 | + "comments": "", |
| 230 | + "response": [], |
| 231 | + }, |
| 232 | + "paths": {}, |
| 233 | + }, |
| 234 | + } |
| 235 | + |
| 236 | + @pytest.mark.parametrize( |
| 237 | + "vex_format, vex_filename, expected_parsed_data", |
| 238 | + [ |
| 239 | + ("cyclonedx", "test_cyclonedx_vex.json", PARSED_DATA_WITHOUT_PURL), |
| 240 | + ], |
| 241 | + ) |
| 242 | + def test_parse_cyclonedx(self, vex_format, vex_filename, expected_parsed_data): |
| 243 | + """Test parsing of CycloneDX VEX""" |
| 244 | + vexparse = VEXParse(str(VEX_PATH / vex_filename), vex_format) |
| 245 | + parsed_data = vexparse.parse_vex() |
| 246 | + assert parsed_data == expected_parsed_data |
| 247 | + |
| 248 | + @pytest.mark.parametrize( |
| 249 | + "vex_format, vex_filename, expected_parsed_data", |
| 250 | + [ |
| 251 | + ("openvex", "test_openvex_vex.json", PARSED_DATA_WITH_PURL), |
| 252 | + ], |
| 253 | + ) |
| 254 | + def test_parse_openvex(self, vex_format, vex_filename, expected_parsed_data): |
| 255 | + """Test parsing of OpenVEX VEX""" |
| 256 | + vexparse = VEXParse(str(VEX_PATH / vex_filename), vex_format) |
| 257 | + parsed_data = vexparse.parse_vex() |
| 258 | + assert parsed_data == expected_parsed_data |
| 259 | + |
116 | 260 |
|
117 | 261 | if __name__ == "__main__":
|
118 | 262 | unittest.main()
|
0 commit comments