commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r795228 - in /commons/proper/codec/trunk/src: java/org/apache/commons/codec/binary/ test/org/apache/commons/codec/binary/
Date Fri, 17 Jul 2009 21:10:36 GMT
Author: ggregory
Date: Fri Jul 17 21:10:36 2009
New Revision: 795228

URL: http://svn.apache.org/viewvc?rev=795228&view=rev
Log:
[CODEC-78] Base64 classes: Improve Code Coverage.

Modified:
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.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
    commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64TestData.java

Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java?rev=795228&r1=795227&r2=795228&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java
(original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java
Fri Jul 17 21:10:36 2009
@@ -126,7 +126,7 @@
     public int read(byte b[], int offset, int len) throws IOException {
         if (b == null) {
             throw new NullPointerException();
-        } else if (offset < 0 || len < 0 || offset + len < 0) {
+        } else if (offset < 0 || len < 0) {
             throw new IndexOutOfBoundsException();
         } else if (offset > b.length || offset + len > b.length) {
             throw new IndexOutOfBoundsException();

Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.java?rev=795228&r1=795227&r2=795228&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.java
(original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.java
Fri Jul 17 21:10:36 2009
@@ -115,7 +115,7 @@
     public void write(byte b[], int offset, int len) throws IOException {
         if (b == null) {
             throw new NullPointerException();
-        } else if (offset < 0 || len < 0 || offset + len < 0) {
+        } else if (offset < 0 || len < 0) {
             throw new IndexOutOfBoundsException();
         } else if (offset > b.length || offset + len > b.length) {
             throw new IndexOutOfBoundsException();

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=795228&r1=795227&r2=795228&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
Fri Jul 17 21:10:36 2009
@@ -311,10 +311,24 @@
         }
 
         try {
+            in.read(buf, 0, -1);
+            fail("Expected Base64InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        }
+
+        try {
             in.read(buf, buf.length + 1, 0);
             fail("Base64InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
         } catch (IndexOutOfBoundsException e) {
             // Expected
         }
+
+        try {
+            in.read(buf, buf.length - 1, 2);
+            fail("Base64InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        }        
     }
 }

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=795228&r1=795227&r2=795228&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
Fri Jul 17 21:10:36 2009
@@ -34,6 +34,10 @@
 
     private final static byte[] LF = {(byte) '\n'};
 
+    private static final String STRING_FIXTURE = "Hello World";
+
+    private static final String UTF_8_NAME = "UTF-8";
+
     /**
      * Construct a new instance of this test case.
      * 
@@ -65,23 +69,23 @@
      */
     public void testBase64OutputStreamByChunk() throws Exception {
         // Hello World test.
-        byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes("UTF-8");
-        byte[] decoded = "Hello World".getBytes("UTF-8");
+        byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes(UTF_8_NAME);
+        byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME);
         testByChunk(encoded, decoded, 76, CRLF);
 
         // Single Byte test.
-        encoded = "AA==\r\n".getBytes("UTF-8");
+        encoded = "AA==\r\n".getBytes(UTF_8_NAME);
         decoded = new byte[]{(byte) 0};
         testByChunk(encoded, decoded, 76, CRLF);
 
         // OpenSSL interop test.
-        encoded = Base64TestData.ENCODED.getBytes("UTF-8");
+        encoded = Base64TestData.ENCODED.getBytes(UTF_8_NAME);
         decoded = Base64TestData.DECODED;
         testByChunk(encoded, decoded, 64, LF);
 
         // Single Line test.
         String singleLine = Base64TestData.ENCODED.replaceAll("\n", "");
-        encoded = singleLine.getBytes("UTF-8");
+        encoded = singleLine.getBytes(UTF_8_NAME);
         decoded = Base64TestData.DECODED;
         testByChunk(encoded, decoded, 0, LF);
 
@@ -102,8 +106,8 @@
      */
     public void testBase64OutputStreamByteByByte() throws Exception {
         // Hello World test.
-        byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes("UTF-8");
-        byte[] decoded = "Hello World".getBytes("UTF-8");
+        byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes(UTF_8_NAME);
+        byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME);
         testByteByByte(encoded, decoded, 76, CRLF);
 
         // Single Byte test.
@@ -112,13 +116,13 @@
         testByteByByte(encoded, decoded, 76, CRLF);
 
         // OpenSSL interop test.
-        encoded = Base64TestData.ENCODED.getBytes("UTF-8");
+        encoded = Base64TestData.ENCODED.getBytes(UTF_8_NAME);
         decoded = Base64TestData.DECODED;
         testByteByByte(encoded, decoded, 64, LF);
 
         // Single Line test.
         String singleLine = Base64TestData.ENCODED.replaceAll("\n", "");
-        encoded = singleLine.getBytes("UTF-8");
+        encoded = singleLine.getBytes(UTF_8_NAME);
         decoded = Base64TestData.DECODED;
         testByteByByte(encoded, decoded, 0, LF);
 
@@ -157,7 +161,7 @@
         out.write(decoded);
         out.close();
         byte[] output = byteOut.toByteArray();
-        assertTrue("Streaming base64 encode", Arrays.equals(output, encoded));
+        assertTrue("Streaming chunked base64 encode", Arrays.equals(output, encoded));
 
         // Now let's try decode.
         byteOut = new ByteArrayOutputStream();
@@ -165,7 +169,7 @@
         out.write(encoded);
         out.close();
         output = byteOut.toByteArray();
-        assertTrue("Streaming base64 decode", Arrays.equals(output, decoded));
+        assertTrue("Streaming chunked base64 decode", Arrays.equals(output, decoded));
 
         // I always wanted to do this! (wrap encoder with decoder etc etc).
         byteOut = new ByteArrayOutputStream();
@@ -178,7 +182,7 @@
         out.close();
         output = byteOut.toByteArray();
 
-        assertTrue("Streaming base64 wrap-wrap-wrap!", Arrays.equals(output, decoded));
+        assertTrue("Streaming chunked base64 wrap-wrap-wrap!", Arrays.equals(output, decoded));
     }
 
     /**
@@ -209,7 +213,7 @@
         }
         out.close();
         byte[] output = byteOut.toByteArray();
-        assertTrue("Streaming base64 encode", Arrays.equals(output, encoded));
+        assertTrue("Streaming byte-by-byte base64 encode", Arrays.equals(output, encoded));
 
         // Now let's try decode.
         byteOut = new ByteArrayOutputStream();
@@ -219,7 +223,18 @@
         }
         out.close();
         output = byteOut.toByteArray();
-        assertTrue("Streaming base64 decode", Arrays.equals(output, decoded));
+        assertTrue("Streaming byte-by-byte base64 decode", Arrays.equals(output, decoded));
+
+        // Now let's try decode with tonnes of flushes.
+        byteOut = new ByteArrayOutputStream();
+        out = new Base64OutputStream(byteOut, false);
+        for (int i = 0; i < encoded.length; i++) {
+            out.write(encoded[i]);
+            out.flush();
+        }                      
+        out.close();
+        output = byteOut.toByteArray();
+        assertTrue("Streaming byte-by-byte flush() base64 decode", Arrays.equals(output,
decoded));        
 
         // I always wanted to do this! (wrap encoder with decoder etc etc).
         byteOut = new ByteArrayOutputStream();
@@ -234,7 +249,7 @@
         out.close();
         output = byteOut.toByteArray();
 
-        assertTrue("Streaming base64 wrap-wrap-wrap!", Arrays.equals(output, decoded));
+        assertTrue("Streaming byte-by-byte base64 wrap-wrap-wrap!", Arrays.equals(output,
decoded));
     }
 
     /**
@@ -249,8 +264,15 @@
         Base64OutputStream out = new Base64OutputStream(bout);
 
         try {
-            out.write(buf, -1, 0);
-            fail("Expected Base64OutputStream.write(buf, -1, 0) to throw a IndexOutOfBoundsException");
+            out.write(buf, -1, 1);
+            fail("Expected Base64OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException ioobe) {
+            // Expected
+        }
+
+        try {
+            out.write(buf, 1, -1);
+            fail("Expected Base64OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException");
         } catch (IndexOutOfBoundsException ioobe) {
             // Expected
         }
@@ -261,6 +283,13 @@
         } catch (IndexOutOfBoundsException ioobe) {
             // Expected
         }
+
+        try {
+            out.write(buf, buf.length - 1, 2);
+            fail("Expected Base64OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException ioobe) {
+            // Expected
+        }        
     }
 
     /**

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=795228&r1=795227&r2=795228&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 Fri
Jul 17 21:10:36 2009
@@ -841,6 +841,9 @@
             byte[] decoded = randomData[0];
             byte[] result = Base64.decodeBase64(encoded);
             assertTrue("url-safe i=" + i, Arrays.equals(decoded, result));
+            assertFalse("url-safe i=" + i + " no '='", Base64TestData.bytesContain(encoded,
(byte) '='));
+            assertFalse("url-safe i=" + i + " no '\\'", Base64TestData.bytesContain(encoded,
(byte) '\\'));
+            assertFalse("url-safe i=" + i + " no '+'", Base64TestData.bytesContain(encoded,
(byte) '+'));            
         }
 
     }

Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64TestData.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64TestData.java?rev=795228&r1=795227&r2=795228&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64TestData.java
(original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64TestData.java
Fri Jul 17 21:10:36 2009
@@ -165,7 +165,7 @@
      * @param urlSafe true if encoding be urlSafe
      * @return two byte[] arrays:  [0] = decoded, [1] = encoded 
      */
-    public static byte[][] randomData(int size, boolean urlSafe) {
+    static byte[][] randomData(int size, boolean urlSafe) {
         Random r = new Random();
         byte[] decoded = new byte[size];
         r.nextBytes(decoded);
@@ -173,4 +173,18 @@
         return new byte[][] {decoded, encoded};
     }
 
+    /**
+     * Tests the supplied byte[] array to see if it contains the specified byte c.
+     *
+     * @param bytes byte[] array to test
+     * @param c byte to look for
+     * @return true if bytes contains c, false otherwise
+     */
+    static boolean bytesContain(byte[] bytes, byte c) {
+        for (int i = 0; i < bytes.length; i++) {
+            if (bytes[i] == c) { return true; }
+        }
+        return false;
+    }
+
 }



Mime
View raw message