diff --git a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/SpringPersistenceTest.java b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/SpringPersistenceTest.java deleted file mode 100644 index c6ad3f33..00000000 --- a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/SpringPersistenceTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package hirs.attestationca.persist; - -import hirs.attestationca.persist.PersistenceConfiguration; -import org.hibernate.SessionFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; - -/** - * Base class that autowires a session factory for use of - * any tests that need a database connection. - */ -@ContextConfiguration(classes = PersistenceConfiguration.class) -@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) -public class SpringPersistenceTest extends AbstractJUnit4SpringContextTests { - - /** - * Autowired session factory. - */ - @SuppressWarnings("checkstyle:visibilitymodifier") - @Autowired - protected SessionFactory sessionFactory; -} \ No newline at end of file diff --git a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/record/TPMMeasurementRecordTest.java b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/record/TPMMeasurementRecordTest.java new file mode 100644 index 00000000..f8ba83e6 --- /dev/null +++ b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/record/TPMMeasurementRecordTest.java @@ -0,0 +1,231 @@ +package hirs.attestationca.persist.entity.userdefined.record; + +import hirs.utils.digest.Digest; +import hirs.utils.digest.DigestAlgorithm; +import org.apache.commons.codec.DecoderException; +import org.apache.commons.codec.binary.Hex; +import org.junit.jupiter.api.Test; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * PCRMeasurementRecordTest represents a unit test class for + * PCRMeasurementRecord. + */ +public class TPMMeasurementRecordTest { + + private static final Logger LOGGER + = LogManager.getLogger(TPMMeasurementRecordTest.class); + private static final int DEFAULT_PCR_ID = 3; + private static final String DEFAULT_HASH = + "3d5f3c2f7f3003d2e4baddc46ed4763a4954f648"; + + /** + * Tests instantiation of new PCRMeasurementRecord. + */ + @Test + public final void tpmMeasurementRecord() { + TPMMeasurementRecord pcrRecord = new TPMMeasurementRecord(0, + getDigest(DEFAULT_HASH)); + assertNotNull(pcrRecord); + } + + /** + * Tests that PCRMeasurementRecord constructor throws a + * NullPointerException with null hash. + */ + @Test + public final void tpmMeasurementRecordNullHash() { + Digest digest = null; + assertThrows(NullPointerException.class, () -> + new TPMMeasurementRecord(0, digest)); + } + + /** + * Tests that PCRMeasurementRecord constructor throws a + * IllegalArgumentException with negative value for pcr id. + */ + @Test + public final void tpmMeasurementRecordNegativePcrId() { + assertThrows(IllegalArgumentException.class, () -> + new TPMMeasurementRecord(-1, getDigest(DEFAULT_HASH))); + } + + /** + * Tests that PCRMeasurementRecord constructor throws a + * IllegalArgumentException with pcr id greater than 23. + */ + @Test + public final void tpmMeasurementRecordInvalidPcrId() { + final int invalidPCR = 24; + assertThrows(IllegalArgumentException.class, () -> + new TPMMeasurementRecord(invalidPCR, getDigest(DEFAULT_HASH))); + } + + /** + * Tests that getHash() returns the measurement hash. + */ + @Test + public final void getHash() { + TPMMeasurementRecord pcrRecord = new TPMMeasurementRecord(0, + getDigest(DEFAULT_HASH)); + assertNotNull(pcrRecord.getHash()); + } + + /** + * Tests that getPcrId() returns the pcr id. + */ + @Test + public final void getPcrId() { + int id; + TPMMeasurementRecord pcrRecord = new TPMMeasurementRecord(0, + getDigest(DEFAULT_HASH)); + id = pcrRecord.getPcrId(); + assertNotNull(id); + } + + /** + * Tests that two IMAMeasurementRecords are equal if they have + * the same name and the same path. + */ + @Test + public final void testEquals() { + TPMMeasurementRecord r1 = getDefaultRecord(); + TPMMeasurementRecord r2 = getDefaultRecord(); + assertEquals(r1, r2); + assertEquals(r2, r1); + assertEquals(r1, r1); + assertEquals(r2, r2); + } + + /** + * Tests that two TPMMeasurementRecords are not equal if the + * PCR IDs are different. + */ + @Test + public final void testNotEqualsPcr() { + final int pcrId = 5; + TPMMeasurementRecord r1 = getDefaultRecord(); + TPMMeasurementRecord r2 = new TPMMeasurementRecord(pcrId, + getDigest(DEFAULT_HASH)); + assertNotEquals(r1, r2); + assertNotEquals(r2, r1); + assertEquals(r1, r1); + assertEquals(r2, r2); + } + + /** + * Tests that two TPMMeasurementRecords are not equal if the + * hashes are different. + */ + @Test + public final void testNotEqualsHash() { + final String hash = "aacc3c2f7f3003d2e4baddc46ed4763a4954f648"; + TPMMeasurementRecord r1 = getDefaultRecord(); + TPMMeasurementRecord r2 = + new TPMMeasurementRecord(DEFAULT_PCR_ID, getDigest(hash)); + assertNotEquals(r1, r2); + assertNotEquals(r2, r1); + assertEquals(r1, r1); + assertEquals(r2, r2); + } + + /** + * Tests that the hash code of two TPMMeasurementRecords are + * the same. + */ + @Test + public final void testHashCodeEquals() { + TPMMeasurementRecord r1 = getDefaultRecord(); + TPMMeasurementRecord r2 = getDefaultRecord(); + assertEquals(r1.hashCode(), r2.hashCode()); + assertEquals(r2.hashCode(), r1.hashCode()); + assertEquals(r1.hashCode(), r1.hashCode()); + assertEquals(r2.hashCode(), r2.hashCode()); + } + + /** + * Tests that the hash code of two TPMBaselineRecords is + * different if they have different names. + */ + @Test + public final void testHashCodeNotEqualsPcrs() { + final int pcrId = 5; + TPMMeasurementRecord r1 = getDefaultRecord(); + TPMMeasurementRecord r2 = new TPMMeasurementRecord(pcrId, + getDigest(DEFAULT_HASH)); + assertNotEquals(r1.hashCode(), r2.hashCode()); + assertNotEquals(r2.hashCode(), r1.hashCode()); + assertEquals(r1.hashCode(), r1.hashCode()); + assertEquals(r2.hashCode(), r2.hashCode()); + } + + /** + * Tests that the hash code of two TPMMeasurementRecords is + * different if they have different hashes. + */ + @Test + public final void testHashCodeNotEqualsHashes() { + final String hash = "aacc3c2f7f3003d2e4baddc46ed4763a4954f648"; + TPMMeasurementRecord r1 = getDefaultRecord(); + TPMMeasurementRecord r2 = + new TPMMeasurementRecord(DEFAULT_PCR_ID, getDigest(hash)); + assertNotEquals(r1.hashCode(), r2.hashCode()); + assertNotEquals(r2.hashCode(), r1.hashCode()); + assertEquals(r1.hashCode(), r1.hashCode()); + assertEquals(r2.hashCode(), r2.hashCode()); + } + + /** + * Tests that the expected valid PCR IDs do not throw an IllegalArgumentException. + */ + @Test + public final void testCheckForValidPcrId() { + final int minPcrId = TPMMeasurementRecord.MIN_PCR_ID; + final int maxPcrId = TPMMeasurementRecord.MAX_PCR_ID; + for (int i = minPcrId; i < maxPcrId; i++) { + TPMMeasurementRecord.checkForValidPcrId(i); + } + } + + /** + * Tests that a negative PCR ID throws an IllegalArgumentException. + */ + @Test + public final void testCheckForValidPcrIdNegative() { + final int pcrId = -1; + assertThrows(IllegalArgumentException.class, () -> + TPMMeasurementRecord.checkForValidPcrId(pcrId)); + } + + /** + * Tests that a high invalid PCR ID throws an IllegalArgumentException. + */ + @Test + public final void testCheckForValidPcrIdInvalidId() { + final int pcrId = 35; + assertThrows(IllegalArgumentException.class, () -> + TPMMeasurementRecord.checkForValidPcrId(pcrId)); + } + + private TPMMeasurementRecord getDefaultRecord() { + return new TPMMeasurementRecord(DEFAULT_PCR_ID, + getDigest(DEFAULT_HASH)); + } + + private Digest getDigest(final String hash) { + try { + final byte[] bytes = Hex.decodeHex(hash.toCharArray()); + return new Digest(DigestAlgorithm.SHA1, bytes); + } catch (DecoderException e) { + LOGGER.error("unable to create digest", e); + throw new RuntimeException("unable to create digest", e); + } + } +} diff --git a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/report/DeviceInfoReportTest.java b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/report/DeviceInfoReportTest.java index 3e470e8b..d30b8b88 100644 --- a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/report/DeviceInfoReportTest.java +++ b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/report/DeviceInfoReportTest.java @@ -1,6 +1,5 @@ package hirs.attestationca.persist.entity.userdefined.report; -import hirs.attestationca.persist.SpringPersistenceTest; import hirs.attestationca.persist.entity.userdefined.info.OSInfo; import hirs.attestationca.persist.entity.userdefined.info.TPMInfo; import hirs.attestationca.persist.entity.userdefined.info.NetworkInfo; @@ -24,7 +23,7 @@ import java.security.cert.X509Certificate; /** * DeviceInfoReportTest is a unit test class for DeviceInfoReports. */ -public class DeviceInfoReportTest extends SpringPersistenceTest { +public class DeviceInfoReportTest { private final NetworkInfo networkInfo = createTestNetworkInfo(); private final OSInfo osInfo = createTestOSInfo(); private final FirmwareInfo firmwareInfo = createTestFirmwareInfo();