This commit appears to be failing for me. The testInvalidIpAddress method is failing on the assert.
Ralph > On Dec 30, 2016, at 2:01 PM, mattsic...@apache.org wrote: > > Repository: logging-log4j2 > Updated Branches: > refs/heads/master 367d26b09 -> 4254e2558 > > > [LOG4J2-1755]: Add converters and validators for hostnames/ports > > > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255 > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255 > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255 > > Branch: refs/heads/master > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018 > Parents: 367d26b > Author: Matt Sicker <matt.sic...@spr.com> > Authored: Fri Dec 30 15:00:17 2016 -0600 > Committer: Matt Sicker <matt.sic...@spr.com> > Committed: Fri Dec 30 15:01:26 2016 -0600 > > ---------------------------------------------------------------------- > .../config/plugins/convert/TypeConverters.java | 13 +++- > .../validation/constraints/ValidHost.java | 41 +++++++++++ > .../validation/constraints/ValidPort.java | 44 ++++++++++++ > .../validators/ValidHostValidator.java | 58 +++++++++++++++ > .../validators/ValidPortValidator.java | 53 ++++++++++++++ > .../config/plugins/validation/HostAndPort.java | 46 ++++++++++++ > .../validators/ValidHostValidatorTest.java | 74 ++++++++++++++++++++ > .../validators/ValidPortValidatorTest.java | 70 ++++++++++++++++++ > src/changes/changes.xml | 3 + > 9 files changed, 401 insertions(+), 1 deletion(-) > ---------------------------------------------------------------------- > > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java > ---------------------------------------------------------------------- > diff --git > a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java > > b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java > index 2895e52..421d711 100644 > --- > a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java > +++ > b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java > @@ -20,6 +20,7 @@ package > org.apache.logging.log4j.core.config.plugins.convert; > import java.io.File; > import java.math.BigDecimal; > import java.math.BigInteger; > +import java.net.InetAddress; > import java.net.MalformedURLException; > import java.net.URI; > import java.net.URISyntaxException; > @@ -28,7 +29,6 @@ import java.nio.charset.Charset; > import java.security.Provider; > import java.security.Security; > import java.util.regex.Pattern; > - > import javax.xml.bind.DatatypeConverter; > > import org.apache.logging.log4j.Level; > @@ -233,6 +233,17 @@ public final class TypeConverters { > } > > /** > + * Converts a {@link String} into an {@link InetAddress}. > + */ > + @Plugin(name = "InetAddress", category = CATEGORY) > + public static class InetAddressConverter implements > TypeConverter<InetAddress> { > + @Override > + public InetAddress convert(final String s) throws Exception { > + return InetAddress.getByName(s); > + } > + } > + > + /** > * Converts a {@link String} into a {@link Integer}. > */ > @Plugin(name = "Integer", category = CATEGORY) > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java > ---------------------------------------------------------------------- > diff --git > a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java > > b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java > new file mode 100644 > index 0000000..c652d40 > --- /dev/null > +++ > b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java > @@ -0,0 +1,41 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one or more > + * contributor license agreements. See the NOTICE file distributed with > + * this work for additional information regarding copyright ownership. > + * The ASF licenses this file to You 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 org.apache.logging.log4j.core.config.plugins.validation.constraints; > + > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint; > +import > org.apache.logging.log4j.core.config.plugins.validation.validators.ValidHostValidator; > + > +import java.lang.annotation.*; > +import java.net.InetAddress; > + > +/** > + * Indicates that a plugin attribute must be a valid host. This relies on > the same validation rules as > + * {@link InetAddress#getByName(String)}. > + * > + * @since 2.8 > + */ > +@Documented > +@Retention(RetentionPolicy.RUNTIME) > +@Target({ElementType.FIELD, ElementType.PARAMETER}) > +@Constraint(ValidHostValidator.class) > +public @interface ValidHost { > + > + /** > + * The message to be logged if this constraint is violated. This should > normally be overridden. > + */ > + String message() default "The hostname is invalid"; > +} > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java > ---------------------------------------------------------------------- > diff --git > a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java > > b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java > new file mode 100644 > index 0000000..a7c68b1 > --- /dev/null > +++ > b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java > @@ -0,0 +1,44 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one or more > + * contributor license agreements. See the NOTICE file distributed with > + * this work for additional information regarding copyright ownership. > + * The ASF licenses this file to You 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 org.apache.logging.log4j.core.config.plugins.validation.constraints; > + > +import java.lang.annotation.Documented; > +import java.lang.annotation.ElementType; > +import java.lang.annotation.Retention; > +import java.lang.annotation.RetentionPolicy; > +import java.lang.annotation.Target; > + > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint; > +import > org.apache.logging.log4j.core.config.plugins.validation.validators.ValidPortValidator; > + > +/** > + * Indicates that a plugin attribute must be a valid port number. A valid > port number is an integer between 0 and > + * 65535. > + * > + * @since 2.8 > + */ > +@Documented > +@Retention(RetentionPolicy.RUNTIME) > +@Target({ElementType.FIELD, ElementType.PARAMETER}) > +@Constraint(ValidPortValidator.class) > +public @interface ValidPort { > + > + /** > + * The message to be logged if this constraint is violated. This should > normally be overridden. > + */ > + String message() default "The port number is invalid"; > +} > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java > ---------------------------------------------------------------------- > diff --git > a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java > > b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java > new file mode 100644 > index 0000000..3669915 > --- /dev/null > +++ > b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java > @@ -0,0 +1,58 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one or more > + * contributor license agreements. See the NOTICE file distributed with > + * this work for additional information regarding copyright ownership. > + * The ASF licenses this file to You 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 org.apache.logging.log4j.core.config.plugins.validation.validators; > + > +import org.apache.logging.log4j.Logger; > +import > org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator; > +import > org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost; > +import org.apache.logging.log4j.status.StatusLogger; > + > +import java.net.InetAddress; > +import java.net.UnknownHostException; > + > +/** > + * Validator that checks an object to verify it is a valid hostname or IP > address. Validation rules follow the same > + * logic as in {@link InetAddress#getByName(String)}. > + * > + * @since 2.8 > + */ > +public class ValidHostValidator implements ConstraintValidator<ValidHost> { > + > + private static final Logger LOGGER = StatusLogger.getLogger(); > + > + private ValidHost annotation; > + > + @Override > + public void initialize(ValidHost annotation) { > + this.annotation = annotation; > + } > + > + @Override > + public boolean isValid(String name, Object value) { > + if (value == null) { > + LOGGER.error(annotation.message()); > + return false; > + } > + try { > + InetAddress.getByName(value.toString()); > + return true; > + } catch (final UnknownHostException e) { > + LOGGER.error(annotation.message(), e); > + return false; > + } > + } > +} > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java > ---------------------------------------------------------------------- > diff --git > a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java > > b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java > new file mode 100644 > index 0000000..f18f8fc > --- /dev/null > +++ > b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java > @@ -0,0 +1,53 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one or more > + * contributor license agreements. See the NOTICE file distributed with > + * this work for additional information regarding copyright ownership. > + * The ASF licenses this file to You 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 org.apache.logging.log4j.core.config.plugins.validation.validators; > + > +import org.apache.logging.log4j.Logger; > +import > org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator; > +import > org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort; > +import org.apache.logging.log4j.status.StatusLogger; > + > +/** > + * Validator that checks an object to verify it is a valid port number (an > integer between 0 and 65535). > + * > + * @since 2.8 > + */ > +public class ValidPortValidator implements ConstraintValidator<ValidPort> { > + > + private static final Logger LOGGER = StatusLogger.getLogger(); > + > + private ValidPort annotation; > + > + @Override > + public void initialize(final ValidPort annotation) { > + this.annotation = annotation; > + } > + > + @Override > + public boolean isValid(final String name, final Object value) { > + if (!Integer.class.isInstance(value)) { > + LOGGER.error(annotation.message()); > + return false; > + } > + int port = (int) value; > + if (port < 0 || port > 65535) { > + LOGGER.error(annotation.message()); > + return false; > + } > + return true; > + } > +} > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java > ---------------------------------------------------------------------- > diff --git > a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java > > b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java > new file mode 100644 > index 0000000..4f05d68 > --- /dev/null > +++ > b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java > @@ -0,0 +1,46 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one or more > + * contributor license agreements. See the NOTICE file distributed with > + * this work for additional information regarding copyright ownership. > + * The ASF licenses this file to You 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 org.apache.logging.log4j.core.config.plugins.validation; > + > +import java.net.InetSocketAddress; > + > +import org.apache.logging.log4j.core.config.plugins.Plugin; > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute; > +import org.apache.logging.log4j.core.config.plugins.PluginFactory; > +import > org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost; > +import > org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort; > + > +@Plugin(name = "HostAndPort", category = "Test") > +public class HostAndPort { > + > + private final InetSocketAddress address; > + > + private HostAndPort(final InetSocketAddress address) { > + this.address = address; > + } > + > + public boolean isValid() { > + return !address.isUnresolved(); > + } > + > + @PluginFactory > + public static HostAndPort createPlugin( > + @ValidHost(message = "Unit test (host)") @PluginAttribute("host") > final String host, > + @ValidPort(message = "Unit test (port)") @PluginAttribute("port") > final int port) { > + return new HostAndPort(new InetSocketAddress(host, port)); > + } > +} > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java > ---------------------------------------------------------------------- > diff --git > a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java > > b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java > new file mode 100644 > index 0000000..3b0480d > --- /dev/null > +++ > b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java > @@ -0,0 +1,74 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one or more > + * contributor license agreements. See the NOTICE file distributed with > + * this work for additional information regarding copyright ownership. > + * The ASF licenses this file to You 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 org.apache.logging.log4j.core.config.plugins.validation.validators; > + > +import java.util.UUID; > + > +import org.apache.logging.log4j.core.config.Node; > +import org.apache.logging.log4j.core.config.NullConfiguration; > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder; > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager; > +import org.apache.logging.log4j.core.config.plugins.util.PluginType; > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort; > +import org.junit.Before; > +import org.junit.Test; > + > +import static org.junit.Assert.*; > + > +public class ValidHostValidatorTest { > + > + private PluginType<HostAndPort> plugin; > + private Node node; > + > + @SuppressWarnings("unchecked") > + @Before > + public void setUp() throws Exception { > + final PluginManager manager = new PluginManager("Test"); > + manager.collectPlugins(); > + plugin = (PluginType<HostAndPort>) > manager.getPluginType("HostAndPort"); > + assertNotNull("Rebuild this module to ensure annotation processing > has been done.", plugin); > + node = new Node(null, "HostAndPort", plugin); > + } > + > + @Test > + public void testNullHost() throws Exception { > + assertNull(buildPlugin()); > + } > + > + @Test > + public void testInvalidIpAddress() throws Exception { > + node.getAttributes().put("host", UUID.randomUUID().toString()); > + node.getAttributes().put("port", "1"); > + assertNull(buildPlugin()); > + } > + > + @Test > + public void testLocalhost() throws Exception { > + node.getAttributes().put("host", "localhost"); > + node.getAttributes().put("port", "1"); > + final HostAndPort hostAndPort = buildPlugin(); > + assertNotNull(hostAndPort); > + assertTrue(hostAndPort.isValid()); > + } > + > + private HostAndPort buildPlugin() { > + return (HostAndPort) new PluginBuilder(plugin) > + .withConfiguration(new NullConfiguration()) > + .withConfigurationNode(node) > + .build(); > + } > +} > \ No newline at end of file > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java > ---------------------------------------------------------------------- > diff --git > a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java > > b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java > new file mode 100644 > index 0000000..3aab08d > --- /dev/null > +++ > b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java > @@ -0,0 +1,70 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one or more > + * contributor license agreements. See the NOTICE file distributed with > + * this work for additional information regarding copyright ownership. > + * The ASF licenses this file to You 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 org.apache.logging.log4j.core.config.plugins.validation.validators; > + > +import org.apache.logging.log4j.core.config.Node; > +import org.apache.logging.log4j.core.config.NullConfiguration; > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder; > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager; > +import org.apache.logging.log4j.core.config.plugins.util.PluginType; > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort; > +import org.junit.Before; > +import org.junit.Test; > + > +import static org.junit.Assert.*; > + > +public class ValidPortValidatorTest { > + private PluginType<HostAndPort> plugin; > + private Node node; > + > + @SuppressWarnings("unchecked") > + @Before > + public void setUp() throws Exception { > + final PluginManager manager = new PluginManager("Test"); > + manager.collectPlugins(); > + plugin = (PluginType<HostAndPort>) > manager.getPluginType("HostAndPort"); > + assertNotNull("Rebuild this module to ensure annotation processing > has been done.", plugin); > + node = new Node(null, "HostAndPort", plugin); > + node.getAttributes().put("host", "localhost"); > + } > + > + @Test > + public void testNegativePort() throws Exception { > + node.getAttributes().put("port", "-1"); > + assertNull(buildPlugin()); > + } > + > + @Test > + public void testValidPort() throws Exception { > + node.getAttributes().put("port", "10"); > + assertNotNull(buildPlugin()); > + } > + > + @Test > + public void testInvalidPort() throws Exception { > + node.getAttributes().put("port", "1234567890"); > + assertNull(buildPlugin()); > + } > + > + private HostAndPort buildPlugin() { > + return (HostAndPort) new PluginBuilder(plugin) > + .withConfiguration(new NullConfiguration()) > + .withConfigurationNode(node) > + .build(); > + } > + > +} > \ No newline at end of file > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml > ---------------------------------------------------------------------- > diff --git a/src/changes/changes.xml b/src/changes/changes.xml > index c05de09..bc08dbb 100644 > --- a/src/changes/changes.xml > +++ b/src/changes/changes.xml > @@ -213,6 +213,9 @@ > <action issue="LOG4J2-1302" dev="rpopma" type="update"> > The log4j-slf4j-impl module now declares a runtime dependency on > log4j-core. While not technically required, this makes the log4j-slf4j-impl > module behave similarly to slf4j-log4j12, and facilitates migration to Log4j > 2. > </action> > + <action issue="LOG4J2-1755" dev="mattsicker" type="add"> > + Add converters and validators related to hostnames and ports. > + </action> > <action issue="LOG4J2-969" dev="ggregory" type="add"> > Refactor SyslogAppender so that Layout is a Plugin element. > </action> > > --------------------------------------------------------------------- To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org For additional commands, e-mail: log4j-dev-h...@logging.apache.org