Skip to content

Commit

Permalink
Test isInAlphabet
Browse files Browse the repository at this point in the history
  • Loading branch information
aherbert committed Aug 27, 2020
1 parent 9ac33a1 commit f39003c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/apache/commons/codec/binary/Base32.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/**
Expand All @@ -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
};

Expand Down
62 changes: 61 additions & 1 deletion src/test/java/org/apache/commons/codec/binary/Base32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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++) {
Expand Down

0 comments on commit f39003c

Please sign in to comment.