|
| 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