Skip to content

Commit f39003c

Browse files
committed
Test isInAlphabet
1 parent 9ac33a1 commit f39003c

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

src/main/java/org/apache/commons/codec/binary/Base32.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public class Base32 extends BaseNCodec {
6666
-1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, // 30-3f 2-7
6767
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 40-4f A-O
6868
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 50-5a P-Z
69-
-1, -1, -1, -1, -1, // 5b - 5f
70-
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 60 - 6f a-o
71-
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 70 - 7a p-z/**/
69+
-1, -1, -1, -1, -1, // 5b-5f
70+
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 60-6f a-o
71+
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 70-7a p-z
7272
};
7373

7474
/**
@@ -91,11 +91,11 @@ public class Base32 extends BaseNCodec {
9191
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f
9292
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f
9393
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20-2f
94-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 30-3f 2-7
94+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 30-3f 0-9
9595
-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 40-4f A-O
9696
25, 26, 27, 28, 29, 30, 31, // 50-56 P-V
97-
-1, -1, -1, -1, -1, -1, -1, -1, -1, // 57-5f Z-_
98-
-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 60-6f `-o
97+
-1, -1, -1, -1, -1, -1, -1, -1, -1, // 57-5f
98+
-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 60-6f a-o
9999
25, 26, 27, 28, 29, 30, 31 // 70-76 p-v
100100
};
101101

src/test/java/org/apache/commons/codec/binary/Base32Test.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.nio.charset.Charset;
2929
import java.nio.charset.StandardCharsets;
3030
import java.util.Arrays;
31-
31+
import junit.framework.Assert;
3232
import org.apache.commons.codec.CodecPolicy;
3333
import org.apache.commons.codec.DecoderException;
3434
import org.apache.commons.lang3.ArrayUtils;
@@ -276,6 +276,18 @@ public void testConstructors() {
276276
} catch (final IllegalArgumentException ignored) {
277277
// Expected
278278
}
279+
try {
280+
base32 = new Base32(32, new byte[] { '\n'}, false, (byte) 'A');
281+
fail("Should have rejected attempt to use 'A' as padding");
282+
} catch (final IllegalArgumentException ignored) {
283+
// Expected
284+
}
285+
try {
286+
base32 = new Base32(32, new byte[] { '\n'}, false, (byte) ' ');
287+
fail("Should have rejected attempt to use ' ' as padding");
288+
} catch (final IllegalArgumentException ignored) {
289+
// Expected
290+
}
279291
base32 = new Base32(32, new byte[] { ' ', '$', '\n', '\r', '\t' }); // OK
280292
assertNotNull(base32);
281293
}
@@ -299,6 +311,54 @@ public void testEmptyBase32() {
299311
assertEquals("empty Base32 encode", null, new Base32().decode((byte[]) null));
300312
}
301313

314+
@Test
315+
public void testIsInAlphabet() {
316+
// invalid bounds
317+
Base32 b32 = new Base32(true);
318+
assertFalse(b32.isInAlphabet((byte)0));
319+
assertFalse(b32.isInAlphabet((byte)1));
320+
assertFalse(b32.isInAlphabet((byte)-1));
321+
assertFalse(b32.isInAlphabet((byte)-15));
322+
assertFalse(b32.isInAlphabet((byte)-32));
323+
assertFalse(b32.isInAlphabet((byte)127));
324+
assertFalse(b32.isInAlphabet((byte)128));
325+
assertFalse(b32.isInAlphabet((byte)255));
326+
327+
// default table
328+
b32 = new Base32(false);
329+
for (char c = '2'; c <= '7'; c++) {
330+
assertTrue(b32.isInAlphabet((byte) c));
331+
}
332+
for (char c = 'A'; c <= 'Z'; c++) {
333+
assertTrue(b32.isInAlphabet((byte) c));
334+
}
335+
for (char c = 'a'; c <= 'z'; c++) {
336+
assertTrue(b32.isInAlphabet((byte) c));
337+
}
338+
assertFalse(b32.isInAlphabet((byte) ('1')));
339+
assertFalse(b32.isInAlphabet((byte) ('8')));
340+
assertFalse(b32.isInAlphabet((byte) ('A' - 1)));
341+
assertFalse(b32.isInAlphabet((byte) ('Z' + 1)));
342+
343+
// hex table
344+
b32 = new Base32(true);
345+
for (char c = '0'; c <= '9'; c++) {
346+
assertTrue(b32.isInAlphabet((byte) c));
347+
}
348+
for (char c = 'A'; c <= 'V'; c++) {
349+
assertTrue(b32.isInAlphabet((byte) c));
350+
}
351+
for (char c = 'a'; c <= 'v'; c++) {
352+
assertTrue(b32.isInAlphabet((byte) c));
353+
}
354+
assertFalse(b32.isInAlphabet((byte) ('0' - 1)));
355+
assertFalse(b32.isInAlphabet((byte) ('9' + 1)));
356+
assertFalse(b32.isInAlphabet((byte) ('A' - 1)));
357+
assertFalse(b32.isInAlphabet((byte) ('V' + 1)));
358+
assertFalse(b32.isInAlphabet((byte) ('a' - 1)));
359+
assertFalse(b32.isInAlphabet((byte) ('v' + 1)));
360+
}
361+
302362
@Test
303363
public void testRandomBytes() {
304364
for (int i = 0; i < 20; i++) {

0 commit comments

Comments
 (0)