You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2020/08/27 23:19:07 UTC

[commons-codec] 07/08: Test isInAlphabet

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 f39003c953df152ff737474d2d2f27b611963a1c
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Fri Aug 28 00:04:15 2020 +0100

    Test isInAlphabet
---
 .../org/apache/commons/codec/binary/Base32.java    | 12 ++---
 .../apache/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 a165791..0194579 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 9e25bd5..f8ada64 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 static org.junit.Assert.fail;
 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 class Base32Test {
         } 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);
     }
@@ -300,6 +312,54 @@ public class Base32Test {
     }
 
     @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++) {
             final Base32 codec = new Base32();