1 | /**************************************************************** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
3 | * or more contributor license agreements. See the NOTICE file * |
---|
4 | * distributed with this work for additional information * |
---|
5 | * regarding copyright ownership. The ASF licenses this file * |
---|
6 | * to you under the Apache License, Version 2.0 (the * |
---|
7 | * "License"); you may not use this file except in compliance * |
---|
8 | * with the License. You may obtain a copy of the License at * |
---|
9 | * * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
11 | * * |
---|
12 | * Unless required by applicable law or agreed to in writing, * |
---|
13 | * software distributed under the License is distributed on an * |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
15 | * KIND, either express or implied. See the License for the * |
---|
16 | * specific language governing permissions and limitations * |
---|
17 | * under the License. * |
---|
18 | ****************************************************************/ |
---|
19 | |
---|
20 | package org.apache.james.mime4j.codec; |
---|
21 | |
---|
22 | import java.io.ByteArrayInputStream; |
---|
23 | import java.io.ByteArrayOutputStream; |
---|
24 | import java.io.IOException; |
---|
25 | import java.io.InputStream; |
---|
26 | import java.io.UnsupportedEncodingException; |
---|
27 | |
---|
28 | import junit.framework.TestCase; |
---|
29 | |
---|
30 | public class QuotedPrintableInputStreamTest extends TestCase { |
---|
31 | |
---|
32 | public void testBasicDecode() throws IOException, UnsupportedEncodingException { |
---|
33 | ByteArrayInputStream bis = new ByteArrayInputStream("=e1=e2=E3=E4\r\n".getBytes("US-ASCII")); |
---|
34 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
35 | assertEquals("\u00e1\u00e2\u00e3\u00e4\r\n", new String(read(decoder), "ISO8859-1")); |
---|
36 | } |
---|
37 | |
---|
38 | public void testDecodeBufferWrapping() throws IOException, UnsupportedEncodingException { |
---|
39 | ByteArrayInputStream bis = new ByteArrayInputStream( |
---|
40 | "=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n".getBytes("US-ASCII")); |
---|
41 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
42 | assertEquals("\u00e1\u00e2\u00e3\u00e4\r\n\u00e1\u00e2\u00e3\u00e4\r\n\u00e1\u00e2\u00e3" + |
---|
43 | "\u00e4\r\n\u00e1\u00e2\u00e3\u00e4\r\n\u00e1\u00e2\u00e3\u00e4\r\n", new String(read(decoder), "ISO8859-1")); |
---|
44 | } |
---|
45 | |
---|
46 | public void testInvalidValueDecode() throws IOException, UnsupportedEncodingException { |
---|
47 | ByteArrayInputStream bis = new ByteArrayInputStream("=e1=g2=E3=E4\r\n".getBytes("US-ASCII")); |
---|
48 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
49 | assertEquals("\u00e1=g2\u00e3\u00e4\r\n", new String(read(decoder), "ISO8859-1")); |
---|
50 | } |
---|
51 | |
---|
52 | public void testDecodeTrailingBlanks() throws IOException, UnsupportedEncodingException { |
---|
53 | ByteArrayInputStream bis = new ByteArrayInputStream(" =e1 =e2 =E3\t=E4 \t \t \r\n".getBytes("US-ASCII")); |
---|
54 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
55 | assertEquals(" \u00e1 \u00e2 \u00e3\t\u00e4\r\n", new String(read(decoder), "ISO8859-1")); |
---|
56 | } |
---|
57 | |
---|
58 | public void testCanonicalSoftBreakDecode() throws IOException, UnsupportedEncodingException { |
---|
59 | ByteArrayInputStream bis = new ByteArrayInputStream("Soft line =\r\nHard line \r\n".getBytes("US-ASCII")); |
---|
60 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
61 | assertEquals("Soft line Hard line\r\n", new String(read(decoder), "ISO8859-1")); |
---|
62 | } |
---|
63 | |
---|
64 | public void testInvalidCR() throws IOException, UnsupportedEncodingException { |
---|
65 | ByteArrayInputStream bis = new ByteArrayInputStream("Invalid=\rCR\rHard line \r\n".getBytes("US-ASCII")); |
---|
66 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
67 | // TODO is this what we really expect from decoding a stream including CR with no LF? |
---|
68 | assertEquals("Invalid=\rCR\rHard line\r\n", new String(read(decoder), "ISO8859-1")); |
---|
69 | } |
---|
70 | |
---|
71 | public void testSoftBreakLoneLFDecode() throws IOException, UnsupportedEncodingException { |
---|
72 | ByteArrayInputStream bis = new ByteArrayInputStream("Soft line =\nHard line \r\n".getBytes("US-ASCII")); |
---|
73 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
74 | assertEquals("Soft line Hard line\r\n", new String(read(decoder), "ISO8859-1")); |
---|
75 | } |
---|
76 | |
---|
77 | public void testSoftBreakTrailingBalnksDecode() throws IOException, UnsupportedEncodingException { |
---|
78 | ByteArrayInputStream bis = new ByteArrayInputStream("Soft line = \t \r\nHard line \r\n".getBytes("US-ASCII")); |
---|
79 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
80 | assertEquals("Soft line Hard line\r\n", new String(read(decoder), "ISO8859-1")); |
---|
81 | } |
---|
82 | |
---|
83 | public void testBrokenSoftBreakDecode() throws IOException, UnsupportedEncodingException { |
---|
84 | ByteArrayInputStream bis = new ByteArrayInputStream("Soft line =\rHard line \r\n".getBytes("US-ASCII")); |
---|
85 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
86 | assertEquals("Soft line =\rHard line\r\n", new String(read(decoder), "ISO8859-1")); |
---|
87 | } |
---|
88 | |
---|
89 | public void testEscapedEQDecode() throws IOException, UnsupportedEncodingException { |
---|
90 | ByteArrayInputStream bis = new ByteArrayInputStream("width==340 height=3d200\r\n".getBytes("US-ASCII")); |
---|
91 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
92 | assertEquals("width=340 height=200\r\n", new String(read(decoder), "ISO8859-1")); |
---|
93 | // TODO this could be even decoded as width=40 height=200. |
---|
94 | } |
---|
95 | |
---|
96 | public void testBrokenEscapedEQDecode() throws IOException, UnsupportedEncodingException { |
---|
97 | /* |
---|
98 | * This isn't valid qp (==) but it is known to occur in certain |
---|
99 | * messages, especially spam. |
---|
100 | */ |
---|
101 | ByteArrayInputStream bis = new ByteArrayInputStream("width==\r\n340 height=3d200\r\n".getBytes("US-ASCII")); |
---|
102 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
103 | assertEquals("width=340 height=200\r\n", new String(read(decoder), "ISO8859-1")); |
---|
104 | } |
---|
105 | |
---|
106 | public void testSpacesBeforeEOL() throws IOException, UnsupportedEncodingException { |
---|
107 | ByteArrayInputStream bis = new ByteArrayInputStream("some \r\n spaced\t\r\ncontent \t \r\n".getBytes("US-ASCII")); |
---|
108 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); |
---|
109 | assertEquals("some\r\n spaced\r\ncontent\r\n", new String(read(decoder), "ISO8859-1")); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | public void testDecodeEndOfStream1() throws IOException, UnsupportedEncodingException { |
---|
114 | ByteArrayInputStream bis = new ByteArrayInputStream("01234567".getBytes("US-ASCII")); |
---|
115 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); |
---|
116 | assertEquals("01234567", new String(read(decoder), "ISO8859-1")); |
---|
117 | } |
---|
118 | |
---|
119 | public void testDecodeEndOfStream2() throws IOException, UnsupportedEncodingException { |
---|
120 | ByteArrayInputStream bis = new ByteArrayInputStream("012345\r".getBytes("US-ASCII")); |
---|
121 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); |
---|
122 | assertEquals("012345", new String(read(decoder), "ISO8859-1")); |
---|
123 | } |
---|
124 | |
---|
125 | public void testDecodeEndOfStream3() throws IOException, UnsupportedEncodingException { |
---|
126 | ByteArrayInputStream bis = new ByteArrayInputStream("012345\n".getBytes("US-ASCII")); |
---|
127 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); |
---|
128 | assertEquals("012345\r\n", new String(read(decoder), "ISO8859-1")); |
---|
129 | } |
---|
130 | |
---|
131 | public void testDecodeEndOfStream4() throws IOException, UnsupportedEncodingException { |
---|
132 | ByteArrayInputStream bis = new ByteArrayInputStream("01234= ".getBytes("US-ASCII")); |
---|
133 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); |
---|
134 | assertEquals("01234", new String(read(decoder), "ISO8859-1")); |
---|
135 | } |
---|
136 | |
---|
137 | public void testDecodeEndOfStream5() throws IOException, UnsupportedEncodingException { |
---|
138 | ByteArrayInputStream bis = new ByteArrayInputStream("01234=\r\n".getBytes("US-ASCII")); |
---|
139 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); |
---|
140 | assertEquals("01234", new String(read(decoder), "ISO8859-1")); |
---|
141 | } |
---|
142 | |
---|
143 | public void testDecodeEndOfStream6() throws IOException, UnsupportedEncodingException { |
---|
144 | ByteArrayInputStream bis = new ByteArrayInputStream("01234\r\n".getBytes("US-ASCII")); |
---|
145 | QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); |
---|
146 | assertEquals("01234\r\n", new String(read(decoder), "ISO8859-1")); |
---|
147 | } |
---|
148 | |
---|
149 | public void testDecodePrematureClose() throws IOException, UnsupportedEncodingException { |
---|
150 | ByteArrayInputStream bis = null; |
---|
151 | QuotedPrintableInputStream decoder = null; |
---|
152 | |
---|
153 | bis = new ByteArrayInputStream("=e1=e2=E3=E4\r\n".getBytes("US-ASCII")); |
---|
154 | decoder = new QuotedPrintableInputStream(bis); |
---|
155 | assertEquals('\u00e1', decoder.read()); |
---|
156 | assertEquals('\u00e2', decoder.read()); |
---|
157 | decoder.close(); |
---|
158 | |
---|
159 | try { |
---|
160 | decoder.read(); |
---|
161 | fail(); |
---|
162 | } catch (IOException expected) { |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | private byte[] read(InputStream is) throws IOException { |
---|
167 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
---|
168 | int b; |
---|
169 | while ((b = is.read()) != -1) { |
---|
170 | bos.write(b); |
---|
171 | } |
---|
172 | return bos.toByteArray(); |
---|
173 | } |
---|
174 | } |
---|