|
| 1 | +/** |
| 2 | + * OWASP Enterprise Security API (ESAPI) |
| 3 | + * |
| 4 | + * This file is part of the Open Web Application Security Project (OWASP) |
| 5 | + * Enterprise Security API (ESAPI) project. For details, please see |
| 6 | + * <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>. |
| 7 | + * |
| 8 | + * Copyright (c) 2007 - The OWASP Foundation |
| 9 | + * |
| 10 | + * The ESAPI is published by OWASP under the BSD license. You should read and accept the |
| 11 | + * LICENSE before you use, modify, and/or redistribute this software. |
| 12 | + * |
| 13 | + * @author Ben Sleek <a href="http://www.spartasystems.com">Sparta Systems</a> |
| 14 | + * @created 2015 |
| 15 | + */ |
| 16 | +package org.owasp.esapi.reference.validation; |
| 17 | + |
| 18 | +import junit.framework.Test; |
| 19 | +import junit.framework.TestCase; |
| 20 | +import junit.framework.TestSuite; |
| 21 | + |
| 22 | +import org.owasp.esapi.Encoder; |
| 23 | +import org.owasp.esapi.errors.ValidationException; |
| 24 | + |
| 25 | +public class BaseValidationRuleTest extends TestCase { |
| 26 | + |
| 27 | + /** |
| 28 | + * Instantiates a new base validation rule test. |
| 29 | + * |
| 30 | + * @param testName |
| 31 | + * the test name |
| 32 | + */ |
| 33 | + public BaseValidationRuleTest(String testName) { |
| 34 | + super(testName); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritDoc} |
| 39 | + * |
| 40 | + * @throws Exception |
| 41 | + */ |
| 42 | + protected void setUp() throws Exception { |
| 43 | + // none |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * {@inheritDoc} |
| 48 | + * |
| 49 | + * @throws Exception |
| 50 | + */ |
| 51 | + protected void tearDown() throws Exception { |
| 52 | + // none |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Suite. |
| 57 | + * |
| 58 | + * @return the test |
| 59 | + */ |
| 60 | + public static Test suite() { |
| 61 | + TestSuite suite = new TestSuite(BaseValidationRuleTest.class); |
| 62 | + return suite; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Verifies assertValid throws ValidationException on invalid input |
| 67 | + * Validates fix for Google issue #195 |
| 68 | + * |
| 69 | + * @throws ValidationException |
| 70 | + */ |
| 71 | + public void testAssertValid() throws ValidationException { |
| 72 | + SampleValidationRule rule = new SampleValidationRule("UnitTest"); |
| 73 | + try { |
| 74 | + rule.assertValid("testcontext", "badinput"); |
| 75 | + fail(); |
| 76 | + } catch (ValidationException e) { |
| 77 | + // success |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public class SampleValidationRule extends BaseValidationRule { |
| 82 | + |
| 83 | + public SampleValidationRule(String typeName, Encoder encoder) { |
| 84 | + super(typeName, encoder); |
| 85 | + } |
| 86 | + |
| 87 | + public SampleValidationRule(String typeName) { |
| 88 | + super(typeName); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected Object sanitize(String context, String input) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + public Object getValid(String context, String input) throws ValidationException { |
| 97 | + throw new ValidationException("Demonstration Exception", "Demonstration Exception"); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | +} |
0 commit comments