From 3e0176d35a00dc47ee9df40f5309df5b7654d95a Mon Sep 17 00:00:00 2001 From: iadgovuser59 <133057011+iadgovuser59@users.noreply.github.com> Date: Mon, 28 Aug 2023 16:50:48 -0400 Subject: [PATCH 1/6] Adding FirmwareInfoTest --- HIRS_Utils/build.gradle | 1 + .../hirs/data/persist/FirmwareInfoTest.java | 207 ++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 HIRS_Utils/src/test/java/hirs/data/persist/FirmwareInfoTest.java diff --git a/HIRS_Utils/build.gradle b/HIRS_Utils/build.gradle index 3d77e493e..f2b6cbd4c 100644 --- a/HIRS_Utils/build.gradle +++ b/HIRS_Utils/build.gradle @@ -45,6 +45,7 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3' testImplementation 'org.junit.platform:junit-platform-launcher:1.9.3' testImplementation 'org.hamcrest:hamcrest:2.2' + testImplementation project(path: ':HIRS_AttestationCA') compileOnly libs.lombok annotationProcessor libs.lombok diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/FirmwareInfoTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/FirmwareInfoTest.java new file mode 100644 index 000000000..e337399f3 --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/data/persist/FirmwareInfoTest.java @@ -0,0 +1,207 @@ +package hirs.data.persist; + +import hirs.attestationca.persist.entity.userdefined.info.FirmwareInfo; +import org.apache.commons.lang3.StringUtils; +import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * FirmwareInfoTest is a unit test class for FirmwareInfo. + */ +public class FirmwareInfoTest { + + private static final String BIOS_VENDOR = "test bios vendor"; + private static final String BIOS_VERSION = "test bios version"; + private static final String BIOS_RELEASE_DATE = "test bios release date"; + + private static final String LONG_BIOS_VENDOR = StringUtils.rightPad( + "test bios vendor", + 257 + ); + private static final String LONG_BIOS_VERSION = StringUtils.rightPad( + "test bios version", + 257 + ); + private static final String LONG_BIOS_RELEASE_DATE = StringUtils.rightPad( + "test bios release date", + 33 + ); + + /** + * Tests instantiation of a FirmwareInfo object. + */ + @Test + public final void firmwareInfo() { + new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + } + + /** + * Tests that the no-parameter constructor for FirmwareInfo contains expected values. + */ + @Test + public final void firmwareInfoNoParams() { + FirmwareInfo firmwareInfo = new FirmwareInfo(); + Assertions.assertEquals(NOT_SPECIFIED, firmwareInfo.getBiosVendor()); + Assertions.assertEquals(NOT_SPECIFIED,firmwareInfo.getBiosVersion()); + Assertions.assertEquals(NOT_SPECIFIED,firmwareInfo.getBiosReleaseDate()); + } + + /** + * Tests that the getters for FirmwareInfo return the expected values. + */ + @Test + public final void firmwareInfoGetters() { + FirmwareInfo firmwareInfo = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + Assertions.assertEquals(BIOS_VENDOR, firmwareInfo.getBiosVendor()); + Assertions.assertEquals(BIOS_VERSION, firmwareInfo.getBiosVersion()); + Assertions.assertEquals(BIOS_RELEASE_DATE, firmwareInfo.getBiosReleaseDate()); + } + + /** + * Tests that an IllegalArgumentException is thrown if BIOS vendor is null. + */ + @Test + public final void biosVendorNullTest() { + Assertions.assertThrows(IllegalArgumentException.class, + () -> new FirmwareInfo(null, BIOS_VERSION, BIOS_RELEASE_DATE)); + } + + /** + * Tests that an IllegalArgumentException is thrown if BIOS version is null. + */ + @Test + public final void biosVersionNullTest() { + Assertions.assertThrows(IllegalArgumentException.class, + () -> new FirmwareInfo(BIOS_VENDOR, null, BIOS_RELEASE_DATE)); + } + + /** + * Tests that an IllegalArgumentException is thrown if BIOS release date is null. + */ + @Test + public final void biosReleaseDateNullTest() { + Assertions.assertThrows(IllegalArgumentException.class, + () -> new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, null)); + } + + /** + * Tests that an IllegalArgumentException is thrown if BIOS vendor is longer than allowed. + */ + @Test + public final void biosVendorLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, + () -> new FirmwareInfo(LONG_BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE)); + } + + /** + * Tests that an IllegalArgumentException is thrown if BIOS version is longer than allowed. + */ + @Test + public final void biosVersionLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, + () -> new FirmwareInfo(BIOS_VENDOR, LONG_BIOS_VERSION, BIOS_RELEASE_DATE)); + } + + /** + * Tests that an IllegalArgumentException is thrown if BIOS release date is longer than allowed. + */ + @Test + public final void biosReleaseDateLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, + () -> new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, LONG_BIOS_RELEASE_DATE)); + } + + /** + * Tests that two FirmwareInfo objects with the same BIOS vendor, version, and release date + * create hash codes that are equal. + */ + @Test + public final void testEqualHashCode() { + FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + Assertions.assertEquals(fi2.hashCode(), fi1.hashCode()); + } + + /** + * Tests that two FirmwareInfo objects with different BIOS vendor information will generate + * different hash codes. + */ + @Test + public final void testNotEqualHashCodeBiosVendor() { + String biosVendor2 = "test bios vendor 2"; + FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + FirmwareInfo fi2 = new FirmwareInfo(biosVendor2, BIOS_VERSION, BIOS_RELEASE_DATE); + Assertions.assertNotEquals(fi2.hashCode(), fi1.hashCode()); + } + + /** + * Tests that two FirmwareInfo objects with different BIOS version information will + * generate different hash codes. + */ + @Test + public final void testNotEqualHashCodeBiosVersion() { + String biosVersion2 = "test bios version 2"; + FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, biosVersion2, BIOS_RELEASE_DATE); + Assertions.assertNotEquals(fi2.hashCode(), fi1.hashCode()); + } + + /** + * Tests that two FirmwareInfo objects with different BIOS release date information will + * generate different hash codes. + */ + @Test + public final void testNotEqualHashCodeBiosReleaseDate() { + String biosReleaseDate2 = "test bios release date 2"; + FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, biosReleaseDate2); + Assertions.assertNotEquals(fi2.hashCode(), fi1.hashCode()); + } + + /** + * Tests that two FirmwareInfo objects with the same BIOS vendor, version, and release date + * information are equal. + */ + @Test + public final void testEqual() { + FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + Assertions.assertEquals(fi2, fi1); + } + + /** + * Tests that two FirmwareInfo objects with different BIOS vendor information are not equal. + */ + @Test + public final void testNotEqualBiosVendor() { + String biosVendor2 = "test bios vendor 2"; + FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + FirmwareInfo fi2 = new FirmwareInfo(biosVendor2, BIOS_VERSION, BIOS_RELEASE_DATE); + Assertions.assertNotEquals(fi2, fi1); + } + + /** + * Tests that two FirmwareInfo objects with different BIOS version information are not equal. + */ + @Test + public final void testNotEqualBiosVersion() { + String biosVersion2 = "test bios version 2"; + FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, biosVersion2, BIOS_RELEASE_DATE); + Assertions.assertNotEquals(fi2, fi1); + } + + /** + * Tests that two FirmwareInfo objects with different BIOS release date information are not + * equal. + */ + @Test + public final void testNotEqualBiosReleaseDate() { + String biosReleaseDate2 = "test bios release date 2"; + FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE); + FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, biosReleaseDate2); + Assertions.assertNotEquals(fi2, fi1); + } +} From 894b13acf8341778533957bb173877a77ce58b2e Mon Sep 17 00:00:00 2001 From: iadgovuser59 <133057011+iadgovuser59@users.noreply.github.com> Date: Mon, 28 Aug 2023 17:11:00 -0400 Subject: [PATCH 2/6] Adding HardwareInfoTest --- .../hirs/data/persist/HardwareInfoTest.java | 353 ++++++++++++++++++ 1 file changed, 353 insertions(+) create mode 100644 HIRS_Utils/src/test/java/hirs/data/persist/HardwareInfoTest.java diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/HardwareInfoTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/HardwareInfoTest.java new file mode 100644 index 000000000..a5ab1f4ef --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/data/persist/HardwareInfoTest.java @@ -0,0 +1,353 @@ +package hirs.data.persist; + +import hirs.attestationca.persist.entity.userdefined.info.HardwareInfo; +import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * HardwareInfoTest is a unit test class for HardwareInfo. + */ +public class HardwareInfoTest { + + private static final String MANUFACTURER = "test manufacturer"; + private static final String PRODUCT_NAME = "test product name"; + private static final String VERSION = "test version"; + private static final String SERIAL_NUMBER = "test serial number"; + private static final String CHASSIS_SERIAL_NUMBER = "test chassis serial number"; + private static final String BASEBOARD_SERIAL_NUMBER = "test baseboard serial number"; + + private static final String LONG_MANUFACTURER = StringUtils.rightPad( + "test manufacturer", + 257 + ); + private static final String LONG_PRODUCT_NAME = StringUtils.rightPad( + "test product name", + 257 + ); + private static final String LONG_VERSION = StringUtils.rightPad( + "test version", + 65 + ); + private static final String LONG_SERIAL_NUMBER = StringUtils.rightPad( + "test serial number", + 257 + ); + private static final String LONG_CHASSIS_SERIAL_NUMBER = StringUtils.rightPad( + "test chassis serial number", + 257 + ); + private static final String LONG_BASEBOARD_SERIAL_NUMBER = StringUtils.rightPad( + "test baseboard serial number", + 257 + ); + + /** + * Tests instantiation of a HardwareInfo object. + */ + @Test + public final void hardwareInfo() { + new HardwareInfo( + MANUFACTURER, + PRODUCT_NAME, + VERSION, + SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, + BASEBOARD_SERIAL_NUMBER + ); + } + + /** + * Tests that the no-parameter constructor for HardwareInfo contains expected values. + */ + @Test + public final void hardwareInfoNoParams() { + HardwareInfo hardwareInfo = new HardwareInfo(); + Assertions.assertEquals(NOT_SPECIFIED, hardwareInfo.getManufacturer()); + Assertions.assertEquals(NOT_SPECIFIED, hardwareInfo.getProductName()); + Assertions.assertEquals(NOT_SPECIFIED, hardwareInfo.getSystemSerialNumber()); + Assertions.assertEquals(NOT_SPECIFIED, hardwareInfo.getVersion()); + Assertions.assertEquals(NOT_SPECIFIED, hardwareInfo.getBaseboardSerialNumber()); + } + + /** + * Tests that the getters for HardwareInfo return the expected values. + */ + @Test + public final void hardwareInfoGetters() { + HardwareInfo hardwareInfo = + new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + Assertions.assertEquals(MANUFACTURER, hardwareInfo.getManufacturer()); + Assertions.assertEquals(PRODUCT_NAME, hardwareInfo.getProductName()); + Assertions.assertEquals(VERSION, hardwareInfo.getVersion()); + Assertions.assertEquals(SERIAL_NUMBER, hardwareInfo.getSystemSerialNumber()); + Assertions.assertEquals(BASEBOARD_SERIAL_NUMBER, hardwareInfo.getBaseboardSerialNumber()); + } + + /** + * Tests that the default value is present if manufacturer is null. + */ + @Test + public final void manufacturerNullTest() { + HardwareInfo hardwareInfo = new HardwareInfo(null, + PRODUCT_NAME, + VERSION, + SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, + BASEBOARD_SERIAL_NUMBER + ); + Assertions.assertEquals(NOT_SPECIFIED, hardwareInfo.getManufacturer()); + } + + /** + * Tests that the default value is present if product name is null. + */ + @Test + public final void productNameNullTest() { + HardwareInfo hardwareInfo = new HardwareInfo(MANUFACTURER, + null, + VERSION, + SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, + BASEBOARD_SERIAL_NUMBER + ); + Assertions.assertEquals(hardwareInfo.getProductName(), NOT_SPECIFIED); + } + + /** + * Tests that the default value is present if version is null. + */ + @Test + public final void versionNullTest() { + HardwareInfo hardwareInfo = new HardwareInfo(MANUFACTURER, + PRODUCT_NAME, + null, + SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, + BASEBOARD_SERIAL_NUMBER + ); + Assertions.assertEquals(hardwareInfo.getVersion(), NOT_SPECIFIED); + } + + /** + * Tests that the default value is present if serial number is null. + */ + @Test + public final void serialNumberNullTest() { + HardwareInfo hardwareInfo = new HardwareInfo(MANUFACTURER, + PRODUCT_NAME, + VERSION, + null, + CHASSIS_SERIAL_NUMBER, + BASEBOARD_SERIAL_NUMBER + ); + Assertions.assertEquals(hardwareInfo.getSystemSerialNumber(), NOT_SPECIFIED); + } + + /** + * Tests that the default value is present if chassis serial number is null. + */ + @Test + public final void chassisSerialNumberNullTest() { + HardwareInfo hardwareInfo = new HardwareInfo(MANUFACTURER, + PRODUCT_NAME, + VERSION, + SERIAL_NUMBER, + null, + BASEBOARD_SERIAL_NUMBER + ); + Assertions.assertEquals(NOT_SPECIFIED, hardwareInfo.getChassisSerialNumber()); + } + + /** + * Tests that the default value is present if baseboard serial number is null. + */ + @Test + public final void baseboardSerialNumberNullTest() { + HardwareInfo hardwareInfo = new HardwareInfo(MANUFACTURER, + PRODUCT_NAME, + VERSION, + SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, + null + ); + Assertions.assertEquals(NOT_SPECIFIED, hardwareInfo.getBaseboardSerialNumber()); + } + + /** + * Tests that an IllegalArgumentException is thrown if manufacturer is too long. + */ + @Test + public final void manufacturerLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> new HardwareInfo( + LONG_MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, CHASSIS_SERIAL_NUMBER, + BASEBOARD_SERIAL_NUMBER + )); + } + + /** + * Tests that an IllegalArgumentException is thrown if product name is too long. + */ + @Test + public final void productNameLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> new HardwareInfo( + MANUFACTURER, LONG_PRODUCT_NAME, VERSION, SERIAL_NUMBER, CHASSIS_SERIAL_NUMBER, + BASEBOARD_SERIAL_NUMBER + )); + } + + /** + * Tests that an IllegalArgumentException is thrown if version is too long. + */ + @Test + public final void versionLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> new HardwareInfo( + MANUFACTURER, PRODUCT_NAME, LONG_VERSION, SERIAL_NUMBER, CHASSIS_SERIAL_NUMBER, + BASEBOARD_SERIAL_NUMBER + )); + } + + /** + * Tests that an IllegalArgumentException is thrown if serial number is too long. + */ + @Test + public final void serialNumberLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> new HardwareInfo( + MANUFACTURER, PRODUCT_NAME, VERSION, LONG_SERIAL_NUMBER, CHASSIS_SERIAL_NUMBER, + BASEBOARD_SERIAL_NUMBER + )); + } + + /** + * Tests that an IllegalArgumentException is thrown if chassis serial number is too long. + */ + @Test + public final void chassisSerialNumberLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> new HardwareInfo( + MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, LONG_CHASSIS_SERIAL_NUMBER, + BASEBOARD_SERIAL_NUMBER + )); + } + + /** + * Tests that an IllegalArgumentException is thrown if chassis serial number is too long. + */ + @Test + public final void baseboardSerialNumberLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> new HardwareInfo( + MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, LONG_CHASSIS_SERIAL_NUMBER, + LONG_BASEBOARD_SERIAL_NUMBER + )); + } + + /** + * Tests that two HardwareInfo objects with the same manufacturer, product name, version, and + * serial number create hash codes that are equal. + */ + @Test + public final void testEqualsAndHashCode() { + HardwareInfo hi1 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + HardwareInfo hi2 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + Assertions.assertEquals(hi2, hi1); + Assertions.assertEquals(hi2.hashCode(), hi1.hashCode()); + + HardwareInfo hi3 = new HardwareInfo(); + Assertions.assertNotEquals(hi3, hi1); + Assertions.assertNotEquals(hi3.hashCode(), hi1.hashCode()); + } + + /** + * Tests that two HardwareInfo objects with different manufacturer information will generate + * different hash codes. + */ + @Test + public final void testEqualsAndHashCodeManufacturer() { + String manufacturer2 = "test manufacturer 2"; + HardwareInfo hi1 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + HardwareInfo hi2 = new HardwareInfo(manufacturer2, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + Assertions.assertNotEquals(hi2, hi1); + Assertions.assertNotEquals(hi2.hashCode(), hi1.hashCode()); + } + + /** + * Tests that two HardwareInfo objects with different product name information will + * generate different hash codes. + */ + @Test + public final void testEqualsAndHashCodeProductName() { + String productName2 = "test product name 2"; + HardwareInfo hi1 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + HardwareInfo hi2 = new HardwareInfo(MANUFACTURER, productName2, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + Assertions.assertNotEquals(hi2, hi1); + Assertions.assertNotEquals(hi2.hashCode(), hi1.hashCode()); + } + + /** + * Tests that two HardwareInfo objects with different version information will generate + * different hash codes. + */ + @Test + public final void testEqualsAndHashCodeVersion() { + String version2 = "test version 2"; + HardwareInfo hi1 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + HardwareInfo hi2 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, version2, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + Assertions.assertNotEquals(hi2, hi1); + Assertions.assertNotEquals(hi2.hashCode(), hi1.hashCode()); + } + + /** + * Tests that two HardwareInfo objects with different serial number information will generate + * different hash codes. + */ + @Test + public final void testEqualsAndHashCodeSerialNumber() { + String serialNumber2 = "test serialNumber 2"; + HardwareInfo hi1 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + HardwareInfo hi2 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, serialNumber2, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + Assertions.assertNotEquals(hi2, hi1); + Assertions.assertNotEquals(hi2.hashCode(), hi1.hashCode()); + } + + /** + * Tests that two HardwareInfo objects with different chassis serial number information will + * generate different hash codes. + */ + @Test + public final void testEqualsAndHashCodeChassisSerialNumber() { + String chassisSerialNumber2 = "test chassisSerialNumber 2"; + HardwareInfo hi1 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + HardwareInfo hi2 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + chassisSerialNumber2, BASEBOARD_SERIAL_NUMBER); + Assertions.assertNotEquals(hi2, hi1); + Assertions.assertNotEquals(hi2.hashCode(), hi1.hashCode()); + } + + /** + * Tests that two HardwareInfo objects with different baseboard serial number information will + * generate different hash codes. + */ + @Test + public final void testEqualsAndHashCodeBaseboardSerialNumber() { + String baseboardSerialNumber2 = "test baseboardSerialNumber 2"; + HardwareInfo hi1 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, BASEBOARD_SERIAL_NUMBER); + HardwareInfo hi2 = new HardwareInfo(MANUFACTURER, PRODUCT_NAME, VERSION, SERIAL_NUMBER, + CHASSIS_SERIAL_NUMBER, baseboardSerialNumber2); + Assertions.assertNotEquals(hi2, hi1); + Assertions.assertNotEquals(hi2.hashCode(), hi1.hashCode()); + } +} From 86a9d886c11f8b40474402516549ed92a7efb437 Mon Sep 17 00:00:00 2001 From: iadgovuser59 <133057011+iadgovuser59@users.noreply.github.com> Date: Thu, 31 Aug 2023 19:15:56 -0400 Subject: [PATCH 3/6] Adding NetworkInfoTest --- .../hirs/data/persist/NetworkInfoTest.java | 189 ++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 HIRS_Utils/src/test/java/hirs/data/persist/NetworkInfoTest.java diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/NetworkInfoTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/NetworkInfoTest.java new file mode 100644 index 000000000..f8476597b --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/data/persist/NetworkInfoTest.java @@ -0,0 +1,189 @@ +package hirs.data.persist; + +import hirs.attestationca.persist.entity.userdefined.info.NetworkInfo; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * NetworkInfoTest is a unit test class for NetworkInfo. + */ +public class NetworkInfoTest { + + private static final String HOSTNAME = "test hostname"; + private static final InetAddress IP_ADDRESS = getTestIpAddress(); + private static final byte[] MAC_ADDRESS = new byte[] {11, 22, 33, 44, 55, + 66}; + + /** + * Tests instantiation of a NetworkInfo object. + */ + @Test + public final void networkInfo() { + new NetworkInfo(null, null, null); + } + + /** + * Tests the getter for hostname of the NetworkInfo object. Hostname can be + * null if not known. + */ + @Test + public final void hostnameTest() { + NetworkInfo networkInfo = new NetworkInfo(null, null, null); + Assertions.assertNull(networkInfo.getHostname()); + + networkInfo = new NetworkInfo(HOSTNAME, null, null); + Assertions.assertEquals(HOSTNAME, networkInfo.getHostname()); + } + + /** + * Tests the getter for the IP address of the NetworkInfo object. IP address + * can be null if not known. + */ + @Test + public final void ipAddressTest() { + NetworkInfo networkInfo = new NetworkInfo(null, null, null); + Assertions.assertNull(networkInfo.getIpAddress()); + + networkInfo = new NetworkInfo(null, IP_ADDRESS, null); + Assertions.assertEquals(IP_ADDRESS, networkInfo.getIpAddress()); + } + + /** + * Tests the getter for the MAC address of the NetworkInfo object. MAC + * address can be null if not known. + */ + @Test + public final void macAddressTest() { + NetworkInfo networkInfo = new NetworkInfo(null, null, null); + Assertions.assertNull(networkInfo.getMacAddress()); + + networkInfo = new NetworkInfo(null, null, MAC_ADDRESS); + Assertions.assertArrayEquals(MAC_ADDRESS, networkInfo.getMacAddress()); + } + + /** + * Tests that trying to set the MAC address to a byte array that's an + * invalid size throws an exception. + */ + @Test + public final void macAddressInvalidSizeTest() { + final byte[] invalidMacAddress = new byte[] {11, 22, 33, 44, 55}; + Assertions.assertThrows(IllegalArgumentException.class, () -> new NetworkInfo( + null, null, invalidMacAddress + )); + } + + /** + * Tests that hashcodes generated by two equal NetworkInfo objects are + * equal. + */ + @Test + public final void testHashCodeEquals() { + NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); + NetworkInfo ni2 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); + Assertions.assertEquals(ni2.hashCode(), ni1.hashCode()); + } + + /** + * Tests that hashcodes generated by NetworkInfo objects with different + * hostnames are not equal. + */ + @Test + public final void testHashCodeNotEqualsHostname() { + String hostname2 = "test hostname2"; + NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); + NetworkInfo ni2 = new NetworkInfo(hostname2, IP_ADDRESS, MAC_ADDRESS); + Assertions.assertNotEquals(ni2.hashCode(), ni1.hashCode()); + } + + /** + * Tests that hashcodes generated by NetworkInfo objects with different IP + * addresses are not equal. + * + * @throws UnknownHostException + * in case the InetAddress is not created correctly + */ + @Test + public final void testHashCodeNotEqualsIpAddress() + throws UnknownHostException { + final InetAddress ipAddress2 = + InetAddress.getByAddress(new byte[] {127, 0, 0, 2}); + NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); + NetworkInfo ni2 = new NetworkInfo(HOSTNAME, ipAddress2, MAC_ADDRESS); + Assertions.assertNotEquals(ni2.hashCode(), ni1.hashCode()); + } + + /** + * Tests that hashcodes generated by NetworkInfo objects with different MAC + * address are not equal. + */ + @Test + public final void testHashCodeNotEqualsMacAddress() { + final byte[] macAddress2 = new byte[] {11, 22, 33, 44, 55, 67}; + NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); + NetworkInfo ni2 = new NetworkInfo(HOSTNAME, IP_ADDRESS, macAddress2); + Assertions.assertNotEquals(ni2.hashCode(), ni1.hashCode()); + } + + /** + * Tests that two NetworkInfo objects are equal if they have the same + * hostname, IP address, and MAC address. + */ + @Test + public final void testEqual() { + NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); + NetworkInfo ni2 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); + Assertions.assertEquals(ni2, ni1); + } + + /** + * Tests that two NetworkInfo objects are not equal if they have different + * hostnames. + */ + @Test + public final void testNotEqualsHostname() { + String hostname2 = "test hostname2"; + NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); + NetworkInfo ni2 = new NetworkInfo(hostname2, IP_ADDRESS, MAC_ADDRESS); + Assertions.assertNotEquals(ni2, ni1); + } + + /** + * Tests that two NetworkInfo objects are not equal if they have different + * IP addresses. + * + * @throws UnknownHostException + * in case InetAddress is not created correctly + */ + @Test + public final void testNotEqualsIpAddress() throws UnknownHostException { + final InetAddress ipAddress2 = + InetAddress.getByAddress(new byte[] {127, 0, 0, 2}); + NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); + NetworkInfo ni2 = new NetworkInfo(HOSTNAME, ipAddress2, MAC_ADDRESS); + Assertions.assertNotEquals(ni2, ni1); + } + + /** + * Tests that two NetworkInfo objects are not equal if they have different + * MAC addresses. + */ + @Test + public final void testNotEqualsMacAddress() { + final byte[] macAddress2 = new byte[] {11, 22, 33, 44, 55, 67}; + NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); + NetworkInfo ni2 = new NetworkInfo(HOSTNAME, IP_ADDRESS, macAddress2); + Assertions.assertNotEquals(ni2, ni1); + } + + private static InetAddress getTestIpAddress() { + try { + return InetAddress.getByAddress(new byte[] {127, 0, 0, 1}); + } catch (UnknownHostException e) { + return null; + } + } +} From 8386864195e04213e08d28f5b65032f5024e699d Mon Sep 17 00:00:00 2001 From: iadgovuser59 <133057011+iadgovuser59@users.noreply.github.com> Date: Fri, 1 Sep 2023 10:50:44 -0400 Subject: [PATCH 4/6] Adding OSInfoTest --- .../java/hirs/data/persist/OSInfoTest.java | 249 ++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 HIRS_Utils/src/test/java/hirs/data/persist/OSInfoTest.java diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/OSInfoTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/OSInfoTest.java new file mode 100644 index 000000000..20b82abf0 --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/data/persist/OSInfoTest.java @@ -0,0 +1,249 @@ +package hirs.data.persist; + +import hirs.attestationca.persist.entity.userdefined.info.OSInfo; +import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * OSInfoTest is a unit test class for OSInfo. + */ +public class OSInfoTest { + + private static final String OS_NAME = "test os"; + private static final String OS_VERSION = "test osVersion"; + private static final String OS_ARCH = "test osArch"; + private static final String DISTRIBUTION = "test distribution"; + private static final String DISTRIBUTION_RELEASE = "test distribution release"; + + private static final String LONG_OS_NAME = StringUtils.rightPad("test os", 257); + private static final String LONG_OS_VERSION = StringUtils.rightPad("test osVersion", 257); + private static final String LONG_OS_ARCH = StringUtils.rightPad("test osArch", 33); + + /** + * Tests instantiation of an OSInfo object. + */ + @Test + public final void osInfo() { + new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + } + + /** + * Tests that the no-parameter constructor for OSInfo contains expected values. + */ + @Test + public final void osInfoNoParams() { + OSInfo osInfo = new OSInfo(); + Assertions.assertEquals(NOT_SPECIFIED, osInfo.getOsName()); + Assertions.assertEquals(NOT_SPECIFIED, osInfo.getOsVersion()); + Assertions.assertEquals(NOT_SPECIFIED, osInfo.getOsArch()); + Assertions.assertEquals(NOT_SPECIFIED, osInfo.getDistribution()); + Assertions.assertEquals(NOT_SPECIFIED, osInfo.getDistributionRelease()); + } + + /** + * Tests that the getters for OSInfo return the expected values. + */ + @Test + public final void osInfoGetters() { + OSInfo osInfo = + new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + Assertions.assertEquals(OS_NAME, osInfo.getOsName()); + Assertions.assertEquals(OS_VERSION, osInfo.getOsVersion()); + Assertions.assertEquals(OS_ARCH, osInfo.getOsArch()); + Assertions.assertEquals(DISTRIBUTION, osInfo.getDistribution()); + Assertions.assertEquals(DISTRIBUTION_RELEASE, osInfo.getDistributionRelease()); + } + + /** + * Tests that a null pointer exception is thrown if OS name is null. + */ + @Test + public final void osNameNullTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> + new OSInfo(null, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); + } + + /** + * Tests that a null pointer exception is thrown if OS version is null. + */ + @Test + public final void osVersionNullTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> + new OSInfo(OS_NAME, null, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); + } + + /** + * Tests that a null pointer exception is thrown if OS arch is null. + */ + @Test + public final void osArchNullTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> + new OSInfo(OS_NAME, OS_VERSION, null, DISTRIBUTION, DISTRIBUTION_RELEASE)); + } + + /** + * Tests that a null pointer exception is thrown if OS name is null. + */ + @Test + public final void osNameLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> + new OSInfo(LONG_OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); + } + + /** + * Tests that a null pointer exception is thrown if OS version is null. + */ + @Test + public final void osVersionLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> + new OSInfo(OS_NAME, LONG_OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); + } + + /** + * Tests that a null pointer exception is thrown if OS arch is null. + */ + @Test + public final void osArchLongTest() { + Assertions.assertThrows(IllegalArgumentException.class, () -> + new OSInfo(OS_NAME, OS_VERSION, LONG_OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); + } + + /** + * Tests that distribution info may be null. + */ + @Test + public final void distributionNullTest() { + new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, null, DISTRIBUTION_RELEASE); + } + + /** + * Tests that distribution release info may be null. + */ + @Test + public final void distributionReleaseNullTest() { + new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, null); + } + + /** + * Tests that two OSInfo objects with the same name, make, version, and + * distribution create hash codes that are equal. + */ + @Test + public final void testEqualHashCode() { + OSInfo oi1 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + OSInfo oi2 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + Assertions.assertEquals(oi2.hashCode(), oi1.hashCode()); + } + + /** + * Tests that two OSInfo objects with different name information will + * generate different hash codes. + */ + @Test + public final void testNotEqualHashCodeOSName() { + String osName2 = "test os name 2"; + OSInfo oi1 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + OSInfo oi2 = new OSInfo(osName2, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + Assertions.assertNotEquals(oi2.hashCode(), oi1.hashCode()); + } + + /** + * Tests that two OSInfo objects with different make information will + * generate different hash codes. + */ + @Test + public final void testNotEqualHashCodeOSMake() { + String osMake2 = "test os make 2"; + OSInfo oi1 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + OSInfo oi2 = new OSInfo(OS_NAME, osMake2, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + Assertions.assertNotEquals(oi2.hashCode(), oi1.hashCode()); + } + + /** + * Tests that two OSInfo objects with different version information will + * generate different hash codes. + */ + @Test + public final void testNotEqualHashCodeOSVersion() { + String osVersion2 = "test os version 2"; + OSInfo oi1 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + OSInfo oi2 = + new OSInfo(OS_NAME, OS_VERSION, osVersion2, DISTRIBUTION, DISTRIBUTION_RELEASE); + Assertions.assertNotEquals(oi2.hashCode(), oi1.hashCode()); + } + + /** + * Tests that two OSInfo objects with different distribution information + * will generate different hash codes. + */ + @Test + public final void testNotEqualHashCodeDistribution() { + String distribution2 = "test distribution 2"; + OSInfo oi1 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + OSInfo oi2 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, distribution2, DISTRIBUTION_RELEASE); + Assertions.assertNotEquals(oi2.hashCode(), oi1.hashCode()); + } + + /** + * Tests that two OSInfo objects with the same name, make, version, and + * distribution information are equal. + */ + @Test + public final void testEqual() { + OSInfo oi1 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + OSInfo oi2 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + Assertions.assertEquals(oi2, oi1); + } + + /** + * Tests that two OSInfo objects with different name information are not + * equal. + */ + @Test + public final void testNotEqualOSName() { + String osName2 = "test os Name 2"; + OSInfo oi1 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + OSInfo oi2 = new OSInfo(osName2, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + Assertions.assertNotEquals(oi2, oi1); + } + + /** + * Tests that two OSInfo objects with different make information are not + * equal. + */ + @Test + public final void testNotEqualOSMake() { + String osMake2 = "test os make 2"; + OSInfo oi1 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + OSInfo oi2 = new OSInfo(OS_NAME, osMake2, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + Assertions.assertNotEquals(oi2, oi1); + } + + /** + * Tests that two OSInfo objects with different version information are not + * equal. + */ + @Test + public final void testNotEqualOSVersion() { + String osVersion2 = "test os version 2"; + OSInfo oi1 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + OSInfo oi2 = + new OSInfo(OS_NAME, OS_VERSION, osVersion2, DISTRIBUTION, DISTRIBUTION_RELEASE); + Assertions.assertNotEquals(oi2, oi1); + } + + /** + * Tests that two OSInfo objects with different distribution information are + * not equal. + */ + @Test + public final void testNotEqualDistribution() { + String distribution2 = "test distribution 2"; + OSInfo oi1 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE); + OSInfo oi2 = new OSInfo(OS_NAME, OS_VERSION, OS_ARCH, distribution2, DISTRIBUTION_RELEASE); + Assertions.assertNotEquals(oi2, oi1); + } +} From 0e6450da7f680576c6c6fbb45e203f1052a9be89 Mon Sep 17 00:00:00 2001 From: iadgovuser59 <133057011+iadgovuser59@users.noreply.github.com> Date: Fri, 1 Sep 2023 12:30:09 -0400 Subject: [PATCH 5/6] Adding PolicyTest and associated changes --- .../persist/entity/UserDefinedEntity.java | 43 +++- .../java/hirs/data/persist/PolicyTest.java | 204 ++++++++++++++++++ .../java/hirs/data/persist/TestPolicy.java | 37 ++++ 3 files changed, 279 insertions(+), 5 deletions(-) create mode 100644 HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java create mode 100644 HIRS_Utils/src/test/java/hirs/data/persist/TestPolicy.java diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/entity/UserDefinedEntity.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/entity/UserDefinedEntity.java index 651d32526..ca38680dc 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/entity/UserDefinedEntity.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/entity/UserDefinedEntity.java @@ -3,28 +3,26 @@ import jakarta.persistence.Column; import jakarta.persistence.MappedSuperclass; import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import lombok.ToString; +import lombok.NonNull; /** * An abstract archivable entity that can be given a user-defined name and description. */ @Getter @Setter -@EqualsAndHashCode(callSuper = false) @AllArgsConstructor @MappedSuperclass public abstract class UserDefinedEntity extends ArchivableEntity { @Column(nullable = false, unique = true) - private String name; + @NonNull private String name; @ToString.Exclude - @EqualsAndHashCode.Exclude @Column(nullable = false, unique = false) - private String description = ""; + @NonNull private String description = ""; /** * Default empty constructor is required for Hibernate. It is protected to @@ -42,5 +40,40 @@ protected UserDefinedEntity() { public UserDefinedEntity(final String name) { this(name, ""); } + + /** + * Returns a boolean if other is equal to this. UserDefinedEntitys are + * identified by their name, so this returns true if other is + * an instance of UserDefinedEntity and its name is the same as this + * UserDefinedEntity. Otherwise this returns false. + * + * @param other + * other object to test for equals + * @return true if other is Baseline and has same name + */ + @Override + public boolean equals(final Object other) { + if (this == other) { + return true; + } + if (!(other instanceof UserDefinedEntity)) { + return false; + } + + final UserDefinedEntity entity = (UserDefinedEntity) other; + return this.getName().equals(entity.getName()); + } + + /** + * Returns the hash code for this UserDefinedEntity. + * UserDefinedEntitys are identified by their name, so the returned hash + * is the hash of the name. + * + * @return hash + */ + @Override + public int hashCode() { + return name.hashCode(); + } } diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java new file mode 100644 index 000000000..93f1f4d3d --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java @@ -0,0 +1,204 @@ +package hirs.data.persist; + +import hirs.attestationca.persist.entity.Policy; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * PolicyTest is a unit test class for the Policy + * class. + */ +public final class PolicyTest { + + private static final Logger LOGGER = LogManager.getLogger(PolicyTest.class); + + /** + * Empty constructor that does nothing. + */ + public PolicyTest() { + /* do nothing */ + } + + /** + * Tests Policy constructor with valid name. + */ + @Test + public void testPolicy() { + LOGGER.debug("testPolicy test started"); + final String name = "myPolicy"; + new TestPolicy(name); + } + + /** + * Tests that Policy constructor throws + * NullPointerException with null name. + */ + @Test + public void testPolicyNullName() { + LOGGER.debug("testPolicyNullName test started"); + Assertions.assertThrows(NullPointerException.class, () -> new TestPolicy(null)); + } + + /** + * Tests that getName() returns the name. + */ + @Test + public void testGetName() { + LOGGER.debug("testGetName test started"); + final String name = "myPolicy"; + Policy p = new TestPolicy(name); + Assertions.assertEquals(name, p.getName()); + } + + /** + * Tests that Policy constructor throws + * NullPointerException with null name. + */ + @Test + public void testPolicyNullDescription() { + LOGGER.debug("testPolicyNullDescription test started"); + final String name = "myPolicy"; + Assertions.assertThrows(NullPointerException.class, () -> new TestPolicy(name, null)); + } + + /** + * Tests that getDescription()() returns the description. + */ + @Test + public void testGetDescription() { + LOGGER.debug("testGetDescription test started"); + final String name = "myPolicy"; + final String description = "myDescription"; + Policy p = new TestPolicy(name, description); + Assertions.assertEquals(description, p.getDescription()); + } + + /** + * Tests that two Policy objects are equal if they have the + * same name. + */ + @Test + public void testEquals() { + LOGGER.debug("testEquals test started"); + final String name1 = "myPolicy"; + Policy p1 = new TestPolicy(name1); + final String name2 = "myPolicy"; + Policy p2 = new TestPolicy(name2); + Assertions.assertEquals(p2, p1); + Assertions.assertEquals(p2, p1); + Assertions.assertEquals(p2, p1); + Assertions.assertEquals(p2, p1); + } + + /** + * Tests that two Policy objects are not equal if the names are + * different. + */ + @Test + public void testNotEquals() { + LOGGER.debug("testNotEquals test started"); + final String name1 = "myPolicy1"; + Policy p1 = new TestPolicy(name1); + final String name2 = "myPolicy2"; + Policy p2 = new TestPolicy(name2); + Assertions.assertNotEquals(p2, p1); + Assertions.assertNotEquals(p2, p1); + Assertions.assertEquals(p1, p1); + Assertions.assertEquals(p2, p2); + } + + /** + * Tests that hash code is that of the name. + */ + @Test + public void testHashCode() { + LOGGER.debug("testHashCode test started"); + final String name = "myPolicy"; + Policy p = new TestPolicy(name); + Assertions.assertEquals(p.hashCode(), name.hashCode()); + } + + /** + * Tests that the hash code of two Policy objects are the same + * if the names are the same. + */ + @Test + public void testHashCodeEquals() { + LOGGER.debug("testHashCodeEquals test started"); + final String name1 = "myPolicy"; + Policy p1 = new TestPolicy(name1); + final String name2 = "myPolicy"; + Policy p2 = new TestPolicy(name2); + Assertions.assertEquals(p2.hashCode(), p1.hashCode()); + } + + /** + * Tests that the hash codes of two Policy objects are + * different if they have different names. + */ + @Test + public void testHashCodeNotEquals() { + LOGGER.debug("testHashCodeNotEquals test started"); + final String name1 = "myPolicy1"; + Policy p1 = new TestPolicy(name1); + final String name2 = "myPolicy2"; + Policy p2 = new TestPolicy(name2); + Assertions.assertNotEquals(p2.hashCode(), p1.hashCode()); + } + + /** + * Tests that the name can be set for a Policy. + */ + @Test + public void setName() { + LOGGER.debug("setName test started"); + final String name = "myPolicy"; + Policy p = new TestPolicy(name); + final String newName = "newPolicy"; + p.setName(newName); + Assertions.assertEquals(p.getName(), newName); + Assertions.assertEquals(p.hashCode(), newName.hashCode()); + } + + /** + * Tests that the description can be set for a Policy. + */ + @Test + public void setDescription() { + LOGGER.debug("setDescription test started"); + final String name = "myPolicy"; + final String description = "myDescription"; + Policy p = new TestPolicy(name, description); + final String newDescription = "newDescription"; + p.setDescription(newDescription); + Assertions.assertEquals(p.getDescription(), newDescription); + } + + /** + * Tests that a name cannot be null for a Policy. + */ + @Test + public void setNameNull() { + LOGGER.debug("setNameNull test started"); + final String name = "myPolicy"; + Policy p = new TestPolicy(name); + Assertions.assertThrows(NullPointerException.class, () -> p.setName(null)); + } + + /** + * Tests that a description cannot be null for a Policy. + */ + @Test + public void setDescriptionNull() { + LOGGER.debug("setDescriptionNull test started"); + final String name = "myPolicy"; + final String description = "myDescription"; + Policy p = new TestPolicy(name, description); + Assertions.assertThrows(NullPointerException.class, () -> p.setDescription(null)); + } +} + diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/TestPolicy.java b/HIRS_Utils/src/test/java/hirs/data/persist/TestPolicy.java new file mode 100644 index 000000000..a8682eab2 --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/data/persist/TestPolicy.java @@ -0,0 +1,37 @@ +package hirs.data.persist; + +import hirs.attestationca.persist.entity.Policy; +import jakarta.persistence.Entity; + +/** + * This Policy class is used exclusively for testing purposes. + */ +@Entity +public class TestPolicy extends Policy { + + /** + * Creates a new TestPolicy with the set name. + * + * @param name name + */ + public TestPolicy(final String name) { + super(name); + } + + /** + * Creates a new TestPolicy with the set name and description. + * + * @param name name + * @param description description + */ + public TestPolicy(final String name, final String description) { + super(name, description); + } + + /** + * Default constructor necessary for Hibernate. + */ + protected TestPolicy() { + super(); + } +} From d5af444605b91588583e8e8886b4fa52864e6fdf Mon Sep 17 00:00:00 2001 From: iadgovuser59 <133057011+iadgovuser59@users.noreply.github.com> Date: Fri, 1 Sep 2023 12:44:04 -0400 Subject: [PATCH 6/6] Minor code cleanup --- HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java index 93f1f4d3d..79a6c4b77 100644 --- a/HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java +++ b/HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java @@ -107,8 +107,6 @@ public void testNotEquals() { Policy p2 = new TestPolicy(name2); Assertions.assertNotEquals(p2, p1); Assertions.assertNotEquals(p2, p1); - Assertions.assertEquals(p1, p1); - Assertions.assertEquals(p2, p2); } /**