@Test public void testValidateDomainNameIsValidIdna() { for (String domain : validArpaDomain) { assertTrue(validateDomainNameIsValidIdna(domain)); } for (String domain : inValidArpaDomain) { assertFalse(validateDomainNameIsValidIdna(domain)); } assertTrue(validateDomainNameIsValidIdna("ａ.cn")); assertTrue(validateDomainNameIsValidIdna("cnnic.cn")); assertTrue(validateDomainNameIsValidIdna("cnnic.cn.")); assertTrue(validateDomainNameIsValidIdna("cnnic.com.cn")); assertTrue(validateDomainNameIsValidIdna("1cnnic.cn")); assertFalse(validateDomainNameIsValidIdna(" cnnic.cn")); assertFalse(validateDomainNameIsValidIdna(" cnnic.cn ")); assertFalse(validateDomainNameIsValidIdna("cnnic.cn ")); assertFalse(validateDomainNameIsValidIdna("我cnnic-.cn")); assertFalse(validateDomainNameIsValidIdna("-我cnnic.cn")); assertFalse(validateDomainNameIsValidIdna("cnnic-.cn")); assertFalse(validateDomainNameIsValidIdna("-cnnic.cn")); assertFalse(validateDomainNameIsValidIdna("boss.-cnnic.cn")); assertFalse(validateDomainNameIsValidIdna("boss.cnnic-.cn")); assertFalse(validateDomainNameIsValidIdna("boss.cnnic.-cn")); assertFalse(validateDomainNameIsValidIdna("boss.cnnic.cn-")); assertFalse(validateDomainNameIsValidIdna("cnnic.")); assertFalse(validateDomainNameIsValidIdna("123")); assertFalse(validateDomainNameIsValidIdna("c nnic.cn")); assertFalse(validateDomainNameIsValidIdna("cnnic")); assertFalse(validateDomainNameIsValidIdna("")); assertFalse(validateDomainNameIsValidIdna(" ")); assertFalse(validateDomainNameIsValidIdna(null)); assertFalse(validateDomainNameIsValidIdna(".")); assertFalse(validateDomainNameIsValidIdna("a.")); assertFalse(validateDomainNameIsValidIdna(".a")); assertFalse(validateDomainNameIsValidIdna("Σ.cn")); assertFalse(validateDomainNameIsValidIdna("@.cn")); assertFalse(validateDomainNameIsValidIdna("a@.cn")); assertFalse(validateDomainNameIsValidIdna("@a.cn")); assertFalse(validateDomainNameIsValidIdna("xn--55qx5d.中国")); assertFalse(DomainUtil.validateDomainNameIsValidIdna( "xn--55qx5d.中国.cn", false)); assertTrue(validateDomainNameIsValidIdna("中国.CN")); assertFalse(validateDomainNameIsValidIdna("55--123.xn--fiqs8s")); assertTrue(DomainUtil.validateDomainNameIsValidIdna( "55--123.xn--fiqs8s", true)); assertTrue(validateDomainNameIsValidIdna("xn--55QX5D.CN")); assertTrue(validateDomainNameIsValidIdna("xn--55qx5d.cn")); assertFalse(validateDomainNameIsValidIdna("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.")); assertTrue(validateDomainNameIsValidIdna("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.aaa.")); assertTrue(validateDomainNameIsValidIdna("xn--notBase64Code.cn")); assertTrue(validateDomainNameIsValidIdna("xn--hxaajaoebldbselhkqsqmapxidccaaahjrgk3chhdip9bclcgddbb4ooioa.bnnhg")); assertTrue(validateDomainNameIsValidIdna("%CF%83%CE%B5%CE%B9%CF%81%CE%AC%CF%84%CE%AC%CE%BE%CE%B7%CF%83%CF%85%CF%80%CE%BF%CF%85%CF%81%CE%B3%CE%B5%CE%AF%CF%89%CE%BD%CF%83%CF%8D%CE%BD%CE%B8%CE%B5%CF%83%CE%B7%CF%85%CF%80%CE%BF%CF%85%CF%81%CE%B3%CE%B9%CE%BA%CE%BF%CF%8D%CF%83%CF%85%CE%BC%CE%B2%CE%BF%CF%85%CE%BB%CE%AF%CE%BF%CF%85%CE%BF%CF%85%CE%BF%CF%85%CE%BF.bnnhg")); assertTrue(validateDomainNameIsValidIdna("xn--hxaajaoebldbselhkqsqmapxidccaaahjrgk3chhdip9bclcgddbb4ooioa.bnnhg")); assertTrue(validateDomainNameIsValidIdna("σειράτάξησυπουργείωνσύνθεσηυπουργικούσυμβουλίουουουο.bnnhg")); assertFalse(validateDomainNameIsValidIdna("xn--caf%C3%A9s.com")); assertFalse(validateDomainNameIsValidIdna("xn--cafés.com")); assertFalse(validateDomainNameIsValidIdna("%CF%83%CE%B5%CE%B9%CF%81%CE%AC%CF%84%CE%AC%CE%BE%CE%B7%CF%83%CF%85%CF%80%CE%BF%CF%85%CF%81%CE%B3%CE%B5%CE%AF%CF%89%CE%BD%CF%83%CF%8D%CE%BD%CE%B8%CE%B5%CF%83%CE%B7%CF%85%CF%80%CE%BF%CF%85%CF%81%CE%B3%CE%B9%CE%BA%CE%BF%CF%8D%CF%83%CF%85%CE%BC%CE%B2%CE%BF%CF%85%CE%BB%CE%AF%CE%BF%CF%85%CE%BF%CF%85%CE%BF%CF%85%CE%BF%CE%BF.bnnhg")); assertFalse(validateDomainNameIsValidIdna("xn--hxaajaoebldbselhkqsqmapxidccaaaahjsgk5chhdiq0cclcgddbb8o9hoa.bnnhg")); assertFalse(validateDomainNameIsValidIdna("σειράτάξησυπουργείωνσύνθεσηυπουργικούσυμβουλίουουουοο.bnnhg")); assertFalse(validateDomainNameIsValidIdna("%CF%83%CE%B5%CE%B9%CF%81%CE%AC%CF%84%CE%AC%CE%BE%CE%B7%CF%83%CF%85%CF%80%CE%BF%CF%85%CF%81%CE%B3%CE%B5%CE%AF%CF%89%CE%BD%CE%A3%CF%8D%CE%BD%CE%B8%CE%B5%CF%83%CE%B7%CF%85%CF%80%CE%BF%CF%85%CF%81%CE%B3%CE%B9%CE%BA%CE%BF%CF%8D%CF%83%CF%85%CE%BC%CE%B2%CE%BF%CF%85%CE%BB%CE%AF%CE%BF%CF%85%CE%BF%CF%85%CE%BF%CF%85%CE%BF.bnnhg")); assertFalse(validateDomainNameIsValidIdna("σειράτάξησυπουργείωνΣύνθεσηυπουργικούσυμβουλίουουουο.bnnhg")); assertFalse(validateDomainNameIsValidIdna("xn--.bnnhg")); assertFalse(validateDomainNameIsValidIdna("xn--1.bnnhg")); assertTrue(validateDomainNameIsValidIdna("σειράτάξησυπουργείωνσύνθεσηυπουργικούσυμβουλίουουουο.bnnhg")); assertTrue(validateDomainNameIsValidIdna("xn--hxaajaoebldbselhkqsqmapxidccaaahjrgk3chhdip9bclcgddbb4ooioa.bnnhg")); assertFalse(validateDomainNameIsValidIdna("xnxnhopefullynonexisting<*")); assertTrue(validateDomainNameIsValidIdna("xn--4xA.bnnhg")); assertFalse(validateDomainNameIsValidIdna("xn--7wA.bnnhg")); assertFalse(validateDomainNameIsValidIdna("xn--7warambrecmdctfmhlrtrmbpyjdccaaahkrgl4chlip9bclcgddbb4ooioa.bnnhg")); assertTrue(validateDomainNameIsValidIdna("σ.bnnhg")); assertTrue(validateDomainNameIsValidIdna("ς.bnnhg")); assertTrue(validateDomainNameIsValidIdna("ß.bnnhg")); assertFalse(validateDomainNameIsValidIdna("Σ.bnnhg")); assertFalse(validateDomainNameIsValidIdna("√.com")); assertFalse(validateDomainNameIsValidIdna("ÖBB.at")); assertFalse(validateDomainNameIsValidIdna("Ⱥbby.com")); assertTrue(validateDomainNameIsValidIdna("\u0205.com")); assertFalse(validateDomainNameIsValidIdna("cnnic%25.cn")); assertFalse(validateDomainNameIsValidIdna("123〳.中国")); assertFalse(validateDomainNameIsValidIdna("123Ā.中国")); assertFalse(validateDomainNameIsValidIdna("123Ƽ.中国")); assertFalse(validateDomainNameIsValidIdna("123஝஝஝஝.中国")); assertFalse(validateDomainNameIsValidIdna("123઱஝஝.中国")); assertFalse(validateDomainNameIsValidIdna("123�஝஝.中国")); assertFalse(validateDomainNameIsValidIdna("cnnic。.cn")); assertFalse(validateDomainNameIsValidIdna("测试。．中国.cn")); }
public static boolean validateDomainNameIsValidIdna(String domainName, boolean forSearch) { if (StringUtils.isBlank(domainName) || !domainName.contains(".")) { return false; } if (domainName.contains(BLANK_IN_DOMAIN)) { return false; } if (StringUtils.containsAny(domainName, DISALLOWED_DELIMITERS)) { return false; } if (!forSearch && !isArpaTldAndLabelIsValid(domainName)) { return false; } String punyDomainName = domainName; try { punyDomainName = geneDomainPunyName(domainName); } catch (Exception e) { LOGGER.error("generate puny name error:" + e.getMessage()); return false; } String punyWithoutLastPoint = deleteLastPoint(punyDomainName); if (!validateDomainLength(punyWithoutLastPoint)) { return false; } String domainNameWithoutLastPoint = deleteLastPoint(domainName); String[] splits = StringUtils.split(punyWithoutLastPoint, "."); if (splits.length > MAX_DOMAIN_LABEL) { return false; } if (nonReservedAsciiDomain(domainName, punyDomainName, punyWithoutLastPoint, domainNameWithoutLastPoint, forSearch)) { return isLdh(domainName); } domainName = deleteLastPoint(domainName); if (StringUtils.isBlank(domainName) || !domainName.contains(".")) { return false; } return IdnaUtil.isValidIdn(domainName); }
DomainUtil { public static boolean validateDomainNameIsValidIdna(String domainName, boolean forSearch) { if (StringUtils.isBlank(domainName) || !domainName.contains(".")) { return false; } if (domainName.contains(BLANK_IN_DOMAIN)) { return false; } if (StringUtils.containsAny(domainName, DISALLOWED_DELIMITERS)) { return false; } if (!forSearch && !isArpaTldAndLabelIsValid(domainName)) { return false; } String punyDomainName = domainName; try { punyDomainName = geneDomainPunyName(domainName); } catch (Exception e) { LOGGER.error("generate puny name error:" + e.getMessage()); return false; } String punyWithoutLastPoint = deleteLastPoint(punyDomainName); if (!validateDomainLength(punyWithoutLastPoint)) { return false; } String domainNameWithoutLastPoint = deleteLastPoint(domainName); String[] splits = StringUtils.split(punyWithoutLastPoint, "."); if (splits.length > MAX_DOMAIN_LABEL) { return false; } if (nonReservedAsciiDomain(domainName, punyDomainName, punyWithoutLastPoint, domainNameWithoutLastPoint, forSearch)) { return isLdh(domainName); } domainName = deleteLastPoint(domainName); if (StringUtils.isBlank(domainName) || !domainName.contains(".")) { return false; } return IdnaUtil.isValidIdn(domainName); } }
DomainUtil { public static boolean validateDomainNameIsValidIdna(String domainName, boolean forSearch) { if (StringUtils.isBlank(domainName) || !domainName.contains(".")) { return false; } if (domainName.contains(BLANK_IN_DOMAIN)) { return false; } if (StringUtils.containsAny(domainName, DISALLOWED_DELIMITERS)) { return false; } if (!forSearch && !isArpaTldAndLabelIsValid(domainName)) { return false; } String punyDomainName = domainName; try { punyDomainName = geneDomainPunyName(domainName); } catch (Exception e) { LOGGER.error("generate puny name error:" + e.getMessage()); return false; } String punyWithoutLastPoint = deleteLastPoint(punyDomainName); if (!validateDomainLength(punyWithoutLastPoint)) { return false; } String domainNameWithoutLastPoint = deleteLastPoint(domainName); String[] splits = StringUtils.split(punyWithoutLastPoint, "."); if (splits.length > MAX_DOMAIN_LABEL) { return false; } if (nonReservedAsciiDomain(domainName, punyDomainName, punyWithoutLastPoint, domainNameWithoutLastPoint, forSearch)) { return isLdh(domainName); } domainName = deleteLastPoint(domainName); if (StringUtils.isBlank(domainName) || !domainName.contains(".")) { return false; } return IdnaUtil.isValidIdn(domainName); } private  DomainUtil(); }
DomainUtil { public static boolean validateDomainNameIsValidIdna(String domainName, boolean forSearch) { if (StringUtils.isBlank(domainName) || !domainName.contains(".")) { return false; } if (domainName.contains(BLANK_IN_DOMAIN)) { return false; } if (StringUtils.containsAny(domainName, DISALLOWED_DELIMITERS)) { return false; } if (!forSearch && !isArpaTldAndLabelIsValid(domainName)) { return false; } String punyDomainName = domainName; try { punyDomainName = geneDomainPunyName(domainName); } catch (Exception e) { LOGGER.error("generate puny name error:" + e.getMessage()); return false; } String punyWithoutLastPoint = deleteLastPoint(punyDomainName); if (!validateDomainLength(punyWithoutLastPoint)) { return false; } String domainNameWithoutLastPoint = deleteLastPoint(domainName); String[] splits = StringUtils.split(punyWithoutLastPoint, "."); if (splits.length > MAX_DOMAIN_LABEL) { return false; } if (nonReservedAsciiDomain(domainName, punyDomainName, punyWithoutLastPoint, domainNameWithoutLastPoint, forSearch)) { return isLdh(domainName); } domainName = deleteLastPoint(domainName); if (StringUtils.isBlank(domainName) || !domainName.contains(".")) { return false; } return IdnaUtil.isValidIdn(domainName); } private  DomainUtil(); static boolean isArpaTldAndLabelIsValid(String domainName); static boolean validateDomainNameIsValidIdna(String domainName,
            boolean forSearch); static boolean validateSearchStringIsValidIdna(String searchString); static String deleteLastPoint(String paramStr); static String getLowerCaseByLabel(String str); static String safeGeneDomainPunyName(String domainName); static String geneDomainPunyName(String domainName); static String urlDecodeAndReplaceAsciiToLowercase(String str); static String replaceAsciiToLowercase(String str); static String urlDecode(String str); static String iso8859Decode(String str); static boolean isLdh(String domain); static boolean validateSearchLdnName(String searchString); }
DomainUtil { public static boolean validateDomainNameIsValidIdna(String domainName, boolean forSearch) { if (StringUtils.isBlank(domainName) || !domainName.contains(".")) { return false; } if (domainName.contains(BLANK_IN_DOMAIN)) { return false; } if (StringUtils.containsAny(domainName, DISALLOWED_DELIMITERS)) { return false; } if (!forSearch && !isArpaTldAndLabelIsValid(domainName)) { return false; } String punyDomainName = domainName; try { punyDomainName = geneDomainPunyName(domainName); } catch (Exception e) { LOGGER.error("generate puny name error:" + e.getMessage()); return false; } String punyWithoutLastPoint = deleteLastPoint(punyDomainName); if (!validateDomainLength(punyWithoutLastPoint)) { return false; } String domainNameWithoutLastPoint = deleteLastPoint(domainName); String[] splits = StringUtils.split(punyWithoutLastPoint, "."); if (splits.length > MAX_DOMAIN_LABEL) { return false; } if (nonReservedAsciiDomain(domainName, punyDomainName, punyWithoutLastPoint, domainNameWithoutLastPoint, forSearch)) { return isLdh(domainName); } domainName = deleteLastPoint(domainName); if (StringUtils.isBlank(domainName) || !domainName.contains(".")) { return false; } return IdnaUtil.isValidIdn(domainName); } private  DomainUtil(); static boolean isArpaTldAndLabelIsValid(String domainName); static boolean validateDomainNameIsValidIdna(String domainName,
            boolean forSearch); static boolean validateSearchStringIsValidIdna(String searchString); static String deleteLastPoint(String paramStr); static String getLowerCaseByLabel(String str); static String safeGeneDomainPunyName(String domainName); static String geneDomainPunyName(String domainName); static String urlDecodeAndReplaceAsciiToLowercase(String str); static String replaceAsciiToLowercase(String str); static String urlDecode(String str); static String iso8859Decode(String str); static boolean isLdh(String domain); static boolean validateSearchLdnName(String searchString); static final String XN_PREFIX; static final String ACE_PREFIX; static final int MAX_DOMAIN_LENGTH_WITHOUT_LAST_DOT; static final int MIN_DOMAIN_LENGTH_WITHOUT_LAST_DOT; static final String ARPA_SUFFIX; static final String IPV4_ARPA_SUFFIX; static final String IPV6_ARPA_SUFFIX; static final int MAX_ASCII_CODE; static final String BLANK_IN_DOMAIN; static final java.lang.String DISALLOWED_DELIMITERS; }