Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Base64Test case for some base64 decoding edge cases #644

Merged
merged 3 commits into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public static byte[] encodeBase64(byte[] binaryData) {
* @return String containing Base64 characters or {@code null} for {@code null} input
*/
public static String encodeBase64String(byte[] binaryData) {
if (binaryData == null) {
return null;
}
return BaseEncoding.base64().encode(binaryData);
}

Expand All @@ -66,6 +69,9 @@ public static byte[] encodeBase64URLSafe(byte[] binaryData) {
* @return String containing Base64 characters or {@code null} for {@code null} input
*/
public static String encodeBase64URLSafeString(byte[] binaryData) {
if (binaryData == null) {
return null;
}
return BaseEncoding.base64Url().omitPadding().encode(binaryData);
}

Expand All @@ -88,11 +94,14 @@ public static byte[] decodeBase64(byte[] base64Data) {
* @return Array containing decoded data or {@code null} for {@code null} input
*/
public static byte[] decodeBase64(String base64String) {
if (base64String == null) {
return null;
}
try {
return BaseEncoding.base64().decode(base64String);
} catch (IllegalArgumentException e) {
if (e.getCause() instanceof DecodingException) {
return BaseEncoding.base64Url().decode(base64String);
return BaseEncoding.base64Url().decode(base64String.trim());
}
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.api.client.util;

import java.nio.charset.StandardCharsets;
import junit.framework.TestCase;

/**
* Tests {@link Base64}.
*
* @author Jeff Ching
*/
public class Base64Test extends TestCase {

public void test_decodeBase64_withPadding() {
String encoded = "Zm9vOmJhcg==";
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), StandardCharsets.UTF_8));
}

public void test_decodeBase64_withoutPadding() {
String encoded = "Zm9vOmJhcg";
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), StandardCharsets.UTF_8));
}

public void test_decodeBase64_withTrailingWhitespace() {
// Some internal use cases append extra space characters that apache-commons base64 decoding
// previously handled.
String encoded = "Zm9vOmJhcg==\r\n";
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), StandardCharsets.UTF_8));
}

public void test_decodeBase64_withNullBytes_shouldReturnNull() {
byte[] encoded = null;
assertNull(Base64.decodeBase64(encoded));
}

public void test_decodeBase64_withNull_shouldReturnNull() {
String encoded = null;
assertNull(Base64.decodeBase64(encoded));
}

public void test_encodeBase64URLSafeString_withNull_shouldReturnNull() {
assertNull(Base64.encodeBase64URLSafeString(null));
}

public void test_encodeBase64URLSafe_withNull_shouldReturnNull() {
assertNull(Base64.encodeBase64URLSafe(null));
}

public void test_encodeBase64_withNull_shouldReturnNull() {
assertNull(Base64.encodeBase64(null));
}
}