Skip to content

Commit

Permalink
dns: make name_dirty test more like hsd rule.verifyName()
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Dec 2, 2021
1 parent c837b2d commit 491088f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -2431,26 +2431,31 @@ hsk_dns_name_alloc(

bool
hsk_dns_name_dirty(const char *name) {
char *s = (char *)name;
int len = strlen(name);
if (len > HSK_DNS_MAX_LABEL)
return true;

while (*s) {
uint8_t c = (uint8_t)*s;
for (int i = 0; i < len; i++) {
uint8_t c = name[i];

if (c >= 0x41 && c <= 0x5a)
c ^= 0x20;

switch (c) {
case 0x28 /*(*/:
case 0x29 /*)*/:
case 0x3b /*;*/:
case 0x20 /* */:
case 0x40 /*@*/:
case 0x22 /*"*/:
case 0x5c /*\\*/:
return true;
case 0x5f: /* _ */
case 0x2d: /* - */
if (i == 0 || i == len - 1) {
return true;
} else {
continue;
}
}

if (c < 0x20 || c > 0x7e)
if (c < 0x30 ||
(c > 0x39 && c < 0x61) ||
c > 0x7a) {
return true;

s += 1;
}
}

return false;
Expand Down
23 changes: 23 additions & 0 deletions test/hnsd-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,28 @@ test_prev_name() {
) == 0);
}

void
test_verify_name() {
printf("test_verify_name\n");

assert(!hsk_dns_name_dirty("hello"));
assert(!hsk_dns_name_dirty("HELLO"));
assert(!hsk_dns_name_dirty("heLLo"));
assert(!hsk_dns_name_dirty("HeLl0"));
assert(!hsk_dns_name_dirty("hel-lo"));
assert(!hsk_dns_name_dirty("1"));
assert(!hsk_dns_name_dirty("000_000"));
assert(!hsk_dns_name_dirty("this-domain-name-has-sixty-three-octets-taking-max-label-length"));
assert(hsk_dns_name_dirty("hel!lo"));
assert(hsk_dns_name_dirty("-hello"));
assert(hsk_dns_name_dirty("hello_"));
assert(hsk_dns_name_dirty("1@1"));
assert(hsk_dns_name_dirty("x\\000y"));
assert(hsk_dns_name_dirty("H&ELLO"));
assert(hsk_dns_name_dirty("3 3"));
assert(hsk_dns_name_dirty("this-domain-name-has-sixtyfour-octets-exceeding-max-label-length"));
}

/*
* TEST RUNNER
*/
Expand All @@ -220,6 +242,7 @@ main() {
test_decode_resource();
test_next_name();
test_prev_name();
test_verify_name();

printf("ok\n");

Expand Down

0 comments on commit 491088f

Please sign in to comment.