Skip to content

Commit 790b5bc

Browse files
committed
Add Did.from negative tests
1 parent 60ae7f3 commit 790b5bc

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/main/java/com/apicatalog/did/Did.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ protected static Did from(final Object uri, final String method, final String sp
134134

135135
// check method specific id
136136
if (specificId == null
137-
|| specificId.length() == 0) {
137+
|| specificId.length() == 0
138+
// FIXME does not validate pct-encoded correctly
139+
|| !specificId.codePoints().allMatch(ID_CHAR.or(ch -> ch == ':' || ch == '%'))) {
138140
throw new IllegalArgumentException("The URI [" + uri + "] is not valid DID, method specific id [" + specificId + "] is blank.");
139141
}
140142

src/test/java/com/apicatalog/did/DidTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
66
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.fail;
78

89
import java.net.URI;
910
import java.util.Arrays;
@@ -31,6 +32,17 @@ void fromString(String uri, String method, String specificId) {
3132
assertEquals(specificId, did.getMethodSpecificId());
3233
}
3334

35+
@DisplayName("!from(String)")
36+
@ParameterizedTest()
37+
@MethodSource({ "negativeVectors" })
38+
void fromStringNegative(String uri) {
39+
try {
40+
Did.from(uri);
41+
fail();
42+
} catch (IllegalArgumentException e) {
43+
/* expected */ }
44+
}
45+
3446
@DisplayName("from(URI)")
3547
@ParameterizedTest(name = "{0}")
3648
@MethodSource({ "validVectors" })
@@ -43,6 +55,20 @@ void fromUri(String input, String method, String specificId) {
4355
assertEquals(specificId, did.getMethodSpecificId());
4456
}
4557

58+
@DisplayName("!from(URI)")
59+
@ParameterizedTest()
60+
@MethodSource({ "negativeVectors" })
61+
void fromUriNegative(String uri) {
62+
63+
final URI tmp = URI.create(uri);
64+
65+
try {
66+
Did.from(tmp);
67+
fail();
68+
} catch (IllegalArgumentException e) {
69+
/* expected */ }
70+
}
71+
4672
@DisplayName("toUri()")
4773
@ParameterizedTest(name = "{0}")
4874
@MethodSource({ "validVectors" })

0 commit comments

Comments
 (0)