1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
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  
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          
44          
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  
75  
76  
77  
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 }