commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1063796 - in /commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary: Base32InputStreamTest.java Base32OutputStreamTest.java Base32TestData.java
Date Wed, 26 Jan 2011 16:59:16 GMT
Author: sebb
Date: Wed Jan 26 16:59:16 2011
New Revision: 1063796

URL: http://svn.apache.org/viewvc?rev=1063796&view=rev
Log:
SOme basic Base32 I/O tests

Added:
    commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32InputStreamTest.java
  (with props)
    commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32OutputStreamTest.java
  (with props)
    commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32TestData.java
  (with props)

Added: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32InputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32InputStreamTest.java?rev=1063796&view=auto
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32InputStreamTest.java
(added)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32InputStreamTest.java
Wed Jan 26 16:59:16 2011
@@ -0,0 +1,412 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.codec.binary;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+public class Base32InputStreamTest 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
+     */
+    public Base32InputStreamTest(String name) {
+        super(name);
+    }
+
+    /**
+     * Tests the bug reported in CODEC-105. Bad interactions with InputStream when reading
one byte at a time.
+     */
+    public void testCodec105() throws IOException {
+        Base32InputStream in = new Base32InputStream(new Codec105ErrorInputStream(), true,
0, null);
+        for (int i = 0; i < 5; i++) {
+            in.read();
+        }
+    }
+
+//    /**
+//     * Test for the CODEC-101 bug:  InputStream.read(byte[]) should never return 0
+//     * because Java's builtin InputStreamReader hates that.
+//     *
+//     * @throws Exception for some failure scenarios.
+//     */
+//    public void testCodec101() throws Exception {
+//        byte[] codec101 = StringUtils.getBytesUtf8(Base32TestData.CODEC_101_MULTIPLE_OF_3);
+//        ByteArrayInputStream bais = new ByteArrayInputStream(codec101);
+//        Base32InputStream in = new Base32InputStream(bais);
+//        byte[] result = new byte[8192];
+//        int c = in.read(result);
+//        assertTrue("Codec101: First read successful [c=" + c + "]", c > 0);
+//
+//        c = in.read(result);
+//        assertTrue("Codec101: Second read should report end-of-stream [c=" + c + "]", c
< 0);
+//    }
+
+    /**
+     * Another test for the CODEC-101 bug:
+     * In commons-codec-1.4 this test shows InputStreamReader explicitly hating an
+     * InputStream.read(byte[]) return of 0:
+     *
+     * java.io.IOException: Underlying input stream returned zero bytes
+     * at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:268)
+     * at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
+     * at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
+     * at java.io.InputStreamReader.read(InputStreamReader.java:167)
+     * at java.io.BufferedReader.fill(BufferedReader.java:136)
+     * at java.io.BufferedReader.readLine(BufferedReader.java:299)
+     * at java.io.BufferedReader.readLine(BufferedReader.java:362)
+     * at org.apache.commons.codec.binary.Base32InputStreamTest.testInputStreamReader(Base32InputStreamTest.java:75)
+     *
+     * But in commons-codec-1.5 it's fixed.  :-)
+     *
+     * @throws Exception for some failure scenarios.
+     */
+//    public void testInputStreamReader() throws Exception {
+//        byte[] codec101 = StringUtils.getBytesUtf8(Base32TestData.CODEC_101_MULTIPLE_OF_3);
+//        ByteArrayInputStream bais = new ByteArrayInputStream(codec101);
+//        Base32InputStream in = new Base32InputStream(bais);
+//        InputStreamReader isr = new InputStreamReader(in);
+//        BufferedReader br = new BufferedReader(isr);
+//        String line = br.readLine();
+//        assertNotNull("Codec101:  InputStreamReader works!", line);
+//    }
+
+    /**
+     * Test the Base32InputStream implementation against the special NPE inducing input
+     * identified in the CODEC-98 bug.
+     *
+     * @throws Exception for some failure scenarios.
+     */
+//    public void testCodec98NPE() throws Exception {
+//        byte[] codec98 = StringUtils.getBytesUtf8(Base32TestData.CODEC_98_NPE);
+//        ByteArrayInputStream data = new ByteArrayInputStream(codec98);
+//        Base32InputStream stream = new Base32InputStream(data);
+//
+//        // This line causes an NPE in commons-codec-1.4.jar:
+//        byte[] decodedBytes = Base32TestData.streamToBytes(stream, new byte[1024]);
+//
+//        String decoded = StringUtils.newStringUtf8(decodedBytes);
+//        assertEquals(
+//            "codec-98 NPE Base32InputStream", Base32TestData.CODEC_98_NPE_DECODED, decoded
+//        );
+//    }
+
+    /**
+     * Tests the Base32InputStream implementation against empty input.
+     * 
+     * @throws Exception
+     *             for some failure scenarios.
+     */
+    public void testBase32EmptyInputStreamMimeChuckSize() throws Exception {
+        testBase32EmptyInputStream(Base32.MIME_CHUNK_SIZE);
+    }
+
+    /**
+     * Tests the Base32InputStream implementation against empty input.
+     * 
+     * @throws Exception
+     *             for some failure scenarios.
+     */
+    public void testBase32EmptyInputStreamPemChuckSize() throws Exception {
+        testBase32EmptyInputStream(Base32.PEM_CHUNK_SIZE);
+    }
+
+    private void testBase32EmptyInputStream(int chuckSize) throws Exception {
+        byte[] emptyEncoded = new byte[0];
+        byte[] emptyDecoded = new byte[0];
+        testByteByByte(emptyEncoded, emptyDecoded, chuckSize, CRLF);
+        testByChunk(emptyEncoded, emptyDecoded, chuckSize, CRLF);
+    }
+
+    /**
+     * Tests the Base32InputStream implementation.
+     * 
+     * @throws Exception
+     *             for some failure scenarios.
+     */
+    public void testBase32InputStreamByChunk() throws Exception {
+        // Hello World test.
+        byte[] encoded = StringUtils.getBytesUtf8(Base32TestData.BASE32_FIXTURE);
+        byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
+        testByChunk(encoded, decoded, Base32.MIME_CHUNK_SIZE, CRLF);
+
+        // Single Byte test.
+        encoded = StringUtils.getBytesUtf8("AA==\r\n");
+        decoded = new byte[]{(byte) 0};
+//        testByChunk(encoded, decoded, Base32.MIME_CHUNK_SIZE, CRLF);
+
+//        // OpenSSL interop test.
+//        encoded = StringUtils.getBytesUtf8(Base32TestData.ENCODED_32_CHARS_PER_LINE);
+//        decoded = Base32TestData.DECODED;
+//        testByChunk(encoded, decoded, Base32.PEM_CHUNK_SIZE, LF);
+//
+//        // Single Line test.
+//        String singleLine = Base32TestData.ENCODED_32_CHARS_PER_LINE.replaceAll("\n", "");
+//        encoded = StringUtils.getBytesUtf8(singleLine);
+//        decoded = Base32TestData.DECODED;
+//        testByChunk(encoded, decoded, 0, LF);
+
+        // test random data of sizes 0 thru 150
+        for (int i = 0; i <= 150; i++) {
+            byte[][] randomData = Base32TestData.randomData(i, false);
+            encoded = randomData[1];
+            decoded = randomData[0];
+            testByChunk(encoded, decoded, 0, LF);
+        }
+    }
+
+    /**
+     * Tests the Base32InputStream implementation.
+     * 
+     * @throws Exception
+     *             for some failure scenarios.
+     */
+    public void testBase32InputStreamByteByByte() throws Exception {
+        // Hello World test.
+        byte[] encoded = StringUtils.getBytesUtf8(Base32TestData.BASE32_FIXTURE);
+        byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
+        testByteByByte(encoded, decoded, Base32.MIME_CHUNK_SIZE, CRLF);
+
+        // Single Byte test.
+        encoded = StringUtils.getBytesUtf8("AA==\r\n");
+        decoded = new byte[]{(byte) 0};
+//        testByteByByte(encoded, decoded, Base32.MIME_CHUNK_SIZE, CRLF);
+
+//        // Single Line test.
+//        String singleLine = Base32TestData.ENCODED_32_CHARS_PER_LINE.replaceAll("\n", "");
+//        encoded = StringUtils.getBytesUtf8(singleLine);
+//        decoded = Base32TestData.DECODED;
+//        testByteByByte(encoded, decoded, 0, LF);
+
+        // test random data of sizes 0 thru 150
+        for (int i = 0; i <= 150; i++) {
+            byte[][] randomData = Base32TestData.randomData(i, false);
+            encoded = randomData[1];
+            decoded = randomData[0];
+            testByteByByte(encoded, decoded, 0, LF);
+        }
+    }
+
+    /**
+     * 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 Base32InputStream wraps itself
in encode and decode mode
+     * over and over again.
+     * 
+     * @param encoded
+     *            base32 encoded data
+     * @param decoded
+     *            the data from above, but decoded
+     * @param chunkSize
+     *            chunk size (line-length) of the base32 encoded data.
+     * @param seperator
+     *            Line separator in the base32 encoded data.
+     * @throws Exception
+     *             Usually signifies a bug in the Base32 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 Base32InputStream(in, true, chunkSize, seperator);
+        byte[] output = Base32TestData.streamToBytes(in);
+
+        assertEquals("EOF", -1, in.read());
+        assertEquals("Still EOF", -1, in.read());
+        assertTrue("Streaming base32 encode", Arrays.equals(output, encoded));
+
+        // Now let's try decode.
+        in = new ByteArrayInputStream(encoded);
+        in = new Base32InputStream(in);
+        output = Base32TestData.streamToBytes(in);
+
+        assertEquals("EOF", -1, in.read());
+        assertEquals("Still EOF", -1, in.read());
+        assertTrue("Streaming base32 decode", Arrays.equals(output, decoded));
+
+        // 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 Base32InputStream(in, true, chunkSize, seperator);
+            in = new Base32InputStream(in, false);
+        }
+        output = Base32TestData.streamToBytes(in);
+
+        assertEquals("EOF", -1, in.read());
+        assertEquals("Still EOF", -1, in.read());
+        assertTrue("Streaming base32 wrap-wrap-wrap!", Arrays.equals(output, 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 Base32InputStream wraps itself
in encode and decode mode
+     * over and over again.
+     * 
+     * @param encoded
+     *            base32 encoded data
+     * @param decoded
+     *            the data from above, but decoded
+     * @param chunkSize
+     *            chunk size (line-length) of the base32 encoded data.
+     * @param seperator
+     *            Line separator in the base32 encoded data.
+     * @throws Exception
+     *             Usually signifies a bug in the Base32 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 Base32InputStream(in, true, chunkSize, seperator);
+        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());
+        assertTrue("Streaming base32 encode", Arrays.equals(output, encoded));
+
+        // Now let's try decode.
+        in = new ByteArrayInputStream(encoded);
+        in = new Base32InputStream(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 base32 decode", Arrays.equals(output, decoded));
+
+        // 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 Base32InputStream(in, true, chunkSize, seperator);
+            in = new Base32InputStream(in, false);
+        }
+        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 base32 wrap-wrap-wrap!", Arrays.equals(output, decoded));
+    }
+
+    /**
+     * Tests markSupported.
+     * 
+     * @throws Exception
+     */
+    public void testMarkSupported() throws Exception {
+        byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
+        ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
+        Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[]{0, 0, 0});
+        // Always returns false for now.
+        assertFalse("Base32InputStream.markSupported() is false", in.markSupported());
+    }
+
+    /**
+     * Tests read returning 0
+     * 
+     * @throws Exception
+     */
+    public void testRead0() throws Exception {
+        byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
+        byte[] buf = new byte[1024];
+        int bytesRead = 0;
+        ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
+        Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[]{0, 0, 0});
+        bytesRead = in.read(buf, 0, 0);
+        assertEquals("Base32InputStream.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 = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
+        ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
+        Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[]{0, 0, 0});
+        try {
+            in.read(null, 0, 0);
+            fail("Base32InputStream.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 = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
+        byte[] buf = new byte[1024];
+        ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
+        Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[]{0, 0, 0});
+
+        try {
+            in.read(buf, -1, 0);
+            fail("Expected Base32InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        }
+
+        try {
+            in.read(buf, 0, -1);
+            fail("Expected Base32InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        }
+
+        try {
+            in.read(buf, buf.length + 1, 0);
+            fail("Base32InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        }
+
+        try {
+            in.read(buf, buf.length - 1, 2);
+            fail("Base32InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        }
+    }
+}

Propchange: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32InputStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32InputStreamTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32OutputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32OutputStreamTest.java?rev=1063796&view=auto
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32OutputStreamTest.java
(added)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32OutputStreamTest.java
Wed Jan 26 16:59:16 2011
@@ -0,0 +1,332 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.codec.binary;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+public class Base32OutputStreamTest 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
+     */
+    public Base32OutputStreamTest(String name) {
+        super(name);
+    }
+
+//    /**
+//     * Test the Base32OutputStream implementation against the special NPE inducing input
+//     * identified in the CODEC-98 bug.
+//     *
+//     * @throws Exception for some failure scenarios.
+//     */
+//    public void testCodec98NPE() throws Exception {
+//        byte[] codec98 = StringUtils.getBytesUtf8(Base32TestData.CODEC_98_NPE);
+//        byte[] codec98_1024 = new byte[1024];
+//        System.arraycopy(codec98, 0, codec98_1024, 0, codec98.length);
+//        ByteArrayOutputStream data = new ByteArrayOutputStream(1024);
+//        Base32OutputStream stream = new Base32OutputStream(data, false);
+//        stream.write(codec98_1024, 0, 1024);
+//        stream.close();
+//
+//        byte[] decodedBytes = data.toByteArray();
+//        String decoded = StringUtils.newStringUtf8(decodedBytes);
+//        assertEquals(
+//            "codec-98 NPE Base32OutputStream", Base32TestData.CODEC_98_NPE_DECODED, decoded
+//        );
+//    }
+    
+
+    /**
+     * Test the Base32OutputStream implementation against empty input.
+     * 
+     * @throws Exception
+     *             for some failure scenarios.
+     */
+    public void testBase32EmptyOutputStreamMimeChunkSize() throws Exception {
+        testBase32EmptyOutputStream(Base32.MIME_CHUNK_SIZE);
+    }
+
+    /**
+     * Test the Base32OutputStream implementation against empty input.
+     * 
+     * @throws Exception
+     *             for some failure scenarios.
+     */
+    public void testBase32EmptyOutputStreamPemChunkSize() throws Exception {
+        testBase32EmptyOutputStream(Base32.PEM_CHUNK_SIZE);
+    }
+
+    private void testBase32EmptyOutputStream(int chunkSize) throws Exception {
+        byte[] emptyEncoded = new byte[0];
+        byte[] emptyDecoded = new byte[0];
+        testByteByByte(emptyEncoded, emptyDecoded, chunkSize, CRLF);
+        testByChunk(emptyEncoded, emptyDecoded, chunkSize, CRLF);
+    }
+
+    /**
+     * Test the Base32OutputStream implementation
+     * 
+     * @throws Exception
+     *             for some failure scenarios.
+     */
+    public void testBase32OutputStreamByChunk() throws Exception {
+        // Hello World test.
+        byte[] encoded = StringUtils.getBytesUtf8(Base32TestData.BASE32_FIXTURE);
+        byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
+        testByChunk(encoded, decoded, Base32.MIME_CHUNK_SIZE, CRLF);
+
+//        // Single Byte test.
+//        encoded = StringUtils.getBytesUtf8("AA==\r\n");
+//        decoded = new byte[]{(byte) 0};
+//        testByChunk(encoded, decoded, Base32.MIME_CHUNK_SIZE, CRLF);
+
+
+//        // Single Line test.
+//        String singleLine = Base32TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
+//        encoded = StringUtils.getBytesUtf8(singleLine);
+//        decoded = Base32TestData.DECODED;
+//        testByChunk(encoded, decoded, 0, LF);
+
+        // test random data of sizes 0 thru 150
+        for (int i = 0; i <= 150; i++) {
+            byte[][] randomData = Base32TestData.randomData(i, false);
+            encoded = randomData[1];
+            decoded = randomData[0];
+            testByChunk(encoded, decoded, 0, LF);
+        }
+    }
+
+    /**
+     * Test the Base32OutputStream implementation
+     * 
+     * @throws Exception
+     *             for some failure scenarios.
+     */
+    public void testBase32OutputStreamByteByByte() throws Exception {
+        // Hello World test.
+        byte[] encoded = StringUtils.getBytesUtf8(Base32TestData.BASE32_FIXTURE);
+        byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
+        testByteByByte(encoded, decoded, 76, CRLF);
+
+//        // Single Byte test.
+//        encoded = StringUtils.getBytesUtf8("AA==\r\n");
+//        decoded = new byte[]{(byte) 0};
+//        testByteByByte(encoded, decoded, 76, CRLF);
+
+
+//        // Single Line test.
+//        String singleLine = Base32TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
+//        encoded = StringUtils.getBytesUtf8(singleLine);
+//        decoded = Base32TestData.DECODED;
+//        testByteByByte(encoded, decoded, 0, LF);
+
+        // test random data of sizes 0 thru 150
+        for (int i = 0; i <= 150; i++) {
+            byte[][] randomData = Base32TestData.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
+     * <p/>
+     * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base32OutputStream wraps
itself in encode and decode
+     * mode over and over again.
+     * 
+     * @param encoded
+     *            Base32 encoded data
+     * @param decoded
+     *            the data from above, but decoded
+     * @param chunkSize
+     *            chunk size (line-length) of the Base32 encoded data.
+     * @param seperator
+     *            Line separator in the Base32 encoded data.
+     * @throws Exception
+     *             Usually signifies a bug in the Base32 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 Base32OutputStream(byteOut, true, chunkSize, seperator);
+        out.write(decoded);
+        out.close();
+        byte[] output = byteOut.toByteArray();
+        assertTrue("Streaming chunked Base32 encode", Arrays.equals(output, encoded));
+
+        // Now let's try decode.
+        byteOut = new ByteArrayOutputStream();
+        out = new Base32OutputStream(byteOut, false);
+        out.write(encoded);
+        out.close();
+        output = byteOut.toByteArray();
+        assertTrue("Streaming chunked Base32 decode", Arrays.equals(output, decoded));
+
+        // 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 Base32OutputStream(out, false);
+            out = new Base32OutputStream(out, true, chunkSize, seperator);
+        }
+        out.write(decoded);
+        out.close();
+        output = byteOut.toByteArray();
+
+        assertTrue("Streaming chunked Base32 wrap-wrap-wrap!", Arrays.equals(output, 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 Base32OutputStream wraps
itself in encode and decode
+     * mode over and over again.
+     * 
+     * @param encoded
+     *            Base32 encoded data
+     * @param decoded
+     *            the data from above, but decoded
+     * @param chunkSize
+     *            chunk size (line-length) of the Base32 encoded data.
+     * @param seperator
+     *            Line separator in the Base32 encoded data.
+     * @throws Exception
+     *             Usually signifies a bug in the Base32 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 Base32OutputStream(byteOut, true, chunkSize, seperator);
+        for (int i = 0; i < decoded.length; i++) {
+            out.write(decoded[i]);
+        }
+        out.close();
+        byte[] output = byteOut.toByteArray();
+        assertTrue("Streaming byte-by-byte Base32 encode", Arrays.equals(output, encoded));
+
+        // Now let's try decode.
+        byteOut = new ByteArrayOutputStream();
+        out = new Base32OutputStream(byteOut, false);
+        for (int i = 0; i < encoded.length; i++) {
+            out.write(encoded[i]);
+        }
+        out.close();
+        output = byteOut.toByteArray();
+        assertTrue("Streaming byte-by-byte Base32 decode", Arrays.equals(output, decoded));
+
+        // Now let's try decode with tonnes of flushes.
+        byteOut = new ByteArrayOutputStream();
+        out = new Base32OutputStream(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() Base32 decode", Arrays.equals(output,
decoded));
+
+        // 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 Base32OutputStream(out, false);
+            out = new Base32OutputStream(out, true, chunkSize, seperator);
+        }
+        for (int i = 0; i < decoded.length; i++) {
+            out.write(decoded[i]);
+        }
+        out.close();
+        output = byteOut.toByteArray();
+
+        assertTrue("Streaming byte-by-byte Base32 wrap-wrap-wrap!", Arrays.equals(output,
decoded));
+    }
+
+    /**
+     * Tests Base32OutputStream.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();
+        Base32OutputStream out = new Base32OutputStream(bout);
+
+        try {
+            out.write(buf, -1, 1);
+            fail("Expected Base32OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException ioobe) {
+            // Expected
+        }
+
+        try {
+            out.write(buf, 1, -1);
+            fail("Expected Base32OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException ioobe) {
+            // Expected
+        }
+
+        try {
+            out.write(buf, buf.length + 1, 0);
+            fail("Expected Base32OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException ioobe) {
+            // Expected
+        }
+
+        try {
+            out.write(buf, buf.length - 1, 2);
+            fail("Expected Base32OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException ioobe) {
+            // Expected
+        }
+    }
+
+    /**
+     * Tests Base32OutputStream.write(null).
+     * 
+     * @throws Exception
+     *             for some failure scenarios.
+     */
+    public void testWriteToNullCoverage() throws Exception {
+        ByteArrayOutputStream bout = new ByteArrayOutputStream();
+        Base32OutputStream out = new Base32OutputStream(bout);
+        try {
+            out.write(null, 0, 0);
+            fail("Expcted Base32OutputStream.write(null) to throw a NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
+}

Propchange: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32OutputStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32OutputStreamTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32TestData.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32TestData.java?rev=1063796&view=auto
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32TestData.java
(added)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32TestData.java
Wed Jan 26 16:59:16 2011
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.codec.binary;
+
+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.
+ * 
+ * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
+ * @author Apache Software Foundation
+ * @version $Id $
+ * @since 1.4
+ */
+public class Base32TestData {
+
+    static final String STRING_FIXTURE = "Hello World";
+
+    static final String BASE32_FIXTURE = "JBSWY3DPEBLW64TMMQ======\r\n";
+//  static final String BASE32HEX_FIXTURE = "91IMOR3F41BMUSJCCG======";
+
+    // Some utility code to help test chunked reads of the InputStream.
+
+    private final static int SIZE_KEY = 0;
+    private final static int LAST_READ_KEY = 1;
+
+    static byte[] streamToBytes(final InputStream in) throws IOException {
+        // new byte[7] is obviously quite slow, but helps exercise the code.
+        return streamToBytes(in, new byte[7]);
+    }
+
+    static byte[] streamToBytes(final InputStream in, byte[] buf) throws IOException {
+        try {
+            int[] status = fill(buf, 0, in);
+            int size = status[SIZE_KEY];
+            int lastRead = status[LAST_READ_KEY];
+            while (lastRead != -1) {
+                buf = resizeArray(buf);
+                status = fill(buf, size, in);
+                size = status[SIZE_KEY];
+                lastRead = status[LAST_READ_KEY];
+            }
+            if (buf.length != size) {
+                byte[] smallerBuf = new byte[size];
+                System.arraycopy(buf, 0, smallerBuf, 0, size);
+                buf = smallerBuf;
+            }
+        }
+        finally {
+            in.close();
+        }
+        return buf;
+    }
+
+    private static int[] fill(final byte[] buf, final int offset, final InputStream in)
+            throws IOException {
+        int read = in.read(buf, offset, buf.length - offset);
+        int lastRead = read;
+        if (read == -1) {
+            read = 0;
+        }
+        while (lastRead != -1 && read + offset < buf.length) {
+            lastRead = in.read(buf, offset + read, buf.length - read - offset);
+            if (lastRead != -1) {
+                read += lastRead;
+            }
+        }
+        return new int[]{offset + read, lastRead};
+    }
+
+    private static byte[] resizeArray(final byte[] bytes) {
+        byte[] biggerBytes = new byte[bytes.length * 2];
+        System.arraycopy(bytes, 0, biggerBytes, 0, bytes.length);
+        return biggerBytes;
+    }
+
+
+    /**
+     * Returns an encoded and decoded copy of the same random data.
+     * 
+     * @param size amount of random data to generate and encode
+     * @param useHex true if encoding be hex
+     * @return two byte[] arrays:  [0] = decoded, [1] = encoded 
+     */
+    static byte[][] randomData(int size, boolean useHex) {
+        Random r = new Random();
+        byte[] decoded = new byte[size];
+        r.nextBytes(decoded);
+        byte[] encoded = new Base32(useHex).encode(decoded);
+        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;
+    }
+
+}

Propchange: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32TestData.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32TestData.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message