View Javadoc
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.util.mime;
18  
19  import static org.junit.Assert.assertArrayEquals;
20  import static org.junit.Assert.fail;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.io.UnsupportedEncodingException;
26  
27  import org.junit.Test;
28  
29  /**
30   * @since 1.3
31   */
32  public final class QuotedPrintableDecoderTestCase {
33  
34      private static final String US_ASCII_CHARSET = "US-ASCII";
35  
36      @Test
37      public void emptyDecode() throws Exception {
38          assertEncoded("", "");
39      }
40  
41      @Test
42      public void plainDecode() throws Exception {
43          // spaces are allowed in encoded data
44          // There are special rules for trailing spaces; these are not currently implemented.
45          assertEncoded("The quick brown fox jumps over the lazy dog.", "The quick brown fox jumps over the lazy dog.");
46      }
47  
48      @Test
49      public void basicEncodeDecode() throws Exception {
50          assertEncoded("= Hello there =\r\n", "=3D Hello there =3D=0D=0A");
51      }
52  
53      @Test
54      public void invalidQuotedPrintableEncoding() throws Exception {
55          assertIOException("truncated escape sequence", "YWJjMTIzXy0uKn4hQCMkJV4mKCkre31cIlxcOzpgLC9bXQ==");
56      }
57  
58      @Test
59      public void unsafeDecode() throws Exception {
60          assertEncoded("=\r\n", "=3D=0D=0A");
61      }
62  
63      @Test
64      public void unsafeDecodeLowerCase() throws Exception {
65          assertEncoded("=\r\n", "=3d=0d=0a");
66      }
67  
68      @Test(expected = IOException.class)
69      public void invalidCharDecode() throws Exception {
70          assertEncoded("=\r\n", "=3D=XD=XA");
71      }
72  
73      /**
74       * This is NOT supported by Commons-Codec, see CODEC-121.
75       *
76       * @throws Exception
77       * @see <a href="https://issues.apache.org/jira/browse/CODEC-121">CODEC-121</a>
78       */
79      @Test
80      public void softLineBreakDecode() throws Exception {
81          assertEncoded("If you believe that truth=beauty, then surely mathematics is the most beautiful branch of philosophy.",
82                        "If you believe that truth=3Dbeauty, then surely=20=\r\nmathematics is the most beautiful branch of philosophy.");
83      }
84  
85      @Test
86      public void invalidSoftBreak1() throws Exception {
87          assertIOException("CR must be followed by LF", "=\r\r");
88      }
89  
90      @Test
91      public void invalidSoftBreak2() throws Exception {
92          assertIOException("CR must be followed by LF", "=\rn");
93      }
94  
95      @Test
96      public void truncatedEscape() throws Exception {
97          assertIOException("truncated", "=1");
98      }
99  
100     private static void assertEncoded(String clearText, String encoded) throws Exception {
101         byte[] expected = clearText.getBytes(US_ASCII_CHARSET);
102 
103         ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
104         byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET);
105         QuotedPrintableDecoder.decode(encodedData, out);
106         byte[] actual = out.toByteArray();
107 
108         assertArrayEquals(expected, actual);
109     }
110 
111     private static void assertIOException(String messageText, String encoded) throws UnsupportedEncodingException {
112         ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
113         byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET);
114         try {
115             QuotedPrintableDecoder.decode(encodedData, out);
116             fail("Expected IOException");
117         } catch (IOException e) {
118             String em = e.getMessage();
119             assertTrue("Expected to find " + messageText + " in '" + em + "'",em.contains(messageText));
120         }
121     }
122 
123 }