Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable28] Implement tests #2562

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions lib/Handler/CertificateEngine/AEngineHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
* @method string getLocality()
* @method IEngineHandler setOrganization(string $organization)
* @method string getOrganization()
* @method IEngineHandler setOrganizationUnit(string $organizationUnit)
* @method string getOrganizationUnit()
* @method IEngineHandler setOrganizationalUnit(string $organizationalUnit)
* @method string getOrganizationalUnit()
* @method string getName()
*/
class AEngineHandler {
Expand All @@ -69,7 +69,7 @@ class AEngineHandler {
protected string $state = '';
protected string $locality = '';
protected string $organization = '';
protected string $organizationUnit = '';
protected string $organizationalUnit = '';
protected string $password = '';
protected string $configPath = '';
protected string $engine = '';
Expand Down Expand Up @@ -135,9 +135,6 @@ public function readCertificate(string $certificate, string $privateKey): array

$return['name'] = $parsed['name'];
$return['subject'] = $parsed['subject'];
if (is_array($return['subject']['OU']) && !empty($return['subject']['OU'])) {
$return['subject']['OU'] = implode(', ', $return['subject']['OU']);
}
$return['issuer'] = $parsed['issuer'];
$return['extensions'] = $parsed['extensions'];
$return['validate'] = [
Expand All @@ -160,7 +157,7 @@ public function translateToLong($name): string {
case 'O':
return 'Organization';
case 'OU':
return 'OrganizationUnit';
return 'OrganizationalUnit';
}
return '';
}
Expand Down Expand Up @@ -259,7 +256,7 @@ protected function getNames(): array {
'ST' => $this->getState(),
'L' => $this->getLocality(),
'O' => $this->getOrganization(),
'OU' => $this->getOrganizationUnit(),
'OU' => $this->getOrganizationalUnit(),
];
$names = array_filter($names, function ($v) {
return !empty($v);
Expand Down
4 changes: 2 additions & 2 deletions lib/Handler/CertificateEngine/IEngineHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
* @method string getLocality()
* @method IEngineHandler setOrganization(string $organization)
* @method string getOrganization()
* @method IEngineHandler setOrganizationUnit(string $organizationUnit)
* @method string getOrganizationUnit()
* @method IEngineHandler setOrganizationalUnit(string $organizationalUnit)
* @method string getOrganizationalUnit()
* @method string getName()
*/
interface IEngineHandler {
Expand Down
6 changes: 3 additions & 3 deletions lib/Migration/Version7000Date20221026003343.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array
$rootCert['names']['O'] = $organization;
$this->appConfig->deleteAppValue('organization');
}
if ($organizationUnit = $this->appConfig->getAppValue('organizationUnit')) {
$rootCert['names']['OU'] = $organizationUnit;
$this->appConfig->deleteAppValue('organizationUnit');
if ($organizationalUnit = $this->appConfig->getAppValue('organizationalUnit')) {
$rootCert['names']['OU'] = $organizationalUnit;
$this->appConfig->deleteAppValue('organizationalUnit');
}
if ($rootCert) {
$this->appConfig->setAppValue('rootCert', json_encode($rootCert));
Expand Down
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',
],
],
];
}
}
95 changes: 55 additions & 40 deletions tests/Unit/Service/InstallServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,63 +74,78 @@ protected function getInstallService(): InstallService {
/**
* @dataProvider providerDownloadCli
*/
public function testDownloadCli(string $url, string $filename, string $path, string $hash, string $algorithm, string $expectedOutput): void {
public function testDownloadCli(string $url, string $filename, string $content, string $hash, string $algorithm, string $expectedOutput): void {
$installService = $this->getInstallService();
$output = new BufferedOutput();
$installService->setOutput($output);

if ($content) {
vfsStream::setup('download');
$path = 'vfs://download/dummy.svg';
file_put_contents($path, $content);
} else {
$path = '';
}

self::invokePrivate($installService, 'downloadCli', [$url, $filename, $path, $hash, $algorithm]);
$actual = $output->fetch();
$this->assertEquals($expectedOutput, $actual);
}

public function providerDownloadCli(): array {
vfsStream::setup('download');

$pathInvalid = 'vfs://download/appInvalid.svg';
file_put_contents($pathInvalid, 'invalidContent');
$pathValid = 'vfs://download/validContent.svg';
file_put_contents($pathValid, 'invalidContent');
return [
[
"http://localhost/apps/libresign/img/app.svg",
'app.svg',
'vfs://download/app.svg',
'',
'md5',
"Downloading app.svg...\n" .
" 0 [>---------------------------]\n".
"Failure on download app.svg, empty file, try again\n",
'url' => 'http://localhost/apps/libresign/img/app.svg',
'filename' => 'app.svg',
'content' => '',
'hash' => '',
'algorithm' => 'md5',
'expectedOutput' => <<<EXPECTEDOUTPUT
Downloading app.svg...
0 [>---------------------------]
Failure on download app.svg, empty file, try again

EXPECTEDOUTPUT
],
[
"http://localhost/apps/libresign/img/appInvalid.svg",
'appInvalid.svg',
$pathInvalid,
'hashInvalid',
'md5',
"Downloading appInvalid.svg...\n" .
" 0 [>---------------------------]\n" .
"Failure on download appInvalid.svg try again\n" .
"Invalid md5\n",
'url' => 'http://localhost/apps/libresign/img/appInvalid.svg',
'filename' => 'appInvalid.svg',
'content' => 'content',
'hash' => 'invalidContent',
'algorithm' => 'md5',
'expectedOutput' => <<<EXPECTEDOUTPUT
Downloading appInvalid.svg...
0 [>---------------------------]
Failure on download appInvalid.svg try again
Invalid md5

EXPECTEDOUTPUT
],
[
"http://localhost/apps/libresign/img/appInvalid.svg",
'appInvalid.svg',
$pathInvalid,
'hashInvalid',
'sha256',
"Downloading appInvalid.svg...\n" .
" 0 [>---------------------------]\n" .
"Failure on download appInvalid.svg try again\n" .
"Invalid sha256\n",
'url' => 'http://localhost/apps/libresign/img/appInvalid.svg',
'filename' => 'appInvalid.svg',
'content' => 'content',
'hash' => 'invalidContent',
'algorithm' => 'sha256',
'expectedOutput' => <<<EXPECTEDOUTPUT
Downloading appInvalid.svg...
0 [>---------------------------]
Failure on download appInvalid.svg try again
Invalid sha256

EXPECTEDOUTPUT
],
[
"http://localhost/apps/libresign/img/validContent.svg",
'validContent.svg',
$pathValid,
hash_file('sha256', $pathValid),
'sha256',
"Downloading validContent.svg...\n" .
" 0 [>---------------------------]\n",
'url' => 'http://localhost/apps/libresign/img/validContent.svg',
'filename' => 'validContent.svg',
'content' => 'content',
'hash' => hash('sha256', 'content'),
'algorithm' => 'sha256',
'expectedOutput' => <<<EXPECTEDOUTPUT
Downloading validContent.svg...
0 [>---------------------------]

EXPECTEDOUTPUT
],
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function requestSignFile($data): File {
'commonName' => 'CommonName',
'country' => 'Brazil',
'organization' => 'Organization',
'organizationUnit' => 'organizationUnit',
'organizationalUnit' => 'organizationalUnit',
'cfsslUri' => self::$server->getServerRoot() . '/api/v1/cfssl/'
]);

Expand Down
24 changes: 12 additions & 12 deletions tests/integration/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading