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  package org.apache.commons.fileupload;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
27  import org.apache.commons.fileupload.servlet.ServletFileUpload;
28  
29  
30  /**
31   * Unit test for items with varying sizes.
32   */
33  public class SizesTest extends FileUploadTestCase
34  {
35  	/** Runs a test with varying file sizes.
36  	 */
37  	public void testFileUpload()
38              throws IOException, FileUploadException
39      {
40          ByteArrayOutputStream baos = new ByteArrayOutputStream();
41          int add = 16;
42          int num = 0;
43          for (int i = 0;  i < 16384;  i += add) {
44              if (++add == 32) {
45                  add = 16;
46              }
47              String header = "-----1234\r\n"
48                  + "Content-Disposition: form-data; name=\"field" + (num++) + "\"\r\n"
49                  + "\r\n";
50              baos.write(header.getBytes("US-ASCII"));
51              for (int j = 0;  j < i;  j++) {
52                  baos.write((byte) j);
53              }
54              baos.write("\r\n".getBytes("US-ASCII"));
55          }
56          baos.write("-----1234--\r\n".getBytes("US-ASCII"));
57  
58          List fileItems = parseUpload(baos.toByteArray());
59          Iterator fileIter = fileItems.iterator();
60          add = 16;
61          num = 0;
62          for (int i = 0;  i < 16384;  i += add) {
63              if (++add == 32) {
64                  add = 16;
65              }
66              FileItem item = (FileItem) fileIter.next();
67              assertEquals("field" + (num++), item.getFieldName());
68              byte[] bytes = item.get();
69              assertEquals(i, bytes.length);
70              for (int j = 0;  j < i;  j++) {
71                  assertEquals((byte) j, bytes[j]);
72              }
73          }
74          assertTrue(!fileIter.hasNext());
75      }
76  
77  	/** Checks, whether limiting the file size works.
78  	 */
79  	public void testFileSizeLimit()
80              throws IOException, FileUploadException
81      {
82  		final String request =
83  			"-----1234\r\n" +
84  			"Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
85  			"Content-Type: text/whatever\r\n" +
86  			"\r\n" +
87  			"This is the content of the file\n" +
88  			"\r\n" +
89  			"-----1234--\r\n";
90  
91  		ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
92  		upload.setFileSizeMax(-1);
93          HttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
94          List fileItems = upload.parseRequest(req);
95          assertEquals(1, fileItems.size());
96          FileItem item = (FileItem) fileItems.get(0);
97          assertEquals("This is the content of the file\n", new String(item.get()));
98          
99  		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 }