-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vitor Mattos <[email protected]>
- Loading branch information
1 parent
aa82e99
commit 182a970
Showing
1 changed file
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
use OCA\Libresign\Handler\CertificateEngine\OpenSslHandler; | ||
use OCP\AppFramework\Services\IAppConfig; | ||
use OCP\Files\AppData\IAppDataFactory; | ||
use OCP\IConfig; | ||
use OCP\IDateTimeFormatter; | ||
use OCP\ITempManager; | ||
use org\bovigo\vfs\vfsStream; | ||
|
||
final class OpenSslHandlerTest extends \OCA\Libresign\Tests\Unit\TestCase { | ||
private IConfig $config; | ||
private IAppConfig $appConfig; | ||
private IAppDataFactory $appDataFactory; | ||
private IDateTimeFormatter $dateTimeFormatter; | ||
private ITempManager $tempManager; | ||
private OpenSslHandler $openSslHandler; | ||
public function setUp(): void { | ||
$this->config = $this->createMock(IConfig::class); | ||
$this->appConfig = $this->createMock(IAppConfig::class); | ||
$this->appDataFactory = $this->createMock(IAppDataFactory::class); | ||
$this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class); | ||
$this->dateTimeFormatter | ||
->method('formatDateTime') | ||
->willReturn('fake date'); | ||
$this->tempManager = $this->createMock(ITempManager::class); | ||
$this->tempManager | ||
->method('getTemporaryFile') | ||
->willReturn(tempnam(sys_get_temp_dir(), 'temp')); | ||
$this->openSslHandler = new OpenSslHandler( | ||
$this->config, | ||
$this->appConfig, | ||
$this->appDataFactory, | ||
$this->dateTimeFormatter, | ||
$this->tempManager, | ||
); | ||
vfsStream::setup('certificate'); | ||
$this->openSslHandler->setConfigPath('vfs://certificate/'); | ||
} | ||
|
||
/** | ||
* @dataProvider dataReadCertificate | ||
*/ | ||
public function testReadCertificate(string $commonName, array $hosts, string $password, array $csrNames): void { | ||
if (isset($csrNames['C'])) { | ||
$this->openSslHandler->setCountry($csrNames['C']); | ||
} | ||
if (isset($csrNames['ST'])) { | ||
$this->openSslHandler->setState($csrNames['ST']); | ||
} | ||
if (isset($csrNames['O'])) { | ||
$this->openSslHandler->setOrganization($csrNames['O']); | ||
} | ||
if (isset($csrNames['OU'])) { | ||
$this->openSslHandler->setOrganizationalUnit($csrNames['OU']); | ||
} | ||
$this->openSslHandler->generateRootCert($commonName, $csrNames); | ||
|
||
$this->openSslHandler->setHosts($hosts); | ||
$this->openSslHandler->setPassword($password); | ||
$certificateContent = $this->openSslHandler->generateCertificate(); | ||
$parsed = $this->openSslHandler->readCertificate($certificateContent, $password); | ||
$this->assertJsonStringEqualsJsonString( | ||
json_encode($csrNames), | ||
json_encode($parsed['subject']) | ||
); | ||
} | ||
|
||
public static function dataReadCertificate(): array { | ||
return [ | ||
[ | ||
'common name', | ||
['[email protected]'], | ||
'password', | ||
[ | ||
'C' => 'CT', | ||
'ST' => 'Some-State', | ||
'O' => 'Organization Name', | ||
], | ||
], | ||
[ | ||
'common name', | ||
['[email protected]'], | ||
'password', | ||
[ | ||
'C' => 'CT', | ||
'ST' => 'Some-State', | ||
'O' => 'Organization Name', | ||
'OU' => 'Organization Unit', | ||
], | ||
], | ||
]; | ||
} | ||
} |