This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-codec.git
commit 196f32e5011d6709e48c5d255bf8079c71b1c0e6 Author: Adam Retter <adam.ret...@googlemail.com> AuthorDate: Wed Jun 24 12:18:01 2020 +0200 Abstract common test code into BaseNTestData --- .../codec/binary/Base16InputStreamTest.java | 16 +-- .../codec/binary/Base16OutputStreamTest.java | 6 +- .../apache/commons/codec/binary/Base16Test.java | 4 +- .../commons/codec/binary/Base16TestData.java | 78 ------------ .../codec/binary/Base32InputStreamTest.java | 16 +-- .../codec/binary/Base32OutputStreamTest.java | 4 +- .../apache/commons/codec/binary/Base32Test.java | 6 +- .../commons/codec/binary/Base32TestData.java | 87 ------------- .../codec/binary/Base64InputStreamTest.java | 28 +++-- .../codec/binary/Base64OutputStreamTest.java | 14 ++- .../apache/commons/codec/binary/Base64Test.java | 17 +-- .../commons/codec/binary/Base64TestData.java | 136 --------------------- .../{Base64TestData.java => BaseNTestData.java} | 106 ++++++---------- 13 files changed, 95 insertions(+), 423 deletions(-) diff --git a/src/test/java/org/apache/commons/codec/binary/Base16InputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base16InputStreamTest.java index c3c8ee0..99066e7 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base16InputStreamTest.java +++ b/src/test/java/org/apache/commons/codec/binary/Base16InputStreamTest.java @@ -92,12 +92,13 @@ public class Base16InputStreamTest { // OpenSSL interop test. encoded = StringUtils.getBytesUtf8(Base16TestData.ENCODED_UTF8_UPPERCASE); - decoded = Base16TestData.DECODED; + decoded = BaseNTestData.DECODED; testByChunk(encoded, decoded); // test random data of sizes 0 thru 150 + final BaseNCodec codec = new Base16(true); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base16TestData.randomData(i); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByChunk(encoded, decoded, true); @@ -123,12 +124,13 @@ public class Base16InputStreamTest { // OpenSSL interop test. encoded = StringUtils.getBytesUtf8(Base16TestData.ENCODED_UTF8_UPPERCASE); - decoded = Base16TestData.DECODED; + decoded = BaseNTestData.DECODED; testByteByByte(encoded, decoded); // test random data of sizes 0 thru 150 + final BaseNCodec codec = new Base16(true); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base16TestData.randomData(i); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByteByByte(encoded, decoded, true); @@ -166,7 +168,7 @@ public class Base16InputStreamTest { // Start with encode. try (final InputStream in = new Base16InputStream(new ByteArrayInputStream(decoded), true, lowerCase)) { - final byte[] output = Base16TestData.streamToBytes(in); + final byte[] output = BaseNTestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -175,7 +177,7 @@ public class Base16InputStreamTest { // Now let's try decode. try (final InputStream in = new Base16InputStream(new ByteArrayInputStream(encoded), false, lowerCase)) { - final byte[] output = Base16TestData.streamToBytes(in); + final byte[] output = BaseNTestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -187,7 +189,7 @@ public class Base16InputStreamTest { final InputStream inEncode = new Base16InputStream(in, true, lowerCase); final InputStream inDecode = new Base16InputStream(inEncode, false, lowerCase)) { - final byte[] output = Base16TestData.streamToBytes(inDecode); + final byte[] output = BaseNTestData.streamToBytes(inDecode); assertEquals("EOF", -1, inDecode.read()); assertEquals("Still EOF", -1, inDecode.read()); diff --git a/src/test/java/org/apache/commons/codec/binary/Base16OutputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base16OutputStreamTest.java index 6bb5d34..fd7b27a 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base16OutputStreamTest.java +++ b/src/test/java/org/apache/commons/codec/binary/Base16OutputStreamTest.java @@ -64,8 +64,9 @@ public class Base16OutputStreamTest { testByChunk(encoded, decoded); // test random data of sizes 0 thru 150 + final BaseNCodec codec = new Base16(true); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base16TestData.randomData(i); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByChunk(encoded, decoded, true); @@ -90,8 +91,9 @@ public class Base16OutputStreamTest { testByteByByte(encoded, decoded); // test random data of sizes 0 thru 150 + final BaseNCodec codec = new Base16(true); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base16TestData.randomData(i); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByteByByte(encoded, decoded, true); diff --git a/src/test/java/org/apache/commons/codec/binary/Base16Test.java b/src/test/java/org/apache/commons/codec/binary/Base16Test.java index 72bff8a..577a842 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base16Test.java +++ b/src/test/java/org/apache/commons/codec/binary/Base16Test.java @@ -117,7 +117,7 @@ public class Base16Test { @Test public void testConstructor_LowerCase() { final Base16 Base16 = new Base16(true); - final byte[] encoded = Base16.encode(Base16TestData.DECODED); + final byte[] encoded = Base16.encode(BaseNTestData.DECODED); final String expectedResult = Base16TestData.ENCODED_UTF8_LOWERCASE; final String result = StringUtils.newStringUtf8(encoded); assertEquals("new Base16(true)", expectedResult, result); @@ -126,7 +126,7 @@ public class Base16Test { @Test public void testConstructor_LowerCase_DecodingPolicy() { final Base16 Base16 = new Base16(false, CodecPolicy.STRICT); - final byte[] encoded = Base16.encode(Base16TestData.DECODED); + final byte[] encoded = Base16.encode(BaseNTestData.DECODED); final String expectedResult = Base16TestData.ENCODED_UTF8_UPPERCASE; final String result = StringUtils.newStringUtf8(encoded); assertEquals("new Base16(false, CodecPolicy.STRICT)", result, expectedResult); diff --git a/src/test/java/org/apache/commons/codec/binary/Base16TestData.java b/src/test/java/org/apache/commons/codec/binary/Base16TestData.java index 575df32..09be1ca 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base16TestData.java +++ b/src/test/java/org/apache/commons/codec/binary/Base16TestData.java @@ -17,11 +17,6 @@ package org.apache.commons.codec.binary; -import java.io.ByteArrayOutputStream; -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. @@ -35,77 +30,4 @@ public class Base16TestData { final static String ENCODED_UTF8_UPPERCASE = "F483CD2B052F74B888029E9CB73D764A2426386B2D5B262F48F79EBEE7C386BCDAC2CEB9BE8CA42A36C88F7DD85936BDC40EDCFC51F2A56725AD9850EE89DF737244F77049E5F4F847DCC011D8DB8F2D61BF8658367113E1625E5CC2C9FF9A7EA81A53B0FA5EA56F03355632D5CD36FF5C320BE92003A0AF45477D712AFF96DF3C00476C4D5E063029F5F84C2E02261D8AFC6ECE7F9C2CCF2ADA37B0AA5239DAD3FD27B0ACF2FA86EF5B3AF960042CABE6FD4A2FBF268E8BE39D3147E343424B88B907BBAA7D3B0520BD0AA20CACC4BFF02E828D1D4CF67360613208FE4656B95EDD041D81C8881E7A5D7785544CF [...] - - final static byte[] DECODED - = {-12, -125, -51, 43, 5, 47, 116, -72, -120, 2, -98, -100, -73, 61, 118, 74, 36, 38, 56, 107, 45, 91, 38, - 47, 72, -9, -98, -66, -25, -61, -122, -68, -38, -62, -50, -71, -66, -116, -92, 42, 54, -56, -113, 125, - -40, 89, 54, -67, -60, 14, -36, -4, 81, -14, -91, 103, 37, -83, -104, 80, -18, -119, -33, 115, 114, 68, - -9, 112, 73, -27, -12, -8, 71, -36, -64, 17, -40, -37, -113, 45, 97, -65, -122, 88, 54, 113, 19, -31, 98, - 94, 92, -62, -55, -1, -102, 126, -88, 26, 83, -80, -6, 94, -91, 111, 3, 53, 86, 50, -43, -51, 54, -1, 92, - 50, 11, -23, 32, 3, -96, -81, 69, 71, 125, 113, 42, -1, -106, -33, 60, 0, 71, 108, 77, 94, 6, 48, 41, -11, - -8, 76, 46, 2, 38, 29, -118, -4, 110, -50, 127, -100, 44, -49, 42, -38, 55, -80, -86, 82, 57, -38, -45, - -3, 39, -80, -84, -14, -6, -122, -17, 91, 58, -7, 96, 4, 44, -85, -26, -3, 74, 47, -65, 38, -114, -117, - -29, -99, 49, 71, -29, 67, 66, 75, -120, -71, 7, -69, -86, 125, 59, 5, 32, -67, 10, -94, 12, -84, -60, -65, - -16, 46, -126, -115, 29, 76, -10, 115, 96, 97, 50, 8, -2, 70, 86, -71, 94, -35, 4, 29, -127, -56, -120, - 30, 122, 93, 119, -123, 84, 76, -15, -111, 81, -75, -34, 41, -72, 126, -7, 77, -33, 108, -110, 39, -125, - -5, 16, 92, -51, -56, 96, 28, -116, 103, -68, 109, -12, 117, -110, -44, -75, 28, 69, -44, 59, 62, -68, - 39, -4, -119, 80, 91, 19, -116, 122, -81, -118, 100, -108, -88, 2, -8, -106, -75, -37, 30, -83, 124, -121, - 108, -127, 26, -1, -8, 102, -81, -118, 127, -113, -51, 36, -46, 15, 106, -33, -104, 106, -43, -84, -122, - 51, -33, 124, -32, 2, -45, 73, -90, 124, 89, -20, -123, 109, -100, 117, 11, 16, -65, 66, -118, -97, -9, - 101, 7, -1, 41, 65, 70, 116, -119, 54, 126, 44, 75, 74, 26, -34, -27, 27, 54, -13, -89, -90, 64, 120, 15, - -43, 123, 82, -33, 90, -74, 41, -62, 38, -68, 62, -62, 34, 92, 50, 95, -67, -110, -99, -71, -44, -123, - 49, 4, 96, 56, 113, 76, 97, -47, -26, -79, -109, 115, -125, 90, 124, 8, -9, -111, 36, -74, 101, -114, 43, - 0, -110, 63, 76, 99, 91, 2, 12, -60, 56, -14, -125, 0, 6, -27, 31, 31, -109, -47, -3, 109, 88, -75, -74, - 19, 26, -66, 110, 39, 13, -50, 47, 104, -38, 18, 19, 84, 103, 100, -42, 48, 110, 37, 21, -107, 83, -52, - -12, 71, 37, -68, -107, -109, 89, -34, -94, -127, 103, -128, -48, -52, 71, 0, 15, 34, 56, -50, 85, -98, - 106, -87, -3, 97, -116, -19, 64, -22, -25, -38, -63, 33, -45, 80, 10, -121, -109, 37, -96, 36, 18, -48, - 46, 44, -66, 115, -94, 3, -102, -27, -17, -116, -51, 88, -17, 7, -109, 24, 66, 83, -91, 105, -92, -19, - 66, -76, 64, -91, 118, -71, 103, -123, 95, 17, -87, -18, -11, 66, -74, 126, 45, 83, -14, 50, 79, 20, 45, - -113, -103, 119, -101, -58, -99, 27, -100, -17, -107, 91, -26, -32, -56, 71, 72, 34, 66, 16, 9, -90, 106, - -44, -62, -106, 11, 114, -82, -120, -28, -67, 4, -99, 109, -20, -19, 0, -40, -110, -119, 42, -6, 4, -31, - 67, 110, -105, 53, 118, 76, 96, -126, -8, -96, 39, -102, 52, 106, 64, 26, -105, -108, -103, -96, -116, - 116, 0, -96, 115, 89, 40, -23, -102, -2, -30, 16, 58, -53, -33, 14, 122, -94, 113, -121, 67, -103, -4, - -126, 98, -27, 124, -12, 120, -64, -44, 127, 45, -120, 50, 124, -27, 87, -20, -84, 81, -35, 113, -77, - -64, -96, -48, -87, -117, -82, 90, -64, -108, -121, 125, -45, -50, -44, -48, -50, 52, -30, -66, -7, 46, - -40, -47, 85, -44, -126, -122, 24, -84, 21, 120, 99, -74, 27, 11, -52, 32, -2, 122, -100, -118, 106, -9, - -106, 109, -19, 71, 42, 126, 66, -56, 10, -51, -44, 68, 109, -13, 81, -109, 65, 121, 60, -68, -117, 126, - -59, 4, -107, -22, 99, -77, 84, 29, 87, 119, -60, 87, 82, -55, -74, 44, -80, 3, 123, -101, 84, -44, 9, 71, - 24, 91, 99, 22, -65, 11, -11, -14, -38, -84, 105, -101, -85, -17, 116, -65, 118, -105, 122, -75, 113, - -57, -81, -33, -110, 28, 104, -24, -110, -57, -78, 38, -5, -15, -79, 87, 105, 85, 41, -42, -114, -67, - -123, 70, 12, 61, 115, 5, 23, -70, 99, 96, -80, 65, -65, 105, -45, -49, 37, -33, -1, 119, -88, 100, 121, - -25, -35, -51, 10, 43, -113, 61, 103, 44, 13, 108, 20, 74, 19, 53, 19, 37, -76, 20, -43, -11, 23, -58, -25, - -52, 121, -40, -118, 58, 50, 19, -8, -33, -30, -49, -27, -11, -80, 93, -17, 34, 93, 69, 100, 66, -54, 40, - 118, 89, -52, -87, 2, 35, -120, 18, 64, 108, 31, -25, 66, 78, 6, -91, -69, -53, 17, 14, -125, 33, -31, -110, - 1, 5, -40, 7, 126, -122, 84, -55, -62, -22, 69, -28, 5, 45, -106, 120, 74, 94, 51, 74, 108, -19, -26, -12, - 49, 64, 88, 68, 41, -65, 126, 125, -1, -8, -83, -67, 74, 2, -114, -80, -119, -9, -89, -125, 21, 95, 34, - -58, -74, 111, -103, 99, 95, 48, 42, 94, -50, -55, -112, -5, -26, 11, -89, -38, -19, 126, 25, 102, 119, - 81, -94, 70, -79, 98, 91, -73, 114, 15, 14, 87, -21, -122, -1, -90, 0, 29, -104, -91, -93, -58, -83, -48, - -22, 100, -112, -41, 77, 22, -24, 112, -72, 105, 100, 6, -86, -39, 40, -43, 35, -2, 4, -94, 97, -121, 52, - -22, 1, 127, -81, -4, -6, -119, 96, 35, -91, 114, 81, 91, 90, -86, -36, 34, -39, 93, -42, 69, 103, -11, - 107, -87, 119, -107, -114, -45, -128, -69, 96}; - - static byte[] streamToBytes(final InputStream is) throws IOException { - final ByteArrayOutputStream os = new ByteArrayOutputStream(); - final byte[] buf = new byte[4096]; - int read; - while ((read = is.read(buf)) > -1) { - os.write(buf, 0, read); - } - return os.toByteArray(); - } - - /** - * Returns an encoded and decoded copy of the same random data. - * - * @param size amount of random data to generate and encode - * @return two byte[] arrays: [0] = decoded, [1] = encoded - */ - static byte[][] randomData(final int size) { - final Random r = new Random(); - final byte[] decoded = new byte[size]; - r.nextBytes(decoded); - final char[] encodedChars = Hex.encodeHex(decoded); - final byte[] encoded = new String(encodedChars).getBytes(Hex.DEFAULT_CHARSET); - return new byte[][] {decoded, encoded}; - } } diff --git a/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java index fe77eee..164f6c3 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java +++ b/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java @@ -57,7 +57,7 @@ public class Base32InputStreamTest { // we skip the first character read from the reader ins.skip(1); - final byte[] decodedBytes = Base32TestData.streamToBytes(ins, new byte[64]); + final byte[] decodedBytes = BaseNTestData.streamToBytes(ins, new byte[64]); final String str = StringUtils.newStringUtf8(decodedBytes); assertEquals(STRING_FIXTURE.substring(1), str); @@ -222,7 +222,7 @@ public class Base32InputStreamTest { // test random data of sizes 0 thru 150 final BaseNCodec codec = new Base32(); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base32TestData.randomData(codec, i); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByChunk(encoded, decoded, 0, LF); @@ -256,7 +256,7 @@ public class Base32InputStreamTest { // test random data of sizes 0 thru 150 final BaseNCodec codec = new Base32(); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base32TestData.randomData(codec, i); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByteByByte(encoded, decoded, 0, LF); @@ -287,7 +287,7 @@ public class Base32InputStreamTest { InputStream in; in = new Base32InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator); - byte[] output = Base32TestData.streamToBytes(in); + byte[] output = BaseNTestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -295,7 +295,7 @@ public class Base32InputStreamTest { // Now let's try decode. in = new Base32InputStream(new ByteArrayInputStream(encoded)); - output = Base32TestData.streamToBytes(in); + output = BaseNTestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -307,7 +307,7 @@ public class Base32InputStreamTest { in = new Base32InputStream(in, true, chunkSize, separator); in = new Base32InputStream(in, false); } - output = Base32TestData.streamToBytes(in); + output = BaseNTestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -572,13 +572,13 @@ public class Base32InputStreamTest { Base32InputStream in = new Base32InputStream(new ByteArrayInputStream(encoded), false); // Default is lenient decoding; it should not throw assertFalse(in.isStrictDecoding()); - Base32TestData.streamToBytes(in); + BaseNTestData.streamToBytes(in); // Strict decoding should throw in = new Base32InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT); assertTrue(in.isStrictDecoding()); try { - Base32TestData.streamToBytes(in); + BaseNTestData.streamToBytes(in); fail(); } catch (final IllegalArgumentException ex) { // expected diff --git a/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java index a276f8e..5ab7724 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java +++ b/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java @@ -117,7 +117,7 @@ public class Base32OutputStreamTest { // test random data of sizes 0 thru 150 final BaseNCodec codec = new Base32(); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base32TestData.randomData(codec, i); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByChunk(encoded, decoded, 0, LF); @@ -152,7 +152,7 @@ public class Base32OutputStreamTest { // test random data of sizes 0 thru 150 final BaseNCodec codec = new Base32(); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base32TestData.randomData(codec, i); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByteByByte(encoded, decoded, 0, LF); diff --git a/src/test/java/org/apache/commons/codec/binary/Base32Test.java b/src/test/java/org/apache/commons/codec/binary/Base32Test.java index 65c828e..83f1c2f 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base32Test.java +++ b/src/test/java/org/apache/commons/codec/binary/Base32Test.java @@ -244,7 +244,7 @@ public class Base32Test { public void testRandomBytes() { for (int i = 0; i < 20; i++) { final Base32 codec = new Base32(); - final byte[][] b = Base32TestData.randomData(codec, i); + final byte[][] b = BaseNTestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } @@ -254,7 +254,7 @@ public class Base32Test { public void testRandomBytesChunked() { for (int i = 0; i < 20; i++) { final Base32 codec = new Base32(10); - final byte[][] b = Base32TestData.randomData(codec, i); + final byte[][] b = BaseNTestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } @@ -264,7 +264,7 @@ public class Base32Test { public void testRandomBytesHex() { for (int i = 0; i < 20; i++) { final Base32 codec = new Base32(true); - final byte[][] b = Base32TestData.randomData(codec, i); + final byte[][] b = BaseNTestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } diff --git a/src/test/java/org/apache/commons/codec/binary/Base32TestData.java b/src/test/java/org/apache/commons/codec/binary/Base32TestData.java index 420fc12..98225f4 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base32TestData.java +++ b/src/test/java/org/apache/commons/codec/binary/Base32TestData.java @@ -17,10 +17,6 @@ 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. @@ -37,87 +33,4 @@ public class Base32TestData { // 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) { - final 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) { - final 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 codec the codec to use - * @param size amount of random data to generate and encode - * @return two byte[] arrays: [0] = decoded, [1] = encoded - */ - static byte[][] randomData(final BaseNCodec codec, final int size) { - final Random r = new Random(); - final byte[] decoded = new byte[size]; - r.nextBytes(decoded); - final byte[] encoded = codec.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(final byte[] bytes, final byte c) { - for (final byte b : bytes) { - if (b == c) { return true; } - } - return false; - } - } diff --git a/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java index 83a0285..731457a 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java +++ b/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java @@ -66,7 +66,7 @@ public class Base64InputStreamTest { // we skip the first character read from the reader ins.skip(1); - final byte[] decodedBytes = Base64TestData.streamToBytes(ins, new byte[64]); + final byte[] decodedBytes = BaseNTestData.streamToBytes(ins, new byte[64]); final String str = StringUtils.newStringUtf8(decodedBytes); assertEquals(STRING_FIXTURE.substring(1), str); @@ -144,7 +144,7 @@ public class Base64InputStreamTest { final Base64InputStream stream = new Base64InputStream(data); // This line causes an NPE in commons-codec-1.4.jar: - final byte[] decodedBytes = Base64TestData.streamToBytes(stream, new byte[1024]); + final byte[] decodedBytes = BaseNTestData.streamToBytes(stream, new byte[1024]); final String decoded = StringUtils.newStringUtf8(decodedBytes); assertEquals("codec-98 NPE Base64InputStream", Base64TestData.CODEC_98_NPE_DECODED, decoded); @@ -219,18 +219,19 @@ public class Base64InputStreamTest { // OpenSSL interop test. encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE); - decoded = Base64TestData.DECODED; + decoded = BaseNTestData.DECODED; testByChunk(encoded, decoded, BaseNCodec.PEM_CHUNK_SIZE, LF); // Single Line test. final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", ""); encoded = StringUtils.getBytesUtf8(singleLine); - decoded = Base64TestData.DECODED; + decoded = BaseNTestData.DECODED; testByChunk(encoded, decoded, 0, LF); // test random data of sizes 0 thru 150 + final BaseNCodec codec = new Base64(0, null, false); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base64TestData.randomData(i, false); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByChunk(encoded, decoded, 0, LF); @@ -257,18 +258,19 @@ public class Base64InputStreamTest { // OpenSSL interop test. encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE); - decoded = Base64TestData.DECODED; + decoded = BaseNTestData.DECODED; testByteByByte(encoded, decoded, BaseNCodec.PEM_CHUNK_SIZE, LF); // Single Line test. final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", ""); encoded = StringUtils.getBytesUtf8(singleLine); - decoded = Base64TestData.DECODED; + decoded = BaseNTestData.DECODED; testByteByByte(encoded, decoded, 0, LF); // test random data of sizes 0 thru 150 + final BaseNCodec codec = new Base64(0, null, false); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base64TestData.randomData(i, false); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByteByByte(encoded, decoded, 0, LF); @@ -298,7 +300,7 @@ public class Base64InputStreamTest { // Start with encode. InputStream in; in = new Base64InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator); - byte[] output = Base64TestData.streamToBytes(in); + byte[] output = BaseNTestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -308,7 +310,7 @@ public class Base64InputStreamTest { // Now let's try decode. in = new Base64InputStream(new ByteArrayInputStream(encoded)); - output = Base64TestData.streamToBytes(in); + output = BaseNTestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -320,7 +322,7 @@ public class Base64InputStreamTest { in = new Base64InputStream(in, true, chunkSize, separator); in = new Base64InputStream(in, false); } - output = Base64TestData.streamToBytes(in); + output = BaseNTestData.streamToBytes(in); assertEquals("EOF", -1, in.read()); assertEquals("Still EOF", -1, in.read()); @@ -585,13 +587,13 @@ public class Base64InputStreamTest { Base64InputStream in = new Base64InputStream(new ByteArrayInputStream(encoded), false); // Default is lenient decoding; it should not throw assertFalse(in.isStrictDecoding()); - Base64TestData.streamToBytes(in); + BaseNTestData.streamToBytes(in); // Strict decoding should throw in = new Base64InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT); assertTrue(in.isStrictDecoding()); try { - Base64TestData.streamToBytes(in); + BaseNTestData.streamToBytes(in); fail(); } catch (final IllegalArgumentException ex) { // expected diff --git a/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java index a2e3157..0b2480f 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java +++ b/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java @@ -111,18 +111,19 @@ public class Base64OutputStreamTest { // OpenSSL interop test. encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE); - decoded = Base64TestData.DECODED; + decoded = BaseNTestData.DECODED; testByChunk(encoded, decoded, BaseNCodec.PEM_CHUNK_SIZE, LF); // Single Line test. final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", ""); encoded = StringUtils.getBytesUtf8(singleLine); - decoded = Base64TestData.DECODED; + decoded = BaseNTestData.DECODED; testByChunk(encoded, decoded, 0, LF); // test random data of sizes 0 thru 150 + final BaseNCodec codec = new Base64(0, null, false); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base64TestData.randomData(i, false); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByChunk(encoded, decoded, 0, LF); @@ -149,18 +150,19 @@ public class Base64OutputStreamTest { // OpenSSL interop test. encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE); - decoded = Base64TestData.DECODED; + decoded = BaseNTestData.DECODED; testByteByByte(encoded, decoded, 64, LF); // Single Line test. final String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", ""); encoded = StringUtils.getBytesUtf8(singleLine); - decoded = Base64TestData.DECODED; + decoded = BaseNTestData.DECODED; testByteByByte(encoded, decoded, 0, LF); // test random data of sizes 0 thru 150 + final BaseNCodec codec = new Base64(0, null, false); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base64TestData.randomData(i, false); + final byte[][] randomData = BaseNTestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByteByByte(encoded, decoded, 0, LF); diff --git a/src/test/java/org/apache/commons/codec/binary/Base64Test.java b/src/test/java/org/apache/commons/codec/binary/Base64Test.java index 884b4a2..caf5363 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base64Test.java +++ b/src/test/java/org/apache/commons/codec/binary/Base64Test.java @@ -186,7 +186,7 @@ public class Base64Test { */ @Test public void testChunkedEncodeMultipleOf76() { - final byte[] expectedEncode = Base64.encodeBase64(Base64TestData.DECODED, true); + final byte[] expectedEncode = Base64.encodeBase64(BaseNTestData.DECODED, true); // convert to "\r\n" so we're equal to the old openssl encoding test // stored // in Base64TestData.ENCODED_76_CHARS_PER_LINE: @@ -309,7 +309,7 @@ public class Base64Test { @Test public void testConstructor_Int_ByteArray_Boolean() { final Base64 base64 = new Base64(65, new byte[] { '\t' }, false); - final byte[] encoded = base64.encode(Base64TestData.DECODED); + final byte[] encoded = base64.encode(BaseNTestData.DECODED); String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE; expectedResult = expectedResult.replace('\n', '\t'); final String result = StringUtils.newStringUtf8(encoded); @@ -320,7 +320,7 @@ public class Base64Test { public void testConstructor_Int_ByteArray_Boolean_UrlSafe() { // url-safe variation final Base64 base64 = new Base64(64, new byte[] { '\t' }, true); - final byte[] encoded = base64.encode(Base64TestData.DECODED); + final byte[] encoded = base64.encode(BaseNTestData.DECODED); String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE; expectedResult = expectedResult.replaceAll("=", ""); // url-safe has no // == padding. @@ -450,7 +450,7 @@ public class Base64Test { private void testEncodeOverMaxSize(final int maxSize) throws Exception { try { - Base64.encodeBase64(Base64TestData.DECODED, true, false, maxSize); + Base64.encodeBase64(BaseNTestData.DECODED, true, false, maxSize); fail("Expected " + IllegalArgumentException.class.getName()); } catch (final IllegalArgumentException e) { // Expected @@ -1149,15 +1149,16 @@ public class Base64Test { @Test public void testUrlSafe() { // test random data of sizes 0 thru 150 + final BaseNCodec codec = new Base64(true); for (int i = 0; i <= 150; i++) { - final byte[][] randomData = Base64TestData.randomData(i, true); + final byte[][] randomData = BaseNTestData.randomData(codec, i); final byte[] encoded = randomData[1]; final byte[] decoded = randomData[0]; final 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) '+')); + assertFalse("url-safe i=" + i + " no '='", BaseNTestData.bytesContain(encoded, (byte) '=')); + assertFalse("url-safe i=" + i + " no '\\'", BaseNTestData.bytesContain(encoded, (byte) '\\')); + assertFalse("url-safe i=" + i + " no '+'", BaseNTestData.bytesContain(encoded, (byte) '+')); } } diff --git a/src/test/java/org/apache/commons/codec/binary/Base64TestData.java b/src/test/java/org/apache/commons/codec/binary/Base64TestData.java index 3e53d3a..5e45860 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base64TestData.java +++ b/src/test/java/org/apache/commons/codec/binary/Base64TestData.java @@ -17,8 +17,6 @@ package org.apache.commons.codec.binary; -import java.io.IOException; -import java.io.InputStream; import java.util.Random; /** @@ -84,138 +82,4 @@ public class Base64TestData { + "WcypAiOIEkBsH+dCTgalu8sRDoMh4ZIBBdgHfoZUycLqReQFLZZ4Sl4zSmzt5vQxQFhEKb9+ff/4\n" + "rb1KAo6wifengxVfIsa2b5ljXzAqXs7JkPvmC6fa7X4ZZndRokaxYlu3cg8OV+uG/6YAHZilo8at\n" + "0OpkkNdNFuhwuGlkBqrZKNUj/gSiYYc06gF/r/z6iWAjpXJRW1qq3CLZXdZFZ/VrqXeVjtOAu2A=\n"; - - final static byte[] DECODED - = {-12, -125, -51, 43, 5, 47, 116, -72, -120, 2, -98, -100, -73, 61, 118, 74, 36, 38, 56, 107, 45, 91, 38, - 47, 72, -9, -98, -66, -25, -61, -122, -68, -38, -62, -50, -71, -66, -116, -92, 42, 54, -56, -113, 125, - -40, 89, 54, -67, -60, 14, -36, -4, 81, -14, -91, 103, 37, -83, -104, 80, -18, -119, -33, 115, 114, 68, - -9, 112, 73, -27, -12, -8, 71, -36, -64, 17, -40, -37, -113, 45, 97, -65, -122, 88, 54, 113, 19, -31, 98, - 94, 92, -62, -55, -1, -102, 126, -88, 26, 83, -80, -6, 94, -91, 111, 3, 53, 86, 50, -43, -51, 54, -1, 92, - 50, 11, -23, 32, 3, -96, -81, 69, 71, 125, 113, 42, -1, -106, -33, 60, 0, 71, 108, 77, 94, 6, 48, 41, -11, - -8, 76, 46, 2, 38, 29, -118, -4, 110, -50, 127, -100, 44, -49, 42, -38, 55, -80, -86, 82, 57, -38, -45, - -3, 39, -80, -84, -14, -6, -122, -17, 91, 58, -7, 96, 4, 44, -85, -26, -3, 74, 47, -65, 38, -114, -117, - -29, -99, 49, 71, -29, 67, 66, 75, -120, -71, 7, -69, -86, 125, 59, 5, 32, -67, 10, -94, 12, -84, -60, -65, - -16, 46, -126, -115, 29, 76, -10, 115, 96, 97, 50, 8, -2, 70, 86, -71, 94, -35, 4, 29, -127, -56, -120, - 30, 122, 93, 119, -123, 84, 76, -15, -111, 81, -75, -34, 41, -72, 126, -7, 77, -33, 108, -110, 39, -125, - -5, 16, 92, -51, -56, 96, 28, -116, 103, -68, 109, -12, 117, -110, -44, -75, 28, 69, -44, 59, 62, -68, - 39, -4, -119, 80, 91, 19, -116, 122, -81, -118, 100, -108, -88, 2, -8, -106, -75, -37, 30, -83, 124, -121, - 108, -127, 26, -1, -8, 102, -81, -118, 127, -113, -51, 36, -46, 15, 106, -33, -104, 106, -43, -84, -122, - 51, -33, 124, -32, 2, -45, 73, -90, 124, 89, -20, -123, 109, -100, 117, 11, 16, -65, 66, -118, -97, -9, - 101, 7, -1, 41, 65, 70, 116, -119, 54, 126, 44, 75, 74, 26, -34, -27, 27, 54, -13, -89, -90, 64, 120, 15, - -43, 123, 82, -33, 90, -74, 41, -62, 38, -68, 62, -62, 34, 92, 50, 95, -67, -110, -99, -71, -44, -123, - 49, 4, 96, 56, 113, 76, 97, -47, -26, -79, -109, 115, -125, 90, 124, 8, -9, -111, 36, -74, 101, -114, 43, - 0, -110, 63, 76, 99, 91, 2, 12, -60, 56, -14, -125, 0, 6, -27, 31, 31, -109, -47, -3, 109, 88, -75, -74, - 19, 26, -66, 110, 39, 13, -50, 47, 104, -38, 18, 19, 84, 103, 100, -42, 48, 110, 37, 21, -107, 83, -52, - -12, 71, 37, -68, -107, -109, 89, -34, -94, -127, 103, -128, -48, -52, 71, 0, 15, 34, 56, -50, 85, -98, - 106, -87, -3, 97, -116, -19, 64, -22, -25, -38, -63, 33, -45, 80, 10, -121, -109, 37, -96, 36, 18, -48, - 46, 44, -66, 115, -94, 3, -102, -27, -17, -116, -51, 88, -17, 7, -109, 24, 66, 83, -91, 105, -92, -19, - 66, -76, 64, -91, 118, -71, 103, -123, 95, 17, -87, -18, -11, 66, -74, 126, 45, 83, -14, 50, 79, 20, 45, - -113, -103, 119, -101, -58, -99, 27, -100, -17, -107, 91, -26, -32, -56, 71, 72, 34, 66, 16, 9, -90, 106, - -44, -62, -106, 11, 114, -82, -120, -28, -67, 4, -99, 109, -20, -19, 0, -40, -110, -119, 42, -6, 4, -31, - 67, 110, -105, 53, 118, 76, 96, -126, -8, -96, 39, -102, 52, 106, 64, 26, -105, -108, -103, -96, -116, - 116, 0, -96, 115, 89, 40, -23, -102, -2, -30, 16, 58, -53, -33, 14, 122, -94, 113, -121, 67, -103, -4, - -126, 98, -27, 124, -12, 120, -64, -44, 127, 45, -120, 50, 124, -27, 87, -20, -84, 81, -35, 113, -77, - -64, -96, -48, -87, -117, -82, 90, -64, -108, -121, 125, -45, -50, -44, -48, -50, 52, -30, -66, -7, 46, - -40, -47, 85, -44, -126, -122, 24, -84, 21, 120, 99, -74, 27, 11, -52, 32, -2, 122, -100, -118, 106, -9, - -106, 109, -19, 71, 42, 126, 66, -56, 10, -51, -44, 68, 109, -13, 81, -109, 65, 121, 60, -68, -117, 126, - -59, 4, -107, -22, 99, -77, 84, 29, 87, 119, -60, 87, 82, -55, -74, 44, -80, 3, 123, -101, 84, -44, 9, 71, - 24, 91, 99, 22, -65, 11, -11, -14, -38, -84, 105, -101, -85, -17, 116, -65, 118, -105, 122, -75, 113, - -57, -81, -33, -110, 28, 104, -24, -110, -57, -78, 38, -5, -15, -79, 87, 105, 85, 41, -42, -114, -67, - -123, 70, 12, 61, 115, 5, 23, -70, 99, 96, -80, 65, -65, 105, -45, -49, 37, -33, -1, 119, -88, 100, 121, - -25, -35, -51, 10, 43, -113, 61, 103, 44, 13, 108, 20, 74, 19, 53, 19, 37, -76, 20, -43, -11, 23, -58, -25, - -52, 121, -40, -118, 58, 50, 19, -8, -33, -30, -49, -27, -11, -80, 93, -17, 34, 93, 69, 100, 66, -54, 40, - 118, 89, -52, -87, 2, 35, -120, 18, 64, 108, 31, -25, 66, 78, 6, -91, -69, -53, 17, 14, -125, 33, -31, -110, - 1, 5, -40, 7, 126, -122, 84, -55, -62, -22, 69, -28, 5, 45, -106, 120, 74, 94, 51, 74, 108, -19, -26, -12, - 49, 64, 88, 68, 41, -65, 126, 125, -1, -8, -83, -67, 74, 2, -114, -80, -119, -9, -89, -125, 21, 95, 34, - -58, -74, 111, -103, 99, 95, 48, 42, 94, -50, -55, -112, -5, -26, 11, -89, -38, -19, 126, 25, 102, 119, - 81, -94, 70, -79, 98, 91, -73, 114, 15, 14, 87, -21, -122, -1, -90, 0, 29, -104, -91, -93, -58, -83, -48, - -22, 100, -112, -41, 77, 22, -24, 112, -72, 105, 100, 6, -86, -39, 40, -43, 35, -2, 4, -94, 97, -121, 52, - -22, 1, 127, -81, -4, -6, -119, 96, 35, -91, 114, 81, 91, 90, -86, -36, 34, -39, 93, -42, 69, 103, -11, - 107, -87, 119, -107, -114, -45, -128, -69, 96}; - - // 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) { - final 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) { - final 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 urlSafe true if encoding be urlSafe - * @return two byte[] arrays: [0] = decoded, [1] = encoded - */ - static byte[][] randomData(final int size, final boolean urlSafe) { - final Random r = new Random(); - final byte[] decoded = new byte[size]; - r.nextBytes(decoded); - final byte[] encoded = urlSafe ? Base64.encodeBase64URLSafe(decoded) : Base64.encodeBase64(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(final byte[] bytes, final byte c) { - for (final byte b : bytes) { - if (b == c) { return true; } - } - return false; - } - } diff --git a/src/test/java/org/apache/commons/codec/binary/Base64TestData.java b/src/test/java/org/apache/commons/codec/binary/BaseNTestData.java similarity index 64% copy from src/test/java/org/apache/commons/codec/binary/Base64TestData.java copy to src/test/java/org/apache/commons/codec/binary/BaseNTestData.java index 3e53d3a..5a43dbe 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base64TestData.java +++ b/src/test/java/org/apache/commons/codec/binary/BaseNTestData.java @@ -17,73 +17,17 @@ package org.apache.commons.codec.binary; +import java.io.ByteArrayOutputStream; 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. + * Data and functions common to BaseN tests. * - * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a> - * @since 1.4 + * @since 1.15 */ -public class Base64TestData { - - public static final String CODEC_101_MULTIPLE_OF_3 = "124"; - - public static final String CODEC_98_NPE - = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM"; - - public static final String CODEC_98_NPE_DECODED - = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123"; - - - // OpenSSL doesn't include the final \n, but it would be annoying beyond belief - // to further parameterize commons-codec to support this pointless variation. - final static String ENCODED_64_CHARS_PER_LINE - = "9IPNKwUvdLiIAp6ctz12SiQmOGstWyYvSPeevufDhrzaws65voykKjbIj33YWTa9\n" - + "xA7c/FHypWclrZhQ7onfc3JE93BJ5fT4R9zAEdjbjy1hv4ZYNnET4WJeXMLJ/5p+\n" - + "qBpTsPpepW8DNVYy1c02/1wyC+kgA6CvRUd9cSr/lt88AEdsTV4GMCn1+EwuAiYd\n" - + "ivxuzn+cLM8q2jewqlI52tP9J7Cs8vqG71s6+WAELKvm/UovvyaOi+OdMUfjQ0JL\n" - + "iLkHu6p9OwUgvQqiDKzEv/Augo0dTPZzYGEyCP5GVrle3QQdgciIHnpdd4VUTPGR\n" - + "UbXeKbh++U3fbJIng/sQXM3IYByMZ7xt9HWS1LUcRdQ7Prwn/IlQWxOMeq+KZJSo\n" - + "AviWtdserXyHbIEa//hmr4p/j80k0g9q35hq1ayGM9984ALTSaZ8WeyFbZx1CxC/\n" - + "Qoqf92UH/ylBRnSJNn4sS0oa3uUbNvOnpkB4D9V7Ut9atinCJrw+wiJcMl+9kp25\n" - + "1IUxBGA4cUxh0eaxk3ODWnwI95EktmWOKwCSP0xjWwIMxDjygwAG5R8fk9H9bVi1\n" - + "thMavm4nDc4vaNoSE1RnZNYwbiUVlVPM9EclvJWTWd6igWeA0MxHAA8iOM5Vnmqp\n" - + "/WGM7UDq59rBIdNQCoeTJaAkEtAuLL5zogOa5e+MzVjvB5MYQlOlaaTtQrRApXa5\n" - + "Z4VfEanu9UK2fi1T8jJPFC2PmXebxp0bnO+VW+bgyEdIIkIQCaZq1MKWC3KuiOS9\n" - + "BJ1t7O0A2JKJKvoE4UNulzV2TGCC+KAnmjRqQBqXlJmgjHQAoHNZKOma/uIQOsvf\n" - + "DnqicYdDmfyCYuV89HjA1H8tiDJ85VfsrFHdcbPAoNCpi65awJSHfdPO1NDONOK+\n" - + "+S7Y0VXUgoYYrBV4Y7YbC8wg/nqcimr3lm3tRyp+QsgKzdREbfNRk0F5PLyLfsUE\n" - + "lepjs1QdV3fEV1LJtiywA3ubVNQJRxhbYxa/C/Xy2qxpm6vvdL92l3q1ccev35Ic\n" - + "aOiSx7Im+/GxV2lVKdaOvYVGDD1zBRe6Y2CwQb9p088l3/93qGR5593NCiuPPWcs\n" - + "DWwUShM1EyW0FNX1F8bnzHnYijoyE/jf4s/l9bBd7yJdRWRCyih2WcypAiOIEkBs\n" - + "H+dCTgalu8sRDoMh4ZIBBdgHfoZUycLqReQFLZZ4Sl4zSmzt5vQxQFhEKb9+ff/4\n" - + "rb1KAo6wifengxVfIsa2b5ljXzAqXs7JkPvmC6fa7X4ZZndRokaxYlu3cg8OV+uG\n" - + "/6YAHZilo8at0OpkkNdNFuhwuGlkBqrZKNUj/gSiYYc06gF/r/z6iWAjpXJRW1qq\n" - + "3CLZXdZFZ/VrqXeVjtOAu2A=\n"; - - final static String ENCODED_76_CHARS_PER_LINE - = "9IPNKwUvdLiIAp6ctz12SiQmOGstWyYvSPeevufDhrzaws65voykKjbIj33YWTa9xA7c/FHypWcl\n" - + "rZhQ7onfc3JE93BJ5fT4R9zAEdjbjy1hv4ZYNnET4WJeXMLJ/5p+qBpTsPpepW8DNVYy1c02/1wy\n" - + "C+kgA6CvRUd9cSr/lt88AEdsTV4GMCn1+EwuAiYdivxuzn+cLM8q2jewqlI52tP9J7Cs8vqG71s6\n" - + "+WAELKvm/UovvyaOi+OdMUfjQ0JLiLkHu6p9OwUgvQqiDKzEv/Augo0dTPZzYGEyCP5GVrle3QQd\n" - + "gciIHnpdd4VUTPGRUbXeKbh++U3fbJIng/sQXM3IYByMZ7xt9HWS1LUcRdQ7Prwn/IlQWxOMeq+K\n" - + "ZJSoAviWtdserXyHbIEa//hmr4p/j80k0g9q35hq1ayGM9984ALTSaZ8WeyFbZx1CxC/Qoqf92UH\n" - + "/ylBRnSJNn4sS0oa3uUbNvOnpkB4D9V7Ut9atinCJrw+wiJcMl+9kp251IUxBGA4cUxh0eaxk3OD\n" - + "WnwI95EktmWOKwCSP0xjWwIMxDjygwAG5R8fk9H9bVi1thMavm4nDc4vaNoSE1RnZNYwbiUVlVPM\n" - + "9EclvJWTWd6igWeA0MxHAA8iOM5Vnmqp/WGM7UDq59rBIdNQCoeTJaAkEtAuLL5zogOa5e+MzVjv\n" - + "B5MYQlOlaaTtQrRApXa5Z4VfEanu9UK2fi1T8jJPFC2PmXebxp0bnO+VW+bgyEdIIkIQCaZq1MKW\n" - + "C3KuiOS9BJ1t7O0A2JKJKvoE4UNulzV2TGCC+KAnmjRqQBqXlJmgjHQAoHNZKOma/uIQOsvfDnqi\n" - + "cYdDmfyCYuV89HjA1H8tiDJ85VfsrFHdcbPAoNCpi65awJSHfdPO1NDONOK++S7Y0VXUgoYYrBV4\n" - + "Y7YbC8wg/nqcimr3lm3tRyp+QsgKzdREbfNRk0F5PLyLfsUElepjs1QdV3fEV1LJtiywA3ubVNQJ\n" - + "RxhbYxa/C/Xy2qxpm6vvdL92l3q1ccev35IcaOiSx7Im+/GxV2lVKdaOvYVGDD1zBRe6Y2CwQb9p\n" - + "088l3/93qGR5593NCiuPPWcsDWwUShM1EyW0FNX1F8bnzHnYijoyE/jf4s/l9bBd7yJdRWRCyih2\n" - + "WcypAiOIEkBsH+dCTgalu8sRDoMh4ZIBBdgHfoZUycLqReQFLZZ4Sl4zSmzt5vQxQFhEKb9+ff/4\n" - + "rb1KAo6wifengxVfIsa2b5ljXzAqXs7JkPvmC6fa7X4ZZndRokaxYlu3cg8OV+uG/6YAHZilo8at\n" - + "0OpkkNdNFuhwuGlkBqrZKNUj/gSiYYc06gF/r/z6iWAjpXJRW1qq3CLZXdZFZ/VrqXeVjtOAu2A=\n"; +public class BaseNTestData { final static byte[] DECODED = {-12, -125, -51, 43, 5, 47, 116, -72, -120, 2, -98, -100, -73, 61, 118, 74, 36, 38, 56, 107, 45, 91, 38, @@ -133,16 +77,38 @@ public class Base64TestData { -22, 1, 127, -81, -4, -6, -119, 96, 35, -91, 114, 81, 91, 90, -86, -36, 34, -39, 93, -42, 69, 103, -11, 107, -87, 119, -107, -114, -45, -128, -69, 96}; - // Some utility code to help test chunked reads of the InputStream. + /** + * Read all bytes from an InputStream into a byte array. + * + * @param in the input stream. + * @return the byte array + * + * @throws IOException if an error occurs whilst reading the input stream + */ + static byte[] streamToBytes(final InputStream in) throws IOException { + final ByteArrayOutputStream os = new ByteArrayOutputStream(); + final byte[] buf = new byte[4096]; + int read; + while ((read = in.read(buf)) > -1) { + os.write(buf, 0, read); + } + return os.toByteArray(); + } 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]); - } - + /** + * Read all bytes from an InputStream into a byte array + * in chunks of {@code buf.length}. + * + * @param in the input stream. + * @param buf the byte array to use for chunking + * + * @return the bytes read from the input stream + * + * @throws IOException if an error occurs whilst reading the input stream + */ static byte[] streamToBytes(final InputStream in, byte[] buf) throws IOException { try { int[] status = fill(buf, 0, in); @@ -188,19 +154,18 @@ public class Base64TestData { return biggerBytes; } - /** * Returns an encoded and decoded copy of the same random data. * + * @param codec the codec to use * @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 */ - static byte[][] randomData(final int size, final boolean urlSafe) { + static byte[][] randomData(final BaseNCodec codec, final int size) { final Random r = new Random(); final byte[] decoded = new byte[size]; r.nextBytes(decoded); - final byte[] encoded = urlSafe ? Base64.encodeBase64URLSafe(decoded) : Base64.encodeBase64(decoded); + final byte[] encoded = codec.encode(decoded); return new byte[][] {decoded, encoded}; } @@ -217,5 +182,4 @@ public class Base64TestData { } return false; } - }