PageRenderTime 1463ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/org/owasp/esapi/reference/ValidatorTest.java

http://owasp-esapi-java.googlecode.com/
Java | 1150 lines | 915 code | 99 blank | 136 comment | 61 complexity | 6e6ab1fec929c49bd6196776f740f1e5 MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-SA-3.0

Large files files are truncated, but you can click here to view the full file

  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 Jeff Williams <a href="http://www.aspectsecurity.com">Aspect Security</a>
  14. * @created 2007
  15. */
  16. package org.owasp.esapi.reference;
  17. import java.io.BufferedReader;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.io.UnsupportedEncodingException;
  23. import java.text.DateFormat;
  24. import java.text.SimpleDateFormat;
  25. import java.util.*;
  26. import junit.framework.Test;
  27. import junit.framework.TestCase;
  28. import junit.framework.TestSuite;
  29. import org.owasp.esapi.*;
  30. import org.owasp.esapi.errors.ValidationException;
  31. import org.owasp.esapi.filters.SecurityWrapperRequest;
  32. import org.owasp.esapi.http.MockHttpServletRequest;
  33. import org.owasp.esapi.http.MockHttpServletResponse;
  34. import org.owasp.esapi.reference.validation.HTMLValidationRule;
  35. import org.owasp.esapi.reference.validation.StringValidationRule;
  36. import javax.servlet.http.Cookie;
  37. import javax.servlet.http.HttpServletRequest;
  38. /**
  39. * The Class ValidatorTest.
  40. *
  41. * @author Mike Fauzy (mike.fauzy@aspectsecurity.com)
  42. * @author Jeff Williams (jeff.williams@aspectsecurity.com)
  43. */
  44. public class ValidatorTest extends TestCase {
  45. private static final String PREFERRED_ENCODING = "UTF-8";
  46. public static Test suite() {
  47. return new TestSuite(ValidatorTest.class);
  48. }
  49. /**
  50. * Instantiates a new HTTP utilities test.
  51. *
  52. * @param testName the test name
  53. */
  54. public ValidatorTest(String testName) {
  55. super(testName);
  56. }
  57. /**
  58. * {@inheritDoc}
  59. *
  60. * @throws Exception
  61. */
  62. protected void setUp() throws Exception {
  63. // none
  64. }
  65. /**
  66. * {@inheritDoc}
  67. *
  68. * @throws Exception
  69. */
  70. protected void tearDown() throws Exception {
  71. // none
  72. }
  73. public void testAddRule() {
  74. Validator validator = ESAPI.validator();
  75. ValidationRule rule = new StringValidationRule("ridiculous");
  76. validator.addRule(rule);
  77. assertEquals(rule, validator.getRule("ridiculous"));
  78. }
  79. public void testAssertValidFileUpload() {
  80. // assertValidFileUpload(String, String, String, byte[], int, boolean, ValidationErrorList)
  81. }
  82. public void testGetPrintable1() {
  83. // getValidPrintable(String, char[], int, boolean, ValidationErrorList)
  84. }
  85. public void testGetPrintable2() {
  86. // getValidPrintable(String, String, int, boolean, ValidationErrorList)
  87. }
  88. public void testGetRule() {
  89. Validator validator = ESAPI.validator();
  90. ValidationRule rule = new StringValidationRule("rule");
  91. validator.addRule(rule);
  92. assertEquals(rule, validator.getRule("rule"));
  93. assertFalse(rule == validator.getRule("ridiculous"));
  94. }
  95. public void testGetValidCreditCard() {
  96. System.out.println("getValidCreditCard");
  97. Validator instance = ESAPI.validator();
  98. ValidationErrorList errors = new ValidationErrorList();
  99. assertTrue(instance.isValidCreditCard("cctest1", "1234 9876 0000 0008", false));
  100. assertTrue(instance.isValidCreditCard("cctest2", "1234987600000008", false));
  101. assertFalse(instance.isValidCreditCard("cctest3", "12349876000000081", false));
  102. assertFalse(instance.isValidCreditCard("cctest4", "4417 1234 5678 9112", false));
  103. instance.getValidCreditCard("cctest5", "1234 9876 0000 0008", false, errors);
  104. assertEquals(0, errors.size());
  105. instance.getValidCreditCard("cctest6", "1234987600000008", false, errors);
  106. assertEquals(0, errors.size());
  107. instance.getValidCreditCard("cctest7", "12349876000000081", false, errors);
  108. assertEquals(1, errors.size());
  109. instance.getValidCreditCard("cctest8", "4417 1234 5678 9112", false, errors);
  110. assertEquals(2, errors.size());
  111. assertTrue(instance.isValidCreditCard("cctest1", "1234 9876 0000 0008", false, errors));
  112. assertTrue(errors.size()==2);
  113. assertTrue(instance.isValidCreditCard("cctest2", "1234987600000008", false, errors));
  114. assertTrue(errors.size()==2);
  115. assertFalse(instance.isValidCreditCard("cctest3", "12349876000000081", false, errors));
  116. assertTrue(errors.size()==3);
  117. assertFalse(instance.isValidCreditCard("cctest4", "4417 1234 5678 9112", false, errors));
  118. assertTrue(errors.size()==4);
  119. }
  120. public void testGetValidDate() throws Exception {
  121. System.out.println("getValidDate");
  122. Validator instance = ESAPI.validator();
  123. ValidationErrorList errors = new ValidationErrorList();
  124. assertTrue(instance.getValidDate("datetest1", "June 23, 1967", DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US), false) != null);
  125. instance.getValidDate("datetest2", "freakshow", DateFormat.getDateInstance(), false, errors);
  126. assertEquals(1, errors.size());
  127. // TODO: This test case fails due to an apparent bug in SimpleDateFormat
  128. // Note: This seems to be fixed in JDK 6. Will leave it commented out since
  129. // we only require JDK 5. -kww
  130. instance.getValidDate("test", "June 32, 2008", DateFormat.getDateInstance(), false, errors);
  131. // assertEquals( 2, errors.size() );
  132. }
  133. // FIXME: Should probably use SecurityConfigurationWrapper and force
  134. // Validator.AcceptLenientDates to be false.
  135. public void testLenientDate() {
  136. System.out.println("testLenientDate");
  137. boolean acceptLenientDates = ESAPI.securityConfiguration().getLenientDatesAccepted();
  138. if ( acceptLenientDates ) {
  139. assertTrue("Lenient date test skipped because Validator.AcceptLenientDates set to true", true);
  140. return;
  141. }
  142. Date lenientDateTest = null;
  143. try {
  144. // lenientDateTest will be null when Validator.AcceptLenientDates
  145. // is set to false (the default).
  146. Validator instance = ESAPI.validator();
  147. lenientDateTest = instance.getValidDate("datatest3-lenient", "15/2/2009 11:83:00",
  148. DateFormat.getDateInstance(DateFormat.SHORT, Locale.US),
  149. false);
  150. fail("Failed to throw expected ValidationException when Validator.AcceptLenientDates set to false.");
  151. } catch (ValidationException ve) {
  152. assertNull( lenientDateTest );
  153. Throwable cause = ve.getCause();
  154. assertTrue( cause.getClass().getName().equals("java.text.ParseException") );
  155. } catch (Exception e) {
  156. fail("Caught unexpected exception: " + e.getClass().getName() + "; msg: " + e);
  157. }
  158. }
  159. public void testGetValidDirectoryPath() throws Exception {
  160. System.out.println("getValidDirectoryPath");
  161. Validator instance = ESAPI.validator();
  162. ValidationErrorList errors = new ValidationErrorList();
  163. // find a directory that exists
  164. File parent = new File("/");
  165. String path = ESAPI.securityConfiguration().getResourceFile("ESAPI.properties").getParentFile().getCanonicalPath();
  166. instance.getValidDirectoryPath("dirtest1", path, parent, true, errors);
  167. assertEquals(0, errors.size());
  168. instance.getValidDirectoryPath("dirtest2", null, parent, false, errors);
  169. assertEquals(1, errors.size());
  170. instance.getValidDirectoryPath("dirtest3", "ridicul%00ous", parent, false, errors);
  171. assertEquals(2, errors.size());
  172. }
  173. public void testGetValidDouble() {
  174. System.out.println("getValidDouble");
  175. Validator instance = ESAPI.validator();
  176. ValidationErrorList errors = new ValidationErrorList();
  177. instance.getValidDouble("dtest1", "1.0", 0, 20, true, errors);
  178. assertEquals(0, errors.size());
  179. instance.getValidDouble("dtest2", null, 0, 20, true, errors);
  180. assertEquals(0, errors.size());
  181. instance.getValidDouble("dtest3", null, 0, 20, false, errors);
  182. assertEquals(1, errors.size());
  183. instance.getValidDouble("dtest4", "ridiculous", 0, 20, true, errors);
  184. assertEquals(2, errors.size());
  185. instance.getValidDouble("dtest5", "" + (Double.MAX_VALUE), 0, 20, true, errors);
  186. assertEquals(3, errors.size());
  187. instance.getValidDouble("dtest6", "" + (Double.MAX_VALUE + .00001), 0, 20, true, errors);
  188. assertEquals(4, errors.size());
  189. }
  190. public void testGetValidFileContent() {
  191. System.out.println("getValidFileContent");
  192. Validator instance = ESAPI.validator();
  193. ValidationErrorList errors = new ValidationErrorList();
  194. byte[] bytes = null;
  195. try {
  196. bytes = "12345".getBytes(PREFERRED_ENCODING);
  197. }
  198. catch (UnsupportedEncodingException e) {
  199. fail(PREFERRED_ENCODING + " not a supported encoding?!?!!");
  200. }
  201. instance.getValidFileContent("test", bytes, 5, true, errors);
  202. assertEquals(0, errors.size());
  203. instance.getValidFileContent("test", bytes, 4, true, errors);
  204. assertEquals(1, errors.size());
  205. }
  206. public void testGetValidFileName() throws Exception {
  207. System.out.println("getValidFileName");
  208. Validator instance = ESAPI.validator();
  209. ValidationErrorList errors = new ValidationErrorList();
  210. String testName = "aspe%20ct.jar";
  211. assertEquals("Percent encoding is not changed", testName, instance.getValidFileName("test", testName, ESAPI.securityConfiguration().getAllowedFileExtensions(), false, errors));
  212. }
  213. public void testGetValidInput() {
  214. System.out.println("getValidInput");
  215. Validator instance = ESAPI.validator();
  216. ValidationErrorList errors = new ValidationErrorList();
  217. // instance.getValidInput(String, String, String, int, boolean, ValidationErrorList)
  218. }
  219. public void testGetValidInteger() {
  220. System.out.println("getValidInteger");
  221. Validator instance = ESAPI.validator();
  222. ValidationErrorList errors = new ValidationErrorList();
  223. // instance.getValidInteger(String, String, int, int, boolean, ValidationErrorList)
  224. }
  225. public void testGetValidListItem() {
  226. System.out.println("getValidListItem");
  227. Validator instance = ESAPI.validator();
  228. ValidationErrorList errors = new ValidationErrorList();
  229. // instance.getValidListItem(String, String, List, ValidationErrorList)
  230. }
  231. public void testGetValidNumber() {
  232. System.out.println("getValidNumber");
  233. Validator instance = ESAPI.validator();
  234. ValidationErrorList errors = new ValidationErrorList();
  235. // instance.getValidNumber(String, String, long, long, boolean, ValidationErrorList)
  236. }
  237. public void testGetValidRedirectLocation() {
  238. System.out.println("getValidRedirectLocation");
  239. Validator instance = ESAPI.validator();
  240. ValidationErrorList errors = new ValidationErrorList();
  241. // instance.getValidRedirectLocation(String, String, boolean, ValidationErrorList)
  242. }
  243. public void testGetValidSafeHTML() throws Exception {
  244. System.out.println("getValidSafeHTML");
  245. Validator instance = ESAPI.validator();
  246. ValidationErrorList errors = new ValidationErrorList();
  247. // new school test case setup
  248. HTMLValidationRule rule = new HTMLValidationRule("test");
  249. ESAPI.validator().addRule(rule);
  250. assertEquals("Test.", ESAPI.validator().getRule("test").getValid("test", "Test. <script>alert(document.cookie)</script>"));
  251. String test1 = "<b>Jeff</b>";
  252. String result1 = instance.getValidSafeHTML("test", test1, 100, false, errors);
  253. assertEquals(test1, result1);
  254. String test2 = "<a href=\"http://www.aspectsecurity.com\">Aspect Security</a>";
  255. String result2 = instance.getValidSafeHTML("test", test2, 100, false, errors);
  256. assertEquals(test2, result2);
  257. String test3 = "Test. <script>alert(document.cookie)</script>";
  258. assertEquals("Test.", rule.getSafe("test", test3));
  259. assertEquals("Test. &lt;<div>load=alert()</div>", rule.getSafe("test", "Test. <<div on<script></script>load=alert()"));
  260. assertEquals("Test. <div>b</div>", rule.getSafe("test", "Test. <div style={xss:expression(xss)}>b</div>"));
  261. assertEquals("Test.", rule.getSafe("test", "Test. <s%00cript>alert(document.cookie)</script>"));
  262. assertEquals("Test. alert(document.cookie)", rule.getSafe("test", "Test. <s\tcript>alert(document.cookie)</script>"));
  263. assertEquals("Test. alert(document.cookie)", rule.getSafe("test", "Test. <s\tcript>alert(document.cookie)</script>"));
  264. // TODO: ENHANCE waiting for a way to validate text headed for an attribute for scripts
  265. // This would be nice to catch, but just looks like text to AntiSamy
  266. // assertFalse(instance.isValidSafeHTML("test", "\" onload=\"alert(document.cookie)\" "));
  267. // String result4 = instance.getValidSafeHTML("test", test4);
  268. // assertEquals("", result4);
  269. }
  270. public void testIsInvalidFilename() {
  271. System.out.println("testIsInvalidFilename");
  272. Validator instance = ESAPI.validator();
  273. char invalidChars[] = "/\\:*?\"<>|".toCharArray();
  274. for (int i = 0; i < invalidChars.length; i++) {
  275. assertFalse(invalidChars[i] + " is an invalid character for a filename",
  276. instance.isValidFileName("test", "as" + invalidChars[i] + "pect.jar", false));
  277. }
  278. assertFalse("Files must have an extension", instance.isValidFileName("test", "", false));
  279. assertFalse("Files must have a valid extension", instance.isValidFileName("test.invalidExtension", "", false));
  280. assertFalse("Filennames cannot be the empty string", instance.isValidFileName("test", "", false));
  281. }
  282. public void testIsValidDate() {
  283. System.out.println("isValidDate");
  284. Validator instance = ESAPI.validator();
  285. DateFormat format = SimpleDateFormat.getDateInstance();
  286. assertTrue(instance.isValidDate("datetest1", "September 11, 2001", format, true));
  287. assertFalse(instance.isValidDate("datetest2", null, format, false));
  288. assertFalse(instance.isValidDate("datetest3", "", format, false));
  289. ValidationErrorList errors = new ValidationErrorList();
  290. assertTrue(instance.isValidDate("datetest1", "September 11, 2001", format, true, errors));
  291. assertTrue(errors.size()==0);
  292. assertFalse(instance.isValidDate("datetest2", null, format, false, errors));
  293. assertTrue(errors.size()==1);
  294. assertFalse(instance.isValidDate("datetest3", "", format, false, errors));
  295. assertTrue(errors.size()==2);
  296. }
  297. public void testIsValidDirectoryPath() throws IOException {
  298. System.out.println("isValidDirectoryPath");
  299. // get an encoder with a special list of codecs and make a validator out of it
  300. List list = new ArrayList();
  301. list.add("HTMLEntityCodec");
  302. Encoder encoder = new DefaultEncoder(list);
  303. Validator instance = new DefaultValidator(encoder);
  304. boolean isWindows = (System.getProperty("os.name").indexOf("Windows") != -1) ? true : false;
  305. File parent = new File("/");
  306. ValidationErrorList errors = new ValidationErrorList();
  307. if (isWindows) {
  308. String sysRoot = new File(System.getenv("SystemRoot")).getCanonicalPath();
  309. // Windows paths that don't exist and thus should fail
  310. assertFalse(instance.isValidDirectoryPath("test", "c:\\ridiculous", parent, false));
  311. assertFalse(instance.isValidDirectoryPath("test", "c:\\jeff", parent, false));
  312. assertFalse(instance.isValidDirectoryPath("test", "c:\\temp\\..\\etc", parent, false));
  313. // Windows paths
  314. assertTrue(instance.isValidDirectoryPath("test", "C:\\", parent, false)); // Windows root directory
  315. assertTrue(instance.isValidDirectoryPath("test", sysRoot, parent, false)); // Windows always exist directory
  316. assertFalse(instance.isValidDirectoryPath("test", sysRoot + "\\System32\\cmd.exe", parent, false)); // Windows command shell
  317. // Unix specific paths should not pass
  318. assertFalse(instance.isValidDirectoryPath("test", "/tmp", parent, false)); // Unix Temporary directory
  319. assertFalse(instance.isValidDirectoryPath("test", "/bin/sh", parent, false)); // Unix Standard shell
  320. assertFalse(instance.isValidDirectoryPath("test", "/etc/config", parent, false));
  321. // Unix specific paths that should not exist or work
  322. assertFalse(instance.isValidDirectoryPath("test", "/etc/ridiculous", parent, false));
  323. assertFalse(instance.isValidDirectoryPath("test", "/tmp/../etc", parent, false));
  324. assertFalse(instance.isValidDirectoryPath("test1", "c:\\ridiculous", parent, false, errors));
  325. assertTrue(errors.size()==1);
  326. assertFalse(instance.isValidDirectoryPath("test2", "c:\\jeff", parent, false, errors));
  327. assertTrue(errors.size()==2);
  328. assertFalse(instance.isValidDirectoryPath("test3", "c:\\temp\\..\\etc", parent, false, errors));
  329. assertTrue(errors.size()==3);
  330. // Windows paths
  331. assertTrue(instance.isValidDirectoryPath("test4", "C:\\", parent, false, errors)); // Windows root directory
  332. assertTrue(errors.size()==3);
  333. assertTrue(instance.isValidDirectoryPath("test5", sysRoot, parent, false, errors)); // Windows always exist directory
  334. assertTrue(errors.size()==3);
  335. assertFalse(instance.isValidDirectoryPath("test6", sysRoot + "\\System32\\cmd.exe", parent, false, errors)); // Windows command shell
  336. assertTrue(errors.size()==4);
  337. // Unix specific paths should not pass
  338. assertFalse(instance.isValidDirectoryPath("test7", "/tmp", parent, false, errors)); // Unix Temporary directory
  339. assertTrue(errors.size()==5);
  340. assertFalse(instance.isValidDirectoryPath("test8", "/bin/sh", parent, false, errors)); // Unix Standard shell
  341. assertTrue(errors.size()==6);
  342. assertFalse(instance.isValidDirectoryPath("test9", "/etc/config", parent, false, errors));
  343. assertTrue(errors.size()==7);
  344. // Unix specific paths that should not exist or work
  345. assertFalse(instance.isValidDirectoryPath("test10", "/etc/ridiculous", parent, false, errors));
  346. assertTrue(errors.size()==8);
  347. assertFalse(instance.isValidDirectoryPath("test11", "/tmp/../etc", parent, false, errors));
  348. assertTrue(errors.size()==9);
  349. } else {
  350. // Windows paths should fail
  351. assertFalse(instance.isValidDirectoryPath("test", "c:\\ridiculous", parent, false));
  352. assertFalse(instance.isValidDirectoryPath("test", "c:\\temp\\..\\etc", parent, false));
  353. // Standard Windows locations should fail
  354. assertFalse(instance.isValidDirectoryPath("test", "c:\\", parent, false)); // Windows root directory
  355. assertFalse(instance.isValidDirectoryPath("test", "c:\\Windows\\temp", parent, false)); // Windows temporary directory
  356. assertFalse(instance.isValidDirectoryPath("test", "c:\\Windows\\System32\\cmd.exe", parent, false)); // Windows command shell
  357. // Unix specific paths should pass
  358. assertTrue(instance.isValidDirectoryPath("test", "/", parent, false)); // Root directory
  359. assertTrue(instance.isValidDirectoryPath("test", "/bin", parent, false)); // Always exist directory
  360. // Unix specific paths that should not exist or work
  361. assertFalse(instance.isValidDirectoryPath("test", "/bin/sh", parent, false)); // Standard shell, not dir
  362. assertFalse(instance.isValidDirectoryPath("test", "/etc/ridiculous", parent, false));
  363. assertFalse(instance.isValidDirectoryPath("test", "/tmp/../etc", parent, false));
  364. // Windows paths should fail
  365. assertFalse(instance.isValidDirectoryPath("test1", "c:\\ridiculous", parent, false, errors));
  366. assertTrue(errors.size()==1);
  367. assertFalse(instance.isValidDirectoryPath("test2", "c:\\temp\\..\\etc", parent, false, errors));
  368. assertTrue(errors.size()==2);
  369. // Standard Windows locations should fail
  370. assertFalse(instance.isValidDirectoryPath("test3", "c:\\", parent, false, errors)); // Windows root directory
  371. assertTrue(errors.size()==3);
  372. assertFalse(instance.isValidDirectoryPath("test4", "c:\\Windows\\temp", parent, false, errors)); // Windows temporary directory
  373. assertTrue(errors.size()==4);
  374. assertFalse(instance.isValidDirectoryPath("test5", "c:\\Windows\\System32\\cmd.exe", parent, false, errors)); // Windows command shell
  375. assertTrue(errors.size()==5);
  376. // Unix specific paths should pass
  377. assertTrue(instance.isValidDirectoryPath("test6", "/", parent, false, errors)); // Root directory
  378. assertTrue(errors.size()==5);
  379. assertTrue(instance.isValidDirectoryPath("test7", "/bin", parent, false, errors)); // Always exist directory
  380. assertTrue(errors.size()==5);
  381. // Unix specific paths that should not exist or work
  382. assertFalse(instance.isValidDirectoryPath("test8", "/bin/sh", parent, false, errors)); // Standard shell, not dir
  383. assertTrue(errors.size()==6);
  384. assertFalse(instance.isValidDirectoryPath("test9", "/etc/ridiculous", parent, false, errors));
  385. assertTrue(errors.size()==7);
  386. assertFalse(instance.isValidDirectoryPath("test10", "/tmp/../etc", parent, false, errors));
  387. assertTrue(errors.size()==8);
  388. }
  389. }
  390. public void TestIsValidDirectoryPath() {
  391. // isValidDirectoryPath(String, String, boolean)
  392. }
  393. public void testIsValidDouble() {
  394. // isValidDouble(String, String, double, double, boolean)
  395. Validator instance = ESAPI.validator();
  396. ValidationErrorList errors = new ValidationErrorList();
  397. //testing negative range
  398. assertFalse(instance.isValidDouble("test1", "-4", 1, 10, false, errors));
  399. assertTrue(errors.size() == 1);
  400. assertTrue(instance.isValidDouble("test2", "-4", -10, 10, false, errors));
  401. assertTrue(errors.size() == 1);
  402. //testing null value
  403. assertTrue(instance.isValidDouble("test3", null, -10, 10, true, errors));
  404. assertTrue(errors.size() == 1);
  405. assertFalse(instance.isValidDouble("test4", null, -10, 10, false, errors));
  406. assertTrue(errors.size() == 2);
  407. //testing empty string
  408. assertTrue(instance.isValidDouble("test5", "", -10, 10, true, errors));
  409. assertTrue(errors.size() == 2);
  410. assertFalse(instance.isValidDouble("test6", "", -10, 10, false, errors));
  411. assertTrue(errors.size() == 3);
  412. //testing improper range
  413. assertFalse(instance.isValidDouble("test7", "50.0", 10, -10, false, errors));
  414. assertTrue(errors.size() == 4);
  415. //testing non-integers
  416. assertTrue(instance.isValidDouble("test8", "4.3214", -10, 10, true, errors));
  417. assertTrue(errors.size() == 4);
  418. assertTrue(instance.isValidDouble("test9", "-1.65", -10, 10, true, errors));
  419. assertTrue(errors.size() == 4);
  420. //other testing
  421. assertTrue(instance.isValidDouble("test10", "4", 1, 10, false, errors));
  422. assertTrue(errors.size() == 4);
  423. assertTrue(instance.isValidDouble("test11", "400", 1, 10000, false, errors));
  424. assertTrue(errors.size() == 4);
  425. assertTrue(instance.isValidDouble("test12", "400000000", 1, 400000000, false, errors));
  426. assertTrue(errors.size() == 4);
  427. assertFalse(instance.isValidDouble("test13", "4000000000000", 1, 10000, false, errors));
  428. assertTrue(errors.size() == 5);
  429. assertFalse(instance.isValidDouble("test14", "alsdkf", 10, 10000, false, errors));
  430. assertTrue(errors.size() == 6);
  431. assertFalse(instance.isValidDouble("test15", "--10", 10, 10000, false, errors));
  432. assertTrue(errors.size() == 7);
  433. assertFalse(instance.isValidDouble("test16", "14.1414234x", 10, 10000, false, errors));
  434. assertTrue(errors.size() == 8);
  435. assertFalse(instance.isValidDouble("test17", "Infinity", 10, 10000, false, errors));
  436. assertTrue(errors.size() == 9);
  437. assertFalse(instance.isValidDouble("test18", "-Infinity", 10, 10000, false, errors));
  438. assertTrue(errors.size() == 10);
  439. assertFalse(instance.isValidDouble("test19", "NaN", 10, 10000, false, errors));
  440. assertTrue(errors.size() == 11);
  441. assertFalse(instance.isValidDouble("test20", "-NaN", 10, 10000, false, errors));
  442. assertTrue(errors.size() == 12);
  443. assertFalse(instance.isValidDouble("test21", "+NaN", 10, 10000, false, errors));
  444. assertTrue(errors.size() == 13);
  445. assertTrue(instance.isValidDouble("test22", "1e-6", -999999999, 999999999, false, errors));
  446. assertTrue(errors.size() == 13);
  447. assertTrue(instance.isValidDouble("test23", "-1e-6", -999999999, 999999999, false, errors));
  448. assertTrue(errors.size() == 13);
  449. }
  450. public void testIsValidFileContent() {
  451. System.out.println("isValidFileContent");
  452. byte[] content = null;
  453. try {
  454. content = "This is some file content".getBytes(PREFERRED_ENCODING);
  455. }
  456. catch (UnsupportedEncodingException e) {
  457. fail(PREFERRED_ENCODING + " not a supported encoding?!?!!!");
  458. }
  459. Validator instance = ESAPI.validator();
  460. assertTrue(instance.isValidFileContent("test", content, 100, false));
  461. }
  462. public void testIsValidFileName() {
  463. System.out.println("isValidFileName");
  464. Validator instance = ESAPI.validator();
  465. assertTrue("Simple valid filename with a valid extension", instance.isValidFileName("test", "aspect.jar", false));
  466. assertTrue("All valid filename characters are accepted", instance.isValidFileName("test", "!@#$%^&{}[]()_+-=,.~'` abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.jar", false));
  467. assertTrue("Legal filenames that decode to legal filenames are accepted", instance.isValidFileName("test", "aspe%20ct.jar", false));
  468. ValidationErrorList errors = new ValidationErrorList();
  469. assertTrue("Simple valid filename with a valid extension", instance.isValidFileName("test", "aspect.jar", false, errors));
  470. assertTrue("All valid filename characters are accepted", instance.isValidFileName("test", "!@#$%^&{}[]()_+-=,.~'` abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.jar", false, errors));
  471. assertTrue("Legal filenames that decode to legal filenames are accepted", instance.isValidFileName("test", "aspe%20ct.jar", false, errors));
  472. assertTrue(errors.size() == 0);
  473. }
  474. public void testIsValidFileUpload() throws IOException {
  475. System.out.println("isValidFileUpload");
  476. String filepath = new File(System.getProperty("user.dir")).getCanonicalPath();
  477. String filename = "aspect.jar";
  478. File parent = new File("/").getCanonicalFile();
  479. ValidationErrorList errors = new ValidationErrorList();
  480. byte[] content = null;
  481. try {
  482. content = "This is some file content".getBytes(PREFERRED_ENCODING);
  483. }
  484. catch (UnsupportedEncodingException e) {
  485. fail(PREFERRED_ENCODING + " not a supported encoding?!?!!!");
  486. }
  487. Validator instance = ESAPI.validator();
  488. assertTrue(instance.isValidFileUpload("test", filepath, filename, parent, content, 100, false));
  489. assertTrue(instance.isValidFileUpload("test", filepath, filename, parent, content, 100, false, errors));
  490. assertTrue(errors.size() == 0);
  491. filepath = "/ridiculous";
  492. filename = "aspect.jar";
  493. try {
  494. content = "This is some file content".getBytes(PREFERRED_ENCODING);
  495. }
  496. catch (UnsupportedEncodingException e) {
  497. fail(PREFERRED_ENCODING + " not a supported encoding?!?!!!");
  498. }
  499. assertFalse(instance.isValidFileUpload("test", filepath, filename, parent, content, 100, false));
  500. assertFalse(instance.isValidFileUpload("test", filepath, filename, parent, content, 100, false, errors));
  501. assertTrue(errors.size() == 1);
  502. }
  503. public void testIsValidHTTPRequestParameterSet() {
  504. // isValidHTTPRequestParameterSet(String, Set, Set)
  505. }
  506. public void testisValidInput() {
  507. System.out.println("isValidInput");
  508. Validator instance = ESAPI.validator();
  509. assertTrue(instance.isValidInput("test", "jeff.williams@aspectsecurity.com", "Email", 100, false));
  510. assertFalse(instance.isValidInput("test", "jeff.williams@@aspectsecurity.com", "Email", 100, false));
  511. assertFalse(instance.isValidInput("test", "jeff.williams@aspectsecurity", "Email", 100, false));
  512. assertTrue(instance.isValidInput("test", "jeff.wil'liams@aspectsecurity.com", "Email", 100, false));
  513. assertTrue(instance.isValidInput("test", "jeff.wil''liams@aspectsecurity.com", "Email", 100, false));
  514. assertTrue(instance.isValidInput("test", "123.168.100.234", "IPAddress", 100, false));
  515. assertTrue(instance.isValidInput("test", "192.168.1.234", "IPAddress", 100, false));
  516. assertFalse(instance.isValidInput("test", "..168.1.234", "IPAddress", 100, false));
  517. assertFalse(instance.isValidInput("test", "10.x.1.234", "IPAddress", 100, false));
  518. assertTrue(instance.isValidInput("test", "http://www.aspectsecurity.com", "URL", 100, false));
  519. assertFalse(instance.isValidInput("test", "http:///www.aspectsecurity.com", "URL", 100, false));
  520. assertFalse(instance.isValidInput("test", "http://www.aspect security.com", "URL", 100, false));
  521. assertTrue(instance.isValidInput("test", "078-05-1120", "SSN", 100, false));
  522. assertTrue(instance.isValidInput("test", "078 05 1120", "SSN", 100, false));
  523. assertTrue(instance.isValidInput("test", "078051120", "SSN", 100, false));
  524. assertFalse(instance.isValidInput("test", "987-65-4320", "SSN", 100, false));
  525. assertFalse(instance.isValidInput("test", "000-00-0000", "SSN", 100, false));
  526. assertFalse(instance.isValidInput("test", "(555) 555-5555", "SSN", 100, false));
  527. assertFalse(instance.isValidInput("test", "test", "SSN", 100, false));
  528. assertTrue(instance.isValidInput("test", "jeffWILLIAMS123", "HTTPParameterValue", 100, false));
  529. assertTrue(instance.isValidInput("test", "jeff .-/+=@_ WILLIAMS", "HTTPParameterValue", 100, false));
  530. // Removed per Issue 116 - The '*' character is valid as a parameter character
  531. // assertFalse(instance.isValidInput("test", "jeff*WILLIAMS", "HTTPParameterValue", 100, false));
  532. assertFalse(instance.isValidInput("test", "jeff^WILLIAMS", "HTTPParameterValue", 100, false));
  533. assertFalse(instance.isValidInput("test", "jeff\\WILLIAMS", "HTTPParameterValue", 100, false));
  534. assertTrue(instance.isValidInput("test", null, "Email", 100, true));
  535. assertFalse(instance.isValidInput("test", null, "Email", 100, false));
  536. ValidationErrorList errors = new ValidationErrorList();
  537. assertTrue(instance.isValidInput("test1", "jeff.williams@aspectsecurity.com", "Email", 100, false, errors));
  538. assertTrue(errors.size()==0);
  539. assertFalse(instance.isValidInput("test2", "jeff.williams@@aspectsecurity.com", "Email", 100, false, errors));
  540. assertTrue(errors.size()==1);
  541. assertFalse(instance.isValidInput("test3", "jeff.williams@aspectsecurity", "Email", 100, false, errors));
  542. assertTrue(errors.size()==2);
  543. assertTrue(instance.isValidInput("test4", "jeff.wil'liams@aspectsecurity.com", "Email", 100, false, errors));
  544. assertTrue(errors.size()==2);
  545. assertTrue(instance.isValidInput("test5", "jeff.wil''liams@aspectsecurity.com", "Email", 100, false, errors));
  546. assertTrue(errors.size()==2);
  547. assertTrue(instance.isValidInput("test6", "123.168.100.234", "IPAddress", 100, false, errors));
  548. assertTrue(errors.size()==2);
  549. assertTrue(instance.isValidInput("test7", "192.168.1.234", "IPAddress", 100, false, errors));
  550. assertTrue(errors.size()==2);
  551. assertFalse(instance.isValidInput("test8", "..168.1.234", "IPAddress", 100, false, errors));
  552. assertTrue(errors.size()==3);
  553. assertFalse(instance.isValidInput("test9", "10.x.1.234", "IPAddress", 100, false, errors));
  554. assertTrue(errors.size()==4);
  555. assertTrue(instance.isValidInput("test10", "http://www.aspectsecurity.com", "URL", 100, false, errors));
  556. assertTrue(errors.size()==4);
  557. assertFalse(instance.isValidInput("test11", "http:///www.aspectsecurity.com", "URL", 100, false, errors));
  558. assertTrue(errors.size()==5);
  559. assertFalse(instance.isValidInput("test12", "http://www.aspect security.com", "URL", 100, false, errors));
  560. assertTrue(errors.size()==6);
  561. assertTrue(instance.isValidInput("test13", "078-05-1120", "SSN", 100, false, errors));
  562. assertTrue(errors.size()==6);
  563. assertTrue(instance.isValidInput("test14", "078 05 1120", "SSN", 100, false, errors));
  564. assertTrue(errors.size()==6);
  565. assertTrue(instance.isValidInput("test15", "078051120", "SSN", 100, false, errors));
  566. assertTrue(errors.size()==6);
  567. assertFalse(instance.isValidInput("test16", "987-65-4320", "SSN", 100, false, errors));
  568. assertTrue(errors.size()==7);
  569. assertFalse(instance.isValidInput("test17", "000-00-0000", "SSN", 100, false, errors));
  570. assertTrue(errors.size()==8);
  571. assertFalse(instance.isValidInput("test18", "(555) 555-5555", "SSN", 100, false, errors));
  572. assertTrue(errors.size()==9);
  573. assertFalse(instance.isValidInput("test19", "test", "SSN", 100, false, errors));
  574. assertTrue(errors.size()==10);
  575. assertTrue(instance.isValidInput("test20", "jeffWILLIAMS123", "HTTPParameterValue", 100, false, errors));
  576. assertTrue(errors.size()==10);
  577. assertTrue(instance.isValidInput("test21", "jeff .-/+=@_ WILLIAMS", "HTTPParameterValue", 100, false, errors));
  578. assertTrue(errors.size()==10);
  579. // Removed per Issue 116 - The '*' character is valid as a parameter character
  580. // assertFalse(instance.isValidInput("test", "jeff*WILLIAMS", "HTTPParameterValue", 100, false));
  581. assertFalse(instance.isValidInput("test22", "jeff^WILLIAMS", "HTTPParameterValue", 100, false, errors));
  582. assertTrue(errors.size()==11);
  583. assertFalse(instance.isValidInput("test23", "jeff\\WILLIAMS", "HTTPParameterValue", 100, false, errors));
  584. assertTrue(errors.size()==12);
  585. assertTrue(instance.isValidInput("test", null, "Email", 100, true, errors));
  586. assertFalse(instance.isValidInput("test", null, "Email", 100, false, errors));
  587. }
  588. public void testIsValidInteger() {
  589. System.out.println("isValidInteger");
  590. Validator instance = ESAPI.validator();
  591. //testing negative range
  592. assertFalse(instance.isValidInteger("test", "-4", 1, 10, false));
  593. assertTrue(instance.isValidInteger("test", "-4", -10, 10, false));
  594. //testing null value
  595. assertTrue(instance.isValidInteger("test", null, -10, 10, true));
  596. assertFalse(instance.isValidInteger("test", null, -10, 10, false));
  597. //testing empty string
  598. assertTrue(instance.isValidInteger("test", "", -10, 10, true));
  599. assertFalse(instance.isValidInteger("test", "", -10, 10, false));
  600. //testing improper range
  601. assertFalse(instance.isValidInteger("test", "50", 10, -10, false));
  602. //testing non-integers
  603. assertFalse(instance.isValidInteger("test", "4.3214", -10, 10, true));
  604. assertFalse(instance.isValidInteger("test", "-1.65", -10, 10, true));
  605. //other testing
  606. assertTrue(instance.isValidInteger("test", "4", 1, 10, false));
  607. assertTrue(instance.isValidInteger("test", "400", 1, 10000, false));
  608. assertTrue(instance.isValidInteger("test", "400000000", 1, 400000000, false));
  609. assertFalse(instance.isValidInteger("test", "4000000000000", 1, 10000, false));
  610. assertFalse(instance.isValidInteger("test", "alsdkf", 10, 10000, false));
  611. assertFalse(instance.isValidInteger("test", "--10", 10, 10000, false));
  612. assertFalse(instance.isValidInteger("test", "14.1414234x", 10, 10000, false));
  613. assertFalse(instance.isValidInteger("test", "Infinity", 10, 10000, false));
  614. assertFalse(instance.isValidInteger("test", "-Infinity", 10, 10000, false));
  615. assertFalse(instance.isValidInteger("test", "NaN", 10, 10000, false));
  616. assertFalse(instance.isValidInteger("test", "-NaN", 10, 10000, false));
  617. assertFalse(instance.isValidInteger("test", "+NaN", 10, 10000, false));
  618. assertFalse(instance.isValidInteger("test", "1e-6", -999999999, 999999999, false));
  619. assertFalse(instance.isValidInteger("test", "-1e-6", -999999999, 999999999, false));
  620. ValidationErrorList errors = new ValidationErrorList();
  621. //testing negative range
  622. assertFalse(instance.isValidInteger("test1", "-4", 1, 10, false, errors));
  623. assertTrue(errors.size() == 1);
  624. assertTrue(instance.isValidInteger("test2", "-4", -10, 10, false, errors));
  625. assertTrue(errors.size() == 1);
  626. //testing null value
  627. assertTrue(instance.isValidInteger("test3", null, -10, 10, true, errors));
  628. assertTrue(errors.size() == 1);
  629. assertFalse(instance.isValidInteger("test4", null, -10, 10, false, errors));
  630. assertTrue(errors.size() == 2);
  631. //testing empty string
  632. assertTrue(instance.isValidInteger("test5", "", -10, 10, true, errors));
  633. assertTrue(errors.size() == 2);
  634. assertFalse(instance.isValidInteger("test6", "", -10, 10, false, errors));
  635. assertTrue(errors.size() == 3);
  636. //testing improper range
  637. assertFalse(instance.isValidInteger("test7", "50", 10, -10, false, errors));
  638. assertTrue(errors.size() == 4);
  639. //testing non-integers
  640. assertFalse(instance.isValidInteger("test8", "4.3214", -10, 10, true, errors));
  641. assertTrue(errors.size() == 5);
  642. assertFalse(instance.isValidInteger("test9", "-1.65", -10, 10, true, errors));
  643. assertTrue(errors.size() == 6);
  644. //other testing
  645. assertTrue(instance.isValidInteger("test10", "4", 1, 10, false, errors));
  646. assertTrue(errors.size() == 6);
  647. assertTrue(instance.isValidInteger("test11", "400", 1, 10000, false, errors));
  648. assertTrue(errors.size() == 6);
  649. assertTrue(instance.isValidInteger("test12", "400000000", 1, 400000000, false, errors));
  650. assertTrue(errors.size() == 6);
  651. assertFalse(instance.isValidInteger("test13", "4000000000000", 1, 10000, false, errors));
  652. assertTrue(errors.size() == 7);
  653. assertFalse(instance.isValidInteger("test14", "alsdkf", 10, 10000, false, errors));
  654. assertTrue(errors.size() == 8);
  655. assertFalse(instance.isValidInteger("test15", "--10", 10, 10000, false, errors));
  656. assertTrue(errors.size() == 9);
  657. assertFalse(instance.isValidInteger("test16", "14.1414234x", 10, 10000, false, errors));
  658. assertTrue(errors.size() == 10);
  659. assertFalse(instance.isValidInteger("test17", "Infinity", 10, 10000, false, errors));
  660. assertTrue(errors.size() == 11);
  661. assertFalse(instance.isValidInteger("test18", "-Infinity", 10, 10000, false, errors));
  662. assertTrue(errors.size() == 12);
  663. assertFalse(instance.isValidInteger("test19", "NaN", 10, 10000, false, errors));
  664. assertTrue(errors.size() == 13);
  665. assertFalse(instance.isValidInteger("test20", "-NaN", 10, 10000, false, errors));
  666. assertTrue(errors.size() == 14);
  667. assertFalse(instance.isValidInteger("test21", "+NaN", 10, 10000, false, errors));
  668. assertTrue(errors.size() == 15);
  669. assertFalse(instance.isValidInteger("test22", "1e-6", -999999999, 999999999, false, errors));
  670. assertTrue(errors.size() == 16);
  671. assertFalse(instance.isValidInteger("test23", "-1e-6", -999999999, 999999999, false, errors));
  672. assertTrue(errors.size() == 17);
  673. }
  674. public void testIsValidListItem() {
  675. System.out.println("isValidListItem");
  676. Validator instance = ESAPI.validator();
  677. List list = new ArrayList();
  678. list.add("one");
  679. list.add("two");
  680. assertTrue(instance.isValidListItem("test", "one", list));
  681. assertFalse(instance.isValidListItem("test", "three", list));
  682. ValidationErrorList errors = new ValidationErrorList();
  683. assertTrue(instance.isValidListItem("test1", "one", list, errors));
  684. assertTrue(errors.size()==0);
  685. assertFalse(instance.isValidListItem("test2", "three", list, errors));
  686. assertTrue(errors.size()==1);
  687. }
  688. public void testIsValidNumber() {
  689. System.out.println("isValidNumber");
  690. Validator instance = ESAPI.validator();
  691. //testing negative range
  692. assertFalse(instance.isValidNumber("test", "-4", 1, 10, false));
  693. assertTrue(instance.isValidNumber("test", "-4", -10, 10, false));
  694. //testing null value
  695. assertTrue(instance.isValidNumber("test", null, -10, 10, true));
  696. assertFalse(instance.isValidNumber("test", null, -10, 10, false));
  697. //testing empty string
  698. assertTrue(instance.isValidNumber("test", "", -10, 10, true));
  699. assertFalse(instance.isValidNumber("test", "", -10, 10, false));
  700. //testing improper range
  701. assertFalse(instance.isValidNumber("test", "5", 10, -10, false));
  702. //testing non-integers
  703. assertTrue(instance.isValidNumber("test", "4.3214", -10, 10, true));
  704. assertTrue(instance.isValidNumber("test", "-1.65", -10, 10, true));
  705. //other testing
  706. assertTrue(instance.isValidNumber("test", "4", 1, 10, false));
  707. assertTrue(instance.isValidNumber("test", "400", 1, 10000, false));
  708. assertTrue(instance.isValidNumber("test", "400000000", 1, 400000000, false));
  709. assertFalse(instance.isValidNumber("test", "4000000000000", 1, 10000, false));
  710. assertFalse(instance.isValidNumber("test", "alsdkf", 10, 10000, false));
  711. assertFalse(instance.isValidNumber("test", "--10", 10, 10000, false));
  712. assertFalse(instance.isValidNumber("test", "14.1414234x", 10, 10000, false));
  713. assertFalse(instance.isValidNumber("test", "Infinity", 10, 10000, false));
  714. assertFalse(instance.isValidNumber("test", "-Infinity", 10, 10000, false));
  715. assertFalse(instance.isValidNumber("test", "NaN", 10, 10000, false));
  716. assertFalse(instance.isValidNumber("test", "-NaN", 10, 10000, false));
  717. assertFalse(instance.isValidNumber("test", "+NaN", 10, 10000, false));
  718. assertTrue(instance.isValidNumber("test", "1e-6", -999999999, 999999999, false));
  719. assertTrue(instance.isValidNumber("test", "-1e-6", -999999999, 999999999, false));
  720. ValidationErrorList errors = new ValidationErrorList();
  721. //testing negative range
  722. assertFalse(instance.isValidNumber("test1", "-4", 1, 10, false, errors));
  723. assertTrue(errors.size()==1);
  724. assertTrue(instance.isValidNumber("test2", "-4", -10, 10, false, errors));
  725. assertTrue(errors.size()==1);
  726. //testing null value
  727. assertTrue(instance.isValidNumber("test3", null, -10, 10, true, errors));
  728. assertTrue(errors.size()==1);
  729. assertFalse(instance.isValidNumber("test4", null, -10, 10, false, errors));
  730. assertTrue(errors.size()==2);
  731. //testing empty string
  732. assertTrue(instance.isValidNumber("test5", "", -10, 10, true, errors));
  733. assertTrue(errors.size()==2);
  734. assertFalse(instance.isValidNumber("test6", "", -10, 10, false, errors));
  735. assertTrue(errors.size()==3);
  736. //testing improper range
  737. assertFalse(instance.isValidNumber("test7", "5", 10, -10, false, errors));
  738. assertTrue(errors.size()==4);
  739. //testing non-integers
  740. assertTrue(instance.isValidNumber("test8", "4.3214", -10, 10, true, errors));
  741. assertTrue(errors.size()==4);
  742. assertTrue(instance.isValidNumber("test9", "-1.65", -10, 10, true, errors));
  743. assertTrue(errors.size()==4);
  744. //other testing
  745. assertTrue(instance.isValidNumber("test10", "4", 1, 10, false, errors));
  746. assertTrue(errors.size()==4);
  747. assertTrue(instance.isValidNumber("test11", "400", 1, 10000, false, errors));
  748. assertTrue(errors.size()==4);
  749. assertTrue(instance.isValidNumber("test12", "400000000", 1, 400000000, false, errors));
  750. assertTrue(errors.size()==4);
  751. assertFalse(instance.isValidNumber("test13", "4000000000000", 1, 10000, false, errors));
  752. assertTrue(errors.size()==5);
  753. assertFalse(instance.isValidNumber("test14", "alsdkf", 10, 10000, false, errors));
  754. assertTrue(errors.size()==6);
  755. assertFalse(instance.isValidNumber("test15", "--10", 10, 10000, false, errors));
  756. assertTrue(errors.size()==7);
  757. assertFalse(instance.isValidNumber("test16", "14.1414234x", 10, 10000, false, errors));
  758. assertTrue(errors.size()==8);
  759. assertFalse(instance.isValidNumber("test17", "Infinity", 10, 10000, false, errors));
  760. assertTrue(errors.size()==9);
  761. assertFalse(instance.isValidNumber("test18", "-Infinity", 10, 10000, false, errors));
  762. assertTrue(errors.size()==10);
  763. assertFalse(instance.isValidNumber("test19", "NaN", 10, 10000, false, errors));
  764. assertTrue(errors.size()==11);
  765. assertFalse(instance.isValidNumber("test20", "-NaN", 10, 10000, false, errors));
  766. assertTrue(errors.size()==12);
  767. assertFalse(instance.isValidNumber("test21", "+NaN", 10, 10000, false, errors));
  768. assertTrue(errors.size()==13);
  769. assertTrue(instance.isValidNumber("test22", "1e-6", -999999999, 999999999, false, errors));
  770. assertTrue(errors.size()==13);
  771. assertTrue(instance.isValidNumber("test23", "-1e-6", -999999999, 999999999, false, errors));
  772. assertTrue(errors.size()==13);
  773. }
  774. public void testIsValidParameterSet() {
  775. System.out.println("isValidParameterSet");
  776. Set requiredNames = new HashSet();
  777. requiredNames.add("p1");
  778. requiredNames.add("p2");
  779. requiredNames.add("p3");
  780. Set optionalNames = new HashSet();
  781. optionalNames.add("p4");
  782. optionalNames.add("p5");
  783. optionalNames.add("p6");
  784. MockHttpServletRequest request = new MockHttpServletRequest();
  785. MockHttpServletResponse response = new MockHttpServletResponse();
  786. request.addParameter("p1", "value");
  787. request.addParameter("p2", "value");
  788. request.addParameter("p3", "value");
  789. ESAPI.httpUtilities().setCurrentHTTP(request, response);
  790. Validator instance = ESAPI.validator();
  791. ValidationErrorList errors = new ValidationErrorList();
  792. assertTrue(instance.isValidHTTPRequestParameterSet("HTTPParameters", request, requiredNames, optionalNames));
  793. assertTrue(instance.isValidHTTPRequestParameterSet("HTTPParameters", request, requiredNames, optionalNames,errors));
  794. assertTrue(errors.size()==0);
  795. request.addParameter("p4", "value");
  796. request.addParameter("p5", "value");
  797. request.addParameter("p6", "value");
  798. assertTrue(instance.isValidHTTPRequestParameterSet("HTTPParameters", request, requiredNames, optionalNames));
  799. assertTrue(instance.isValidHTTPRequestParameterSet("HTTPParameters", request, requiredNames, optionalNames, errors));
  800. assertTrue(errors.size()==0);
  801. request.removeParameter("p1");
  802. assertFalse(instance.isValidHTTPRequestParameterSet("HTTPParameters", request, requiredNames, optionalNames));
  803. assertFalse(instance.isValidHTTPRequestParameterSet("HTTPParameters", request, requiredNames, optionalNames, errors));
  804. assertTrue(errors.size() ==1);
  805. }
  806. public void testIsValidPrintable() {
  807. System.out.println("isValidPrintable");
  808. Validator instance = ESAPI.validator();
  809. assertTrue(instance.isValidPrintable("name", "abcDEF", 100, false))

Large files files are truncated, but you can click here to view the full file