Skip to content

Commit

Permalink
fix: unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Jun 3, 2024
1 parent 27806da commit b36b013
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
8 changes: 7 additions & 1 deletion lib/Service/Install/SignFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class SignFiles {
'unauthetnicated',
];
private string $architecture;
private string $appInfoDir;
public function __construct(
private FileAccessHelper $fileAccessHelper,
private IConfig $config,
Expand Down Expand Up @@ -73,9 +74,14 @@ public function writeAppSignature(
X509 $certificate,
RSA $privateKey,
string $architecture,
string $appInfoDir = '',
) {
$this->architecture = $architecture;
$appInfoDir = __DIR__ . '/../../../appinfo';
if (is_dir($appInfoDir)) {
$this->appInfoDir = $appInfoDir;
} else {
$appInfoDir = realpath(__DIR__ . '/../../../appinfo');
}
try {
$this->fileAccessHelper->assertDirectoryExists($appInfoDir);

Expand Down
30 changes: 23 additions & 7 deletions tests/Unit/Service/Install/SignFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
use PHPUnit\Framework\MockObject\MockObject;

final class SignFilesTest extends \OCA\Libresign\Tests\Unit\TestCase {
private FileAccessHelper&MockObject $fileAccessHelper;
private FileAccessHelper $fileAccessHelper;
private IConfig&MockObject $config;
private IAppDataFactory&MockObject $appDataFactory;
private IAppManager&MockObject $appManager;

public function setUp(): void {
$this->fileAccessHelper = $this->createMock(FileAccessHelper::class);
$this->fileAccessHelper = new FileAccessHelper();
$this->config = $this->createMock(IConfig::class);
$this->appDataFactory = $this->createMock(IAppDataFactory::class);
$this->appManager = $this->createMock(IAppManager::class);
Expand Down Expand Up @@ -58,7 +58,7 @@ private function getNewCert(): array {
$x509 = openssl_csr_sign($csr, null, $privateKey, $days = 365, ['digest_alg' => 'sha256']);

openssl_x509_export($x509, $rootCertificate);
openssl_pkey_export($privateKey, $rootPrivateKey);
openssl_pkey_export($privateKey, $publicKey);

$privateKey = openssl_pkey_new([
'private_key_bits' => 2048,
Expand All @@ -67,6 +67,7 @@ private function getNewCert(): array {
return [
'privateKey' => $privateKey,
'certificate' => $rootCertificate,
'publicKey' => $publicKey,
];
}

Expand Down Expand Up @@ -105,12 +106,20 @@ public function testWriteAppSignature(string $architecture): void {
$certificate = $this->getNewCert('123456');
$rsa = new RSA();
$rsa->loadKey($certificate['privateKey']);
$rsa->loadKey($certificate['publicKey']);
$x509 = new X509();
$x509->loadX509($certificate['certificate']);
$x509->setPrivateKey($rsa);


vfsStream::setup('home');
$structure = [
'data' => [
'libresign' => [
'fakeFile' => 'content',
],
],
'appinfo' => [],
];
$root = vfsStream::setup('home', 0755, $structure);

$this->config->method('getSystemValue')
->willReturn(vfsStream::url('home/data'));
Expand All @@ -119,8 +128,15 @@ public function testWriteAppSignature(string $architecture): void {
$signFiles->expects($this->any())
->method('getInternalPathOfFolder')
->willReturn('libresign');
mkdir('vfs://home/data/libresign', 0755, true);
$signFiles->writeAppSignature($x509, $rsa, $architecture);
$signFiles->writeAppSignature($x509, $rsa, $architecture, 'vfs://home/appinfo');
$this->assertFileExists('vfs://home/appinfo/install-' . $architecture . '.json');
$json = file_get_contents('vfs://home/appinfo/install-' . $architecture . '.json');
$signatureContent = json_decode($json, true);
$this->assertArrayHasKey('hashes', $signatureContent);
$this->assertCount(1, $signatureContent['hashes']);
$expected = hash('sha512', $structure['data']['libresign']['fakeFile']);
$actual = $signatureContent['hashes']['fakeFile'];
$this->assertEquals($expected, $actual);
}

public static function dataWriteAppSignature(): array {
Expand Down

0 comments on commit b36b013

Please sign in to comment.