View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  
29  package org.apache.hc.client5.http.utils;
30  
31  import org.junit.jupiter.api.Test;
32  
33  import static java.nio.charset.StandardCharsets.US_ASCII;
34  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  import static org.junit.jupiter.api.Assertions.assertNull;
37  
38  public class TestBase64 {
39  
40      public static final char CHAR_ZERO = 0;
41      public static final String EMPTY_STR = "";
42      public static final byte[] EMPTY_BYTES = new byte[0];
43      public static final String NULL_STR = null;
44      public static final byte[] NULL_BYTE_ARRAY = null;
45      public static final String EMOJI = "\uD83D\uDE15";
46      public static final char SPACE = ' ';
47  
48      private final Base64 target = new Base64();
49  
50      @Test
51      void nullHandling() {
52          assertNull(target.decode(NULL_STR));
53          assertNull(target.decode(NULL_BYTE_ARRAY));
54          assertNull(Base64.decodeBase64(NULL_STR));
55          assertNull(Base64.decodeBase64(NULL_BYTE_ARRAY));
56  
57          assertNull(target.encode(NULL_BYTE_ARRAY));
58          assertNull(Base64.encodeBase64(NULL_BYTE_ARRAY));
59          assertNull(Base64.encodeBase64String(NULL_BYTE_ARRAY));
60      }
61  
62      @Test
63      void zeroLength() {
64          assertArrayEquals(EMPTY_BYTES, target.decode(EMPTY_STR));
65          assertArrayEquals(EMPTY_BYTES, target.decode(EMPTY_BYTES));
66          assertArrayEquals(EMPTY_BYTES, Base64.decodeBase64(EMPTY_STR));
67          assertArrayEquals(EMPTY_BYTES, Base64.decodeBase64(EMPTY_BYTES));
68  
69          assertArrayEquals(EMPTY_BYTES, target.encode(EMPTY_BYTES));
70          assertArrayEquals(EMPTY_BYTES, Base64.encodeBase64(EMPTY_BYTES));
71          assertEquals(EMPTY_STR, Base64.encodeBase64String(EMPTY_BYTES));
72      }
73  
74      @Test
75      void validValues() {
76          final byte[] unencodedBytes = "Hello World!".getBytes(US_ASCII);
77          checkDecode(unencodedBytes, "SGVsbG8gV29ybGQh");
78          checkEncode("SGVsbG8gV29ybGQh", unencodedBytes);
79      }
80  
81      @Test
82      void decodeIgnoresEmbeddedInvalidChars() {
83          checkEquivalentDecode(fourOf("A"), " A A A A ");
84          checkEquivalentDecode(fourOf("A"), "AA" + EMOJI + "AA");
85      }
86  
87      @Test
88      void decodeInvalid() {
89          checkDecode(EMPTY_BYTES, fourOf(EMOJI));
90          checkDecode(EMPTY_BYTES, "A");
91          checkDecode(EMPTY_BYTES, "A===");
92          checkDecode(EMPTY_BYTES, fourOf(SPACE));
93          checkDecode(EMPTY_BYTES, fourOf('='));
94          checkDecode(EMPTY_BYTES, fourOf('@'));
95          checkDecode(EMPTY_BYTES, fourOf(CHAR_ZERO));
96      }
97  
98      @Test
99      void decodeUnpadded() {
100         checkEquivalentDecode("AA==", "AA");
101     }
102 
103     private void checkDecode(final byte[] expectedDecoded, final String testInput) {
104         final byte[] decoded = target.decode(testInput);
105         assertArrayEquals(expectedDecoded, decoded);
106     }
107 
108     private void checkEncode(final String expectedEncoded, final byte[] testInput) {
109         final byte[] encoded = target.encode(testInput);
110         assertEquals(expectedEncoded, new String(encoded, US_ASCII));
111     }
112 
113     private void checkEquivalentDecode(final String expectedEquivalentTo, final String testInput) {
114         final byte[] decoded = target.decode(testInput);
115 
116         final byte[] expectedDecoded = java.util.Base64.getDecoder().decode(expectedEquivalentTo);
117         assertArrayEquals(expectedDecoded, decoded);
118     }
119 
120     private static String fourOf(final char c) {
121         final String charStr = String.valueOf(c);
122         return fourOf(charStr);
123     }
124 
125     private static String fourOf(final String str) {
126         return str + str + str + str;
127     }
128 
129 }