Skip to content

Commit 95cadfa

Browse files
tests: add test for relinking certs to their ca upon import #605
1 parent 7a55f2f commit 95cadfa

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Tests/APIModelsCertificateTestCase.inc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace RESTAPI\Tests;
44

55
use RESTAPI\Core\TestCase;
66
use RESTAPI\Models\Certificate;
7+
use RESTAPI\Models\CertificateAuthorityGenerate;
8+
use RESTAPI\Models\CertificateGenerate;
79

810
class APIModelsCertificateTestCase extends TestCase {
911
const EXAMPLE_CRT = "-----BEGIN CERTIFICATE-----
@@ -110,4 +112,56 @@ R02Pul8ulWQ8Kl3Q3pou8As7W1mMzA2DxQ==
110112
},
111113
);
112114
}
115+
116+
/**
117+
* Checks that certificates are relinked to their CAs (if found) when they are created/imported.
118+
*/
119+
public function test_certificate_is_relinked_to_ca_on_create(): void {
120+
# Create a CA we can use to test the relinking
121+
$ca = new CertificateAuthorityGenerate(
122+
descr: 'test_ca',
123+
trust: true,
124+
randomserial: true,
125+
is_intermediate: false,
126+
keytype: 'RSA',
127+
keylen: 2048,
128+
digest_alg: 'sha256',
129+
lifetime: 3650,
130+
dn_country: 'US',
131+
dn_state: 'UT',
132+
dn_city: 'Salt Lake City',
133+
dn_organization: 'ACME Org',
134+
dn_organizationalunit: 'IT',
135+
);
136+
$ca->always_apply = false; # Disable always_apply so we can test the create method without overloading cpu
137+
$ca->create();
138+
139+
# Generate a new certificates using the CA
140+
$cert = new CertificateGenerate(
141+
descr: 'testcert',
142+
caref: $ca->refid->value,
143+
keytype: 'RSA',
144+
keylen: 2048,
145+
digest_alg: 'sha256',
146+
lifetime: 3650,
147+
type: 'user',
148+
dn_country: 'US',
149+
dn_state: 'UT',
150+
dn_city: 'Salt Lake City',
151+
dn_organization: 'ACME Org',
152+
dn_organizationalunit: 'IT',
153+
dn_commonname: 'testcert.example.com',
154+
);
155+
$cert->create();
156+
157+
# Capture the crt and prv values of the certificate and delete it
158+
$crt = $cert->crt->value;
159+
$prv = $cert->prv->value;
160+
$cert->delete();
161+
162+
# Import the certificate and ensure it is automatically relinked to the CA
163+
$cert = new Certificate(descr: 'testcert', type: 'user', crt: $crt, prv: $prv);
164+
$cert->create();
165+
$this->assert_equals($ca->refid->value, $cert->caref->value);
166+
}
113167
}

0 commit comments

Comments
 (0)