Skip to content

Commit 4a5330f

Browse files
committed
Improve hasValidPrefix() [fixes #38]
PS: Humans are still smarter than AI... for now! ;-)
1 parent 5d8615a commit 4a5330f

3 files changed

Lines changed: 44 additions & 31 deletions

File tree

src/main/java/io/ipfs/multibase/Multibase.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.ipfs.multibase;
22

33
import java.util.Map;
4+
import java.util.Optional;
45
import java.util.TreeMap;
56

67
import io.ipfs.multibase.binary.Base32;
@@ -45,14 +46,21 @@ public enum Base {
4546
lookup.put(b.prefix, b);
4647
}
4748

48-
public static Base lookup(String data) {
49+
private static Optional<Base> lookupOptional(String data) {
50+
if (data == null || data.isEmpty())
51+
return Optional.empty();
4952
String p = Character.toString(data.codePointAt(0));
5053
Base base = lookup.get(p);
5154
if (base != null)
52-
return base;
55+
return Optional.of(base);
5356
if (data.startsWith(Base256Emoji.prefix))
54-
return Base256Emoji;
55-
throw new IllegalArgumentException("Unknown Multibase type: " + p);
57+
return Optional.of(Base256Emoji);
58+
return Optional.empty();
59+
}
60+
61+
public static Base lookup(String data) {
62+
return lookupOptional(data)
63+
.orElseThrow(() -> new IllegalArgumentException("Unknown Multibase type: " + data));
5664
}
5765
}
5866

@@ -155,12 +163,21 @@ private static String safeSubstringFromIndexOne(String data) {
155163
return data.substring(charIndex);
156164
}
157165

158-
public static boolean isValid(String data) {
159-
try {
160-
decode(data);
161-
return true;
162-
} catch (IllegalArgumentException | UnsupportedOperationException e) {
163-
return false;
164-
}
166+
/**
167+
* Check if the given data has a valid multibase prefix.
168+
*
169+
* <p>
170+
* Please note that "having a valid prefix" is NOT the same as "being an
171+
* entirely valid multibase string"; even if <tt>true</tt>, it's still entirely
172+
* possible for {@link #decode(String)} to throw an
173+
* <tt>IllegalArgumentException</tt>, if prefix is valid, but the following
174+
* data is not.
175+
*
176+
* @param data Multibase string to check.
177+
* @return true if the data has a valid multibase prefix, false otherwise; but
178+
* see above.
179+
*/
180+
public static boolean hasValidPrefix(String data) {
181+
return Base.lookupOptional(data).isPresent();
165182
}
166183
}

src/test/java/io/ipfs/multibase/MultibaseBadInputsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.ipfs.multibase;
22

3+
import static org.junit.jupiter.api.Assertions.assertFalse;
34
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.Arrays;
@@ -29,4 +30,17 @@ public void badInputTest(String input) {
2930
});
3031
}
3132

33+
public static Collection<String> invalidPrefix() {
34+
return Arrays.asList(
35+
"2", // '2' is not a valid encoding marker
36+
"", // Empty string is not a valid multibase
37+
"🫕🚀" // This not a valid Emoji Multibase (note how it's inverted)
38+
);
39+
}
40+
41+
@MethodSource("invalidPrefix")
42+
@ParameterizedTest(name = "{index}: \"{0}\"")
43+
public void invalidPrefix(String input) {
44+
assertFalse(Multibase.hasValidPrefix(input));
45+
}
3246
}

src/test/java/io/ipfs/multibase/MultibaseTest.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static org.junit.jupiter.api.Assertions.assertEquals;
1010
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1111
import static org.junit.jupiter.api.Assertions.assertTrue;
12-
import static org.junit.jupiter.api.Assertions.assertFalse;
1312

1413
public class MultibaseTest {
1514

@@ -71,25 +70,8 @@ public void testDecode(Multibase.Base base, byte[] raw, String encoded) {
7170

7271
@MethodSource("data")
7372
@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));
73+
public void testHasValidPrefix(Multibase.Base base, byte[] raw, String encoded) {
74+
assertTrue(Multibase.hasValidPrefix(encoded));
9375
}
9476

9577
//Copied from https://stackoverflow.com/a/140861

0 commit comments

Comments
 (0)