|
14 | 14 |
|
15 | 15 | package com.google.api.client.util;
|
16 | 16 |
|
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | + |
17 | 19 | import java.nio.charset.StandardCharsets;
|
18 | 20 | import junit.framework.TestCase;
|
19 | 21 |
|
@@ -62,4 +64,73 @@ public void test_encodeBase64URLSafe_withNull_shouldReturnNull() {
|
62 | 64 | public void test_encodeBase64_withNull_shouldReturnNull() {
|
63 | 65 | assertNull(Base64.encodeBase64(null));
|
64 | 66 | }
|
| 67 | + |
| 68 | + public void test_decodeBase64_newline_character_invalid_length() { |
| 69 | + // The RFC 4648 (https://datatracker.ietf.org/doc/html/rfc4648#section-3.3) states that a |
| 70 | + // specification referring to the Base64 encoding may state that it ignores characters outside |
| 71 | + // the base alphabet. |
| 72 | + |
| 73 | + // In Base64 encoding, 3 characters (24 bits) are converted to 4 of 6-bits, each of which is |
| 74 | + // converted to a byte (a character). |
| 75 | + // Base64encode("abc") => "YWJj" (4 characters) |
| 76 | + // Base64encode("def") => "ZGVm" (4 characters) |
| 77 | + // Adding a new line character between them. This should be discarded. |
| 78 | + String encodedString = "YWJj\nZGVm"; |
| 79 | + |
| 80 | + // This is a reference implementation by Apache Commons Codec. It discards the new line |
| 81 | + // characters. |
| 82 | + // assertEquals( |
| 83 | + // "abcdef", |
| 84 | + // new String( |
| 85 | + // org.apache.commons.codec.binary.Base64.decodeBase64(encodedString), |
| 86 | + // StandardCharsets.UTF_8)); |
| 87 | + |
| 88 | + // This is our implementation. Before the |
| 89 | + // https://github.com/googleapis/google-http-java-client/pull/1941/, it was throwing |
| 90 | + // IllegalArgumentException("Invalid length 9"). |
| 91 | + assertEquals("abcdef", new String(Base64.decodeBase64(encodedString), StandardCharsets.UTF_8)); |
| 92 | + } |
| 93 | + |
| 94 | + public void test_decodeBase64_newline_character() { |
| 95 | + // In Base64 encoding, 2 characters (16 bits) are converted to 3 of 6-bits plus the padding |
| 96 | + // character ('="). |
| 97 | + // Base64encode("ab") => "YWI=" (3 characters + padding character) |
| 98 | + // Adding a new line character that should be discarded between them |
| 99 | + String encodedString = "YW\nI="; |
| 100 | + |
| 101 | + // This is a reference implementation by Apache Commons Codec. It discards the new line |
| 102 | + // characters. |
| 103 | + // assertEquals( |
| 104 | + // "ab", |
| 105 | + // new String( |
| 106 | + // org.apache.commons.codec.binary.Base64.decodeBase64(encodedString), |
| 107 | + // StandardCharsets.UTF_8)); |
| 108 | + |
| 109 | + // This is our implementation. Before the fix |
| 110 | + // https://github.com/googleapis/google-http-java-client/pull/1941/, it was throwing |
| 111 | + // IllegalArgumentException("Unrecognized character: 0xa"). |
| 112 | + assertEquals("ab", new String(Base64.decodeBase64(encodedString), StandardCharsets.UTF_8)); |
| 113 | + } |
| 114 | + |
| 115 | + public void test_decodeBase64_plus_and_newline_characters() { |
| 116 | + // The plus sign is 62 in the Base64 table. So it's a valid character in encoded strings. |
| 117 | + // https://datatracker.ietf.org/doc/html/rfc4648#section-4 |
| 118 | + String encodedString = "+\nw=="; |
| 119 | + |
| 120 | + byte[] actual = Base64.decodeBase64(encodedString); |
| 121 | + // Before the fix https://github.com/googleapis/google-http-java-client/pull/1941/, it was |
| 122 | + // throwing IllegalArgumentException("Unrecognized character: +"). |
| 123 | + assertThat(actual).isEqualTo(new byte[] {(byte) 0xfb}); |
| 124 | + } |
| 125 | + |
| 126 | + public void test_decodeBase64_slash_and_newline_characters() { |
| 127 | + // The slash sign is 63 in the Base64 table. So it's a valid character in encoded strings. |
| 128 | + // https://datatracker.ietf.org/doc/html/rfc4648#section-4 |
| 129 | + String encodedString = "/\nw=="; |
| 130 | + |
| 131 | + byte[] actual = Base64.decodeBase64(encodedString); |
| 132 | + // Before the fix https://github.com/googleapis/google-http-java-client/pull/1941/, it was |
| 133 | + // throwing IllegalArgumentException("Unrecognized character: /"). |
| 134 | + assertThat(actual).isEqualTo(new byte[] {(byte) 0xff}); |
| 135 | + } |
65 | 136 | }
|
0 commit comments