assertEquals("Test string: " + dumpString(testString), dumpString(testString), dumpString(ourDecoding));
}
}
public void testBadUTF8Decode() throws Exception {
Encoding utf8Encoding = Encoding.getJVMEncoding("UTF-8");
byte[][] badSequences = new byte[][] {
// One-byte illegal sequences
{ (byte)0x80 }, // First byte may not be 10xxxxxx
// Two-byte illegal sequences
{ (byte)0xc0, (byte)0x00 }, // Second byte must be 10xxxxxx
{ (byte)0xc0, (byte)0x80 }, // Can't represent a value < 0x80
// Three-byte illegal sequences
{ (byte)0xe0, (byte)0x00 }, // Second byte must be 10xxxxxx
{ (byte)0xe0, (byte)0x80, (byte)0x00 }, // Third byte must be 10xxxxxx
{ (byte)0xe0, (byte)0x80, (byte)0x80 }, // Can't represent a value < 0x800
{ (byte)0xed, (byte)0xa0, (byte)0x80 }, // Not allowed to encode the range d800..dfff
// Four-byte illegal sequences
{ (byte)0xf0, (byte)0x00 }, // Second byte must be 10xxxxxx
{ (byte)0xf0, (byte)0x80, (byte)0x00 }, // Third byte must be 10xxxxxx
{ (byte)0xf0, (byte)0x80, (byte)0x80, (byte)0x00 }, // Fourth byte must be 10xxxxxx
{ (byte)0xf0, (byte)0x80, (byte)0x80, (byte)0x80 }, // Can't represent a value < 0x10000
// Five-byte illegal sequences
{ (byte)0xf8 }, // Can't have a five-byte sequence.
// Six-byte illegal sequences
{ (byte)0xfc }, // Can't have a six-byte sequence.
// Seven-byte illegal sequences
{ (byte)0xfe }, // Can't have a seven-byte sequence.
// Eigth-byte illegal sequences
{ (byte)0xff }, // Can't have an eight-byte sequence.
};
byte[] paddedSequence = new byte[32];
for (int i = 0; i < badSequences.length; ++i) {
byte[] sequence = badSequences[i];
try {
String str = utf8Encoding.decode(sequence, 0, sequence.length);
fail("Expected an IOException on sequence " + i + ", but decoded to <" + str + ">");
} catch (IOException ioe) {
// Expected exception.
}
// Try it with padding.
Arrays.fill(paddedSequence, (byte)0);
System.arraycopy(sequence, 0, paddedSequence, 0, sequence.length);
try {
String str = utf8Encoding.decode(paddedSequence, 0, paddedSequence.length);
fail("Expected an IOException on sequence " + i + ", but decoded to <" + str + ">");
} catch (IOException ioe) {
// Expected exception.
}
}