1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.fileupload;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.FilterInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Iterator;
25 import java.util.List;
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.apache.commons.fileupload.FileUploadBase.FileUploadIOException;
29 import org.apache.commons.fileupload.FileUploadBase.IOFileUploadException;
30 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
31 import org.apache.commons.fileupload.servlet.ServletFileUpload;
32 import org.apache.commons.fileupload.servlet.ServletRequestContext;
33
34 import junit.framework.TestCase;
35
36
37 /**
38 * Unit test for items with varying sizes.
39 */
40 public class StreamingTest extends TestCase
41 {
42 /**
43 * Tests a file upload with varying file sizes.
44 */
45 public void testFileUpload()
46 throws IOException, FileUploadException
47 {
48 byte[] request = newRequest();
49 List fileItems = parseUpload(request);
50 Iterator fileIter = fileItems.iterator();
51 int add = 16;
52 int num = 0;
53 for (int i = 0; i < 16384; i += add) {
54 if (++add == 32) {
55 add = 16;
56 }
57 FileItem item = (FileItem) fileIter.next();
58 assertEquals("field" + (num++), item.getFieldName());
59 byte[] bytes = item.get();
60 assertEquals(i, bytes.length);
61 for (int j = 0; j < i; j++) {
62 assertEquals((byte) j, bytes[j]);
63 }
64 }
65 assertTrue(!fileIter.hasNext());
66 }
67
68
69 /**
70 * Tests, whether an invalid request throws a proper
71 * exception.
72 */
73 public void testFileUploadException()
74 throws IOException, FileUploadException {
75 byte[] request = newRequest();
76 byte[] invalidRequest = new byte[request.length-11];
77 System.arraycopy(request, 0, invalidRequest, 0, request.length-11);
78 try {
79 parseUpload(invalidRequest);
80 fail("Expected EndOfStreamException");
81 } catch (IOFileUploadException e) {
82 assertTrue(e.getCause() instanceof MultipartStream.MalformedStreamException);
83 }
84 }
85
86 /**
87 * Tests, whether an IOException is properly delegated.
88 */
89 public void testIOException()
90 throws IOException, FileUploadException {
91 byte[] request = newRequest();
92 InputStream stream = new FilterInputStream(new ByteArrayInputStream(request)){
93 private int num;
94 public int read() throws IOException {
95 if (++num > 123) {
96 throw new IOException("123");
97 }
98 return super.read();
99 }
100 public int read(byte[] pB, int pOff, int pLen)
101 throws IOException {
102 for (int i = 0; i < pLen; i++) {
103 int res = read();
104 if (res == -1) {
105 return i == 0 ? -1 : i;
106 }
107 pB[pOff+i] = (byte) res;
108 }
109 return pLen;
110 }
111 };
112 try {
113 parseUpload(stream, request.length);
114 } catch (IOFileUploadException e) {
115 assertTrue(e.getCause() instanceof IOException);
116 assertEquals("123", e.getCause().getMessage());
117 }
118 }
119
120 private List parseUpload(byte[] bytes) throws FileUploadException {
121 return parseUpload(new ByteArrayInputStream(bytes), bytes.length);
122 }
123
124 private List parseUpload(InputStream pStream, int pLength)
125 throws FileUploadException {
126 String contentType = "multipart/form-data; boundary=---1234";
127
128 FileUploadBase upload = new ServletFileUpload();
129 upload.setFileItemFactory(new DiskFileItemFactory());
130 HttpServletRequest request = new MockHttpServletRequest(pStream,
131 pLength, contentType);
132
133 List fileItems = upload.parseRequest(new ServletRequestContext(request));
134 return fileItems;
135 }
136
137 private byte[] newRequest() throws IOException {
138 ByteArrayOutputStream baos = new ByteArrayOutputStream();
139 int add = 16;
140 int num = 0;
141 for (int i = 0; i < 16384; i += add) {
142 if (++add == 32) {
143 add = 16;
144 }
145 String header = "-----1234\r\n"
146 + "Content-Disposition: form-data; name=\"field" + (num++) + "\"\r\n"
147 + "\r\n";
148 baos.write(header.getBytes("US-ASCII"));
149 for (int j = 0; j < i; j++) {
150 baos.write((byte) j);
151 }
152 baos.write("\r\n".getBytes("US-ASCII"));
153 }
154 baos.write("-----1234--\r\n".getBytes("US-ASCII"));
155 return baos.toByteArray();
156 }
157 }