From df83fb6064e1675fc5cbf60f77d95c494c9f5cea Mon Sep 17 00:00:00 2001 From: iadgovuser62 Date: Fri, 29 Dec 2023 06:53:09 -0500 Subject: [PATCH 1/6] Adding SpringPersistenceTest and necessary implementation in HIRS_AttestationCA build.gradle file --- HIRS_AttestationCA/build.gradle | 1 + .../persist/SpringPersistenceTest.java | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/SpringPersistenceTest.java diff --git a/HIRS_AttestationCA/build.gradle b/HIRS_AttestationCA/build.gradle index 893997d90..6c927d93e 100644 --- a/HIRS_AttestationCA/build.gradle +++ b/HIRS_AttestationCA/build.gradle @@ -29,6 +29,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.0.1' implementation 'com.github.darrachequesne:spring-data-jpa-datatables:6.0.1' implementation 'org.springframework.retry:spring-retry:2.0.0' + implementation 'org.springframework:spring-test:6.0.11' implementation libs.springdatajpa implementation libs.bouncycastle diff --git a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/SpringPersistenceTest.java b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/SpringPersistenceTest.java new file mode 100644 index 000000000..c6ad3f338 --- /dev/null +++ b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/SpringPersistenceTest.java @@ -0,0 +1,24 @@ +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 From 91abb7b9a50ad427c01c2a1ac3a9f3a70134906c Mon Sep 17 00:00:00 2001 From: iadgovuser62 Date: Fri, 29 Dec 2023 06:57:27 -0500 Subject: [PATCH 2/6] Adding DeviceInfoReportTest and necessary resource file --- .../report/DeviceInfoReportTest.java | 211 ++++++++++++++++++ HIRS_AttestationCA/src/test/resources/VERSION | 1 + 2 files changed, 212 insertions(+) create mode 100644 HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/report/DeviceInfoReportTest.java create mode 100644 HIRS_AttestationCA/src/test/resources/VERSION 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 new file mode 100644 index 000000000..3e470e8bc --- /dev/null +++ b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/report/DeviceInfoReportTest.java @@ -0,0 +1,211 @@ +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; +import hirs.attestationca.persist.entity.userdefined.info.HardwareInfo; +import hirs.attestationca.persist.entity.userdefined.info.FirmwareInfo; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; + +/** + * DeviceInfoReportTest is a unit test class for DeviceInfoReports. + */ +public class DeviceInfoReportTest extends SpringPersistenceTest { + private final NetworkInfo networkInfo = createTestNetworkInfo(); + private final OSInfo osInfo = createTestOSInfo(); + private final FirmwareInfo firmwareInfo = createTestFirmwareInfo(); + private final HardwareInfo hardwareInfo = createTestHardwareInfo(); + private final TPMInfo tpmInfo = createTPMInfo(); + private static final String TEST_IDENTITY_CERT = "/tpm/sample_identity_cert.cer"; + + private static final Logger LOGGER = LogManager.getLogger(DeviceInfoReportTest.class); + + private static final String EXPECTED_CLIENT_VERSION = "Test.Version"; + + /** + * Tests instantiation of a DeviceInfoReport. + */ + @Test + public final void deviceInfoReport() { + new DeviceInfoReport(networkInfo, osInfo, firmwareInfo, hardwareInfo, tpmInfo); + } + + /** + * Tests that NetworkInfo cannot be null. + */ + @Test + public final void networkInfoNull() { + assertThrows(NullPointerException.class, () -> + new DeviceInfoReport(null, osInfo, firmwareInfo, hardwareInfo, tpmInfo)); + } + + /** + * Tests that OSInfo cannot be null. + */ + @Test + public final void osInfoNull() { + assertThrows(NullPointerException.class, () -> + new DeviceInfoReport(networkInfo, null, firmwareInfo, hardwareInfo, tpmInfo)); + } + + /** + * Tests that FirmwareInfo cannot be null. + */ + @Test + public final void firmwareInfoNull() { + assertThrows(NullPointerException.class, () -> + new DeviceInfoReport(networkInfo, osInfo, null, hardwareInfo, tpmInfo)); + } + + /** + * Tests that HardwareInfo cannot be null. + */ + @Test + public final void hardwareInfoNull() { + assertThrows(NullPointerException.class, () -> + new DeviceInfoReport(networkInfo, osInfo, firmwareInfo, null, tpmInfo)); + } + + /** + * Tests that TPMInfo may be null. + */ + @Test + public final void tpmInfoNull() { + new DeviceInfoReport(networkInfo, osInfo, firmwareInfo, hardwareInfo, null); + } + + /** + * Tests that the getters for DeviceInfoReport work as expected. + */ + @Test + public final void testGetters() { + DeviceInfoReport deviceInfoReport = + new DeviceInfoReport(networkInfo, osInfo, firmwareInfo, hardwareInfo, tpmInfo); + assertEquals(deviceInfoReport.getNetworkInfo(), networkInfo); + assertEquals(deviceInfoReport.getOSInfo(), osInfo); + assertEquals(deviceInfoReport.getFirmwareInfo(), firmwareInfo); + assertEquals(deviceInfoReport.getHardwareInfo(), hardwareInfo); + assertEquals(deviceInfoReport.getTpmInfo(), tpmInfo); + assertEquals(deviceInfoReport.getClientApplicationVersion(), + EXPECTED_CLIENT_VERSION); + } + + /** + * Creates a DeviceInfoReport instance usable for testing. + * + * @return a test DeviceInfoReport + */ + public static DeviceInfoReport getTestReport() { + return new DeviceInfoReport( + createTestNetworkInfo(), createTestOSInfo(), createTestFirmwareInfo(), + createTestHardwareInfo(), createTPMInfo() + ); + } + + /** + * Creates a test instance of NetworkInfo. + * + * @return network information for a fake device + */ + public static NetworkInfo createTestNetworkInfo() { + try { + final String hostname = "test.hostname"; + final InetAddress ipAddress = + InetAddress.getByAddress(new byte[] {127, 0, 0, 1}); + final byte[] macAddress = new byte[] {11, 22, 33, 44, 55, 66}; + return new NetworkInfo(hostname, ipAddress, macAddress); + + } catch (UnknownHostException e) { + LOGGER.error("error occurred while creating InetAddress"); + return null; + } + + } + + /** + * Creates a test instance of OSInfo. + * + * @return OS information for a fake device + */ + public static OSInfo createTestOSInfo() { + return new OSInfo("test os name", "test os version", "test os arch", + "test distribution", "test distribution release"); + } + + /** + * Creates a test instance of FirmwareInfo. + * + * @return Firmware information for a fake device + */ + public static FirmwareInfo createTestFirmwareInfo() { + return new FirmwareInfo("test bios vendor", "test bios version", "test bios release date"); + } + + /** + * Creates a test instance of HardwareInfo. + * + * @return Hardware information for a fake device + */ + public static HardwareInfo createTestHardwareInfo() { + return new HardwareInfo("test manufacturer", "test product name", "test version", + "test really long serial number with many characters", "test really long chassis " + + "serial number with many characters", + "test really long baseboard serial number with many characters"); + } + + /** + * Creates a test instance of TPMInfo. + * + * @return TPM information for a fake device + */ + public static final TPMInfo createTPMInfo() { + final short num1 = 1; + final short num2 = 2; + final short num3 = 3; + final short num4 = 4; + return new TPMInfo("test os make", num1, num2, num3, num4, + getTestIdentityCertificate()); + } + + private static X509Certificate getTestIdentityCertificate() { + X509Certificate certificateValue = null; + InputStream istream = null; + istream = DeviceInfoReportTest.class.getResourceAsStream( + TEST_IDENTITY_CERT + ); + try { + if (istream == null) { + throw new FileNotFoundException(TEST_IDENTITY_CERT); + } + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + certificateValue = (X509Certificate) cf.generateCertificate( + istream); + + } catch (Exception e) { + return null; + } finally { + if (istream != null) { + try { + istream.close(); + } catch (IOException e) { + LOGGER.error("test certificate file could not be closed"); + } + } + } + return certificateValue; + } +} diff --git a/HIRS_AttestationCA/src/test/resources/VERSION b/HIRS_AttestationCA/src/test/resources/VERSION new file mode 100644 index 000000000..b20b9417e --- /dev/null +++ b/HIRS_AttestationCA/src/test/resources/VERSION @@ -0,0 +1 @@ +Test.Version From 412ac2bb75c2cd86fb86502646289e94402a5b3b Mon Sep 17 00:00:00 2001 From: iadgovuser62 Date: Fri, 29 Dec 2023 06:59:06 -0500 Subject: [PATCH 3/6] Adding DeviceTest --- .../entity/userdefined/DeviceTest.java | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java diff --git a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java new file mode 100644 index 000000000..958edeae1 --- /dev/null +++ b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java @@ -0,0 +1,154 @@ +package hirs.attestationca.persist.entity.userdefined; + +import hirs.attestationca.persist.entity.userdefined.report.DeviceInfoReport; +import hirs.attestationca.persist.entity.userdefined.report.DeviceInfoReportTest; +import hirs.attestationca.persist.enums.AppraisalStatus; +import hirs.attestationca.persist.enums.HealthStatus; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * This is the test class for the Device class. + * + */ +public final class DeviceTest { + /** + * Utility method for getting a Device that can be used for + * testing. + * + * @param name name for the Device + * + * @throws Exception in case there are errors getting a report + * + * @return device + */ + public static Device getTestDevice(final String name) throws Exception { + final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); + return new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + } + + /** + * Tests that the device constructor can take a name. + */ + @Test + public void testDevice() { + final String name = "my-laptop"; + final Device device = new Device(name, null, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null , null); + assertNotNull(device); + } + + /** + * Tests that a name and device info report can be passed into the + * constructor. + * + * @throws Exception + * in case there are errors getting a report + * + */ + @Test + public void testDeviceNameAndInfo() throws Exception { + final String name = "my-laptop"; + final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); + new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + } + + /** + * Tests that the device name can be supplied and device info be null. + */ + @Test + public void testDeviceNameAndNullInfo() { + final String name = "my-laptop"; + final DeviceInfoReport deviceInfo = null; + new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + } + + /** + * Tests that get device info report returns the device info report. + * + * @throws Exception + * in case there are errors getting a report + */ + @Test + public void testGetDeviceInfo() throws Exception { + final String name = "my-laptop"; + final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); + final Device device = new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + assertEquals(device.getDeviceInfo(), deviceInfo); + } + + /** + * Tests that device info can be set. + * + * @throws Exception + * in case there are errors getting a report + */ + @Test + public void testSetDeviceInfo() throws Exception { + final String name = "my-laptop"; + final Device device = new Device(name, null, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + assertNull(device.getDeviceInfo()); + final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); + device.setDeviceInfo(deviceInfo); + assertEquals(device.getDeviceInfo(), deviceInfo); + } + + /** + * Tests that get device info report returns the device info report. + * + * @throws Exception + * in case there are errors getting a report + */ + @Test + public void testSetNullDeviceInfo() throws Exception { + final String name = "my-laptop"; + final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); + final Device device = new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + assertEquals(device.getDeviceInfo(), deviceInfo); + device.setDeviceInfo(null); + assertNull(device.getDeviceInfo()); + } + + /** + * Tests that retrieving a null LastReportTimestamp will not trigger an exception. + * + * @throws Exception + * In case there is an error getting a report + */ + @Test + public void testNullLastReportTimeStamp() throws Exception { + final String name = "my-laptop"; + final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); + final Device device = new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + assertNull(device.getLastReportTimestamp()); + //Successful if test does not throw Exception + } + + /** + * Tests that setting and getting the health status works correctly. + */ + @Test + public void testSetHealthStatus() { + final Device device = new Device("test-device", null, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + device.setHealthStatus(HealthStatus.TRUSTED); + assertEquals(HealthStatus.TRUSTED, device.getHealthStatus()); + } + + /** + * Tests equals returns true for two devices that have the same name. + * + * @throws Exception + * in case there are errors getting a report + */ + @Test + public void testDeviceEquals() throws Exception { + final String name = "my-laptop"; + final String otherName = "my-laptop"; + final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); + final Device device = new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + final Device other = new Device(otherName, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + assertEquals(device, other); + } +} From 2b594571b5eb378ade6d377eba8e753d1ecbd402 Mon Sep 17 00:00:00 2001 From: iadgovuser62 Date: Wed, 10 Jan 2024 14:37:35 -0500 Subject: [PATCH 4/6] Removing SpringPersistenceTest after concluding it is unnecessary; Modifying DeviceInfoReportTest after removal of SpringPersistenceTest. Adding TPMMeasurementRecordTest --- .../persist/SpringPersistenceTest.java | 24 -- .../record/TPMMeasurementRecordTest.java | 231 ++++++++++++++++++ .../report/DeviceInfoReportTest.java | 3 +- 3 files changed, 232 insertions(+), 26 deletions(-) delete mode 100644 HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/SpringPersistenceTest.java create mode 100644 HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/record/TPMMeasurementRecordTest.java 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 c6ad3f338..000000000 --- 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 000000000..f8ba83e6f --- /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 3e470e8bc..d30b8b880 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 @@ /** * 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(); From 09284caa571e7d00c83c4c42683bd941c871d3de Mon Sep 17 00:00:00 2001 From: iadgovuser62 Date: Wed, 10 Jan 2024 14:57:56 -0500 Subject: [PATCH 5/6] Undoing addition to HIRS_AttestationCA build.gradle file; Adding tests to DeviceTest --- HIRS_AttestationCA/build.gradle | 1 - .../entity/userdefined/DeviceTest.java | 23 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/HIRS_AttestationCA/build.gradle b/HIRS_AttestationCA/build.gradle index 6c927d93e..893997d90 100644 --- a/HIRS_AttestationCA/build.gradle +++ b/HIRS_AttestationCA/build.gradle @@ -29,7 +29,6 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.0.1' implementation 'com.github.darrachequesne:spring-data-jpa-datatables:6.0.1' implementation 'org.springframework.retry:spring-retry:2.0.0' - implementation 'org.springframework:spring-test:6.0.11' implementation libs.springdatajpa implementation libs.bouncycastle diff --git a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java index 958edeae1..e04e369de 100644 --- a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java +++ b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java @@ -151,4 +151,27 @@ public void testDeviceEquals() throws Exception { final Device other = new Device(otherName, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); assertEquals(device, other); } + + /** + * Tests that the default setting of the supply chain validation status is unknown. + */ + @Test + public void testGetDefaultSupplyChainStatus() { + String name = "my-laptop"; + DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); + final Device device = new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + assertEquals(AppraisalStatus.Status.UNKNOWN, device.getSupplyChainValidationStatus()); + } + + /** + * Tests that the supply chain validation status getters and setters work. + */ + @Test + public void testSetAndGetSupplyChainStatus() { + String name = "my-laptop"; + DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); + final Device device = new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); + device.setSupplyChainValidationStatus(AppraisalStatus.Status.PASS); + assertEquals(AppraisalStatus.Status.PASS, device.getSupplyChainValidationStatus()); + } } From 18a8f4269907454ed523cb1e4bf7edd02586ef13 Mon Sep 17 00:00:00 2001 From: iadgovuser62 Date: Thu, 11 Jan 2024 11:35:50 -0500 Subject: [PATCH 6/6] Fixed expected/actual order in tests, added ExaminableRecord tests to TPMMeasurementRecordTest --- .../entity/userdefined/DeviceTest.java | 42 ++++----------- .../record/TPMMeasurementRecordTest.java | 53 +++++++++++++++++++ .../report/DeviceInfoReportTest.java | 13 +++-- 3 files changed, 69 insertions(+), 39 deletions(-) diff --git a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java index e04e369de..9363281e9 100644 --- a/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java +++ b/HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/entity/userdefined/DeviceTest.java @@ -21,11 +21,9 @@ public final class DeviceTest { * * @param name name for the Device * - * @throws Exception in case there are errors getting a report - * * @return device */ - public static Device getTestDevice(final String name) throws Exception { + public static Device getTestDevice(final String name) { final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); return new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); } @@ -43,13 +41,9 @@ public void testDevice() { /** * Tests that a name and device info report can be passed into the * constructor. - * - * @throws Exception - * in case there are errors getting a report - * */ @Test - public void testDeviceNameAndInfo() throws Exception { + public void testDeviceNameAndInfo() { final String name = "my-laptop"; final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); @@ -67,63 +61,50 @@ public void testDeviceNameAndNullInfo() { /** * Tests that get device info report returns the device info report. - * - * @throws Exception - * in case there are errors getting a report */ @Test - public void testGetDeviceInfo() throws Exception { + public void testGetDeviceInfo() { final String name = "my-laptop"; final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); final Device device = new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); - assertEquals(device.getDeviceInfo(), deviceInfo); + assertEquals(deviceInfo, device.getDeviceInfo()); } /** * Tests that device info can be set. - * - * @throws Exception - * in case there are errors getting a report */ @Test - public void testSetDeviceInfo() throws Exception { + public void testSetDeviceInfo() { final String name = "my-laptop"; final Device device = new Device(name, null, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); assertNull(device.getDeviceInfo()); final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); device.setDeviceInfo(deviceInfo); - assertEquals(device.getDeviceInfo(), deviceInfo); + assertEquals(deviceInfo, device.getDeviceInfo()); } /** * Tests that get device info report returns the device info report. - * - * @throws Exception - * in case there are errors getting a report */ @Test - public void testSetNullDeviceInfo() throws Exception { + public void testSetNullDeviceInfo() { final String name = "my-laptop"; final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); final Device device = new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); - assertEquals(device.getDeviceInfo(), deviceInfo); + assertEquals(deviceInfo, device.getDeviceInfo()); device.setDeviceInfo(null); assertNull(device.getDeviceInfo()); } /** * Tests that retrieving a null LastReportTimestamp will not trigger an exception. - * - * @throws Exception - * In case there is an error getting a report */ @Test - public void testNullLastReportTimeStamp() throws Exception { + public void testNullLastReportTimeStamp() { final String name = "my-laptop"; final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); final Device device = new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null); assertNull(device.getLastReportTimestamp()); - //Successful if test does not throw Exception } /** @@ -138,12 +119,9 @@ public void testSetHealthStatus() { /** * Tests equals returns true for two devices that have the same name. - * - * @throws Exception - * in case there are errors getting a report */ @Test - public void testDeviceEquals() throws Exception { + public void testDeviceEquals() { final String name = "my-laptop"; final String otherName = "my-laptop"; final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport(); 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 index f8ba83e6f..d77c23fda 100644 --- 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 @@ -1,5 +1,6 @@ package hirs.attestationca.persist.entity.userdefined.record; +import hirs.attestationca.persist.entity.userdefined.ExaminableRecord; import hirs.utils.digest.Digest; import hirs.utils.digest.DigestAlgorithm; import org.apache.commons.codec.DecoderException; @@ -25,6 +26,7 @@ public class TPMMeasurementRecordTest { private static final int DEFAULT_PCR_ID = 3; private static final String DEFAULT_HASH = "3d5f3c2f7f3003d2e4baddc46ed4763a4954f648"; + private static final ExaminableRecord.ExamineState DEFAULT_STATE = ExaminableRecord.ExamineState.UNEXAMINED; /** * Tests instantiation of new PCRMeasurementRecord. @@ -90,6 +92,15 @@ public final void getPcrId() { assertNotNull(id); } + /** + * Tests that getExamineState returns the correct state. + */ + @Test + public final void getExamineState() { + final TPMMeasurementRecord record = getDefaultRecord(); + assertEquals(DEFAULT_STATE, record.getExamineState()); + } + /** * Tests that two IMAMeasurementRecords are equal if they have * the same name and the same path. @@ -214,6 +225,48 @@ public final void testCheckForValidPcrIdInvalidId() { TPMMeasurementRecord.checkForValidPcrId(pcrId)); } + /** + * Tests that the ExamineState can be successfully set to EXAMINED. + */ + @Test + public final void testSetExamineStateExamined() { + final ExaminableRecord.ExamineState state = ExaminableRecord.ExamineState.EXAMINED; + TPMMeasurementRecord r1 = getDefaultRecord(); + r1.setExamineState(state); + assertEquals(state, r1.getExamineState()); + } + + /** + * Tests that the ExamineState can be successfully set to IGNORED. + */ + @Test + public final void testSetExamineStateIgnored() { + final ExaminableRecord.ExamineState state = ExaminableRecord.ExamineState.IGNORED; + TPMMeasurementRecord r1 = getDefaultRecord(); + r1.setExamineState(state); + assertEquals(state, r1.getExamineState()); + } + + /** + * Tests that the ExamineState is successfully initialized to UNEXAMINED. + */ + @Test + public final void testSetExamineStateInitial() { + TPMMeasurementRecord r1 = getDefaultRecord(); + assertEquals(ExaminableRecord.ExamineState.UNEXAMINED, r1.getExamineState()); + } + + /** + * Tests that setting the ExamineState to UNEXAMINED throws an IllegalArgumentException. + */ + @Test + public final void testSetExamineStateUnexamined() { + final ExaminableRecord.ExamineState state = ExaminableRecord.ExamineState.UNEXAMINED; + TPMMeasurementRecord r1 = getDefaultRecord(); + assertThrows(IllegalArgumentException.class, () -> + r1.setExamineState(state)); + } + private TPMMeasurementRecord getDefaultRecord() { return new TPMMeasurementRecord(DEFAULT_PCR_ID, getDigest(DEFAULT_HASH)); 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 d30b8b880..a51f94da1 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 @@ -94,13 +94,12 @@ public final void tpmInfoNull() { public final void testGetters() { DeviceInfoReport deviceInfoReport = new DeviceInfoReport(networkInfo, osInfo, firmwareInfo, hardwareInfo, tpmInfo); - assertEquals(deviceInfoReport.getNetworkInfo(), networkInfo); - assertEquals(deviceInfoReport.getOSInfo(), osInfo); - assertEquals(deviceInfoReport.getFirmwareInfo(), firmwareInfo); - assertEquals(deviceInfoReport.getHardwareInfo(), hardwareInfo); - assertEquals(deviceInfoReport.getTpmInfo(), tpmInfo); - assertEquals(deviceInfoReport.getClientApplicationVersion(), - EXPECTED_CLIENT_VERSION); + assertEquals(networkInfo, deviceInfoReport.getNetworkInfo()); + assertEquals(osInfo, deviceInfoReport.getOSInfo()); + assertEquals(firmwareInfo, deviceInfoReport.getFirmwareInfo()); + assertEquals(hardwareInfo, deviceInfoReport.getHardwareInfo()); + assertEquals(tpmInfo, deviceInfoReport.getTpmInfo()); + assertEquals(EXPECTED_CLIENT_VERSION, deviceInfoReport.getClientApplicationVersion()); } /**