001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml.multipart;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.FileInputStream;
024: import java.io.FileNotFoundException;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.util.ArrayList;
028: import java.util.Enumeration;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.List;
032:
033: import javax.mail.Header;
034: import javax.mail.MessagingException;
035: import javax.mail.internet.InternetHeaders;
036:
037: import junit.framework.TestCase;
038:
039: /**
040: * This class tests the RFC822Headers class by parsing
041: * some example headers and recorded multipart/form-data
042: * requests made by different browsers
043: *
044: * @author mleidig@schlund.de
045: */
046: public class RFC822HeadersTest extends TestCase {
047:
048: static final byte CR = 0x0D;
049: static final byte LF = 0x0A;
050: static final byte[] CRLF = { CR, LF };
051:
052: public void testComplexHeader() throws Exception {
053: InputStream in = getInputStream("complex_header.dump");
054: InternetHeaders mailHeaders = new InternetHeaders(in);
055: HashSet<String> headerSet = new HashSet<String>();
056: Enumeration<?> headersEnum = mailHeaders.getAllHeaders();
057: while (headersEnum.hasMoreElements()) {
058: Header header = (Header) headersEnum.nextElement();
059: String name = header.getName();
060: headerSet.add(name);
061: }
062: in.close();
063: in = getInputStream("complex_header.dump");
064: RFC822Headers headers = new RFC822Headers(in, null);
065: in.close();
066: Iterator<String> it = headerSet.iterator();
067: while (it.hasNext()) {
068: String name = it.next();
069: String[] refValues = mailHeaders.getHeader(name);
070: String[] values = headers.getHeader(name);
071: assertNotNull(values);
072: if (values.length != refValues.length)
073: assertEquals(values.length, refValues.length);
074: else
075: for (int i = 0; i < values.length; i++)
076: assertEquals(values[i], refValues[i]);
077: }
078: }
079:
080: public void testIEWinUpload() throws Exception {
081: String boundary = "-----------------------------7d6203aa006c";
082: String filename = "ie_win_upload.dump";
083: uploadTest(boundary, filename);
084: }
085:
086: public void testFFWinUpload() throws Exception {
087: String boundary = "-----------------------------41184676334";
088: String filename = "ff_win_upload.dump";
089: uploadTest(boundary, filename);
090: }
091:
092: public void testFFLinuxUpload() throws Exception {
093: String boundary = "-----------------------------1383361789892271212848029586";
094: String filename = "ff_linux_upload.dump";
095: uploadTest(boundary, filename);
096: }
097:
098: private void uploadTest(String boundary, String filename)
099: throws Exception {
100: InputStream in = getInputStream(filename);
101: List<Part> refParts = parseMultipart(in, boundary, false);
102: in = getInputStream(filename);
103: List<Part> parts = parseMultipart(in, boundary, true);
104: assertEquals(parts.size(), refParts.size());
105: for (int i = 0; i < parts.size(); i++) {
106: byte[] body = parts.get(i).body;
107: byte[] refBody = refParts.get(i).body;
108: assertEquals(body.length, refBody.length);
109: for (int j = 0; j < body.length; j++)
110: assertEquals(body[j], refBody[j]);
111: assertEquals(parts.get(i).ctype, refParts.get(i).ctype);
112: }
113: }
114:
115: private InputStream getInputStream(String fileName) {
116: InputStream in = getClass().getClassLoader()
117: .getResourceAsStream(
118: "de/schlund/pfixxml/multipart/" + fileName);
119: if (in == null) {
120: try {
121: in = new FileInputStream(
122: "tests/junit/de/schlund/pfixxml/multipart/"
123: + fileName);
124: } catch (FileNotFoundException x) {
125: throw new RuntimeException(x);
126: }
127: }
128: return in;
129: }
130:
131: private List<Part> parseMultipart(InputStream in, String boundary,
132: boolean custom) throws IOException, MessagingException {
133: ArrayList<Part> parts = new ArrayList<Part>();
134: ByteArrayOutputStream body = new ByteArrayOutputStream();
135: ByteArrayOutputStream out = new ByteArrayOutputStream();
136: boolean endBoundary = false;
137: int b = -1;
138: while ((b = in.read()) != -1) {
139: if (b == CR) {
140: b = in.read();
141: if (b == LF) {
142: byte[] lineBytes = out.toByteArray();
143: if (isBoundary(lineBytes, boundary)) {
144: if (endBoundary) {
145: byte[] bodyBytes = body.toByteArray();
146: Part part = new Part();
147: parts.add(part);
148: part.body = bodyBytes;
149: body.reset();
150: if (custom) {
151: RFC822Headers headers = new RFC822Headers(
152: in, "UTF-8");
153: String vals[] = headers
154: .getHeader("Content-Type");
155: if (vals != null && vals.length > 0)
156: part.ctype = vals[0];
157: vals = headers
158: .getHeader("Content-Disposition");
159: if (vals != null && vals.length > 0)
160: part.cdisp = vals[0];
161: } else {
162: InternetHeaders headers = new InternetHeaders(
163: in);
164: String[] vals = headers
165: .getHeader("Content-Type");
166: if (vals != null && vals.length > 0)
167: part.ctype = vals[0];
168: vals = headers
169: .getHeader("Content-Disposition");
170: if (vals != null && vals.length > 0)
171: part.cdisp = vals[0];
172: }
173: }
174: endBoundary = true;
175: } else
176: body.write(lineBytes);
177: out.reset();
178: } else {
179: out.write(CR);
180: out.write(b);
181: }
182: } else
183: out.write(b);
184: }
185: return parts;
186: }
187:
188: private boolean isBoundary(byte[] bytes, String boundary) {
189: if (bytes.length >= boundary.length()) {
190: for (int i = 0; i < boundary.length(); i++)
191: if (bytes[i] != boundary.charAt(i))
192: return false;
193: return true;
194: }
195: return false;
196: }
197:
198: class Part {
199: byte[] body;
200: String ctype;
201: String cdisp;
202: }
203:
204: public static void main(String[] args) throws Exception {
205: RFC822HeadersTest test = new RFC822HeadersTest();
206: test.testComplexHeader();
207: test.testFFLinuxUpload();
208: test.testFFWinUpload();
209: test.testIEWinUpload();
210: }
211:
212: }
|