0
|
1 package org.msgpack.unpacker;
|
|
2
|
|
3 import static org.junit.Assert.assertArrayEquals;
|
|
4 import static org.junit.Assert.assertEquals;
|
|
5 import static org.junit.Assert.fail;
|
|
6
|
|
7 import java.io.ByteArrayInputStream;
|
|
8 import java.io.ByteArrayOutputStream;
|
|
9 import java.nio.charset.CharacterCodingException;
|
|
10
|
|
11 import org.junit.Test;
|
|
12 import org.msgpack.MessagePack;
|
|
13 import org.msgpack.MessageTypeException;
|
|
14 import org.msgpack.packer.Packer;
|
|
15 import org.msgpack.packer.BufferPacker;
|
|
16 import org.msgpack.unpacker.Unpacker;
|
|
17 import org.msgpack.unpacker.Converter;
|
|
18 import org.msgpack.type.RawValue;
|
|
19 import org.msgpack.type.ValueFactory;
|
|
20 import org.msgpack.util.json.JSON;
|
|
21
|
|
22 public class TestMalformedEncoding {
|
|
23 private byte[][] malforms = new byte[][] { { (byte) 0xc0, (byte) 0xaf }, // '/'
|
|
24 // in
|
|
25 // 2
|
|
26 // bytes
|
|
27 { (byte) 0xe0, (byte) 0x80, (byte) 0xaf } // '/' in 3 bytes
|
|
28 };
|
|
29
|
|
30 @Test
|
|
31 public void testRawValueGetString() throws Exception {
|
|
32 for (byte[] malform : malforms) {
|
|
33 RawValue r = ValueFactory.createRawValue(malform);
|
|
34 try {
|
|
35 r.getString();
|
|
36 fail("no exception");
|
|
37 } catch (MessageTypeException expected) {
|
|
38 }
|
|
39 byte[] a = r.getByteArray();
|
|
40 assertArrayEquals(malform, a);
|
|
41 }
|
|
42 }
|
|
43
|
|
44 @Test
|
|
45 public void testBufferUnpackerUnpackString() throws Exception {
|
|
46 for (byte[] malform : malforms) {
|
|
47 MessagePack msgpack = new MessagePack();
|
|
48 BufferPacker pk = msgpack.createBufferPacker();
|
|
49 pk.write(malform);
|
|
50 byte[] b = pk.toByteArray();
|
|
51 Unpacker u = msgpack.createBufferUnpacker(b);
|
|
52 try {
|
|
53 u.readString();
|
|
54 fail("no exception");
|
|
55 } catch (MessageTypeException expected) {
|
|
56 }
|
|
57 byte[] a = u.readByteArray();
|
|
58 assertArrayEquals(malform, a);
|
|
59 }
|
|
60 }
|
|
61
|
|
62 @Test
|
|
63 public void testUnpackerUnpackString() throws Exception {
|
|
64 for (byte[] malform : malforms) {
|
|
65 MessagePack msgpack = new MessagePack();
|
|
66 BufferPacker pk = msgpack.createBufferPacker();
|
|
67 pk.write(malform);
|
|
68 byte[] b = pk.toByteArray();
|
|
69 Unpacker u = msgpack.createUnpacker(new ByteArrayInputStream(b));
|
|
70 try {
|
|
71 u.readString();
|
|
72 fail("no exception");
|
|
73 } catch (MessageTypeException expected) {
|
|
74 }
|
|
75 byte[] a = u.readByteArray();
|
|
76 assertArrayEquals(malform, a);
|
|
77 }
|
|
78 }
|
|
79
|
|
80 @Test
|
|
81 public void testConverterUnpackString() throws Exception {
|
|
82 for (byte[] malform : malforms) {
|
|
83 MessagePack msgpack = new MessagePack();
|
|
84 RawValue r = ValueFactory.createRawValue(malform);
|
|
85 Converter u = new Converter(msgpack, r);
|
|
86 try {
|
|
87 u.readString();
|
|
88 fail("no exception");
|
|
89 } catch (MessageTypeException expected) {
|
|
90 }
|
|
91 byte[] a = u.readByteArray();
|
|
92 assertArrayEquals(malform, a);
|
|
93 }
|
|
94 }
|
|
95
|
|
96 @Test
|
|
97 public void testJSONPackerWriteString() throws Exception {
|
|
98 for (byte[] malform : malforms) {
|
|
99 JSON json = new JSON();
|
|
100 Packer pk = json.createPacker(new ByteArrayOutputStream());
|
|
101 try {
|
|
102 pk.write(malform);
|
|
103 fail("no exception");
|
|
104 } catch (CharacterCodingException expected) {
|
|
105 }
|
|
106 }
|
|
107 }
|
|
108
|
|
109 @Test
|
|
110 public void testJSONBufferPackerWriteString() throws Exception {
|
|
111 for (byte[] malform : malforms) {
|
|
112 JSON json = new JSON();
|
|
113 Packer pk = json.createBufferPacker();
|
|
114 try {
|
|
115 pk.write(malform);
|
|
116 fail("no exception");
|
|
117 } catch (CharacterCodingException expected) {
|
|
118 }
|
|
119 }
|
|
120 }
|
|
121
|
|
122 @Test
|
|
123 public void testValueToString() throws Exception {
|
|
124 for (byte[] malform : malforms) {
|
|
125 RawValue r = ValueFactory.createRawValue(malform);
|
|
126 String str = r.toString();
|
|
127 // malformed bytes will be ignored
|
|
128 assertEquals("\"\"", str);
|
|
129 }
|
|
130 }
|
|
131 }
|