Skip to content

Commit

Permalink
Add failing test to detect BOM for Utf16Le
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneBab committed Nov 11, 2024
1 parent 85013fb commit 748fdae
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions test/freenet/client/filter/ContentFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,24 +380,37 @@ public void testEvilCharset() throws IOException {
}
}


@Test
public void charsetDetectionUsesUTF8DefaultForEmptyText() throws IOException {
String s = "";
byte[] buf = s.getBytes(StandardCharsets.UTF_8);
public void byteOrderMarkForUtf8IsDetectedCorrectly() throws IOException {
byte[] buf = { (byte) 0xef, (byte) 0xbb, (byte) 0xbf, 0x40 };
ArrayBucket out = new ArrayBucket();
FilterStatus fo = ContentFilter.filter(new ArrayBucket(buf).getInputStream(), out.getOutputStream(), "text/plain", null, null, null);
assertTrue("utf-8".equals(fo.charset));
}

@Test
public void charsetDetectionUsesBomForText() throws IOException {
String s = new String(CSSReadFilter.utf16be);
byte[] buf = s.getBytes(StandardCharsets.UTF_8);
public void byteOrderMarkForUtf16BeIsDetectedCorrectly() throws IOException {
byte[] buf = { (byte) 0xfe, (byte) 0xff, 0x00, 0x40 };
ArrayBucket out = new ArrayBucket();
FilterStatus fo = ContentFilter.filter(new ArrayBucket(buf).getInputStream(), out.getOutputStream(), "text/plain", null, null, null);
assertTrue("UTF-16BE".equals(fo.charset));
}

@Test
public void byteOrderMarkForUtf16LeIsDetectedCorrectly() throws IOException {
byte[] buf = { (byte) 0xff, (byte) 0xfe, 0x40, 0x00 };
ArrayBucket out = new ArrayBucket();
FilterStatus fo = ContentFilter.filter(
new ArrayBucket(buf).getInputStream(),
out.getOutputStream(),
"text/plain",
null,
null,
null);
assertTrue("UTF-16LE".equals(fo.charset));
}

public static String htmlFilter(String data) throws Exception {
if (data.startsWith("<html")) return htmlFilter(data, false);
if (data.startsWith("<?")) return htmlFilter(data, false);
Expand Down

0 comments on commit 748fdae

Please sign in to comment.