Merge pull request #648 from nsacyber/v3_issue_615-unittest

HIRS_Utils Unit Tests Migration from /hirs/persist/ and /hirs/appraiser/ directories
This commit is contained in:
Cyrus 2023-12-18 11:42:56 -05:00 committed by GitHub
commit a30b81a68e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 454 additions and 84 deletions

View File

@ -6,12 +6,7 @@ import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import lombok.AccessLevel; import lombok.*;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
/** /**
* The <code>Appraiser</code> class represents an appraiser that can appraise a <code>Report</code>. * The <code>Appraiser</code> class represents an appraiser that can appraise a <code>Report</code>.
@ -67,7 +62,6 @@ public class Appraiser {
private Long id; private Long id;
@Getter @Getter
@Setter
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
private String name; private String name;
@ -83,6 +77,19 @@ public class Appraiser {
* @param name unique name * @param name unique name
*/ */
public Appraiser(final String name) { public Appraiser(final String name) {
setName(name);
}
/**
* Sets the name that uniquely identifies this <code>Appraiser</code>. The name may not be
* null.
*
* @param name unique name for this <code>Appraiser</code>
*/
public final void setName(final String name) {
if (name == null) {
throw new NullPointerException("name");
}
this.name = name; this.name = name;
} }
} }

View File

@ -0,0 +1,164 @@
package hirs.attestationca.persist.entity;
import hirs.attestationca.persist.entity.userdefined.certificate.CertificateAuthorityCredential;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.security.cert.CertificateException;
import static org.junit.jupiter.api.Assertions.*;
/**
* Unit tests for the class <code>Appraiser</code>.
*/
public final class AppraiserTest {
/**
* Tests that an <code>Appraiser</code> can be created with a valid name.
*/
@Test
public void testAppraiser() {
final String name = "Test Appraiser";
new TestAppraiser(name);
}
/**
* Tests that <code>Appraiser</code> throws a <code>NullPointerException</code> if a name is not
* provided.
*/
@Test
public void testAppraiserNullName() {
assertThrows(NullPointerException.class, () ->
new TestAppraiser(null));
}
/**
* Tests that the name is returned from <code>getName</code>.
*/
@Test
public void testGetName() {
final String name = "Test Appraiser";
final Appraiser appraiser = new TestAppraiser(name);
assertEquals(name, appraiser.getName());
}
/**
* Tests that the name property can be set.
*/
@Test
public void testSetName() {
final String originalName = "Test Appraiser";
final Appraiser appraiser = new TestAppraiser(originalName);
assertEquals(originalName, appraiser.getName());
final String newName = "Awesome Test Appraiser";
appraiser.setName(newName);
assertEquals(newName, appraiser.getName());
}
/**
* Tests that a <code>NullPointerException</code> is thrown if the name is null.
*/
@Test
public void testSetNameNull() {
final String name = "Test Appraiser";
final Appraiser appraiser = new TestAppraiser(name);
assertEquals(name, appraiser.getName());
NullPointerException expected = null;
try {
appraiser.setName(null);
} catch (NullPointerException e) {
expected = e;
}
assertNotNull(expected, "NullPointerException not caught");
assertEquals(name, appraiser.getName());
}
/**
* Tests that x.equals(null) returns false.
*/
@Test
public void testEqualsNull() {
final String name = "Test Appraiser";
final Appraiser appraiser = new TestAppraiser(name);
assertFalse(appraiser.equals(null));
}
/**
* Tests that x.equals(x) for an appraiser.
*/
@Test
public void testEqualsReflexive() {
final String name = "Test Appraiser";
final Appraiser appraiser = new TestAppraiser(name);
assertTrue(appraiser.equals(appraiser));
}
/**
* Tests that x.equals(y) and y.equals(x) for an appraiser.
*/
@Test
public void testEqualsSymmetric() {
final String name = "Test Appraiser";
final Appraiser appraiser1 = new TestAppraiser(name);
final Appraiser appraiser2 = new TestAppraiser(name);
assertTrue(appraiser1.equals(appraiser2));
assertTrue(appraiser2.equals(appraiser1));
}
/**
* Tests that x.equals(y) and y.equals(z) then x.equals(z) for an appraiser.
*/
@Test
public void testEqualsTransitive() {
final String name = "Test Appraiser";
final Appraiser appraiser1 = new TestAppraiser(name);
final Appraiser appraiser2 = new TestAppraiser(name);
final Appraiser appraiser3 = new TestAppraiser(name);
assertTrue(appraiser1.equals(appraiser2));
assertTrue(appraiser2.equals(appraiser3));
assertTrue(appraiser1.equals(appraiser3));
}
/**
* Tests that two appraisers are not equal if their names are different.
*/
@Test
public void testNotEquals() {
final String name1 = "Test Appraiser";
final String name2 = "Other Appraiser";
final Appraiser appraiser1 = new TestAppraiser(name1);
final Appraiser appraiser2 = new TestAppraiser(name2);
assertFalse(appraiser1.equals(appraiser2));
assertFalse(appraiser2.equals(appraiser1));
}
/**
* Tests that if two appraisers are equal that their hash codes are equal.
*/
@Test
public void testHashCodeEquals() {
final String name = "Test Appraiser";
final Appraiser appraiser1 = new TestAppraiser(name);
final Appraiser appraiser2 = new TestAppraiser(name);
assertTrue(appraiser1.equals(appraiser2));
assertTrue(appraiser2.equals(appraiser1));
assertEquals(appraiser1.hashCode(), appraiser2.hashCode());
assertEquals(appraiser2.hashCode(), appraiser1.hashCode());
}
/**
* Tests that if two appraisers are not equal that their hash codes are not equal.
*/
@Test
public void testHashCodeNotEquals() {
final String name1 = "Test Appraiser";
final String name2 = "Other Appraiser";
final Appraiser appraiser1 = new TestAppraiser(name1);
final Appraiser appraiser2 = new TestAppraiser(name2);
assertFalse(appraiser1.equals(appraiser2));
assertFalse(appraiser2.equals(appraiser1));
assertNotEquals(appraiser1.hashCode(), appraiser2.hashCode());
assertNotEquals(appraiser2.hashCode(), appraiser1.hashCode());
}
}

View File

@ -0,0 +1,26 @@
package hirs.attestationca.persist.entity;
import jakarta.persistence.Entity;
/**
* Test class for the <code>Appraiser</code> abstract base class.
*/
@Entity
public class TestAppraiser extends Appraiser {
/**
* Creates a new <code>TestAppraiser</code>.
*
* @param name name
*/
public TestAppraiser(final String name) {
super(name);
}
/**
* Default constructor necessary for Hibernate.
*/
protected TestAppraiser() {
/* do nothing */
}
}

View File

@ -0,0 +1,164 @@
package hirs.attestationca.persist.entity.tpm;
import hirs.attestationca.persist.entity.manager.TPM2ProvisionerStateRepository;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Random;
/**
* Contains unit tests for {@link TPM2ProvisionerState}.
*/
public class TPM2ProvisionerStateTest {
/**
* Tests that the values passed to the constructor are equal to the values
* returned by the getters.
*
* @throws IOException this will never happen
*/
@Test
public final void testTPM2ProvisionerState() throws IOException {
Random rand = new Random();
byte[] nonce = new byte[32];
byte[] identityClaim = new byte[360];
rand.nextBytes(nonce);
rand.nextBytes(identityClaim);
TPM2ProvisionerState state = new TPM2ProvisionerState(nonce, identityClaim);
assertArrayEquals(nonce, state.getNonce());
assertArrayEquals(identityClaim, state.getIdentityClaim());
}
/**
* Test that the constructor throws an {@link IllegalArgumentException} when a null is
* passed in for the nonce.
*
* @throws IOException this will never happen
*/
@Test
public final void testNullNonce() throws IOException {
Random rand = new Random();
byte[] nonce = null;
byte[] identityClaim = new byte[360];
rand.nextBytes(identityClaim);
assertThrows(IllegalArgumentException.class, () ->
new TPM2ProvisionerState(nonce, identityClaim));
}
/**
* Test that the constructor throws an {@link IllegalArgumentException} when a null is
* passed in for the identity claim.
*
* @throws IOException this will never happen
*/
@Test
public final void testNullIdentityClaim() throws IOException {
Random rand = new Random();
byte[] nonce = new byte[32];
byte[] identityClaim = null;
rand.nextBytes(nonce);
assertThrows(IllegalArgumentException.class, () ->
new TPM2ProvisionerState(nonce, identityClaim));
}
/**
* Test that the constructor throws an {@link IllegalArgumentException} when a nonce is
* passed in that is less than 8 bytes.
*
* @throws IOException this will never happen
*/
@Test
public final void testNonceToSmall() throws IOException {
Random rand = new Random();
byte[] nonce = new byte[7];
byte[] identityClaim = new byte[360];
rand.nextBytes(nonce);
rand.nextBytes(identityClaim);
assertThrows(IllegalArgumentException.class, () ->
new TPM2ProvisionerState(nonce, identityClaim));
}
/**
* Test that {@link TPM2ProvisionerState#getTPM2ProvisionerState(TPM2ProvisionerStateRepository, byte[])} works.
* {@link TPM2ProvisionerState#getTPM2ProvisionerState(TPM2ProvisionerStateRepository, byte[])}, null is returned.
* @throws IOException this will never happen
*/
@Test
public final void testGetTPM2ProvisionerStateNominal() throws IOException {
TPM2ProvisionerStateRepository tpm2ProvisionerStateRepository = mock(TPM2ProvisionerStateRepository.class);
Random rand = new Random();
byte[] nonce = new byte[32];
byte[] identityClaim = new byte[360];
rand.nextBytes(nonce);
rand.nextBytes(identityClaim);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(nonce));
Long index = dis.readLong();
dis.close();
TPM2ProvisionerState value = new TPM2ProvisionerState(nonce, identityClaim);
when(tpm2ProvisionerStateRepository.findByFirstPartOfNonce(index)).thenReturn(value);
TPM2ProvisionerState tpm2ProvisionerState
= TPM2ProvisionerState.getTPM2ProvisionerState(tpm2ProvisionerStateRepository, nonce);
assertNotNull(tpm2ProvisionerState);
assertArrayEquals(value.getIdentityClaim(), tpm2ProvisionerState.getIdentityClaim());
}
/**
* Test that if a null is passed as a nonce to
* {@link TPM2ProvisionerState#getTPM2ProvisionerState(TPM2ProvisionerStateRepository, byte[])}, null is returned.
* @throws IOException this will never happen
*/
@Test
public final void testGetTPM2ProvisionerStateNullNonce() throws IOException {
TPM2ProvisionerStateRepository tpm2ProvisionerStateRepository = mock(TPM2ProvisionerStateRepository.class);
Random rand = new Random();
byte[] nonce = new byte[32];
byte[] identityClaim = new byte[360];
rand.nextBytes(nonce);
rand.nextBytes(identityClaim);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(nonce));
Long index = dis.readLong();
dis.close();
TPM2ProvisionerState value = new TPM2ProvisionerState(nonce, identityClaim);
when(tpm2ProvisionerStateRepository.findByFirstPartOfNonce(index)).thenReturn(value);
TPM2ProvisionerState tpm2ProvisionerState
= TPM2ProvisionerState.getTPM2ProvisionerState(tpm2ProvisionerStateRepository, null);
assertNull(tpm2ProvisionerState);
}
/**
* Test that if a nonce that is less than 8 bytes is passed to
* {@link TPM2ProvisionerState#getTPM2ProvisionerState(TPM2ProvisionerStateRepository, byte[])}, null is returned.
* @throws IOException this will never happen
*/
@Test
public final void testGetTPM2ProvisionerStateNonceTooSmall() throws IOException {
TPM2ProvisionerStateRepository tpm2ProvisionerStateRepository = mock(TPM2ProvisionerStateRepository.class);
Random rand = new Random();
byte[] nonce = new byte[32];
byte[] identityClaim = new byte[360];
rand.nextBytes(nonce);
rand.nextBytes(identityClaim);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(nonce));
Long index = dis.readLong();
dis.close();
TPM2ProvisionerState value = new TPM2ProvisionerState(nonce, identityClaim);
when(tpm2ProvisionerStateRepository.findByFirstPartOfNonce(index)).thenReturn(value);
TPM2ProvisionerState tpm2ProvisionerState =
TPM2ProvisionerState.getTPM2ProvisionerState(tpm2ProvisionerStateRepository, new byte[7]);
assertNull(tpm2ProvisionerState);
}
}

View File

@ -15,6 +15,7 @@ import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
import hirs.attestationca.persist.entity.userdefined.certificate.*; import hirs.attestationca.persist.entity.userdefined.certificate.*;
import org.bouncycastle.cert.X509AttributeCertificateHolder; import org.bouncycastle.cert.X509AttributeCertificateHolder;
@ -118,12 +119,12 @@ public class CertificateTest {
public void testConstructCertFromByteArray() throws IOException, URISyntaxException { public void testConstructCertFromByteArray() throws IOException, URISyntaxException {
Certificate certificate = new CertificateAuthorityCredential( Certificate certificate = new CertificateAuthorityCredential(
Files.readAllBytes( Files.readAllBytes(
Paths.get(this.getClass().getResource(FAKE_ROOT_CA_FILE).toURI()) Paths.get(Objects.requireNonNull(this.getClass().getResource(FAKE_ROOT_CA_FILE)).toURI())
) )
); );
assertEquals( assertEquals(
certificate.getX509Certificate().getIssuerDN().getName(), "CN=Fake Root CA",
"CN=Fake Root CA" certificate.getX509Certificate().getIssuerX500Principal().getName()
); );
} }
@ -162,11 +163,11 @@ public class CertificateTest {
@Test @Test
public void testConstructCertFromPath() throws URISyntaxException, IOException { public void testConstructCertFromPath() throws URISyntaxException, IOException {
Certificate certificate = new CertificateAuthorityCredential( Certificate certificate = new CertificateAuthorityCredential(
Paths.get(this.getClass().getResource(FAKE_ROOT_CA_FILE).toURI()) Paths.get(Objects.requireNonNull(this.getClass().getResource(FAKE_ROOT_CA_FILE)).toURI())
); );
assertEquals( assertEquals(
certificate.getX509Certificate().getIssuerDN().getName(), "CN=Fake Root CA",
"CN=Fake Root CA" certificate.getX509Certificate().getIssuerX500Principal().getName()
); );
} }
@ -190,19 +191,23 @@ public class CertificateTest {
*/ */
@Test @Test
public void testGetCertificateType() throws IOException { public void testGetCertificateType() throws IOException {
assertEquals(getTestCertificate(FAKE_ROOT_CA_FILE).getCertificateType(), assertEquals(
Certificate.CertificateType.X509_CERTIFICATE); Certificate.CertificateType.X509_CERTIFICATE,
assertNotEquals(getTestCertificate(FAKE_ROOT_CA_FILE).getCertificateType(), getTestCertificate(FAKE_ROOT_CA_FILE).getCertificateType());
Certificate.CertificateType.ATTRIBUTE_CERTIFICATE); assertNotEquals(
Certificate.CertificateType.ATTRIBUTE_CERTIFICATE,
getTestCertificate(FAKE_ROOT_CA_FILE).getCertificateType());
assertNotEquals(getTestCertificate( assertNotEquals(
Certificate.CertificateType.X509_CERTIFICATE,
getTestCertificate(
PlatformCredential.class, PlatformCredential.class,
PlatformCredentialTest.TEST_PLATFORM_CERT_3).getCertificateType(), PlatformCredentialTest.TEST_PLATFORM_CERT_3).getCertificateType());
Certificate.CertificateType.X509_CERTIFICATE); assertEquals(
assertEquals(getTestCertificate( Certificate.CertificateType.ATTRIBUTE_CERTIFICATE,
getTestCertificate(
PlatformCredential.class, PlatformCredential.class,
PlatformCredentialTest.TEST_PLATFORM_CERT_3).getCertificateType(), PlatformCredentialTest.TEST_PLATFORM_CERT_3).getCertificateType());
Certificate.CertificateType.ATTRIBUTE_CERTIFICATE);
} }
@ -218,22 +223,24 @@ public class CertificateTest {
PlatformCredential.class, PlatformCredentialTest.TEST_PLATFORM_CERT_4 PlatformCredential.class, PlatformCredentialTest.TEST_PLATFORM_CERT_4
); );
assertEquals(platformCredential.getCertificateType(),
Certificate.CertificateType.ATTRIBUTE_CERTIFICATE);
assertEquals( assertEquals(
((PlatformCredential) platformCredential).getPlatformSerial(), Certificate.CertificateType.ATTRIBUTE_CERTIFICATE,
"GETY421001GV" platformCredential.getCertificateType());
assertEquals(
"GETY421001GV",
((PlatformCredential) platformCredential).getPlatformSerial()
); );
platformCredential = getTestCertificate( platformCredential = getTestCertificate(
PlatformCredential.class, PlatformCredentialTest.TEST_PLATFORM_CERT_5 PlatformCredential.class, PlatformCredentialTest.TEST_PLATFORM_CERT_5
); );
assertEquals(platformCredential.getCertificateType(),
Certificate.CertificateType.ATTRIBUTE_CERTIFICATE);
assertEquals( assertEquals(
((PlatformCredential) platformCredential).getPlatformSerial(), Certificate.CertificateType.ATTRIBUTE_CERTIFICATE,
"GETY42100160" platformCredential.getCertificateType());
assertEquals(
"GETY42100160",
((PlatformCredential) platformCredential).getPlatformSerial()
); );
} }
@ -248,16 +255,16 @@ public class CertificateTest {
Certificate rootCert = getTestCertificate(FAKE_ROOT_CA_FILE); Certificate rootCert = getTestCertificate(FAKE_ROOT_CA_FILE);
X509Certificate certificate = readX509Certificate(FAKE_ROOT_CA_FILE); X509Certificate certificate = readX509Certificate(FAKE_ROOT_CA_FILE);
assertEquals(rootCert.getSerialNumber(), certificate.getSerialNumber()); assertEquals(certificate.getSerialNumber(), rootCert.getSerialNumber());
assertEquals(rootCert.getIssuer(), assertEquals(certificate.getIssuerX500Principal().getName(),
certificate.getIssuerX500Principal().getName()); rootCert.getIssuer());
assertEquals(rootCert.getSubject(), assertEquals(certificate.getSubjectX500Principal().getName(),
certificate.getSubjectX500Principal().getName()); rootCert.getSubject());
assertArrayEquals(rootCert.getEncodedPublicKey(), assertArrayEquals(certificate.getPublicKey().getEncoded(),
certificate.getPublicKey().getEncoded()); rootCert.getEncodedPublicKey());
assertArrayEquals(rootCert.getSignature(), certificate.getSignature()); assertArrayEquals(certificate.getSignature(), rootCert.getSignature());
assertEquals(rootCert.getBeginValidity(), certificate.getNotBefore()); assertEquals(certificate.getNotBefore(), rootCert.getBeginValidity());
assertEquals(rootCert.getEndValidity(), certificate.getNotAfter()); assertEquals(certificate.getNotAfter(), rootCert.getEndValidity());
} }
/** /**
@ -268,11 +275,13 @@ public class CertificateTest {
@Test @Test
public void testX509CertificateParsingExtended() throws IOException { public void testX509CertificateParsingExtended() throws IOException {
Certificate rootCert = getTestCertificate(INTEL_INT_CA_FILE); Certificate rootCert = getTestCertificate(INTEL_INT_CA_FILE);
assertEquals(rootCert.getAuthorityInfoAccess(), assertEquals(
"https://trustedservices.intel.com/" "https://trustedservices.intel.com/"
+ "content/TSC/certs/TSC_SS_RootCA_Certificate.cer\n"); + "content/TSC/certs/TSC_SS_RootCA_Certificate.cer\n",
assertEquals(rootCert.getAuthorityKeyIdentifier(), rootCert.getAuthorityInfoAccess());
"b56f72cdfd66ce839e1fdb40498f07291f5b99b7"); assertEquals(
"b56f72cdfd66ce839e1fdb40498f07291f5b99b7",
rootCert.getAuthorityKeyIdentifier());
} }
/** /**
@ -290,24 +299,24 @@ public class CertificateTest {
); );
X509AttributeCertificateHolder attrCertHolder = new X509AttributeCertificateHolder( X509AttributeCertificateHolder attrCertHolder = new X509AttributeCertificateHolder(
Files.readAllBytes(Paths.get(this.getClass().getResource( Files.readAllBytes(Paths.get(Objects.requireNonNull(this.getClass().getResource(
PlatformCredentialTest.TEST_PLATFORM_CERT_3 PlatformCredentialTest.TEST_PLATFORM_CERT_3
).toURI())) )).toURI()))
); );
assertEquals( assertEquals(
platformCert.getSerialNumber(), attrCertHolder.getSerialNumber(),
attrCertHolder.getSerialNumber() platformCert.getSerialNumber()
); );
assertEquals( assertEquals(
platformCert.getIssuer(), attrCertHolder.getIssuer().getNames()[0].toString(),
attrCertHolder.getIssuer().getNames()[0].toString() platformCert.getIssuer()
); );
assertEquals(platformCert.getSubject(), null); assertEquals(null, platformCert.getSubject());
assertArrayEquals(platformCert.getEncodedPublicKey(), null); assertArrayEquals(null, platformCert.getEncodedPublicKey());
assertArrayEquals(platformCert.getSignature(), attrCertHolder.getSignature()); assertArrayEquals(attrCertHolder.getSignature(), platformCert.getSignature());
assertEquals(platformCert.getBeginValidity(), attrCertHolder.getNotBefore()); assertEquals(attrCertHolder.getNotBefore(), platformCert.getBeginValidity());
assertEquals(platformCert.getEndValidity(), attrCertHolder.getNotAfter()); assertEquals(attrCertHolder.getNotAfter(), platformCert.getEndValidity());
} }
/** /**
@ -323,11 +332,11 @@ public class CertificateTest {
Certificate platformCert = getTestCertificate( Certificate platformCert = getTestCertificate(
PlatformCredential.class, PlatformCredentialTest.TEST_PLATFORM_CERT_6); PlatformCredential.class, PlatformCredentialTest.TEST_PLATFORM_CERT_6);
assertEquals(platformCert.getAuthorityInfoAccess(), assertEquals("https://trustedservices.intel.com/"
"https://trustedservices.intel.com/" + "content/TSC/certs/TSC_IssuingCAIKGF_TEST.cer\n",
+ "content/TSC/certs/TSC_IssuingCAIKGF_TEST.cer\n"); platformCert.getAuthorityInfoAccess());
assertEquals(platformCert.getAuthorityKeyIdentifier(), assertEquals("a5ecc6c07da02c6af8764d4e5c16483610a0b040",
"a5ecc6c07da02c6af8764d4e5c16483610a0b040"); platformCert.getAuthorityKeyIdentifier());
} }
/** /**
@ -338,16 +347,16 @@ public class CertificateTest {
*/ */
@Test @Test
public void testCertificateTrim() throws IOException, URISyntaxException { public void testCertificateTrim() throws IOException, URISyntaxException {
byte[] rawFileBytes = Files.readAllBytes(Paths.get(CertificateTest.class byte[] rawFileBytes = Files.readAllBytes(Paths.get(Objects.requireNonNull(CertificateTest.class
.getResource(EK_CERT_WITH_PADDED_BYTES).toURI())); .getResource(EK_CERT_WITH_PADDED_BYTES)).toURI()));
byte[] expectedCertBytes = Arrays.copyOfRange(rawFileBytes, 0, 908); byte[] expectedCertBytes = Arrays.copyOfRange(rawFileBytes, 0, 908);
Certificate ekCert = getTestCertificate(EndorsementCredential.class, Certificate ekCert = getTestCertificate(EndorsementCredential.class,
EK_CERT_WITH_PADDED_BYTES); EK_CERT_WITH_PADDED_BYTES);
assertEquals(ekCert.getSerialNumber(), new BigInteger("16842032579184247954")); assertEquals(new BigInteger("16842032579184247954"), ekCert.getSerialNumber());
assertEquals(ekCert.getIssuer(), assertEquals("CN=Nuvoton TPM Root CA 2010+O=Nuvoton Technology Corporation+C=TW",
"CN=Nuvoton TPM Root CA 2010+O=Nuvoton Technology Corporation+C=TW"); ekCert.getIssuer());
assertEquals(ekCert.getSubject(), ""); assertEquals("", ekCert.getSubject());
assertArrayEquals(ekCert.getRawBytes(), expectedCertBytes); assertArrayEquals(expectedCertBytes, ekCert.getRawBytes());
} }
/** /**
@ -360,8 +369,8 @@ public class CertificateTest {
@Test @Test
public void testCertificateTrimThrowsWhenNoLengthFieldFound() throws IOException, public void testCertificateTrimThrowsWhenNoLengthFieldFound() throws IOException,
URISyntaxException { URISyntaxException {
byte[] rawFileBytes = Files.readAllBytes(Paths.get(CertificateTest.class byte[] rawFileBytes = Files.readAllBytes(Paths.get(Objects.requireNonNull(CertificateTest.class
.getResource(EK_CERT_WITH_PADDED_BYTES).toURI())); .getResource(EK_CERT_WITH_PADDED_BYTES)).toURI()));
assertThrows(IllegalArgumentException.class, () -> assertThrows(IllegalArgumentException.class, () ->
new EndorsementCredential(Arrays.copyOfRange(rawFileBytes, 0, 2)), new EndorsementCredential(Arrays.copyOfRange(rawFileBytes, 0, 2)),
".* No certificate length field could be found\\."); ".* No certificate length field could be found\\.");
@ -377,8 +386,8 @@ public class CertificateTest {
@Test @Test
public void testCertificateTrimThrowsWhenOnlyASN1Sequence() throws IOException, public void testCertificateTrimThrowsWhenOnlyASN1Sequence() throws IOException,
URISyntaxException { URISyntaxException {
byte[] rawFileBytes = Files.readAllBytes(Paths.get(CertificateTest.class byte[] rawFileBytes = Files.readAllBytes(Paths.get(Objects.requireNonNull(CertificateTest.class
.getResource(EK_CERT_WITH_PADDED_BYTES).toURI())); .getResource(EK_CERT_WITH_PADDED_BYTES)).toURI()));
assertThrows(IllegalArgumentException.class, () -> assertThrows(IllegalArgumentException.class, () ->
new EndorsementCredential(Arrays.copyOfRange(rawFileBytes, 0, 4)), new EndorsementCredential(Arrays.copyOfRange(rawFileBytes, 0, 4)),
".* Certificate is nothing more than ASN.1 Sequence\\\\."); ".* Certificate is nothing more than ASN.1 Sequence\\\\.");
@ -394,8 +403,8 @@ public class CertificateTest {
@Test @Test
public void testCertificateTrimThrowsWhenLengthIsTooLarge() throws IOException, public void testCertificateTrimThrowsWhenLengthIsTooLarge() throws IOException,
URISyntaxException { URISyntaxException {
byte[] rawFileBytes = Files.readAllBytes(Paths.get(CertificateTest.class byte[] rawFileBytes = Files.readAllBytes(Paths.get(Objects.requireNonNull(CertificateTest.class
.getResource(EK_CERT_WITH_PADDED_BYTES).toURI())); .getResource(EK_CERT_WITH_PADDED_BYTES)).toURI()));
assertThrows(IllegalArgumentException.class, () -> assertThrows(IllegalArgumentException.class, () ->
new EndorsementCredential(Arrays.copyOfRange(rawFileBytes, 0, 42)), new EndorsementCredential(Arrays.copyOfRange(rawFileBytes, 0, 42)),
".* Value of certificate length field extends beyond" ".* Value of certificate length field extends beyond"
@ -419,11 +428,11 @@ public class CertificateTest {
assertEquals( assertEquals(
new CertificateAuthorityCredential( new CertificateAuthorityCredential(
Paths.get(this.getClass().getResource(FAKE_ROOT_CA_FILE).toURI()) Paths.get(Objects.requireNonNull(this.getClass().getResource(FAKE_ROOT_CA_FILE)).toURI())
), ),
new CertificateAuthorityCredential( new CertificateAuthorityCredential(
Files.readAllBytes( Files.readAllBytes(
Paths.get(this.getClass().getResource(FAKE_ROOT_CA_FILE).toURI()) Paths.get(Objects.requireNonNull(this.getClass().getResource(FAKE_ROOT_CA_FILE)).toURI())
) )
) )
); );
@ -439,9 +448,9 @@ public class CertificateTest {
); );
assertNotEquals( assertNotEquals(
getTestCertificate(CertificateAuthorityCredential.class, FAKE_ROOT_CA_FILE), null,
null getTestCertificate(CertificateAuthorityCredential.class, FAKE_ROOT_CA_FILE)
); );
} }
/** /**
@ -459,7 +468,7 @@ public class CertificateTest {
Certificate issuerCert = getTestCertificate(FAKE_ROOT_CA_FILE); Certificate issuerCert = getTestCertificate(FAKE_ROOT_CA_FILE);
Certificate cert = getTestCertificate(INT_CA_CERT02); Certificate cert = getTestCertificate(INT_CA_CERT02);
assertEquals(issuerCert.isIssuer(cert), "Certificate signature failed to verify"); assertEquals("Certificate signature failed to verify", issuerCert.isIssuer(cert));
assertTrue(cert.isIssuer(issuerCert).isEmpty()); assertTrue(cert.isIssuer(issuerCert).isEmpty());
} }
@ -480,11 +489,11 @@ public class CertificateTest {
assertEquals( assertEquals(
new CertificateAuthorityCredential( new CertificateAuthorityCredential(
Paths.get(this.getClass().getResource(FAKE_ROOT_CA_FILE).toURI()) Paths.get(Objects.requireNonNull(this.getClass().getResource(FAKE_ROOT_CA_FILE)).toURI())
).hashCode(), ).hashCode(),
new CertificateAuthorityCredential( new CertificateAuthorityCredential(
Files.readAllBytes( Files.readAllBytes(
Paths.get(this.getClass().getResource(FAKE_ROOT_CA_FILE).toURI()) Paths.get(Objects.requireNonNull(this.getClass().getResource(FAKE_ROOT_CA_FILE)).toURI())
) )
).hashCode() ).hashCode()
); );
@ -546,7 +555,7 @@ public class CertificateTest {
Path certPath; Path certPath;
try { try {
certPath = Paths.get(CertificateTest.class.getResource(filename).toURI()); certPath = Paths.get(Objects.requireNonNull(CertificateTest.class.getResource(filename)).toURI());
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new IOException("Could not resolve path URI", e); throw new IOException("Could not resolve path URI", e);
} }
@ -595,7 +604,7 @@ public class CertificateTest {
} }
try (FileInputStream certInputStream = new FileInputStream( try (FileInputStream certInputStream = new FileInputStream(
Paths.get(CertificateTest.class.getResource(resourceName).toURI()).toFile() Paths.get(Objects.requireNonNull(CertificateTest.class.getResource(resourceName)).toURI()).toFile()
)) { )) {
return (X509Certificate) cf.generateCertificate(certInputStream); return (X509Certificate) cf.generateCertificate(certInputStream);
} catch (CertificateException | URISyntaxException e) { } catch (CertificateException | URISyntaxException e) {