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: * Unit test for items with varying sizes.
031: */
032: public class SizesTest extends FileUploadTestCase {
033: /** Runs a test with varying file sizes.
034: */
035: public void testFileUpload() throws IOException,
036: FileUploadException {
037: ByteArrayOutputStream baos = new ByteArrayOutputStream();
038: int add = 16;
039: int num = 0;
040: for (int i = 0; i < 16384; i += add) {
041: if (++add == 32) {
042: add = 16;
043: }
044: String header = "-----1234\r\n"
045: + "Content-Disposition: form-data; name=\"field"
046: + (num++) + "\"\r\n" + "\r\n";
047: baos.write(header.getBytes("US-ASCII"));
048: for (int j = 0; j < i; j++) {
049: baos.write((byte) j);
050: }
051: baos.write("\r\n".getBytes("US-ASCII"));
052: }
053: baos.write("-----1234--\r\n".getBytes("US-ASCII"));
054:
055: List fileItems = parseUpload(baos.toByteArray());
056: Iterator fileIter = fileItems.iterator();
057: add = 16;
058: num = 0;
059: for (int i = 0; i < 16384; i += add) {
060: if (++add == 32) {
061: add = 16;
062: }
063: FileItem item = (FileItem) fileIter.next();
064: assertEquals("field" + (num++), item.getFieldName());
065: byte[] bytes = item.get();
066: assertEquals(i, bytes.length);
067: for (int j = 0; j < i; j++) {
068: assertEquals((byte) j, bytes[j]);
069: }
070: }
071: assertTrue(!fileIter.hasNext());
072: }
073:
074: /** Checks, whether limiting the file size works.
075: */
076: public void testFileSizeLimit() throws IOException,
077: FileUploadException {
078: final String request = "-----1234\r\n"
079: + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n"
080: + "Content-Type: text/whatever\r\n" + "\r\n"
081: + "This is the content of the file\n" + "\r\n"
082: + "-----1234--\r\n";
083:
084: ServletFileUpload upload = new ServletFileUpload(
085: new DiskFileItemFactory());
086: upload.setFileSizeMax(-1);
087: HttpServletRequest req = new MockHttpServletRequest(request
088: .getBytes("US-ASCII"), CONTENT_TYPE);
089: List fileItems = upload.parseRequest(req);
090: assertEquals(1, fileItems.size());
091: FileItem item = (FileItem) fileItems.get(0);
092: assertEquals("This is the content of the file\n", new String(
093: item.get()));
094:
095: upload = new ServletFileUpload(new DiskFileItemFactory());
096: upload.setFileSizeMax(40);
097: req = new MockHttpServletRequest(request.getBytes("US-ASCII"),
098: CONTENT_TYPE);
099: fileItems = upload.parseRequest(req);
100: assertEquals(1, fileItems.size());
101: item = (FileItem) fileItems.get(0);
102: assertEquals("This is the content of the file\n", new String(
103: item.get()));
104:
105: upload = new ServletFileUpload(new DiskFileItemFactory());
106: upload.setFileSizeMax(30);
107: req = new MockHttpServletRequest(request.getBytes("US-ASCII"),
108: CONTENT_TYPE);
109: try {
110: upload.parseRequest(req);
111: fail("Expected exception.");
112: } catch (FileUploadBase.FileSizeLimitExceededException e) {
113: assertEquals(30, e.getPermittedSize());
114: }
115: }
116: }
|