Skip to content

Commit

Permalink
test: refactor AddTaintsInterfaceTest
Browse files Browse the repository at this point in the history
Extract duplicate code to methods.
  • Loading branch information
Patrick-Remy committed Jan 19, 2025
1 parent 5471bb1 commit 4af11dc
Showing 1 changed file with 53 additions and 166 deletions.
219 changes: 53 additions & 166 deletions tests/Config/Plugin/EventHandler/AddTaints/AddTaintsInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,7 @@ private function getProjectAnalyzerWithConfig(Config $config): ProjectAnalyzer
);
}

public function setUp(): void
{
RuntimeCaches::clearAll();
$this->file_provider = new FakeFileProvider();
}

public function testTaintBadDataVariables(): void
private function setupProjectAnalyzerWithTaintBadDataPlugin(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
Expand All @@ -77,28 +71,10 @@ public function testTaintBadDataVariables(): void
</psalm>',
),
);

$this->project_analyzer->getCodebase()->config->initializePlugins($this->project_analyzer);

$file_path = getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php // --taint-analysis
echo $bad_data;
',
);

$this->project_analyzer->trackTaintedInputs();

$this->expectException(CodeException::class);
$this->expectExceptionMessageMatches('/TaintedHtml/');

$this->analyzeFile($file_path, new Context());
}

public function testTaintsArePassedByTaintedAssignments(): void
private function setupProjectAnalyzerWithActiveRecordPlugin(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
Expand All @@ -112,13 +88,50 @@ public function testTaintsArePassedByTaintedAssignments(): void
<directory name="src" />
</projectFiles>
<plugins>
<plugin filename="tests/Config/Plugin/EventHandler/AddTaints/TaintBadDataPlugin.php" />
<plugin filename="examples/plugins/TaintActiveRecords.php" />
</plugins>
</psalm>',
),
);

$this->project_analyzer->getCodebase()->config->initializePlugins($this->project_analyzer);
}

private function expectTaintedHtml(): void
{
$this->project_analyzer->trackTaintedInputs();

$this->expectException(CodeException::class);
$this->expectExceptionMessageMatches('/TaintedHtml/');
}

public function setUp(): void
{
RuntimeCaches::clearAll();
$this->file_provider = new FakeFileProvider();
}

public function testTaintBadDataVariables(): void
{
$this->setupProjectAnalyzerWithTaintBadDataPlugin();

$file_path = getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php // --taint-analysis
echo $bad_data;
',
);

$this->expectTaintedHtml();

$this->analyzeFile($file_path, new Context());
}

public function testTaintsArePassedByTaintedAssignments(): void
{
$this->setupProjectAnalyzerWithTaintBadDataPlugin();

$file_path = getcwd() . '/src/somefile.php';

Expand All @@ -131,35 +144,14 @@ public function testTaintsArePassedByTaintedAssignments(): void
',
);

$this->project_analyzer->trackTaintedInputs();

$this->expectException(CodeException::class);
$this->expectExceptionMessageMatches('/TaintedHtml/');
$this->expectTaintedHtml();

$this->analyzeFile($file_path, new Context());
}

public function testTaintsAreOverriddenByRawAssignments(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 5) . DIRECTORY_SEPARATOR,
'<?xml version="1.0"?>
<psalm
errorLevel="6"
runTaintAnalysis="true"
>
<projectFiles>
<directory name="src" />
</projectFiles>
<plugins>
<plugin filename="tests/Config/Plugin/EventHandler/AddTaints/TaintBadDataPlugin.php" />
</plugins>
</psalm>',
),
);

$this->project_analyzer->getCodebase()->config->initializePlugins($this->project_analyzer);
$this->setupProjectAnalyzerWithTaintBadDataPlugin();

$file_path = getcwd() . '/src/somefile.php';

Expand All @@ -181,25 +173,7 @@ public function testTaintsAreOverriddenByRawAssignments(): void

public function testTaintsArePassedByTaintedFuncReturns(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 5) . DIRECTORY_SEPARATOR,
'<?xml version="1.0"?>
<psalm
errorLevel="6"
runTaintAnalysis="true"
>
<projectFiles>
<directory name="src" />
</projectFiles>
<plugins>
<plugin filename="tests/Config/Plugin/EventHandler/AddTaints/TaintBadDataPlugin.php" />
</plugins>
</psalm>',
),
);

$this->project_analyzer->getCodebase()->config->initializePlugins($this->project_analyzer);
$this->setupProjectAnalyzerWithTaintBadDataPlugin();

$file_path = getcwd() . '/src/somefile.php';

Expand All @@ -215,35 +189,14 @@ function genBadData() {
',
);

$this->project_analyzer->trackTaintedInputs();

$this->expectException(CodeException::class);
$this->expectExceptionMessageMatches('/TaintedHtml/');
$this->expectTaintedHtml();

$this->analyzeFile($file_path, new Context());
}

public function testTaintsArePassedByTaintedFuncMultipleReturns(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 5) . DIRECTORY_SEPARATOR,
'<?xml version="1.0"?>
<psalm
errorLevel="6"
runTaintAnalysis="true"
>
<projectFiles>
<directory name="src" />
</projectFiles>
<plugins>
<plugin filename="tests/Config/Plugin/EventHandler/AddTaints/TaintBadDataPlugin.php" />
</plugins>
</psalm>',
),
);

$this->project_analyzer->getCodebase()->config->initializePlugins($this->project_analyzer);
$this->setupProjectAnalyzerWithTaintBadDataPlugin();

$file_path = getcwd() . '/src/somefile.php';

Expand All @@ -263,36 +216,15 @@ function genBadData(bool $html) {
',
);

$this->project_analyzer->trackTaintedInputs();

// Find TaintedHtml here, not TaintedSql, as this is not a sink for echo
$this->expectException(CodeException::class);
$this->expectExceptionMessageMatches('/TaintedHtml/');
$this->expectTaintedHtml();

$this->analyzeFile($file_path, new Context());
}

public function testTaintsArePassedByTaintedMethodReturns(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 5) . DIRECTORY_SEPARATOR,
'<?xml version="1.0"?>
<psalm
errorLevel="6"
runTaintAnalysis="true"
>
<projectFiles>
<directory name="src" />
</projectFiles>
<plugins>
<plugin filename="tests/Config/Plugin/EventHandler/AddTaints/TaintBadDataPlugin.php" />
</plugins>
</psalm>',
),
);

$this->project_analyzer->getCodebase()->config->initializePlugins($this->project_analyzer);
$this->setupProjectAnalyzerWithTaintBadDataPlugin();

$file_path = getcwd() . '/src/somefile.php';

Expand All @@ -311,35 +243,14 @@ public function genBadData() {
',
);

$this->project_analyzer->trackTaintedInputs();

$this->expectException(CodeException::class);
$this->expectExceptionMessageMatches('/TaintedHtml/');
$this->expectTaintedHtml();

$this->analyzeFile($file_path, new Context());
}

public function testAddTaintsActiveRecord(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 5) . DIRECTORY_SEPARATOR,
'<?xml version="1.0"?>
<psalm
errorLevel="6"
runTaintAnalysis="true"
>
<projectFiles>
<directory name="src" />
</projectFiles>
<plugins>
<plugin filename="examples/plugins/TaintActiveRecords.php" />
</plugins>
</psalm>',
),
);

$this->project_analyzer->getCodebase()->config->initializePlugins($this->project_analyzer);
$this->setupProjectAnalyzerWithActiveRecordPlugin();

$file_path = getcwd() . '/src/somefile.php';

Expand All @@ -358,35 +269,14 @@ class User {
',
);

$this->project_analyzer->trackTaintedInputs();

$this->expectException(CodeException::class);
$this->expectExceptionMessageMatches('/TaintedHtml/');
$this->expectTaintedHtml();

$this->analyzeFile($file_path, new Context());
}

public function testAddTaintsActiveRecordList(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 5) . DIRECTORY_SEPARATOR,
'<?xml version="1.0"?>
<psalm
errorLevel="6"
runTaintAnalysis="true"
>
<projectFiles>
<directory name="src" />
</projectFiles>
<plugins>
<plugin filename="examples/plugins/TaintActiveRecords.php" />
</plugins>
</psalm>',
),
);

$this->project_analyzer->getCodebase()->config->initializePlugins($this->project_analyzer);
$this->setupProjectAnalyzerWithActiveRecordPlugin();

$file_path = getcwd() . '/src/somefile.php';

Expand Down Expand Up @@ -416,10 +306,7 @@ public static function findAll(): array {
',
);

$this->project_analyzer->trackTaintedInputs();

$this->expectException(CodeException::class);
$this->expectExceptionMessageMatches('/TaintedHtml/');
$this->expectTaintedHtml();

$this->analyzeFile($file_path, new Context());
}
Expand Down

0 comments on commit 4af11dc

Please sign in to comment.