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 FX_SEPARATOR = ":";
public static final String RFC3852_PFX = "rcf3852"; public static final String RFC3852_PFX = "rcf3852";
public static final String RFC3339_PFX = "rcf3339"; public static final String RFC3339_PFX = "rcf3339";
public static final String _COLLOQUIAL_VERSION_STR = N8060_PFX + FX_SEPARATOR + public static final String _COLLOQUIAL_VERSION_STR = N8060_PFX + FX_SEPARATOR
COLLOQUIAL_VERSION; + COLLOQUIAL_VERSION;
public static final String _PRODUCT_STR = N8060_PFX + FX_SEPARATOR + public static final String _PRODUCT_STR = N8060_PFX + FX_SEPARATOR
PRODUCT; + PRODUCT;
public static final String _REVISION_STR = N8060_PFX + FX_SEPARATOR + public static final String _REVISION_STR = N8060_PFX + FX_SEPARATOR
REVISION; + REVISION;
public static final String _EDITION_STR = N8060_PFX + FX_SEPARATOR + public static final String _EDITION_STR = N8060_PFX + FX_SEPARATOR
EDITION; + EDITION;
public static final String _RIM_LINK_HASH_STR = RIM_PFX + FX_SEPARATOR + public static final String _RIM_LINK_HASH_STR = RIM_PFX + FX_SEPARATOR
RIM_LINK_HASH; + RIM_LINK_HASH;
public static final String _BINDING_SPEC_STR = RIM_PFX + FX_SEPARATOR + public static final String _BINDING_SPEC_STR = RIM_PFX + FX_SEPARATOR
BINDING_SPEC; + BINDING_SPEC;
public static final String _BINDING_SPEC_VERSION_STR = RIM_PFX + FX_SEPARATOR + public static final String _BINDING_SPEC_VERSION_STR = RIM_PFX + FX_SEPARATOR
BINDING_SPEC_VERSION; + BINDING_SPEC_VERSION;
public static final String _PLATFORM_MANUFACTURER_STR = RIM_PFX + FX_SEPARATOR + public static final String _PLATFORM_MANUFACTURER_STR = RIM_PFX + FX_SEPARATOR
PLATFORM_MANUFACTURER_STR; + PLATFORM_MANUFACTURER_STR;
public static final String _PLATFORM_MANUFACTURER_ID_STR = RIM_PFX + FX_SEPARATOR + public static final String _PLATFORM_MANUFACTURER_ID_STR = RIM_PFX + FX_SEPARATOR
PLATFORM_MANUFACTURER_ID; + PLATFORM_MANUFACTURER_ID;
public static final String _PLATFORM_MODEL_STR = RIM_PFX + FX_SEPARATOR + public static final String _PLATFORM_MODEL_STR = RIM_PFX + FX_SEPARATOR
PLATFORM_MODEL; + PLATFORM_MODEL;
public static final String _PLATFORM_VERSION_STR = RIM_PFX + FX_SEPARATOR + public static final String _PLATFORM_VERSION_STR = RIM_PFX + FX_SEPARATOR
PLATFORM_VERSION; + PLATFORM_VERSION;
public static final String _PAYLOAD_TYPE_STR = RIM_PFX + FX_SEPARATOR + public static final String _PAYLOAD_TYPE_STR = RIM_PFX + FX_SEPARATOR
PAYLOAD_TYPE; + PAYLOAD_TYPE;
public static final String _PC_URI_LOCAL_STR = RIM_PFX + FX_SEPARATOR + public static final String _PC_URI_LOCAL_STR = RIM_PFX + FX_SEPARATOR
PC_URI_LOCAL; + PC_URI_LOCAL;
public static final String _PC_URI_GLOBAL_STR = RIM_PFX + FX_SEPARATOR + public static final String _PC_URI_GLOBAL_STR = RIM_PFX + FX_SEPARATOR
PC_URI_GLOBAL; + PC_URI_GLOBAL;
public static final QName _SHA256_HASH = new QName( public static final QName _SHA256_HASH = new QName(
"http://www.w3.org/2001/04/xmlenc#sha256", HASH, "SHA256"); "http://www.w3.org/2001/04/xmlenc#sha256", HASH, "SHA256");
public static final QName _COLLOQUIAL_VERSION = new QName( public static final QName _COLLOQUIAL_VERSION = new QName(

View File

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

View File

@ -12,7 +12,7 @@ import java.nio.charset.StandardCharsets;
* The first 16 bytes of the event data header MUST be a String based identifier (Signature), * 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", * 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. * which implies the data is a DEVICE_SECURITY_EVENT_DATA or ..DATA2.
* * <p>
* HEADERS defined by PFP v1.06 Rev 52. * HEADERS defined by PFP v1.06 Rev 52.
* Certain fields are common to both ..HEADER and ..HEADER2, and are noted below the structures. * Certain fields are common to both ..HEADER and ..HEADER2, and are noted below the structures.
* <p> * <p>
@ -51,12 +51,16 @@ import java.nio.charset.StandardCharsets;
*/ */
public abstract class DeviceSecurityEventHeader { public abstract class DeviceSecurityEventHeader {
/**
* UEFI Device Path Length.
*/
@Getter
private final int devicePathLength = 0;
/** /**
* Contains the size (in bytes) of the header. * Contains the size (in bytes) of the header.
*/ */
@Getter @Getter
private Integer dsedHeaderLength = 0; private Integer dsedHeaderLength = 0;
/** /**
* Signature (text) data. * Signature (text) data.
*/ */
@ -73,11 +77,6 @@ public abstract class DeviceSecurityEventHeader {
*/ */
@Getter @Getter
private int deviceType = -1; private int deviceType = -1;
/**
* UEFI Device Path Length.
*/
@Getter
private int devicePathLength = 0;
/** /**
* UEFI Device path. * UEFI Device path.
*/ */
@ -131,19 +130,19 @@ public abstract class DeviceSecurityEventHeader {
// get the device path length // get the device path length
byte[] devicePathLengthBytes = new byte[8]; byte[] devicePathLengthBytes = new byte[8];
System.arraycopy(dsedBytes, startByteUpdated, devicePathLengthBytes, 0, 8); System.arraycopy(dsedBytes, startByteUpdated, devicePathLengthBytes, 0, 8);
int devicePathLength = HexUtils.leReverseInt(devicePathLengthBytes); int retrievedDevicePathLength = HexUtils.leReverseInt(devicePathLengthBytes);
// get the device path // get the device path
if (devicePathLength > 0) { if (retrievedDevicePathLength > 0) {
startByteUpdated = startByteUpdated + 8; startByteUpdated = startByteUpdated + 8;
byte[] devPathBytes = new byte[devicePathLength]; byte[] devPathBytes = new byte[retrievedDevicePathLength];
System.arraycopy(dsedBytes, startByteUpdated, devPathBytes, System.arraycopy(dsedBytes, startByteUpdated, devPathBytes,
0, devicePathLength); 0, retrievedDevicePathLength);
devicePath = new UefiDevicePath(devPathBytes); devicePath = new UefiDevicePath(devPathBytes);
} }
// header total size // header total size
dsedHeaderLength = startByteUpdated + devicePathLength; dsedHeaderLength = startByteUpdated + retrievedDevicePathLength;
} }
/** /**

View File

@ -6,11 +6,6 @@ package hirs.utils.tpm.eventlog.spdm;
*/ */
public class SpdmHa { public class SpdmHa {
/**
* Default private constructor so checkstyles doesn't complain
*/
private SpdmHa() { }
/** /**
* Spdm Hash Alg = Raw bit stream. * Spdm Hash Alg = Raw bit stream.
*/ */
@ -40,6 +35,12 @@ public class SpdmHa {
*/ */
public static final int TPM_ALG_SHA3_512 = 64; 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. * Returns the hash name via a lookup.
* Lookup based upon SPDM Spec v1.03 section 10.4. * Lookup based upon SPDM Spec v1.03 section 10.4.
@ -93,21 +94,15 @@ public class SpdmHa {
case TPM_ALG_SHA_256: case TPM_ALG_SHA_256:
byteSize = 32; byteSize = 32;
break; break;
case TPM_ALG_SHA_384: case TPM_ALG_SHA_384, TPM_ALG_SHA3_384:
byteSize = 48; byteSize = 48;
break; break;
case TPM_ALG_SHA_512: case TPM_ALG_SHA_512, TPM_ALG_SHA3_512:
byteSize = 64; byteSize = 64;
break; break;
case TPM_ALG_SHA3_256: case TPM_ALG_SHA3_256:
byteSize = 32; byteSize = 32;
break; break;
case TPM_ALG_SHA3_384:
byteSize = 48;
break;
case TPM_ALG_SHA3_512:
byteSize = 64;
break;
default: default:
byteSize = -1; byteSize = -1;
} }

View File

@ -27,15 +27,15 @@ import lombok.Getter;
*/ */
public class SpdmMeasurement { public class SpdmMeasurement {
/**
* Measurement value (digest).
*/
private final byte[] dmtfSpecMeasurementValue;
/** /**
* Measurement value type (such as mutable firmware, etc). * Measurement value type (such as mutable firmware, etc).
*/ */
@Getter @Getter
private int dmtfSpecMeasurementValueType = 0; private int dmtfSpecMeasurementValueType = 0;
/**
* Measurement value (digest).
*/
private byte[] dmtfSpecMeasurementValue;
/** /**
* SpdmMeasurement Constructor. * SpdmMeasurement Constructor.
@ -49,14 +49,16 @@ public class SpdmMeasurement {
1); 1);
dmtfSpecMeasurementValueType = HexUtils.leReverseInt(dmtfSpecMeasurementValueTypeBytes); 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]; byte[] dmtfSpecMeasurementValueSizeBytes = new byte[2];
System.arraycopy(spdmMeasBytes, 1, dmtfSpecMeasurementValueSizeBytes, 0, System.arraycopy(spdmMeasBytes, 1, dmtfSpecMeasurementValueSizeBytes, 0,
2); 2);
int dmtfSpecMeasurementValueSize = HexUtils.leReverseInt(dmtfSpecMeasurementValueSizeBytes); int dmtfSpecMeasurementValueSize = HexUtils.leReverseInt(dmtfSpecMeasurementValueSizeBytes);
dmtfSpecMeasurementValue = new byte[dmtfSpecMeasurementValueSize]; dmtfSpecMeasurementValue = new byte[dmtfSpecMeasurementValueSize];
System.arraycopy(spdmMeasBytes, 3, dmtfSpecMeasurementValue, 0,
final int sourceIndex = 3;
System.arraycopy(spdmMeasBytes, sourceIndex, dmtfSpecMeasurementValue, 0,
dmtfSpecMeasurementValueSize); dmtfSpecMeasurementValueSize);
} }
@ -64,49 +66,24 @@ public class SpdmMeasurement {
* Lookup for SPDM measurement value type. * Lookup for SPDM measurement value type.
* *
* @param measValType the numerical representation of the measurement value type. * @param measValType the numerical representation of the measurement value type.
*
* @return a description of the measurement value type. * @return a description of the measurement value type.
*/ */
public String dmtfSpecMeasurementValueTypeToString(final int measValType) { public String dmtfSpecMeasurementValueTypeToString(final int measValType) {
String measValTypeStr; String measValTypeStr = switch (measValType) {
switch (measValType) { case 0 -> "Immutable ROM";
case 0: case 1 -> "Mutable firmware";
measValTypeStr = "Immutable ROM"; case 2 -> "Hardware configuration";
break; case 3 -> "Firmware configuration";
case 1: case 4 -> "Freeform measurement manifest";
measValTypeStr = "Mutable firmware"; case 5 -> "Structured representation of debug and device mode";
break; case 6 -> "Mutable firmware's version number";
case 2: case 7 -> "Mutable firmware's security version number";
measValTypeStr = "Hardware configuration"; case 8 -> "Hash-extended measurement";
break; case 9 -> "Informational";
case 3: case 10 -> "Structured measurement manifest";
measValTypeStr = "Firmware configuration"; default -> "Unknown or invalid DMTF Spec Measurement Value Type";
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";
}
return measValTypeStr; return measValTypeStr;
} }

View File

@ -7,11 +7,6 @@ package hirs.utils.tpm.eventlog.uefi;
*/ */
public final class UefiConstants { public final class UefiConstants {
/**
* Default private constructor so checkstyles doesn't complain
*/
private UefiConstants() { }
/** /**
* 2 byte size. * 2 byte size.
*/ */
@ -287,4 +282,9 @@ public final class UefiConstants {
* file status, where file is not accessible (either not found, or no access permission). * file status, where file is not accessible (either not found, or no access permission).
*/ */
public static final String FILESTATUS_NOT_ACCESSIBLE = "fileNotAccessible"; 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; package hirs.data.persist;
import java.util.Arrays;
import hirs.utils.digest.Digest; import hirs.utils.digest.Digest;
import hirs.utils.digest.DigestAlgorithm; import hirs.utils.digest.DigestAlgorithm;
import hirs.utils.digest.DigestComparisonResultType; import hirs.utils.digest.DigestComparisonResultType;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays;
/** /**
* Unit tests for the <code>Digest</code> class. * Unit tests for the <code>Digest</code> class.
*/ */
public class DigestTest { public class DigestTest {
private static final int DIGEST_LENGTH_BYTES = 20; 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. * Get a test SHA1 digest.
* *
@ -464,7 +48,455 @@ public class DigestTest {
return new byte[count]; 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() { 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 hirs.attestationca.persist.entity.userdefined.info.FirmwareInfo;
import org.apache.commons.lang3.StringUtils; 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.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED;
/** /**
* FirmwareInfoTest is a unit test class for FirmwareInfo. * 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_VENDOR = "test bios vendor";
private static final String BIOS_VERSION = "test bios version"; private static final String BIOS_VERSION = "test bios version";
private static final String BIOS_RELEASE_DATE = "test bios release date"; 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( private static final String LONG_BIOS_VENDOR = StringUtils.rightPad(
"test bios vendor", BIOS_VENDOR,
257 PRIMARY_SIZE
); );
private static final String LONG_BIOS_VERSION = StringUtils.rightPad( private static final String LONG_BIOS_VERSION = StringUtils.rightPad(
"test bios version", BIOS_VERSION,
257 PRIMARY_SIZE
); );
private static final String LONG_BIOS_RELEASE_DATE = StringUtils.rightPad( private static final String LONG_BIOS_RELEASE_DATE = StringUtils.rightPad(
"test bios release date", BIOS_RELEASE_DATE,
33 SECONDARY_SIZE
); );
/** /**
@ -44,8 +46,8 @@ public class FirmwareInfoTest {
public final void firmwareInfoNoParams() { public final void firmwareInfoNoParams() {
FirmwareInfo firmwareInfo = new FirmwareInfo(); FirmwareInfo firmwareInfo = new FirmwareInfo();
Assertions.assertEquals(NOT_SPECIFIED, firmwareInfo.getBiosVendor()); Assertions.assertEquals(NOT_SPECIFIED, firmwareInfo.getBiosVendor());
Assertions.assertEquals(NOT_SPECIFIED,firmwareInfo.getBiosVersion()); Assertions.assertEquals(NOT_SPECIFIED, firmwareInfo.getBiosVersion());
Assertions.assertEquals(NOT_SPECIFIED,firmwareInfo.getBiosReleaseDate()); Assertions.assertEquals(NOT_SPECIFIED, firmwareInfo.getBiosReleaseDate());
} }
/** /**

View File

@ -1,12 +1,12 @@
package hirs.data.persist; package hirs.data.persist;
import hirs.attestationca.persist.entity.userdefined.info.HardwareInfo; import hirs.attestationca.persist.entity.userdefined.info.HardwareInfo;
import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED;
/** /**
* HardwareInfoTest is a unit test class for HardwareInfo. * 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 SERIAL_NUMBER = "test serial number";
private static final String CHASSIS_SERIAL_NUMBER = "test chassis serial number"; private static final String CHASSIS_SERIAL_NUMBER = "test chassis serial number";
private static final String BASEBOARD_SERIAL_NUMBER = "test baseboard serial number"; private static final String 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( private static final String LONG_MANUFACTURER = StringUtils.rightPad(
"test manufacturer", MANUFACTURER,
257 PRIMARY_SIZE
); );
private static final String LONG_PRODUCT_NAME = StringUtils.rightPad( private static final String LONG_PRODUCT_NAME = StringUtils.rightPad(
"test product name", PRODUCT_NAME,
257 PRIMARY_SIZE
); );
private static final String LONG_VERSION = StringUtils.rightPad( private static final String LONG_VERSION = StringUtils.rightPad(
"test version", VERSION,
65 SECONDARY_SIZE
); );
private static final String LONG_SERIAL_NUMBER = StringUtils.rightPad( private static final String LONG_SERIAL_NUMBER = StringUtils.rightPad(
"test serial number", SERIAL_NUMBER,
257 PRIMARY_SIZE
); );
private static final String LONG_CHASSIS_SERIAL_NUMBER = StringUtils.rightPad( private static final String LONG_CHASSIS_SERIAL_NUMBER = StringUtils.rightPad(
"test chassis serial number", CHASSIS_SERIAL_NUMBER,
257 PRIMARY_SIZE
); );
private static final String LONG_BASEBOARD_SERIAL_NUMBER = StringUtils.rightPad( private static final String LONG_BASEBOARD_SERIAL_NUMBER = StringUtils.rightPad(
"test baseboard serial number", BASEBOARD_SERIAL_NUMBER,
257 PRIMARY_SIZE
); );
/** /**

View File

@ -1,12 +1,12 @@
package hirs.data.persist; package hirs.data.persist;
import hirs.attestationca.persist.entity.userdefined.info.NetworkInfo; 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.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
/** /**
* NetworkInfoTest is a unit test class for NetworkInfo. * 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, private static final byte[] MAC_ADDRESS = new byte[] {11, 22, 33, 44, 55,
66}; 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. * Tests instantiation of a NetworkInfo object.
*/ */
@ -103,14 +112,14 @@ public class NetworkInfoTest {
* Tests that hashcodes generated by NetworkInfo objects with different IP * Tests that hashcodes generated by NetworkInfo objects with different IP
* addresses are not equal. * addresses are not equal.
* *
* @throws UnknownHostException * @throws UnknownHostException in case the InetAddress is not created correctly
* in case the InetAddress is not created correctly
*/ */
@Test @Test
public final void testHashCodeNotEqualsIpAddress() public final void testHashCodeNotEqualsIpAddress()
throws UnknownHostException { throws UnknownHostException {
final byte[] byteAddress = new byte[] {127, 0, 0, 2};
final InetAddress ipAddress2 = final InetAddress ipAddress2 =
InetAddress.getByAddress(new byte[] {127, 0, 0, 2}); InetAddress.getByAddress(byteAddress);
NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS);
NetworkInfo ni2 = new NetworkInfo(HOSTNAME, ipAddress2, MAC_ADDRESS); NetworkInfo ni2 = new NetworkInfo(HOSTNAME, ipAddress2, MAC_ADDRESS);
Assertions.assertNotEquals(ni2.hashCode(), ni1.hashCode()); 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 * Tests that two NetworkInfo objects are not equal if they have different
* IP addresses. * IP addresses.
* *
* @throws UnknownHostException * @throws UnknownHostException in case InetAddress is not created correctly
* in case InetAddress is not created correctly
*/ */
@Test @Test
public final void testNotEqualsIpAddress() throws UnknownHostException { public final void testNotEqualsIpAddress() throws UnknownHostException {
final byte[] byteAddress = new byte[] {127, 0, 0, 2};
final InetAddress ipAddress2 = final InetAddress ipAddress2 =
InetAddress.getByAddress(new byte[] {127, 0, 0, 2}); InetAddress.getByAddress(byteAddress);
NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS); NetworkInfo ni1 = new NetworkInfo(HOSTNAME, IP_ADDRESS, MAC_ADDRESS);
NetworkInfo ni2 = new NetworkInfo(HOSTNAME, ipAddress2, MAC_ADDRESS); NetworkInfo ni2 = new NetworkInfo(HOSTNAME, ipAddress2, MAC_ADDRESS);
Assertions.assertNotEquals(ni2, ni1); Assertions.assertNotEquals(ni2, ni1);
@ -178,12 +187,4 @@ public class NetworkInfoTest {
NetworkInfo ni2 = new NetworkInfo(HOSTNAME, IP_ADDRESS, macAddress2); NetworkInfo ni2 = new NetworkInfo(HOSTNAME, IP_ADDRESS, macAddress2);
Assertions.assertNotEquals(ni2, ni1); 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; package hirs.data.persist;
import hirs.attestationca.persist.entity.userdefined.info.OSInfo; import hirs.attestationca.persist.entity.userdefined.info.OSInfo;
import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED;
/** /**
* OSInfoTest is a unit test class for OSInfo. * 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 OS_ARCH = "test osArch";
private static final String DISTRIBUTION = "test distribution"; private static final String DISTRIBUTION = "test distribution";
private static final String DISTRIBUTION_RELEASE = "test distribution release"; 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_NAME = StringUtils.rightPad(OS_NAME, PRIMARY_SIZE);
private static final String LONG_OS_VERSION = StringUtils.rightPad("test osVersion", 257); private static final String LONG_OS_VERSION = StringUtils.rightPad(OS_VERSION, PRIMARY_SIZE);
private static final String LONG_OS_ARCH = StringUtils.rightPad("test osArch", 33); private static final String LONG_OS_ARCH = StringUtils.rightPad(OS_ARCH, SECONDARY_SIZE);
/** /**
* Tests instantiation of an OSInfo object. * Tests instantiation of an OSInfo object.

View File

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

View File

@ -1,25 +1,29 @@
package hirs.tpm.eventlog.uefi; 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 com.eclipsesource.json.JsonObject;
import hirs.utils.HexUtils;
import hirs.utils.JsonUtils; 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.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import hirs.utils.HexUtils;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; 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. * Class for testing TCG Event Log processing of UEFI defined Data.
*/ */
@ -70,7 +74,7 @@ public class UefiProcessingTest {
Path jsonPath = Paths.get(this.getClass() Path jsonPath = Paths.get(this.getClass()
.getResource(JSON_FILE).toURI()); .getResource(JSON_FILE).toURI());
String uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream(UEFI_VARIABLE_BOOT), String uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream(UEFI_VARIABLE_BOOT),
"UTF-8"); StandardCharsets.UTF_8);
byte[] uefiVariableBytes = HexUtils.hexStringToByteArray(uefiTxt); byte[] uefiVariableBytes = HexUtils.hexStringToByteArray(uefiTxt);
UefiVariable uefiVariable = new UefiVariable(uefiVariableBytes); UefiVariable uefiVariable = new UefiVariable(uefiVariableBytes);
UefiGuid guid = uefiVariable.getUefiVarGuid(); UefiGuid guid = uefiVariable.getUefiVarGuid();
@ -83,7 +87,7 @@ public class UefiProcessingTest {
uefiTxt = IOUtils.toString(this.getClass() uefiTxt = IOUtils.toString(this.getClass()
.getResourceAsStream(UEFI_VARIABLE_BOOT_SECURE_BOOT), .getResourceAsStream(UEFI_VARIABLE_BOOT_SECURE_BOOT),
"UTF-8"); StandardCharsets.UTF_8);
uefiVariableBytes = HexUtils.hexStringToByteArray(uefiTxt); uefiVariableBytes = HexUtils.hexStringToByteArray(uefiTxt);
uefiVariable = new UefiVariable(uefiVariableBytes); uefiVariable = new UefiVariable(uefiVariableBytes);
guid = uefiVariable.getUefiVarGuid(); guid = uefiVariable.getUefiVarGuid();
@ -94,7 +98,7 @@ public class UefiProcessingTest {
Assertions.assertEquals("SecureBoot", varName); Assertions.assertEquals("SecureBoot", varName);
uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream( 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); uefiVariableBytes = HexUtils.hexStringToByteArray(uefiTxt);
uefiVariable = new UefiVariable(uefiVariableBytes); uefiVariable = new UefiVariable(uefiVariableBytes);
varName = uefiVariable.getEfiVarName(); varName = uefiVariable.getEfiVarName();
@ -116,7 +120,7 @@ public class UefiProcessingTest {
Path jsonPath = Paths.get(this.getClass() Path jsonPath = Paths.get(this.getClass()
.getResource(JSON_FILE).toURI()); .getResource(JSON_FILE).toURI());
String uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream(UEFI_GPT_EVENT), String uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream(UEFI_GPT_EVENT),
"UTF-8"); StandardCharsets.UTF_8);
byte[] uefiPartitionBytes = HexUtils.hexStringToByteArray(uefiTxt); byte[] uefiPartitionBytes = HexUtils.hexStringToByteArray(uefiTxt);
UefiPartition gptPart = new UefiPartition(uefiPartitionBytes); UefiPartition gptPart = new UefiPartition(uefiPartitionBytes);
String gptPartName = gptPart.getPartitionName(); String gptPartName = gptPart.getPartitionName();
@ -143,13 +147,17 @@ public class UefiProcessingTest {
CertificateException, NoSuchAlgorithmException { CertificateException, NoSuchAlgorithmException {
LOGGER.debug("Testing the parsing of Uefi Firmware Blob"); LOGGER.debug("Testing the parsing of Uefi Firmware Blob");
String uefiTxt = IOUtils.toString(this.getClass() String uefiTxt = IOUtils.toString(this.getClass()
.getResourceAsStream(UEFI_FW_BLOB), "UTF-8"); .getResourceAsStream(UEFI_FW_BLOB), StandardCharsets.UTF_8);
byte[] uefiFwBlobBytes = HexUtils.hexStringToByteArray(uefiTxt); byte[] uefiFwBlobBytes = HexUtils.hexStringToByteArray(uefiTxt);
UefiFirmware uefiFWBlob = new UefiFirmware(uefiFwBlobBytes); UefiFirmware uefiFWBlob = new UefiFirmware(uefiFwBlobBytes);
int fwAddress = uefiFWBlob.getPhysicalBlobAddress(); int fwAddress = uefiFWBlob.getPhysicalBlobAddress();
int fwLength = uefiFWBlob.getBlobLength(); 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);
} }
/** /**
@ -162,7 +170,7 @@ public class UefiProcessingTest {
public final void testUefiDevicePath() throws IOException, URISyntaxException { public final void testUefiDevicePath() throws IOException, URISyntaxException {
LOGGER.debug("Testing the parsing of Uefi Device Path"); LOGGER.debug("Testing the parsing of Uefi Device Path");
String uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream(UEFI_DEVICE_PATH), String uefiTxt = IOUtils.toString(this.getClass().getResourceAsStream(UEFI_DEVICE_PATH),
"UTF-8"); StandardCharsets.UTF_8);
byte[] uefiFwBlobBytes = HexUtils.hexStringToByteArray(uefiTxt); byte[] uefiFwBlobBytes = HexUtils.hexStringToByteArray(uefiTxt);
UefiDevicePath uefiDevPath = new UefiDevicePath(uefiFwBlobBytes); UefiDevicePath uefiDevPath = new UefiDevicePath(uefiFwBlobBytes);
String devPathType = uefiDevPath.getType(); String devPathType = uefiDevPath.getType();

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.apache.logging.log4j.util.Strings;
import org.junit.jupiter.api.Test; 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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Tests methods in the (@link BouncyCastleUtils) utility class. * Tests methods in the (@link BouncyCastleUtils) utility class.

View File

@ -1,6 +1,7 @@
package hirs.utils; package hirs.utils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -14,11 +15,10 @@ public class HexUtilsTest {
*/ */
@Test @Test
public void testHexStringToByteArray() { public void testHexStringToByteArray() {
String s = "abcd1234"; final String testString = "abcd1234";
byte[] target = {-85, -51, 18, 52}; final byte[] expectedBytes = {-85, -51, 18, 52};
final byte[] actualBytes = HexUtils.hexStringToByteArray(testString);
byte[] b = HexUtils.hexStringToByteArray(s); assertArrayEquals(expectedBytes, actualBytes);
assertArrayEquals(b, target);
} }
/** /**
@ -26,11 +26,10 @@ public class HexUtilsTest {
*/ */
@Test @Test
public void testByteArrayToHexString() { public void testByteArrayToHexString() {
String target = "abcd1234"; final byte[] byteArray = {-85, -51, 18, 52};
byte[] b = {-85, -51, 18, 52}; final String expectedString = "abcd1234";
final String actualString = HexUtils.byteArrayToHexString(byteArray);
String s = HexUtils.byteArrayToHexString(b); assertEquals(expectedString, actualString);
assertEquals(s, target);
} }
/** /**
@ -38,11 +37,10 @@ public class HexUtilsTest {
*/ */
@Test @Test
public void testByteArrayToHexStringConditional() { public void testByteArrayToHexStringConditional() {
String target = "abcd0100"; final byte[] byteArray = {-85, -51, 1, 0};
byte[] b = {-85, -51, 1, 0}; final String expectedHexString = "abcd0100";
final String actualHexString = HexUtils.byteArrayToHexString(byteArray);
String s = HexUtils.byteArrayToHexString(b); assertEquals(expectedHexString, actualHexString);
assertEquals(s, target);
} }
/** /**
@ -50,9 +48,10 @@ public class HexUtilsTest {
*/ */
@Test @Test
public void testHexToInt() { public void testHexToInt() {
String s = "ff"; final String testString = "ff";
Integer i = HexUtils.hexToInt(s); final int expectedInt = HexUtils.FF_BYTE;
assertEquals((int) i, HexUtils.FF_BYTE); final Integer actualInt = HexUtils.hexToInt(testString);
assertEquals(expectedInt, (int) actualInt);
} }
/** /**
@ -60,14 +59,11 @@ public class HexUtilsTest {
*/ */
@Test @Test
public void testSubarray() { public void testSubarray() {
byte[] b = {-85, -51, 18, 52}; final byte[] b = {-85, -51, 18, 52};
byte[] target = {-51, 18}; final byte[] expectedSubArray = {-51, 18};
final byte[] actualSubArray = HexUtils.subarray(b, 1, 2);
byte[] sub = HexUtils.subarray(b, 1, 2); assertArrayEquals(expectedSubArray, actualSubArray);
assertArrayEquals(sub, target);
} }
} }

View File

@ -1,11 +1,12 @@
package hirs.utils; package hirs.utils;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.mockito.Mockito;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows; import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals; 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; import static org.mockito.Mockito.times;
/** /**

View File

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

View File

@ -1,20 +1,20 @@
package hirs.utils.tpm.eventlog; 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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.util.Arrays; import java.util.Arrays;
import org.apache.commons.io.IOUtils; import static hirs.utils.tpm.eventlog.TCGEventLog.PCR_COUNT;
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 org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -36,7 +36,6 @@ public class TCGEventLogTest {
@BeforeAll @BeforeAll
public static final void setup() { public static final void setup() {
LOGGER.debug("retrieving session factory"); LOGGER.debug("retrieving session factory");
} }
/** /**
@ -49,6 +48,7 @@ public class TCGEventLogTest {
/** /**
* Tests the processing of a crypto agile event log. * Tests the processing of a crypto agile 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 NoSuchAlgorithmException if an unknown algorithm is encountered.
* @throws CertificateException if a certificate fails to parse. * @throws CertificateException if a certificate fails to parse.
@ -57,28 +57,31 @@ public class TCGEventLogTest {
public final void testCryptoAgileTCGEventLog() throws IOException, CertificateException, public final void testCryptoAgileTCGEventLog() throws IOException, CertificateException,
NoSuchAlgorithmException { NoSuchAlgorithmException {
LOGGER.debug("Testing the parsing of a Crypto Agile formatted TCG Event Log"); LOGGER.debug("Testing the parsing of a Crypto Agile formatted TCG Event Log");
InputStream log, pcrs;
// 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);
boolean testPass = true; 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 // Test 1 get all PCRs
for (int i = 0; i < 24; i++) { for (int i = 0; i < PCR_COUNT; i++) {
if (pcrFromLog[i].compareToIgnoreCase(pcrTxt[i]) != 0) { if (pcrFromLog[i].compareToIgnoreCase(pcrTxt[i]) != 0) {
testPass = false; testPass = false;
LOGGER.error("\ntestTCGEventLogProcessorParser error with PCR " + i); LOGGER.error("\ntestTCGEventLogProcessorParser error with PCR {}", i);
} }
} }
assertTrue(testPass); assertTrue(testPass);
// Test 2 get an individual PCR // Test 2 get an individual PCR
String pcr3 = evlog.getExpectedPCRValue(3); final int pcrIndex = 3;
assertThat(pcrFromLog[3], equalTo(pcr3)); String pcr3 = evlog.getExpectedPCRValue(pcrIndex);
assertThat(pcrFromLog[pcrIndex], equalTo(pcr3));
// Test 3 check the Algorithm String Identifier used in the log // Test 3 check the Algorithm String Identifier used in the log
String algStr = evlog.getEventLogHashAlgorithm(); String algStr = evlog.getEventLogHashAlgorithm();
@ -93,6 +96,7 @@ public class TCGEventLogTest {
/** /**
* Tests the processing of a SHA1 formatted Event log. * 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 NoSuchAlgorithmException if an unknown algorithm is encountered.
* @throws CertificateException if a certificate fails to parse. * @throws CertificateException if a certificate fails to parse.
@ -101,21 +105,23 @@ public class TCGEventLogTest {
public final void testSHA1TCGEventLog() throws IOException, CertificateException, public final void testSHA1TCGEventLog() throws IOException, CertificateException,
NoSuchAlgorithmException { NoSuchAlgorithmException {
LOGGER.debug("Testing the parsing of a SHA1 formated TCG Event Log"); LOGGER.debug("Testing the parsing of a SHA1 formated TCG Event Log");
InputStream log, pcrs;
// 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; 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 // Test 1 get all PCRs
for (int i = 0; i < 24; i++) { for (int i = 0; i < PCR_COUNT; i++) {
if (pcrFromLog[i].compareToIgnoreCase(pcrTxt[i]) != 0) { if (pcrFromLog[i].compareToIgnoreCase(pcrTxt[i]) != 0) {
testPass = false; testPass = false;
LOGGER.error("\ntestTCGEventLogProcessorParser error with PCR " + i); LOGGER.error("\ntestTCGEventLogProcessorParser error with PCR {}", i);
} }
} }
assertTrue(testPass); assertTrue(testPass);
@ -134,5 +140,4 @@ public class TCGEventLogTest {
LOGGER.debug("OK. Parsing of a SHA1 formatted TCG Event Log Success"); 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>
<module name="IllegalInstantiation"/> <module name="IllegalInstantiation"/>
<module name="InnerAssignment"/> <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="MissingSwitchDefault"/>
<module name="MultipleVariableDeclarations"/> <module name="MultipleVariableDeclarations"/>
<module name="SimplifyBooleanExpression"/> <module name="SimplifyBooleanExpression"/>