Author: ggregory Date: Thu Jul 16 04:01:17 2009 New Revision: 794515 URL: http://svn.apache.org/viewvc?rev=794515&view=rev Log: [CODEC-78] Base64: Improve Code Coverage.
Modified: 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/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=794515&r1=794514&r2=794515&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 Thu Jul 16 04:01:17 2009 @@ -17,7 +17,6 @@ package org.apache.commons.codec.binary; - import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.Arrays; @@ -27,26 +26,34 @@ /** * @author Apache Software Foundation * @version $Id $ + * @since 1.4 */ public class Base64InputStreamTest extends TestCase { private final static byte[] CRLF = {(byte) '\r', (byte) '\n'}; + 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. - * - * @param name Name of the test case + * + * @param name + * Name of the test case */ public Base64InputStreamTest(String name) { super(name); } /** - * Test the Base64InputStream implementation against empty input. - * - * @throws Exception for some failure scenarios. - */ + * Tests 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]; @@ -55,89 +62,103 @@ } /** - * Test the Base64InputStream implementation. - * - * @throws Exception for some failure scenarios. + * Tests the Base64InputStream implementation. + * + * @throws Exception + * for some failure scenarios. */ - public void testBase64InputStreamByteByByte() throws Exception { + public void testBase64InputStreamByChunk() throws Exception { // Hello World test. - byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes("UTF-8"); - byte[] decoded = "Hello World".getBytes("UTF-8"); - testByteByByte(encoded, decoded, 76, CRLF); + 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}; - testByteByByte(encoded, decoded, 76, CRLF); + testByChunk(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); + 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; - testByteByByte(encoded, decoded, 0, LF); + testByChunk(encoded, decoded, 0, LF); + + // test random data of sizes 0 thru 150 + for (int i = 0; i <= 150; i++) { + byte[][] randomData = Base64TestData.randomData(i, false); + encoded = randomData[1]; + decoded = randomData[0]; + testByChunk(encoded, decoded, 0, LF); + } } /** - * Test the Base64InputStream implementation. - * - * @throws Exception for some failure scenarios. + * Tests the Base64InputStream implementation. + * + * @throws Exception + * for some failure scenarios. */ - public void testBase64InputStreamByChunk() throws Exception { + public void testBase64InputStreamByteByByte() throws Exception { // Hello World test. - byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes("UTF-8"); - byte[] decoded = "Hello World".getBytes("UTF-8"); - testByChunk(encoded, decoded, 76, CRLF); + 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. - 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); + testByteByByte(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); + 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; - testByChunk(encoded, decoded, 0, LF); - } + testByteByByte(encoded, decoded, 0, LF); + // test random data of sizes 0 thru 150 + for (int i = 0; i <= 150; i++) { + byte[][] randomData = Base64TestData.randomData(i, false); + encoded = randomData[1]; + decoded = randomData[0]; + testByteByByte(encoded, decoded, 0, LF); + } + } /** - * Test method does three tests on the supplied data: - * 1. encoded ---[DECODE]--> decoded - * 2. decoded ---[ENCODE]--> encoded - * 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded + * Tests method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]--> + * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded * <p/> - * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the - * Base64InputStream wraps itself in encode and decode mode + * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base64InputStream wraps itself in encode and decode mode * over and over again. - * - * @param encoded base64 encoded data - * @param decoded the data from above, but decoded - * @param chunkSize chunk size (line-length) of the base64 encoded data. - * @param seperator Line separator in the base64 encoded data. - * @throws Exception Usually signifies a bug in the Base64 commons-codec implementation. - */ - private void testByteByByte( - byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator - ) throws Exception { + * + * @param encoded + * base64 encoded data + * @param decoded + * the data from above, but decoded + * @param chunkSize + * chunk size (line-length) of the base64 encoded data. + * @param seperator + * Line separator in the base64 encoded data. + * @throws Exception + * Usually signifies a bug in the Base64 commons-codec implementation. + */ + private void testByChunk(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception { // Start with encode. InputStream in = new ByteArrayInputStream(decoded); in = new Base64InputStream(in, true, chunkSize, seperator); - byte[] output = new byte[encoded.length]; - for (int i = 0; i < output.length; i++) { - output[i] = (byte) in.read(); - } + byte[] output = Base64TestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -146,25 +167,19 @@ // Now let's try decode. in = new ByteArrayInputStream(encoded); in = new Base64InputStream(in); - output = new byte[decoded.length]; - for (int i = 0; i < output.length; i++) { - output[i] = (byte) in.read(); - } + output = Base64TestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); assertTrue("Streaming base64 decode", Arrays.equals(output, decoded)); - // I always wanted to do this! (wrap encoder with decoder etc etc). + // I always wanted to do this! (wrap encoder with decoder etc etc). in = new ByteArrayInputStream(decoded); for (int i = 0; i < 10; i++) { in = new Base64InputStream(in, true, chunkSize, seperator); in = new Base64InputStream(in, false); } - output = new byte[decoded.length]; - for (int i = 0; i < output.length; i++) { - output[i] = (byte) in.read(); - } + output = Base64TestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -172,29 +187,32 @@ } /** - * Test method does three tests on the supplied data: - * 1. encoded ---[DECODE]--> decoded - * 2. decoded ---[ENCODE]--> encoded - * 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded + * Tests method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]--> + * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded * <p/> - * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the - * Base64InputStream wraps itself in encode and decode mode + * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base64InputStream wraps itself in encode and decode mode * over and over again. - * - * @param encoded base64 encoded data - * @param decoded the data from above, but decoded - * @param chunkSize chunk size (line-length) of the base64 encoded data. - * @param seperator Line separator in the base64 encoded data. - * @throws Exception Usually signifies a bug in the Base64 commons-codec implementation. - */ - private void testByChunk( - byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator - ) throws Exception { + * + * @param encoded + * base64 encoded data + * @param decoded + * the data from above, but decoded + * @param chunkSize + * chunk size (line-length) of the base64 encoded data. + * @param seperator + * Line separator in the base64 encoded data. + * @throws Exception + * Usually signifies a bug in the Base64 commons-codec implementation. + */ + private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception { // Start with encode. InputStream in = new ByteArrayInputStream(decoded); in = new Base64InputStream(in, true, chunkSize, seperator); - byte[] output = Base64TestData.streamToBytes(in); + byte[] output = new byte[encoded.length]; + for (int i = 0; i < output.length; i++) { + output[i] = (byte) in.read(); + } assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -203,22 +221,100 @@ // Now let's try decode. in = new ByteArrayInputStream(encoded); in = new Base64InputStream(in); - output = Base64TestData.streamToBytes(in); + output = new byte[decoded.length]; + for (int i = 0; i < output.length; i++) { + output[i] = (byte) in.read(); + } assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); assertTrue("Streaming base64 decode", Arrays.equals(output, decoded)); - // I always wanted to do this! (wrap encoder with decoder etc etc). + // I always wanted to do this! (wrap encoder with decoder etc etc). in = new ByteArrayInputStream(decoded); for (int i = 0; i < 10; i++) { in = new Base64InputStream(in, true, chunkSize, seperator); in = new Base64InputStream(in, false); } - output = Base64TestData.streamToBytes(in); + output = new byte[decoded.length]; + for (int i = 0; i < output.length; i++) { + output[i] = (byte) in.read(); + } assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); assertTrue("Streaming base64 wrap-wrap-wrap!", Arrays.equals(output, decoded)); } + + /** + * Tests markSupported. + * + * @throws Exception + */ + public void testMarkSupported() throws Exception { + byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME); + ByteArrayInputStream bin = new ByteArrayInputStream(decoded); + Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0}); + // Always returns false for now. + assertFalse("Base64InputStream.markSupported() is false", in.markSupported()); + } + + /** + * Tests read returning 0 + * + * @throws Exception + */ + public void testRead0() throws Exception { + byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME); + byte[] buf = new byte[1024]; + int bytesRead = 0; + ByteArrayInputStream bin = new ByteArrayInputStream(decoded); + Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0}); + bytesRead = in.read(buf, 0, 0); + assertEquals("Base64InputStream.read(buf, 0, 0) returns 0", 0, bytesRead); + } + + /** + * Tests read with null. + * + * @throws Exception + * for some failure scenarios. + */ + public void testReadNull() throws Exception { + byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME); + ByteArrayInputStream bin = new ByteArrayInputStream(decoded); + Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0}); + try { + in.read(null, 0, 0); + fail("Base64InputStream.read(null, 0, 0) to throw a NullPointerException"); + } catch (NullPointerException e) { + // Expected + } + } + + /** + * Tests read throwing IndexOutOfBoundsException + * + * @throws Exception + */ + public void testReadOutOfBounds() throws Exception { + byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME); + byte[] buf = new byte[1024]; + ByteArrayInputStream bin = new ByteArrayInputStream(decoded); + Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0}); + + try { + in.read(buf, -1, 0); + fail("Expected Base64InputStream.read(buf, -1, 0) 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 + } + } } 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=794515&r1=794514&r2=794515&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 Thu Jul 16 04:01:17 2009 @@ -17,7 +17,6 @@ package org.apache.commons.codec.binary; - import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.util.Arrays; @@ -27,16 +26,19 @@ /** * @author Apache Software Foundation * @version $Id $ + * @since 1.4 */ public class Base64OutputStreamTest extends TestCase { private final static byte[] CRLF = {(byte) '\r', (byte) '\n'}; + private final static byte[] LF = {(byte) '\n'}; /** * Construct a new instance of this test case. - * - * @param name Name of the test case + * + * @param name + * Name of the test case */ public Base64OutputStreamTest(String name) { super(name); @@ -44,99 +46,115 @@ /** * Test the Base64OutputStream implementation against empty input. - * - * @throws Exception for some failure scenarios. + * + * @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. + * + * @throws Exception + * for some failure scenarios. */ - public void testBase64OutputStreamByteByByte() 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"); - testByteByByte(encoded, decoded, 76, CRLF); + testByChunk(encoded, decoded, 76, CRLF); // Single Byte test. encoded = "AA==\r\n".getBytes("UTF-8"); decoded = new byte[]{(byte) 0}; - testByteByByte(encoded, decoded, 76, CRLF); + testByChunk(encoded, decoded, 76, CRLF); // OpenSSL interop test. encoded = Base64TestData.ENCODED.getBytes("UTF-8"); decoded = Base64TestData.DECODED; - testByteByByte(encoded, decoded, 64, LF); + testByChunk(encoded, decoded, 64, LF); // Single Line test. String singleLine = Base64TestData.ENCODED.replaceAll("\n", ""); encoded = singleLine.getBytes("UTF-8"); decoded = Base64TestData.DECODED; - testByteByByte(encoded, decoded, 0, LF); + testByChunk(encoded, decoded, 0, LF); + + // test random data of sizes 0 thru 150 + for (int i = 0; i <= 150; i++) { + byte[][] randomData = Base64TestData.randomData(i, false); + encoded = randomData[1]; + decoded = randomData[0]; + testByChunk(encoded, decoded, 0, LF); + } } /** * Test the Base64OutputStream implementation - * - * @throws Exception for some failure scenarios. + * + * @throws Exception + * for some failure scenarios. */ - public void testBase64OutputStreamByChunk() 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"); - testByChunk(encoded, decoded, 76, CRLF); + testByteByByte(encoded, decoded, 76, CRLF); // Single Byte test. encoded = "AA==\r\n".getBytes("UTF-8"); decoded = new byte[]{(byte) 0}; - testByChunk(encoded, decoded, 76, CRLF); + testByteByByte(encoded, decoded, 76, CRLF); // OpenSSL interop test. encoded = Base64TestData.ENCODED.getBytes("UTF-8"); decoded = Base64TestData.DECODED; - testByChunk(encoded, decoded, 64, LF); + testByteByByte(encoded, decoded, 64, LF); // Single Line test. String singleLine = Base64TestData.ENCODED.replaceAll("\n", ""); encoded = singleLine.getBytes("UTF-8"); decoded = Base64TestData.DECODED; - testByChunk(encoded, decoded, 0, LF); - } + testByteByByte(encoded, decoded, 0, LF); + // test random data of sizes 0 thru 150 + for (int i = 0; i <= 150; i++) { + byte[][] randomData = Base64TestData.randomData(i, false); + encoded = randomData[1]; + decoded = randomData[0]; + testByteByByte(encoded, decoded, 0, LF); + } + } /** - * Test method does three tests on the supplied data: - * 1. encoded ---[DECODE]--> decoded - * 2. decoded ---[ENCODE]--> encoded - * 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded + * Test method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]--> + * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded * <p/> - * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the - * Base64OutputStream wraps itself in encode and decode mode - * over and over again. - * - * @param encoded base64 encoded data - * @param decoded the data from above, but decoded - * @param chunkSize chunk size (line-length) of the base64 encoded data. - * @param seperator Line separator in the base64 encoded data. - * @throws Exception Usually signifies a bug in the Base64 commons-codec implementation. - */ - private void testByteByByte( - byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator - ) throws Exception { + * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base64OutputStream wraps itself in encode and decode + * mode over and over again. + * + * @param encoded + * base64 encoded data + * @param decoded + * the data from above, but decoded + * @param chunkSize + * chunk size (line-length) of the base64 encoded data. + * @param seperator + * Line separator in the base64 encoded data. + * @throws Exception + * Usually signifies a bug in the Base64 commons-codec implementation. + */ + private void testByChunk(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception { // Start with encode. ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); OutputStream out = new Base64OutputStream(byteOut, true, chunkSize, seperator); - for (int i = 0; i < decoded.length; i++) { - out.write(decoded[i]); - } + out.write(decoded); out.close(); byte[] output = byteOut.toByteArray(); assertTrue("Streaming base64 encode", Arrays.equals(output, encoded)); @@ -144,23 +162,19 @@ // Now let's try decode. byteOut = new ByteArrayOutputStream(); out = new Base64OutputStream(byteOut, false); - for (int i = 0; i < encoded.length; i++) { - out.write(encoded[i]); - } + out.write(encoded); out.close(); output = byteOut.toByteArray(); assertTrue("Streaming base64 decode", Arrays.equals(output, decoded)); - // I always wanted to do this! (wrap encoder with decoder etc etc). + // I always wanted to do this! (wrap encoder with decoder etc etc). byteOut = new ByteArrayOutputStream(); out = byteOut; for (int i = 0; i < 10; i++) { out = new Base64OutputStream(out, false); out = new Base64OutputStream(out, true, chunkSize, seperator); } - for (int i = 0; i < decoded.length; i++) { - out.write(decoded[i]); - } + out.write(decoded); out.close(); output = byteOut.toByteArray(); @@ -168,29 +182,31 @@ } /** - * Test method does three tests on the supplied data: - * 1. encoded ---[DECODE]--> decoded - * 2. decoded ---[ENCODE]--> encoded - * 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded + * Test method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]--> + * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded * <p/> - * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the - * Base64OutputStream wraps itself in encode and decode mode - * over and over again. - * - * @param encoded base64 encoded data - * @param decoded the data from above, but decoded - * @param chunkSize chunk size (line-length) of the base64 encoded data. - * @param seperator Line separator in the base64 encoded data. - * @throws Exception Usually signifies a bug in the Base64 commons-codec implementation. - */ - private void testByChunk( - byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator - ) throws Exception { + * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base64OutputStream wraps itself in encode and decode + * mode over and over again. + * + * @param encoded + * base64 encoded data + * @param decoded + * the data from above, but decoded + * @param chunkSize + * chunk size (line-length) of the base64 encoded data. + * @param seperator + * Line separator in the base64 encoded data. + * @throws Exception + * Usually signifies a bug in the Base64 commons-codec implementation. + */ + private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception { // Start with encode. ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); OutputStream out = new Base64OutputStream(byteOut, true, chunkSize, seperator); - out.write(decoded); + for (int i = 0; i < decoded.length; i++) { + out.write(decoded[i]); + } out.close(); byte[] output = byteOut.toByteArray(); assertTrue("Streaming base64 encode", Arrays.equals(output, encoded)); @@ -198,23 +214,70 @@ // Now let's try decode. byteOut = new ByteArrayOutputStream(); out = new Base64OutputStream(byteOut, false); - out.write(encoded); + for (int i = 0; i < encoded.length; i++) { + out.write(encoded[i]); + } out.close(); output = byteOut.toByteArray(); assertTrue("Streaming base64 decode", Arrays.equals(output, decoded)); - // I always wanted to do this! (wrap encoder with decoder etc etc). + // I always wanted to do this! (wrap encoder with decoder etc etc). byteOut = new ByteArrayOutputStream(); out = byteOut; for (int i = 0; i < 10; i++) { out = new Base64OutputStream(out, false); out = new Base64OutputStream(out, true, chunkSize, seperator); } - out.write(decoded); + for (int i = 0; i < decoded.length; i++) { + out.write(decoded[i]); + } out.close(); output = byteOut.toByteArray(); assertTrue("Streaming base64 wrap-wrap-wrap!", Arrays.equals(output, decoded)); } + /** + * Tests Base64OutputStream.write for expected IndexOutOfBoundsException conditions. + * + * @throws Exception + * for some failure scenarios. + */ + public void testWriteOutOfBounds() throws Exception { + byte[] buf = new byte[1024]; + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + Base64OutputStream out = new Base64OutputStream(bout); + + try { + out.write(buf, -1, 0); + fail("Expected Base64OutputStream.write(buf, -1, 0) to throw a IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException ioobe) { + // Expected + } + + try { + out.write(buf, buf.length + 1, 0); + fail("Expected Base64OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException ioobe) { + // Expected + } + } + + /** + * Tests Base64OutputStream.write(null). + * + * @throws Exception + * for some failure scenarios. + */ + public void testWriteToNullCoverage() throws Exception { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + Base64OutputStream out = new Base64OutputStream(bout); + try { + out.write(null, 0, 0); + fail("Expcted Base64OutputStream.write(null) to throw a NullPointerException"); + } catch (NullPointerException e) { + // 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=794515&r1=794514&r2=794515&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 Thu Jul 16 04:01:17 2009 @@ -723,27 +723,27 @@ try { base64 = new Base64(-1,new byte[]{'A'}); fail("Should have rejected attempt to use 'A' as a line separator"); - } catch (IllegalArgumentException ignored){ - + } catch (IllegalArgumentException ignored) { + // Expected } try { base64 = new Base64(64,new byte[]{'A'}); fail("Should have rejected attempt to use 'A' as a line separator"); - } catch (IllegalArgumentException ignored){ - + } catch (IllegalArgumentException ignored) { + // Expected } try { base64 = new Base64(64,new byte[]{'='}); fail("Should have rejected attempt to use '=' as a line separator"); - } catch (IllegalArgumentException ignored){ - + } catch (IllegalArgumentException ignored) { + // Expected } base64 = new Base64(64,new byte[]{'$'}); // OK try { base64 = new Base64(64,new byte[]{'A','$'}); fail("Should have rejected attempt to use 'A$' as a line separator"); - } catch (IllegalArgumentException ignored){ - + } catch (IllegalArgumentException ignored) { + // Expected } base64 = new Base64(64,new byte[]{' ','$','\n','\r','\t'}); // OK } @@ -831,16 +831,47 @@ } /** + * Tests url-safe Base64 against random data, sizes 0 to 150. + */ + public void testUrlSafe() { + // test random data of sizes 0 thru 150 + for (int i = 0; i <= 150; i++) { + byte[][] randomData = Base64TestData.randomData(i, true); + byte[] encoded = randomData[1]; + byte[] decoded = randomData[0]; + byte[] result = Base64.decodeBase64(encoded); + assertTrue("url-safe i=" + i, Arrays.equals(decoded, result)); + } + + } + + /** + * Tests isUrlSafe. + */ + public void testIsUrlSafe() { + Base64 base64Standard = new Base64(false); + Base64 base64URLSafe = new Base64(true); + + assertFalse("Base64.isUrlSafe=false", base64Standard.isUrlSafe()); + assertTrue("Base64.isUrlSafe=true", base64URLSafe.isUrlSafe()); + + byte[] whiteSpace = {' ', '\n', '\r', '\t'}; + assertTrue("Base64.isArrayByteBase64(whiteSpace)=true", Base64.isArrayByteBase64(whiteSpace)); + } + + /** * 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); - + assertEquals("empty base64 encode", null, Base64.encodeBase64(null)); + empty = new byte[0]; result = Base64.decodeBase64(empty); assertEquals("empty base64 decode", 0, result.length); + assertEquals("empty base64 encode", null, Base64.decodeBase64(null)); } // -------------------------------------------------------- Private Methods 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=794515&r1=794514&r2=794515&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 Thu Jul 16 04:01:17 2009 @@ -19,14 +19,15 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Random; /** - * This random data was encoded by OpenSSL. Java had nothing to do with it. - * This data helps us test interop between Commons-Codec and OpenSSL. Notice - * that OpenSSL creates 64 character lines instead of the 76 of Commons-Codec. - * + * This random data was encoded by OpenSSL. Java had nothing to do with it. This data helps us test interop between + * Commons-Codec and OpenSSL. Notice that OpenSSL creates 64 character lines instead of the 76 of Commons-Codec. + * * @author Apache Software Foundation * @version $Id $ + * @since 1.4 */ public class Base64TestData { @@ -156,4 +157,20 @@ return biggerBytes; } + + /** + * Returns an encoded and decoded copy of the same random data. + * + * @param size amount of random data to generate and encode + * @param urlSafe true if encoding be urlSafe + * @return two byte[] arrays: [0] = decoded, [1] = encoded + */ + public static byte[][] randomData(int size, boolean urlSafe) { + Random r = new Random(); + byte[] decoded = new byte[size]; + r.nextBytes(decoded); + byte[] encoded = urlSafe ? Base64.encodeBase64URLSafe(decoded) : Base64.encodeBase64(decoded); + return new byte[][] {decoded, encoded}; + } + }