-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathAEngineHandlerTest.php
More file actions
409 lines (361 loc) · 12.4 KB
/
AEngineHandlerTest.php
File metadata and controls
409 lines (361 loc) · 12.4 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Handler\CertificateEngine\OpenSslHandler;
use OCA\Libresign\Service\CaIdentifierService;
use OCA\Libresign\Service\CertificatePolicyService;
use OCP\Files\AppData\IAppDataFactory;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\ITempManager;
use OCP\IURLGenerator;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Log\LoggerInterface;
final class AEngineHandlerTest extends \OCA\Libresign\Tests\Unit\TestCase {
private IConfig $config;
private IAppConfig $appConfig;
private IAppDataFactory $appDataFactory;
private IDateTimeFormatter $dateTimeFormatter;
private ITempManager $tempManager;
private CertificatePolicyService $certificatePolicyService;
private IURLGenerator $urlGenerator;
private CaIdentifierService $caIdentifierService;
private LoggerInterface $logger;
public function setUp(): void {
$this->config = \OCP\Server::get(IConfig::class);
$this->appConfig = $this->getMockAppConfigWithReset();
$this->appDataFactory = \OCP\Server::get(IAppDataFactory::class);
$this->dateTimeFormatter = \OCP\Server::get(IDateTimeFormatter::class);
$this->tempManager = \OCP\Server::get(ITempManager::class);
$this->certificatePolicyService = \OCP\Server::get(CertificatePolicyService::class);
$this->urlGenerator = \OCP\Server::get(IURLGenerator::class);
$this->caIdentifierService = \OCP\Server::get(CaIdentifierService::class);
$this->logger = \OCP\Server::get(LoggerInterface::class);
}
private function getInstance(): OpenSslHandler {
return new OpenSslHandler(
$this->config,
$this->appConfig,
$this->appDataFactory,
$this->dateTimeFormatter,
$this->tempManager,
$this->certificatePolicyService,
$this->urlGenerator,
\OCP\Server::get(\OCA\Libresign\Service\SerialNumberService::class),
$this->caIdentifierService,
$this->logger,
\OCP\Server::get(\OCA\Libresign\Db\CrlMapper::class),
);
}
#[DataProvider('dataProviderEngines')]
public function testSetEngineSavesCertificateEngineConfig(string $engine): void {
$instance = $this->getInstance();
$instance->setEngine($engine);
$savedEngine = $this->appConfig->getValueString(Application::APP_ID, 'certificate_engine');
$this->assertEquals($engine, $savedEngine);
}
#[DataProvider('dataProviderEngines')]
public function testSetEngineUpdatesInternalProperty(string $engine): void {
$instance = $this->getInstance();
$instance->setEngine($engine);
$this->assertEquals($engine, $instance->getEngine());
}
#[DataProvider('dataProviderIdentifyMethodsNoneEngine')]
public function testSetEngineConfiguresIdentifyMethodsForNoneEngine(
string $fromEngine,
?array $initialIdentifyMethods,
string $description,
): void {
$instance = $this->getInstance();
if ($fromEngine !== '') {
$this->appConfig->setValueString(Application::APP_ID, 'certificate_engine', $fromEngine);
}
if ($initialIdentifyMethods !== null) {
$this->appConfig->setValueArray(Application::APP_ID, 'identify_methods', $initialIdentifyMethods);
}
$instance->setEngine('none');
$savedIdentifyMethods = $this->appConfig->getValueArray(Application::APP_ID, 'identify_methods');
$expected = [[
'name' => 'account',
'enabled' => true,
'mandatory' => true,
]];
$this->assertEquals(
$expected,
$savedIdentifyMethods,
"identify_methods should be restricted for none engine: $description"
);
}
#[DataProvider('dataProviderIdentifyMethodsOtherEngines')]
public function testSetEnginePreservesIdentifyMethodsForOtherEngines(
string $fromEngine,
string $toEngine,
?array $initialIdentifyMethods,
string $description,
): void {
$instance = $this->getInstance();
if ($fromEngine !== '') {
$this->appConfig->setValueString(Application::APP_ID, 'certificate_engine', $fromEngine);
}
if ($initialIdentifyMethods !== null) {
$this->appConfig->setValueArray(Application::APP_ID, 'identify_methods', $initialIdentifyMethods);
}
$instance->setEngine($toEngine);
$savedIdentifyMethods = $this->appConfig->getValueArray(Application::APP_ID, 'identify_methods');
$this->assertEquals(
$initialIdentifyMethods ?? [],
$savedIdentifyMethods,
"identify_methods should not be modified: $description"
);
}
public static function dataProviderEngines(): array {
return [
'openssl engine' => ['openssl'],
'cfssl engine' => ['cfssl'],
'none engine' => ['none'],
];
}
public static function dataProviderIdentifyMethodsNoneEngine(): array {
$noneConfig = [[
'name' => 'account',
'enabled' => true,
'mandatory' => true,
'signatureMethods' => [
'clickToSign' => ['enabled' => false, 'name' => 'clickToSign'],
'emailToken' => ['enabled' => false, 'name' => 'emailToken'],
'password' => ['enabled' => true, 'name' => 'password'],
],
]];
$fullConfig = [
[
'name' => 'account',
'enabled' => true,
'mandatory' => false,
'signatureMethods' => [
'clickToSign' => ['enabled' => true, 'name' => 'clickToSign'],
'emailToken' => ['enabled' => true, 'name' => 'emailToken'],
'password' => ['enabled' => true, 'name' => 'password'],
],
],
[
'name' => 'email',
'enabled' => true,
'mandatory' => false,
'signatureMethods' => [
'clickToSign' => ['enabled' => true, 'name' => 'clickToSign'],
'emailToken' => ['enabled' => true, 'name' => 'emailToken'],
'password' => ['enabled' => true, 'name' => 'password'],
],
],
];
return [
'First time setting to none' => ['', null, 'no previous config'],
'From openssl to none' => ['openssl', $fullConfig, 'with full config'],
'From cfssl to none' => ['cfssl', $fullConfig, 'with full config'],
'Keeping none' => ['none', $noneConfig, 'already restricted'],
];
}
public static function dataProviderIdentifyMethodsOtherEngines(): array {
$noneConfig = [[
'name' => 'account',
'enabled' => true,
'mandatory' => true,
'signatureMethods' => [
'clickToSign' => ['enabled' => false, 'name' => 'clickToSign'],
'emailToken' => ['enabled' => false, 'name' => 'emailToken'],
'password' => ['enabled' => true, 'name' => 'password'],
],
]];
$fullConfig = [
[
'name' => 'account',
'enabled' => true,
'mandatory' => false,
'signatureMethods' => [
'clickToSign' => ['enabled' => true],
'emailToken' => ['enabled' => true],
'password' => ['enabled' => true],
],
],
[
'name' => 'email',
'enabled' => true,
'mandatory' => false,
'signatureMethods' => [
'clickToSign' => ['enabled' => true],
'emailToken' => ['enabled' => true],
'password' => ['enabled' => true],
],
],
];
return [
'From none to openssl' => ['none', 'openssl', $noneConfig, 'should preserve restricted config'],
'From none to cfssl' => ['none', 'cfssl', $noneConfig, 'should preserve restricted config'],
'From openssl to cfssl' => ['openssl', 'cfssl', $fullConfig, 'should preserve full config'],
'From cfssl to openssl' => ['cfssl', 'openssl', $fullConfig, 'should preserve full config'],
'First time openssl' => ['', 'openssl', null, 'no previous config'],
'First time cfssl' => ['', 'cfssl', null, 'no previous config'],
];
}
#[DataProvider('dataProviderToArray')]
public function testToArrayReturnsExpectedStructure(
bool $isSetupOk,
array $certificateData,
array $expectedKeys,
string $description,
): void {
$instance = $this->getInstance();
foreach ($certificateData as $setter => $value) {
$method = 'set' . ucfirst($setter);
if (method_exists($instance, $method)) {
$instance->$method($value);
}
}
if (!$isSetupOk) {
$this->appConfig->deleteKey(Application::APP_ID, 'config_path');
}
$result = $instance->toArray();
foreach ($expectedKeys as $key) {
$this->assertArrayHasKey($key, $result, "toArray() should contain '$key': $description");
}
$this->assertIsBool($result['generated'], "generated should be boolean: $description");
}
#[DataProvider('dataProviderToArrayConfigPath')]
public function testToArrayFiltersConfigPathWhenNotGenerated(
bool $certificateGenerated,
string $expectedConfigPath,
string $description,
): void {
$instance = $this->getInstance();
$tempPath = $this->tempManager->getTemporaryFolder('test-config');
$instance->setConfigPath($tempPath);
if ($certificateGenerated) {
file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'ca.pem', 'fake-cert');
file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'ca-key.pem', 'fake-key');
}
$result = $instance->toArray();
if ($expectedConfigPath === '') {
$this->assertEmpty($result['configPath'], "configPath should be empty: $description");
} else {
$this->assertNotEmpty($result['configPath'], "configPath should not be empty: $description");
}
$this->assertEquals($certificateGenerated, $result['generated'], "generated flag: $description");
}
#[DataProvider('dataProviderToArrayCaIdFiltering')]
public function testToArrayFiltersCaIdFromOrganizationalUnitWhenNotGenerated(
bool $certificateGenerated,
array $organizationalUnits,
array $expectedOuValues,
string $description,
): void {
$instance = $this->getInstance();
$tempPath = $this->tempManager->getTemporaryFolder('test-config');
$instance->setConfigPath($tempPath);
$instance->setOrganizationalUnit($organizationalUnits);
$instance->setCountry('BR');
if ($certificateGenerated) {
file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'ca.pem', 'fake-cert');
file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'ca-key.pem', 'fake-key');
}
$result = $instance->toArray();
$ouFound = null;
foreach ($result['rootCert']['names'] as $name) {
if ($name['id'] === 'OU') {
$ouFound = $name['value'];
break;
}
}
if (!empty($expectedOuValues)) {
$this->assertNotNull($ouFound, "OU should be present in names array: $description");
$this->assertEquals(
$expectedOuValues,
$ouFound,
"OrganizationalUnit filtering: $description"
);
} else {
$this->assertNull($ouFound, "OU should not be present when filtered to empty: $description");
}
}
public static function dataProviderToArray(): array {
return [
'Basic structure with minimal data' => [
false,
['commonName' => 'Test CA'],
['configPath', 'generated', 'rootCert', 'policySection'],
'minimal certificate data',
],
'Complete certificate data' => [
false,
[
'commonName' => 'LibreSign CA',
'country' => 'BR',
'state' => 'Santa Catarina',
'locality' => 'Florianópolis',
'organization' => 'LibreCode',
'organizationalUnit' => ['Engineering', 'Security'],
],
['configPath', 'generated', 'rootCert', 'policySection'],
'full certificate data',
],
];
}
public static function dataProviderToArrayConfigPath(): array {
return [
'Certificate not generated' => [
false,
'',
'configPath should be empty when certificate not generated',
],
'Certificate generated' => [
true,
'/path/to/config',
'configPath should be set when certificate is generated',
],
];
}
public static function dataProviderToArrayCaIdFiltering(): array {
return [
'OU without CA ID - not generated' => [
false,
['Engineering', 'Security'],
['Engineering', 'Security'],
'normal OU values should pass through when not generated',
],
'OU without CA ID - generated' => [
true,
['Engineering', 'Security'],
['Engineering', 'Security'],
'normal OU values should pass through when generated',
],
'OU with CA ID - not generated (filtered)' => [
false,
['libresign-ca-id:abc123_g:1_e:openssl', 'Engineering', 'Security'],
['Engineering', 'Security'],
'CA ID should be filtered when certificate not generated',
],
'OU with CA ID - generated (kept)' => [
true,
['libresign-ca-id:abc123_g:1_e:openssl', 'Engineering', 'Security'],
['libresign-ca-id:abc123_g:1_e:openssl', 'Engineering', 'Security'],
'CA ID should be kept when certificate is generated',
],
'OU with only CA ID - not generated' => [
false,
['libresign-ca-id:abc123_g:1_e:openssl'],
[],
'OU should be empty when only CA ID and not generated',
],
'OU with only CA ID - generated' => [
true,
['libresign-ca-id:abc123_g:1_e:openssl'],
['libresign-ca-id:abc123_g:1_e:openssl'],
'OU with only CA ID should be kept when generated',
],
];
}
}