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  
18  package org.apache.commons.net.util;
19  
20  import static org.junit.Assert.assertArrayEquals;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  import static org.junit.Assert.assertThrows;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  import java.nio.charset.StandardCharsets;
30  
31  import org.junit.Ignore;
32  import org.junit.Test;
33  
34  public class Base64Test {
35  
36      @Test
37      public void testBase64() {
38          final Base64 b64 = new Base64();
39          assertFalse(b64.isUrlSafe());
40      }
41  
42      @Test
43      public void testBase64Boolean() {
44          final Base64 b64 = new Base64(true);
45          assertTrue(b64.isUrlSafe());
46          assertArrayEquals(new byte[] { '\r', '\n' }, b64.getLineSeparator());
47      }
48  
49      @Test
50      public void testBase64Int() {
51          Base64 b64;
52          b64 = new Base64(8);
53          assertFalse(b64.isUrlSafe());
54          assertEquals(8, b64.getLineLength());
55          b64 = new Base64(11);
56          assertEquals(8, b64.getLineLength());
57      }
58  
59      @Test
60      public void testBase64IntByteArray() {
61          final Base64 b64;
62          b64 = new Base64(8, new byte[] {});
63          assertFalse(b64.isUrlSafe());
64          assertArrayEquals(new byte[] {}, b64.getLineSeparator());
65      }
66  
67      @Test
68      public void testBase64IntByteArrayBoolean() {
69          Base64 b64;
70          b64 = new Base64(8, new byte[] {}, false);
71          assertFalse(b64.isUrlSafe());
72          b64 = new Base64(8, new byte[] {}, true);
73          assertTrue(b64.isUrlSafe());
74      }
75  
76      @Test
77      public void testDecodeBase64ByteArray() {
78          final byte[] base64Data = {'b', 'G', 'l', 'n', 'a', 'H', 'Q', 'g', 'd', 'w', '=', '='};
79          final byte[] decoded = Base64.decodeBase64(base64Data);
80          assertEquals("light w", new String(decoded, StandardCharsets.UTF_8));
81      }
82  
83      @Test
84      public void testDecodeBase64String() {
85          final String base64Data = "bGlnaHQgdw==";
86          final byte[] decoded = Base64.decodeBase64(base64Data);
87          assertEquals("light w", new String(decoded, StandardCharsets.UTF_8));
88      }
89  
90      @Test
91      public void testDecodeByteArray() {
92          final byte[] base64Data = {'Z', 'm', '9', 'v', 'Y', 'm', 'F', 'y'};
93          final byte[] decoded = new Base64().decode(base64Data);
94          assertEquals("foobar", new String(decoded, StandardCharsets.UTF_8));
95      }
96  
97      @Test
98      public void testDecodeByteArrayEmpty() {
99          final byte[] base64Data = {};
100         final byte[] decoded = new Base64().decode(base64Data);
101         assertArrayEquals(base64Data, decoded);
102     }
103 
104     @Test
105     public void testDecodeByteArrayNull() {
106         final byte[] decoded = new Base64().decode((byte[]) null);
107         assertNull(decoded);
108     }
109 
110     @Test
111     @Ignore
112     public void testDecodeInteger() {
113         fail("Not yet implemented");
114     }
115 
116     @Test
117     public void testDecodeNullString() {
118         final Base64 base64 = new Base64();
119         assertThrows(NullPointerException.class, () -> base64.decode((String) null));
120     }
121 
122     @Test
123     @Ignore
124     public void testDecodeObject() {
125         fail("Not yet implemented");
126     }
127 
128     @Test
129     public void testDecodeString() {
130         final String base64String = "SGVsbG8gV29ybGQh";
131         final byte[] decoded = new Base64().decode(base64String);
132         assertEquals("Hello World!", new String(decoded));
133     }
134 
135     @Test
136     public void testEncodeBase64ByteArray() {
137         final byte[] binaryData = null;
138         assertArrayEquals(binaryData, Base64.encodeBase64(binaryData));
139     }
140 
141     @Test
142     @Ignore
143     public void testEncodeBase64ByteArrayBoolean() {
144         fail("Not yet implemented");
145     }
146 
147     @Test
148     @Ignore
149     public void testEncodeBase64ByteArrayBooleanBoolean() {
150         fail("Not yet implemented");
151     }
152 
153     @Test
154     public void testEncodeBase64ByteArrayBooleanBooleanInt() {
155         final byte[] binaryData = { '1', '2', '3' };
156         byte[] encoded;
157         encoded = Base64.encodeBase64(binaryData, false, false);
158         assertNotNull(encoded);
159         assertEquals(4, encoded.length);
160         assertThrows(IllegalArgumentException.class, () -> Base64.encodeBase64(binaryData, false, false, 3));
161         encoded = Base64.encodeBase64(binaryData, false, false, 4); // NET-483
162         assertNotNull(encoded);
163         assertEquals(4, encoded.length);
164         encoded = Base64.encodeBase64(binaryData, true, false);
165         assertNotNull(encoded);
166         assertEquals(6, encoded.length); // always adds trailer
167         assertThrows(IllegalArgumentException.class, () -> Base64.encodeBase64(binaryData, true, false, 5));
168         encoded = Base64.encodeBase64(binaryData, true, false, 6);
169         assertNotNull(encoded);
170         assertEquals(6, encoded.length);
171     }
172 
173     @Test
174     public void testEncodeBase64Chunked() {
175         final byte[] bytesToEncode = {'f', 'o', 'o', 'b', 'a', 'r'};
176         final byte[] encodedData = Base64.encodeBase64Chunked(bytesToEncode);
177         assertEquals("Zm9vYmFy\r\n", new String(encodedData, StandardCharsets.UTF_8));
178     }
179 
180     @Test
181     public void testEncodeBase64StringByteArray() {
182         final String stringToEncode = "Many hands make light work.";
183         final String encodedData = Base64.encodeBase64String(stringToEncode.getBytes());
184         assertEquals("TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu\r\n", encodedData);
185     }
186 
187     @Test
188     public void testEncodeBase64StringByteArrayBoolean() {
189         final byte[] bytesToEncode = "light work.".getBytes();
190         final String chunkedResult = Base64.encodeBase64String(bytesToEncode, true);
191         assertEquals("bGlnaHQgd29yay4=\r\n", chunkedResult);
192         final String unchunkedResult = Base64.encodeBase64String(bytesToEncode, false);
193         assertEquals("bGlnaHQgd29yay4=", unchunkedResult);
194     }
195 
196     @Test
197     public void testEncodeBase64StringUnChunked() {
198         final byte[] bytesToEncode = "Many hands make light work.".getBytes();
199         final String encodedData = Base64.encodeBase64StringUnChunked(bytesToEncode);
200         assertEquals("TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu", encodedData);
201     }
202 
203     @Test
204     @Ignore
205     public void testEncodeBase64URLSafe() {
206         fail("Not yet implemented");
207     }
208 
209     @Test
210     @Ignore
211     public void testEncodeBase64URLSafeString() {
212         fail("Not yet implemented");
213     }
214 
215     @Test
216     @Ignore
217     public void testEncodeByteArray() {
218         fail("Not yet implemented");
219     }
220 
221     @Test
222     @Ignore
223     public void testEncodeInteger() {
224         fail("Not yet implemented");
225     }
226 
227     @Test
228     @Ignore
229     public void testEncodeObject() {
230         fail("Not yet implemented");
231     }
232 
233     @Test
234     public void testEncodeToString() {
235         final Base64 base64 = new Base64();
236         final byte[] bytesToEncode = {'l', 'i', 'g', 'h', 't', ' ', 'w', 'o', 'r'};
237         assertEquals("bGlnaHQgd29y\r\n", base64.encodeToString(bytesToEncode));
238     }
239 
240     @Test
241     public void testIsArrayByteBase64() {
242         assertTrue(Base64.isArrayByteBase64(new byte[] { 'b', ' ' }));
243         assertFalse(Base64.isArrayByteBase64(new byte[] { '?' }));
244     }
245 
246     @Test
247     public void testIsBase64() {
248         assertTrue(Base64.isBase64((byte) 'b'));
249         assertFalse(Base64.isBase64((byte) ' '));
250     }
251 
252     @Test
253     @Ignore
254     public void testToIntegerBytes() {
255         fail("Not yet implemented");
256     }
257 
258 }