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  
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 Base64DecoderTestCase {
33  
34      private static final String US_ASCII_CHARSET = "US-ASCII";
35  
36      
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50      @Test
51      public void rfc4648Section10Decode() throws Exception {
52          assertEncoded("", "");
53          assertEncoded("f", "Zg==");
54          assertEncoded("fo", "Zm8=");
55          assertEncoded("foo", "Zm9v");
56          assertEncoded("foob", "Zm9vYg==");
57          assertEncoded("fooba", "Zm9vYmE=");
58          assertEncoded("foobar", "Zm9vYmFy");
59      }
60  
61      
62  
63  
64  
65  
66      @Test
67      public void decodeWithInnerPad() throws Exception {
68          assertEncoded("Hello WorldHello World", "SGVsbG8gV29ybGQ=SGVsbG8gV29ybGQ=");
69      }
70  
71      
72  
73  
74      @Test
75      public void nonBase64Bytes() throws Exception {
76          assertEncoded("Hello World", "S?G!V%sbG 8g\rV\t\n29ybGQ*=");
77      }
78  
79      @Test(expected = IOException.class)
80      public void truncatedString() throws Exception {
81          final byte[] x = new byte[]{'n'};
82          Base64Decoder.decode(x, new ByteArrayOutputStream());
83      }
84  
85      @Test
86      public void decodeTrailingJunk() throws Exception {
87          assertEncoded("foobar", "Zm9vYmFy!!!");
88      }
89  
90      
91      @Test
92      public void decodeTrailing1() throws Exception {
93          assertIOException("truncated", "Zm9vYmFy1");
94      }
95  
96      
97      @Test
98      public void decodeTrailing2() throws Exception {
99          assertIOException("truncated", "Zm9vYmFy12");
100     }
101 
102     
103     @Test
104     public void decodeTrailing3() throws Exception {
105         assertIOException("truncated", "Zm9vYmFy123");
106     }
107 
108     @Test
109     public void badPadding() throws Exception {
110         assertIOException("incorrect padding, 4th byte", "Zg=a");
111     }
112 
113     @Test
114     public void badPaddingLeading1() throws Exception {
115         assertIOException("incorrect padding, first two bytes cannot be padding", "=A==");
116     }
117 
118     @Test
119     public void badPaddingLeading2() throws Exception {
120         assertIOException("incorrect padding, first two bytes cannot be padding", "====");
121     }
122 
123     
124     
125     
126     @Test
127     public void badLength() throws Exception {
128         assertIOException("truncated", "Zm8==");
129     }
130 
131     
132     
133     
134     @Test
135     public void nonASCIIcharacter() throws Exception {
136         assertEncoded("f","Zg=À="); 
137         assertEncoded("f","Zg=\u0100=");
138     }
139 
140     private static void assertEncoded(String clearText, String encoded) throws Exception {
141         byte[] expected = clearText.getBytes(US_ASCII_CHARSET);
142 
143         ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
144         byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET);
145         Base64Decoder.decode(encodedData, out);
146         byte[] actual = out.toByteArray();
147 
148         assertArrayEquals(expected, actual);
149     }
150 
151     private static void assertIOException(String messageText, String encoded) throws UnsupportedEncodingException {
152         ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
153         byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET);
154         try {
155             Base64Decoder.decode(encodedData, out);
156             fail("Expected IOException");
157         } catch (IOException e) {
158             String em = e.getMessage();
159             assertTrue("Expected to find " + messageText + " in '" + em + "'",em.contains(messageText));
160         }
161     }
162 
163 }