mirror of
https://github.com/google/nomulus
synced 2026-04-21 16:50:44 +00:00
Add an IDN helper (#2217)
* Add an IDN helper Add a helper that checks the validity of labels in IDNs. All organizes TLDs according to the IDNs they support.
This commit is contained in:
94
core/src/test/java/google/registry/bsa/IdnCheckerTest.java
Normal file
94
core/src/test/java/google/registry/bsa/IdnCheckerTest.java
Normal file
@@ -0,0 +1,94 @@
|
||||
// Copyright 2023 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 google.registry.bsa;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.tldconfig.idn.IdnTableEnum.EXTENDED_LATIN;
|
||||
import static google.registry.tldconfig.idn.IdnTableEnum.JA;
|
||||
import static google.registry.tldconfig.idn.IdnTableEnum.UNCONFUSABLE_LATIN;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import google.registry.model.tld.Tld;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class IdnCheckerTest {
|
||||
|
||||
@Mock Tld jaonly;
|
||||
@Mock Tld jandelatin;
|
||||
@Mock Tld strictlatin;
|
||||
IdnChecker idnChecker;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
idnChecker =
|
||||
new IdnChecker(
|
||||
ImmutableMap.of(
|
||||
JA,
|
||||
ImmutableSet.of(jandelatin, jaonly),
|
||||
EXTENDED_LATIN,
|
||||
ImmutableSet.of(jandelatin),
|
||||
UNCONFUSABLE_LATIN,
|
||||
ImmutableSet.of(strictlatin)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllValidIdns_allTlds() {
|
||||
assertThat(idnChecker.getAllValidIdns("all"))
|
||||
.containsExactly(EXTENDED_LATIN, JA, UNCONFUSABLE_LATIN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllValidIdns_notJa() {
|
||||
assertThat(idnChecker.getAllValidIdns("à")).containsExactly(EXTENDED_LATIN, UNCONFUSABLE_LATIN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllValidIdns_extendedLatinOnly() {
|
||||
assertThat(idnChecker.getAllValidIdns("á")).containsExactly(EXTENDED_LATIN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllValidIdns_jaOnly() {
|
||||
assertThat(idnChecker.getAllValidIdns("っ")).containsExactly(JA);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllValidIdns_none() {
|
||||
assertThat(idnChecker.getAllValidIdns("д")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSupportingTlds_singleTld_success() {
|
||||
assertThat(idnChecker.getSupportingTlds(ImmutableSet.of("EXTENDED_LATIN")))
|
||||
.containsExactly(jandelatin);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSupportingTlds_multiTld_success() {
|
||||
assertThat(idnChecker.getSupportingTlds(ImmutableSet.of("JA")))
|
||||
.containsExactly(jandelatin, jaonly);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getForbiddingTlds_success() {
|
||||
assertThat(idnChecker.getForbiddingTlds(ImmutableSet.of("JA"))).containsExactly(strictlatin);
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,14 @@
|
||||
|
||||
package google.registry.tldconfig.idn;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static google.registry.testing.DatabaseHelper.createTld;
|
||||
import static google.registry.testing.DatabaseHelper.persistResource;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import google.registry.model.tld.Tld;
|
||||
import google.registry.persistence.transaction.JpaTestExtensions;
|
||||
import google.registry.persistence.transaction.JpaTestExtensions.JpaIntegrationTestExtension;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -118,4 +121,24 @@ class IdnLabelValidatorTest {
|
||||
// Extended Latin shouldn't include Japanese characters
|
||||
assertThat(idnLabelValidator.findValidIdnTableForTld("みんな", "tld")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetIdnTablesForTld_custom() {
|
||||
persistResource(
|
||||
createTld("tld")
|
||||
.asBuilder()
|
||||
.setIdnTables(ImmutableSet.of(IdnTableEnum.EXTENDED_LATIN))
|
||||
.build());
|
||||
Tld tld = tm().transact(() -> tm().loadByKey(Tld.createVKey("tld")));
|
||||
assertThat(idnLabelValidator.getIdnTablesForTld(tld))
|
||||
.containsExactly(IdnTableEnum.EXTENDED_LATIN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetIdnTablesForTld_default() {
|
||||
persistResource(createTld("tld").asBuilder().build());
|
||||
Tld tld = tm().transact(() -> tm().loadByKey(Tld.createVKey("tld")));
|
||||
assertThat(idnLabelValidator.getIdnTablesForTld(tld))
|
||||
.containsExactly(IdnTableEnum.EXTENDED_LATIN, IdnTableEnum.JA);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user