use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.
the class TelnetCodecTest method testDecode_WithExitByte.
private void testDecode_WithExitByte(byte[] exitbytes, boolean isChannelClose) throws IOException {
// init channel
Channel channel = getServerSideChannel(url);
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(exitbytes);
// decode
codec.decode(channel, buffer);
Assertions.assertEquals(isChannelClose, channel.isClosed());
}
use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.
the class ExchangeCodecTest method testMessageLengthGreaterThanMessageActualLength.
@Test
public void testMessageLengthGreaterThanMessageActualLength() throws Exception {
Channel channel = getCliendSideChannel(url);
Request request = new Request(1L);
request.setVersion(Version.getProtocolVersion());
Date date = new Date();
request.setData(date);
ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
codec.encode(channel, encodeBuffer, request);
byte[] bytes = new byte[encodeBuffer.writerIndex()];
encodeBuffer.readBytes(bytes);
int len = Bytes.bytes2int(bytes, 12);
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
out.write(bytes, 0, 12);
/*
* The fill length can not be less than 256, because by default, hessian reads 256 bytes from the stream each time.
* Refer Hessian2Input.readBuffer for more details
*/
int padding = 512;
out.write(Bytes.int2bytes(len + padding));
out.write(bytes, 16, bytes.length - 16);
for (int i = 0; i < padding; i++) {
out.write(1);
}
out.write(bytes);
/* request|1111...|request */
ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(out.toByteArray());
Request decodedRequest = (Request) codec.decode(channel, decodeBuffer);
Assertions.assertEquals(date, decodedRequest.getData());
Assertions.assertEquals(bytes.length + padding, decodeBuffer.readerIndex());
decodedRequest = (Request) codec.decode(channel, decodeBuffer);
Assertions.assertEquals(date, decodedRequest.getData());
}
use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.
the class ExchangeCodecTest method test_Decode_Check_Payload.
@Test
public void test_Decode_Check_Payload() throws IOException {
byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
byte[] request = assemblyDataProtocol(header);
try {
Channel channel = getServerSideChannel(url);
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(request);
Object obj = codec.decode(channel, buffer);
Assertions.assertTrue(obj instanceof Response);
Assertions.assertTrue(((Response) obj).getErrorMessage().startsWith("Data length too large: " + Bytes.bytes2int(new byte[] { 1, 1, 1, 1 })));
} catch (IOException expected) {
Assertions.assertTrue(expected.getMessage().startsWith("Data length too large: " + Bytes.bytes2int(new byte[] { 1, 1, 1, 1 })));
}
}
use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.
the class ExchangeCodecTest method test_Decode_Error_Length.
@Test
public void test_Decode_Error_Length() throws IOException {
byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 0x02, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Person person = new Person();
byte[] request = getRequestBytes(person, header);
Channel channel = getServerSideChannel(url);
byte[] baddata = new byte[] { 1, 2 };
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(join(request, baddata));
Response obj = (Response) codec.decode(channel, buffer);
Assertions.assertEquals(person, obj.getResult());
// only decode necessary bytes
Assertions.assertEquals(request.length, buffer.readerIndex());
}
use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.
the class ExchangeCodecTest method test_Decode_MigicCodec_Contain_ExchangeHeader.
@Test
public void test_Decode_MigicCodec_Contain_ExchangeHeader() throws IOException {
byte[] header = new byte[] { 0, 0, MAGIC_HIGH, MAGIC_LOW, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Channel channel = getServerSideChannel(url);
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(header);
Object obj = codec.decode(channel, buffer);
Assertions.assertEquals(TelnetCodec.DecodeResult.NEED_MORE_INPUT, obj);
// If the telnet data and request data are in the same data packet, we should guarantee that the receipt of request data won't be affected by the factor that telnet does not have an end characters.
Assertions.assertEquals(2, buffer.readerIndex());
}
Aggregations