issue_873: fixed all checkstyle issues

This commit is contained in:
TheSilentCoder 2024-11-08 15:48:21 -05:00
parent 0f2a5864ba
commit 52f5f1bb70
28 changed files with 104 additions and 89 deletions

View File

@ -39,10 +39,10 @@ configurations.checkstyle {
}
}
checkstyleMain {
source ='src/main/java'
source = 'src/main/java'
}
checkstyleTest {
source ='src/test/java'
source = 'src/test/java'
}
tasks.withType(Checkstyle) {
reports {

View File

@ -2,11 +2,11 @@
<!-- Docs at http://findbugs.sourceforge.net/manual/filter.html -->
<FindBugsFilter>
<Match>
<Package name="~hirs\.structs.*" />
<Package name="~hirs\.structs.*"/>
</Match>
<Match>
<!-- https://github.com/spotbugs/spotbugs/pull/2748 -->
<Bug pattern="CT_CONSTRUCTOR_THROW" />
<Bug pattern="CT_CONSTRUCTOR_THROW"/>
</Match>
<!-- <Match>-->

View File

@ -2,22 +2,24 @@ package hirs.structs.converters;
import hirs.structs.elements.Struct;
import hirs.structs.elements.StructElementLength;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
/**
* StructBuilder implementation.
*
* @param <T> the type of Struct to build
*/
public class SimpleStructBuilder<T extends Struct> implements StructBuilder {
private T struct;
private final Class<T> clazz;
private T struct;
/**
* Instantiates the builder.
*
* @param clazz The type of struct to build
*/
public SimpleStructBuilder(final Class<T> clazz) {
@ -29,7 +31,7 @@ public class SimpleStructBuilder<T extends Struct> implements StructBuilder {
try {
struct = ConstructorUtils.invokeConstructor(clazz);
} catch (InstantiationException | IllegalAccessException
| NoSuchMethodException | InvocationTargetException e) {
| NoSuchMethodException | InvocationTargetException e) {
throw new StructBuilderException(
String.format("Unexpected error constructing new instance: %s",
clazz.getSimpleName(), e.getMessage()), e);
@ -59,17 +61,13 @@ public class SimpleStructBuilder<T extends Struct> implements StructBuilder {
public SimpleStructBuilder<T> set(final String field, final Number value) {
try {
String type = clazz.getDeclaredField(field).getType().getSimpleName();
switch (clazz.getDeclaredField(field).getType().getSimpleName()) {
case "short":
return setField(field, value.shortValue());
case "int":
return setField(field, value.intValue());
case "byte":
return setField(field, value.byteValue());
default:
throw new StructBuilderException(
String.format("Unhandled numeric field type: %s", type));
}
return switch (clazz.getDeclaredField(field).getType().getSimpleName()) {
case "short" -> setField(field, value.shortValue());
case "int" -> setField(field, value.intValue());
case "byte" -> setField(field, value.byteValue());
default -> throw new StructBuilderException(
String.format("Unhandled numeric field type: %s", type));
};
} catch (NoSuchFieldException | SecurityException e) {
throw new StructBuilderException(
String.format("Unexpected error setting field: %s",
@ -122,7 +120,7 @@ public class SimpleStructBuilder<T extends Struct> implements StructBuilder {
FieldUtils.writeField(field, struct, value);
field.setAccessible(false);
} catch (NoSuchFieldException | SecurityException
| IllegalArgumentException | IllegalAccessException e) {
| IllegalArgumentException | IllegalAccessException e) {
throw new StructBuilderException(
String.format("Unexpected error setting field: %s",
fieldName, e.getMessage()), e);

View File

@ -25,7 +25,7 @@ public class SimpleStructConverter implements StructConverter {
// using output stream resources, serialize the specified struct
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream)) {
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream)) {
// obtain the struct elements definition
StructElements structElements = struct.getClass().getAnnotation(StructElements.class);

View File

@ -20,8 +20,8 @@ public interface StructConverter {
*
* @param data to be parsed
* @param type type of data being parsed
* @param <T> the {@link Struct} type
* @param <T> the {@link Struct} type
* @return de-serialized struct
*/
<T extends Struct> T convert(final byte[] data, final Class<T> type);
<T extends Struct> T convert(byte[] data, Class<T> type);
}

View File

@ -14,7 +14,7 @@ import java.lang.annotation.Target;
public @interface StructElementLength {
/**
* the field that this length represents.
* @return the field that this length represents.
*/
String fieldName();
}

View File

@ -13,7 +13,7 @@ import java.lang.annotation.Target;
public @interface StructElements {
/**
* elements in order to be processed by a converter.
* @return elements in order to be processed by a converter.
*/
String[] elements();
}

View File

@ -10,10 +10,10 @@ import java.util.Arrays;
* A container for an encoded {@link hirs.structs.elements.tpm.IdentityRequest},
* its associated endorsement credential, and its device information.
*/
@StructElements(elements = { "requestLength", "request",
"endorsementCredentialModulusLength", "endorsementCredentialModulus",
"endorsementCredentialLength", "endorsementCredential",
"deviceInfoReportLength", "deviceInfoReport" })
@StructElements(elements = {"requestLength", "request",
"endorsementCredentialModulusLength", "endorsementCredentialModulus",
"endorsementCredentialLength", "endorsementCredential",
"deviceInfoReportLength", "deviceInfoReport"})
public class IdentityRequestEnvelope implements Struct {
@StructElementLength(fieldName = "request")
@ -72,7 +72,6 @@ public class IdentityRequestEnvelope implements Struct {
}
/**
*
* @return the endorsementCredential
*/
public byte[] getEndorsementCredential() {
@ -80,7 +79,6 @@ public class IdentityRequestEnvelope implements Struct {
}
/**
*
* @return the length of the device info report
*/
public int getDeviceInfoReportLength() {
@ -88,7 +86,6 @@ public class IdentityRequestEnvelope implements Struct {
}
/**
*
* @return the device info report
*/
public byte[] getDeviceInfoReport() {

View File

@ -9,8 +9,8 @@ import java.util.Arrays;
/**
* Data structure used by the ACA to respond back to a client's {@link IdentityRequestEnvelope}.
*/
@StructElements(elements = { "asymmetricContentsSize", "asymmetricContents",
"symmetricAttestation" })
@StructElements(elements = {"asymmetricContentsSize", "asymmetricContents",
"symmetricAttestation"})
public class IdentityResponseEnvelope implements Struct {
@StructElementLength(fieldName = "asymmetricContents")

View File

@ -12,7 +12,7 @@ import java.util.Arrays;
* the envelope contains the Identity Credential that is signed by the ACA. This along with the key
* parameters are typically sent to the TPM to activate an Identity.
*/
@StructElements(elements = { "credentialSize", "algorithm", "credential" })
@StructElements(elements = {"credentialSize", "algorithm", "credential"})
public class SymmetricAttestation implements Struct {
@StructElementLength(fieldName = "credential")

View File

@ -8,8 +8,8 @@ import hirs.structs.elements.StructElements;
* As defined in TCPA 4.20, the key parameters data structure describes the parameters used to
* generate a key pair and to store the parts of a key.
*/
@StructElements(elements = { "algorithmId", "encryptionScheme", "signatureScheme", "paramsSize",
"params" })
@StructElements(elements = {"algorithmId", "encryptionScheme", "signatureScheme", "paramsSize",
"params"})
public class AsymmetricKeyParams implements Struct {
private int algorithmId;

View File

@ -8,7 +8,7 @@ import hirs.structs.elements.StructElements;
* portion of an asymmetric key pair. It contains all the information necessary for it's unambiguous
* usage.
*/
@StructElements(elements = { "asymmetricKeyParams", "storePubKey" })
@StructElements(elements = {"asymmetricKeyParams", "storePubKey"})
public class AsymmetricPublicKey implements Struct {
/**

View File

@ -35,11 +35,6 @@ public enum EncryptionScheme {
this.encryptionScheme = encryptionScheme;
}
@Override
public String toString() {
return this.encryptionScheme;
}
/**
* Maps an {@link EncryptionScheme} based upon an integer. If the scheme is unmapped, the
* default, {@link #PKCS1} is returned.
@ -48,11 +43,14 @@ public enum EncryptionScheme {
* @return the encryption scheme, or if unknown, the default.
*/
public static EncryptionScheme fromInt(final int scheme) {
switch (scheme) {
case OAEP_VALUE:
return OAEP;
default:
return PKCS1;
if (scheme == OAEP_VALUE) {
return OAEP;
}
return PKCS1;
}
@Override
public String toString() {
return this.encryptionScheme;
}
}

View File

@ -11,9 +11,9 @@ import java.util.Arrays;
* identity process. This structure contains information that is required by the Attestation
* Certificate Authority to attest an identity request.
*/
@StructElements(elements = { "version", "labelSize", "identityBindingSize", "endorsementSize",
@StructElements(elements = {"version", "labelSize", "identityBindingSize", "endorsementSize",
"platformSize", "conformanceSize", "identityKey", "label", "identityBinding",
"endorsementCredential", "platformCredential", "conformanceCredential" })
"endorsementCredential", "platformCredential", "conformanceCredential"})
public class IdentityProof implements Struct {
private Version version;

View File

@ -10,8 +10,8 @@ import java.util.Arrays;
* As specified in TCPA 4.30.2 specification. This structure is sent to the Attestation Certificate
* Authority to create an Identity Credential.
*/
@StructElements(elements = { "asymmetricBlobSize", "symmetricBlobSize", "asymmetricAlgorithm",
"symmetricAlgorithm", "asymmetricBlob", "symmetricBlob" })
@StructElements(elements = {"asymmetricBlobSize", "symmetricBlobSize", "asymmetricAlgorithm",
"symmetricAlgorithm", "asymmetricBlob", "symmetricBlob"})
public class IdentityRequest implements Struct {
@StructElementLength(fieldName = "asymmetricBlob")

View File

@ -8,7 +8,7 @@ import hirs.structs.elements.StructElements;
* portion of an asymmetric key pair. It contains all the information necessary for it's unambiguous
* usage.
*/
@StructElements(elements = { "asymmetricKeyParams", "storePubKey" })
@StructElements(elements = {"asymmetricKeyParams", "storePubKey"})
public class PublicKey implements Struct {
private AsymmetricKeyParams asymmetricKeyParams;

View File

@ -10,7 +10,7 @@ import java.util.Arrays;
* Parameters that are used to describe a particular {@link AsymmetricKeyParams} as specified by the
* TCPA 4.20.
*/
@StructElements(elements = { "keyLength", "totalPrimes", "exponentSize", "exponent" })
@StructElements(elements = {"keyLength", "totalPrimes", "exponentSize", "exponent"})
public class RsaSubParams implements Struct {
private int keyLength;

View File

@ -10,7 +10,7 @@ import java.util.Arrays;
* As specified in TCPA Main Specification section 4.27.2. This structure represents a public key of
* an asymmetric key pair.
*/
@StructElements(elements = { "keyLength", "key" })
@StructElements(elements = {"keyLength", "key"})
public class StorePubKey implements Struct {
@StructElementLength(fieldName = "key")

View File

@ -10,7 +10,7 @@ import java.util.Arrays;
* Part of the TPM Identity Request. This Structure is encrypted inside the request and is typically
* unencrypted by an Attestation Certificate Authority.
*/
@StructElements(elements = { "algorithmId", "encryptionScheme", "keySize", "key" })
@StructElements(elements = {"algorithmId", "encryptionScheme", "keySize", "key"})
public class SymmetricKey implements Struct {
/**

View File

@ -7,8 +7,8 @@ import hirs.structs.elements.StructElements;
/**
* Represents a symmetric key as specified in section 4.20 of the TCPA.
*/
@StructElements(elements = { "algorithmId", "encryptionScheme", "signatureScheme", "paramsSize",
"params" })
@StructElements(elements = {"algorithmId", "encryptionScheme", "signatureScheme", "paramsSize",
"params"})
public class SymmetricKeyParams implements Struct {
/**

View File

@ -10,7 +10,7 @@ import java.util.Arrays;
* Represents a dynamic key parameters data structure that is enclosed inside a {@link
* SymmetricKeyParams}.
*/
@StructElements(elements = { "keyLength", "blockSize", "ivSize", "iv" })
@StructElements(elements = {"keyLength", "blockSize", "ivSize", "iv"})
public class SymmetricSubParams implements Struct {
private int keyLength;

View File

@ -7,7 +7,7 @@ import hirs.structs.elements.StructElements;
* As specified in the TCPA Main Specification section 4.5. This structure represents the version of
* the TPM.
*/
@StructElements(elements = { "major", "minor", "revisionMajor", "revisionMinor" })
@StructElements(elements = {"major", "minor", "revisionMajor", "revisionMinor"})
public class Version implements Struct {
private byte major;

View File

@ -1,8 +1,9 @@
package hirs.structs.converters;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
* Tests suite for {@link SimpleStructConverter}.
@ -13,24 +14,25 @@ public class SimpleStructBuilderTest {
/**
* Tests {@link SimpleStructBuilder#build()}.
* @throws NoSuchFieldException sometimes
* @throws IllegalAccessException sometimes
*
* @throws NoSuchFieldException sometimes
* @throws IllegalAccessException sometimes
* @throws IllegalArgumentException sometimes
*/
@Test
public final void testBuild() throws NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
TestStruct struct = new SimpleStructBuilder<>(TestStruct.class)
.set("testShort", NUMBER)
.set("testByte", NUMBER)
.set("testEmbeddedStruct", new SimpleStructBuilder<>(TestEmbeddedStruct.class)
.set("embeddedShort", NUMBER)
.set("embedded", ARRAY)
.build())
.set("testVariableStruct", new SimpleStructBuilder<>(TestVariableStruct.class)
.set("testArray", ARRAY)
.build())
.build();
.set("testShort", NUMBER)
.set("testByte", NUMBER)
.set("testEmbeddedStruct", new SimpleStructBuilder<>(TestEmbeddedStruct.class)
.set("embeddedShort", NUMBER)
.set("embedded", ARRAY)
.build())
.set("testVariableStruct", new SimpleStructBuilder<>(TestVariableStruct.class)
.set("testArray", ARRAY)
.build())
.build();
assertEquals(NUMBER, struct.getTestShort());
assertEquals(NUMBER, struct.getTestByte());

View File

@ -1,8 +1,9 @@
package hirs.structs.converters;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/**
* Tests suite for {@link SimpleStructConverter}.
@ -10,11 +11,11 @@ import org.junit.jupiter.api.Test;
public class SimpleStructConverterTest {
private static final byte[] EXPECTED_BYTES =
new byte[]{0, 5, 0, 0, 0, 10, 0, 7, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 6, 0, 0, 0, 0};
new byte[] {0, 5, 0, 0, 0, 10, 0, 7, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 6, 0, 0, 0, 0};
private final TestStruct testStruct = new TestStruct();
private StructConverter converter = new SimpleStructConverter();
private final StructConverter converter = new SimpleStructConverter();
/**
* Tests {@link SimpleStructConverter#convert(hirs.structs.elements.Struct)}.
@ -47,8 +48,9 @@ public class SimpleStructConverterTest {
*/
@Test
public final void testNoElementsStructConvertToArray() {
assertThrows(StructConversionException.class, () ->
{converter.convert(new TestNoElementsAnnotationStruct());}, ".*@StructElements.*");
assertThrows(StructConversionException.class, () -> {
converter.convert(new TestNoElementsAnnotationStruct());
}, ".*@StructElements.*");
}
/**
@ -57,8 +59,9 @@ public class SimpleStructConverterTest {
*/
@Test
public final void testNoElementsStructConvertToStruct() {
assertThrows(StructConversionException.class, () ->
{converter.convert(new byte[1], TestNoElementsAnnotationStruct.class);}, ".*@StructElements.*");
assertThrows(StructConversionException.class, () -> {
converter.convert(new byte[1], TestNoElementsAnnotationStruct.class);
}, ".*@StructElements.*");
}
/**
@ -68,8 +71,9 @@ public class SimpleStructConverterTest {
*/
@Test
public final void testInvalidDataTypeStructConvertToArray() {
assertThrows(StructConversionException.class, () ->
{converter.convert(new TestInvalidDataTypeStruct());}, "Unsupported field type.*");
assertThrows(StructConversionException.class, () -> {
converter.convert(new TestInvalidDataTypeStruct());
}, "Unsupported field type.*");
}
/**
@ -79,8 +83,9 @@ public class SimpleStructConverterTest {
*/
@Test
public final void testInvalidDataTypeStructConvertToStruct() {
assertThrows(StructConversionException.class, () ->
{converter.convert(new byte[1], TestInvalidDataTypeStruct.class);}, "Unsupported field type.*");
assertThrows(StructConversionException.class, () -> {
converter.convert(new byte[1], TestInvalidDataTypeStruct.class);
}, "Unsupported field type.*");
}

View File

@ -9,7 +9,7 @@ import java.util.Arrays;
/**
* A struct to be embedded within TestStruct.
*/
@StructElements(elements = { "embeddedSize", "embeddedShort", "embedded" })
@StructElements(elements = {"embeddedSize", "embeddedShort", "embedded"})
public class TestEmbeddedStruct implements Struct {
private static final int EMBEDDED_SIZE = 10;
@ -18,7 +18,7 @@ public class TestEmbeddedStruct implements Struct {
private static final int HASH_CODE = 31;
private static final byte[] DEFAULT_ARRAY = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
private static final byte[] DEFAULT_ARRAY = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
@StructElementLength(fieldName = "embedded")
@ -28,6 +28,12 @@ public class TestEmbeddedStruct implements Struct {
private byte[] embedded = DEFAULT_ARRAY;
/**
* Returns true if the provided object is equivalent to this class.
*
* @param o object to compare
* @return true if the provided object is equal to this class
*/
@Override
public boolean equals(final Object o) {
if (this == o) {
@ -53,6 +59,7 @@ public class TestEmbeddedStruct implements Struct {
/**
* Getter.
*
* @return value
*/
public byte[] getEmbedded() {
@ -61,6 +68,7 @@ public class TestEmbeddedStruct implements Struct {
/**
* Getter.
*
* @return value
*/
public short getEmbeddedShort() {
@ -69,6 +77,7 @@ public class TestEmbeddedStruct implements Struct {
/**
* Getter.
*
* @return value
*/
public int getEmbeddedSize() {

View File

@ -6,7 +6,7 @@ import hirs.structs.elements.StructElements;
/**
* Test Struct that has an unsupported data type.
*/
@StructElements(elements = { "testLong" })
@StructElements(elements = {"testLong"})
public class TestInvalidDataTypeStruct implements Struct {
private static final Long TEST_LONG_VALUE = 1L;

View File

@ -8,8 +8,8 @@ import hirs.structs.elements.StructElements;
* A Struct class designed to fully test the design of the converter being tested.
*/
@StructElements(
elements = { "testShort", "testEmbeddedStruct", "testByte", "testVariableStructLength",
"testVariableStruct" })
elements = {"testShort", "testEmbeddedStruct", "testByte", "testVariableStructLength",
"testVariableStruct"})
public class TestStruct implements Struct {
private static final short TEST_SHORT = 0x5;
@ -54,6 +54,7 @@ public class TestStruct implements Struct {
/**
* Getter.
*
* @return value
*/
public byte getTestByte() {
@ -62,6 +63,7 @@ public class TestStruct implements Struct {
/**
* Getter.
*
* @return value
*/
public short getTestShort() {
@ -70,6 +72,7 @@ public class TestStruct implements Struct {
/**
* Getter.
*
* @return value
*/
public TestEmbeddedStruct getTestEmbeddedStruct() {
@ -78,6 +81,7 @@ public class TestStruct implements Struct {
/**
* Getter.
*
* @return value
*/
public TestVariableStruct getTestVariableStruct() {
@ -86,6 +90,7 @@ public class TestStruct implements Struct {
/**
* Getter.
*
* @return value
*/
public int getTestVariableStructLength() {

View File

@ -14,6 +14,7 @@ public class TestVariableStruct implements Struct {
/**
* Getter.
*
* @return value
*/
public byte[] getTestArray() {