Removing SpringPersistenceTest after concluding it is unnecessary; Modifying DeviceInfoReportTest after removal of SpringPersistenceTest. Adding TPMMeasurementRecordTest

This commit is contained in:
iadgovuser62 2024-01-10 14:37:35 -05:00
parent 412ac2bb75
commit 2b594571b5
3 changed files with 232 additions and 26 deletions

View File

@ -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;
}

View File

@ -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;
/**
* <code>PCRMeasurementRecordTest</code> represents a unit test class for
* <code>PCRMeasurementRecord</code>.
*/
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 <code>PCRMeasurementRecord</code>.
*/
@Test
public final void tpmMeasurementRecord() {
TPMMeasurementRecord pcrRecord = new TPMMeasurementRecord(0,
getDigest(DEFAULT_HASH));
assertNotNull(pcrRecord);
}
/**
* Tests that <code>PCRMeasurementRecord</code> constructor throws a
* NullPointerException with null hash.
*/
@Test
public final void tpmMeasurementRecordNullHash() {
Digest digest = null;
assertThrows(NullPointerException.class, () ->
new TPMMeasurementRecord(0, digest));
}
/**
* Tests that <code>PCRMeasurementRecord</code> 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 <code>PCRMeasurementRecord</code> 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 <code>getHash()</code> returns the measurement hash.
*/
@Test
public final void getHash() {
TPMMeasurementRecord pcrRecord = new TPMMeasurementRecord(0,
getDigest(DEFAULT_HASH));
assertNotNull(pcrRecord.getHash());
}
/**
* Tests that <code>getPcrId()</code> 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 <code>IMAMeasurementRecord</code>s 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 <code>TPMMeasurementRecord</code>s 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 <code>TPMMeasurementRecord</code>s 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 <code>TPMMeasurementRecord</code>s 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 <code>TPMBaselineRecord</code>s 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 <code>TPMMeasurementRecord</code>s 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);
}
}
}

View File

@ -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();