|
14 | 14 | import org.owasp.html.PolicyFactory; |
15 | 15 | import org.owasp.html.Sanitizers; |
16 | 16 |
|
| 17 | +import java.io.ByteArrayInputStream; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.nio.charset.Charset; |
17 | 21 | import java.security.DigestException; |
18 | 22 | import java.security.NoSuchAlgorithmException; |
19 | 23 |
|
@@ -170,4 +174,32 @@ public void testHash() throws DigestException, NoSuchAlgorithmException { |
170 | 174 | assertEquals(StringUtils.hash(originalString), StringUtils.hash(originalString, length)); |
171 | 175 | assertEquals(StringUtils.hash(originalString), hashedString); |
172 | 176 | } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testStringFromInputStream() throws IOException { |
| 180 | + Charset utf8 = Charset.forName("UTF-8"); |
| 181 | + Charset utf16 = Charset.forName("UTF-16"); |
| 182 | + Charset utf16be = Charset.forName("UTF-16BE"); |
| 183 | + Charset utf16le = Charset.forName("UTF-16LE"); |
| 184 | + |
| 185 | + String text = "hello"; |
| 186 | + |
| 187 | + assertEquals(text, testStringFromInputStream(text, utf8)); |
| 188 | + assertEquals(text, testStringFromInputStream(text, utf16)); |
| 189 | + assertEquals(text, testStringFromInputStream(text, utf16be)); |
| 190 | + assertEquals(text, testStringFromInputStream(text, utf16le)); |
| 191 | + |
| 192 | + // BOM should be removed (UTF-8) |
| 193 | + byte[] UTF8BOM = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }; |
| 194 | + String textUTF8BOM = new String(UTF8BOM) + text; |
| 195 | + assertEquals(text, testStringFromInputStream(textUTF8BOM, utf8)); |
| 196 | + } |
| 197 | + |
| 198 | + private String testStringFromInputStream(String text, Charset charset) throws IOException { |
| 199 | + return StringUtils.stringFromInputStream(stringToInputStream(text, charset), charset); |
| 200 | + } |
| 201 | + |
| 202 | + private InputStream stringToInputStream(String str, Charset charset) { |
| 203 | + return new ByteArrayInputStream(str.getBytes(charset)); |
| 204 | + } |
173 | 205 | } |
0 commit comments