validation modifications and test code modifications

This commit is contained in:
Brian Sweeney 2018-08-23 17:53:21 -06:00
parent 4c48fd4454
commit cebb79822e
2 changed files with 19 additions and 67 deletions

View File

@ -117,7 +117,15 @@ final public class CorrelationAttributeNormalizer {
if(validator.isValid(data)){ if(validator.isValid(data)){
return data.toLowerCase(); return data.toLowerCase();
} else { } else {
throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid domain: %s", data)); if(data == null){
throw new CorrelationAttributeNormalizationException("Data was expected to be a valid domain: null");
}
final String validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
if(data.matches(validIpAddressRegex)){
return data;
} else {
throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid domain: %s", data));
}
} }
} }
@ -146,19 +154,10 @@ final public class CorrelationAttributeNormalizer {
} }
/** /**
* USB ID is of the form: hhhh:hhhh where h is a hex digit. Convert to lower case. * Vacuous - will be replaced with something reasonable later.
*/ */
private static String normalizeUsbId(String data) throws CorrelationAttributeNormalizationException { private static String normalizeUsbId(String data) throws CorrelationAttributeNormalizationException {
final String errorMessage = "Data was expected to be a valid USB device ID: %s"; //TODO replace with correct usb id validation at a later date
if(data == null){ return data;
throw new CorrelationAttributeNormalizationException(String.format(errorMessage, data));
}
String validUsbIdRegex = "^(0[Xx])?[A-Fa-f0-9]{4}[:\\\\\\ \\-.]?(0[Xx])?[A-Fa-f0-9]{4}$";
if(data.matches(validUsbIdRegex)){
return data.toLowerCase();
} else {
throw new CorrelationAttributeNormalizationException(String.format(errorMessage, data));
}
} }
} }

View File

@ -20,6 +20,7 @@
package org.sleuthkit.autopsy.centralrepository.datamodel; package org.sleuthkit.autopsy.centralrepository.datamodel;
import junit.framework.Test; import junit.framework.Test;
import org.junit.Assert;
import org.netbeans.junit.NbModuleSuite; import org.netbeans.junit.NbModuleSuite;
import org.netbeans.junit.NbTestCase; import org.netbeans.junit.NbTestCase;
import org.openide.util.Exceptions; import org.openide.util.Exceptions;
@ -294,69 +295,21 @@ public class CorrelationAttributeNormalizerTest extends NbTestCase {
public void testValidateUsbId() { public void testValidateUsbId() {
final String goodIdOne = "0202:AAFF"; //should pass and be lowered final String goodIdOne = "0202:AAFF"; //should pass and be lowered
final String goodIdTwo = "0202:aaff"; //should pass //TODO add all this back when theres is something to validate
/*final String goodIdTwo = "0202:aaff"; //should pass
final String badIdThree = "0202:axxf"; //should fail final String badIdThree = "0202:axxf"; //should fail
final String badIdFour = ""; //should fail final String badIdFour = ""; //should fail
final String badIdFive = null; //should fail final String badIdFive = null; //should fail
final String goodIdSix = "0202 AAFF"; //should pass final String goodIdSix = "0202 AAFF"; //should pass
final String goodIdSeven = "0202AAFF"; //should pass final String goodIdSeven = "0202AAFF"; //should pass
final String goodIdEight = "0202-AAFF"; //should pass final String goodIdEight = "0202-AAFF"; //should pass*/
final int USBID_TYPE_ID = CorrelationAttribute.USBID_TYPE_ID; final int USBID_TYPE_ID = CorrelationAttribute.USBID_TYPE_ID;
try { try {
assertTrue(THIS_USB_ID_SHOULD_PASS, CorrelationAttributeNormalizer.normalize(USBID_TYPE_ID, goodIdOne).equals(goodIdOne.toLowerCase())); assertTrue(THIS_USB_ID_SHOULD_PASS, CorrelationAttributeNormalizer.normalize(USBID_TYPE_ID, goodIdOne).equals(goodIdOne));
} catch (CorrelationAttributeNormalizationException ex) { } catch (CorrelationAttributeNormalizationException ex) {
Exceptions.printStackTrace(ex); Assert.fail(ex.getMessage());
fail(ex.getMessage());
}
try {
assertTrue(THIS_USB_ID_SHOULD_PASS, CorrelationAttributeNormalizer.normalize(USBID_TYPE_ID, goodIdTwo).equals(goodIdTwo));
} catch (CorrelationAttributeNormalizationException ex) {
Exceptions.printStackTrace(ex);
fail(ex.getMessage());
}
try {
assertTrue("This USB ID should fail.", CorrelationAttributeNormalizer.normalize(USBID_TYPE_ID, badIdThree).equals(badIdThree));
fail(THIS_SHOULD_HAVE_THROWN_AN_EXCEPTION);
} catch (CorrelationAttributeNormalizationException ex) {
assertTrue(WE_EXPECT_AN_EXCEPTION_HERE, true);
}
try {
CorrelationAttributeNormalizer.normalize(USBID_TYPE_ID, badIdFour);
fail(THIS_SHOULD_HAVE_THROWN_AN_EXCEPTION);
} catch (CorrelationAttributeNormalizationException ex) {
assertTrue(WE_EXPECT_AN_EXCEPTION_HERE, true);
}
try {
CorrelationAttributeNormalizer.normalize(USBID_TYPE_ID, badIdFive);
fail(THIS_SHOULD_HAVE_THROWN_AN_EXCEPTION);
} catch (CorrelationAttributeNormalizationException ex) {
assertTrue(WE_EXPECT_AN_EXCEPTION_HERE, true);
}
try {
CorrelationAttributeNormalizer.normalize(USBID_TYPE_ID, badIdFive);
fail(THIS_SHOULD_HAVE_THROWN_AN_EXCEPTION);
} catch (CorrelationAttributeNormalizationException ex) {
assertTrue(WE_EXPECT_AN_EXCEPTION_HERE, true);
}
try {
assertTrue(THIS_USB_ID_SHOULD_PASS, CorrelationAttributeNormalizer.normalize(USBID_TYPE_ID, goodIdSix).equals(goodIdSix.toLowerCase()));
} catch (CorrelationAttributeNormalizationException ex) {
Exceptions.printStackTrace(ex);
fail(ex.getMessage());
}
try {
assertTrue(THIS_USB_ID_SHOULD_PASS, CorrelationAttributeNormalizer.normalize(USBID_TYPE_ID, goodIdSeven).equals(goodIdSeven.toLowerCase()));
} catch (CorrelationAttributeNormalizationException ex) {
Exceptions.printStackTrace(ex);
fail(ex.getMessage());
}
try {
assertTrue(THIS_USB_ID_SHOULD_PASS, CorrelationAttributeNormalizer.normalize(USBID_TYPE_ID, goodIdEight).equals(goodIdEight.toLowerCase()));
} catch (CorrelationAttributeNormalizationException ex) {
Exceptions.printStackTrace(ex);
fail(ex.getMessage());
} }
} }
private static final String THIS_USB_ID_SHOULD_PASS = "This USB ID should pass."; private static final String THIS_USB_ID_SHOULD_PASS = "This USB ID should pass.";