issue-844: Finished cleaning up code using checkstyles guidelines on HIRS-Utils test module. Finishing up cleaning on main module.

This commit is contained in:
TheSilentCoder 2024-10-11 15:15:08 -04:00
parent b80d7221c1
commit 337a626335
27 changed files with 794 additions and 758 deletions

View File

@ -0,0 +1 @@
package hirs.utils.digest;

View File

@ -0,0 +1 @@
package hirs.utils.enums;

View File

@ -0,0 +1 @@
package hirs.utils.exception;

View File

@ -0,0 +1 @@
package hirs.utils;

View File

@ -0,0 +1 @@
package hirs.utils.rim;

View File

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

View File

@ -0,0 +1 @@
package hirs.utils.swid;

View File

@ -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.
*
* <p>
* HEADERS defined by PFP v1.06 Rev 52.
* Certain fields are common to both ..HEADER and ..HEADER2, and are noted below the structures.
* <p>
* 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;
* <p>
* 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;
* <p>
* Fields common to both ..HEADER and ..HEADER2:
* Signature
* Version
* DeviceType
* DevicePathLength
* DevicePath
* Signature
* Version
* DeviceType
* DevicePathLength
* DevicePath
* <p>
*/
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;
}
/**

View File

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

View File

@ -8,34 +8,34 @@ import lombok.Getter;
* <p>
* Measurement, defined by SPDM v1.03, Sect 10.11.1, Table 54:
* DMTF measurement spec format {
* DMTFSpecMeasurementValueType 1 byte;
* DMTFSpecMeasurementValueSize 2 bytes;
* DMTFSpecMeasurementValue <DMTFSpecMeasurementValueSize> bytes;
* DMTFSpecMeasurementValueType 1 byte;
* DMTFSpecMeasurementValueSize 2 bytes;
* DMTFSpecMeasurementValue <DMTFSpecMeasurementValueSize> bytes;
* }
* <p>
* 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.
* <p>
*/
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;
}

View File

@ -2,4 +2,4 @@
* Non-persistent classes related to TGC Event Logs.
*/
package hirs.utils.tpm.eventlog.spdm;
package hirs.utils.tpm.eventlog.spdm;

View File

@ -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() {
}
}

View File

@ -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 <code>Digest</code> class.
*/
public class DigestTest {
private static final int DIGEST_LENGTH_BYTES = 20;
/**
* Tests that constructor throws a <code>IllegalArgumentException</code> when a
* null <code>DigestAlgorithm</code> 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 <code>IllegalArgumentException</code> 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 <code>IllegalArgumentException</code> 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 <code>DigestAlgorithm</code> 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 <code>Digest</code> 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>IllegalArgumentException</code> when a
* null <code>DigestAlgorithm</code> 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 <code>IllegalArgumentException</code> 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 <code>IllegalArgumentException</code> 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 <code>DigestAlgorithm</code> 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 <code>Digest</code> 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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 <code>Digest</code>s 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[] {});
}
}

View File

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

View File

@ -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
);
/**

View File

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

View File

@ -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));
}
/**

View File

@ -0,0 +1 @@
package hirs.data.persist;

View File

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

View File

@ -0,0 +1 @@
package hirs.tpm.eventlog.uefi;

View File

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

View File

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

View File

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

View File

@ -0,0 +1 @@
package hirs.utils;

View File

@ -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 <code>SessionFactory</code> 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");
}
}

View File

@ -0,0 +1 @@
package hirs.utils.tpm.eventlog;

View File

@ -171,7 +171,12 @@
</module>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
<module name="MagicNumber">
<property name="ignoreAnnotation" value="true"/>
<property name="ignoreFieldDeclaration" value="true"/>
<property name="constantWaiverParentToken" value="ASSIGN,ARRAY_INIT,EXPR,
UNARY_PLUS, UNARY_MINUS, TYPECAST, ELIST, LITERAL_NEW"/>
</module>
<module name="MissingSwitchDefault"/>
<module name="MultipleVariableDeclarations"/>
<module name="SimplifyBooleanExpression"/>