-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathPkcs7HandlerTest.php
More file actions
75 lines (62 loc) · 2.47 KB
/
Pkcs7HandlerTest.php
File metadata and controls
75 lines (62 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace OCA\Libresign\Tests\Unit\Handler\SignEngine;
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
use OCA\Libresign\Handler\SignEngine\Pkcs7Handler;
use OCA\Libresign\Service\FolderService;
use OCP\IL10N;
use OCP\L10N\IFactory as IL10NFactory;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
final class Pkcs7HandlerTest extends \OCA\Libresign\Tests\Unit\TestCase {
private IL10N $l10n;
private FolderService&MockObject $folderService;
private LoggerInterface&MockObject $logger;
public function setUp(): void {
parent::setUp();
$this->l10n = \OCP\Server::get(IL10NFactory::class)->get(\OCA\Libresign\AppInfo\Application::APP_ID);
$this->folderService = $this->createMock(\OCA\Libresign\Service\FolderService::class);
$this->logger = $this->createMock(LoggerInterface::class);
}
protected function getInstance(array $methods = []): Pkcs7Handler|MockObject {
if (empty($methods)) {
return new Pkcs7Handler(
$this->l10n,
$this->folderService,
$this->logger,
);
}
return $this->getMockBuilder(Pkcs7Handler::class)
->setConstructorArgs([
$this->l10n,
$this->folderService,
$this->logger,
])
->onlyMethods($methods)
->getMock();
}
public function testSignWithSuccess():void {
$p7sRealFile = tempnam(sys_get_temp_dir(), 'p7s');
$p7sFile = $this->createMock(\OCP\Files\File::class);
$p7sFile->method('getInternalPath')->willReturn($p7sRealFile);
$fileToSignRealFile = tempnam(sys_get_temp_dir(), 'txt');
$content = 'A simple test';
file_put_contents($fileToSignRealFile, $content);
$fileToSign = $this->createMock(\OCP\Files\File::class);
$fileToSign->method('getInternalPath')->willReturn($fileToSignRealFile);
$handler = $this->getInstance(['getP7sFile', 'getInputFile']);
$handler->method('getP7sFile')->willReturn($p7sFile);
$handler->method('getInputFile')->willReturn($fileToSign);
$certKeys = json_decode(file_get_contents(__DIR__ . '/../../../fixtures/cfssl/newcert-with-success.json'), true);
$certKeys = $certKeys['result'];
openssl_pkcs12_export($certKeys['certificate'], $certContent, $certKeys['private_key'], 'password');
$handler->setCertificate($certContent);
$handler->setPassword('password');
$handler->sign();
$this->assertStringContainsString($content, file_get_contents($p7sRealFile));
$this->assertGreaterThan($content, file_get_contents($p7sRealFile));
}
}