-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for validation of XML submission files #2437
Draft
sbelsk
wants to merge
5
commits into
Kitware:master
Choose a base branch
from
sbelsk:submission-xml-files-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cf73d7d
add XML validation to all submissions
sbelsk 6c1ad03
hide submission validation behind a feature flag
sbelsk 84b56c3
add unit test for xml submission file validation
sbelsk 858edce
Use storage_path on ctest submit for validation
josephsnyder 309f06e
Switch to submission time validation of XML files
josephsnyder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
use RuntimeException; | ||
|
||
class CDashXMLValidationException extends RuntimeException | ||
{ | ||
/** | ||
* @param array<int,string> $message | ||
*/ | ||
public function __construct(array $message = [], int $code = 0, Exception $previous = null) | ||
{ | ||
$encoded_msg = json_encode($message); | ||
$encoded_msg = $encoded_msg===false ? "" : $encoded_msg; | ||
parent::__construct($encoded_msg, $code, $previous); | ||
} | ||
|
||
/** | ||
* @return array<int,string> | ||
*/ | ||
public function getDecodedMessage(bool $assoc = false): array | ||
{ | ||
$decoded_msg = json_decode($this->getMessage(), $assoc); | ||
if (!isset($decoded_msg) || is_bool($decoded_msg)) { | ||
$decoded_msg = ["An XML validation error has occurred!"]; | ||
} | ||
return $decoded_msg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,18 +7,22 @@ | |
use CDash\Database; | ||
use CDash\Model\Build; | ||
use CDash\Model\BuildUpdate; | ||
use DOMDocument; | ||
use App\Exceptions\CDashXMLValidationException; | ||
|
||
class SubmissionUtils | ||
{ | ||
|
||
/** | ||
* Figure out what type of XML file this is | ||
* @return array<string,mixed> | ||
* @throws CDashXMLValidationException | ||
*/ | ||
public static function get_xml_type(mixed $filehandle): array | ||
public static function get_xml_type(mixed $filehandle, string $xml_file): array | ||
{ | ||
$file = ''; | ||
$handler = null; | ||
$schemas_dir = base_path()."/app/Validators/Schemas"; | ||
$schema_file = null; | ||
// read file contents until we recognize its elements | ||
while ($file === '' && !feof($filehandle)) { | ||
$content = fread($filehandle, 8192); | ||
|
@@ -30,56 +34,111 @@ public static function get_xml_type(mixed $filehandle): array | |
// Should be first otherwise confused with Build | ||
$handler = \UpdateHandler::class; | ||
$file = 'Update'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} elseif (str_contains($content, '<Build')) { | ||
$handler = \BuildHandler::class; | ||
$file = 'Build'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} elseif (str_contains($content, '<Configure')) { | ||
$handler = \ConfigureHandler::class; | ||
$file = 'Configure'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} elseif (str_contains($content, '<Testing')) { | ||
$handler = \TestingHandler::class; | ||
$file = 'Test'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} elseif (str_contains($content, '<CoverageLog')) { | ||
// Should be before coverage | ||
$handler = \CoverageLogHandler::class; | ||
$file = 'CoverageLog'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} elseif (str_contains($content, '<Coverage')) { | ||
$handler = \CoverageHandler::class; | ||
$file = 'Coverage'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} elseif (str_contains($content, '<report')) { | ||
$handler = \CoverageJUnitHandler::class; | ||
$file = 'CoverageJUnit'; | ||
} elseif (str_contains($content, '<Notes')) { | ||
$handler = \NoteHandler::class; | ||
$file = 'Notes'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} elseif (str_contains($content, '<DynamicAnalysis')) { | ||
$handler = \DynamicAnalysisHandler::class; | ||
$file = 'DynamicAnalysis'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} elseif (str_contains($content, '<Project')) { | ||
$handler = \ProjectHandler::class; | ||
$file = 'Project'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} elseif (str_contains($content, '<Upload')) { | ||
$handler = \UploadHandler::class; | ||
$file = 'Upload'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} elseif (str_contains($content, '<testsuite')) { | ||
$handler = \TestingJUnitHandler::class; | ||
$file = 'TestJUnit'; | ||
} elseif (str_contains($content, '<Done')) { | ||
$handler = \DoneHandler::class; | ||
$file = 'Done'; | ||
$schema_file = "{$schemas_dir}/{$file}.xsd"; | ||
} | ||
} | ||
|
||
// restore the file descriptor to beginning of file | ||
rewind($filehandle); | ||
|
||
// perform minimal error checking as a sanity check | ||
if ($file === '') { | ||
throw new CDashXMLValidationException(["ERROR: Could not determine submission" | ||
." file type for: '{$xml_file}'"]); | ||
} | ||
if (isset($schema_file) && !file_exists($schema_file)) { | ||
throw new CDashXMLValidationException(["ERROR: Could not find schema file '{$schema_file}'" | ||
." to validate input file: '{$xml_file}'"]); | ||
} | ||
|
||
return [ | ||
'file_handle' => $filehandle, | ||
'xml_handler' => $handler, | ||
'xml_type' => $file, | ||
'xml_schema' => $schema_file, | ||
]; | ||
} | ||
|
||
/** | ||
* Validate the given XML file based on its type | ||
* @throws CDashXMLValidationException | ||
*/ | ||
public static function validate_xml(string $xml_file, string $schema_file): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about moving this to |
||
{ | ||
$errors = []; | ||
|
||
// let us control the failures so we can continue | ||
// parsing files instead of crashing midway | ||
libxml_use_internal_errors(true); | ||
|
||
// load the input file to be validated | ||
$xml = new DOMDocument(); | ||
$xml->load($xml_file, LIBXML_PARSEHUGE); | ||
|
||
// run the validator and collect errors if there are any | ||
if (!$xml->schemaValidate($schema_file)) { | ||
$validation_errors = libxml_get_errors(); | ||
foreach ($validation_errors as $error) { | ||
if ($error->level === LIBXML_ERR_ERROR || $error->level === LIBXML_ERR_FATAL) { | ||
williamjallen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$errors[] = "ERROR: {$error->message} in {$error->file}," | ||
." line: {$error->line}, column: {$error->column}"; | ||
} | ||
} | ||
libxml_clear_errors(); | ||
} | ||
|
||
if (count($errors) !== 0) { | ||
throw new CDashXMLValidationException($errors); | ||
} | ||
} | ||
|
||
/** Add a new build */ | ||
public static function add_build(Build $build) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about running all submissions through the validator? I think it would be useful to log the result of submission validation for all submissions, and instead use the config setting to control whether mis-formatted submissions are entirely rejected or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that idea. It seems like it would be useful information about malformed submissions that aren't "rejected"