Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print more details in CLI regex-test #1287

Merged
merged 2 commits into from Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/datastructure.c
Expand Up @@ -588,3 +588,41 @@ void _query_set_status(queriesData *query, const enum query_status new_status, c
// Update status
query->status = new_status;
}

const char * __attribute__ ((const)) get_query_reply_str(const enum reply_type reply)
DL6ER marked this conversation as resolved.
Show resolved Hide resolved
{
switch(reply)
{
case REPLY_UNKNOWN:
return "UNKNOWN";
case REPLY_NODATA:
return "NODATA";
case REPLY_NXDOMAIN:
return "NXDOMAIN";
case REPLY_CNAME:
return "CNAME";
case REPLY_IP:
return "IP";
case REPLY_DOMAIN:
return "DOMAIN";
case REPLY_RRNAME:
return "RRNAME";
case REPLY_SERVFAIL:
return "SERVFAIL";
case REPLY_REFUSED:
return "REFUSED";
case REPLY_NOTIMP:
return "NOTIMP";
case REPLY_OTHER:
return "OTHER";
case REPLY_DNSSEC:
return "DNSSEC";
case REPLY_NONE:
return "NONE";
case REPLY_BLOB:
return "BLOB";
default:
case QUERY_REPLY_MAX:
return "INVALID";
}
}
61 changes: 43 additions & 18 deletions src/regex.c
Expand Up @@ -410,25 +410,50 @@ int match_regex(const char *input, DNSCacheData* dns_cache, const int clientID,
input, regex[index].string);
}

if(regextest && regexid == REGEX_CLI)
if(regextest)
{
// CLI provided regular expression
logg(" %s%s%s matches",
cli_bold(), regex[index].string, cli_normal());
}
else if(regextest && regexid == REGEX_BLACKLIST)
{
// Database-sourced regular expression
logg(" %s%s%s matches (regex blacklist, DB ID %i)",
cli_bold(), regex[index].string, cli_normal(),
regex[index].database_id);
}
else if(regextest && regexid == REGEX_WHITELIST)
{
// Database-sourced regular expression
logg(" %s%s%s matches (regex whitelist, DB ID %i)",
cli_bold(), regex[index].string, cli_normal(),
regex[index].database_id);
if(regexid == REGEX_CLI)
{
// CLI provided regular expression
logg(" %s%s%s matches",
cli_bold(), regex[index].string, cli_normal());
}
else if(regextest && regexid == REGEX_BLACKLIST)
{
// Database-sourced regular expression
logg(" %s%s%s matches (regex blacklist, DB ID %i)",
cli_bold(), regex[index].string, cli_normal(),
regex[index].database_id);
}
else if(regextest && regexid == REGEX_WHITELIST)
{
// Database-sourced regular expression
logg(" %s%s%s matches (regex whitelist, DB ID %i)",
cli_bold(), regex[index].string, cli_normal(),
regex[index].database_id);
}

// Check query type filtering
if(regex[index].ext.query_type != 0)
{
logg(" Hint: This regex %s type %s queries",
regex[index].ext.query_type_inverted ? "does not match" : "matches only",
querytypes[regex[index].ext.query_type]);
}

// Check inversion
if(regex[index].ext.inverted)
{
logg(" Hint: This regex is inverted");
}

// Check special reply type
if(regex[index].ext.reply != REPLY_UNKNOWN)
{
logg(" Hint: This regex forces reply type %s",
get_query_reply_str(regex[index].ext.reply));
}

}
else
{
Expand Down
28 changes: 28 additions & 0 deletions test/test_suite.bats
Expand Up @@ -986,6 +986,34 @@
[[ ${lines[0]} == "fe80::1234" ]]
}

@test "Regex Test 47: Option \";querytype=A\" reported on CLI" {
run bash -c './pihole-FTL regex-test "f" f\;querytype=A'
printf "%s\n" "${lines[@]}"
[[ $status == 0 ]]
[[ ${lines[4]} == " Hint: This regex matches only type A queries" ]]
}

@test "Regex Test 48: Option \";querytype=!TXT\" reported on CLI" {
run bash -c './pihole-FTL regex-test "f" f\;querytype=!TXT'
printf "%s\n" "${lines[@]}"
[[ $status == 0 ]]
[[ ${lines[4]} == " Hint: This regex does not match type TXT queries" ]]
}

@test "Regex Test 49: Option \";reply=NXDOMAIN\" reported on CLI" {
run bash -c './pihole-FTL regex-test "f" f\;reply=NXDOMAIN'
printf "%s\n" "${lines[@]}"
[[ $status == 0 ]]
[[ ${lines[4]} == " Hint: This regex forces reply type NXDOMAIN" ]]
}

@test "Regex Test 50: Option \";invert\" reported on CLI" {
run bash -c './pihole-FTL regex-test "f" g\;invert'
printf "%s\n" "${lines[@]}"
[[ $status == 0 ]]
[[ ${lines[4]} == " Hint: This regex is inverted" ]]
}

# x86_64-musl is built on busybox which has a slightly different
# variant of ls displaying three, instead of one, spaces between the
# user and group names.
Expand Down