diff options
author | Martin Polden <mpolden@mpolden.no> | 2019-02-04 09:59:24 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2019-02-04 10:08:09 +0100 |
commit | 955ebb989ec0c61cbcdf8fe439f7cde9094120fe (patch) | |
tree | 1898e443467e44a7c9f2c82f1d4aff0e8ec3a913 /config-model | |
parent | 4cabf81b4078dc5974d37270f1170a92e5ed37b8 (diff) |
Disallow consecutive dashes in rotation name
Diffstat (limited to 'config-model')
-rw-r--r-- | config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/Rotations.java | 8 | ||||
-rw-r--r-- | config-model/src/test/java/com/yahoo/vespa/model/builder/xml/dom/RotationsTest.java | 9 |
2 files changed, 12 insertions, 5 deletions
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/Rotations.java b/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/Rotations.java index 429c80f1b2c..2071f2a1ac3 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/Rotations.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/Rotations.java @@ -12,7 +12,7 @@ import java.util.TreeSet; import java.util.regex.Pattern; /** - * Read rotations from the <rotations/> element in services.xml. + * Read rotations from the rotations element in services.xml. * * @author mpolden */ @@ -23,9 +23,11 @@ public class Rotations { * - lowercase * - alphanumeric * - begin with a character + * - contain zero consecutive dashes * - have a length between 1 and 12 */ - private static final Pattern pattern = Pattern.compile("^[a-z][a-z0-9-]{0,11}$"); + private static final Pattern pattern = Pattern.compile("^[a-z](?:-?[a-z0-9]+)*$"); + private static final int maxLength = 12; private Rotations() {} @@ -35,7 +37,7 @@ public class Rotations { List<Element> children = XML.getChildren(rotationsElement, "rotation"); for (Element el : children) { String name = el.getAttribute("id"); - if (name == null || !pattern.matcher(name).find()) { + if (name == null || name.length() > maxLength || !pattern.matcher(name).matches()) { throw new IllegalArgumentException("Rotation ID '" + name + "' is missing or has invalid format"); } RotationName rotation = RotationName.from(name); diff --git a/config-model/src/test/java/com/yahoo/vespa/model/builder/xml/dom/RotationsTest.java b/config-model/src/test/java/com/yahoo/vespa/model/builder/xml/dom/RotationsTest.java index 0c3f31be0f7..3063b1aabc2 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/builder/xml/dom/RotationsTest.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/builder/xml/dom/RotationsTest.java @@ -19,20 +19,25 @@ public class RotationsTest { @Test public void invalid_ids() { - assertInvalid("<rotation/>"); + assertInvalid("<rotation/>"); // Unset assertInvalid("<rotation id=''/>"); // Blank assertInvalid("<rotation id='FOO'/>"); // Uppercaes assertInvalid("<rotation id='123'/>"); // Starting with non-character assertInvalid("<rotation id='foo!'/>"); // Non-alphanumeric - assertInvalid("<rotation id='fooooooooooooo'/>"); // Too long + assertInvalid("<rotation id='foo--bar'/>"); // Multiple consecutive dashes + assertInvalid("<rotation id='foo-'/>"); // Trailing dash + assertInvalid("<rotation id='foooooooooooo'/>"); // Too long assertInvalid("<rotation id='foo'/><rotation id='foo'/>"); // Duplicate ID } @Test public void valid_ids() { assertEquals(rotations(), xml("")); + assertEquals(rotations("f"), xml("<rotation id='f'/>")); assertEquals(rotations("foo"), xml("<rotation id='foo'/>")); + assertEquals(rotations("foo-bar"), xml("<rotation id='foo-bar'/>")); assertEquals(rotations("foo", "bar"), xml("<rotation id='foo'/><rotation id='bar'/>")); + assertEquals(rotations("fooooooooooo"), xml("<rotation id='fooooooooooo'/>")); } private static Set<RotationName> rotations(String... rotation) { |