Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add RejectDomainNamePublicSuffixValidator
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...main/java/uk/org/iay/incommon/mda/validate/net/RejectDomainNamePublicSuffixValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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 uk.org.iay.incommon.mda.validate.net; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import com.google.common.net.InternetDomainName; | ||
|
||
import net.shibboleth.metadata.Item; | ||
import net.shibboleth.metadata.validate.Validator; | ||
import uk.org.iay.incommon.mda.validate.BaseLocalValidator; | ||
|
||
/** | ||
* A validator that rejects an {@link InternetDomainName} if it is a public suffix. | ||
*/ | ||
public class RejectDomainNamePublicSuffixValidator extends BaseLocalValidator | ||
implements Validator<InternetDomainName> { | ||
|
||
@Override | ||
public Action validate(@Nonnull final InternetDomainName domain, @Nonnull final Item<?> item, | ||
@Nonnull final String stageId) { | ||
if (domain.isPublicSuffix()) { | ||
addErrorMessage(domain, item, stageId); | ||
return Action.DONE; | ||
} else { | ||
return Action.CONTINUE; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
.../java/uk/org/iay/incommon/mda/validate/net/RejectDomainNamePublicSuffixValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package uk.org.iay.incommon.mda.validate.net; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import com.google.common.net.InternetDomainName; | ||
|
||
import net.shibboleth.metadata.ErrorStatus; | ||
import net.shibboleth.metadata.Item; | ||
import net.shibboleth.metadata.validate.Validator.Action; | ||
import uk.org.ukfederation.mda.MockItem; | ||
|
||
public class RejectDomainNamePublicSuffixValidatorTest { | ||
|
||
@Test | ||
public void normal() throws Exception { | ||
final Item<String> item = new MockItem("content"); | ||
final RejectDomainNamePublicSuffixValidator val = | ||
new RejectDomainNamePublicSuffixValidator(); | ||
val.setId("validate"); | ||
val.initialize(); | ||
|
||
final InternetDomainName domain = InternetDomainName.from("example.org"); | ||
Assert.assertNotNull(domain); | ||
final Action res = val.validate(domain, item, "stage"); | ||
Assert.assertNotNull(res); | ||
Assert.assertEquals(res, Action.CONTINUE); | ||
Assert.assertEquals(item.getItemMetadata().get(ErrorStatus.class).size(), 0); | ||
|
||
Assert.assertEquals(val.validate(InternetDomainName.from("ed.ac.uk"), item, "stage"), Action.CONTINUE); | ||
Assert.assertEquals(item.getItemMetadata().get(ErrorStatus.class).size(), 0); | ||
|
||
Assert.assertEquals(val.validate(InternetDomainName.from("complete.nonsense"), item, "stage"), Action.CONTINUE); | ||
Assert.assertEquals(item.getItemMetadata().get(ErrorStatus.class).size(), 0); | ||
} | ||
|
||
@Test | ||
public void uk() throws Exception { | ||
final Item<String> item = new MockItem("content"); | ||
final RejectDomainNamePublicSuffixValidator val = | ||
new RejectDomainNamePublicSuffixValidator(); | ||
val.setId("validate"); | ||
val.initialize(); | ||
|
||
final InternetDomainName domain = InternetDomainName.from("uk"); | ||
Assert.assertNotNull(domain); | ||
final Action res = val.validate(domain, item, "stage"); | ||
Assert.assertNotNull(res); | ||
Assert.assertEquals(res, Action.DONE); | ||
Assert.assertEquals(item.getItemMetadata().get(ErrorStatus.class).size(), 1); | ||
Assert.assertTrue(item.getItemMetadata().get(ErrorStatus.class).get(0).getStatusMessage().contains("rejected")); | ||
} | ||
|
||
@Test | ||
public void ac_uk() throws Exception { | ||
final Item<String> item = new MockItem("content"); | ||
final RejectDomainNamePublicSuffixValidator val = | ||
new RejectDomainNamePublicSuffixValidator(); | ||
val.setId("validate"); | ||
val.initialize(); | ||
|
||
final InternetDomainName domain = InternetDomainName.from("ac.uk"); | ||
Assert.assertNotNull(domain); | ||
final Action res = val.validate(domain, item, "stage"); | ||
Assert.assertNotNull(res); | ||
Assert.assertEquals(res, Action.DONE); | ||
Assert.assertEquals(item.getItemMetadata().get(ErrorStatus.class).size(), 1); | ||
Assert.assertTrue(item.getItemMetadata().get(ErrorStatus.class).get(0).getStatusMessage().contains("rejected")); | ||
} | ||
|
||
} |