Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Mar 20, 2024
1 parent aa82e99 commit 182a970
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions tests/Unit/Handler/OpenSslHandlerTest.php
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',
],
],
];
}
}

0 comments on commit 182a970

Please sign in to comment.