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.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
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
89
90 assertEncoded("The quick brown fox jumps over the lazy dog.", "The quick brown fox jumps over the lazy dog.");
91 }
92
93
94
95
96
97
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 }