Skip to content

Commit 0a8eeec

Browse files
committed
feat(tool): fix failing tests
1 parent 2ab4a8c commit 0a8eeec

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

cve_bin_tool/vex_manager/validate.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -193,31 +193,42 @@ def validate_file(
193193
else:
194194
# Use traditional jsonschema validation for CSAF and OpenVEX
195195
schema = self._get_schema(vex_type, version)
196-
if (
197-
schema and len(schema) > 0
198-
): # Only validate if schema was successfully loaded and not empty
199-
try:
200-
jsonschema.validate(instance=raw_data, schema=schema)
201-
self.logger.info(f"VEX file passed {vex_type} schema validation")
202-
except JsonSchemaValidationError as e:
203-
# Convert jsonschema validation error to our format
204-
path = ".".join(str(p) for p in e.path) if e.path else None
205-
196+
if not schema or len(schema) == 0: # Check if schema loading failed
197+
# If we have errors from schema loading, don't proceed with validation
198+
if self.errors:
199+
return False, self.errors, self.warnings
200+
else:
201+
# If no specific errors but empty schema, add a generic error
206202
self.errors.append(
207203
ValidationError(
208204
"SCHEMA_ERROR",
209-
f"Schema validation error: {e.message}",
210-
field=path,
211-
location=str(e.schema_path) if e.schema_path else None,
205+
f"Failed to load schema for {vex_type} validation",
212206
)
213207
)
214-
except Exception as e:
215-
self.errors.append(
216-
ValidationError(
217-
"VALIDATION_ERROR",
218-
f"Error during schema validation: {str(e)}",
219-
)
208+
return False, self.errors, self.warnings
209+
210+
try:
211+
jsonschema.validate(instance=raw_data, schema=schema)
212+
self.logger.info(f"VEX file passed {vex_type} schema validation")
213+
except JsonSchemaValidationError as e:
214+
# Convert jsonschema validation error to our format
215+
path = ".".join(str(p) for p in e.path) if e.path else None
216+
217+
self.errors.append(
218+
ValidationError(
219+
"SCHEMA_ERROR",
220+
f"Schema validation error: {e.message}",
221+
field=path,
222+
location=str(e.schema_path) if e.schema_path else None,
223+
)
224+
)
225+
except Exception as e:
226+
self.errors.append(
227+
ValidationError(
228+
"VALIDATION_ERROR",
229+
f"Error during schema validation: {str(e)}",
220230
)
231+
)
221232

222233
# Perform format-specific validations
223234
self._perform_format_specific_validation(raw_data, vex_type)

test/test_vex_validate.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,15 @@ def test_schema_download_failure(self, mock_detect, mock_load_local, mock_urlope
287287

288288
@patch("urllib.request.urlopen")
289289
@patch("cve_bin_tool.vex_manager.validate.VEXValidator._load_local_schema")
290-
def test_network_timeout(self, mock_load_local, mock_urlopen):
290+
@patch("cve_bin_tool.vex_manager.validate.SCHEMA_CACHE", new_callable=dict)
291+
def test_network_timeout(self, mock_cache, mock_load_local, mock_urlopen):
291292
"""Test handling of network timeout during schema download for non-CycloneDX formats."""
292293
import socket
293294

294-
# Mock local schema loading to return None to force download
295+
# Clear cache to ensure clean test state
296+
mock_cache.clear()
297+
298+
# Mock local schema loading to return empty dict to force download
295299
mock_load_local.return_value = {}
296300
mock_urlopen.side_effect = socket.timeout("Connection timed out")
297301

0 commit comments

Comments
 (0)