1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.util;
11
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertThrows;
16 import static org.junit.Assert.assertTrue;
17
18 import java.nio.charset.StandardCharsets;
19
20 import org.junit.Test;
21
22
23
24
25 public class Base85Test {
26
27 private static final String VALID_CHARS = "0123456789"
28 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
29 + "!#$%&()*+-;<=>?@^_`{|}~";
30
31 @Test
32 public void testChars() {
33 for (int i = 0; i < 256; i++) {
34 byte[] testData = { '1', '2', '3', '4', (byte) i };
35 if (VALID_CHARS.indexOf(i) >= 0) {
36 byte[] decoded = Base85.decode(testData, 4);
37 assertNotNull(decoded);
38 } else {
39 assertThrows(IllegalArgumentException.class,
40 () -> Base85.decode(testData, 4));
41 }
42 }
43 }
44
45 private void roundtrip(byte[] data, int expectedLength) {
46 byte[] encoded = Base85.encode(data);
47 assertEquals(expectedLength, encoded.length);
48 assertArrayEquals(data, Base85.decode(encoded, data.length));
49 }
50
51 private void roundtrip(String data, int expectedLength) {
52 roundtrip(data.getBytes(StandardCharsets.US_ASCII), expectedLength);
53 }
54
55 @Test
56 public void testPadding() {
57 roundtrip("", 0);
58 roundtrip("a", 5);
59 roundtrip("ab", 5);
60 roundtrip("abc", 5);
61 roundtrip("abcd", 5);
62 roundtrip("abcde", 10);
63 roundtrip("abcdef", 10);
64 roundtrip("abcdefg", 10);
65 roundtrip("abcdefgh", 10);
66 roundtrip("abcdefghi", 15);
67 }
68
69 @Test
70 public void testBinary() {
71 roundtrip(new byte[] { 1 }, 5);
72 roundtrip(new byte[] { 1, 2 }, 5);
73 roundtrip(new byte[] { 1, 2, 3 }, 5);
74 roundtrip(new byte[] { 1, 2, 3, 4 }, 5);
75 roundtrip(new byte[] { 1, 2, 3, 4, 5 }, 10);
76 roundtrip(new byte[] { 1, 2, 3, 4, 5, 0, 0, 0 }, 10);
77 roundtrip(new byte[] { 1, 2, 3, 4, 0, 0, 0, 5 }, 10);
78 }
79
80 @Test
81 public void testOverflow() {
82 IllegalArgumentException e = assertThrows(
83 IllegalArgumentException.class,
84 () -> Base85.decode(new byte[] { '~', '~', '~', '~', '~' }, 4));
85 assertTrue(e.getMessage().contains("overflow"));
86 }
87 }