Add testcases for all marshal encoder and decoder. See #324
This commit is contained in:
parent
bc308310e0
commit
98a8bd25bb
|
@ -37,19 +37,19 @@ import org.jboss.netty.handler.codec.replay.VoidEnum;
|
|||
*/
|
||||
public class CompatibleMarshallingDecoder extends ReplayingDecoder<VoidEnum> {
|
||||
protected final UnmarshallerProvider provider;
|
||||
protected final long maxObjectSize;
|
||||
protected final int maxObjectSize;
|
||||
|
||||
/**
|
||||
* Create a new instance of {@link CompatibleMarshallingDecoder}.
|
||||
*
|
||||
* @param provider the {@link UnmarshallerProvider} which is used to obtain the {@link Unmarshaller} for the {@link Channel}
|
||||
* @param maxObjectSize the maximal size (in bytes) of the {@link Object} to unmarshal. Once the size is exceeded
|
||||
* the {@link Channel} will get closed. Use a a maxObjectSize of <= 0 to disable this.
|
||||
* the {@link Channel} will get closed. Use a a maxObjectSize of {@link Integer#MAX_VALUE} to disable this.
|
||||
* You should only do this if you are sure that the received Objects will never be big and the
|
||||
* sending side are trusted, as this opens the possibility for a DOS-Attack due an {@link OutOfMemoryError}.
|
||||
*
|
||||
*/
|
||||
public CompatibleMarshallingDecoder(UnmarshallerProvider provider, long maxObjectSize) {
|
||||
public CompatibleMarshallingDecoder(UnmarshallerProvider provider, int maxObjectSize) {
|
||||
this.provider = provider;
|
||||
this.maxObjectSize = maxObjectSize;
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ public class CompatibleMarshallingDecoder extends ReplayingDecoder<VoidEnum> {
|
|||
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, VoidEnum state) throws Exception {
|
||||
Unmarshaller unmarshaller = provider.getUnmarshaller(channel);
|
||||
ByteInput input = new ChannelBufferByteInput(buffer);
|
||||
if (maxObjectSize > 0) {
|
||||
if (maxObjectSize != Integer.MAX_VALUE) {
|
||||
input = new LimitingByteInput(input, maxObjectSize);
|
||||
}
|
||||
try {
|
||||
|
|
|
@ -69,7 +69,7 @@ public class MarshallingDecoder extends LengthFieldBasedFrameDecoder {
|
|||
}
|
||||
|
||||
Unmarshaller unmarshaller = provider.getUnmarshaller(channel);
|
||||
ByteInput input = new ChannelBufferByteInput(buffer);
|
||||
ByteInput input = new ChannelBufferByteInput(frame);
|
||||
|
||||
try {
|
||||
unmarshaller.start(input);
|
||||
|
|
|
@ -76,8 +76,8 @@ public class MarshallingEncoder extends OneToOneEncoder {
|
|||
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
|
||||
Marshaller marshaller = provider.getMarshaller(channel);
|
||||
ChannelBufferByteOutput output = new ChannelBufferByteOutput(ctx.getChannel().getConfig().getBufferFactory(), estimatedLength);
|
||||
output.getBuffer().writeBytes(LENGTH_PLACEHOLDER);
|
||||
marshaller.start(output);
|
||||
marshaller.write(LENGTH_PLACEHOLDER);
|
||||
marshaller.writeObject(msg);
|
||||
marshaller.finish();
|
||||
marshaller.close();
|
||||
|
|
|
@ -44,7 +44,7 @@ public abstract class AbstractCompatibleMarshallingDecoderTest {
|
|||
MarshallerFactory marshallerFactory = createMarshallerFactory();
|
||||
MarshallingConfiguration configuration = createMarshallingConfig();
|
||||
|
||||
DecoderEmbedder<Object> decoder = new DecoderEmbedder<Object>(createDecoder(0));
|
||||
DecoderEmbedder<Object> decoder = new DecoderEmbedder<Object>(createDecoder(Integer.MAX_VALUE));
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
Marshaller marshaller = marshallerFactory.createMarshaller(configuration);
|
||||
|
@ -55,7 +55,7 @@ public abstract class AbstractCompatibleMarshallingDecoderTest {
|
|||
|
||||
byte[] testBytes = bout.toByteArray();
|
||||
|
||||
decoder.offer(ChannelBuffers.wrappedBuffer(testBytes));
|
||||
decoder.offer(input(testBytes));
|
||||
assertTrue(decoder.finish());
|
||||
|
||||
String unmarshalled = (String) decoder.poll();
|
||||
|
@ -65,13 +65,16 @@ public abstract class AbstractCompatibleMarshallingDecoderTest {
|
|||
Assert.assertNull(decoder.poll());
|
||||
}
|
||||
|
||||
protected ChannelBuffer input(byte[] input) {
|
||||
return ChannelBuffers.wrappedBuffer(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFragmentedUnmarshalling() throws IOException {
|
||||
MarshallerFactory marshallerFactory = createMarshallerFactory();
|
||||
MarshallingConfiguration configuration = createMarshallingConfig();
|
||||
|
||||
DecoderEmbedder<Object> decoder = new DecoderEmbedder<Object>(createDecoder(0));
|
||||
DecoderEmbedder<Object> decoder = new DecoderEmbedder<Object>(createDecoder(Integer.MAX_VALUE));
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
Marshaller marshaller = marshallerFactory.createMarshaller(configuration);
|
||||
|
@ -82,7 +85,7 @@ public abstract class AbstractCompatibleMarshallingDecoderTest {
|
|||
|
||||
byte[] testBytes = bout.toByteArray();
|
||||
|
||||
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(testBytes);
|
||||
ChannelBuffer buffer = input(testBytes);
|
||||
ChannelBuffer slice = buffer.readSlice(2);
|
||||
|
||||
decoder.offer(slice);
|
||||
|
@ -102,7 +105,7 @@ public abstract class AbstractCompatibleMarshallingDecoderTest {
|
|||
MarshallerFactory marshallerFactory = createMarshallerFactory();
|
||||
MarshallingConfiguration configuration = createMarshallingConfig();
|
||||
|
||||
ChannelUpstreamHandler mDecoder = createDecoder(1);
|
||||
ChannelUpstreamHandler mDecoder = createDecoder(4);
|
||||
DecoderEmbedder<Object> decoder = new DecoderEmbedder<Object>(mDecoder);
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
|
@ -115,7 +118,7 @@ public abstract class AbstractCompatibleMarshallingDecoderTest {
|
|||
byte[] testBytes = bout.toByteArray();
|
||||
|
||||
try {
|
||||
decoder.offer(ChannelBuffers.wrappedBuffer(testBytes));
|
||||
decoder.offer(input(testBytes));
|
||||
fail();
|
||||
} catch (CodecEmbedderException e) {
|
||||
assertEquals(TooLongFrameException.class, e.getCause().getClass());
|
||||
|
@ -125,7 +128,7 @@ public abstract class AbstractCompatibleMarshallingDecoderTest {
|
|||
|
||||
}
|
||||
|
||||
private ChannelUpstreamHandler createDecoder(long maxObjectSize) {
|
||||
protected ChannelUpstreamHandler createDecoder(int maxObjectSize) {
|
||||
return new CompatibleMarshallingDecoder(createProvider(createMarshallerFactory(), createMarshallingConfig()), maxObjectSize);
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ public abstract class AbstractCompatibleMarshallingEncoderTest {
|
|||
ChannelBuffer buffer = encoder.poll();
|
||||
|
||||
Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration);
|
||||
unmarshaller.start(Marshalling.createByteInput(buffer.toByteBuffer()));
|
||||
unmarshaller.start(Marshalling.createByteInput(truncate(buffer).toByteBuffer()));
|
||||
String read = (String) unmarshaller.readObject();
|
||||
Assert.assertEquals(testObject, read);
|
||||
|
||||
|
@ -56,6 +56,9 @@ public abstract class AbstractCompatibleMarshallingEncoderTest {
|
|||
unmarshaller.finish();
|
||||
unmarshaller.close();
|
||||
}
|
||||
protected ChannelBuffer truncate(ChannelBuffer buf) {
|
||||
return buf;
|
||||
}
|
||||
|
||||
protected ChannelDownstreamHandler createEncoder() {
|
||||
return new CompatibleMarshallingEncoder(createProvider());
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright 2012 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.handler.codec.marshalling;
|
||||
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.buffer.ChannelBuffers;
|
||||
import org.jboss.netty.channel.ChannelUpstreamHandler;
|
||||
|
||||
public class RiverMarshallingDecoderTest extends RiverCompatibleMarshallingDecoderTest {
|
||||
|
||||
@Override
|
||||
protected ChannelBuffer input(byte[] input) {
|
||||
ChannelBuffer length = ChannelBuffers.buffer(4);
|
||||
length.writeInt(input.length);
|
||||
return ChannelBuffers.wrappedBuffer(length, ChannelBuffers.wrappedBuffer(input));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ChannelUpstreamHandler createDecoder(int maxObjectSize) {
|
||||
return new MarshallingDecoder(createProvider(createMarshallerFactory(), createMarshallingConfig()), maxObjectSize);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright 2012 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.handler.codec.marshalling;
|
||||
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.channel.ChannelDownstreamHandler;
|
||||
|
||||
public class RiverMarshallingEncoderTest extends RiverCompatibleMarshallingEncoderTest {
|
||||
|
||||
@Override
|
||||
protected ChannelBuffer truncate(ChannelBuffer buf) {
|
||||
buf.readInt();
|
||||
return buf;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ChannelDownstreamHandler createEncoder() {
|
||||
return new MarshallingEncoder(createProvider());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 2012 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.handler.codec.marshalling;
|
||||
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.buffer.ChannelBuffers;
|
||||
import org.jboss.netty.channel.ChannelUpstreamHandler;
|
||||
|
||||
public class SerialMarshallingDecoderTest extends SerialCompatibleMarshallingDecoderTest {
|
||||
|
||||
@Override
|
||||
protected ChannelBuffer input(byte[] input) {
|
||||
ChannelBuffer length = ChannelBuffers.buffer(4);
|
||||
length.writeInt(input.length);
|
||||
return ChannelBuffers.wrappedBuffer(length, ChannelBuffers.wrappedBuffer(input));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ChannelUpstreamHandler createDecoder(int maxObjectSize) {
|
||||
return new MarshallingDecoder(createProvider(createMarshallerFactory(), createMarshallingConfig()), maxObjectSize);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright 2012 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.handler.codec.marshalling;
|
||||
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.channel.ChannelDownstreamHandler;
|
||||
|
||||
public class SerialMarshallingEncoderTest extends SerialCompatibleMarshallingEncoderTest{
|
||||
|
||||
@Override
|
||||
protected ChannelBuffer truncate(ChannelBuffer buf) {
|
||||
buf.readInt();
|
||||
return buf;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ChannelDownstreamHandler createEncoder() {
|
||||
return new MarshallingEncoder(createProvider());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user