Initial commit

This commit is contained in:
2026-08-30 22:02:02 +00:00
commit b6bd2277f5
2334 changed files with 646393 additions and 0 deletions
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
/**
* DocTimeStampTest.php
*
* @since 2026-07-15
* @category Library
* @package PdfSign
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf-sign
*
* This file is part of tc-lib-pdf-sign software library.
*/
namespace Test\Output;
use Com\Tecnick\Pdf\Sign\Output\DocTimeStamp;
use Com\Tecnick\Pdf\Sign\Output\Signature;
use PHPUnit\Framework\TestCase;
/**
* DocTimeStamp Output Test
*
* @since 2026-07-15
* @category Library
* @package PdfSign
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf-sign
*/
class DocTimeStampTest extends TestCase
{
private DocTimeStamp $docTimeStamp;
protected function setUp(): void
{
$this->docTimeStamp = new DocTimeStamp();
}
public function testValueObjectStructure(): void
{
$out = $this->docTimeStamp->valueObject(7);
$this->assertStringStartsWith("7 0 obj\n", $out);
$this->assertStringEndsWith(" >>\nendobj\n", $out);
$this->assertStringContainsString('/Type /DocTimeStamp /Filter /Adobe.PPKLite /SubFilter /ETSI.RFC3161', $out);
$this->assertStringContainsString(Signature::BYTE_RANGE_PLACEHOLDER, $out);
$this->assertStringContainsString(
'/Contents<' . \str_repeat('0', Signature::DEFAULT_CONTENTS_LENGTH) . '>',
$out,
);
// A document timestamp is not a signature: no /Sig, /Reference, /M, or /V.
$this->assertStringNotContainsString('/Type /Sig', $out);
$this->assertStringNotContainsString('/Reference', $out);
$this->assertStringNotContainsString('/M ', $out);
$this->assertStringNotContainsString('/V ', $out);
}
public function testCustomContentsLength(): void
{
$out = $this->docTimeStamp->valueObject(2, 64);
$this->assertStringContainsString('/Contents<' . \str_repeat('0', 64) . '>', $out);
$this->assertStringNotContainsString(\str_repeat('0', 65), $out);
}
}
@@ -0,0 +1,122 @@
<?php
declare(strict_types=1);
/**
* DssTest.php
*
* @since 2026-07-15
* @category Library
* @package PdfSign
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf-sign
*
* This file is part of tc-lib-pdf-sign software library.
*/
namespace Test\Output;
use Com\Tecnick\Pdf\Sign\Exception;
use Com\Tecnick\Pdf\Sign\Output\Dss;
use PHPUnit\Framework\TestCase;
/**
* DSS Output Test
*
* @since 2026-07-15
* @category Library
* @package PdfSign
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf-sign
*/
class DssTest extends TestCase
{
private Dss $dss;
protected function setUp(): void
{
$this->dss = new Dss();
}
public function testEmitReturnsNothingForEmptyMaterial(): void
{
$pon = 7;
$result = $this->dss->emit(['certs' => [], 'ocsp' => [], 'crls' => []], 'SIG', $pon);
$this->assertSame([], $result['objects']);
$this->assertSame(0, $result['object_id']);
$this->assertSame(7, $pon);
}
public function testEmitProducesStreamsVriAndDss(): void
{
$pon = 10;
$contents = 'CMS-SIGNATURE-BYTES';
$result = $this->dss->emit(
['certs' => ['CERT-DER'], 'ocsp' => ['OCSP-RESP'], 'crls' => ['CRL-DATA']],
$contents,
$pon,
);
// 3 streams (11,12,13), VRI (14), DSS (15).
$this->assertSame(15, $pon);
$this->assertSame(15, $result['object_id']);
// The whole map is keyed by object number, ready for an incremental xref.
$vriKey = \strtoupper(\sha1($contents));
$this->assertSame(
[
11 => "11 0 obj\n<< /Length 8 >>\nstream\nCERT-DER\nendstream\nendobj\n",
12 => "12 0 obj\n<< /Length 9 >>\nstream\nOCSP-RESP\nendstream\nendobj\n",
13 => "13 0 obj\n<< /Length 8 >>\nstream\nCRL-DATA\nendstream\nendobj\n",
14 => "14 0 obj\n<< /Type /VRI /Cert [ 11 0 R ] /OCSP [ 12 0 R ] /CRL [ 13 0 R ] >>\nendobj\n",
15 =>
"15 0 obj\n<< /Type /DSS /VRI << /"
. $vriKey
. ' 14 0 R >>'
. ' /Certs [ 11 0 R ] /OCSPs [ 12 0 R ] /CRLs [ 13 0 R ]'
. " >>\nendobj\n",
],
$result['objects'],
);
}
public function testEmitOmitsEmptyCategories(): void
{
$pon = 0;
$result = $this->dss->emit(['certs' => ['A', 'B'], 'ocsp' => [], 'crls' => []], 'SIG', $pon);
// 2 cert streams (1,2), VRI (3), DSS (4).
$this->assertSame(4, $result['object_id']);
$objects = \implode('', $result['objects']);
$this->assertStringContainsString('<< /Type /VRI /Cert [ 1 0 R 2 0 R ] >>', $objects);
$this->assertStringNotContainsString('/OCSP ', $objects);
$this->assertStringNotContainsString('/CRL ', $objects);
$this->assertStringContainsString('/Certs [ 1 0 R 2 0 R ]', $objects);
$this->assertStringNotContainsString('/OCSPs', $objects);
$this->assertStringNotContainsString('/CRLs', $objects);
}
public function testEmitEncryptsStreams(): void
{
$pon = 0;
$encryptor = static fn(string $data, int $objectId): string => 'E' . $objectId . ':' . $data;
$result = $this->dss->emit(['certs' => ['X'], 'ocsp' => [], 'crls' => []], 'SIG', $pon, $encryptor);
// Stream 1 carries the encrypted payload "E1:X" (length 4).
$objects = \implode('', $result['objects']);
$this->assertStringContainsString("1 0 obj\n<< /Length 4 >>\nstream\nE1:X\nendstream\nendobj\n", $objects);
}
public function testEmitRejectsNonStringEncryptorResult(): void
{
$pon = 0;
$encryptor = static fn(string $_data, int $objectId): int => $objectId;
$this->expectException(Exception::class);
$this->dss->emit(['certs' => ['X'], 'ocsp' => [], 'crls' => []], 'SIG', $pon, $encryptor);
}
}
@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
/**
* SignatureTest.php
*
* @since 2026-07-15
* @category Library
* @package PdfSign
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf-sign
*
* This file is part of tc-lib-pdf-sign software library.
*/
namespace Test\Output;
use Com\Tecnick\Pdf\Sign\Exception;
use Com\Tecnick\Pdf\Sign\Output\Signature;
use PHPUnit\Framework\TestCase;
/**
* Signature Output Test
*
* @since 2026-07-15
* @category Library
* @package PdfSign
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf-sign
*/
class SignatureTest extends TestCase
{
private Signature $signature;
protected function setUp(): void
{
$this->signature = new Signature();
}
private const DOCMDP_REFERENCE =
' /Reference [ << /Type /SigRef /TransformMethod /DocMDP'
. ' /TransformParams << /Type /TransformParams /P 2 /V /1.2 >> >> ]';
private const DATE_VALUE = "(D:20231114221320+00'00')";
public function testValueObjectWithReferenceAndInfo(): void
{
$out = $this->signature->valueObject(
12,
'ETSI.CAdES.detached',
self::DOCMDP_REFERENCE,
['Name' => 'Jane Doe', 'Location' => 'Rome', 'Reason' => 'Approval', 'ContactInfo' => 'jane@example.org'],
self::DATE_VALUE,
);
$this->assertStringStartsWith("12 0 obj\n", $out);
$this->assertStringEndsWith(" >>\nendobj\n", $out);
$this->assertStringContainsString('/Type /Sig /Filter /Adobe.PPKLite /SubFilter /ETSI.CAdES.detached', $out);
$this->assertStringContainsString(Signature::BYTE_RANGE_PLACEHOLDER, $out);
$this->assertStringContainsString(
'/Contents<' . \str_repeat('0', Signature::DEFAULT_CONTENTS_LENGTH) . '>',
$out,
);
$this->assertStringContainsString(self::DOCMDP_REFERENCE, $out);
$this->assertStringContainsString('/Name (Jane Doe)', $out);
$this->assertStringContainsString('/Location (Rome)', $out);
$this->assertStringContainsString('/Reason (Approval)', $out);
$this->assertStringContainsString('/ContactInfo (jane@example.org)', $out);
// The /M date token is appended verbatim (already encoded by the caller).
$this->assertStringContainsString(' /M ' . self::DATE_VALUE . ' >>', $out);
}
public function testEmptyReferenceIsOmitted(): void
{
$out = $this->signature->valueObject(3, 'ETSI.CAdES.detached', '', [], self::DATE_VALUE);
$this->assertStringNotContainsString('/Reference', $out);
$this->assertStringNotContainsString('/Name', $out);
$this->assertStringContainsString('/SubFilter /ETSI.CAdES.detached', $out);
}
public function testCustomContentsLength(): void
{
$out = $this->signature->valueObject(1, 'adbe.pkcs7.detached', '', [], self::DATE_VALUE, 128);
$this->assertStringContainsString('/Contents<' . \str_repeat('0', 128) . '>', $out);
$this->assertStringNotContainsString(\str_repeat('0', 129), $out);
}
public function testDefaultEncoderEscapesLiteralStrings(): void
{
$out = $this->signature->valueObject(1, 'adbe.pkcs7.detached', '', ['Name' => 'A (B) \\ C'], self::DATE_VALUE);
$this->assertStringContainsString('/Name (A \\(B\\) \\\\ C)', $out);
}
public function testUsesInjectedStringEncoder(): void
{
$encoder = static fn(string $text, int $_objectId): string => '<' . \bin2hex($text) . '>';
$out = $this->signature->valueObject(
5,
'ETSI.CAdES.detached',
'',
['Reason' => 'Hi'],
self::DATE_VALUE,
Signature::DEFAULT_CONTENTS_LENGTH,
$encoder,
);
$this->assertStringContainsString('/Reason <' . \bin2hex('Hi') . '>', $out);
}
public function testRejectsNonStringEncoderResult(): void
{
$encoder = static fn(string $_text, int $objectId): int => $objectId;
$this->expectException(Exception::class);
$this->signature->valueObject(
5,
'ETSI.CAdES.detached',
'',
['Reason' => 'Hi'],
self::DATE_VALUE,
Signature::DEFAULT_CONTENTS_LENGTH,
$encoder,
);
}
}
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
/**
* WidgetTest.php
*
* @since 2026-07-15
* @category Library
* @package PdfSign
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf-sign
*
* This file is part of tc-lib-pdf-sign software library.
*/
namespace Test\Output;
use Com\Tecnick\Pdf\Sign\Exception;
use Com\Tecnick\Pdf\Sign\Output\Widget;
use PHPUnit\Framework\TestCase;
/**
* Widget Output Test
*
* @since 2026-07-15
* @category Library
* @package PdfSign
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf-sign
*/
class WidgetTest extends TestCase
{
private Widget $widget;
protected function setUp(): void
{
$this->widget = new Widget();
}
public function testSignedFieldWidget(): void
{
$out = $this->widget->annotation(8, '10.0 20.0 110.0 60.0', 5, 'Signature', 9, ' /AS /N /AP << /N 20 0 R >>');
$this->assertStringStartsWith("8 0 obj\n", $out);
$this->assertStringEndsWith(" >>\nendobj\n", $out);
$this->assertStringContainsString('/Type /Annot /Subtype /Widget', $out);
$this->assertStringContainsString('/Rect [10.0 20.0 110.0 60.0]', $out);
$this->assertStringContainsString('/P 5 0 R', $out);
$this->assertStringContainsString('/F 4 /FT /Sig', $out);
$this->assertStringContainsString('/T (Signature)', $out);
$this->assertStringContainsString('/Ff 0', $out);
$this->assertStringContainsString('/AS /N /AP << /N 20 0 R >>', $out);
$this->assertStringContainsString('/V 9 0 R', $out);
}
public function testEmptyFieldWidgetHasNoValueOrAppearance(): void
{
$out = $this->widget->annotation(4, '0 0 100 40', 5, 'Reviewer [002]');
$this->assertStringContainsString('/T (Reviewer [002])', $out);
$this->assertStringNotContainsString('/V ', $out);
$this->assertStringNotContainsString('/AP', $out);
}
public function testUsesInjectedStringEncoder(): void
{
$encoder = static fn(string $text, int $_objectId): string => '<' . \bin2hex($text) . '>';
$out = $this->widget->annotation(4, '0 0 1 1', 5, 'Sig', null, '', $encoder);
$this->assertStringContainsString('/T <' . \bin2hex('Sig') . '>', $out);
}
public function testRejectsNonStringEncoderResult(): void
{
$encoder = static fn(string $_text, int $objectId): int => $objectId;
$this->expectException(Exception::class);
$this->widget->annotation(4, '0 0 1 1', 5, 'Sig', null, '', $encoder);
}
}