Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
46749bd
DateValidationRule Logic Updates
jeremiahjstacey Jan 8, 2019
cb93195
DateValidationRule Test Content
jeremiahjstacey Jan 8, 2019
d9aa1a8
Dependency Cleanup: JodaTime
jeremiahjstacey Jan 8, 2019
500e74f
DateValidationRule Test Content
jeremiahjstacey Jan 9, 2019
1b38336
DateValidationRule Test Content
jeremiahjstacey Jan 9, 2019
0333719
Validation Test Cleanup
jeremiahjstacey Jan 9, 2019
3e814f2
Validation Test Cleanup
jeremiahjstacey Jan 9, 2019
3ab93c2
Validation Test Cleanup
jeremiahjstacey Jan 9, 2019
fcd08de
Validation Test Cleanup
jeremiahjstacey Jan 9, 2019
9aa3b16
Validation Test Cleanup
jeremiahjstacey Jan 9, 2019
9ec6841
Validation Test Cleanup BaseValidationRuleTest
jeremiahjstacey Jan 9, 2019
09c8ce6
Validation Test Cleanup: BaseValidationRuleTest
jeremiahjstacey Jan 9, 2019
1c9f174
Validation Test Cleanup: BaseValidationRuleTest
jeremiahjstacey Jan 10, 2019
544a5d2
Validation Test Cleanup: DateValidationRuleTest
jeremiahjstacey Jan 10, 2019
4ed0349
Validation Test Cleanup: DateValidationRuleTest
jeremiahjstacey Jan 11, 2019
8bbdd47
Merge branch 'ValidationTests_Sanity' into DateValidationRule_299
jeremiahjstacey Jan 11, 2019
d271440
DateValidationRule Logic & Test Updates
jeremiahjstacey Jan 11, 2019
7be7724
Validation Test Updates: BaseValidationRuleTest
jeremiahjstacey Jan 11, 2019
9632d8d
DateValidation Corrections for DefaultValidator
jeremiahjstacey Jan 11, 2019
c80b069
DateValidationTest API Extension
jeremiahjstacey Jan 11, 2019
935fe55
DefaultValidator Date Validation Logic Update
jeremiahjstacey Jan 11, 2019
281c7c2
DefaultValidator Date API Testing
jeremiahjstacey Jan 11, 2019
f086eea
DefaultValidator Date API Testing
jeremiahjstacey Jan 11, 2019
6fd83ca
DefaultValidator Logic Update & Date API Tests
jeremiahjstacey Jan 11, 2019
ae7a93d
DefaultValidator Date API Negative Tests
jeremiahjstacey Jan 11, 2019
16ebdf4
DefaultValidator Date API Test Cleanup
jeremiahjstacey Jan 11, 2019
aee2865
DefaultValidator cleanup
jeremiahjstacey Jan 11, 2019
7dca5b0
ValidatorTest Cleanup :: New ESAPI API Test
jeremiahjstacey Jan 11, 2019
53adbde
DateValdationRuleTest Additions
jeremiahjstacey Jan 11, 2019
faab33e
BaseValidationRuleTest UTF-16 Whitelist Addition
jeremiahjstacey Jan 14, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/owasp/esapi/ValidationErrorList.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public class ValidationErrorList {
* @param vex A {@code ValidationException}.
*/
public void addError(String context, ValidationException vex) {
if ( context == null ) throw new RuntimeException( "Context for cannot be null: " + vex.getLogMessage(), vex );
if ( vex == null ) throw new RuntimeException( "Context (" + context + ") cannot be null" );
if ( context == null ) throw new RuntimeException( "Context cannot be null: " + vex.getLogMessage(), vex );
if ( vex == null ) throw new RuntimeException( "ValidationException cannot be null for context (" + context + ")" );
if (getError(context) != null) throw new RuntimeException("Context (" + context + ") already exists, must be unique");
errorList.put(context, vex);
}
Expand Down
37 changes: 20 additions & 17 deletions src/main/java/org/owasp/esapi/reference/DefaultValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,35 +277,38 @@ public boolean isValidDate(String context, String input, DateFormat format, bool
* {@inheritDoc}
*/
public boolean isValidDate(String context, String input, DateFormat format, boolean allowNull, ValidationErrorList errors) throws IntrusionException {
try {
getValidDate( context, input, format, allowNull);
return true;
} catch( ValidationException e ) {
errors.addError(context, e);
return false;
}
getValidDate( context, input, format, allowNull, errors);
return errors.isEmpty();
}

/**
* {@inheritDoc}
*/
public Date getValidDate(String context, String input, DateFormat format, boolean allowNull) throws ValidationException, IntrusionException {
DateValidationRule dvr = new DateValidationRule( "SimpleDate", encoder, format);
dvr.setAllowNull(allowNull);
return dvr.getValid(context, input);

ValidationErrorList vel = new ValidationErrorList();
Date validDate = getValidDate(context, input, format, allowNull, vel);

if (vel.isEmpty()) {
return validDate;
}

throw vel.errors().get(0);
}

/**
* {@inheritDoc}
*/
public Date getValidDate(String context, String input, DateFormat format, boolean allowNull, ValidationErrorList errors) throws IntrusionException {
try {
return getValidDate(context, input, format, allowNull);
} catch (ValidationException e) {
errors.addError(context, e);
}
// error has been added to list, so return null
return null;
Date safeDate = null;
DateValidationRule dvr = new DateValidationRule( "SimpleDate", encoder, format);
dvr.setAllowNull(allowNull);
safeDate = dvr.sanitize(context, input, errors);
if (!errors.isEmpty()) {
safeDate = null;
}
// error has been added to list, so return null
return safeDate;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.owasp.esapi.ESAPI;
import org.owasp.esapi.Encoder;
import org.owasp.esapi.StringUtilities;
import org.owasp.esapi.ValidationErrorList;
import org.owasp.esapi.errors.ValidationException;

/**
Expand Down Expand Up @@ -60,7 +61,7 @@ public final void setDateFormat( DateFormat newFormat ) {
* {@inheritDoc}
*/
public Date getValid( String context, String input ) throws ValidationException {
return safelyParse(context, input);
return safelyParse(context, input, false);
}

/**
Expand All @@ -70,16 +71,25 @@ public Date getValid( String context, String input ) throws ValidationException
*/
@Override
public Date sanitize( String context, String input ) {
Date date = new Date(0);
try {
date = safelyParse(context, input);
} catch (ValidationException e) {
// do nothing
}
return date;
return sanitize(context, input, null);
}

/**
* {@inheritDoc}
*/
public Date sanitize( String context, String input, ValidationErrorList errorList ) {
Date date = new Date(0);
try {
date = safelyParse(context, input, true);
} catch (ValidationException e) {
if (errorList != null) {
errorList.addError(context, e);
}
}
return date;
}

private Date safelyParse(String context, String input)
private Date safelyParse(String context, String input, boolean sanitize)
throws ValidationException {
// CHECKME should this allow empty Strings? " " use IsBlank instead?
if (StringUtilities.isEmpty(input)) {
Expand All @@ -91,10 +101,16 @@ private Date safelyParse(String context, String input)
+ input, context);
}

String canonical = encoder.canonicalize(input);

try {
return format.parse(canonical);
String canonical = encoder.canonicalize(input);
try {
Date rval = format.parse(canonical);
if (sanitize) {
String cycled = format.format(rval);
if (!cycled.equals(canonical)) {
throw new Exception("Parameter date is not a clean translation between String and Date contexts. Check input for additional characters");
}
}
return rval;
} catch (Exception e) {
throw new ValidationException(context
+ ": Invalid date must follow the "
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/org/owasp/esapi/ESAPIContractAPITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.owasp.esapi;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.internal.verification.VerificationModeFactory;
import org.owasp.esapi.util.ObjFactory;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;


@RunWith(PowerMockRunner.class)
@PrepareForTest({ObjFactory.class})
public class ESAPIContractAPITest {

@Mock
private SecurityConfiguration mockSecConfig;

@Mock
private Validator mockValidator;

@Before
public void configureStaticContexts() throws Exception {
PowerMockito.mockStatic(ObjFactory.class);
PowerMockito.when(ObjFactory.class, "make", ArgumentMatchers.anyString(), ArgumentMatchers.eq("SecurityConfiguration")).thenReturn(mockSecConfig);
PowerMockito.when(ObjFactory.class, "make", ArgumentMatchers.eq("MOCK_TEST_VALIDATOR"), ArgumentMatchers.eq("Validator")).thenReturn(mockValidator);

PowerMockito.when(mockSecConfig.getValidationImplementation()).thenReturn("MOCK_TEST_VALIDATOR");
}

@Test
public void testValidatorFromConfiguration() {
Validator validator = ESAPI.validator();
Assert.assertEquals("ESAPI Configuration should return Validator as specified by the SecurityConfiguration", mockValidator, validator);

PowerMockito.verifyStatic(ObjFactory.class, VerificationModeFactory.times(1));
ObjFactory.make(ArgumentMatchers.anyString(), ArgumentMatchers.eq("SecurityConfiguration"));

PowerMockito.verifyStatic(ObjFactory.class, VerificationModeFactory.times(1));
ObjFactory.make(ArgumentMatchers.eq("MOCK_TEST_VALIDATOR"), ArgumentMatchers.eq("Validator"));

PowerMockito.verifyNoMoreInteractions(ObjFactory.class);

Mockito.verify(mockSecConfig, Mockito.times(1)).getValidationImplementation();
}

}
164 changes: 57 additions & 107 deletions src/test/java/org/owasp/esapi/ValidationErrorListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,126 +15,76 @@
*/
package org.owasp.esapi;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.owasp.esapi.errors.IntrusionException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TestName;
import org.owasp.esapi.errors.ValidationException;


/**
* @author Jeff Williams (jeff.williams@aspectsecurity.com)
*/
public class ValidationErrorListTest extends TestCase {

/**
* Instantiates a new executor test.
*
* @param testName
* the test name
*/
public ValidationErrorListTest(String testName) {
super(testName);
}

/**
* {@inheritDoc}
*
* @throws Exception
*/
protected void setUp() throws Exception {
// none
}
public class ValidationErrorListTest {
@Rule
public ExpectedException exEx = ExpectedException.none();
@Rule
public TestName testName = new TestName();

/**
* {@inheritDoc}
*
* @throws Exception
*/
protected void tearDown() throws Exception {
// none
}
ValidationErrorList vel = new ValidationErrorList();
ValidationException vex = new ValidationException(testName.getMethodName(), testName.getMethodName());
@Test
public void testAddErrorNullContextThrows() {
exEx.expect(RuntimeException.class);
exEx.expectMessage("Context cannot be null");
vel.addError(null, vex);
}

/**
* Suite.
*
* @return the test
*/
public static Test suite() {
TestSuite suite = new TestSuite(ValidationErrorListTest.class);
return suite;
}

public void testAddError() throws Exception {
System.out.println("testAddError");
ValidationErrorList vel = new ValidationErrorList();
ValidationException vex = createValidationException();
vel.addError("context", vex );
try {
vel.addError(null, vex );
fail();
} catch( Exception e ) {
// expected
}
try {
vel.addError("context1", null );
fail();
} catch( Exception e ) {
// expected
}
try {
vel.addError("context", vex ); // add the same context again
fail();
} catch( Exception e ) {
// expected
}
}
@Test
public void testAddErrorNullExceptionThrows() {
exEx.expect(RuntimeException.class);
exEx.expectMessage("ValidationException cannot be null");
vel.addError(testName.getMethodName(), null);
}
public void testAddErrorDuplicateContextThrows() {
exEx.expect(RuntimeException.class);
exEx.expectMessage("already exists, must be unique");
vel.addError(testName.getMethodName(), vex);
vel.addError(testName.getMethodName(), vex);
}

public void testErrors() throws Exception {
System.out.println("testErrors");
ValidationErrorList vel = new ValidationErrorList();
ValidationException vex = createValidationException();
vel.addError("context", vex );
assertTrue( vel.errors().get(0) == vex );
}
@Test
public void testErrors() throws Exception {
vel.addError("context", vex );
assertTrue("Validation Errors List should contain the added ValidationException Reference",vel.errors().contains( vex) );
}

public void testGetError() throws Exception {
System.out.println("testGetError");
ValidationErrorList vel = new ValidationErrorList();
ValidationException vex = createValidationException();
vel.addError("context", vex );
assertTrue( vel.getError( "context" ) == vex );
assertTrue( vel.getError( "ridiculous" ) == null );
}
@Test
public void testGetError() throws Exception {
vel.addError("context", vex );
assertTrue( vel.getError( "context" ) == vex );
assertNull( vel.getError( "ridiculous" ) );
}

public void testIsEmpty() throws Exception {
System.out.println("testIsEmpty");
ValidationErrorList vel = new ValidationErrorList();
assertTrue( vel.isEmpty() );
ValidationException vex = createValidationException();
vel.addError("context", vex );
assertFalse( vel.isEmpty() );
}
@Test
public void testIsEmpty() throws Exception {
assertTrue( vel.isEmpty() );
vel.addError("context", vex );
assertFalse( vel.isEmpty() );
}

public void testSize() throws Exception {
System.out.println("testSize");
ValidationErrorList vel = new ValidationErrorList();
assertTrue( vel.size() == 0 );
ValidationException vex = createValidationException();
vel.addError("context", vex );
assertTrue( vel.size() == 1 );
}
@Test
public void testSize() throws Exception {
assertEquals(0, vel.size() );
vel.addError("context", vex );
assertEquals(1, vel.size());
}

private ValidationException createValidationException() {
ValidationException vex = null;
try {
vex = new ValidationException("User message", "Log Message");
} catch( IntrusionException e ) {
// expected occasionally
}
return vex;
}

}


Loading