-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from cs278/refactor-command
Add unit tests of advisory loading
- Loading branch information
Showing
9 changed files
with
238 additions
and
32 deletions.
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,33 @@ | ||
<?php | ||
|
||
namespace Cs278\ComposerAudit; | ||
|
||
/** | ||
* Handles installing security advisories. | ||
* | ||
* @copyright 2020 Chris Smith | ||
* @license MIT | ||
*/ | ||
interface AdvisoriesInstallerInterface | ||
{ | ||
/** | ||
* Require the installer to check if updates are available. | ||
* | ||
* @return void | ||
*/ | ||
public function mustUpdate(); | ||
|
||
/** | ||
* Install advisories database package in to the given directory. | ||
* | ||
* @param string $varDirectory Directory to store the database | ||
* @param string $packageName Package name of the advisories database | ||
* @param string $packageConstraint Required constraint for the advisories | ||
* database package. | ||
* | ||
* @return string The base directory of the advisories database, this will | ||
* usually be $varDirectory or a sub-directory of it but | ||
* consumers shouldn't rely on this. | ||
*/ | ||
public function install($varDirectory, $packageName, $packageConstraint); | ||
} |
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,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cs278\ComposerAudit; | ||
|
||
use Composer\Composer; | ||
use Composer\Semver\Semver; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
use Symfony\Component\Process\Process; | ||
|
||
use function Cs278\Mktemp\temporaryDir; | ||
|
||
/** | ||
* @covers Cs278\ComposerAudit\AdvisoriesManager | ||
*/ | ||
final class AdvisoriesManagerTest extends TestCase | ||
{ | ||
use SetUpTearDownTrait; | ||
|
||
/** | ||
* @dataProvider dataFindByPackageNameAndVersion | ||
* | ||
*/ | ||
public function testFindByPackageNameAndVersion(array $expected, string $packageName, string $packageVersion, string $advisories) | ||
{ | ||
$manager = $this->createManager($advisories); | ||
$results = []; | ||
|
||
foreach ($manager->findByPackageNameAndVersion($packageName, $packageVersion) as $advisory) { | ||
$results[] = $advisory['title']; | ||
|
||
self::assertEquals(sprintf('composer://%s', $packageName), $advisory['reference']); | ||
} | ||
|
||
self::assertEquals($expected, $results); | ||
} | ||
|
||
public function dataFindByPackageNameAndVersion(): iterable | ||
{ | ||
yield [ | ||
[], | ||
'foo/bar', | ||
'13.37.0', | ||
'empty', | ||
]; | ||
yield [ | ||
[ | ||
'CVE-9999-1234567: Left the front door open', | ||
], | ||
'foo/bar', | ||
'13.37', | ||
'simple', | ||
]; | ||
} | ||
|
||
private function createManager(string $advisories): AdvisoriesManager | ||
{ | ||
$installer = new class($advisories) implements AdvisoriesInstallerInterface { | ||
private $advisories; | ||
|
||
public function __construct(string $advisories) | ||
{ | ||
$this->advisories = __DIR__.'/advisories/'.$advisories; | ||
|
||
if (!is_dir($this->advisories)) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'%s is invalid, `%s` is not a directory', | ||
$advisories, | ||
$this->advisories | ||
)); | ||
} | ||
} | ||
|
||
public function mustUpdate() | ||
{ | ||
return; // No op | ||
} | ||
|
||
public function install($varDirectory, $packageName, $packageConstraint) | ||
{ | ||
return $this->advisories; | ||
} | ||
}; | ||
|
||
return new AdvisoriesManager($installer); | ||
} | ||
} |
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,6 @@ | ||
|
||
{ | ||
"name": "sensiolabs/security-advisories", | ||
"description": "Database of known security vulnerabilities in various PHP projects and libraries", | ||
"license": "Unlicense" | ||
} |
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,6 @@ | ||
|
||
{ | ||
"name": "sensiolabs/security-advisories", | ||
"description": "Database of known security vulnerabilities in various PHP projects and libraries", | ||
"license": "Unlicense" | ||
} |
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,8 @@ | ||
title: "CVE-9999-1234567: Left the front door open" | ||
link: https://example.com/CVE-9999-1234567 | ||
cve: CVE-9999-1234567 | ||
branches: | ||
"1337": | ||
time: 2020-01-01 12:32:00 | ||
versions: ['>=13.37.0', '<13.37.100'] | ||
reference: composer://foo/bar |