public void testAllowLocal() { DomainValidator noLocal = DomainValidator.getInstance(false); DomainValidator allowLocal = DomainValidator.getInstance(true); // Default is false, and should use singletons assertEquals(noLocal, validator); // Default won't allow local assertFalse("localhost.localdomain should validate", noLocal.isValid("localhost.localdomain")); assertFalse("localhost should validate", noLocal.isValid("localhost")); // But it may be requested assertTrue("localhost.localdomain should validate", allowLocal.isValid("localhost.localdomain")); assertTrue("localhost should validate", allowLocal.isValid("localhost")); assertTrue("hostname should validate", allowLocal.isValid("hostname")); assertTrue("machinename should validate", allowLocal.isValid("machinename")); // Check the localhost one with a few others assertTrue("apache.org should validate", allowLocal.isValid("apache.org")); assertFalse("domain name with spaces shouldn't validate", allowLocal.isValid(" apache.org ")); }
public void testTopLevelDomains() { // infrastructure TLDs assertTrue(".arpa should validate as iTLD", validator.isValidInfrastructureTld(".arpa")); assertFalse(".com shouldn't validate as iTLD", validator.isValidInfrastructureTld(".com")); // generic TLDs assertTrue(".name should validate as gTLD", validator.isValidGenericTld(".name")); assertFalse(".us shouldn't validate as gTLD", validator.isValidGenericTld(".us")); // country code TLDs assertTrue(".uk should validate as ccTLD", validator.isValidCountryCodeTld(".uk")); assertFalse(".org shouldn't validate as ccTLD", validator.isValidCountryCodeTld(".org")); // case-insensitive assertTrue(".COM should validate as TLD", validator.isValidTld(".COM")); assertTrue(".BiZ should validate as TLD", validator.isValidTld(".BiZ")); // corner cases assertFalse("invalid TLD shouldn't validate", validator.isValid(".nope")); // TODO this is not guaranteed invalid forever assertFalse("empty string shouldn't validate as TLD", validator.isValid("")); assertFalse("null shouldn't validate as TLD", validator.isValid(null)); }
/** * Returns true if the specified <code>String</code> matches any * IANA-defined top-level domain. Leading dots are ignored if present. * The search is case-insensitive. * @param tld the parameter to check for TLD status, not null * @return true if the parameter is a TLD */ public boolean isValidTld(String tld) { tld = unicodeToASCII(tld); if(allowLocal && isValidLocalTld(tld)) { return true; } return isValidInfrastructureTld(tld) || isValidGenericTld(tld) || isValidCountryCodeTld(tld); }
@Override public void setUp() { validator = DomainValidator.getInstance(); DomainValidator.clearTLDOverrides(); // N.B. this clears the inUse flag, allowing overrides }
/** * Returns true if the domain component of an email address is valid. * * @param domain being validated, may be in IDN format * @return true if the email address's domain is valid. */ protected boolean isValidDomain(String domain) { // see if domain is an IP address in brackets Matcher ipDomainMatcher = IP_DOMAIN_PATTERN.matcher(domain); if (ipDomainMatcher.matches()) { InetAddressValidator inetAddressValidator = InetAddressValidator.getInstance(); return inetAddressValidator.isValid(ipDomainMatcher.group(1)); } // Domain is symbolic name DomainValidator domainValidator = DomainValidator.getInstance(allowLocal); if (allowTld) { return domainValidator.isValid(domain) || (!domain.startsWith(".") && domainValidator.isValidTld(domain)); } else { return domainValidator.isValid(domain); } }
final String authorityASCII = DomainValidator.unicodeToASCII(authority); DomainValidator domainValidator = DomainValidator.getInstance(isOn(ALLOW_LOCAL_URLS)); if (!domainValidator.isValid(hostLocation)) {
public void testIDNJava6OrLater() { String version = System.getProperty("java.version"); if (version.compareTo("1.6") < 0) { System.out.println("Cannot run Unicode IDN tests"); return; // Cannot run the test } // xn--d1abbgf6aiiy.xn--p1ai http://президент.рф assertTrue("b\u00fccher.ch should validate", validator.isValid("www.b\u00fccher.ch")); assertTrue("xn--d1abbgf6aiiy.xn--p1ai should validate", validator.isValid("xn--d1abbgf6aiiy.xn--p1ai")); assertTrue("президент.рф should validate", validator.isValid("президент.рф")); assertFalse("www.\uFFFD.ch FFFD should fail", validator.isValid("www.\uFFFD.ch")); }
DomainValidator dv = DomainValidator.getInstance(); File txtFile = new File("target/tlds-alpha-by-domain.txt"); long timestamp = download(txtFile, "https://data.iana.org/TLD/tlds-alpha-by-domain.txt", 0L); unicodeTld = asciiTld; if (!dv.isValidTld(asciiTld)) { String [] info = htmlInfo.get(asciiTld); if (info != null) {
public void testVALIDATOR_412() { assertFalse(validator.isValidGenericTld("local")); assertFalse(validator.isValid("abc.local")); assertFalse(validator.isValidGenericTld("pvt")); assertFalse(validator.isValid("abc.pvt")); DomainValidator.updateTLDOverride(ArrayType.GENERIC_PLUS, new String[]{"local", "pvt"}); assertTrue(validator.isValidGenericTld("local")); assertTrue(validator.isValid("abc.local")); assertTrue(validator.isValidGenericTld("pvt")); assertTrue(validator.isValid("abc.pvt")); }
public void testCannotUpdate() { DomainValidator.updateTLDOverride(ArrayType.GENERIC_PLUS, new String[]{"ch"}); // OK DomainValidator dv = DomainValidator.getInstance(); assertNotNull(dv); try { DomainValidator.updateTLDOverride(ArrayType.GENERIC_PLUS, new String[]{"ch"}); fail("Expected IllegalStateException"); } catch (IllegalStateException ise) { // expected } } // Download and process local copy of http://data.iana.org/TLD/tlds-alpha-by-domain.txt
public DomainValidator getDomainValidator() { return DomainValidator.getInstance(this.allowLocalUrls); }
/** * Validates if string <code>ip</code> is a valid generic top-level domain or * not. * * @param domain to be validated. * @return true if valid generic top-level domain. */ public static boolean isGenericTld(String domain) { return DomainValidator.getInstance().isValidGenericTld(domain); }
/** * Validates if string <code>ip</code> is a valid country top-level domain or * not. * * @param domain to be validated. * @return true if valid country top-level domain. */ public static boolean isCountryTld(String domain) { return DomainValidator.getInstance().isValidCountryCodeTld(domain); }
/** * Returns true if the specified <code>String</code> parses * as a valid domain name with a recognized top-level domain. * The parsing is case-insensitive. * @param domain the parameter to check for domain name syntax * @return true if the parameter is a valid domain name */ public boolean isValid(String domain) { if (domain == null) { return false; } domain = unicodeToASCII(domain); // hosts must be equally reachable via punycode and Unicode; // Unicode is never shorter than punycode, so check punycode // if domain did not convert, then it will be caught by ASCII // checks in the regexes below if (domain.length() > MAX_DOMAIN_LENGTH) { return false; } String[] groups = domainRegex.match(domain); if (groups != null && groups.length > 0) { return isValidTld(groups[0]); } return allowLocal && hostnameRegex.isValid(domain); }
public void testUpdateCountryCode() { assertFalse(validator.isValidCountryCodeTld("com")); // cannot be valid DomainValidator.updateTLDOverride(ArrayType.COUNTRY_CODE_PLUS, new String[]{"com"}); assertTrue(validator.isValidCountryCodeTld("com")); // it is now! DomainValidator.updateTLDOverride(ArrayType.COUNTRY_CODE_MINUS, new String[]{"com"}); assertFalse(validator.isValidCountryCodeTld("com")); // show that minus overrides the rest assertTrue(validator.isValidCountryCodeTld("ch")); DomainValidator.updateTLDOverride(ArrayType.COUNTRY_CODE_MINUS, new String[]{"ch"}); assertFalse(validator.isValidCountryCodeTld("ch")); DomainValidator.updateTLDOverride(ArrayType.COUNTRY_CODE_MINUS, new String[]{"xx"}); assertTrue(validator.isValidCountryCodeTld("ch")); }
public void testUpdateGeneric() { assertFalse(validator.isValidGenericTld("ch")); // cannot be valid DomainValidator.updateTLDOverride(ArrayType.GENERIC_PLUS, new String[]{"ch"}); assertTrue(validator.isValidGenericTld("ch")); // it is now! DomainValidator.updateTLDOverride(ArrayType.GENERIC_MINUS, new String[]{"ch"}); assertFalse(validator.isValidGenericTld("ch")); // show that minus overrides the rest assertTrue(validator.isValidGenericTld("com")); DomainValidator.updateTLDOverride(ArrayType.GENERIC_MINUS, new String[]{"com"}); assertFalse(validator.isValidGenericTld("com")); DomainValidator.updateTLDOverride(ArrayType.GENERIC_MINUS, new String[]{"xx"}); // change the minus list assertTrue(validator.isValidGenericTld("com")); }
/** * Checks if the specified String matches an IANA-defined country code top-level domain. */ public boolean isValidCountryCodeTid(String ccTld) { return DOMAIN_VALIDATOR.isValidCountryCodeTld(ccTld); }
/** * Checks if the specified String matches an IANA-defined generic top-level domain. */ public boolean isValidGenericTId(String gTld) { return DOMAIN_VALIDATOR.isValidGenericTld(gTld); }
/** * Checks if the specified String matches an IANA-defined top-level domain. */ public boolean isValidTId(String tld) { return DOMAIN_VALIDATOR.isValidTld(tld); } }
/** * Checks if the specified String matches an IANA-defined infrastructure top-level domain. */ public boolean isValidInfrastructureTId(String iTld) { return DOMAIN_VALIDATOR.isValidInfrastructureTld(iTld); }