Skip to content

Commit 812c16d

Browse files
committed
improve test coverage
1 parent 0e2e343 commit 812c16d

File tree

6 files changed

+122
-30
lines changed

6 files changed

+122
-30
lines changed

src/main/java/com/siemens/pki/cmpracomponent/msgprocessing/RaDownstream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private PKIMessage handleCrmfCertificateRequest(
228228
final Extensions newExtensions = new Extensions(Stream.concat(
229229
Arrays.stream(extensions.getExtensionOIDs())
230230
.filter(oid -> !oid.equals(AttestationObjectIdentifiers.id_aa_evidence))
231-
.map(oid -> extensions.getExtension(oid)),
231+
.map(extensions::getExtension),
232232
Arrays.asList(ratExtension).stream())
233233
.toArray(Extension[]::new));
234234
certTemplate = new CertTemplateBuilder()
@@ -935,7 +935,7 @@ private PKIMessage processCertResponse(
935935

936936
private Extension processRatVerification(
937937
final RatVerifierAdapter verifyAdapter, final byte[] transactionId, Extensions extensions)
938-
throws CmpProcessingException, InterruptedException, IOException {
938+
throws IOException {
939939
if (extensions == null) {
940940
return null;
941941
}

src/main/java/com/siemens/pki/cmpracomponent/persistency/TransactionStateTracker.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,6 @@ private boolean isCertResponse(final PKIMessage msg) {
148148
}
149149
}
150150

151-
private boolean isCertResponseWithWaitingIndication(final PKIMessage msg) {
152-
try {
153-
return ((CertRepMessage) msg.getBody().getContent())
154-
.getResponse()[0]
155-
.getStatus()
156-
.getStatus()
157-
.intValue()
158-
== PKIStatus.WAITING;
159-
} catch (final Exception ex) {
160-
return false;
161-
}
162-
}
163-
164151
private boolean isConfirmConfirm(final PKIMessage msg) {
165152
return msg.getBody().getType() == PKIBody.TYPE_CONFIRM;
166153
}
@@ -378,7 +365,7 @@ public void trackMessage(final PKIMessage message) throws BaseCmpException, IOEx
378365
PKIFailureInfo.badMessageCheck,
379366
"request was not answered by cert response for " + MessageDumper.msgAsShortString(message));
380367
}
381-
if (isCertResponseWithWaitingIndication(message)) {
368+
if (isWaitingIndication(message)) {
382369
persistencyContext.setLastTransactionState(LastTransactionState.CERTIFICATE_POLLING);
383370
return;
384371
}

src/main/java/com/siemens/pki/verifieradapter/asn1/AttestationResultBundle.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ private AttestationResultBundle(ASN1Sequence sequence) {
5151
}
5252
}
5353

54-
public AttestationResultBundle(AttestationResult[] results) {
55-
this(results, null);
56-
}
57-
5854
public AttestationResultBundle(AttestationResult[] results, Certificate[] certs) {
5955
this.results = new ASN1EncodableVector();
6056
this.results.addAll(results);

src/test/java/com/siemens/pki/cmpclientcomponent/test/TestCrWithRAT.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,7 @@ public boolean isRaVerifiedAcceptable(final String certProfile, final int bodyTy
410410
};
411411
}
412412

413-
@Override
414-
ClientContext getClientContext(final int enrollmentType, KeyPair keyPair, byte[] certificationRequest) {
413+
ClientContext getRatClientContext(final int enrollmentType, KeyPair keyPair, boolean requestImplicitConfirm) {
415414
return new ClientContext() {
416415

417416
@Override
@@ -480,11 +479,6 @@ public KeyPair getCertificateKeypair() {
480479
return keyPair;
481480
}
482481

483-
@Override
484-
public byte[] getCertificationRequest() {
485-
return certificationRequest;
486-
}
487-
488482
@Override
489483
public VerificationContext getEnrollmentTrust() {
490484
return enrollmentCredentials;
@@ -507,7 +501,7 @@ public X509Certificate getOldCert() {
507501

508502
@Override
509503
public boolean getRequestImplictConfirm() {
510-
return false;
504+
return requestImplicitConfirm;
511505
}
512506

513507
@Override
@@ -534,10 +528,32 @@ public void setUp() throws Exception {
534528
public void testCr() throws Exception {
535529
final EnrollmentResult ret = getSignatureBasedCmpClient(
536530
"theCertProfileForOnlineEnrollment",
537-
getClientContext(
531+
getRatClientContext(
532+
PKIBody.TYPE_CERT_REQ,
533+
ConfigurationFactory.getKeyGenerator().generateKeyPair(),
534+
false),
535+
UPSTREAM_TRUST_PATH)
536+
.invokeEnrollment();
537+
final ASN1OctetString extValue = ASN1OctetString.getInstance(
538+
ret.getEnrolledCertificate().getExtensionValue(AttestationObjectIdentifiers.id_aa_ar.getId()));
539+
final AttestationResultBundle attestationResultBundle =
540+
AttestationResultBundle.getInstance(extValue.getOctets());
541+
assertNotNull(attestationResultBundle);
542+
assertNotNull(attestationResultBundle.getCerts());
543+
for (AttestationResult result : attestationResultBundle.getResults()) {
544+
assertNotNull(result.getType());
545+
assertNotNull(result.getStmt());
546+
}
547+
}
548+
549+
@Test
550+
public void testCrWithImplicitConfirm() throws Exception {
551+
final EnrollmentResult ret = getSignatureBasedCmpClient(
552+
"theCertProfileForOnlineEnrollment",
553+
getRatClientContext(
538554
PKIBody.TYPE_CERT_REQ,
539555
ConfigurationFactory.getKeyGenerator().generateKeyPair(),
540-
null),
556+
true),
541557
UPSTREAM_TRUST_PATH)
542558
.invokeEnrollment();
543559
final ASN1OctetString extValue = ASN1OctetString.getInstance(
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2025 Siemens AG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
package com.siemens.pki.verifieradapter.asn1;
19+
20+
import static org.junit.Assert.*;
21+
22+
import com.siemens.pki.verifieradapter.asn1.NonceRequestValue.NonceRequest;
23+
import com.siemens.pki.verifieradapter.asn1.NonceResponseValue.NonceResponse;
24+
import java.io.IOException;
25+
import java.math.BigInteger;
26+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
27+
import org.bouncycastle.asn1.DERUTF8String;
28+
import org.junit.Test;
29+
30+
/**
31+
* test some unusual parameter combinations to improve test coverage
32+
*/
33+
public class TestRatAsn1 {
34+
35+
@Test
36+
public void testAttestationResultBundle() throws IOException {
37+
byte[] encoded = new AttestationResultBundle(
38+
new AttestationResult[] {
39+
new AttestationResult(new ASN1ObjectIdentifier("1.2.3"), new DERUTF8String("hallo"))
40+
},
41+
null)
42+
.getEncoded();
43+
AttestationResultBundle decoded = AttestationResultBundle.getInstance(encoded);
44+
assertEquals(1, decoded.getResults().length);
45+
final AttestationResult attestationResult = decoded.getResults()[0];
46+
assertEquals(new ASN1ObjectIdentifier("1.2.3"), attestationResult.getType());
47+
assertEquals(new DERUTF8String("hallo"), attestationResult.getStmt());
48+
assertNull(decoded.getCerts());
49+
}
50+
51+
@Test
52+
public void testEvidenceBundle() throws IOException {
53+
byte[] encoded = new EvidenceBundle(
54+
new EvidenceStatement[] {
55+
new EvidenceStatement(new ASN1ObjectIdentifier("1.2.3"), new DERUTF8String("hallo"), null)
56+
},
57+
null)
58+
.getEncoded();
59+
EvidenceBundle decoded = EvidenceBundle.getInstance(encoded);
60+
assertEquals(1, decoded.getEvidences().length);
61+
final EvidenceStatement evidenceStatement = decoded.getEvidences()[0];
62+
assertEquals(new ASN1ObjectIdentifier("1.2.3"), evidenceStatement.getType());
63+
assertEquals(new DERUTF8String("hallo"), evidenceStatement.getStmt());
64+
assertNull(evidenceStatement.getHint());
65+
assertNull(decoded.getCerts());
66+
}
67+
68+
@Test
69+
public void testNonceRequestValue() throws IOException {
70+
byte[] encoded = new NonceRequestValue(new NonceRequest[] {new NonceRequest((BigInteger) null, null, null)})
71+
.getEncoded();
72+
NonceRequestValue decoded = NonceRequestValue.getInstance(encoded);
73+
assertEquals(1, decoded.getNonceRequests().length);
74+
final NonceRequest nonceRequest = decoded.getNonceRequests()[0];
75+
assertNull(nonceRequest.getLen());
76+
assertNull(nonceRequest.getType());
77+
assertNull(nonceRequest.getHint());
78+
}
79+
80+
@Test
81+
public void testNonceResponseValue() throws IOException {
82+
byte[] encoded = new NonceResponseValue(new NonceResponse[] {new NonceResponse(new byte[10], null, null, null)})
83+
.getEncoded();
84+
NonceResponseValue decoded = NonceResponseValue.getInstance(encoded);
85+
assertEquals(1, decoded.getNonceResponse().length);
86+
final NonceResponse nonceRequest = decoded.getNonceResponse()[0];
87+
assertNotNull(nonceRequest.getNonce());
88+
assertNull(nonceRequest.getExpiry());
89+
assertNull(nonceRequest.getType());
90+
assertNull(nonceRequest.getHint());
91+
}
92+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package com.siemens.pki.verifieradapter.asn1;

0 commit comments

Comments
 (0)