Skip to content

Commit 5b6bdb3

Browse files
authored
Merge pull request #17 from JSignPdf/fix/tests
fix: update tests to don't fail with old cert
2 parents fa302ee + 8b87c9b commit 5b6bdb3

1 file changed

Lines changed: 55 additions & 3 deletions

File tree

tests/JSignPDFTest.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,61 @@ protected function setUp(): void
1919
$this->service = new JSignService();
2020
}
2121

22+
private function getNewCert($password)
23+
{
24+
$privateKey = openssl_pkey_new([
25+
'private_key_bits' => 2048,
26+
'private_key_type' => OPENSSL_KEYTYPE_RSA,
27+
]);
28+
29+
$csrNames = ['commonName' => 'Jhon Doe'];
30+
31+
$csr = openssl_csr_new($csrNames, $privateKey, ['digest_alg' => 'sha256']);
32+
$x509 = openssl_csr_sign($csr, null, $privateKey, $days = 365, ['digest_alg' => 'sha256']);
33+
34+
openssl_x509_export($x509, $rootCertificate);
35+
openssl_pkey_export($privateKey, $rootPrivateKey);
36+
37+
$privateKey = openssl_pkey_new([
38+
'private_key_bits' => 2048,
39+
'private_key_type' => OPENSSL_KEYTYPE_RSA,
40+
]);
41+
$temporaryFile = tempnam(sys_get_temp_dir(), 'cfg');
42+
$csr = openssl_csr_new($csrNames, $privateKey);
43+
$x509 = openssl_csr_sign($csr, $rootCertificate, $rootPrivateKey, 365);
44+
return $this->exportToPkcs12($x509, $privateKey, $password);
45+
}
46+
47+
private function exportToPkcs12(\OpenSSLCertificate $certificate, \OpenSSLAsymmetricKey $privateKey, string $password)
48+
{
49+
$certContent = null;
50+
openssl_pkcs12_export(
51+
$certificate,
52+
$certContent,
53+
$privateKey,
54+
$password,
55+
);
56+
return $certContent;
57+
}
58+
2259
public function testSignSuccess()
2360
{
2461
if (!class_exists('JSignPDF\JSignPDFBin\JavaCommandService')) {
2562
$this->markTestSkipped('Install jsignpdf/jsignpdf-bin');
2663
}
2764
$params = JSignParamBuilder::instance()->withDefault();
65+
$params->setCertificate($this->getNewCert($params->getPassword()));
66+
$fileSigned = $this->service->sign($params);
67+
$this->assertNotNull($fileSigned);
68+
}
69+
70+
public function testCertificateExpired()
71+
{
72+
if (!class_exists('JSignPDF\JSignPDFBin\JavaCommandService')) {
73+
$this->markTestSkipped('Install jsignpdf/jsignpdf-bin');
74+
}
75+
$this->expectExceptionMessage('Certificate expired.');
76+
$params = JSignParamBuilder::instance()->withDefault();
2877
$fileSigned = $this->service->sign($params);
2978
$this->assertNotNull($fileSigned);
3079
}
@@ -43,6 +92,7 @@ public function testWithWhenResponseIsBase64()
4392
$this->markTestSkipped('Install jsignpdf/jsignpdf-bin');
4493
}
4594
$params = JSignParamBuilder::instance()->withDefault()->setIsOutputTypeBase64(true);
95+
$params->setCertificate($this->getNewCert($params->getPassword()));
4696
$fileSigned = $this->service->sign($params);
4797
$this->assertTrue(base64_decode($fileSigned, true) == true);
4898
}
@@ -82,11 +132,12 @@ public function testSignWhenPasswordIsInvalid()
82132
$this->service->sign($params);
83133
}
84134

85-
public function testSignWhenJarNotFound()
135+
public function testJSignPDFNotFound()
86136
{
87137
$this->expectExceptionMessageMatches('/Jar of JSignPDF not found on path/');
88-
$params = JSignParamBuilder::instance()->withDefault()->setIsUseJavaInstalled(true);
89-
$this->service->sign($params);
138+
$params = JSignParamBuilder::instance()->withDefault()->setjSignPdfJarPath('invalid_path');
139+
$params->setCertificate($this->getNewCert($params->getPassword()));
140+
$this->service->getVersion($params);
90141
}
91142

92143
public function testSignWhenJavaNotFound()
@@ -98,6 +149,7 @@ public function testSignWhenJavaNotFound()
98149
}
99150
$this->expectExceptionMessage('Java not installed, set the flag "isUseJavaInstalled" as false or install java.');
100151
$params = JSignParamBuilder::instance()->withDefault()->setIsUseJavaInstalled(true);
152+
$params->setCertificate($this->getNewCert($params->getPassword()));
101153
$this->service->sign($params);
102154
}
103155

0 commit comments

Comments
 (0)