diff --git a/HIRS_Utils/build.gradle b/HIRS_Utils/build.gradle index f2b6cbd4..366c27b9 100644 --- a/HIRS_Utils/build.gradle +++ b/HIRS_Utils/build.gradle @@ -47,6 +47,8 @@ dependencies { testImplementation 'org.hamcrest:hamcrest:2.2' testImplementation project(path: ':HIRS_AttestationCA') + testImplementation 'org.mockito:mockito-core:4.2.0' + compileOnly libs.lombok annotationProcessor libs.lombok diff --git a/HIRS_Utils/src/test/java/hirs/utils/BouncyCastleUtilsTest.java b/HIRS_Utils/src/test/java/hirs/utils/BouncyCastleUtilsTest.java new file mode 100644 index 00000000..8c706f23 --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/utils/BouncyCastleUtilsTest.java @@ -0,0 +1,51 @@ +package hirs.utils; + +import lombok.extern.log4j.Log4j2; +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; + +/** + * Tests methods in the (@link BouncyCastleUtils) utility class. + */ +@Log4j2 +public class BouncyCastleUtilsTest { + + private static final String VALID_RDN_STRING = "OU=PCTest,O=example.com,C=US"; + + private static final String VALID_RDN_STRING_SWITCHED = "C=US,OU=PCTest,O=example.com"; + private static final String VALID_RDN_STRING_UPPERCASE = "OU=PCTEST,O=EXAMPLE.COM,C=US"; + private static final String UNEQUAL_RDN_STRING = "OU=PCTest,O=example1.com,C=US"; + private static final String MALFORMED_RDN_STRING = "OU=PCTest,OZ=example1.com,C=US"; + + /** + * True Test of x500NameCompare method, of class BouncyCastleUtils. + */ + @Test + public void testX500NameCompareTrue() { + assertTrue(BouncyCastleUtils.x500NameCompare( + VALID_RDN_STRING, VALID_RDN_STRING_SWITCHED)); + assertTrue(BouncyCastleUtils.x500NameCompare( + VALID_RDN_STRING, VALID_RDN_STRING_UPPERCASE)); + } + + /** + * False Test of x500NameCompare method, of class BouncyCastleUtils. + */ + @Test + public void testX500NameCompareFalse() { + assertFalse(BouncyCastleUtils.x500NameCompare( + VALID_RDN_STRING, UNEQUAL_RDN_STRING)); + // Error that aren't thrown but logged + assertFalse(BouncyCastleUtils.x500NameCompare(VALID_RDN_STRING, Strings.EMPTY)); + assertFalse(BouncyCastleUtils.x500NameCompare(Strings.EMPTY, VALID_RDN_STRING)); + assertFalse(BouncyCastleUtils.x500NameCompare(Strings.EMPTY, Strings.EMPTY)); + assertFalse(BouncyCastleUtils.x500NameCompare( + VALID_RDN_STRING, MALFORMED_RDN_STRING)); + assertFalse(BouncyCastleUtils.x500NameCompare( + MALFORMED_RDN_STRING, VALID_RDN_STRING)); + assertFalse(BouncyCastleUtils.x500NameCompare( + MALFORMED_RDN_STRING, MALFORMED_RDN_STRING)); + } +} \ No newline at end of file diff --git a/HIRS_Utils/src/test/java/hirs/utils/HexUtilsTest.java b/HIRS_Utils/src/test/java/hirs/utils/HexUtilsTest.java new file mode 100644 index 00000000..9e3ba416 --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/utils/HexUtilsTest.java @@ -0,0 +1,73 @@ +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; + +/** + * Class for testing the HexUtils class. + */ +public class HexUtilsTest { + + /** + * Tests that we can convert a hex string to a byte array. + */ + @Test + public void testHexStringToByteArray() { + String s = "abcd1234"; + byte[] target = {-85, -51, 18, 52}; + + byte[] b = HexUtils.hexStringToByteArray(s); + assertArrayEquals(b, target); + } + + /** + * Tests that we can convert a hex string to a byte array. + */ + @Test + public void testByteArrayToHexString() { + String target = "abcd1234"; + byte[] b = {-85, -51, 18, 52}; + + String s = HexUtils.byteArrayToHexString(b); + assertEquals(s, target); + } + + /** + * Tests that we can convert a hex string to a byte array. + */ + @Test + public void testByteArrayToHexStringConditional() { + String target = "abcd0100"; + byte[] b = {-85, -51, 1, 0}; + + String s = HexUtils.byteArrayToHexString(b); + assertEquals(s, target); + } + + /** + * Tests that a hex string can be converted to an integer. + */ + @Test + public void testHexToInt() { + String s = "ff"; + Integer i = HexUtils.hexToInt(s); + assertEquals((int) i, HexUtils.FF_BYTE); + } + + /** + * Tests the subarray utility method. + */ + @Test + public void testSubarray() { + byte[] b = {-85, -51, 18, 52}; + byte[] target = {-51, 18}; + + byte[] sub = HexUtils.subarray(b, 1, 2); + + assertArrayEquals(sub, target); + } + + + +} diff --git a/HIRS_Utils/src/test/java/hirs/utils/StringValidatorTest.java b/HIRS_Utils/src/test/java/hirs/utils/StringValidatorTest.java new file mode 100644 index 00000000..18efdbc7 --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/utils/StringValidatorTest.java @@ -0,0 +1,158 @@ +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 static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.times; + +/** + * Contains tests for StringValidator. + */ +public class StringValidatorTest { + private static final int SMALL_LENGTH = 5; + private static final int LENGTH = 32; + private static final String NONEMPTY_VALUE = "test value"; + private static final String FIELD_NAME = "test field"; + + /** + * Tests that a value can be passed through with no actual validation. + */ + @Test + public void testNoValidation() { + assertEquals( + StringValidator.check(NONEMPTY_VALUE, FIELD_NAME).getValue(), + NONEMPTY_VALUE + ); + } + + /** + * Tests that a value can be passed through with no actual validation. + */ + @Test + public void testNoValidationOnNull() { + assertNull( + StringValidator.check(null, FIELD_NAME).getValue() + ); + } + + /** + * Tests that a non-null check against a String passes validation. + */ + @Test + public void testValidateNotNullSuccess() { + assertEquals( + StringValidator.check(NONEMPTY_VALUE, FIELD_NAME).notNull().getValue(), + NONEMPTY_VALUE + ); + } + + /** + * Tests that a non-null check against a null value fails validation. + */ + @Test + public void testValidateNotNullFailOnNull() { + assertThrows(IllegalArgumentException.class, () -> + StringValidator.check(null, FIELD_NAME).notNull()); + } + + /** + * Tests that a not-blank check against a String passes validation. + */ + @Test + public void testValidateNotBlankSuccess() { + assertEquals( + StringValidator.check(NONEMPTY_VALUE, FIELD_NAME).notBlank().getValue(), + NONEMPTY_VALUE + ); + } + + /** + * Tests that a not-blank check fails against a null String. + */ + @Test + public void testValidateNotBlankFailOnNull() { + assertThrows(IllegalArgumentException.class, () -> + StringValidator.check(null, FIELD_NAME).notBlank()); + } + + /** + * Tests that a not-blank check fails against an empty String. + */ + @Test + public void testValidateNotBlankFailOnEmpty() { + assertThrows(IllegalArgumentException.class, () -> + StringValidator.check("", FIELD_NAME).notBlank()); + } + + /** + * Tests that a max-length check against a String passes validation. + */ + @Test + public void testValidateMaxLengthSuccess() { + assertEquals( + StringValidator.check(NONEMPTY_VALUE, FIELD_NAME).maxLength(LENGTH).getValue(), + NONEMPTY_VALUE + ); + } + + /** + * Tests that a max-length check against a null String passes validation. + */ + @Test + public void testValidateMaxLengthSuccessOnNull() { + assertNull( + StringValidator.check(null, FIELD_NAME).maxLength(LENGTH).getValue() + ); + } + + /** + * Tests that a max-length check against a String (which is too long) fails validation. + */ + @Test + public void testValidateMaxLengthFailOnLongString() { + assertThrows(IllegalArgumentException.class, () -> + assertEquals( + StringValidator.check(NONEMPTY_VALUE, FIELD_NAME).maxLength(SMALL_LENGTH).getValue(), + NONEMPTY_VALUE + )); + } + + /** + * Tests that a String which passes multiple validations successfully returns its value. + */ + @Test + public void testValidateSeveralConditionsSuccess() { + assertEquals( + StringValidator.check(NONEMPTY_VALUE, FIELD_NAME) + .notNull().notBlank().maxLength(LENGTH).getValue(), + NONEMPTY_VALUE + ); + } + + /** + * Tests that a String can pass multiple validations and still fail the last one. + */ + @Test + public void testValidateSeveralConditionsFailOnLast() { + assertThrows(IllegalArgumentException.class, () -> + StringValidator.check(NONEMPTY_VALUE, FIELD_NAME) + .notNull().notBlank().maxLength(SMALL_LENGTH)); + } + + /** + * Tests that StringValidator will log error output to a provided logger. + */ + @Test + public void testCheckLoggerIsUsedOnFailure() { + Logger mockLogger = Mockito.mock(Logger.class); + try { + StringValidator.check(NONEMPTY_VALUE, FIELD_NAME, mockLogger).maxLength(SMALL_LENGTH); + } catch (IllegalArgumentException e) { + System.out.println("Avoiding empty catch block."); + } + Mockito.verify(mockLogger, times(1)).error(Mockito.anyString()); + } +} \ No newline at end of file diff --git a/HIRS_Utils/src/test/java/hirs/utils/VersionHelperTest.java b/HIRS_Utils/src/test/java/hirs/utils/VersionHelperTest.java new file mode 100644 index 00000000..d2bda70d --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/utils/VersionHelperTest.java @@ -0,0 +1,32 @@ +package hirs.utils; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests for VersionHelper. + */ +public class VersionHelperTest { + + /** + * Test that case where a version file does not exist. + */ + @Test + public void testGetVersionFail() { + + String actual = VersionHelper.getVersion("somefile"); + assertTrue(actual.startsWith("")); + } + + /** + * Test that a version file exists and can be read properly. + */ + @Test + public void testGetVersionDefault() { + + String expected = "Test.Version"; + String actual = VersionHelper.getVersion(); + assertEquals(expected, actual); + } +} diff --git a/HIRS_Utils/src/test/resources/VERSION b/HIRS_Utils/src/test/resources/VERSION new file mode 100644 index 00000000..b20b9417 --- /dev/null +++ b/HIRS_Utils/src/test/resources/VERSION @@ -0,0 +1 @@ +Test.Version