From ecaba683482a9bff9f1e6cdd04c6d7531238542a Mon Sep 17 00:00:00 2001 From: ursa Date: Sat, 16 Mar 2013 23:08:27 +0400 Subject: [PATCH] Fix bug in memory-based HTTP data content initialization with input stream, add test (port from branch 3). - Fixes #1170 --- .../multipart/AbstractMemoryHttpData.java | 2 +- .../multipart/AbstractMemoryHttpDataTest.java | 91 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpDataTest.java diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java index 196e6ae2c4..f3461d8f88 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java @@ -67,7 +67,7 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData { int read = inputStream.read(bytes); int written = 0; while (read > 0) { - buffer.writeBytes(bytes); + buffer.writeBytes(bytes, 0, read); written += read; read = inputStream.read(bytes); } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpDataTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpDataTest.java new file mode 100644 index 0000000000..1ecbc910a5 --- /dev/null +++ b/codec-http/src/test/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpDataTest.java @@ -0,0 +1,91 @@ +/* + * 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 io.netty.handler.codec.http.multipart; + +import io.netty.buffer.ByteBuf; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.nio.charset.Charset; +import java.security.SecureRandom; +import java.util.Arrays; +import java.util.Random; + +import static io.netty.util.CharsetUtil.UTF_8; +import static org.junit.Assert.*; + +/** {@link AbstractMemoryHttpData} test cases. */ +public class AbstractMemoryHttpDataTest { + /** + * Provide content into HTTP data with input stream. + * + * @throws Exception In case of any exception. + */ + @Test + public void testSetContentFromStream() throws Exception { + Random random = new SecureRandom(); + + for (int i = 0; i < 20; i++) { + // Generate input data bytes. + int size = random.nextInt(Short.MAX_VALUE); + byte[] bytes = new byte[size]; + + random.nextBytes(bytes); + + // Generate parsed HTTP data block. + TestHttpData data = new TestHttpData("name", UTF_8, 0); + + data.setContent(new ByteArrayInputStream(bytes)); + + // Validate stored data. + ByteBuf buffer = data.getByteBuf(); + + assertEquals(0, buffer.readerIndex()); + assertEquals(bytes.length, buffer.writerIndex()); + assertArrayEquals(bytes, Arrays.copyOf(buffer.array(), bytes.length)); + } + } + + + /** Memory-based HTTP data implementation for test purposes. */ + private static final class TestHttpData extends AbstractMemoryHttpData { + /** + * Constructs HTTP data for tests. + * + * @param name Name of parsed data block. + * @param charset Used charset for data decoding. + * @param size Expected data block size. + */ + protected TestHttpData(String name, Charset charset, long size) { + super(name, charset, size); + } + + @Override + public InterfaceHttpData.HttpDataType getHttpDataType() { + throw new UnsupportedOperationException("Should never be called."); + } + + @Override + public HttpData copy() { + throw new UnsupportedOperationException("Should never be called."); + } + + @Override + public int compareTo(InterfaceHttpData o) { + throw new UnsupportedOperationException("Should never be called."); + } + } +}