Skip to content

Commit 91cbfdb

Browse files
authored
Add Base64Test case for some base64 decoding edge cases (#644)
* Add Base64Test case for some base64 decoding edge cases * Preserve decoding behavior for null decodeBase64(null) * Handle encoding with null inputs
1 parent e64f72f commit 91cbfdb

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

google-http-client/src/main/java/com/google/api/client/util/Base64.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public static byte[] encodeBase64(byte[] binaryData) {
4343
* @return String containing Base64 characters or {@code null} for {@code null} input
4444
*/
4545
public static String encodeBase64String(byte[] binaryData) {
46+
if (binaryData == null) {
47+
return null;
48+
}
4649
return BaseEncoding.base64().encode(binaryData);
4750
}
4851

@@ -66,6 +69,9 @@ public static byte[] encodeBase64URLSafe(byte[] binaryData) {
6669
* @return String containing Base64 characters or {@code null} for {@code null} input
6770
*/
6871
public static String encodeBase64URLSafeString(byte[] binaryData) {
72+
if (binaryData == null) {
73+
return null;
74+
}
6975
return BaseEncoding.base64Url().omitPadding().encode(binaryData);
7076
}
7177

@@ -88,11 +94,14 @@ public static byte[] decodeBase64(byte[] base64Data) {
8894
* @return Array containing decoded data or {@code null} for {@code null} input
8995
*/
9096
public static byte[] decodeBase64(String base64String) {
97+
if (base64String == null) {
98+
return null;
99+
}
91100
try {
92101
return BaseEncoding.base64().decode(base64String);
93102
} catch (IllegalArgumentException e) {
94103
if (e.getCause() instanceof DecodingException) {
95-
return BaseEncoding.base64Url().decode(base64String);
104+
return BaseEncoding.base64Url().decode(base64String.trim());
96105
}
97106
throw e;
98107
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.util;
16+
17+
import java.nio.charset.StandardCharsets;
18+
import junit.framework.TestCase;
19+
20+
/**
21+
* Tests {@link Base64}.
22+
*
23+
* @author Jeff Ching
24+
*/
25+
public class Base64Test extends TestCase {
26+
27+
public void test_decodeBase64_withPadding() {
28+
String encoded = "Zm9vOmJhcg==";
29+
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), StandardCharsets.UTF_8));
30+
}
31+
32+
public void test_decodeBase64_withoutPadding() {
33+
String encoded = "Zm9vOmJhcg";
34+
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), StandardCharsets.UTF_8));
35+
}
36+
37+
public void test_decodeBase64_withTrailingWhitespace() {
38+
// Some internal use cases append extra space characters that apache-commons base64 decoding
39+
// previously handled.
40+
String encoded = "Zm9vOmJhcg==\r\n";
41+
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), StandardCharsets.UTF_8));
42+
}
43+
44+
public void test_decodeBase64_withNullBytes_shouldReturnNull() {
45+
byte[] encoded = null;
46+
assertNull(Base64.decodeBase64(encoded));
47+
}
48+
49+
public void test_decodeBase64_withNull_shouldReturnNull() {
50+
String encoded = null;
51+
assertNull(Base64.decodeBase64(encoded));
52+
}
53+
54+
public void test_encodeBase64URLSafeString_withNull_shouldReturnNull() {
55+
assertNull(Base64.encodeBase64URLSafeString(null));
56+
}
57+
58+
public void test_encodeBase64URLSafe_withNull_shouldReturnNull() {
59+
assertNull(Base64.encodeBase64URLSafe(null));
60+
}
61+
62+
public void test_encodeBase64_withNull_shouldReturnNull() {
63+
assertNull(Base64.encodeBase64(null));
64+
}
65+
}

0 commit comments

Comments
 (0)