implement domain matching function and adjust tests

This commit is contained in:
Sunshine 2022-09-01 18:56:30 -04:00
parent 9356161a89
commit b13be1e0ce
No known key found for this signature in database
GPG Key ID: B80CA68703CD8AB1
2 changed files with 57 additions and 36 deletions

View File

@ -93,33 +93,60 @@ pub fn detect_media_type_by_file_name(filename: &str) -> String {
} }
pub fn domain_is_within_domain(domain: &str, domain_to_match_against: &str) -> bool { pub fn domain_is_within_domain(domain: &str, domain_to_match_against: &str) -> bool {
let domain_partials: Vec<&str> = domain.split(".").collect(); if domain_to_match_against.len() == 0 {
return false;
}
if domain_to_match_against == "." {
return true;
}
let domain_partials: Vec<&str> = domain.trim_end_matches(".").rsplit(".").collect();
let domain_to_match_against_partials: Vec<&str> = domain_to_match_against let domain_to_match_against_partials: Vec<&str> = domain_to_match_against
.trim_start_matches(".") .trim_end_matches(".")
.split(".") .rsplit(".")
.collect(); .collect();
let domain_to_match_against_starts_with_a_dot = domain_to_match_against.starts_with(".");
let mut i: usize = domain_partials.len(); let mut i: usize = 0;
let mut j: usize = domain_to_match_against_partials.len(); let l: usize = std::cmp::max(
domain_partials.len(),
domain_to_match_against_partials.len(),
);
let mut ok: bool = true;
if i >= j { while i < l {
while j > 0 { // Exit and return false if went out of bounds of domain to match against, and it didn't start with a dot
if !domain_partials if domain_to_match_against_partials.len() < i + 1
.get(i - 1) && !domain_to_match_against_starts_with_a_dot
.unwrap() {
.eq_ignore_ascii_case(&domain_to_match_against_partials.get(j - 1).unwrap()) ok = false;
{ break;
break;
}
i -= 1;
j -= 1;
} }
j == 0 let domain_partial = if domain_partials.len() < i + 1 {
} else { ""
false } else {
domain_partials.get(i).unwrap()
};
let domain_to_match_against_partial = if domain_to_match_against_partials.len() < i + 1 {
""
} else {
domain_to_match_against_partials.get(i).unwrap()
};
let parts_match = domain_to_match_against_starts_with_a_dot
|| domain_to_match_against_partial.eq_ignore_ascii_case(domain_partial);
if !parts_match {
ok = false;
break;
}
i += 1;
} }
ok
} }
pub fn indent(level: u32) -> String { pub fn indent(level: u32) -> String {

View File

@ -25,14 +25,6 @@ mod passing {
)); ));
} }
#[test]
fn dotted_domain_is_within_domain() {
assert!(utils::domain_is_within_domain(
".ycombinator.com",
"ycombinator.com"
));
}
#[test] #[test]
fn sub_domain_is_within_dotted_domain() { fn sub_domain_is_within_dotted_domain() {
assert!(utils::domain_is_within_domain( assert!(utils::domain_is_within_domain(
@ -67,18 +59,12 @@ mod passing {
#[test] #[test]
fn domain_with_trailing_dot_is_within_single_dot() { fn domain_with_trailing_dot_is_within_single_dot() {
assert!(utils::domain_is_within_domain( assert!(utils::domain_is_within_domain("ycombinator.com.", "."));
"ycombinator.com.",
"."
));
} }
#[test] #[test]
fn domain_matches_single_dot() { fn domain_matches_single_dot() {
assert!(utils::domain_is_within_domain( assert!(utils::domain_is_within_domain("ycombinator.com", "."));
"ycombinator.com",
"."
));
} }
#[test] #[test]
@ -140,6 +126,14 @@ mod failing {
)); ));
} }
#[test]
fn dotted_domain_is_not_within_domain() {
assert!(!utils::domain_is_within_domain(
".ycombinator.com",
"ycombinator.com"
));
}
#[test] #[test]
fn no_domain_can_be_within_empty_domain() { fn no_domain_can_be_within_empty_domain() {
assert!(!utils::domain_is_within_domain("ycombinator.com", "")); assert!(!utils::domain_is_within_domain("ycombinator.com", ""));