Skip to content

Commit b8c2d9d

Browse files
committed
[CODEC-77] Base64 bug with empty input (new byte[0])
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@793734 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2f7454a commit b8c2d9d

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/java/org/apache/commons/codec/binary/Base64.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ void encode(byte[] in, int inPos, int inAvail) {
443443
}
444444
break;
445445
}
446-
if (lineLength > 0) {
446+
if (lineLength > 0 && pos > 0) {
447447
System.arraycopy(lineSeparator, 0, buf, pos, lineSeparator.length);
448448
pos += lineSeparator.length;
449449
}

src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public Base64InputStreamTest(String name) {
4242
super(name);
4343
}
4444

45+
/**
46+
* Test the Base64InputStream implementation against empty input.
47+
*
48+
* @throws Exception for some failure scenarios.
49+
*/
50+
public void testBase64EmptyInputStream() throws Exception {
51+
byte[] emptyEncoded = new byte[0];
52+
byte[] emptyDecoded = new byte[0];
53+
testByteByByte(emptyEncoded, emptyDecoded, 76, CRLF);
54+
testByChunk(emptyEncoded, emptyDecoded, 76, CRLF);
55+
}
56+
4557
/**
4658
* Test the Base64InputStream implementation.
4759
*

src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,24 @@ public Base64OutputStreamTest(String name) {
4242
super(name);
4343
}
4444

45+
/**
46+
* Test the Base64OutputStream implementation against empty input.
47+
*
48+
* @throws Exception for some failure scenarios.
49+
*/
50+
public void testBase64EmptyOutputStream() throws Exception {
51+
byte[] emptyEncoded = new byte[0];
52+
byte[] emptyDecoded = new byte[0];
53+
testByteByByte(emptyEncoded, emptyDecoded, 76, CRLF);
54+
testByChunk(emptyEncoded, emptyDecoded, 76, CRLF);
55+
}
56+
4557
/**
4658
* Test the Base64OutputStream implementation
4759
*
4860
* @throws Exception for some failure scenarios.
4961
*/
50-
public void testBase64InputStreamByteByByte() throws Exception {
62+
public void testBase64OutputStreamByteByByte() throws Exception {
5163
// Hello World test.
5264
byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes("UTF-8");
5365
byte[] decoded = "Hello World".getBytes("UTF-8");
@@ -75,7 +87,7 @@ public void testBase64InputStreamByteByByte() throws Exception {
7587
*
7688
* @throws Exception for some failure scenarios.
7789
*/
78-
public void testBase64InputStreamByChunk() throws Exception {
90+
public void testBase64OutputStreamByChunk() throws Exception {
7991
// Hello World test.
8092
byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes("UTF-8");
8193
byte[] decoded = "Hello World".getBytes("UTF-8");

src/test/org/apache/commons/codec/binary/Base64Test.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import junit.framework.TestCase;
3030

3131
/**
32-
* Testcases for Base64 class.
32+
* Test cases for Base64 class.
3333
*
3434
* @author Apache Software Foundation
3535
* @version $Id$
@@ -830,6 +830,19 @@ public void testUUID() throws DecoderException, UnsupportedEncodingException {
830830
}
831831
}
832832

833+
/**
834+
* Test encode and decode of empty byte array.
835+
*/
836+
public void testEmptyBase64() {
837+
byte[] empty = new byte[0];
838+
byte[] result = Base64.encodeBase64(empty);
839+
assertEquals("empty base64 encode", 0, result.length);
840+
841+
empty = new byte[0];
842+
result = Base64.decodeBase64(empty);
843+
assertEquals("empty base64 decode", 0, result.length);
844+
}
845+
833846
// -------------------------------------------------------- Private Methods
834847

835848
private String toString(byte[] data) {

0 commit comments

Comments
 (0)