Skip to content

Commit 3ca93c5

Browse files

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

src/main/java/org/owasp/esapi/StringUtilities.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public static String stripControls( String input ) {
6161
public static char[] union(char[]... list) {
6262
StringBuilder sb = new StringBuilder();
6363

64-
for (char[] characters : list) {
65-
for (int i = 0; i < list.length; i++) {
66-
if (!contains(sb, characters[i]))
67-
sb.append(list[i]);
68-
}
69-
}
64+
for (char[] characters : list) {
65+
for ( char c : characters ) {
66+
if ( !contains( sb, c ) )
67+
sb.append( c );
68+
}
69+
}
7070

7171
char[] toReturn = new char[sb.length()];
7272
sb.getChars(0, sb.length(), toReturn, 0);

src/test/java/org/owasp/esapi/StringUtilitiesTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.owasp.esapi;
22

3+
import java.util.Arrays;
4+
35
import junit.framework.Test;
46
import junit.framework.TestCase;
57
import junit.framework.TestSuite;
@@ -42,7 +44,22 @@ public void testGetLevenshteinDistance() {
4244
assertTrue( ex.getClass().getName().equals( IllegalArgumentException.class.getName() ));
4345
}
4446
}
47+
48+
/** Test the union() method. */
49+
public void testUnion() {
50+
char[] a1 = { 'a', 'b', 'c' };
51+
char[] a2 = { 'c', 'd', 'e' };
52+
char[] union = StringUtilities.union(a1, a2);
53+
assertTrue( Arrays.equals( union, new char[] {'a','b','c','d','e' } ) );
54+
}
4555

56+
/** Test the contains() method. */
57+
public void contains() {
58+
StringBuilder sb = new StringBuilder( "abc" );
59+
assertTrue( StringUtilities.contains(sb, 'b') );
60+
assertFalse( StringUtilities.contains(sb, 'x') );
61+
}
62+
4663
/** Test the notNullOrEmpty() method. */
4764
public void testNotNullOrEmpty() {
4865
String str = "A string";

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.owasp.esapi.reference;
1717

18+
import java.io.FileWriter;
19+
import java.io.IOException;
1820
import java.util.ArrayList;
1921

2022
import junit.framework.Test;
@@ -76,16 +78,28 @@ public static Test suite() {
7678
public void testGetRandomString() {
7779
System.out.println("getRandomString");
7880
int length = 20;
81+
int trials = 1000;
7982
Randomizer instance = ESAPI.randomizer();
80-
for ( int i = 0; i < 100; i++ ) {
83+
int[] counts = new int[128];
84+
for ( int i = 0; i < 1000; i++ ) {
8185
String result = instance.getRandomString(length, EncoderConstants.CHAR_ALPHANUMERICS );
8286
for ( int j=0;j<result.length();j++ ) {
83-
if ( !Codec.containsCharacter( result.charAt(j), EncoderConstants.CHAR_ALPHANUMERICS) ) {
84-
fail();
85-
}
87+
char c = result.charAt(j);
88+
counts[c]++;
8689
}
8790
assertEquals(length, result.length());
8891
}
92+
93+
// Simple check to see if the overall character counts are within 10% of each other
94+
int min=Integer.MAX_VALUE;
95+
int max=0;
96+
for ( int i = 0; i < 128; i++ ) {
97+
if ( counts[i] > max ) { max = counts[i]; }
98+
if ( counts[i] > 0 && counts[i] < min ) { min = counts[i]; }
99+
if ( max - min > trials/10 ) {
100+
fail( "getRandomString randomness counts are off" );
101+
}
102+
}
89103
}
90104

91105
/**
@@ -140,5 +154,24 @@ public void testGetRandomGUID() throws EncryptionException {
140154
}
141155
}
142156

157+
158+
/**
159+
* Run this class to generate a file named "tokens.txt" with 20,000 random 20 character ALPHANUMERIC tokens.
160+
* Use Burp Pro sequencer to load this file and run a series of randomness tests.
161+
*
162+
* NOTE: be careful not to include any CRLF characters (10 or 13 ASCII) because they'll create new tokens
163+
* Check to be sure your analysis tool loads exactly 20,000 tokens of 20 characters each.
164+
*/
165+
166+
public static void main(String[] args) throws IOException {
167+
FileWriter fw = new FileWriter("tokens.txt");
168+
for (int i = 0; i < 20000; i++) {
169+
String token = ESAPI.randomizer().getRandomString(20, EncoderConstants.CHAR_ALPHANUMERICS);
170+
fw.write(token + "\n");
171+
}
172+
fw.close();
173+
}
174+
175+
143176

144177
}

0 commit comments

Comments
 (0)