commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r793734 - in /commons/proper/codec/trunk/src: java/org/apache/commons/codec/binary/ test/org/apache/commons/codec/binary/
Date Mon, 13 Jul 2009 22:33:29 GMT
Author: ggregory
Date: Mon Jul 13 22:33:28 2009
New Revision: 793734

URL: http://svn.apache.org/viewvc?rev=793734&view=rev
Log:
[CODEC-77] Base64 bug with empty input (new byte[0])

Modified:
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java
    commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java
    commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java
    commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java

Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java?rev=793734&r1=793733&r2=793734&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java Mon Jul
13 22:33:28 2009
@@ -443,7 +443,7 @@
                     }
                     break;
             }
-            if (lineLength > 0) {
+            if (lineLength > 0 && pos > 0) {
                 System.arraycopy(lineSeparator, 0, buf, pos, lineSeparator.length);
                 pos += lineSeparator.length;
             }

Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java?rev=793734&r1=793733&r2=793734&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java
(original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java
Mon Jul 13 22:33:28 2009
@@ -43,6 +43,18 @@
     }
 
     /**
+     * Test the Base64InputStream implementation against empty input.
+     *
+     * @throws Exception for some failure scenarios.
+     */    
+    public void testBase64EmptyInputStream() throws Exception {
+        byte[] emptyEncoded = new byte[0];
+        byte[] emptyDecoded = new byte[0];
+        testByteByByte(emptyEncoded, emptyDecoded, 76, CRLF);
+        testByChunk(emptyEncoded, emptyDecoded, 76, CRLF);
+    }
+
+    /**
      * Test the Base64InputStream implementation.
      *
      * @throws Exception for some failure scenarios.

Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java?rev=793734&r1=793733&r2=793734&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java
(original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java
Mon Jul 13 22:33:28 2009
@@ -43,11 +43,23 @@
     }
 
     /**
+     * Test the Base64OutputStream implementation against empty input.
+     *
+     * @throws Exception for some failure scenarios.
+     */
+    public void testBase64EmptyOutputStream() throws Exception {
+        byte[] emptyEncoded = new byte[0];
+        byte[] emptyDecoded = new byte[0];
+        testByteByByte(emptyEncoded, emptyDecoded, 76, CRLF);
+        testByChunk(emptyEncoded, emptyDecoded, 76, CRLF);
+    }    
+
+    /**
      * Test the Base64OutputStream implementation
      *
      * @throws Exception for some failure scenarios.
      */
-    public void testBase64InputStreamByteByByte() throws Exception {
+    public void testBase64OutputStreamByteByByte() throws Exception {
         // Hello World test.
         byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes("UTF-8");
         byte[] decoded = "Hello World".getBytes("UTF-8");
@@ -75,7 +87,7 @@
      *
      * @throws Exception for some failure scenarios.
      */
-    public void testBase64InputStreamByChunk() throws Exception {
+    public void testBase64OutputStreamByChunk() throws Exception {
         // Hello World test.
         byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes("UTF-8");
         byte[] decoded = "Hello World".getBytes("UTF-8");

Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java?rev=793734&r1=793733&r2=793734&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java (original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java Mon
Jul 13 22:33:28 2009
@@ -29,7 +29,7 @@
 import junit.framework.TestCase;
 
 /**
- * Testcases for Base64 class.
+ * Test cases for Base64 class.
  * 
  * @author Apache Software Foundation
  * @version $Id$
@@ -830,6 +830,19 @@
         }
     }
 
+    /**
+     * Test encode and decode of empty byte array.
+     */
+    public void testEmptyBase64() {
+        byte[] empty = new byte[0];
+        byte[] result = Base64.encodeBase64(empty);
+        assertEquals("empty base64 encode", 0, result.length);
+
+        empty = new byte[0];
+        result = Base64.decodeBase64(empty);        
+        assertEquals("empty base64 decode", 0, result.length);        
+    }
+
     // -------------------------------------------------------- Private Methods
 
     private String toString(byte[] data) {



Mime
View raw message