001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.fileupload;
018
019 import java.io.ByteArrayOutputStream;
020 import java.io.IOException;
021 import java.util.Iterator;
022 import java.util.List;
023
024 import javax.servlet.http.HttpServletRequest;
025
026 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
027 import org.apache.commons.fileupload.servlet.ServletFileUpload;
028
029
030 /**
031 * Unit test for items with varying sizes.
032 */
033 public class SizesTest extends FileUploadTestCase
034 {
035 /** Runs a test with varying file sizes.
036 */
037 public void testFileUpload()
038 throws IOException, FileUploadException
039 {
040 ByteArrayOutputStream baos = new ByteArrayOutputStream();
041 int add = 16;
042 int num = 0;
043 for (int i = 0; i < 16384; i += add) {
044 if (++add == 32) {
045 add = 16;
046 }
047 String header = "-----1234\r\n"
048 + "Content-Disposition: form-data; name=\"field" + (num++) + "\"\r\n"
049 + "\r\n";
050 baos.write(header.getBytes("US-ASCII"));
051 for (int j = 0; j < i; j++) {
052 baos.write((byte) j);
053 }
054 baos.write("\r\n".getBytes("US-ASCII"));
055 }
056 baos.write("-----1234--\r\n".getBytes("US-ASCII"));
057
058 List fileItems = parseUpload(baos.toByteArray());
059 Iterator fileIter = fileItems.iterator();
060 add = 16;
061 num = 0;
062 for (int i = 0; i < 16384; i += add) {
063 if (++add == 32) {
064 add = 16;
065 }
066 FileItem item = (FileItem) fileIter.next();
067 assertEquals("field" + (num++), item.getFieldName());
068 byte[] bytes = item.get();
069 assertEquals(i, bytes.length);
070 for (int j = 0; j < i; j++) {
071 assertEquals((byte) j, bytes[j]);
072 }
073 }
074 assertTrue(!fileIter.hasNext());
075 }
076
077 /** Checks, whether limiting the file size works.
078 */
079 public void testFileSizeLimit()
080 throws IOException, FileUploadException
081 {
082 final String request =
083 "-----1234\r\n" +
084 "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
085 "Content-Type: text/whatever\r\n" +
086 "\r\n" +
087 "This is the content of the file\n" +
088 "\r\n" +
089 "-----1234--\r\n";
090
091 ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
092 upload.setFileSizeMax(-1);
093 HttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
094 List fileItems = upload.parseRequest(req);
095 assertEquals(1, fileItems.size());
096 FileItem item = (FileItem) fileItems.get(0);
097 assertEquals("This is the content of the file\n", new String(item.get()));
098
099 upload = new ServletFileUpload(new DiskFileItemFactory());
100 upload.setFileSizeMax(40);
101 req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
102 fileItems = upload.parseRequest(req);
103 assertEquals(1, fileItems.size());
104 item = (FileItem) fileItems.get(0);
105 assertEquals("This is the content of the file\n", new String(item.get()));
106
107 upload = new ServletFileUpload(new DiskFileItemFactory());
108 upload.setFileSizeMax(30);
109 req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
110 try {
111 upload.parseRequest(req);
112 fail("Expected exception.");
113 } catch (FileUploadBase.FileSizeLimitExceededException e) {
114 assertEquals(30, e.getPermittedSize());
115 }
116 }
117 }