Skip to content

Commit 3009f9f

Browse files
authored
Merge pull request #6198 from LibreSign/backport/6197/stable32
[stable32] refactor: consolidate pdf fixtures
2 parents 660ba46 + ff9c387 commit 3009f9f

25 files changed

Lines changed: 687 additions & 316 deletions

REUSE.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ path = [
5050
"src/types/openapi/openapi.ts",
5151
"tests/php/Unit/Handler/mock/cert.json",
5252
"tests/php/fixtures/cfssl/newcert-with-success.json",
53-
"tests/php/fixtures/real_jsignpdf_level1.pdf",
54-
"tests/php/fixtures/small_valid-signed.pdf",
55-
"tests/php/fixtures/small_valid.pdf",
53+
"tests/php/fixtures/pdfs/real_jsignpdf_level1.pdf",
54+
"tests/php/fixtures/pdfs/small_valid-signed.pdf",
55+
"tests/php/fixtures/pdfs/small_valid.pdf",
5656
"tests/integration/composer.json",
5757
"tests/integration/composer.lock",
5858
"tests/integration/features/**/*.feature",

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
},
5454
"autoload-dev": {
5555
"psr-4": {
56-
"OCP\\": "vendor/nextcloud/ocp/OCP"
56+
"OCP\\": "vendor/nextcloud/ocp/OCP",
57+
"OCA\\Libresign\\Tests\\Unit\\": "tests/php/Unit/",
58+
"OCA\\Libresign\\Tests\\Fixtures\\": "tests/php/fixtures/"
5759
}
5860
},
5961
"require": {

lib/Controller/DevelopController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function pdf(): FileDisplayResponse|Response {
4848
if (!$this->isDebugMode()) {
4949
return new DataResponse([], Http::STATUS_NOT_FOUND);
5050
}
51-
$file = new InMemoryFile('file.pdf', file_get_contents(__DIR__ . '/../../tests/php/fixtures/small_valid.pdf'));
51+
$file = new InMemoryFile('file.pdf', file_get_contents(__DIR__ . '/../../tests/php/fixtures/pdfs/small_valid.pdf'));
5252
$response = new FileDisplayResponse($file);
5353
$response->setHeaders([
5454
'Content-Disposition' => 'inline; filename="file.pdf"',

lib/Handler/SignEngine/Pkcs12Handler.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,23 @@ public function __construct(
5252
private function getSignatures($resource): iterable {
5353
rewind($resource);
5454
$content = stream_get_contents($resource);
55-
preg_match_all(
56-
'/ByteRange\s*\[\s*(?<offset1>\d+)\s+(?<length1>\d+)\s+(?<offset2>\d+)\s+(?<length2>\d+)\s*\]/',
57-
$content,
58-
$bytes
59-
);
60-
if (empty($bytes['offset1']) || empty($bytes['length1']) || empty($bytes['offset2']) || empty($bytes['length2'])) {
61-
throw new LibresignException($this->l10n->t('Unsigned file.'));
62-
}
6355

64-
for ($i = 0; $i < count($bytes['offset1']); $i++) {
65-
$offset1 = (int)$bytes['offset1'][$i];
66-
$length1 = (int)$bytes['length1'][$i];
67-
$offset2 = (int)$bytes['offset2'][$i];
56+
preg_match_all('/\/Contents\s*<([0-9a-fA-F]+)>/', $content, $contents, PREG_OFFSET_CAPTURE);
6857

69-
$signatureStart = $offset1 + $length1 + 1;
70-
$signatureLength = $offset2 - $signatureStart - 1;
58+
if (empty($contents[1])) {
59+
throw new LibresignException($this->l10n->t('Unsigned file.'));
60+
}
7161

72-
rewind($resource);
62+
$seenHexSignatures = [];
63+
foreach ($contents[1] as $match) {
64+
$signatureHex = $match[0];
7365

74-
$signature = stream_get_contents($resource, $signatureLength, $signatureStart);
75-
if ($signature === false) {
76-
yield null;
66+
if (isset($seenHexSignatures[$signatureHex])) {
7767
continue;
7868
}
69+
$seenHexSignatures[$signatureHex] = true;
7970

80-
$decodedSignature = @hex2bin($signature);
71+
$decodedSignature = @hex2bin($signatureHex);
8172
if ($decodedSignature === false) {
8273
yield null;
8374
continue;
@@ -101,7 +92,17 @@ public function getCertificateChain($resource): array {
10192
$certificates = [];
10293

10394
foreach ($this->getSignatures($resource) as $signature) {
104-
$certificates[] = $this->processSignature($resource, $signature);
95+
if (!$signature) {
96+
continue;
97+
}
98+
99+
$result = $this->processSignature($resource, $signature);
100+
101+
if (empty($result['chain'])) {
102+
continue;
103+
}
104+
105+
$certificates[] = $result;
105106
}
106107
return $certificates;
107108
}

tests/php/Api/Controller/FileControllerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testValidateWithSuccessUsingUnloggedUser():void {
5555

5656
$user->setEMailAddress('person@test.coop');
5757
$file = $this->requestSignFile([
58-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
58+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
5959
'name' => 'test',
6060
'users' => [
6161
[
@@ -93,7 +93,7 @@ public function testValidateWithSuccessUsingSigner():void {
9393

9494
$user->setEMailAddress('person@test.coop');
9595
$file = $this->requestSignFile([
96-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
96+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
9797
'name' => 'test',
9898
'users' => [
9999
[
@@ -149,7 +149,7 @@ public function testSendNewFile():void {
149149
->withMethod('POST')
150150
->withRequestBody([
151151
'name' => 'test',
152-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
152+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
153153
]);
154154

155155
$this->assertRequest();

tests/php/Api/Controller/FileElementControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testPostSuccess():array {
2323
$user = $this->createAccount('username', 'password');
2424
$user->setEMailAddress('person@test.coop');
2525
$file = $this->requestSignFile([
26-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
26+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
2727
'name' => 'test',
2828
'users' => [
2929
[

tests/php/Api/Controller/IdDocsControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testPostIdDocsWithSuccess():void {
6060
[
6161
'type' => 'IDENTIFICATION',
6262
'file' => [
63-
'base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))
63+
'base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))
6464
]
6565
]
6666
]

tests/php/Api/Controller/NotifyControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testNotifySignersWithSuccess():void {
4949
$appConfig->setValueArray(Application::APP_ID, 'groups_request_sign', ['admin','testGroup']);
5050
$appConfig->setValueBool(Application::APP_ID, 'notifyUnsignedUser', true);
5151
$file = $this->requestSignFile([
52-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
52+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
5353
'name' => 'test',
5454
'users' => [
5555
[

tests/php/Api/Controller/RequestSignatureControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testPostRegisterWithSuccess():void {
6161
->withRequestBody([
6262
'name' => 'filename',
6363
'file' => [
64-
'base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))
64+
'base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))
6565
],
6666
'users' => [
6767
[
@@ -114,7 +114,7 @@ public function testPatchRegisterWithSuccess():void {
114114

115115
$user->setEMailAddress('person@test.coop');
116116
$file = $this->requestSignFile([
117-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
117+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
118118
'name' => 'test',
119119
'users' => [
120120
[

tests/php/Api/Controller/SignFileControllerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testSignUsingFileIdWithAlreadySignedFile():void {
7878

7979
$user->setEMailAddress('person@test.coop');
8080
$file = $this->requestSignFile([
81-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
81+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
8282
'name' => 'test',
8383
'users' => [
8484
[
@@ -122,7 +122,7 @@ public function testSignUsingFileIdWithNotFoundFile():void {
122122

123123
$user->setEMailAddress('person@test.coop');
124124
$file = $this->requestSignFile([
125-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
125+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
126126
'name' => 'test',
127127
'users' => [
128128
[
@@ -166,7 +166,7 @@ public function testSignUsingUuidWithEmptyToken():void {
166166

167167
$user->setEMailAddress('person@test.coop');
168168
$file = $this->requestSignFile([
169-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
169+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
170170
'name' => 'test',
171171
'users' => [
172172
[
@@ -203,7 +203,7 @@ public function testSignWithCertificateButEmptyPassword():void {
203203

204204
$user->setEMailAddress('person@test.coop');
205205
$file = $this->requestSignFile([
206-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
206+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
207207
'name' => 'test',
208208
'users' => [
209209
[
@@ -259,7 +259,7 @@ public function testAccountSignatureEndpointWithFailure():void {
259259
public function testDeleteSignFileIdSignRequestIdWithSuccess():void {
260260
$user = $this->createAccount('allowrequestsign', 'password', 'testGroup');
261261
$file = $this->requestSignFile([
262-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
262+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
263263
'name' => 'test',
264264
'users' => [
265265
[
@@ -307,7 +307,7 @@ public function testDeleteSignFileIdSignRequestIdWithError():void {
307307
public function testDeleteUsingSignFileIdWithSuccess():void {
308308
$user = $this->createAccount('allowrequestsign', 'password', 'testGroup');
309309
$file = $this->requestSignFile([
310-
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
310+
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
311311
'name' => 'test',
312312
'users' => [
313313
[

0 commit comments

Comments
 (0)