From ba6ea5237eece18427419bd26ed5fbe322d32073 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 4 Jul 2024 07:18:57 +0200 Subject: [PATCH] splits and improves RR_REGEX and adds more tests --- ...HsDomainDnsSetupHostingAssetValidator.java | 25 +++++++----- ...DnsSetupHostingAssetValidatorUnitTest.java | 39 ++++++++++++++++++- ...gAssetEntityValidatorRegistryUnitTest.java | 3 +- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidator.java index f924c71c..531b645e 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidator.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidator.java @@ -12,13 +12,18 @@ import static net.hostsharing.hsadminng.hs.validation.StringProperty.stringPrope class HsDomainDnsSetupHostingAssetValidator extends HsHostingAssetEntityValidator { - final static String RR_REGEX = - "[a-z0-9\\.-]+\\s+" + // a (e.g. domain) name - "([1-9][0-9]*[HDW]\\s+){0,1}" + // optional TTL - "IN\\s+" + // record class IN for Internet - "([1-9][0-9]*[HDW]\\s+){0,1}" + // optional TTL - "[A-Z]+\\s+([a-z0-9\\.-]+|\"[^\"]*\")\\s*" + // either a simple argument or a more complex in quotes - "(;.*)*"; // optional comment at the end of the line + static final String RR_REGEX_NAME = "([a-z0-9\\.-]+|@)\\s+"; + static final String RR_REGEX_TTL = "(([1-9][0-9]*[mMhHdDwW]{0,1})+\\s+)*"; + static final String RR_REGEX_IN = "IN\\s+"; // record class IN for Internet + static final String RR_RECORD_TYPE = "[A-Z]+\\s+"; + static final String RR_RECORD_DATA = "([a-z0-9\\.-]+|\"[^\"]*\")\\s*"; + static final String RR_COMMENT = "(;.*)*"; + + static final String RR_REGEX_TTL_IN = + RR_REGEX_NAME + RR_REGEX_TTL + RR_REGEX_IN + RR_RECORD_TYPE + RR_RECORD_DATA + RR_COMMENT; + + static final String RR_REGEX_IN_TTL = + RR_REGEX_NAME + RR_REGEX_IN + RR_REGEX_TTL + RR_RECORD_TYPE + RR_RECORD_DATA + RR_COMMENT; HsDomainDnsSetupHostingAssetValidator() { super( BookingItem.mustBeNull(), @@ -31,15 +36,15 @@ class HsDomainDnsSetupHostingAssetValidator extends HsHostingAssetEntityValidato booleanProperty("auto-NS-RR").withDefault(true), booleanProperty("auto-MX-RR").withDefault(true), booleanProperty("auto-A+AAAA-RR").withDefault(true), - booleanProperty("auto-MAILSERVICES-RR").withDefault(true), // incl. AUTOCONFIG_RR, AUTODISCOVER_RR + booleanProperty("auto-MAILSERVICES-RR").withDefault(true), // TODO. specl: incl. AUTOCONFIG_RR, AUTODISCOVER_RR? booleanProperty("auto-DKIM-RR").withDefault(true), booleanProperty("auto-SPF-RR").withDefault(true), booleanProperty("auto-WILDCARD-MX-RR").withDefault(true), booleanProperty("auto-WILDCARD-A+AAAA-RR").withDefault(true), - booleanProperty("auto-WILDCARD-DKIM-RR").withDefault(true), // TODO.spec: ask Peter + booleanProperty("auto-WILDCARD-DKIM-RR").withDefault(true), // TODO.spec: @Peter booleanProperty("auto-WILDCARD-SPF-RR").withDefault(true), arrayOf( - stringProperty("user-RR").matchesRegEx(RR_REGEX).required() + stringProperty("user-RR").matchesRegEx(RR_REGEX_TTL_IN, RR_REGEX_IN_TTL).required() ).optional()); } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidatorUnitTest.java index 9da99764..eccf8b01 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidatorUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainDnsSetupHostingAssetValidatorUnitTest.java @@ -13,6 +13,12 @@ import static java.util.Map.entry; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.CLOUD_SERVER; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_DNS_SETUP; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_SETUP; +import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsDomainDnsSetupHostingAssetValidator.RR_COMMENT; +import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsDomainDnsSetupHostingAssetValidator.RR_RECORD_DATA; +import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsDomainDnsSetupHostingAssetValidator.RR_RECORD_TYPE; +import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsDomainDnsSetupHostingAssetValidator.RR_REGEX_IN; +import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsDomainDnsSetupHostingAssetValidator.RR_REGEX_NAME; +import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsDomainDnsSetupHostingAssetValidator.RR_REGEX_TTL; import static org.assertj.core.api.Assertions.assertThat; class HsDomainDnsSetupHostingAssetValidatorUnitTest { @@ -29,8 +35,10 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { .identifier("example.org") .config(Map.ofEntries( entry("user-RR", Array.of( - "www IN CNAME example.com. ; www.example.com is an alias for example.com", - "ns IN A 192.0.2.2 ; IPv4 address for ns.example.com") + "www IN CNAME example.com. ; www.example.com is an alias for example.com", + "test1 IN 1h30m CNAME example.com.", + "test2 1h30m IN CNAME example.com.", + "ns IN A 192.0.2.2; IPv4 address for ns.example.com") ) )); } @@ -91,4 +99,31 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest { "'DOMAIN_DNS_SETUP:example.org.parentAsset' must be of type DOMAIN_SETUP but is of type null", "'DOMAIN_DNS_SETUP:example.org.assignedToAsset' must be null but is set to D-???????-?:null"); } + + @Test + void validStringMatchesRegEx() { + assertThat("@ ").matches(RR_REGEX_NAME); + assertThat("ns ").matches(RR_REGEX_NAME); + assertThat("example.com. ").matches(RR_REGEX_NAME); + + assertThat("12400 ").matches(RR_REGEX_TTL); + assertThat("12400\t\t ").matches(RR_REGEX_TTL); + assertThat("12400 \t\t").matches(RR_REGEX_TTL); + assertThat("1h30m ").matches(RR_REGEX_TTL); + assertThat("30m ").matches(RR_REGEX_TTL); + + assertThat("IN ").matches(RR_REGEX_IN); + assertThat("IN\t\t ").matches(RR_REGEX_IN); + assertThat("IN \t\t").matches(RR_REGEX_IN); + + assertThat("CNAME ").matches(RR_RECORD_TYPE); + assertThat("CNAME\t\t ").matches(RR_RECORD_TYPE); + assertThat("CNAME \t\t").matches(RR_RECORD_TYPE); + + assertThat("example.com.").matches(RR_RECORD_DATA); + assertThat("123.123.123.123").matches(RR_RECORD_DATA); + assertThat("\"some more complex argument; including a semicolon\"").matches(RR_RECORD_DATA); + + assertThat("; whatever ; \" really anything").matches(RR_COMMENT); + } } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistryUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistryUnitTest.java index 7e55d727..4e4abae9 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistryUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorRegistryUnitTest.java @@ -34,7 +34,8 @@ class HsHostingAssetEntityValidatorRegistryUnitTest { HsHostingAssetType.MANAGED_WEBSPACE, HsHostingAssetType.UNIX_USER, HsHostingAssetType.EMAIL_ALIAS, - HsHostingAssetType.DOMAIN_SETUP + HsHostingAssetType.DOMAIN_SETUP, + HsHostingAssetType.DOMAIN_DNS_SETUP ); } }