OutgoingEmail: Allow 'local' as a valid TLD for outgoing mails

Add 'local' to the list of overrides on the domain validator.

Split the validation out to a utility class to make it easier to invoke
the same code from tests.

Bug: Issue 4332
Change-Id: I95bfabeeb588a5ee9ff49265698cef31c494d788
This commit is contained in:
David Pursehouse
2016-08-05 14:47:41 +09:00
parent 199bbae076
commit b16e464c9d
3 changed files with 39 additions and 6 deletions

View File

@@ -27,7 +27,6 @@ import com.google.gerrit.server.validators.ValidationException;
import com.google.gwtorm.server.OrmException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.routines.EmailValidator;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.InternalContextAdapterImpl;
@@ -382,7 +381,7 @@ public abstract class OutgoingEmail {
/** Schedule delivery of this message to the given account. */
protected void add(final RecipientType rt, final Address addr) {
if (addr != null && addr.email != null && addr.email.length() > 0) {
if (!EmailValidator.getInstance().isValid(addr.email)) {
if (!OutgoingEmailValidator.isValid(addr.email)) {
log.warn("Not emailing " + addr.email + " (invalid email address)");
} else if (!args.emailSender.canEmail(addr.email)) {
log.warn("Not emailing " + addr.email + " (prohibited by allowrcpt)");

View File

@@ -0,0 +1,30 @@
// Copyright (C) 2016 The Android Open Source Project
//
// 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 com.google.gerrit.server.mail;
import static org.apache.commons.validator.routines.DomainValidator.ArrayType.GENERIC_PLUS;
import org.apache.commons.validator.routines.DomainValidator;
import org.apache.commons.validator.routines.EmailValidator;
public class OutgoingEmailValidator {
static {
DomainValidator.updateTLDOverride(GENERIC_PLUS, new String[]{"local"});
}
public static boolean isValid(String addr) {
return EmailValidator.getInstance(true, true).isValid(addr);
}
}

View File

@@ -15,8 +15,8 @@
package com.google.gerrit.server.mail;
import static com.google.common.truth.Truth.assert_;
import static com.google.common.truth.Truth.assertThat;
import org.apache.commons.validator.routines.EmailValidator;
import org.junit.Test;
import java.io.BufferedReader;
@@ -26,6 +26,11 @@ import java.io.InputStreamReader;
public class ValidatorTest {
private static final String UNSUPPORTED_PREFIX = "#! ";
@Test
public void validateLocalDomain() throws Exception {
assertThat(OutgoingEmailValidator.isValid("foo@bar.local")).isTrue();
}
@Test
public void validateTopLevelDomains() throws Exception {
try (InputStream in =
@@ -35,7 +40,6 @@ public class ValidatorTest {
}
BufferedReader r = new BufferedReader(new InputStreamReader(in));
String tld;
EmailValidator validator = EmailValidator.getInstance();
while ((tld = r.readLine()) != null) {
if (tld.startsWith("# ") || tld.startsWith("XN--")) {
// Ignore comments and non-latin domains
@@ -46,13 +50,13 @@ public class ValidatorTest {
+ tld.toLowerCase().substring(UNSUPPORTED_PREFIX.length());
assert_()
.withFailureMessage("expected invalid TLD \"" + test + "\"")
.that(validator.isValid(test))
.that(OutgoingEmailValidator.isValid(test))
.isFalse();
} else {
String test = "test@example." + tld.toLowerCase();
assert_()
.withFailureMessage("failed to validate TLD \"" + test + "\"")
.that(validator.isValid(test))
.that(OutgoingEmailValidator.isValid(test))
.isTrue();
}
}