handle utf8 decode errors

This commit is contained in:
Naoyuki Kanezawa
2014-06-30 10:09:41 +09:00
parent 3e64ad64f9
commit b1e43ba1b2
5 changed files with 65 additions and 16 deletions

View File

@@ -169,6 +169,13 @@ public class ParserTest {
assertThat(p.data, is(ERROR_DATA));
}
@Test
public void decodeInvalidUTF8() {
Packet<String> p = decodePacket("4\uffff");
assertThat(p.type, is(Packet.ERROR));
assertThat(p.data, is(ERROR_DATA));
}
@Test
public void encodePayloads() {
encodePayload(new Packet[]{new Packet(Packet.PING), new Packet(Packet.PONG)}, new EncodeCallback<byte[]>() {
@@ -313,6 +320,20 @@ public class ParserTest {
});
}
@Test
public void decodePayloadInvalidUTF8() {
decodePayload("2:4\uffff", new DecodePayloadCallback<String>() {
@Override
public boolean call(Packet<String> packet, int index, int total) {
boolean isLast = index + 1 == total;
assertThat(packet.type, is(Packet.ERROR));
assertThat(packet.data, is(ERROR_DATA));
assertThat(isLast, is(true));
return true;
}
});
}
@Test
public void encodeBinaryMessage() {
final byte[] data = new byte[5];

View File

@@ -55,23 +55,23 @@ public class UTF8Test {
public ExpectedException exception = ExpectedException.none();
@Test
public void encodeAndDecode() {
public void encodeAndDecode() throws UTF8Exception {
for (Data data : DATA) {
String reason = data.description != null? data.description : "U+" + Integer.toHexString(data.codePoint).toUpperCase();
assertThat("Encoding: " + reason, data.encoded, is(UTF8.encode(data.decoded)));
assertThat("Decoding: " + reason, data.decoded, is(UTF8.decode(data.encoded)));
}
exception.expect(RuntimeException.class);
exception.expect(UTF8Exception.class);
UTF8.decode("\uFFFF");
exception.expect(RuntimeException.class);
exception.expect(UTF8Exception.class);
UTF8.decode("\u00E9\u0000\u0000");
exception.expect(RuntimeException.class);
exception.expect(UTF8Exception.class);
UTF8.decode("\u00C2\uFFFF");
exception.expect(RuntimeException.class);
exception.expect(UTF8Exception.class);
UTF8.decode("\u00F0\u009D");
}