-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathPasswordTest.php
More file actions
284 lines (262 loc) · 9.15 KB
/
PasswordTest.php
File metadata and controls
284 lines (262 loc) · 9.15 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Tests\Unit\Service\IdentifyMethod;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Handler\DocMdpHandler;
use OCA\Libresign\Handler\FooterHandler;
use OCA\Libresign\Handler\SignEngine\Pkcs12Handler;
use OCA\Libresign\Service\CaIdentifierService;
use OCA\Libresign\Service\Crl\CrlService;
use OCA\Libresign\Service\FolderService;
use OCA\Libresign\Service\IdentifyMethod\IdentifyService;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\Password;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\ITempManager;
use OCP\IUserSession;
use OCP\L10N\IFactory as IL10NFactory;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
final class PasswordTest extends \OCA\Libresign\Tests\Unit\TestCase {
private IdentifyService&MockObject $identifyService;
private Pkcs12Handler&MockObject $pkcs12Handler;
private IUserSession&MockObject $userSession;
private IAppConfig $appConfig;
private FolderService&MockObject $folderService;
private CertificateEngineFactory&MockObject $certificateEngineFactory;
private IL10N $l10n;
private FooterHandler&MockObject $footerHandler;
private ITempManager $tempManager;
private LoggerInterface&MockObject $logger;
private CaIdentifierService&MockObject $caIdentifierService;
private DocMdpHandler&MockObject $docMdpHandler;
private CrlService&MockObject $crlService;
public function setUp(): void {
$this->identifyService = $this->createMock(IdentifyService::class);
$this->appConfig = $this->getMockAppConfigWithReset();
$this->folderService = $this->createMock(FolderService::class);
$this->certificateEngineFactory = $this->createMock(CertificateEngineFactory::class);
$this->l10n = \OCP\Server::get(IL10NFactory::class)->get(Application::APP_ID);
$this->footerHandler = $this->createMock(FooterHandler::class);
$this->tempManager = \OCP\Server::get(ITempManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->caIdentifierService = $this->createMock(CaIdentifierService::class);
$this->docMdpHandler = $this->createMock(DocMdpHandler::class);
$this->crlService = $this->createMock(CrlService::class);
$this->pkcs12Handler = $this->getPkcs12Instance();
}
private function getClass(): Password {
return new Password(
$this->identifyService,
$this->pkcs12Handler,
$this->userSession,
);
}
/**
* @return Pkcs12Handler&MockObject
*/
private function getPkcs12Instance(array $methods = []) {
return $this->getMockBuilder(Pkcs12Handler::class)
->setConstructorArgs([
$this->folderService,
$this->appConfig,
$this->certificateEngineFactory,
$this->l10n,
$this->footerHandler,
$this->tempManager,
$this->logger,
$this->caIdentifierService,
$this->docMdpHandler,
$this->crlService,
])
->onlyMethods($methods)
->getMock();
}
#[DataProvider('providerValidateToIdentify')]
public function testValidateToIdentify(string $pfx, bool $shouldThrow): void {
$this->pkcs12Handler = $this->getPkcs12Instance(['getPfxOfCurrentSigner']);
$this->pkcs12Handler->method('getPfxOfCurrentSigner')->willReturn($pfx);
$password = $this->getClass();
$password->setCodeSentByUser('senha');
if ($shouldThrow) {
$this->expectException(LibresignException::class);
$password->validateToIdentify();
} else {
$password->validateToIdentify();
$this->expectNotToPerformAssertions();
}
}
public static function providerValidateToIdentify(): array {
return [
'valid pfx' => ['mock-pfx', false],
'empty pfx' => ['', true],
];
}
#[DataProvider('providerValidateToSignWithError')]
public function testValidateToSignWithError(bool $throwsException, string $pfx): void {
$this->pkcs12Handler = $this->getPkcs12Instance(['getPfxOfCurrentSigner']);
$this->pkcs12Handler->method('getPfxOfCurrentSigner')->willReturn($pfx);
if ($throwsException) {
$this->expectException(LibresignException::class);
} else {
$this->expectNotToPerformAssertions();
}
$password = $this->getClass();
$password->setCodeSentByUser('senha');
$password->validateToSign();
}
public static function providerValidateToSignWithError(): array {
return [
'Invalid certificate' => [true, ''],
'throws InvalidPasswordException' => [true, 'mock-pfx'],
];
}
#[DataProvider('providerValidateToSignWithCertificateData')]
public function testValidateToSignWithCertificateData(array $certificateData, bool $shouldThrow, string $expectedMessage = ''): void {
$this->pkcs12Handler = $this->getPkcs12Instance(['getPfxOfCurrentSigner', 'setCertificate', 'setPassword', 'readCertificate']);
$this->pkcs12Handler->method('getPfxOfCurrentSigner')->willReturn('mock-pfx');
$this->pkcs12Handler->method('setCertificate')->willReturnSelf();
$this->pkcs12Handler->method('setPassword')->willReturnSelf();
$this->pkcs12Handler->method('readCertificate')->willReturn($certificateData);
$this->identifyService->method('getL10n')->willReturn($this->l10n);
$password = $this->getClass();
$password->setCodeSentByUser('senha');
if ($shouldThrow) {
$this->expectException(LibresignException::class);
if ($expectedMessage) {
$this->expectExceptionMessage($expectedMessage);
}
}
$password->validateToSign();
if (!$shouldThrow) {
$this->expectNotToPerformAssertions();
}
}
public static function providerValidateToSignWithCertificateData(): array {
$futureTimestamp = (new \DateTime('+50 years'))->getTimestamp();
$pastTimestamp = (new \DateTime('-50 years'))->getTimestamp();
return [
'valid certificate - no expiration data' => [
'certificateData' => [],
'shouldThrow' => false,
],
'valid certificate - future expiration' => [
'certificateData' => [
'validTo_time_t' => $futureTimestamp,
],
'shouldThrow' => false,
],
'expired certificate' => [
'certificateData' => [
'validTo_time_t' => $pastTimestamp,
],
'shouldThrow' => true,
'expectedMessage' => 'Certificate has expired',
],
'invalid certificate - validTo_time_t is string' => [
'certificateData' => [
'validTo_time_t' => '1234567890',
],
'shouldThrow' => true,
'expectedMessage' => 'Invalid certificate',
],
'invalid certificate - validTo_time_t is null' => [
'certificateData' => [
'validTo_time_t' => null,
],
'shouldThrow' => true,
'expectedMessage' => 'Invalid certificate',
],
'invalid certificate - validTo_time_t is float' => [
'certificateData' => [
'validTo_time_t' => 1234567890.5,
],
'shouldThrow' => true,
'expectedMessage' => 'Invalid certificate',
],
'invalid certificate - validTo_time_t is boolean true' => [
'certificateData' => [
'validTo_time_t' => true,
],
'shouldThrow' => true,
'expectedMessage' => 'Invalid certificate',
],
'invalid certificate - validTo_time_t is boolean false' => [
'certificateData' => [
'validTo_time_t' => false,
],
'shouldThrow' => true,
'expectedMessage' => 'Invalid certificate',
],
'invalid certificate - validTo_time_t is array' => [
'certificateData' => [
'validTo_time_t' => ['timestamp' => 1234567890],
],
'shouldThrow' => true,
'expectedMessage' => 'Invalid certificate',
],
'revoked certificate' => [
'certificateData' => [
'validTo_time_t' => $futureTimestamp,
'crl_validation' => 'revoked',
],
'shouldThrow' => true,
'expectedMessage' => 'Certificate has been revoked',
],
'valid certificate with crl validation' => [
'certificateData' => [
'validTo_time_t' => $futureTimestamp,
'crl_validation' => 'valid',
],
'shouldThrow' => false,
],
'invalid certificate - crl validation failed' => [
'certificateData' => [
'validTo_time_t' => $futureTimestamp,
'crl_validation' => 'failed',
],
'shouldThrow' => true,
'expectedMessage' => 'Certificate has been revoked',
],
'invalid certificate - crl validation empty string' => [
'certificateData' => [
'validTo_time_t' => $futureTimestamp,
'crl_validation' => '',
],
'shouldThrow' => true,
'expectedMessage' => 'Certificate has been revoked',
],
'invalid certificate - crl validation null' => [
'certificateData' => [
'validTo_time_t' => $futureTimestamp,
'crl_validation' => null,
],
'shouldThrow' => true,
'expectedMessage' => 'Certificate has been revoked',
],
'revoked and expired certificate' => [
'certificateData' => [
'validTo_time_t' => $pastTimestamp,
'crl_validation' => 'revoked',
],
'shouldThrow' => true,
'expectedMessage' => 'Certificate has been revoked', // revocation is checked first
],
'valid certificate - old date but valid (1970s timestamp)' => [
'certificateData' => [
'validTo_time_t' => 31536000, // 1971-01-01
],
'shouldThrow' => true,
'expectedMessage' => 'Certificate has expired',
],
];
}
}