From f39003c953df152ff737474d2d2f27b611963a1c Mon Sep 17 00:00:00 2001 From: Alex Herbert Date: Fri, 28 Aug 2020 00:04:15 +0100 Subject: [PATCH] Test isInAlphabet --- .../apache/commons/codec/binary/Base32.java | 12 ++-- .../commons/codec/binary/Base32Test.java | 62 ++++++++++++++++++- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/codec/binary/Base32.java b/src/main/java/org/apache/commons/codec/binary/Base32.java index a165791600..0194579b05 100644 --- a/src/main/java/org/apache/commons/codec/binary/Base32.java +++ b/src/main/java/org/apache/commons/codec/binary/Base32.java @@ -66,9 +66,9 @@ public class Base32 extends BaseNCodec { -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, // 30-3f 2-7 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 40-4f A-O 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 50-5a P-Z - -1, -1, -1, -1, -1, // 5b - 5f - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 60 - 6f a-o - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 70 - 7a p-z/**/ + -1, -1, -1, -1, -1, // 5b-5f + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 60-6f a-o + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 70-7a p-z }; /** @@ -91,11 +91,11 @@ public class Base32 extends BaseNCodec { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20-2f - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 30-3f 2-7 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 30-3f 0-9 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 40-4f A-O 25, 26, 27, 28, 29, 30, 31, // 50-56 P-V - -1, -1, -1, -1, -1, -1, -1, -1, -1, // 57-5f Z-_ - -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 60-6f `-o + -1, -1, -1, -1, -1, -1, -1, -1, -1, // 57-5f + -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 60-6f a-o 25, 26, 27, 28, 29, 30, 31 // 70-76 p-v }; 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 9e25bd5ce8..f8ada64a26 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base32Test.java +++ b/src/test/java/org/apache/commons/codec/binary/Base32Test.java @@ -28,7 +28,7 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Arrays; - +import junit.framework.Assert; import org.apache.commons.codec.CodecPolicy; import org.apache.commons.codec.DecoderException; import org.apache.commons.lang3.ArrayUtils; @@ -276,6 +276,18 @@ public void testConstructors() { } catch (final IllegalArgumentException ignored) { // Expected } + try { + base32 = new Base32(32, new byte[] { '\n'}, false, (byte) 'A'); + fail("Should have rejected attempt to use 'A' as padding"); + } catch (final IllegalArgumentException ignored) { + // Expected + } + try { + base32 = new Base32(32, new byte[] { '\n'}, false, (byte) ' '); + fail("Should have rejected attempt to use ' ' as padding"); + } catch (final IllegalArgumentException ignored) { + // Expected + } base32 = new Base32(32, new byte[] { ' ', '$', '\n', '\r', '\t' }); // OK assertNotNull(base32); } @@ -299,6 +311,54 @@ public void testEmptyBase32() { assertEquals("empty Base32 encode", null, new Base32().decode((byte[]) null)); } + @Test + public void testIsInAlphabet() { + // invalid bounds + Base32 b32 = new Base32(true); + assertFalse(b32.isInAlphabet((byte)0)); + assertFalse(b32.isInAlphabet((byte)1)); + assertFalse(b32.isInAlphabet((byte)-1)); + assertFalse(b32.isInAlphabet((byte)-15)); + assertFalse(b32.isInAlphabet((byte)-32)); + assertFalse(b32.isInAlphabet((byte)127)); + assertFalse(b32.isInAlphabet((byte)128)); + assertFalse(b32.isInAlphabet((byte)255)); + + // default table + b32 = new Base32(false); + for (char c = '2'; c <= '7'; c++) { + assertTrue(b32.isInAlphabet((byte) c)); + } + for (char c = 'A'; c <= 'Z'; c++) { + assertTrue(b32.isInAlphabet((byte) c)); + } + for (char c = 'a'; c <= 'z'; c++) { + assertTrue(b32.isInAlphabet((byte) c)); + } + assertFalse(b32.isInAlphabet((byte) ('1'))); + assertFalse(b32.isInAlphabet((byte) ('8'))); + assertFalse(b32.isInAlphabet((byte) ('A' - 1))); + assertFalse(b32.isInAlphabet((byte) ('Z' + 1))); + + // hex table + b32 = new Base32(true); + for (char c = '0'; c <= '9'; c++) { + assertTrue(b32.isInAlphabet((byte) c)); + } + for (char c = 'A'; c <= 'V'; c++) { + assertTrue(b32.isInAlphabet((byte) c)); + } + for (char c = 'a'; c <= 'v'; c++) { + assertTrue(b32.isInAlphabet((byte) c)); + } + assertFalse(b32.isInAlphabet((byte) ('0' - 1))); + assertFalse(b32.isInAlphabet((byte) ('9' + 1))); + assertFalse(b32.isInAlphabet((byte) ('A' - 1))); + assertFalse(b32.isInAlphabet((byte) ('V' + 1))); + assertFalse(b32.isInAlphabet((byte) ('a' - 1))); + assertFalse(b32.isInAlphabet((byte) ('v' + 1))); + } + @Test public void testRandomBytes() { for (int i = 0; i < 20; i++) {