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