-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38ed95c
commit 7a2ab77
Showing
7 changed files
with
204 additions
and
14 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
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,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'); | ||
} | ||
} |
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,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)); | ||
} | ||
} |
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,41 @@ | ||
-----BEGIN PGP PUBLIC K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