Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Tinker <michael.tinker@swirldslabs.com>
  • Loading branch information
tinker-michaelj committed Nov 19, 2024
1 parent 0a59249 commit aa0b34e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.hedera.hapi.node.base.ResponseCodeEnum;
Expand Down Expand Up @@ -73,8 +72,7 @@ void invalidCertificatePem() throws CertificateException, IOException {
final var cert = readCertificatePemFile(pemFilePath);
final var test = Path.of(tmpDir.getPath() + "/test");
Files.write(test, cert.getEncoded());
final var genCert = readCertificatePemFile(test);
assertNull(genCert);
assertThrows(CertificateException.class, () -> readCertificatePemFile(test));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,18 @@ public static X509Certificate readCertificatePemFile(@NonNull final Path pemFile
public static X509Certificate readCertificatePemFile(@NonNull final InputStream in)
throws IOException, CertificateException {
requireNonNull(in);
Object entry;
try (final var parser = new PEMParser(new InputStreamReader(in))) {
final var entry = parser.readObject();
if (!(entry instanceof X509CertificateHolder holder)) {
throw new CertificateException();
while ((entry = parser.readObject()) != null) {
if (entry instanceof X509CertificateHolder ch) {
return new JcaX509CertificateConverter().getCertificate(ch);
} else {
throw new CertificateException(
"Not X509 Certificate, it is " + entry.getClass().getSimpleName());
}
}
return new JcaX509CertificateConverter().getCertificate(holder);
}
throw new CertificateException("No X509 Certificate found in the PEM file");
}

/**
Expand Down

0 comments on commit aa0b34e

Please sign in to comment.