use async_trait::async_trait;
use candid::Principal;
use fqdn::FQDN;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ValidationError {
#[error("existing DNS TXT _canister-id record at {src}")]
ExistingDnsTxtCanisterId { src: String },
#[error("existing DNS TXT challenge record at {src}")]
ExistingDnsTxtChallenge { src: String },
#[error("missing DNS CNAME record from {src} to {dst}")]
MissingDnsCname { src: String, dst: String },
#[error("missing DNS TXT record from {src} to a canister id")]
MissingDnsTxtCanisterId { src: String },
#[error("multiple DNS TXT records for canister id at {src}: {records:?}")]
MultipleDnsTxtCanisterId { src: String, records: Vec<String> },
#[error("invalid DNS TXT record from {src} to {id}")]
InvalidDnsTxtCanisterId { src: String, id: String },
#[error("failed to retrieve known domains from the canister {id}: {error}")]
KnownDomainsUnavailable { id: String, error: String },
#[error("domain is missing from canister {id} list of known domains")]
MissingKnownDomains { id: String },
#[error(transparent)]
UnexpectedError(#[from] anyhow::Error),
}
#[async_trait]
#[cfg_attr(test, mockall::automock)]
pub trait ValidatesDomains: Send + Sync {
async fn validate(&self, domain: &FQDN) -> Result<Principal, ValidationError>;
async fn validate_limited(&self, domain: &FQDN) -> Result<(), ValidationError>;
async fn validate_deletion(&self, domain: &FQDN) -> Result<(), ValidationError>;
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_validation_error_display() {
assert_eq!(
ValidationError::ExistingDnsTxtCanisterId {
src: "_canister-id.foo.bar".into()
}
.to_string(),
"existing DNS TXT _canister-id record at _canister-id.foo.bar"
);
assert_eq!(
ValidationError::ExistingDnsTxtChallenge {
src: "_acme-challenge.foo.bar".into()
}
.to_string(),
"existing DNS TXT challenge record at _acme-challenge.foo.bar"
);
assert_eq!(
ValidationError::MissingDnsCname {
src: "foo.bar".into(),
dst: "ic0.app".into()
}
.to_string(),
"missing DNS CNAME record from foo.bar to ic0.app"
);
assert_eq!(
ValidationError::MissingDnsTxtCanisterId {
src: "_canister-id.foo.bar".into()
}
.to_string(),
"missing DNS TXT record from _canister-id.foo.bar to a canister id"
);
assert_eq!(
ValidationError::MultipleDnsTxtCanisterId {
src: "_canister-id.foo.bar".into(),
records: vec!["aaaaa-aa".into(), "2vxsx-fae".into()]
}
.to_string(),
r#"multiple DNS TXT records for canister id at _canister-id.foo.bar: ["aaaaa-aa", "2vxsx-fae"]"#
);
assert_eq!(
ValidationError::MultipleDnsTxtCanisterId {
src: "foo.bar".into(),
records: vec![]
}
.to_string(),
"multiple DNS TXT records for canister id at foo.bar: []"
);
assert_eq!(
ValidationError::InvalidDnsTxtCanisterId {
src: "_canister-id.foo.bar".into(),
id: "not-a-principal".into()
}
.to_string(),
"invalid DNS TXT record from _canister-id.foo.bar to not-a-principal"
);
assert_eq!(
ValidationError::KnownDomainsUnavailable {
id: "aaaaa-aa".into(),
error: "timed out".into()
}
.to_string(),
"failed to retrieve known domains from the canister aaaaa-aa: timed out"
);
assert_eq!(
ValidationError::MissingKnownDomains {
id: "aaaaa-aa".into()
}
.to_string(),
"domain is missing from canister aaaaa-aa list of known domains"
);
}
#[test]
fn test_validation_error_unexpected() {
let err: ValidationError = anyhow::anyhow!("something went wrong").into();
assert!(matches!(err, ValidationError::UnexpectedError(_)));
assert_eq!(err.to_string(), "something went wrong");
let inner = anyhow::anyhow!("root cause").context("while doing stuff");
assert_eq!(
ValidationError::from(inner).to_string(),
"while doing stuff"
);
}
}