| 1 |
//! Processes for running checks. |
| 2 |
|
| 3 |
use strum::{EnumMessage, VariantArray}; |
| 4 |
|
| 5 |
use crate::{httpdata::HttpData, prelude::RdapResponse}; |
| 6 |
|
| 7 |
use super::{Check, CheckClass, CheckParams, CheckSummary, Checks, GetChecks, traverse_checks}; |
| 8 |
|
| 9 |
pub fn do_check_processing( |
| 10 |
rdap: &RdapResponse, |
| 11 |
http_data: Option<&HttpData>, |
| 12 |
expect_exts: Option<&[String]>, |
| 13 |
allow_unreg_ext: bool, |
| 14 |
) -> Checks { |
| 15 |
let check_params = CheckParams { |
| 16 |
root: rdap, |
| 17 |
parent_type: rdap.get_type(), |
| 18 |
allow_unreg_ext, |
| 19 |
}; |
| 20 |
let mut checks = rdap.get_checks(None, check_params); |
| 21 |
|
| 22 |
if let Some(http_data) = http_data { |
| 23 |
// add these to the root check structure |
| 24 |
checks |
| 25 |
.items |
| 26 |
.append(&mut http_data.get_checks(None, check_params).items); |
| 27 |
} |
| 28 |
|
| 29 |
// add expected extension checks |
| 30 |
if let Some(expected_extensions) = expect_exts { |
| 31 |
for ext in expected_extensions { |
| 32 |
if !rdap_has_expected_extension(rdap, ext) { |
| 33 |
// add this to the root check structure |
| 34 |
checks |
| 35 |
.items |
| 36 |
.push(Check::ExpectedExtensionNotFound.check_item()); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
checks |
| 42 |
} |
| 43 |
|
| 44 |
fn rdap_has_expected_extension(rdap: &RdapResponse, ext: &str) -> bool { |
| 45 |
let count = ext.split('|').filter(|s| rdap.has_extension(s)).count(); |
| 46 |
count > 0 |
| 47 |
} |
| 48 |
|
| 49 |
pub fn get_summaries(checks: &Checks, classes: Option<&[CheckClass]>) -> Vec<CheckSummary> { |
| 50 |
let mut summaries = vec![]; |
| 51 |
let classes = classes.unwrap_or(CheckClass::VARIANTS); |
| 52 |
traverse_checks(checks, classes, None, &mut |struct_name, item| { |
| 53 |
let summary = CheckSummary { |
| 54 |
structure: struct_name.to_string(), |
| 55 |
code: item.check as usize, |
| 56 |
message: item |
| 57 |
.check |
| 58 |
.get_message() |
| 59 |
.unwrap_or("[Message Unavailable]") |
| 60 |
.to_string(), |
| 61 |
item: item.clone(), |
| 62 |
}; |
| 63 |
summaries.push(summary); |
| 64 |
}); |
| 65 |
summaries |
| 66 |
} |
| 67 |
|
| 68 |
#[cfg(test)] |
| 69 |
mod tests { |
| 70 |
|
| 71 |
use crate::{ |
| 72 |
check::process::rdap_has_expected_extension, |
| 73 |
prelude::{Domain, Extension, ToResponse}, |
| 74 |
}; |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_expected_extension_rdap_has() { |
| 78 |
// GIVEN |
| 79 |
let domain = Domain::response_obj() |
| 80 |
.extension(Extension::from("foo0")) |
| 81 |
.ldh_name("foo.example.com") |
| 82 |
.build(); |
| 83 |
let rdap = domain.to_response(); |
| 84 |
|
| 85 |
// WHEN |
| 86 |
let actual = rdap_has_expected_extension(&rdap, "foo0"); |
| 87 |
|
| 88 |
// THEN |
| 89 |
assert!(actual); |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn test_expected_extension_rdap_does_not_have() { |
| 94 |
// GIVEN |
| 95 |
let domain = Domain::response_obj() |
| 96 |
.extension(Extension::from("foo0")) |
| 97 |
.ldh_name("foo.example.com") |
| 98 |
.build(); |
| 99 |
let rdap = domain.to_response(); |
| 100 |
|
| 101 |
// WHEN |
| 102 |
let actual = rdap_has_expected_extension(&rdap, "foo1"); |
| 103 |
|
| 104 |
// THEN |
| 105 |
assert!(!actual); |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn test_compound_expected_extension() { |
| 110 |
// GIVEN |
| 111 |
let domain = Domain::response_obj() |
| 112 |
.extension(Extension::from("foo0")) |
| 113 |
.ldh_name("foo.example.com") |
| 114 |
.build(); |
| 115 |
let rdap = domain.to_response(); |
| 116 |
|
| 117 |
// WHEN |
| 118 |
let actual = rdap_has_expected_extension(&rdap, "foo0|foo1"); |
| 119 |
|
| 120 |
// THEN |
| 121 |
assert!(actual); |
| 122 |
} |
| 123 |
} |