|
8 | 8 |
|
9 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals; |
10 | 10 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 12 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
11 | 13 |
|
12 | 14 | public class MultibaseTest { |
13 | 15 |
|
@@ -67,6 +69,29 @@ public void testDecode(Multibase.Base base, byte[] raw, String encoded) { |
67 | 69 | assertArrayEquals(raw, output, String.format("Expected %s, but got %s", bytesToHex(raw), bytesToHex(output))); |
68 | 70 | } |
69 | 71 |
|
| 72 | + @MethodSource("data") |
| 73 | + @ParameterizedTest(name = "{index}: {0}, {2}") |
| 74 | + public void testIsValid(Multibase.Base base, byte[] raw, String encoded) { |
| 75 | + assertTrue(Multibase.isValid(encoded)); |
| 76 | + } |
| 77 | + |
| 78 | + public static Collection<String> invalidData() { |
| 79 | + return Arrays.asList( |
| 80 | + "f012", // Hex string of odd length, not allowed in Base16 |
| 81 | + "f0g", // 'g' char is not allowed in Base16 |
| 82 | + "zt1Zv2yaI", // 'I' char is not allowed in Base58 |
| 83 | + "2", // '2' is not a valid encoding marker |
| 84 | + "", // Empty string is not a valid multibase |
| 85 | + "🚀🫕" // This Emoji (Swiss Fondue) is not part of the Base256Emoji table |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + @MethodSource("invalidData") |
| 90 | + @ParameterizedTest(name = "{index}: \"{0}\"") |
| 91 | + public void testIsInvalid(String input) { |
| 92 | + assertFalse(Multibase.isValid(input)); |
| 93 | + } |
| 94 | + |
70 | 95 | //Copied from https://stackoverflow.com/a/140861 |
71 | 96 | private static byte[] hexToBytes(String s) { |
72 | 97 | int len = s.length(); |
|
0 commit comments