View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.commons.fileupload;
    19
    20  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
    21  import static org.junit.jupiter.api.Assertions.assertNotNull;
    22  import static org.junit.jupiter.api.Assertions.assertThrows;
    23
    24  import java.io.ByteArrayInputStream;
    25  import java.io.IOException;
    26  import java.io.InputStream;
    27
    28  import org.apache.commons.fileupload.MultipartStream.MalformedStreamException;
    29  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    30  import org.apache.commons.fileupload.servlet.ServletFileUpload;
    31  import org.junit.Test;
    32
    33  /**
    34   * Tests {@link org.apache.commons.fileupload.MultipartStream}.
    35   */
    36  public class MultipartStreamTest {
    37
    38      static private final String BOUNDARY_TEXT = "myboundary";
    39
    40      @Test
    41      public void testMalformedUploadTruncatedHeaders()
    42              throws IOException, FileUploadException {
    43          final String request =
    44              "-----1234\r\n" +
    45              "Content-Disposition: form-data; name=\"file1\"; filename=\"foo1.tab\"\r\n" +
    46              "Content-Type: text/whatever\r\n" +
    47              "Content-Length: 10\r\n" +
    48              "\r\n" +
    49              "This is the content of the file\n" +
    50              "\r\n" +
    51              "-----1234\r\n" +
    52              "Content-Disposition: form-data; name=\"file2\"; filename=\"foo2.tab\"\r\n" +
    53              "Content-Type: text/whatever\r\n" +
    54              "\r\n" +
    55              "This is the content of the file\n";
    56
    57          final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
    58          upload.setFileSizeMax(-1);
    59          upload.setSizeMax(-1);
    60
    61          final MockHttpServletRequest req = new MockHttpServletRequest(
    62                  request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
    63          assertThrows(FileUploadBase.IOFileUploadException.class, () -> upload.parseRequest(req));
    64      }
    65
    66      @Test
    67      public void testMalformedUploadTruncatedHeadersOnBoundary() throws IOException {
    68          final StringBuilder request = new StringBuilder(
    69              "-----1234\r\n" +
    70              "Content-Disposition: form-data; name=\"file1\"; filename=\"foo1.tab\"\r\n" +
    71              "Content-Type: text/whatever\r\n" +
    72              "Content-Length: 10\r\n" +
    73              "X-Padding: ");
    74          final int paddingLength = MultipartStream.DEFAULT_BUFSIZE - request.length();
    75          for (int i = 0; i < paddingLength; i++) {
    76              request.append('x');
    77          }
    78
    79          final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
    80          upload.setFileSizeMax(-1);
    81          upload.setSizeMax(-1);
    82          upload.setPartHeaderSizeMax(-1);
    83
    84          final MockHttpServletRequest req = new MockHttpServletRequest(
    85                  request.toString().getBytes("US-ASCII"), Constants.CONTENT_TYPE);
    86          final FileUploadException e = assertThrows(FileUploadException.class, () -> upload.parseRequest(req));
    87          assertInstanceOf(MalformedStreamException.class, e.getCause());
    88      }
    89
    90      @Test(expected = IllegalArgumentException.class)
    91      public void testSmallBuffer() throws Exception {
    92          final String strData = "foobar";
    93          final byte[] contents = strData.getBytes();
    94          final InputStream input = new ByteArrayInputStream(contents);
    95          final byte[] boundary = BOUNDARY_TEXT.getBytes();
    96          final int iBufSize = 1;
    97          @SuppressWarnings("unused")
    98          final MultipartStream unused = new MultipartStream(input, boundary, iBufSize, new MultipartStream.ProgressNotifier(null, contents.length));
    99      }
    100
    101     @Test
    102     public void testThreeParamConstructor() throws Exception {
    103         final String strData = "foobar";
    104         final byte[] contents = strData.getBytes();
    105         final InputStream input = new ByteArrayInputStream(contents);
    106         final byte[] boundary = BOUNDARY_TEXT.getBytes();
    107         final int iBufSize = boundary.length + MultipartStream.BOUNDARY_PREFIX.length + 1;
    108         final MultipartStream ms = new MultipartStream(input, boundary, iBufSize, new MultipartStream.ProgressNotifier(null, contents.length));
    109         assertNotNull(ms);
    110     }
    111
    112     @Test
    113     public void testTwoParamConstructor() throws Exception {
    114         final String strData = "foobar";
    115         final byte[] contents = strData.getBytes();
    116         final InputStream input = new ByteArrayInputStream(contents);
    117         final byte[] boundary = BOUNDARY_TEXT.getBytes();
    118         final MultipartStream ms = new MultipartStream(input, boundary, new MultipartStream.ProgressNotifier(null, contents.length));
    119         assertNotNull(ms);
    120     }
    121 }