Be more strict about what constitutes a valid NamespacedKey

* Brings us largely in line with what is supported by the implementation
* Adds unit tests

By: md_5 <git@md-5.net>
This commit is contained in:
Bukkit/Spigot 2018-07-10 12:21:23 +10:00
parent 5e26ef2298
commit 536017764d
2 changed files with 66 additions and 6 deletions

View file

@ -3,11 +3,19 @@ package org.bukkit;
import com.google.common.base.Preconditions;
import java.util.Locale;
import java.util.UUID;
import java.util.regex.Pattern;
import org.bukkit.plugin.Plugin;
/**
* Represents a String based key which consists of two components - a namespace
* and a key.
*
* Namespaces may only contain lowercase alphanumeric characters, periods,
* underscores, and hyphens.
* <p>
* Keys may only contain lowercase alphanumeric characters, periods,
* underscores, hyphens, and forward slashes.
*
*/
public final class NamespacedKey {
@ -21,6 +29,9 @@ public final class NamespacedKey {
*/
public static final String BUKKIT = "bukkit";
//
private static final Pattern VALID_NAMESPACE = Pattern.compile("[a-z0-9._-]+");
private static final Pattern VALID_KEY = Pattern.compile("[a-z0-9/._-]+");
//
private final String namespace;
private final String key;
@ -33,14 +44,13 @@ public final class NamespacedKey {
*/
@Deprecated
public NamespacedKey(String namespace, String key) {
Preconditions.checkArgument(namespace != null && !namespace.isEmpty(), "namespace");
Preconditions.checkArgument(key != null, "key");
Preconditions.checkArgument(namespace != null && VALID_NAMESPACE.matcher(namespace).matches(), "namespace");
Preconditions.checkArgument(key != null && VALID_KEY.matcher(key).matches(), "key");
this.namespace = namespace;
this.key = key;
String string = toString();
Preconditions.checkArgument(string.indexOf(' ') == -1, "NamespacedKey cannot contain spaces (%s)", string);
Preconditions.checkArgument(string.length() < 256, "NamespacedKey must be less than 256 characters", string);
}
@ -54,12 +64,13 @@ public final class NamespacedKey {
Preconditions.checkArgument(plugin != null, "plugin");
Preconditions.checkArgument(key != null, "key");
// Plugin names cannot have spaces anymore (SimplePluginManager)
Preconditions.checkArgument(key.indexOf(' ') == -1, "key cannot contain spaces (%s)", key);
this.namespace = plugin.getName().toLowerCase(Locale.ROOT);
this.key = key.toLowerCase().toLowerCase(Locale.ROOT);
// Check validity after normalization
Preconditions.checkArgument(VALID_NAMESPACE.matcher(namespace).matches(), "namespace");
Preconditions.checkArgument(VALID_KEY.matcher(key).matches(), "key");
String string = toString();
Preconditions.checkArgument(string.length() < 256, "NamespacedKey must be less than 256 characters (%s)", string);
}

View file

@ -0,0 +1,49 @@
package org.bukkit;
import org.junit.Assert;
import org.junit.Test;
public class NamespacedKeyTest {
@Test
public void testValid() {
Assert.assertEquals("minecraft:foo", new NamespacedKey("minecraft", "foo").toString());
Assert.assertEquals("minecraft:foo/bar", new NamespacedKey("minecraft", "foo/bar").toString());
Assert.assertEquals("minecraft:foo/bar_baz", new NamespacedKey("minecraft", "foo/bar_baz").toString());
Assert.assertEquals("minecraft:foo/bar_baz-qux", new NamespacedKey("minecraft", "foo/bar_baz-qux").toString());
Assert.assertEquals("minecraft:foo/bar_baz-qux.quux", new NamespacedKey("minecraft", "foo/bar_baz-qux.quux").toString());
}
@Test(expected = IllegalArgumentException.class)
public void testEmptyNamespace() {
new NamespacedKey("", "foo").toString();
}
@Test(expected = IllegalArgumentException.class)
public void testEmptyKey() {
new NamespacedKey("minecraft", "").toString();
}
@Test(expected = IllegalArgumentException.class)
public void testInvalidNamespace() {
new NamespacedKey("minecraft/test", "foo").toString();
}
@Test(expected = IllegalArgumentException.class)
public void testInvalidKey() {
new NamespacedKey("minecraft", "foo!").toString();
}
@Test
public void testBelowLength() {
new NamespacedKey("loremipsumdolorsitametconsecteturadipiscingelitduisvolutpatvelitsitametmaximusscelerisquemorbiullamcorperexacconsequategestas",
"loremipsumdolorsitametconsecteturadipiscingelitduisvolutpatvelitsitametmaximusscelerisquemorbiullamcorperexacconsequategestas").toString();
}
@Test(expected = IllegalArgumentException.class)
public void testAboveLength() {
new NamespacedKey("loremipsumdolorsitametconsecteturadipiscingelitduisvolutpatvelitsitametmaximusscelerisquemorbiullamcorperexacconsequategestas",
"loremipsumdolorsitametconsecteturadipiscingelitduisvolutpatvelitsitametmaximusscelerisquemorbiullamcorperexacconsequategestas/"
+ "loremipsumdolorsitametconsecteturadipiscingelitduisvolutpatvelitsitametmaximusscelerisquemorbiullamcorperexacconsequategestas").toString();
}
}