Skip to content

Commit

Permalink
Add tests for PGPKeyManager
Browse files Browse the repository at this point in the history
  • Loading branch information
cracksalad committed Jul 7, 2024
1 parent 38ed95c commit 7a2ab77
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 14 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"require-dev": {
"squizlabs/php_codesniffer": "^3.7.2",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"phpcompatibility/php-compatibility": "^9.3.5"
"phpcompatibility/php-compatibility": "^9.3.5",
"psr/log": "^1.0 || ^2.0 || ^3.0"
},
"suggest": {
"ext-gnupg": "is technically required, but not compatible with Windows",
Expand Down
10 changes: 10 additions & 0 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@
<code><![CDATA[PHPMailerPGP]]></code>
</PropertyNotSetInConstructor>
</file>
<file src="tests/PGPKeyManagerTest.php">
<MissingDependency>
<code><![CDATA[PGPTestCase]]></code>
</MissingDependency>
</file>
<file src="tests/PHPMailerPGPTest.php">
<MissingDependency>
<code><![CDATA[PGPTestCase]]></code>
</MissingDependency>
</file>
</files>
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<errorLevel type="suppress">
<referencedClass name="gnupg" />
<referencedClass name="PHPUnit\Framework\TestCase"></referencedClass>
<referencedClass name="Psr\Log\LoggerInterface"></referencedClass>
</errorLevel>
</UndefinedClass>
<UndefinedDocblockClass errorLevel="suppress" />
Expand Down
115 changes: 115 additions & 0 deletions tests/PGPKeyManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
declare(strict_types=1);

namespace PHPMailerPGP\Test;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPMailer\PHPMailerPGP\PGPKeyManager;
use PHPMailer\PHPMailerPGP\PGPHelper;

/**
* @author Andreas Wahlen
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
#[CoversClass(PGPHelper::class)]
#[CoversClass(PGPKeyManager::class)]
class PGPKeyManagerTest extends PGPTestCase
{
private PGPKeyManager $keyManager;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

$gnupg = new \gnupg();
$gnupg->seterrormode(GNUPG_ERROR_EXCEPTION);
if ($gnupg->import(file_get_contents(__DIR__.'/public.asc')) === false) {
echo 'failed to import example key'.PHP_EOL;
}
}

protected function setUp(): void
{
parent::setUp();

$this->keyManager = new PGPKeyManager(new class() implements \Psr\Log\LoggerInterface {
/**
* @param string|\Stringable $message
*/
public function emergency($message, array $context = []): void {
$this->log(\Psr\Log\LogLevel::EMERGENCY, $message, $context);
}

/**
* @param string|\Stringable $message
*/
public function alert($message, array $context = []): void {
$this->log(\Psr\Log\LogLevel::ALERT, $message, $context);
}

/**
* @param string|\Stringable $message
*/
public function critical($message, array $context = []): void {
$this->log(\Psr\Log\LogLevel::CRITICAL, $message, $context);
}

/**
* @param string|\Stringable $message
*/
public function error($message, array $context = []): void {
$this->log(\Psr\Log\LogLevel::ERROR, $message, $context);
}

/**
* @param string|\Stringable $message
*/
public function warning($message, array $context = []): void {
$this->log(\Psr\Log\LogLevel::WARNING, $message, $context);
}

/**
* @param string|\Stringable $message
*/
public function notice($message, array $context = []): void {
$this->log(\Psr\Log\LogLevel::NOTICE, $message, $context);
}

/**
* @param string|\Stringable $message
*/
public function info($message, array $context = []): void {
$this->log(\Psr\Log\LogLevel::INFO, $message, $context);
}

/**
* @param string|\Stringable $message
*/
public function debug($message, array $context = []): void {
$this->log(\Psr\Log\LogLevel::DEBUG, $message, $context);
}

/**
* @param string|\Stringable $message
*/
public function log($level, $message, array $context = []): void {
foreach($context as $key => $value){
$message = str_replace('{'.$key.'}', var_export($value, true), $message);
}
//echo strtoupper($level)."\t".$message.PHP_EOL;
}
});
$this->keyManager->setGPGHome(self::$gnupgHome);
}

public function testDeleteAndImport(): void
{
$this->keyManager->deleteKey('user@example.com', true);
$this->assertEmpty($this->keyManager->getKeys('user@example.com', 'sign'), 'private key still there');
$this->assertEmpty($this->keyManager->getKeys('user@example.com', 'encrypt'), 'public key still there');

$this->keyManager->importKeyFile(__DIR__.'/public.asc');
$this->assertCount(1, $this->keyManager->getKeys('user@example.com', 'sign'), 'private key not found');
$this->assertCount(1, $this->keyManager->getKeys('user@example.com', 'encrypt'), 'public key not found');
}
}
34 changes: 34 additions & 0 deletions tests/PGPTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

namespace PHPMailerPGP\Test;

use PHPUnit\Framework\TestCase;

/**
* @author Andreas Wahlen
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
class PGPTestCase extends TestCase
{
protected static string $gnupgHome;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

self::$gnupgHome = __DIR__ . '/.gnupg';
if (!file_exists(self::$gnupgHome)) {
if (!mkdir(self::$gnupgHome) || !chmod(self::$gnupgHome, 0700)) {
echo 'unable to create GnuPG home'.PHP_EOL;
}
}
putenv('GNUPGHOME=' . escapeshellcmd(self::$gnupgHome));
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
exec('rm -rf '.escapeshellcmd(self::$gnupgHome));
}
}
14 changes: 1 addition & 13 deletions tests/PHPMailerPGPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPMailer\PHPMailerPGP\PHPMailerPGP;
use PHPMailer\PHPMailerPGP\PGPHelper;
use PHPMailer\PHPMailerPGP\PGPKeyManager;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

/**
Expand All @@ -16,25 +15,14 @@
#[CoversClass(PHPMailerPGP::class)]
#[CoversClass(PGPHelper::class)]
#[CoversClass(PGPKeyManager::class)]
class PHPMailerPGPTest extends TestCase
class PHPMailerPGPTest extends PGPTestCase
{
private static string $gnupgHome;

private PHPMailerPGP $mailer;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

self::$gnupgHome = __DIR__ . '/.gnupg';
if (!file_exists(self::$gnupgHome)) {
echo 'GnuPG home did not exist'.PHP_EOL;
if (!mkdir(self::$gnupgHome)) {
echo 'unable to create GnuPG home'.PHP_EOL;
}
}
putenv('GNUPGHOME=' . escapeshellcmd(self::$gnupgHome));

$gnupg = new \gnupg();
if ($gnupg->import(file_get_contents(__DIR__.'/private.key')) === false) {
echo 'failed to import example key'.PHP_EOL;
Expand Down
41 changes: 41 additions & 0 deletions tests/public.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQGNBGX0uLYBDAChB+OMEWb7uk3xwmiWmU3CQR+UT6b/k8L55fAfGJxVwA8UlFa8
7TnhpYG4hb6vgYUiOvU57r46VGpahwCO48ZX0nN0qZQSFHJQYYUmWNCTpwNuyO8Y
Kbnjc6EhlhZquABRXeJrvraOGPLRuU6KEtkAdVLnAylObmelrWjmZm+om/H+GtRB
ketDoR1tSt7egfxpZNA0a/zf0hKH1JOskru7lG4rjCpbaF8xynmjZtXIYjFAgDCo
DRTZSYvmo55ESoKvbd77ukXjHandpZQEnILiozY43xOJDsNASTqCi5PqUqW7OASL
aANxarC76KYGYw7yp3W+OduQcL+aZ/7OrJ6G6zcERijWWeXndVlLH427uRRDNzFQ
/MjgzGzt0JMkNeC/KqHsnYv8ScIBQzhCNJznIjRndbnh0SMWKV/tizcrFE0xXtf9
2CxNRGwOZOKsVhDFVNn9ptWleW9fQhqPf5OwEJjD8568RTRRxQAPYEC/s2m6X4hh
zDrP6DM8Fhm4D+EAEQEAAbQdVW5pdFRlc3RlciA8dXNlckBleGFtcGxlLmNvbT6J
AdQEEwEKAD4WIQSXPGhtwOMQXlAc8j7Ycttxrux93AUCZfS4tgIbAwUJA8JnAAUL
CQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRDYcttxrux93DCKC/9ECuOz/nGNBzgV
XWtoB8MB3bZlJaIDGFl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