From 337a6263356d733db7c570aba2933d066cb526cf Mon Sep 17 00:00:00 2001 From: TheSilentCoder <184309164+ThatSilentCoder@users.noreply.github.com> Date: Fri, 11 Oct 2024 15:15:08 -0400 Subject: [PATCH] issue-844: Finished cleaning up code using checkstyles guidelines on HIRS-Utils test module. Finishing up cleaning on main module. --- .../java/hirs/utils/digest/package-info.java | 1 + .../java/hirs/utils/enums/package-info.java | 1 + .../hirs/utils/exception/package-info.java | 1 + .../main/java/hirs/utils/package-info.java | 1 + .../java/hirs/utils/rim/package-info.java | 1 + .../hirs/utils/swid/SwidTagConstants.java | 56 +- .../java/hirs/utils/swid/package-info.java | 1 + .../events/DeviceSecurityEventHeader.java | 71 +- .../hirs/utils/tpm/eventlog/spdm/SpdmHa.java | 21 +- .../tpm/eventlog/spdm/SpdmMeasurement.java | 89 +- .../utils/tpm/eventlog/spdm/package-info.java | 2 +- .../tpm/eventlog/uefi/UefiConstants.java | 16 +- .../java/hirs/data/persist/DigestTest.java | 870 +++++++++--------- .../hirs/data/persist/FirmwareInfoTest.java | 22 +- .../hirs/data/persist/HardwareInfoTest.java | 30 +- .../hirs/data/persist/NetworkInfoTest.java | 35 +- .../java/hirs/data/persist/OSInfoTest.java | 20 +- .../java/hirs/data/persist/package-info.java | 1 + .../tpm/eventlog/uefi/UefiProcessingTest.java | 52 +- .../hirs/tpm/eventlog/uefi/package-info.java | 1 + .../hirs/utils/BouncyCastleUtilsTest.java | 5 +- .../test/java/hirs/utils/HexUtilsTest.java | 46 +- .../java/hirs/utils/StringValidatorTest.java | 21 +- .../test/java/hirs/utils/package-info.java | 1 + .../utils/tpm/eventlog/TCGEventLogTest.java | 179 ++-- .../hirs/utils/tpm/eventlog/package-info.java | 1 + config/checkstyle/checkstyle.xml | 7 +- 27 files changed, 794 insertions(+), 758 deletions(-) create mode 100644 HIRS_Utils/src/main/java/hirs/utils/digest/package-info.java create mode 100644 HIRS_Utils/src/main/java/hirs/utils/enums/package-info.java create mode 100644 HIRS_Utils/src/main/java/hirs/utils/exception/package-info.java create mode 100644 HIRS_Utils/src/main/java/hirs/utils/package-info.java create mode 100644 HIRS_Utils/src/main/java/hirs/utils/rim/package-info.java create mode 100644 HIRS_Utils/src/main/java/hirs/utils/swid/package-info.java create mode 100644 HIRS_Utils/src/test/java/hirs/data/persist/package-info.java create mode 100644 HIRS_Utils/src/test/java/hirs/tpm/eventlog/uefi/package-info.java create mode 100644 HIRS_Utils/src/test/java/hirs/utils/package-info.java create mode 100644 HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/package-info.java diff --git a/HIRS_Utils/src/main/java/hirs/utils/digest/package-info.java b/HIRS_Utils/src/main/java/hirs/utils/digest/package-info.java new file mode 100644 index 00000000..2d2e9eca --- /dev/null +++ b/HIRS_Utils/src/main/java/hirs/utils/digest/package-info.java @@ -0,0 +1 @@ +package hirs.utils.digest; diff --git a/HIRS_Utils/src/main/java/hirs/utils/enums/package-info.java b/HIRS_Utils/src/main/java/hirs/utils/enums/package-info.java new file mode 100644 index 00000000..3ff5c9ba --- /dev/null +++ b/HIRS_Utils/src/main/java/hirs/utils/enums/package-info.java @@ -0,0 +1 @@ +package hirs.utils.enums; diff --git a/HIRS_Utils/src/main/java/hirs/utils/exception/package-info.java b/HIRS_Utils/src/main/java/hirs/utils/exception/package-info.java new file mode 100644 index 00000000..464dea8c --- /dev/null +++ b/HIRS_Utils/src/main/java/hirs/utils/exception/package-info.java @@ -0,0 +1 @@ +package hirs.utils.exception; diff --git a/HIRS_Utils/src/main/java/hirs/utils/package-info.java b/HIRS_Utils/src/main/java/hirs/utils/package-info.java new file mode 100644 index 00000000..361ebc74 --- /dev/null +++ b/HIRS_Utils/src/main/java/hirs/utils/package-info.java @@ -0,0 +1 @@ +package hirs.utils; diff --git a/HIRS_Utils/src/main/java/hirs/utils/rim/package-info.java b/HIRS_Utils/src/main/java/hirs/utils/rim/package-info.java new file mode 100644 index 00000000..e72d99d1 --- /dev/null +++ b/HIRS_Utils/src/main/java/hirs/utils/rim/package-info.java @@ -0,0 +1 @@ +package hirs.utils.rim; diff --git a/HIRS_Utils/src/main/java/hirs/utils/swid/SwidTagConstants.java b/HIRS_Utils/src/main/java/hirs/utils/swid/SwidTagConstants.java index a1708613..baec697f 100644 --- a/HIRS_Utils/src/main/java/hirs/utils/swid/SwidTagConstants.java +++ b/HIRS_Utils/src/main/java/hirs/utils/swid/SwidTagConstants.java @@ -78,34 +78,34 @@ public class SwidTagConstants { public static final String FX_SEPARATOR = ":"; public static final String RFC3852_PFX = "rcf3852"; public static final String RFC3339_PFX = "rcf3339"; - public static final String _COLLOQUIAL_VERSION_STR = N8060_PFX + FX_SEPARATOR + - COLLOQUIAL_VERSION; - public static final String _PRODUCT_STR = N8060_PFX + FX_SEPARATOR + - PRODUCT; - public static final String _REVISION_STR = N8060_PFX + FX_SEPARATOR + - REVISION; - public static final String _EDITION_STR = N8060_PFX + FX_SEPARATOR + - EDITION; - public static final String _RIM_LINK_HASH_STR = RIM_PFX + FX_SEPARATOR + - RIM_LINK_HASH; - public static final String _BINDING_SPEC_STR = RIM_PFX + FX_SEPARATOR + - BINDING_SPEC; - public static final String _BINDING_SPEC_VERSION_STR = RIM_PFX + FX_SEPARATOR + - BINDING_SPEC_VERSION; - public static final String _PLATFORM_MANUFACTURER_STR = RIM_PFX + FX_SEPARATOR + - PLATFORM_MANUFACTURER_STR; - public static final String _PLATFORM_MANUFACTURER_ID_STR = RIM_PFX + FX_SEPARATOR + - PLATFORM_MANUFACTURER_ID; - public static final String _PLATFORM_MODEL_STR = RIM_PFX + FX_SEPARATOR + - PLATFORM_MODEL; - public static final String _PLATFORM_VERSION_STR = RIM_PFX + FX_SEPARATOR + - PLATFORM_VERSION; - public static final String _PAYLOAD_TYPE_STR = RIM_PFX + FX_SEPARATOR + - PAYLOAD_TYPE; - public static final String _PC_URI_LOCAL_STR = RIM_PFX + FX_SEPARATOR + - PC_URI_LOCAL; - public static final String _PC_URI_GLOBAL_STR = RIM_PFX + FX_SEPARATOR + - PC_URI_GLOBAL; + public static final String _COLLOQUIAL_VERSION_STR = N8060_PFX + FX_SEPARATOR + + COLLOQUIAL_VERSION; + public static final String _PRODUCT_STR = N8060_PFX + FX_SEPARATOR + + PRODUCT; + public static final String _REVISION_STR = N8060_PFX + FX_SEPARATOR + + REVISION; + public static final String _EDITION_STR = N8060_PFX + FX_SEPARATOR + + EDITION; + public static final String _RIM_LINK_HASH_STR = RIM_PFX + FX_SEPARATOR + + RIM_LINK_HASH; + public static final String _BINDING_SPEC_STR = RIM_PFX + FX_SEPARATOR + + BINDING_SPEC; + public static final String _BINDING_SPEC_VERSION_STR = RIM_PFX + FX_SEPARATOR + + BINDING_SPEC_VERSION; + public static final String _PLATFORM_MANUFACTURER_STR = RIM_PFX + FX_SEPARATOR + + PLATFORM_MANUFACTURER_STR; + public static final String _PLATFORM_MANUFACTURER_ID_STR = RIM_PFX + FX_SEPARATOR + + PLATFORM_MANUFACTURER_ID; + public static final String _PLATFORM_MODEL_STR = RIM_PFX + FX_SEPARATOR + + PLATFORM_MODEL; + public static final String _PLATFORM_VERSION_STR = RIM_PFX + FX_SEPARATOR + + PLATFORM_VERSION; + public static final String _PAYLOAD_TYPE_STR = RIM_PFX + FX_SEPARATOR + + PAYLOAD_TYPE; + public static final String _PC_URI_LOCAL_STR = RIM_PFX + FX_SEPARATOR + + PC_URI_LOCAL; + public static final String _PC_URI_GLOBAL_STR = RIM_PFX + FX_SEPARATOR + + PC_URI_GLOBAL; public static final QName _SHA256_HASH = new QName( "http://www.w3.org/2001/04/xmlenc#sha256", HASH, "SHA256"); public static final QName _COLLOQUIAL_VERSION = new QName( diff --git a/HIRS_Utils/src/main/java/hirs/utils/swid/package-info.java b/HIRS_Utils/src/main/java/hirs/utils/swid/package-info.java new file mode 100644 index 00000000..91c5a00f --- /dev/null +++ b/HIRS_Utils/src/main/java/hirs/utils/swid/package-info.java @@ -0,0 +1 @@ +package hirs.utils.swid; diff --git a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/events/DeviceSecurityEventHeader.java b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/events/DeviceSecurityEventHeader.java index 13612538..1f413c20 100644 --- a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/events/DeviceSecurityEventHeader.java +++ b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/events/DeviceSecurityEventHeader.java @@ -12,51 +12,55 @@ import java.nio.charset.StandardCharsets; * The first 16 bytes of the event data header MUST be a String based identifier (Signature), * NUL-terminated, per PFP. The only currently defined Signature is "SPDM Device Sec", * which implies the data is a DEVICE_SECURITY_EVENT_DATA or ..DATA2. - * + *

* HEADERS defined by PFP v1.06 Rev 52. * Certain fields are common to both ..HEADER and ..HEADER2, and are noted below the structures. *

* typedef struct tdDEVICE_SECURITY_EVENT_DATA_HEADER { - * UINT8 Signature[16]; - * UINT16 Version; - * UINT16 Length; - * UINT32 SpdmHashAlg; - * UINT32 DeviceType; - * SPDM_MEASUREMENT_BLOCK SpdmMeasurementBlock; - * UINT64 DevicePathLength; - * UNIT8 DevicePath[DevicePathLength] + * UINT8 Signature[16]; + * UINT16 Version; + * UINT16 Length; + * UINT32 SpdmHashAlg; + * UINT32 DeviceType; + * SPDM_MEASUREMENT_BLOCK SpdmMeasurementBlock; + * UINT64 DevicePathLength; + * UNIT8 DevicePath[DevicePathLength] * } DEVICE_SECURITY_EVENT_DATA_HEADER; *

* typedef struct tdDEVICE_SECURITY_EVENT_DATA_HEADER2 { - NOT IMPLEMENTED YET - * UINT8 Signature[16]; - * UINT16 Version; - * UINT8 AuthState; - * UINT8 Reserved; - * UINT32 Length; - * UINT32 DeviceType; - * UINT32 SubHeaderType; - * UINT32 SubHeaderLength; - * UINT32 SubHeaderUID; - * UINT64 DevicePathLength; - * UNIT8 DevicePath[DevicePathLength] + * UINT8 Signature[16]; + * UINT16 Version; + * UINT8 AuthState; + * UINT8 Reserved; + * UINT32 Length; + * UINT32 DeviceType; + * UINT32 SubHeaderType; + * UINT32 SubHeaderLength; + * UINT32 SubHeaderUID; + * UINT64 DevicePathLength; + * UNIT8 DevicePath[DevicePathLength] * } DEVICE_SECURITY_EVENT_DATA_HEADER2; *

* Fields common to both ..HEADER and ..HEADER2: - * Signature - * Version - * DeviceType - * DevicePathLength - * DevicePath + * Signature + * Version + * DeviceType + * DevicePathLength + * DevicePath *

*/ public abstract class DeviceSecurityEventHeader { + /** + * UEFI Device Path Length. + */ + @Getter + private final int devicePathLength = 0; /** * Contains the size (in bytes) of the header. */ @Getter private Integer dsedHeaderLength = 0; - /** * Signature (text) data. */ @@ -73,11 +77,6 @@ public abstract class DeviceSecurityEventHeader { */ @Getter private int deviceType = -1; - /** - * UEFI Device Path Length. - */ - @Getter - private int devicePathLength = 0; /** * UEFI Device path. */ @@ -131,19 +130,19 @@ public abstract class DeviceSecurityEventHeader { // get the device path length byte[] devicePathLengthBytes = new byte[8]; System.arraycopy(dsedBytes, startByteUpdated, devicePathLengthBytes, 0, 8); - int devicePathLength = HexUtils.leReverseInt(devicePathLengthBytes); + int retrievedDevicePathLength = HexUtils.leReverseInt(devicePathLengthBytes); // get the device path - if (devicePathLength > 0) { + if (retrievedDevicePathLength > 0) { startByteUpdated = startByteUpdated + 8; - byte[] devPathBytes = new byte[devicePathLength]; + byte[] devPathBytes = new byte[retrievedDevicePathLength]; System.arraycopy(dsedBytes, startByteUpdated, devPathBytes, - 0, devicePathLength); + 0, retrievedDevicePathLength); devicePath = new UefiDevicePath(devPathBytes); } // header total size - dsedHeaderLength = startByteUpdated + devicePathLength; + dsedHeaderLength = startByteUpdated + retrievedDevicePathLength; } /** diff --git a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/SpdmHa.java b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/SpdmHa.java index b64d013b..ae618926 100644 --- a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/SpdmHa.java +++ b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/SpdmHa.java @@ -6,11 +6,6 @@ package hirs.utils.tpm.eventlog.spdm; */ public class SpdmHa { - /** - * Default private constructor so checkstyles doesn't complain - */ - private SpdmHa() { } - /** * Spdm Hash Alg = Raw bit stream. */ @@ -40,6 +35,12 @@ public class SpdmHa { */ public static final int TPM_ALG_SHA3_512 = 64; + /** + * Default private constructor so checkstyles doesn't complain. + */ + private SpdmHa() { + } + /** * Returns the hash name via a lookup. * Lookup based upon SPDM Spec v1.03 section 10.4. @@ -93,21 +94,15 @@ public class SpdmHa { case TPM_ALG_SHA_256: byteSize = 32; break; - case TPM_ALG_SHA_384: + case TPM_ALG_SHA_384, TPM_ALG_SHA3_384: byteSize = 48; break; - case TPM_ALG_SHA_512: + case TPM_ALG_SHA_512, TPM_ALG_SHA3_512: byteSize = 64; break; case TPM_ALG_SHA3_256: byteSize = 32; break; - case TPM_ALG_SHA3_384: - byteSize = 48; - break; - case TPM_ALG_SHA3_512: - byteSize = 64; - break; default: byteSize = -1; } diff --git a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/SpdmMeasurement.java b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/SpdmMeasurement.java index 4a90cacb..b1ed423e 100644 --- a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/SpdmMeasurement.java +++ b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/SpdmMeasurement.java @@ -8,34 +8,34 @@ import lombok.Getter; *

* Measurement, defined by SPDM v1.03, Sect 10.11.1, Table 54: * DMTF measurement spec format { - * DMTFSpecMeasurementValueType 1 byte; - * DMTFSpecMeasurementValueSize 2 bytes; - * DMTFSpecMeasurementValue bytes; + * DMTFSpecMeasurementValueType 1 byte; + * DMTFSpecMeasurementValueSize 2 bytes; + * DMTFSpecMeasurementValue bytes; * } *

* DMTFSpecMeasurementValueType[7] - * Indicates how bits [0:6] are represented - * Bit = 0: Digest - * Bit = 1: Raw bit stream + * Indicates how bits [0:6] are represented + * Bit = 0: Digest + * Bit = 1: Raw bit stream * DMTFSpecMeasurementValueType[6:0] (see SPDM Spec, Table 55 "DMTFSpecMeasurementValueType[6:0]") - * Immutable ROM 0x0 - * Mutable firmware 0x1 - * Hardware configuration 0x2 - * Firmware configuration 0x3 - * etc. + * Immutable ROM 0x0 + * Mutable firmware 0x1 + * Hardware configuration 0x2 + * Firmware configuration 0x3 + * etc. *

*/ public class SpdmMeasurement { + /** + * Measurement value (digest). + */ + private final byte[] dmtfSpecMeasurementValue; /** * Measurement value type (such as mutable firmware, etc). */ @Getter private int dmtfSpecMeasurementValueType = 0; - /** - * Measurement value (digest). - */ - private byte[] dmtfSpecMeasurementValue; /** * SpdmMeasurement Constructor. @@ -49,14 +49,16 @@ public class SpdmMeasurement { 1); dmtfSpecMeasurementValueType = HexUtils.leReverseInt(dmtfSpecMeasurementValueTypeBytes); - // in future, can crosscheck this value size + 3 with the spdm block MeasurementSize size + // in the future, can crosscheck this value size + 3 with the spdm block MeasurementSize size byte[] dmtfSpecMeasurementValueSizeBytes = new byte[2]; System.arraycopy(spdmMeasBytes, 1, dmtfSpecMeasurementValueSizeBytes, 0, 2); int dmtfSpecMeasurementValueSize = HexUtils.leReverseInt(dmtfSpecMeasurementValueSizeBytes); dmtfSpecMeasurementValue = new byte[dmtfSpecMeasurementValueSize]; - System.arraycopy(spdmMeasBytes, 3, dmtfSpecMeasurementValue, 0, + + final int sourceIndex = 3; + System.arraycopy(spdmMeasBytes, sourceIndex, dmtfSpecMeasurementValue, 0, dmtfSpecMeasurementValueSize); } @@ -64,49 +66,24 @@ public class SpdmMeasurement { * Lookup for SPDM measurement value type. * * @param measValType the numerical representation of the measurement value type. - * * @return a description of the measurement value type. */ public String dmtfSpecMeasurementValueTypeToString(final int measValType) { - String measValTypeStr; - switch (measValType) { - case 0: - measValTypeStr = "Immutable ROM"; - break; - case 1: - measValTypeStr = "Mutable firmware"; - break; - case 2: - measValTypeStr = "Hardware configuration"; - break; - case 3: - measValTypeStr = "Firmware configuration"; - break; - case 4: - measValTypeStr = "Freeform measurement manifest"; - break; - case 5: - measValTypeStr = "Structured representation of debug and device mode"; - break; - case 6: - measValTypeStr = "Mutable firmware's version number"; - break; - case 7: - measValTypeStr = "Mutable firmware's security version number"; - break; - case 8: - measValTypeStr = "Hash-extended measurement"; - break; - case 9: - measValTypeStr = "Informational"; - break; - case 10: - measValTypeStr = "Structured measurement manifest"; - break; - default: - measValTypeStr = "Unknown or invalid DMTF Spec Measurement Value Type"; - } + String measValTypeStr = switch (measValType) { + case 0 -> "Immutable ROM"; + case 1 -> "Mutable firmware"; + case 2 -> "Hardware configuration"; + case 3 -> "Firmware configuration"; + case 4 -> "Freeform measurement manifest"; + case 5 -> "Structured representation of debug and device mode"; + case 6 -> "Mutable firmware's version number"; + case 7 -> "Mutable firmware's security version number"; + case 8 -> "Hash-extended measurement"; + case 9 -> "Informational"; + case 10 -> "Structured measurement manifest"; + default -> "Unknown or invalid DMTF Spec Measurement Value Type"; + }; return measValTypeStr; } diff --git a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/package-info.java b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/package-info.java index 64b778b4..f76263ad 100644 --- a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/package-info.java +++ b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/spdm/package-info.java @@ -2,4 +2,4 @@ * Non-persistent classes related to TGC Event Logs. */ -package hirs.utils.tpm.eventlog.spdm; \ No newline at end of file +package hirs.utils.tpm.eventlog.spdm; diff --git a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/uefi/UefiConstants.java b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/uefi/UefiConstants.java index 655ffa68..71e94243 100644 --- a/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/uefi/UefiConstants.java +++ b/HIRS_Utils/src/main/java/hirs/utils/tpm/eventlog/uefi/UefiConstants.java @@ -7,11 +7,6 @@ package hirs.utils.tpm.eventlog.uefi; */ public final class UefiConstants { - /** - * Default private constructor so checkstyles doesn't complain - */ - private UefiConstants() { } - /** * 2 byte size. */ @@ -278,13 +273,18 @@ public final class UefiConstants { public static final String FILESTATUS_FROM_FILESYSTEM = "fileFromFilesystem"; /** * file status, where file was not found on local machine, so file from code was used. - * For instance, if vendor-table.json is not found in filesystem at location - * /etc/hirs/aca/default-properties/, it will be grabbed from code at - * HIRS_AttestationCA/src/main/resources/. + * For instance, if vendor-table.json is not found in filesystem at location + * /etc/hirs/aca/default-properties/, it will be grabbed from code at + * HIRS_AttestationCA/src/main/resources/. */ public static final String FILESTATUS_FROM_CODE = "fileFromCode"; /** * file status, where file is not accessible (either not found, or no access permission). */ public static final String FILESTATUS_NOT_ACCESSIBLE = "fileNotAccessible"; + /** + * Default private constructor so checkstyles doesn't complain. + */ + private UefiConstants() { + } } diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/DigestTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/DigestTest.java index c0f4a82a..bc030f81 100644 --- a/HIRS_Utils/src/test/java/hirs/data/persist/DigestTest.java +++ b/HIRS_Utils/src/test/java/hirs/data/persist/DigestTest.java @@ -1,436 +1,20 @@ package hirs.data.persist; -import java.util.Arrays; - import hirs.utils.digest.Digest; import hirs.utils.digest.DigestAlgorithm; import hirs.utils.digest.DigestComparisonResultType; import org.apache.commons.codec.digest.DigestUtils; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.Arrays; + /** * Unit tests for the Digest class. */ public class DigestTest { private static final int DIGEST_LENGTH_BYTES = 20; - /** - * Tests that constructor throws a IllegalArgumentException when a - * null DigestAlgorithm is passed into constructor. - */ - @Test - public final void nullAlgorithm() { - final byte[] digest = getTestDigest(16); - Assertions.assertThrows(IllegalArgumentException.class, - () -> new Digest(null, digest)); - } - - /** - * Tests that constructor throws a IllegalArgumentException when a - * null digest is passed into constructor. - */ - @Test - public final void nullDigest() { - Assertions.assertThrows(IllegalArgumentException.class, - () -> new Digest(DigestAlgorithm.MD2, null)); - } - - /** - * Tests that constructor throws a IllegalArgumentException when an - * digest that is an empty array is passed into constructor. - */ - @Test - public final void emptyArrayDigest() { - Assertions.assertThrows(IllegalArgumentException.class, - () -> new Digest(DigestAlgorithm.MD2, new byte[0])); - } - - /** - * Tests that MD2 digest can be created. - */ - @Test - public final void md2() { - final byte[] digest = getTestDigest(16); - final Digest d = new Digest(DigestAlgorithm.MD2, digest); - Assertions.assertNotNull(d); - } - - /** - * Tests that an MD2 digest can be recreated from a string. - */ - @Test - public final void testFromStringMD2() { - final byte[] digestBytes = getTestDigest(16); - Digest digest = new Digest(DigestAlgorithm.MD2, digestBytes); - String digestString = digest.toString(); - Digest digestFromString = Digest.fromString(digestString); - Assertions.assertEquals(digest, digestFromString); - } - - /** - * Tests that MD2 digest cannot be created with a digest that has extra - * bytes. - */ - @Test - public final void md2IllegalDigest() { - final byte[] digest = getTestDigest(17); - Assertions.assertThrows(IllegalArgumentException.class, - () -> new Digest(DigestAlgorithm.MD2, digest)); - } - - /** - * Tests that MD5 digest can be created. - */ - @Test - public final void md5() { - final byte[] digest = getTestDigest(16); - final Digest d = new Digest(DigestAlgorithm.MD5, digest); - Assertions.assertNotNull(d); - } - - /** - * Tests that an MD5 digest can be recreated from a string. - */ - @Test - public final void testFromStringMD5() { - final byte[] digestBytes = getTestDigest(16); - Digest digest = new Digest(DigestAlgorithm.MD5, digestBytes); - String digestString = digest.toString(); - Digest digestFromString = Digest.fromString(digestString); - Assertions.assertEquals(digest, digestFromString); - } - - /** - * Tests that MD5 digest cannot be created with a digest that has extra - * bytes. - */ - @Test - public final void md5IllegalDigest() { - final byte[] digest = getTestDigest(17); - Assertions.assertThrows(IllegalArgumentException.class, - () -> new Digest(DigestAlgorithm.MD5, digest)); - } - - /** - * Tests that SHA1 digest can be created. - */ - @Test - public final void sha1() { - final byte[] digest = getTestDigest(20); - final Digest d = new Digest(DigestAlgorithm.SHA1, digest); - Assertions.assertNotNull(d); - } - - /** - * Tests that SHA1 digest can be recreated from a string. - */ - @Test - public final void testFromStringSHA1() { - final byte[] digestBytes = getTestDigest(20); - Digest digest = new Digest(DigestAlgorithm.SHA1, digestBytes); - String digestString = digest.toString(); - Digest digestFromString = Digest.fromString(digestString); - Assertions.assertEquals(digest, digestFromString); - } - - /** - * Tests that SHA1 digest cannot be created with a digest that has extra - * bytes. - */ - @Test - public final void sha1IllegalDigest() { - final byte[] digest = getTestDigest(21); - Assertions.assertThrows(IllegalArgumentException.class, - () -> new Digest(DigestAlgorithm.SHA1, digest)); - } - - /** - * Tests that SHA256 digest can be created. - */ - @Test - public final void sha256() { - final byte[] digest = getTestDigest(32); - final Digest d = new Digest(DigestAlgorithm.SHA256, digest); - Assertions.assertNotNull(d); - } - - /** - * Tests that SHA256 digest can be recreated from a string. - */ - @Test - public final void testFromStringSHA256() { - final byte[] digestBytes = getTestDigest(32); - Digest digest = new Digest(DigestAlgorithm.SHA256, digestBytes); - String digestString = digest.toString(); - Digest digestFromString = Digest.fromString(digestString); - Assertions.assertEquals(digest, digestFromString); - } - - /** - * Tests that SHA256 digest cannot be created with a digest that has extra - * bytes. - */ - @Test - public final void sha256IllegalDigest() { - final byte[] digest = getTestDigest(33); - Assertions.assertThrows(IllegalArgumentException.class, - () -> new Digest(DigestAlgorithm.SHA256, digest)); - } - - /** - * Tests that SHA384 digest can be created. - */ - @Test - public final void sha384() { - final byte[] digest = getTestDigest(48); - final Digest d = new Digest(DigestAlgorithm.SHA384, digest); - Assertions.assertNotNull(d); - } - - /** - * Tests that SHA384 digest can be recreated from a string. - */ - @Test - public final void testFromStringSHA384() { - final byte[] digestBytes = getTestDigest(48); - Digest digest = new Digest(DigestAlgorithm.SHA384, digestBytes); - String digestString = digest.toString(); - Digest digestFromString = Digest.fromString(digestString); - Assertions.assertEquals(digest, digestFromString); - } - - /** - * Tests that SHA384 digest cannot be created with a digest that has extra - * bytes. - */ - @Test - public final void sha384IllegalDigest() { - final byte[] digest = getTestDigest(49); - Assertions.assertThrows(IllegalArgumentException.class, - () -> new Digest(DigestAlgorithm.SHA384, digest)); - } - - /** - * Tests that SHA512 digest can be created. - */ - @Test - public final void sha512() { - final byte[] digest = getTestDigest(64); - final Digest d = new Digest(DigestAlgorithm.SHA512, digest); - Assertions.assertNotNull(d); - } - - /** - * Tests that SHA512 digest can be recreated from a string. - */ - @Test - public final void testFromStringSHA512() { - final byte[] digestBytes = getTestDigest(64); - Digest digest = new Digest(DigestAlgorithm.SHA512, digestBytes); - String digestString = digest.toString(); - Digest digestFromString = Digest.fromString(digestString); - Assertions.assertEquals(digest, digestFromString); - } - - /** - * Tests that SHA512 digest cannot be created with a digest that has extra - * bytes. - */ - @Test - public final void sha512IllegalDigest() { - final byte[] digest = getTestDigest(65); - Assertions.assertThrows(IllegalArgumentException.class, - () -> new Digest(DigestAlgorithm.SHA512, digest)); - } - - /** - * Tests that the correct DigestAlgorithm is returned by - * {@link Digest#getAlgorithm()}. - */ - @Test - public final void testGetAlgorithm() { - final Digest d = new Digest(DigestAlgorithm.SHA1, getTestDigest(20)); - Assertions.assertEquals(d.getAlgorithm(), DigestAlgorithm.SHA1); - } - - /** - * Tests that the bytes of the digest are created and do not affect the - * underlying state of the Digest instance. - */ - @Test - public final void testGetDigest() { - final Digest d = new Digest(DigestAlgorithm.SHA1, getTestDigest(20)); - final byte[] digestBytes = d.getDigest(); - final byte[] testBytes = getTestDigest(20); - Assertions.assertArrayEquals(digestBytes, testBytes); - digestBytes[0] = (byte) (digestBytes[0] + 1); - Assertions.assertArrayEquals(d.getDigest(), testBytes); - Assertions.assertFalse(Arrays.equals(d.getDigest(), digestBytes)); - } - - /** - * Tests that two Digests have equal hash code for same - * algorithm and digest. - */ - @Test - public final void testHashCodeEqual() { - final Digest d1 = new Digest(DigestAlgorithm.SHA1, getTestDigest(20)); - final Digest d2 = new Digest(DigestAlgorithm.SHA1, getTestDigest(20)); - Assertions.assertEquals(d2.hashCode(), d1.hashCode()); - } - - /** - * Tests that two Digests indicate MATCH when compared. - */ - @Test - public final void testMatchedComparison() { - final Digest d1 = new Digest(DigestAlgorithm.SHA1, getTestDigest(20)); - final Digest d2 = new Digest(DigestAlgorithm.SHA1, getTestDigest(20)); - Assertions.assertEquals(DigestComparisonResultType.MATCH, d1.compare(d2)); - Assertions.assertEquals(DigestComparisonResultType.MATCH, d2.compare(d1)); - } - - /** - * Tests that two Digests have unequal hash code for same - * digest but different algorithm. - */ - @Test - public final void testHashCodeNotEqualAlgorithm() { - final Digest d1 = new Digest(DigestAlgorithm.MD2, getTestDigest(16)); - final Digest d2 = new Digest(DigestAlgorithm.MD5, getTestDigest(16)); - Assertions.assertNotEquals(d2.hashCode(), d1.hashCode()); - } - - /** - * Tests that two Digests indicate MISMATCH when compared. - */ - @Test - public final void testMismatchAlgorithm() { - final Digest d1 = new Digest(DigestAlgorithm.MD2, getTestDigest(16)); - final Digest d2 = new Digest(DigestAlgorithm.MD5, getTestDigest(16)); - Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d1.compare(d2)); - Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d2.compare(d1)); - } - - /** - * Tests that two Digests have unequal hash code for same - * algorithm but different digest. - */ - @Test - public final void testHashCodeNotEqualDigest() { - final byte[] digest = getTestDigest(20); - final Digest d1 = new Digest(DigestAlgorithm.SHA1, digest); - digest[0] += 1; - final Digest d2 = new Digest(DigestAlgorithm.SHA1, digest); - Assertions.assertNotEquals(d2.hashCode(), d1.hashCode()); - Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d1.compare(d2)); - } - - /** - * Tests that two Digests are equal for same algorithm and - * digest. - */ - @Test - public final void testEqual() { - final Digest d1 = new Digest(DigestAlgorithm.SHA1, getTestDigest(20)); - final Digest d2 = new Digest(DigestAlgorithm.SHA1, getTestDigest(20)); - Assertions.assertEquals(d2, d1); - } - - /** - * Tests that two Digests are unequal for same digest but - * different algorithm. - */ - @Test - public final void testNotEqualAlgorithm() { - final Digest d1 = new Digest(DigestAlgorithm.MD2, getTestDigest(16)); - final Digest d2 = new Digest(DigestAlgorithm.MD5, getTestDigest(16)); - Assertions.assertNotEquals(d2, d1); - } - - /** - * Tests that two Digests are unequal for same algorithm but - * different digest. - */ - @Test - public final void testNotEqualDigest() { - final byte[] digest = getTestDigest(20); - final Digest d1 = new Digest(DigestAlgorithm.SHA1, digest); - digest[0] += 1; - final Digest d2 = new Digest(DigestAlgorithm.SHA1, digest); - Assertions.assertNotEquals(d2, d1); - } - - /** - * Tests that comparing a null Digest to a Digest indicates an UNKNOWN - * comparison type. - */ - @Test - public final void testCompareToNull() { - final Digest d1 = new Digest(DigestAlgorithm.MD2, getTestDigest(16)); - Assertions.assertEquals(DigestComparisonResultType.UNKNOWN, d1.compare(null)); - } - - /** - * Tests that comparing two Digests with hashes with values of zero gives a MATCH - * comparison result. - */ - @Test - public final void testCompareToDigestWithBothZeroizedHash() { - final Digest d1 = new Digest(DigestAlgorithm.SHA1, getZeroValueDigest(20)); - final Digest d2 = new Digest(DigestAlgorithm.SHA1, getZeroValueDigest(20)); - Assertions.assertEquals(DigestComparisonResultType.MATCH, d1.compare(d2)); - Assertions.assertEquals(DigestComparisonResultType.MATCH, d2.compare(d1)); - } - - /** - * Tests that comparing two Digests, one with a hash of value zero, gives a MISMATCH - * comparison result. - */ - @Test - public final void testCompareToDigestWithOneZeroizedHash() { - final Digest d1 = new Digest(DigestAlgorithm.SHA1, getTestDigest(20)); - final Digest d2 = new Digest(DigestAlgorithm.SHA1, getZeroValueDigest(20)); - Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d1.compare(d2)); - Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d2.compare(d1)); - } - - /** - * Tests that comparing two Digests with a hash of no data gives a MATCH - * comparison result. - */ - @Test - public final void testCompareToDigestWithBothEmptyHash() { - final Digest d1 = new Digest(DigestAlgorithm.SHA1, getEmptySHA1Digest()); - final Digest d2 = new Digest(DigestAlgorithm.SHA1, getEmptySHA1Digest()); - Assertions.assertEquals(DigestComparisonResultType.MATCH, d1.compare(d2)); - Assertions.assertEquals(DigestComparisonResultType.MATCH, d2.compare(d1)); - } - - /** - * Tests that comparing two Digests, one with a hash of no data, gives a MISMATCH - * comparison result. - */ - @Test - public final void testCompareToDigestWithOneEmptyHash() { - final Digest d1 = new Digest(DigestAlgorithm.SHA1, getTestDigest(20)); - final Digest d2 = new Digest(DigestAlgorithm.SHA1, getEmptySHA1Digest()); - Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d1.compare(d2)); - Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d2.compare(d1)); - } - - /** - * Tests that if someone tries to recreate a Digest using an invalid String, an error is thrown. - */ - @Test - public final void testFromStringInvalid() { - String invalidDigestString = "SHA1 00000000000000000000"; - Assertions.assertThrows(IllegalArgumentException.class, - () -> Digest.fromString(invalidDigestString)); - } - /** * Get a test SHA1 digest. * @@ -464,7 +48,455 @@ public class DigestTest { return new byte[count]; } + /** + * Tests that constructor throws a IllegalArgumentException when a + * null DigestAlgorithm is passed into constructor. + */ + @Test + public final void nullAlgorithm() { + final int count = 16; + final byte[] digest = getTestDigest(count); + Assertions.assertThrows(IllegalArgumentException.class, + () -> new Digest(null, digest)); + } + + /** + * Tests that constructor throws a IllegalArgumentException when a + * null digest is passed into constructor. + */ + @Test + public final void nullDigest() { + Assertions.assertThrows(IllegalArgumentException.class, + () -> new Digest(DigestAlgorithm.MD2, null)); + } + + /** + * Tests that constructor throws a IllegalArgumentException when an + * digest that is an empty array is passed into constructor. + */ + @Test + public final void emptyArrayDigest() { + Assertions.assertThrows(IllegalArgumentException.class, + () -> new Digest(DigestAlgorithm.MD2, new byte[0])); + } + + /** + * Tests that MD2 digest can be created. + */ + @Test + public final void md2() { + final int count = 16; + final byte[] digest = getTestDigest(count); + final Digest d = new Digest(DigestAlgorithm.MD2, digest); + Assertions.assertNotNull(d); + } + + /** + * Tests that an MD2 digest can be recreated from a string. + */ + @Test + public final void testFromStringMD2() { + final int count = 16; + final byte[] digestBytes = getTestDigest(count); + Digest digest = new Digest(DigestAlgorithm.MD2, digestBytes); + String digestString = digest.toString(); + Digest digestFromString = Digest.fromString(digestString); + Assertions.assertEquals(digest, digestFromString); + } + + /** + * Tests that MD2 digest cannot be created with a digest that has extra + * bytes. + */ + @Test + public final void md2IllegalDigest() { + final int count = 17; + final byte[] digest = getTestDigest(count); + Assertions.assertThrows(IllegalArgumentException.class, + () -> new Digest(DigestAlgorithm.MD2, digest)); + } + + /** + * Tests that MD5 digest can be created. + */ + @Test + public final void md5() { + final int count = 16; + final byte[] digest = getTestDigest(count); + final Digest d = new Digest(DigestAlgorithm.MD5, digest); + Assertions.assertNotNull(d); + } + + /** + * Tests that an MD5 digest can be recreated from a string. + */ + @Test + public final void testFromStringMD5() { + final int count = 16; + final byte[] digestBytes = getTestDigest(count); + Digest digest = new Digest(DigestAlgorithm.MD5, digestBytes); + String digestString = digest.toString(); + Digest digestFromString = Digest.fromString(digestString); + Assertions.assertEquals(digest, digestFromString); + } + + /** + * Tests that MD5 digest cannot be created with a digest that has extra + * bytes. + */ + @Test + public final void md5IllegalDigest() { + final int count = 17; + final byte[] digest = getTestDigest(count); + Assertions.assertThrows(IllegalArgumentException.class, + () -> new Digest(DigestAlgorithm.MD5, digest)); + } + + /** + * Tests that SHA1 digest can be created. + */ + @Test + public final void sha1() { + final int count = 20; + final byte[] digest = getTestDigest(count); + final Digest d = new Digest(DigestAlgorithm.SHA1, digest); + Assertions.assertNotNull(d); + } + + /** + * Tests that SHA1 digest can be recreated from a string. + */ + @Test + public final void testFromStringSHA1() { + final int count = 20; + final byte[] digestBytes = getTestDigest(count); + Digest digest = new Digest(DigestAlgorithm.SHA1, digestBytes); + String digestString = digest.toString(); + Digest digestFromString = Digest.fromString(digestString); + Assertions.assertEquals(digest, digestFromString); + } + + /** + * Tests that SHA1 digest cannot be created with a digest that has extra + * bytes. + */ + @Test + public final void sha1IllegalDigest() { + final int count = 21; + final byte[] digest = getTestDigest(count); + Assertions.assertThrows(IllegalArgumentException.class, + () -> new Digest(DigestAlgorithm.SHA1, digest)); + } + + /** + * Tests that SHA256 digest can be created. + */ + @Test + public final void sha256() { + final int count = 32; + final byte[] digest = getTestDigest(count); + final Digest d = new Digest(DigestAlgorithm.SHA256, digest); + Assertions.assertNotNull(d); + } + + /** + * Tests that SHA256 digest can be recreated from a string. + */ + @Test + public final void testFromStringSHA256() { + final int count = 32; + final byte[] digestBytes = getTestDigest(count); + Digest digest = new Digest(DigestAlgorithm.SHA256, digestBytes); + String digestString = digest.toString(); + Digest digestFromString = Digest.fromString(digestString); + Assertions.assertEquals(digest, digestFromString); + } + + /** + * Tests that SHA256 digest cannot be created with a digest that has extra + * bytes. + */ + @Test + public final void sha256IllegalDigest() { + final int count = 33; + final byte[] digest = getTestDigest(count); + Assertions.assertThrows(IllegalArgumentException.class, + () -> new Digest(DigestAlgorithm.SHA256, digest)); + } + + /** + * Tests that SHA384 digest can be created. + */ + @Test + public final void sha384() { + final int count = 48; + final byte[] digest = getTestDigest(count); + final Digest d = new Digest(DigestAlgorithm.SHA384, digest); + Assertions.assertNotNull(d); + } + + /** + * Tests that SHA384 digest can be recreated from a string. + */ + @Test + public final void testFromStringSHA384() { + final int count = 48; + final byte[] digestBytes = getTestDigest(count); + Digest digest = new Digest(DigestAlgorithm.SHA384, digestBytes); + String digestString = digest.toString(); + Digest digestFromString = Digest.fromString(digestString); + Assertions.assertEquals(digest, digestFromString); + } + + /** + * Tests that SHA384 digest cannot be created with a digest that has extra + * bytes. + */ + @Test + public final void sha384IllegalDigest() { + final int count = 49; + final byte[] digest = getTestDigest(count); + Assertions.assertThrows(IllegalArgumentException.class, + () -> new Digest(DigestAlgorithm.SHA384, digest)); + } + + /** + * Tests that SHA512 digest can be created. + */ + @Test + public final void sha512() { + final int count = 64; + final byte[] digest = getTestDigest(count); + final Digest d = new Digest(DigestAlgorithm.SHA512, digest); + Assertions.assertNotNull(d); + } + + /** + * Tests that SHA512 digest can be recreated from a string. + */ + @Test + public final void testFromStringSHA512() { + final int count = 64; + final byte[] digestBytes = getTestDigest(count); + Digest digest = new Digest(DigestAlgorithm.SHA512, digestBytes); + String digestString = digest.toString(); + Digest digestFromString = Digest.fromString(digestString); + Assertions.assertEquals(digest, digestFromString); + } + + /** + * Tests that SHA512 digest cannot be created with a digest that has extra + * bytes. + */ + @Test + public final void sha512IllegalDigest() { + final int count = 65; + final byte[] digest = getTestDigest(count); + Assertions.assertThrows(IllegalArgumentException.class, + () -> new Digest(DigestAlgorithm.SHA512, digest)); + } + + /** + * Tests that the correct DigestAlgorithm is returned by + * {@link Digest#getAlgorithm()}. + */ + @Test + public final void testGetAlgorithm() { + final int count = 20; + final Digest d = new Digest(DigestAlgorithm.SHA1, getTestDigest(count)); + Assertions.assertEquals(d.getAlgorithm(), DigestAlgorithm.SHA1); + } + + /** + * Tests that the bytes of the digest are created and do not affect the + * underlying state of the Digest instance. + */ + @Test + public final void testGetDigest() { + final int count = 20; + final Digest d = new Digest(DigestAlgorithm.SHA1, getTestDigest(count)); + final byte[] digestBytes = d.getDigest(); + final byte[] testBytes = getTestDigest(count); + Assertions.assertArrayEquals(digestBytes, testBytes); + digestBytes[0] = (byte) (digestBytes[0] + 1); + Assertions.assertArrayEquals(d.getDigest(), testBytes); + Assertions.assertFalse(Arrays.equals(d.getDigest(), digestBytes)); + } + + /** + * Tests that two Digests have equal hash code for same + * algorithm and digest. + */ + @Test + public final void testHashCodeEqual() { + final int count = 20; + final Digest d1 = new Digest(DigestAlgorithm.SHA1, getTestDigest(count)); + final Digest d2 = new Digest(DigestAlgorithm.SHA1, getTestDigest(count)); + Assertions.assertEquals(d2.hashCode(), d1.hashCode()); + } + + /** + * Tests that two Digests indicate MATCH when compared. + */ + @Test + public final void testMatchedComparison() { + final int count = 20; + final Digest d1 = new Digest(DigestAlgorithm.SHA1, getTestDigest(count)); + final Digest d2 = new Digest(DigestAlgorithm.SHA1, getTestDigest(count)); + Assertions.assertEquals(DigestComparisonResultType.MATCH, d1.compare(d2)); + Assertions.assertEquals(DigestComparisonResultType.MATCH, d2.compare(d1)); + } + + /** + * Tests that two Digests have unequal hash code for same + * digest but different algorithm. + */ + @Test + public final void testHashCodeNotEqualAlgorithm() { + final int count = 16; + final Digest d1 = new Digest(DigestAlgorithm.MD2, getTestDigest(count)); + final Digest d2 = new Digest(DigestAlgorithm.MD5, getTestDigest(count)); + Assertions.assertNotEquals(d2.hashCode(), d1.hashCode()); + } + + /** + * Tests that two Digests indicate MISMATCH when compared. + */ + @Test + public final void testMismatchAlgorithm() { + final int count = 16; + final Digest d1 = new Digest(DigestAlgorithm.MD2, getTestDigest(count)); + final Digest d2 = new Digest(DigestAlgorithm.MD5, getTestDigest(count)); + Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d1.compare(d2)); + Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d2.compare(d1)); + } + + /** + * Tests that two Digests have unequal hash code for same + * algorithm but different digest. + */ + @Test + public final void testHashCodeNotEqualDigest() { + final int count = 20; + final byte[] digest = getTestDigest(count); + final Digest d1 = new Digest(DigestAlgorithm.SHA1, digest); + digest[0] += 1; + final Digest d2 = new Digest(DigestAlgorithm.SHA1, digest); + Assertions.assertNotEquals(d2.hashCode(), d1.hashCode()); + Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d1.compare(d2)); + } + + /** + * Tests that two Digests are equal for same algorithm and + * digest. + */ + @Test + public final void testEqual() { + final int count = 20; + final Digest d1 = new Digest(DigestAlgorithm.SHA1, getTestDigest(count)); + final Digest d2 = new Digest(DigestAlgorithm.SHA1, getTestDigest(count)); + Assertions.assertEquals(d2, d1); + } + + /** + * Tests that two Digests are unequal for same digest but + * different algorithm. + */ + @Test + public final void testNotEqualAlgorithm() { + final int count = 16; + final Digest d1 = new Digest(DigestAlgorithm.MD2, getTestDigest(count)); + final Digest d2 = new Digest(DigestAlgorithm.MD5, getTestDigest(count)); + Assertions.assertNotEquals(d2, d1); + } + + /** + * Tests that two Digests are unequal for same algorithm but + * different digest. + */ + @Test + public final void testNotEqualDigest() { + final int count = 20; + final byte[] digest = getTestDigest(count); + final Digest d1 = new Digest(DigestAlgorithm.SHA1, digest); + digest[0] += 1; + final Digest d2 = new Digest(DigestAlgorithm.SHA1, digest); + Assertions.assertNotEquals(d2, d1); + } + + /** + * Tests that comparing a null Digest to a Digest indicates an UNKNOWN + * comparison type. + */ + @Test + public final void testCompareToNull() { + final int count = 16; + final Digest d1 = new Digest(DigestAlgorithm.MD2, getTestDigest(count)); + Assertions.assertEquals(DigestComparisonResultType.UNKNOWN, d1.compare(null)); + } + + /** + * Tests that comparing two Digests with hashes with values of zero gives a MATCH + * comparison result. + */ + @Test + public final void testCompareToDigestWithBothZeroizedHash() { + final int count = 20; + final Digest d1 = new Digest(DigestAlgorithm.SHA1, getZeroValueDigest(count)); + final Digest d2 = new Digest(DigestAlgorithm.SHA1, getZeroValueDigest(count)); + Assertions.assertEquals(DigestComparisonResultType.MATCH, d1.compare(d2)); + Assertions.assertEquals(DigestComparisonResultType.MATCH, d2.compare(d1)); + } + + /** + * Tests that comparing two Digests, one with a hash of value zero, gives a MISMATCH + * comparison result. + */ + @Test + public final void testCompareToDigestWithOneZeroizedHash() { + final int count = 20; + final Digest d1 = new Digest(DigestAlgorithm.SHA1, getTestDigest(count)); + final Digest d2 = new Digest(DigestAlgorithm.SHA1, getZeroValueDigest(count)); + Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d1.compare(d2)); + Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d2.compare(d1)); + } + + /** + * Tests that comparing two Digests with a hash of no data gives a MATCH + * comparison result. + */ + @Test + public final void testCompareToDigestWithBothEmptyHash() { + final Digest d1 = new Digest(DigestAlgorithm.SHA1, getEmptySHA1Digest()); + final Digest d2 = new Digest(DigestAlgorithm.SHA1, getEmptySHA1Digest()); + Assertions.assertEquals(DigestComparisonResultType.MATCH, d1.compare(d2)); + Assertions.assertEquals(DigestComparisonResultType.MATCH, d2.compare(d1)); + } + + /** + * Tests that comparing two Digests, one with a hash of no data, gives a MISMATCH + * comparison result. + */ + @Test + public final void testCompareToDigestWithOneEmptyHash() { + final int count = 20; + final Digest d1 = new Digest(DigestAlgorithm.SHA1, getTestDigest(count)); + final Digest d2 = new Digest(DigestAlgorithm.SHA1, getEmptySHA1Digest()); + Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d1.compare(d2)); + Assertions.assertEquals(DigestComparisonResultType.MISMATCH, d2.compare(d1)); + } + + /** + * Tests that if someone tries to recreate a Digest using an invalid String, an error is thrown. + */ + @Test + public final void testFromStringInvalid() { + String invalidDigestString = "SHA1 00000000000000000000"; + Assertions.assertThrows(IllegalArgumentException.class, + () -> Digest.fromString(invalidDigestString)); + } + private byte[] getEmptySHA1Digest() { - return DigestUtils.sha1(new byte[]{}); + return DigestUtils.sha1(new byte[] {}); } } diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/FirmwareInfoTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/FirmwareInfoTest.java index e337399f..5c8aad51 100644 --- a/HIRS_Utils/src/test/java/hirs/data/persist/FirmwareInfoTest.java +++ b/HIRS_Utils/src/test/java/hirs/data/persist/FirmwareInfoTest.java @@ -2,11 +2,11 @@ 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; +import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED; + /** * FirmwareInfoTest is a unit test class for FirmwareInfo. */ @@ -15,18 +15,20 @@ 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 int PRIMARY_SIZE = 257; + private static final int SECONDARY_SIZE = 33; private static final String LONG_BIOS_VENDOR = StringUtils.rightPad( - "test bios vendor", - 257 + BIOS_VENDOR, + PRIMARY_SIZE ); private static final String LONG_BIOS_VERSION = StringUtils.rightPad( - "test bios version", - 257 + BIOS_VERSION, + PRIMARY_SIZE ); private static final String LONG_BIOS_RELEASE_DATE = StringUtils.rightPad( - "test bios release date", - 33 + BIOS_RELEASE_DATE, + SECONDARY_SIZE ); /** @@ -44,8 +46,8 @@ public class FirmwareInfoTest { 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()); + Assertions.assertEquals(NOT_SPECIFIED, firmwareInfo.getBiosVersion()); + Assertions.assertEquals(NOT_SPECIFIED, firmwareInfo.getBiosReleaseDate()); } /** diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/HardwareInfoTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/HardwareInfoTest.java index a5ab1f4e..d0ffee83 100644 --- a/HIRS_Utils/src/test/java/hirs/data/persist/HardwareInfoTest.java +++ b/HIRS_Utils/src/test/java/hirs/data/persist/HardwareInfoTest.java @@ -1,12 +1,12 @@ 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; +import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED; + /** * HardwareInfoTest is a unit test class for HardwareInfo. */ @@ -18,30 +18,32 @@ public class HardwareInfoTest { 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 int PRIMARY_SIZE = 257; + private static final int SECONDARY_SIZE = 65; private static final String LONG_MANUFACTURER = StringUtils.rightPad( - "test manufacturer", - 257 + MANUFACTURER, + PRIMARY_SIZE ); private static final String LONG_PRODUCT_NAME = StringUtils.rightPad( - "test product name", - 257 + PRODUCT_NAME, + PRIMARY_SIZE ); private static final String LONG_VERSION = StringUtils.rightPad( - "test version", - 65 + VERSION, + SECONDARY_SIZE ); private static final String LONG_SERIAL_NUMBER = StringUtils.rightPad( - "test serial number", - 257 + SERIAL_NUMBER, + PRIMARY_SIZE ); private static final String LONG_CHASSIS_SERIAL_NUMBER = StringUtils.rightPad( - "test chassis serial number", - 257 + CHASSIS_SERIAL_NUMBER, + PRIMARY_SIZE ); private static final String LONG_BASEBOARD_SERIAL_NUMBER = StringUtils.rightPad( - "test baseboard serial number", - 257 + BASEBOARD_SERIAL_NUMBER, + PRIMARY_SIZE ); /** diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/NetworkInfoTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/NetworkInfoTest.java index f8476597..042f0037 100644 --- a/HIRS_Utils/src/test/java/hirs/data/persist/NetworkInfoTest.java +++ b/HIRS_Utils/src/test/java/hirs/data/persist/NetworkInfoTest.java @@ -1,12 +1,12 @@ 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; +import java.net.InetAddress; +import java.net.UnknownHostException; + /** * NetworkInfoTest is a unit test class for NetworkInfo. */ @@ -17,6 +17,15 @@ public class NetworkInfoTest { private static final byte[] MAC_ADDRESS = new byte[] {11, 22, 33, 44, 55, 66}; + private static InetAddress getTestIpAddress() { + try { + final byte[] byteAddress = new byte[] {127, 0, 0, 1}; + return InetAddress.getByAddress(byteAddress); + } catch (UnknownHostException e) { + return null; + } + } + /** * Tests instantiation of a NetworkInfo object. */ @@ -103,14 +112,14 @@ public class NetworkInfoTest { * Tests that hashcodes generated by NetworkInfo objects with different IP * addresses are not equal. * - * @throws UnknownHostException - * in case the InetAddress is not created correctly + * @throws UnknownHostException in case the InetAddress is not created correctly */ @Test public final void testHashCodeNotEqualsIpAddress() throws UnknownHostException { + final byte[] byteAddress = new byte[] {127, 0, 0, 2}; final InetAddress ipAddress2 = - InetAddress.getByAddress(new byte[] {127, 0, 0, 2}); + InetAddress.getByAddress(byteAddress); NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); NetworkInfo ni2 = new NetworkInfo(HOSTNAME, ipAddress2, MAC_ADDRESS); Assertions.assertNotEquals(ni2.hashCode(), ni1.hashCode()); @@ -155,13 +164,13 @@ public class NetworkInfoTest { * Tests that two NetworkInfo objects are not equal if they have different * IP addresses. * - * @throws UnknownHostException - * in case InetAddress is not created correctly + * @throws UnknownHostException in case InetAddress is not created correctly */ @Test public final void testNotEqualsIpAddress() throws UnknownHostException { + final byte[] byteAddress = new byte[] {127, 0, 0, 2}; final InetAddress ipAddress2 = - InetAddress.getByAddress(new byte[] {127, 0, 0, 2}); + InetAddress.getByAddress(byteAddress); NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); NetworkInfo ni2 = new NetworkInfo(HOSTNAME, ipAddress2, MAC_ADDRESS); Assertions.assertNotEquals(ni2, ni1); @@ -178,12 +187,4 @@ public class NetworkInfoTest { 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; - } - } } diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/OSInfoTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/OSInfoTest.java index 20b82abf..39d64863 100644 --- a/HIRS_Utils/src/test/java/hirs/data/persist/OSInfoTest.java +++ b/HIRS_Utils/src/test/java/hirs/data/persist/OSInfoTest.java @@ -1,12 +1,12 @@ 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; +import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED; + /** * OSInfoTest is a unit test class for OSInfo. */ @@ -17,10 +17,12 @@ public class OSInfoTest { 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 int PRIMARY_SIZE = 257; + private static final int SECONDARY_SIZE = 33; - 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); + private static final String LONG_OS_NAME = StringUtils.rightPad(OS_NAME, PRIMARY_SIZE); + private static final String LONG_OS_VERSION = StringUtils.rightPad(OS_VERSION, PRIMARY_SIZE); + private static final String LONG_OS_ARCH = StringUtils.rightPad(OS_ARCH, SECONDARY_SIZE); /** * Tests instantiation of an OSInfo object. @@ -63,7 +65,7 @@ public class OSInfoTest { @Test public final void osNameNullTest() { Assertions.assertThrows(IllegalArgumentException.class, () -> - new OSInfo(null, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); + new OSInfo(null, OS_VERSION, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); } /** @@ -72,7 +74,7 @@ public class OSInfoTest { @Test public final void osVersionNullTest() { Assertions.assertThrows(IllegalArgumentException.class, () -> - new OSInfo(OS_NAME, null, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); + new OSInfo(OS_NAME, null, OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); } /** @@ -81,7 +83,7 @@ public class OSInfoTest { @Test public final void osArchNullTest() { Assertions.assertThrows(IllegalArgumentException.class, () -> - new OSInfo(OS_NAME, OS_VERSION, null, DISTRIBUTION, DISTRIBUTION_RELEASE)); + new OSInfo(OS_NAME, OS_VERSION, null, DISTRIBUTION, DISTRIBUTION_RELEASE)); } /** @@ -108,7 +110,7 @@ public class OSInfoTest { @Test public final void osArchLongTest() { Assertions.assertThrows(IllegalArgumentException.class, () -> - new OSInfo(OS_NAME, OS_VERSION, LONG_OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); + new OSInfo(OS_NAME, OS_VERSION, LONG_OS_ARCH, DISTRIBUTION, DISTRIBUTION_RELEASE)); } /** diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/package-info.java b/HIRS_Utils/src/test/java/hirs/data/persist/package-info.java new file mode 100644 index 00000000..387d0845 --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/data/persist/package-info.java @@ -0,0 +1 @@ +package hirs.data.persist; diff --git a/HIRS_Utils/src/test/java/hirs/tpm/eventlog/uefi/UefiProcessingTest.java b/HIRS_Utils/src/test/java/hirs/tpm/eventlog/uefi/UefiProcessingTest.java index 183af293..f069208e 100644 --- a/HIRS_Utils/src/test/java/hirs/tpm/eventlog/uefi/UefiProcessingTest.java +++ b/HIRS_Utils/src/test/java/hirs/tpm/eventlog/uefi/UefiProcessingTest.java @@ -1,25 +1,29 @@ package hirs.tpm.eventlog.uefi; -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.security.NoSuchAlgorithmException; -import java.security.cert.CertificateException; - import com.eclipsesource.json.JsonObject; +import hirs.utils.HexUtils; import hirs.utils.JsonUtils; -import hirs.utils.tpm.eventlog.uefi.*; +import hirs.utils.tpm.eventlog.uefi.UefiDevicePath; +import hirs.utils.tpm.eventlog.uefi.UefiFirmware; +import hirs.utils.tpm.eventlog.uefi.UefiGuid; +import hirs.utils.tpm.eventlog.uefi.UefiPartition; +import hirs.utils.tpm.eventlog.uefi.UefiVariable; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import hirs.utils.HexUtils; - import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; + /** * Class for testing TCG Event Log processing of UEFI defined Data. */ @@ -61,7 +65,7 @@ public class UefiProcessingTest { * @throws IOException when processing the test fails. * @throws NoSuchAlgorithmException if non TCG Algorithm is encountered. * @throws CertificateException if parsing issue for X509 cert is encountered. - * @throws URISyntaxException File location exception + * @throws URISyntaxException File location exception */ @Test public final void testUefiVariables() throws IOException, @@ -70,7 +74,7 @@ public class UefiProcessingTest { Path jsonPath = Paths.get(this.getClass() .getResource(JSON_FILE).toURI()); String uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream(UEFI_VARIABLE_BOOT), - "UTF-8"); + StandardCharsets.UTF_8); byte[] uefiVariableBytes = HexUtils.hexStringToByteArray(uefiTxt); UefiVariable uefiVariable = new UefiVariable(uefiVariableBytes); UefiGuid guid = uefiVariable.getUefiVarGuid(); @@ -83,7 +87,7 @@ public class UefiProcessingTest { uefiTxt = IOUtils.toString(this.getClass() .getResourceAsStream(UEFI_VARIABLE_BOOT_SECURE_BOOT), - "UTF-8"); + StandardCharsets.UTF_8); uefiVariableBytes = HexUtils.hexStringToByteArray(uefiTxt); uefiVariable = new UefiVariable(uefiVariableBytes); guid = uefiVariable.getUefiVarGuid(); @@ -94,7 +98,7 @@ public class UefiProcessingTest { Assertions.assertEquals("SecureBoot", varName); uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream( - UEFI_VARIABLE_BOOT_DRIVER_CONFIG_KEK), "UTF-8"); + UEFI_VARIABLE_BOOT_DRIVER_CONFIG_KEK), StandardCharsets.UTF_8); uefiVariableBytes = HexUtils.hexStringToByteArray(uefiTxt); uefiVariable = new UefiVariable(uefiVariableBytes); varName = uefiVariable.getEfiVarName(); @@ -107,7 +111,7 @@ public class UefiProcessingTest { * @throws IOException when processing the test fails. * @throws NoSuchAlgorithmException if non TCG Algorithm is encountered. * @throws CertificateException if parsing issue for X509 cert is encountered. - * @throws URISyntaxException File location exception + * @throws URISyntaxException File location exception */ @Test public final void testUefiPartiton() throws IOException, @@ -116,7 +120,7 @@ public class UefiProcessingTest { Path jsonPath = Paths.get(this.getClass() .getResource(JSON_FILE).toURI()); String uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream(UEFI_GPT_EVENT), - "UTF-8"); + StandardCharsets.UTF_8); byte[] uefiPartitionBytes = HexUtils.hexStringToByteArray(uefiTxt); UefiPartition gptPart = new UefiPartition(uefiPartitionBytes); String gptPartName = gptPart.getPartitionName(); @@ -143,29 +147,33 @@ public class UefiProcessingTest { CertificateException, NoSuchAlgorithmException { LOGGER.debug("Testing the parsing of Uefi Firmware Blob"); String uefiTxt = IOUtils.toString(this.getClass() - .getResourceAsStream(UEFI_FW_BLOB), "UTF-8"); + .getResourceAsStream(UEFI_FW_BLOB), StandardCharsets.UTF_8); byte[] uefiFwBlobBytes = HexUtils.hexStringToByteArray(uefiTxt); UefiFirmware uefiFWBlob = new UefiFirmware(uefiFwBlobBytes); int fwAddress = uefiFWBlob.getPhysicalBlobAddress(); int fwLength = uefiFWBlob.getBlobLength(); - Assertions.assertEquals(1797287936, fwAddress); - Assertions.assertEquals(851968, fwLength); + + final int expectedFwAddress = 1797287936; + Assertions.assertEquals(expectedFwAddress, fwAddress); + + final int expectedFwLength = 851968; + Assertions.assertEquals(expectedFwLength, fwLength); } /** * Tests the processing of a UEFI defined Device Path. * - * @throws IOException when processing the test fails. + * @throws IOException when processing the test fails. * @throws URISyntaxException File location exception */ @Test public final void testUefiDevicePath() throws IOException, URISyntaxException { LOGGER.debug("Testing the parsing of Uefi Device Path"); String uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream(UEFI_DEVICE_PATH), - "UTF-8"); + StandardCharsets.UTF_8); byte[] uefiFwBlobBytes = HexUtils.hexStringToByteArray(uefiTxt); UefiDevicePath uefiDevPath = new UefiDevicePath(uefiFwBlobBytes); String devPathType = uefiDevPath.getType(); Assertions.assertEquals("Media Device Path", devPathType); } -} \ No newline at end of file +} diff --git a/HIRS_Utils/src/test/java/hirs/tpm/eventlog/uefi/package-info.java b/HIRS_Utils/src/test/java/hirs/tpm/eventlog/uefi/package-info.java new file mode 100644 index 00000000..8762db47 --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/tpm/eventlog/uefi/package-info.java @@ -0,0 +1 @@ +package hirs.tpm.eventlog.uefi; diff --git a/HIRS_Utils/src/test/java/hirs/utils/BouncyCastleUtilsTest.java b/HIRS_Utils/src/test/java/hirs/utils/BouncyCastleUtilsTest.java index 14116536..6588051c 100644 --- a/HIRS_Utils/src/test/java/hirs/utils/BouncyCastleUtilsTest.java +++ b/HIRS_Utils/src/test/java/hirs/utils/BouncyCastleUtilsTest.java @@ -2,8 +2,9 @@ package hirs.utils; import org.apache.logging.log4j.util.Strings; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertTrue; + import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Tests methods in the (@link BouncyCastleUtils) utility class. @@ -46,4 +47,4 @@ public class BouncyCastleUtilsTest { assertFalse(BouncyCastleUtils.x500NameCompare( MALFORMED_RDN_STRING, MALFORMED_RDN_STRING)); } -} \ No newline at end of file +} diff --git a/HIRS_Utils/src/test/java/hirs/utils/HexUtilsTest.java b/HIRS_Utils/src/test/java/hirs/utils/HexUtilsTest.java index 9e3ba416..11fd72a8 100644 --- a/HIRS_Utils/src/test/java/hirs/utils/HexUtilsTest.java +++ b/HIRS_Utils/src/test/java/hirs/utils/HexUtilsTest.java @@ -1,6 +1,7 @@ package hirs.utils; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -14,11 +15,10 @@ public class HexUtilsTest { */ @Test public void testHexStringToByteArray() { - String s = "abcd1234"; - byte[] target = {-85, -51, 18, 52}; - - byte[] b = HexUtils.hexStringToByteArray(s); - assertArrayEquals(b, target); + final String testString = "abcd1234"; + final byte[] expectedBytes = {-85, -51, 18, 52}; + final byte[] actualBytes = HexUtils.hexStringToByteArray(testString); + assertArrayEquals(expectedBytes, actualBytes); } /** @@ -26,11 +26,10 @@ public class HexUtilsTest { */ @Test public void testByteArrayToHexString() { - String target = "abcd1234"; - byte[] b = {-85, -51, 18, 52}; - - String s = HexUtils.byteArrayToHexString(b); - assertEquals(s, target); + final byte[] byteArray = {-85, -51, 18, 52}; + final String expectedString = "abcd1234"; + final String actualString = HexUtils.byteArrayToHexString(byteArray); + assertEquals(expectedString, actualString); } /** @@ -38,11 +37,10 @@ public class HexUtilsTest { */ @Test public void testByteArrayToHexStringConditional() { - String target = "abcd0100"; - byte[] b = {-85, -51, 1, 0}; - - String s = HexUtils.byteArrayToHexString(b); - assertEquals(s, target); + final byte[] byteArray = {-85, -51, 1, 0}; + final String expectedHexString = "abcd0100"; + final String actualHexString = HexUtils.byteArrayToHexString(byteArray); + assertEquals(expectedHexString, actualHexString); } /** @@ -50,9 +48,10 @@ public class HexUtilsTest { */ @Test public void testHexToInt() { - String s = "ff"; - Integer i = HexUtils.hexToInt(s); - assertEquals((int) i, HexUtils.FF_BYTE); + final String testString = "ff"; + final int expectedInt = HexUtils.FF_BYTE; + final Integer actualInt = HexUtils.hexToInt(testString); + assertEquals(expectedInt, (int) actualInt); } /** @@ -60,14 +59,11 @@ public class HexUtilsTest { */ @Test public void testSubarray() { - byte[] b = {-85, -51, 18, 52}; - byte[] target = {-51, 18}; - - byte[] sub = HexUtils.subarray(b, 1, 2); - - assertArrayEquals(sub, target); + final byte[] b = {-85, -51, 18, 52}; + final byte[] expectedSubArray = {-51, 18}; + final byte[] actualSubArray = HexUtils.subarray(b, 1, 2); + assertArrayEquals(expectedSubArray, actualSubArray); } - } diff --git a/HIRS_Utils/src/test/java/hirs/utils/StringValidatorTest.java b/HIRS_Utils/src/test/java/hirs/utils/StringValidatorTest.java index 18efdbc7..200d1e25 100644 --- a/HIRS_Utils/src/test/java/hirs/utils/StringValidatorTest.java +++ b/HIRS_Utils/src/test/java/hirs/utils/StringValidatorTest.java @@ -1,11 +1,12 @@ package hirs.utils; import org.apache.logging.log4j.Logger; -import org.mockito.Mockito; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertNull; +import org.mockito.Mockito; + import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.times; /** @@ -114,10 +115,10 @@ public class StringValidatorTest { @Test public void testValidateMaxLengthFailOnLongString() { assertThrows(IllegalArgumentException.class, () -> - assertEquals( - StringValidator.check(NONEMPTY_VALUE, FIELD_NAME).maxLength(SMALL_LENGTH).getValue(), - NONEMPTY_VALUE - )); + assertEquals( + StringValidator.check(NONEMPTY_VALUE, FIELD_NAME).maxLength(SMALL_LENGTH).getValue(), + NONEMPTY_VALUE + )); } /** @@ -138,8 +139,8 @@ public class StringValidatorTest { @Test public void testValidateSeveralConditionsFailOnLast() { assertThrows(IllegalArgumentException.class, () -> - StringValidator.check(NONEMPTY_VALUE, FIELD_NAME) - .notNull().notBlank().maxLength(SMALL_LENGTH)); + StringValidator.check(NONEMPTY_VALUE, FIELD_NAME) + .notNull().notBlank().maxLength(SMALL_LENGTH)); } /** @@ -155,4 +156,4 @@ public class StringValidatorTest { } Mockito.verify(mockLogger, times(1)).error(Mockito.anyString()); } -} \ No newline at end of file +} diff --git a/HIRS_Utils/src/test/java/hirs/utils/package-info.java b/HIRS_Utils/src/test/java/hirs/utils/package-info.java new file mode 100644 index 00000000..361ebc74 --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/utils/package-info.java @@ -0,0 +1 @@ +package hirs.utils; diff --git a/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/TCGEventLogTest.java b/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/TCGEventLogTest.java index 7fe0aefd..b3c81ac5 100644 --- a/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/TCGEventLogTest.java +++ b/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/TCGEventLogTest.java @@ -1,20 +1,20 @@ package hirs.utils.tpm.eventlog; +import org.apache.commons.io.IOUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + import java.io.IOException; import java.io.InputStream; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.util.Arrays; -import org.apache.commons.io.IOUtils; - -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.LogManager; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import static hirs.utils.tpm.eventlog.TCGEventLog.PCR_COUNT; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -36,103 +36,108 @@ public class TCGEventLogTest { @BeforeAll public static final void setup() { LOGGER.debug("retrieving session factory"); - } /** * Closes the SessionFactory from setup. */ @AfterAll - public static final void tearDown() { - LOGGER.debug("closing session factory"); - } + public static final void tearDown() { + LOGGER.debug("closing session factory"); + } - /** - * Tests the processing of a crypto agile event log. - * @throws IOException when processing the test fails - * @throws NoSuchAlgorithmException if an unknown algorithm is encountered. - * @throws CertificateException if a certificate fails to parse. - */ + /** + * Tests the processing of a crypto agile event log. + * + * @throws IOException when processing the test fails + * @throws NoSuchAlgorithmException if an unknown algorithm is encountered. + * @throws CertificateException if a certificate fails to parse. + */ @Test public final void testCryptoAgileTCGEventLog() throws IOException, CertificateException, - NoSuchAlgorithmException { - LOGGER.debug("Testing the parsing of a Crypto Agile formatted TCG Event Log"); - InputStream log, pcrs; - boolean testPass = true; - log = this.getClass().getResourceAsStream(DEFAULT_EVENT_LOG); - byte[] rawLogBytes = IOUtils.toByteArray(log); - TCGEventLog evlog = new TCGEventLog(rawLogBytes, false, false, false); - String[] pcrFromLog = evlog.getExpectedPCRValues(); - pcrs = this.getClass().getResourceAsStream(DEFAULT_EXPECTED_PCRS); - Object[] pcrObj = IOUtils.readLines(pcrs, "UTF-8").toArray(); - String[] pcrTxt = Arrays.copyOf(pcrObj, pcrObj.length, String[].class); - - // Test 1 get all PCRs - for (int i = 0; i < 24; i++) { - if (pcrFromLog[i].compareToIgnoreCase(pcrTxt[i]) != 0) { - testPass = false; - LOGGER.error("\ntestTCGEventLogProcessorParser error with PCR " + i); - } - } - assertTrue(testPass); - - // Test 2 get an individual PCR - String pcr3 = evlog.getExpectedPCRValue(3); - assertThat(pcrFromLog[3], equalTo(pcr3)); + NoSuchAlgorithmException { + LOGGER.debug("Testing the parsing of a Crypto Agile formatted TCG Event Log"); - // Test 3 check the Algorithm String Identifier used in the log - String algStr = evlog.getEventLogHashAlgorithm(); - assertThat("TPM_ALG_SHA256", equalTo(algStr)); + // setup + final InputStream log = this.getClass().getResourceAsStream(DEFAULT_EVENT_LOG); + final InputStream pcrs = this.getClass().getResourceAsStream(DEFAULT_EXPECTED_PCRS); + final byte[] rawLogBytes = IOUtils.toByteArray(log); + final TCGEventLog evlog = new TCGEventLog(rawLogBytes, false, false, false); + final String[] pcrFromLog = evlog.getExpectedPCRValues(); + final Object[] pcrObj = IOUtils.readLines(pcrs, "UTF-8").toArray(); + final String[] pcrTxt = Arrays.copyOf(pcrObj, pcrObj.length, String[].class); - // Test 4 check the Algorithm # Identifier used in the log - int id = evlog.getEventLogHashAlgorithmID(); - assertThat(TcgTpmtHa.TPM_ALG_SHA256, equalTo(id)); - - LOGGER.debug("OK. Parsing of a Crypto Agile Format Success"); + boolean testPass = true; + + // Test 1 get all PCRs + for (int i = 0; i < PCR_COUNT; i++) { + if (pcrFromLog[i].compareToIgnoreCase(pcrTxt[i]) != 0) { + testPass = false; + LOGGER.error("\ntestTCGEventLogProcessorParser error with PCR {}", i); + } + } + assertTrue(testPass); + + // Test 2 get an individual PCR + final int pcrIndex = 3; + String pcr3 = evlog.getExpectedPCRValue(pcrIndex); + assertThat(pcrFromLog[pcrIndex], equalTo(pcr3)); + + // Test 3 check the Algorithm String Identifier used in the log + String algStr = evlog.getEventLogHashAlgorithm(); + assertThat("TPM_ALG_SHA256", equalTo(algStr)); + + // Test 4 check the Algorithm # Identifier used in the log + int id = evlog.getEventLogHashAlgorithmID(); + assertThat(TcgTpmtHa.TPM_ALG_SHA256, equalTo(id)); + + LOGGER.debug("OK. Parsing of a Crypto Agile Format Success"); } /** * Tests the processing of a SHA1 formatted Event log. - * @throws IOException when processing the test fails + * + * @throws IOException when processing the test fails * @throws NoSuchAlgorithmException if an unknown algorithm is encountered. - * @throws CertificateException if a certificate fails to parse. + * @throws CertificateException if a certificate fails to parse. */ @Test public final void testSHA1TCGEventLog() throws IOException, CertificateException, - NoSuchAlgorithmException { - LOGGER.debug("Testing the parsing of a SHA1 formated TCG Event Log"); - InputStream log, pcrs; - boolean testPass = true; - log = this.getClass().getResourceAsStream(SHA1_EVENT_LOG); - byte[] rawLogBytes = IOUtils.toByteArray(log); - TCGEventLog evlog = new TCGEventLog(rawLogBytes, false, false, false); - String[] pcrFromLog = evlog.getExpectedPCRValues(); - pcrs = this.getClass().getResourceAsStream(SHA1_EXPECTED_PCRS); - Object[] pcrObj = IOUtils.readLines(pcrs, "UTF-8").toArray(); - String[] pcrTxt = Arrays.copyOf(pcrObj, pcrObj.length, String[].class); - - // Test 1 get all PCRs - for (int i = 0; i < 24; i++) { - if (pcrFromLog[i].compareToIgnoreCase(pcrTxt[i]) != 0) { - testPass = false; - LOGGER.error("\ntestTCGEventLogProcessorParser error with PCR " + i); - } - } - assertTrue(testPass); - - // Test 2 get an individual PCR - String pcr0 = evlog.getExpectedPCRValue(0); - assertThat(pcrFromLog[0], equalTo(pcr0)); - - // Test 3 check the Algorithm String Identifier used in the log - String algStr = evlog.getEventLogHashAlgorithm(); - assertThat("TPM_ALG_SHA1", equalTo(algStr)); + NoSuchAlgorithmException { + LOGGER.debug("Testing the parsing of a SHA1 formated TCG Event Log"); - // Test 4 check the Algorithm # Identifier used in the log - int id = evlog.getEventLogHashAlgorithmID(); - assertThat(TcgTpmtHa.TPM_ALG_SHA1, equalTo(id)); - - LOGGER.debug("OK. Parsing of a SHA1 formatted TCG Event Log Success"); - } - + // setup + final InputStream log = this.getClass().getResourceAsStream(SHA1_EVENT_LOG); + final InputStream pcrs = this.getClass().getResourceAsStream(SHA1_EXPECTED_PCRS); + final byte[] rawLogBytes = IOUtils.toByteArray(log); + final TCGEventLog evlog = new TCGEventLog(rawLogBytes, false, false, false); + final String[] pcrFromLog = evlog.getExpectedPCRValues(); + final Object[] pcrObj = IOUtils.readLines(pcrs, "UTF-8").toArray(); + final String[] pcrTxt = Arrays.copyOf(pcrObj, pcrObj.length, String[].class); + + boolean testPass = true; + + // Test 1 get all PCRs + for (int i = 0; i < PCR_COUNT; i++) { + if (pcrFromLog[i].compareToIgnoreCase(pcrTxt[i]) != 0) { + testPass = false; + LOGGER.error("\ntestTCGEventLogProcessorParser error with PCR {}", i); + } + } + assertTrue(testPass); + + // Test 2 get an individual PCR + String pcr0 = evlog.getExpectedPCRValue(0); + assertThat(pcrFromLog[0], equalTo(pcr0)); + + // Test 3 check the Algorithm String Identifier used in the log + String algStr = evlog.getEventLogHashAlgorithm(); + assertThat("TPM_ALG_SHA1", equalTo(algStr)); + + // Test 4 check the Algorithm # Identifier used in the log + int id = evlog.getEventLogHashAlgorithmID(); + assertThat(TcgTpmtHa.TPM_ALG_SHA1, equalTo(id)); + + LOGGER.debug("OK. Parsing of a SHA1 formatted TCG Event Log Success"); + } } diff --git a/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/package-info.java b/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/package-info.java new file mode 100644 index 00000000..979c6b4f --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/utils/tpm/eventlog/package-info.java @@ -0,0 +1 @@ +package hirs.utils.tpm.eventlog; diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index dbae748b..cf854c69 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -171,7 +171,12 @@ - + + + + +