Skip to content

Commit

Permalink
Switch to submission time validation of XML files
Browse files Browse the repository at this point in the history
Add a test which utilizes the submission function of KWTester to
submit the same data as the manual validation steps.
  • Loading branch information
josephsnyder committed Nov 25, 2024
1 parent 858edce commit 309f06e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 26 deletions.
31 changes: 31 additions & 0 deletions app/Http/Controllers/SubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Models\Site;
use App\Utils\AuthTokenUtil;
use App\Utils\UnparsedSubmissionProcessor;
use App\Utils\SubmissionUtils;
use App\Exceptions\CDashXMLValidationException;
use CDash\Model\Build;
use CDash\Model\PendingSubmissions;
use CDash\Model\Project;
Expand Down Expand Up @@ -128,6 +130,35 @@ private function submitProcess(): Response
} elseif (intval($this->project->Id) < 1) {
abort(Response::HTTP_NOT_FOUND, 'The requested project does not exist.');
}
if ((bool) config('cdash.validate_xml_submissions') === true) {
// Figure out what type of XML file this is.
try {
$filename = "inbox/".$filename;
$xml_info = SubmissionUtils::get_xml_type(fopen(Storage::path($filename), 'r'), $filename);
} catch (CDashXMLValidationException $e) {
foreach ($e->getDecodedMessage() as $error) {
Log::error($error);
}
abort(Response::HTTP_NOT_FOUND, 'Unable to determine the Type of the submission file.');
}
$filehandle = $xml_info['file_handle'];
$handler_ref = $xml_info['xml_handler'];
$file = $xml_info['xml_type'];
$schema_file = $xml_info['xml_schema'];

// If validation is enabled and if this file has a corresponding schema, validate it
if (isset($schema_file)) {
try {
SubmissionUtils::validate_xml(storage_path("app/".$filename), $schema_file);
} catch (CDashXMLValidationException $e) {
foreach ($e->getDecodedMessage() as $error) {
Log::error("Validating $filename: ".$error);
}
abort(400, "Xml validation failed: rejected file $filename");
}
}
}


// Check if CTest provided us enough info to assign a buildid.
$pendingSubmissions = new PendingSubmissions();
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/ProcessSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private function doSubmit($filename, $projectid, $buildid = null, $expected_md5
}

// Parse the XML file
$handler = ctest_parse($filehandle, $filename, $projectid, $expected_md5, $buildid);
$handler = ctest_parse($filehandle, $filename, $projectid, $expected_md5, $buildid);
fclose($filehandle);
unset($filehandle);

Expand Down
26 changes: 2 additions & 24 deletions app/cdash/include/ctestparser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

use CDash\Database;
use App\Models\BuildFile;
use App\Utils\SubmissionUtils;
use CDash\Model\Project;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use App\Utils\SubmissionUtils;
use App\Exceptions\CDashXMLValidationException;

class CDashParseException extends RuntimeException
{
Expand Down Expand Up @@ -179,31 +178,10 @@ function ctest_parse($filehandle, string $filename, $projectid, $expected_md5 =
$Project->Id = $projectid;

// Figure out what type of XML file this is.
try {
$xml_info = SubmissionUtils::get_xml_type($filehandle, $filename);
} catch (CDashXMLValidationException $e) {
foreach ($e->getDecodedMessage() as $error) {
Log::error($error);
}
return false;
}
$xml_info = SubmissionUtils::get_xml_type($filehandle, $filename);
$filehandle = $xml_info['file_handle'];
$handler_ref = $xml_info['xml_handler'];
$file = $xml_info['xml_type'];
$schema_file = $xml_info['xml_schema'];

// If validation is enabled and if this file has a corresponding schema, validate it
if (((bool) config('cdash.validate_xml_submissions')) === true && isset($schema_file)) {
try {
SubmissionUtils::validate_xml(storage_path("app/".$filename), $schema_file);
} catch (CDashXMLValidationException $e) {
foreach ($e->getDecodedMessage() as $error) {
Log::error("Validating $filename: ".$error);
}
abort(400, "Xml validation failed: rejected file $filename");
}
}

$handler = isset($handler_ref) ? new $handler_ref($Project) : null;

rewind($filehandle);
Expand Down
3 changes: 3 additions & 0 deletions app/cdash/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -604,5 +604,8 @@ set_tests_properties(/Feature/RemoteWorkers PROPERTIES DEPENDS install_3)

add_laravel_test(/Unit/app/Console/Command/ValidateXmlCommandTest)

add_php_test(submissionvalidation)
set_tests_properties(submissionvalidation PROPERTIES DEPENDS createpublicdashboard)

add_subdirectory(ctest)
add_subdirectory(autoremovebuilds)
39 changes: 39 additions & 0 deletions app/cdash/tests/test_submissionvalidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
require_once dirname(__FILE__) . '/cdash_test_case.php';



class SubmissionValidationTestCase extends KWWebTestCase
{
protected $PDO;
protected $ConfigFile;
protected $Original;

public function _construct()
{

parent::__construct();
}

public function submit($fileName)
{
$rep = dirname(__FILE__) . '/../../../tests/data/XmlValidation';
$file = "$rep/$fileName";

return $this->submission('PublicDashboard', $file);
}

public function testSubmissionValidation()
{

$this->ConfigFile = dirname(__FILE__) . '/../../../.env';
$this->Original = file_get_contents($this->ConfigFile);
file_put_contents($this->ConfigFile, "VALIDATE_XML_SUBMISSIONS=true\n", FILE_APPEND | LOCK_EX);

$this->assertFalse($this->submit("invalid_Configure.xml"), "Submission of invalid_syntax_Build.xml was not succeessful");
$this->assertFalse($this->submit("invalid_syntax_Build.xml"), "Submission of invalid_syntax_Build.xml was not succeessful");
$this->assertTrue($this->submit("valid_Build.xml"), "Submission of valid_Build.xml has succeeded");

file_put_contents($this->ConfigFile, $this->Original);
}
}
62 changes: 61 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ parameters:
path: app/Console/Commands/UpdateDependencies.php

-
message: "#^Part \\$xml_type \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
message: "#^Parameter \\#2 \\$schema_file of static method App\\\\Utils\\\\SubmissionUtils\\:\\:validate_xml\\(\\) expects string, mixed given\\.$#"
count: 1
path: app/Console/Commands/ValidateXml.php

Expand All @@ -186,6 +186,11 @@ parameters:
count: 1
path: app/Console/Kernel.php

-
message: "#^Method App\\\\Exceptions\\\\CDashXMLValidationException\\:\\:getDecodedMessage\\(\\) should return array\\<int, string\\> but returns mixed\\.$#"
count: 1
path: app/Exceptions/CDashXMLValidationException.php

-
message: "#^PHPDoc type array of property App\\\\Exceptions\\\\Handler\\:\\:\\$dontFlash is not the same as PHPDoc type array\\<int, string\\> of overridden property Illuminate\\\\Foundation\\\\Exceptions\\\\Handler\\:\\:\\$dontFlash\\.$#"
count: 1
Expand Down Expand Up @@ -2798,6 +2803,11 @@ parameters:
count: 1
path: app/Http/Controllers/SubmissionController.php

-
message: "#^Parameter \\#2 \\$schema_file of static method App\\\\Utils\\\\SubmissionUtils\\:\\:validate_xml\\(\\) expects string, mixed given\\.$#"
count: 1
path: app/Http/Controllers/SubmissionController.php

-
message: "#^Part \\$projectname \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 4
Expand Down Expand Up @@ -14491,6 +14501,11 @@ parameters:
count: 1
path: app/cdash/include/ctestparser.php

-
message: "#^Function ctest_parse\\(\\) throws checked exception App\\\\Exceptions\\\\CDashXMLValidationException but it's missing from the PHPDoc @throws tag\\.$#"
count: 1
path: app/cdash/include/ctestparser.php

-
message: "#^Function generateBackupFileName\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -27029,6 +27044,46 @@ parameters:
count: 1
path: app/cdash/tests/test_submission_assign_buildid.php

-
message: "#^Method SubmissionValidationTestCase\\:\\:_construct\\(\\) has no return type specified\\.$#"
count: 1
path: app/cdash/tests/test_submissionvalidation.php

-
message: "#^Method SubmissionValidationTestCase\\:\\:submit\\(\\) has no return type specified\\.$#"
count: 1
path: app/cdash/tests/test_submissionvalidation.php

-
message: "#^Method SubmissionValidationTestCase\\:\\:submit\\(\\) has parameter \\$fileName with no type specified\\.$#"
count: 1
path: app/cdash/tests/test_submissionvalidation.php

-
message: "#^Method SubmissionValidationTestCase\\:\\:testSubmissionValidation\\(\\) has no return type specified\\.$#"
count: 1
path: app/cdash/tests/test_submissionvalidation.php

-
message: "#^Property SubmissionValidationTestCase\\:\\:\\$ConfigFile has no type specified\\.$#"
count: 1
path: app/cdash/tests/test_submissionvalidation.php

-
message: "#^Property SubmissionValidationTestCase\\:\\:\\$Original has no type specified\\.$#"
count: 1
path: app/cdash/tests/test_submissionvalidation.php

-
message: "#^Property SubmissionValidationTestCase\\:\\:\\$PDO has no type specified\\.$#"
count: 1
path: app/cdash/tests/test_submissionvalidation.php

-
message: "#^Static call to __construct\\(\\) is only allowed on a parent class in the constructor\\.$#"
count: 1
path: app/cdash/tests/test_submissionvalidation.php

-
message: "#^Method SubmitSortingDataTestCase\\:\\:submitFile\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -32723,6 +32778,11 @@ parameters:
count: 1
path: tests/Feature/UserCommand.php

-
message: "#^Part \\$file \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: tests/Unit/app/Console/Command/ValidateXmlCommandTest.php

-
message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, array\\<int, string\\>\\|string given\\.$#"
count: 1
Expand Down

0 comments on commit 309f06e

Please sign in to comment.