splits and improves RR_REGEX and adds more tests

This commit is contained in:
Michael Hoennig 2024-07-04 07:18:57 +02:00
parent 34c146341e
commit ba6ea5237e
3 changed files with 54 additions and 13 deletions

View File

@ -12,13 +12,18 @@ import static net.hostsharing.hsadminng.hs.validation.StringProperty.stringPrope
class HsDomainDnsSetupHostingAssetValidator extends HsHostingAssetEntityValidator { class HsDomainDnsSetupHostingAssetValidator extends HsHostingAssetEntityValidator {
final static String RR_REGEX = static final String RR_REGEX_NAME = "([a-z0-9\\.-]+|@)\\s+";
"[a-z0-9\\.-]+\\s+" + // a (e.g. domain) name static final String RR_REGEX_TTL = "(([1-9][0-9]*[mMhHdDwW]{0,1})+\\s+)*";
"([1-9][0-9]*[HDW]\\s+){0,1}" + // optional TTL static final String RR_REGEX_IN = "IN\\s+"; // record class IN for Internet
"IN\\s+" + // record class IN for Internet static final String RR_RECORD_TYPE = "[A-Z]+\\s+";
"([1-9][0-9]*[HDW]\\s+){0,1}" + // optional TTL static final String RR_RECORD_DATA = "([a-z0-9\\.-]+|\"[^\"]*\")\\s*";
"[A-Z]+\\s+([a-z0-9\\.-]+|\"[^\"]*\")\\s*" + // either a simple argument or a more complex in quotes static final String RR_COMMENT = "(;.*)*";
"(;.*)*"; // optional comment at the end of the line
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() { HsDomainDnsSetupHostingAssetValidator() {
super( BookingItem.mustBeNull(), super( BookingItem.mustBeNull(),
@ -31,15 +36,15 @@ class HsDomainDnsSetupHostingAssetValidator extends HsHostingAssetEntityValidato
booleanProperty("auto-NS-RR").withDefault(true), booleanProperty("auto-NS-RR").withDefault(true),
booleanProperty("auto-MX-RR").withDefault(true), booleanProperty("auto-MX-RR").withDefault(true),
booleanProperty("auto-A+AAAA-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-DKIM-RR").withDefault(true),
booleanProperty("auto-SPF-RR").withDefault(true), booleanProperty("auto-SPF-RR").withDefault(true),
booleanProperty("auto-WILDCARD-MX-RR").withDefault(true), booleanProperty("auto-WILDCARD-MX-RR").withDefault(true),
booleanProperty("auto-WILDCARD-A+AAAA-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), booleanProperty("auto-WILDCARD-SPF-RR").withDefault(true),
arrayOf( arrayOf(
stringProperty("user-RR").matchesRegEx(RR_REGEX).required() stringProperty("user-RR").matchesRegEx(RR_REGEX_TTL_IN, RR_REGEX_IN_TTL).required()
).optional()); ).optional());
} }

View File

@ -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.CLOUD_SERVER;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_DNS_SETUP; 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.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; import static org.assertj.core.api.Assertions.assertThat;
class HsDomainDnsSetupHostingAssetValidatorUnitTest { class HsDomainDnsSetupHostingAssetValidatorUnitTest {
@ -30,6 +36,8 @@ class HsDomainDnsSetupHostingAssetValidatorUnitTest {
.config(Map.ofEntries( .config(Map.ofEntries(
entry("user-RR", Array.of( entry("user-RR", Array.of(
"www IN CNAME example.com. ; www.example.com is an alias for 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") "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.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"); "'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);
}
} }

View File

@ -34,7 +34,8 @@ class HsHostingAssetEntityValidatorRegistryUnitTest {
HsHostingAssetType.MANAGED_WEBSPACE, HsHostingAssetType.MANAGED_WEBSPACE,
HsHostingAssetType.UNIX_USER, HsHostingAssetType.UNIX_USER,
HsHostingAssetType.EMAIL_ALIAS, HsHostingAssetType.EMAIL_ALIAS,
HsHostingAssetType.DOMAIN_SETUP HsHostingAssetType.DOMAIN_SETUP,
HsHostingAssetType.DOMAIN_DNS_SETUP
); );
} }
} }