-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathStringUtilsTest.java
More file actions
214 lines (174 loc) · 6.87 KB
/
Copy pathStringUtilsTest.java
File metadata and controls
214 lines (174 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package communitycommons;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.owasp.html.PolicyFactory;
import org.owasp.html.Sanitizers;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.DigestException;
import java.security.NoSuchAlgorithmException;
/**
*
* @author res
*/
public class StringUtilsTest {
private static final int PASSWORD_LENGTH = 12;
public StringUtilsTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Tests of HTMLToPlainText method, of class StringUtils.
*/
@Test
public void testHTMLToPlainText() throws Exception {
System.out.println("HTMLToPlainText");
String html = "<html>Hello world</html>";
String expResult = "Hello world";
String result = StringUtils.HTMLToPlainText(html);
assertEquals(expResult, result);
}
@Test
public void testHTMLToPlainText_line_break() throws Exception {
System.out.println("HTMLToPlainText");
String html = "<html>Hello world<h1>header</h1><br><p>paragraph</p><hr><p>paragraph2</html>";
String expResult = "Hello worldheader\r\n\r\nparagraph\r\n\r\nparagraph2\r\n";
String result = StringUtils.HTMLToPlainText(html);
assertEquals(expResult, result);
}
/**
* Test of sanitizeHTML method, of class StringUtils.
*/
@Test
public void testSanitizeHTML_String_PolicyFactory() {
System.out.println("sanitizeHTML");
String html = "<a onclick=\"alert(1)\">link</a>";
PolicyFactory policyFactory = Sanitizers.FORMATTING;
String expResult = "link";
String result = StringUtils.sanitizeHTML(html, policyFactory);
assertEquals(expResult, result);
}
@Test
public void testRandomStrongPassword_WithDigitsOnly() {
String password = StringUtils.randomStrongPassword(PASSWORD_LENGTH, PASSWORD_LENGTH, 0, PASSWORD_LENGTH, 0);
assertEquals(password.length(), PASSWORD_LENGTH);
assertTrue(password.matches("^\\d+$"));
}
@Test
public void testRandomStrongPassword_WithUppercaseAlphaOnly() {
String password = StringUtils.randomStrongPassword(PASSWORD_LENGTH, PASSWORD_LENGTH, PASSWORD_LENGTH, 0, 0);
assertEquals(PASSWORD_LENGTH, password.length());
assertTrue(password.matches("^[A-Z]+$"));
}
@Test
public void testRandomStrongPassword_WithLowercaseAlphaOnly() {
String password = StringUtils.randomStrongPassword(PASSWORD_LENGTH, PASSWORD_LENGTH, 0, PASSWORD_LENGTH, 0, 0);
assertEquals(PASSWORD_LENGTH, password.length());
assertTrue(password.matches("^[a-z]+$"));
}
@Test
public void testRandomStrongPassword_Combined() {
var passwordLength = 12;
var password = StringUtils.randomStrongPassword(passwordLength, passwordLength, 2, 2, 2, 2);
assertEquals(passwordLength, password.length());
assertTrue(password.chars().filter(c -> Character.isUpperCase(c)).count() >= 2);
assertTrue(password.chars().filter(c -> Character.isLowerCase(c)).count() >= 2);
assertTrue(password.chars().filter(c -> Character.isDigit(c)).count() >= 2);
assertTrue(password.chars().filter(c -> StringUtils.SPECIAL.indexOf(c) >= 0).count() == 2);
}
@Test
public void testRandomStrongPassword_WithNoSpecifiedCharacters() {
String password = StringUtils.randomStrongPassword(PASSWORD_LENGTH, PASSWORD_LENGTH, 0, 0, 0);
assertEquals(PASSWORD_LENGTH, password.length());
assertTrue(password.matches("^[A-Za-z0-9]+$"));
}
@Test
public void testRandomStrongPassword_WithVaryingLength() {
final int minLength = 10;
final int maxLength = 11;
final int runs = 50;
boolean minimumHit = false;
boolean maximumHit = false;
// Since we are dealing with randomness, there is a probability of this test failing.
// With 50 runs, the chance of this test failing is about 1 in 562.949.953.421.312
for (int run = 0; run < runs; run++) {
String password = StringUtils.randomStrongPassword(minLength, maxLength, 0, 0, 0);
assertTrue(password.length() >= minLength);
assertTrue(password.length() <= maxLength);
if (password.length() == minLength) {
minimumHit = true;
} else {
maximumHit = true;
}
}
assertTrue(minimumHit);
assertTrue(maximumHit);
}
@Test
public void testRandomStrongPassword_WithMinLengthLargerThanMaxLength() {
final int minLength = 12;
final int maxLength = 10;
assertThrows(IllegalArgumentException.class, () -> StringUtils.randomStrongPassword(minLength, maxLength, 0, 0, 0));
}
@Test
public void testRandomStrongPassword_WithNumberOfSpecifiedCharactersGreaterThanLength() {
final int upperCount = 10;
final int digitCount = 10;
final int specialCount = 10;
// Password length is 1 less than the total number of minimum specified characters.
final int length = upperCount + digitCount + specialCount - 1;
assertThrows(IllegalArgumentException.class, () ->
StringUtils.randomStrongPassword(length, length, upperCount, digitCount, specialCount));
}
@Test
public void testHash() throws DigestException, NoSuchAlgorithmException {
final int length = 32;
final String originalString = "original string";
final String hashedString = "18760223948747fb081582fdef27e3d216d0e6bc67734eb080bb1c0c4b22d01b";
assertEquals(StringUtils.hash(originalString), StringUtils.hash(originalString, length));
assertEquals(StringUtils.hash(originalString), hashedString);
}
@Test
public void testStringFromInputStream() throws IOException {
Charset utf8 = Charset.forName("UTF-8");
Charset utf16 = Charset.forName("UTF-16");
Charset utf16be = Charset.forName("UTF-16BE");
Charset utf16le = Charset.forName("UTF-16LE");
String text = "hello";
assertEquals(text, testStringFromInputStream(text, utf8));
assertEquals(text, testStringFromInputStream(text, utf16));
assertEquals(text, testStringFromInputStream(text, utf16be));
assertEquals(text, testStringFromInputStream(text, utf16le));
// BOM should be removed (UTF-8)
byte[] UTF8BOM = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
String textUTF8BOM = new String(UTF8BOM) + text;
assertEquals(text, testStringFromInputStream(textUTF8BOM, utf8));
}
private String testStringFromInputStream(String text, Charset charset) throws IOException {
return StringUtils.stringFromInputStream(stringToInputStream(text, charset), charset);
}
private InputStream stringToInputStream(String str, Charset charset) {
return new ByteArrayInputStream(str.getBytes(charset));
}
}