diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java index 8f060865ba..8c49beab46 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java @@ -37,19 +37,19 @@ import org.jboss.netty.handler.codec.replay.VoidEnum; */ public class CompatibleMarshallingDecoder extends ReplayingDecoder { 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 { 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 { diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingDecoder.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingDecoder.java index 889a416a10..d4a1ad30ad 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingDecoder.java @@ -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); diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingEncoder.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingEncoder.java index 6bee81ceed..ea003f1965 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingEncoder.java @@ -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(); diff --git a/src/test/java/org/jboss/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java index bf87bcee82..0f4d758315 100644 --- a/src/test/java/org/jboss/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java +++ b/src/test/java/org/jboss/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java @@ -44,7 +44,7 @@ public abstract class AbstractCompatibleMarshallingDecoderTest { MarshallerFactory marshallerFactory = createMarshallerFactory(); MarshallingConfiguration configuration = createMarshallingConfig(); - DecoderEmbedder decoder = new DecoderEmbedder(createDecoder(0)); + DecoderEmbedder decoder = new DecoderEmbedder(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 decoder = new DecoderEmbedder(createDecoder(0)); + DecoderEmbedder decoder = new DecoderEmbedder(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 decoder = new DecoderEmbedder(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); } diff --git a/src/test/java/org/jboss/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java index 2247a0f755..078531cc5b 100644 --- a/src/test/java/org/jboss/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java +++ b/src/test/java/org/jboss/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java @@ -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()); diff --git a/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java new file mode 100644 index 0000000000..14d95c264c --- /dev/null +++ b/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java @@ -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); + } + + +} diff --git a/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverMarshallingEncoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverMarshallingEncoderTest.java new file mode 100644 index 0000000000..d5ad21a027 --- /dev/null +++ b/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverMarshallingEncoderTest.java @@ -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()); + } + +} diff --git a/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java new file mode 100644 index 0000000000..23b490bc5d --- /dev/null +++ b/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java @@ -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); + } + +} diff --git a/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialMarshallingEncoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialMarshallingEncoderTest.java new file mode 100644 index 0000000000..748690d47c --- /dev/null +++ b/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialMarshallingEncoderTest.java @@ -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()); + } + +}