[java-metadata-aggregator] 01/03: MDA-299 - Add AsURLStringValidator
Ian Young
ian at iay.org.uk
Wed Jan 31 14:09:40 UTC 2024
This is an automated email from the git hooks/post-receive script.
iay pushed a commit to branch dev/MDA-299
in repository java-metadata-aggregator.
View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=c67db9463b8df6b06746355942bd2dea6438261c
commit c67db9463b8df6b06746355942bd2dea6438261c
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Mon Nov 27 16:38:52 2023 +0000
MDA-299 - Add AsURLStringValidator
https://shibboleth.atlassian.net/browse/MDA-299
---
.../validate/string/AsURLStringValidator.java | 62 ++++++++++++
.../resources/net/shibboleth/metadata/beans.xml | 3 +
.../string/AsURLStringValidatorLitmusTest.java | 111 ++++++++++++++++++++
.../validate/string/AsURLStringValidatorTest.java | 112 +++++++++++++++++++++
.../AsURLStringValidatorLitmusTest-config.xml | 23 +++++
5 files changed, 311 insertions(+)
diff --git a/mda-framework/src/main/java/net/shibboleth/metadata/validate/string/AsURLStringValidator.java b/mda-framework/src/main/java/net/shibboleth/metadata/validate/string/AsURLStringValidator.java
new file mode 100644
index 0000000..7340c0d
--- /dev/null
+++ b/mda-framework/src/main/java/net/shibboleth/metadata/validate/string/AsURLStringValidator.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.metadata.validate.string;
+
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import javax.annotation.Nonnull;
+
+import net.shibboleth.metadata.validate.BaseAsValidator;
+import net.shibboleth.metadata.validate.Validator;
+
+/**
+ * A <code>Validator</code> that checks {@link String} values as URLs by converting the
+ * value to an {@link URL} and applying a sequence of validators to that value.
+ *
+ * <p>
+ * This validator fails (and returns {@link net.shibboleth.metadata.validate.Validator.Action#DONE}) if the
+ * value can not be converted to a {@link URL}.
+ * </p>
+ *
+ * <p>
+ * Otherwise, the validator applies the sequence of validators to the {@link URL} and returns
+ * the value of that sequence.
+ * </p>
+ *
+ * @since 0.10.0
+ */
+public class AsURLStringValidator extends BaseAsValidator<String, URL>
+ implements Validator<String> {
+
+ @Override
+ protected @Nonnull URL convert(@Nonnull final String value) throws IllegalArgumentException {
+ try {
+ final var uri = new URI(value);
+ final var result = uri.toURL();
+ assert result != null;
+ return result;
+ } catch (final MalformedURLException e) {
+ throw new IllegalArgumentException(e.getMessage(), e);
+ } catch (final URISyntaxException e) {
+ throw new IllegalArgumentException(e.getMessage(), e);
+ } catch (final IllegalArgumentException e) {
+ throw e;
+ }
+ }
+
+}
diff --git a/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml b/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml
index 7577ce1..9e040c7 100644
--- a/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml
+++ b/mda-framework/src/main/resources/net/shibboleth/metadata/beans.xml
@@ -338,6 +338,9 @@
<bean id="mda.AsLiteralTailStringValidator" abstract="true" parent="mda.component_parent"
class="net.shibboleth.metadata.validate.string.AsLiteralTailStringValidator"/>
+ <bean id="mda.AsURLStringValidator" abstract="true" parent="mda.component_parent"
+ class="net.shibboleth.metadata.validate.string.AsURLStringValidator"/>
+
<bean id="mda.RejectStringRegexValidator" abstract="true" parent="mda.validator_parent"
class="net.shibboleth.metadata.validate.string.RejectStringRegexValidator"/>
diff --git a/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest.java b/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest.java
new file mode 100644
index 0000000..96e2115
--- /dev/null
+++ b/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest.java
@@ -0,0 +1,111 @@
+
+package net.shibboleth.metadata.validate.string;
+
+import javax.annotation.Nonnull;
+
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import net.shibboleth.metadata.ErrorStatus;
+import net.shibboleth.metadata.testing.MockItem;
+import net.shibboleth.metadata.validate.Validator.Action;
+
+/**
+ * A litmus test for {@link AsURLStringValidator} involving a set of valid and invalid
+ * values.
+ *
+ * <p>
+ * The configuration for the stage is taken from a Spring XML configuration file.
+ * </p>
+ */
+ at ContextConfiguration("AsURLStringValidatorLitmusTest-config.xml")
+public class AsURLStringValidatorLitmusTest extends AbstractTestNGSpringContextTests {
+
+ /** {@link AsURLStringValidator} to run for each test. */
+ private AsURLStringValidator validator;
+
+ @BeforeClass
+ private void setUp() throws Exception {
+ validator = makeValidator();
+ }
+
+ /** Acquire the configured validator from the Spring context. */
+ private AsURLStringValidator makeValidator() throws Exception {
+ assert applicationContext != null;
+ final AsURLStringValidator validator = applicationContext.getBean("litmusTest", AsURLStringValidator.class);
+ validator.initialize();
+ return validator;
+ }
+
+ /**
+ * Test a value we expect to be accepted.
+ *
+ * @param value value to test
+ * @throws Exception if something goes wrong
+ */
+ private void good(@Nonnull final String value) throws Exception {
+ final var item = new MockItem("item");
+ var result = validator.validate(value, item, "stage");
+ Assert.assertEquals(result, Action.CONTINUE);
+ Assert.assertTrue(item.getItemMetadata().isEmpty());
+ }
+
+ /**
+ * Test a value we expect to be rejected.
+ *
+ * @param value value to test
+ * @throws Exception if something goes wrong
+ */
+ private void bad(@Nonnull final String value) throws Exception {
+ final var item = new MockItem("item");
+ var result = validator.validate(value, item, "stage");
+ Assert.assertEquals(result, Action.DONE);
+ final var errors = item.getItemMetadata().get(ErrorStatus.class);
+ Assert.assertEquals(errors.size(), 1);
+ }
+
+ /**
+ * Litmus tests from <code>URLCheckerTest</code>.
+ *
+ * <p>
+ * We want the <em>configured</em> {@link AsURLStringValidator} to have
+ * the same behaviour as the old Xalan extension.
+ * </p>
+ *
+ * @throws Exception if something goes wrong.
+ */
+ @Test
+ public void testsFromURLCheckerTest() throws Exception {
+ good("https://example.org:1234/example");
+
+ // Non-integer port number
+ bad("https://example.org:port/example");
+
+ /**
+ * Test the case where the authority's port field is present but empty.
+ *
+ * This is valid by the specification, but is regarded as invalid by
+ * libxml2's xs:anyURI checker.
+ */
+ //bad("http://example.org:/example/");
+
+ // Doubled scheme looks like empty port field
+ //bad("http://http://example.org/example/");
+
+ // Bare domain
+ bad("www.example.org");
+
+ // Empty value
+ bad("");
+
+ // Missing authority caused by extra slash
+ //bad("http:///foo/");
+
+ // Import transform artifact
+ bad("http://*** FILL IN ***/");
+
+ }
+}
diff --git a/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorTest.java b/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorTest.java
new file mode 100644
index 0000000..f26515e
--- /dev/null
+++ b/mda-framework/src/test/java/net/shibboleth/metadata/validate/string/AsURLStringValidatorTest.java
@@ -0,0 +1,112 @@
+package net.shibboleth.metadata.validate.string;
+
+import java.net.URL;
+
+import javax.annotation.Nonnull;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import net.shibboleth.metadata.ErrorStatus;
+import net.shibboleth.metadata.testing.MockItem;
+import net.shibboleth.metadata.validate.Validator.Action;
+import net.shibboleth.metadata.validate.testing.CollectingValidator;
+import net.shibboleth.shared.collection.CollectionSupport;
+
+public class AsURLStringValidatorTest {
+
+ /**
+ * Generic test for a good URL.
+ *
+ * @throws Exception if something goes wrong
+ */
+ private @Nonnull URL good(@Nonnull final String value) throws Exception {
+ final var cv = CollectingValidator.<URL>getInstance("collect");
+ final var item = new MockItem("item");
+ final var v = new AsURLStringValidator();
+ v.setId("test");
+ v.setValidators(CollectionSupport.singletonList(cv));
+ v.initialize();
+ var result = v.validate(value, item, "stage");
+ Assert.assertEquals(result, Action.CONTINUE);
+ Assert.assertTrue(item.getItemMetadata().isEmpty());
+ var collected = cv.getValues();
+ Assert.assertEquals(collected.size(), 1);
+ var url = collected.get(0);
+ cv.destroy();
+ v.destroy();
+ assert url != null;
+ return url;
+ }
+
+ @Test
+ public void testSuccess() throws Exception {
+ final var url = good("HTTPS://example.org:1234/example");
+ // Confirm lower-casing of protocol field.
+ Assert.assertEquals(url.getProtocol(), "https");
+ }
+
+ /**
+ * Generic test for a bad URL.
+ *
+ * @param bad bad URL to test
+ */
+ private @Nonnull ErrorStatus badURL(@Nonnull String value) throws Exception {
+ final var item = new MockItem("item");
+ final var v = new AsURLStringValidator();
+ v.setId("test");
+ v.initialize();
+ var result = v.validate(value, item, "stage");
+ Assert.assertEquals(result, Action.DONE);
+ final var errors = item.getItemMetadata().get(ErrorStatus.class);
+ Assert.assertEquals(errors.size(), 1);
+ final var error = errors.get(0);
+ assert error != null;
+ v.destroy();
+ return error;
+ }
+
+ @Test
+ public void testBadPort() throws Exception {
+ String bad = "https://example.org:port/example";
+ badURL(bad);
+ }
+
+
+ /**
+ * Test the case where the authority's port field is present but empty.
+ *
+ * This is valid by the specification, but is regarded as invalid by
+ * libxml2's xs:anyURI checker.
+ */
+// @Test
+// public void testEmptyPort() throws Exception {
+// badURL("http://example.org:/example/");
+// }
+
+// @Test
+// public void testDoubleScheme() throws Exception {
+// badURL("http://http://example.org/example/");
+// }
+
+ @Test
+ public void testBareDomain() throws Exception {
+ badURL("www.example.org");
+ }
+
+ @Test
+ public void testEmptyUrl() throws Exception {
+ badURL("");
+ }
+
+// @Test
+// public void testEmptyAuthority() throws Exception {
+// badURL("http:///foo/");
+// }
+
+ @Test
+ public void testFillInHostName() throws Exception {
+ badURL("http://*** FILL IN ***/");
+ }
+
+}
diff --git a/mda-framework/src/test/resources/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest-config.xml b/mda-framework/src/test/resources/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest-config.xml
new file mode 100644
index 0000000..9d8f920
--- /dev/null
+++ b/mda-framework/src/test/resources/net/shibboleth/metadata/validate/string/AsURLStringValidatorLitmusTest-config.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:c="http://www.springframework.org/schema/c"
+ xmlns:p="http://www.springframework.org/schema/p"
+ xmlns:util="http://www.springframework.org/schema/util"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
+
+ <import resource="classpath:net/shibboleth/metadata/beans.xml"/>
+
+ <bean parent="mda.IdentifiableBeanPostProcessor"/>
+
+ <bean id="litmusTest" parent="mda.AsURLStringValidator">
+ <property name="validators">
+ <list>
+ <!-- URL validators go here -->
+ </list>
+ </property>
+ </bean>
+
+</beans>
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list