#![cfg(all(feature = "vtn", feature = "conformance", feature = "internal-auth"))]
use std::sync::Arc;
use axum::http::StatusCode;
use futures_util::StreamExt as _;
use openadr::{
conformance::{Credential, Outcome, Runner, Severity, Target, checks},
vtn::{
Vtn, VtnConfig,
auth::{InternalAuth, Scope, Scopes},
notify::{Notifiers, RecordingNotifier},
store::MemoryStorage,
},
};
const BL: &str = "bl-secret";
const VEN: &str = "ven-secret";
const BL_CLIENT: &str = "bl-client-1";
const VEN_CLIENT: &str = "ven-client-1";
async fn start(configure: impl FnOnce(VtnConfig) -> VtnConfig) -> String {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let base = format!("http://{addr}/openadr3/3.1.0");
let config = configure(VtnConfig {
base_path: "/openadr3/3.1.0".into(),
mqtt_topic_prefix: "openadr3".into(),
..Default::default()
});
let vtn = Vtn::builder()
.storage(MemoryStorage::shared())
.authenticator(Arc::new(
InternalAuth::builder(format!("{base}/auth/token"))
.client(BL_CLIENT, BL, Scopes::new(Scope::BUSINESS_LOGIC))
.unwrap()
.client(VEN_CLIENT, VEN, Scopes::new(Scope::VEN))
.unwrap()
.build(),
))
.notifier(Notifiers::new().with(RecordingNotifier::shared()).shared())
.config(config)
.mqtt(openadr::model::MqttNotifierBinding {
uris: vec!["mqtts://broker.test:8883".into()],
serialization: openadr::model::Serialization::Json,
authentication: openadr::model::MqttAuthentication::Oauth2BearerToken {
username: "{clientID}".into(),
},
})
.build();
let router = vtn.router();
tokio::spawn(async move {
let _ = axum::serve(listener, router).await;
});
base
}
fn bl_credential() -> Credential {
Credential::ClientCredentials {
id: BL_CLIENT.into(),
secret: BL.into(),
}
}
fn ven_credential() -> Credential {
Credential::ClientCredentials {
id: VEN_CLIENT.into(),
secret: VEN.into(),
}
}
fn full_target(base: &str) -> Target {
Target::new(base)
.with_business_logic(bl_credential())
.with_ven(ven_credential(), VEN_CLIENT)
}
#[tokio::test]
async fn this_vtn_passes_its_own_suite() {
let base = start(|c| c).await;
let report = Runner::new(full_target(&base)).unwrap().run().await;
assert!(
report.is_conformant(),
"this VTN failed its own conformance suite:\n{report}"
);
let skipped: Vec<&str> = report
.findings
.iter()
.filter(|f| f.outcome.is_skipped())
.map(|f| f.check.id)
.collect();
assert!(
skipped.is_empty(),
"checks skipped against a fully-configured VTN: {skipped:?}\n{report}"
);
let (passed, failed, _) = report.tally(Severity::Extension);
assert_eq!(failed, 0, "an extension check failed:\n{report}");
assert!(passed > 0);
}
#[tokio::test]
async fn the_suite_cleans_up_after_itself() {
let base = start(|c| c).await;
let runner = Runner::new(full_target(&base)).unwrap();
runner.run().await;
let client = openadr::client::Client::<openadr::client::BusinessLogic>::builder(&base)
.unwrap()
.credentials(openadr::client::Credentials::new(BL_CLIENT, BL))
.build()
.unwrap();
for collection in ["programs", "events", "vens", "resources", "reports"] {
let remaining: serde_json::Value = client
.get_json(collection, &openadr::client::Query::new().limit(50))
.await
.unwrap();
let litter: Vec<String> = remaining
.as_array()
.map(|items| {
items
.iter()
.filter(|o| o.to_string().contains("oadr-conformance-"))
.map(|o| o["id"].as_str().unwrap_or("?").to_string())
.collect()
})
.unwrap_or_default();
assert!(
litter.is_empty(),
"the suite left {} object(s) behind in /{collection}: {litter:?}",
litter.len()
);
}
let second = Runner::new(full_target(&base)).unwrap().run().await;
assert!(second.is_conformant(), "the second run failed:\n{second}");
}
#[tokio::test]
async fn a_run_without_ven_credentials_skips_rather_than_passes() {
let base = start(|c| c).await;
let target = Target::new(&base).with_business_logic(bl_credential());
let report = Runner::new(target).unwrap().run().await;
let skipped: Vec<&str> = report
.findings
.iter()
.filter(|f| f.outcome.is_skipped())
.map(|f| f.check.id)
.collect();
assert!(
skipped.contains(&"target-hiding-on-reads"),
"object privacy was not skipped without a VEN credential: {skipped:?}"
);
assert!(report.is_conformant(), "{report}");
let rendered = report.to_string();
assert!(rendered.contains("skip"));
assert!(rendered.contains("no VEN credential was supplied"));
}
#[tokio::test]
async fn a_run_with_no_credentials_at_all_still_checks_what_it_can() {
let base = start(|c| c).await;
let report = Runner::new(Target::new(&base)).unwrap().run().await;
let attempted: Vec<&str> = report
.findings
.iter()
.filter(|f| !f.outcome.is_skipped())
.map(|f| f.check.id)
.collect();
assert!(
attempted.contains(&"auth-server-unauthenticated"),
"the unauthenticated endpoint was not checked: {attempted:?}"
);
assert!(
attempted.contains(&"unauthenticated-write-refused"),
"{attempted:?}"
);
let mut attempted = attempted;
attempted.sort_unstable();
assert_eq!(
attempted,
[
"auth-server-unauthenticated",
"token-response-is-not-stored",
"unauthenticated-write-refused",
"unauthorized-names-the-scheme",
],
"{report}"
);
}
enum Break {
Config(fn(VtnConfig) -> VtnConfig),
Request(fn(&str, &mut String, &mut axum::http::HeaderMap)),
Status(fn(&axum::http::Method, &str, StatusCode) -> StatusCode),
Header(fn(&str, StatusCode, &mut axum::http::HeaderMap)),
Body(fn(&axum::http::Method, &str, StatusCode, &serde_json::Value, &mut serde_json::Value)),
EveryCredentialIsBusinessLogic,
}
struct Fault {
name: &'static str,
how: Break,
caught_by: &'static [&'static str],
}
const FAULTS: &[Fault] = &[
Fault {
name: "no ETag on reads",
how: Break::Config(|c| VtnConfig {
http_caching: false,
..c
}),
caught_by: &["etag-and-conditional-read"],
},
Fault {
name: "no programName lookup",
how: Break::Config(|c| VtnConfig {
program_name_lookup: false,
..c
}),
caught_by: &["program-name-lookup"],
},
Fault {
name: "?eventID= is accepted and ignored",
how: Break::Request(|_, target, _| strip_param(target, "eventID")),
caught_by: &["report-filters-by-event"],
},
Fault {
name: "?targets= is accepted and ignored",
how: Break::Request(|_, target, _| strip_param(target, "targets")),
caught_by: &[
"granted-targets-are-visible",
"target-hiding-on-reads",
"targets-accept-both-forms",
],
},
Fault {
name: "?programID= is accepted and ignored",
how: Break::Request(|_, target, _| strip_param(target, "programID")),
caught_by: &["filters-are-additive"],
},
Fault {
name: "?skip= is accepted and ignored",
how: Break::Request(|_, target, _| strip_param(target, "skip")),
caught_by: &["pagination-is-complete-and-ordered"],
},
Fault {
name: "?active= is accepted and ignored",
how: Break::Request(|_, target, _| strip_param(target, "active")),
caught_by: &["active-excludes-transpired-events"],
},
Fault {
name: "?limit= beyond the maximum is clamped rather than refused",
how: Break::Request(|_, target, _| {
*target = target.replace("limit=500", "limit=50");
}),
caught_by: &["limit-is-capped-at-fifty"],
},
Fault {
name: "the Content-Type of a request body is not read",
how: Break::Request(|_, _, headers| {
if headers.contains_key(axum::http::header::CONTENT_TYPE) {
headers.insert(
axum::http::header::CONTENT_TYPE,
axum::http::HeaderValue::from_static("application/json"),
);
}
}),
caught_by: &["foreign-media-type-is-refused"],
},
Fault {
name: "a 401 carries no WWW-Authenticate",
how: Break::Header(|_, status, headers| {
if status == StatusCode::UNAUTHORIZED {
headers.remove("www-authenticate");
}
}),
caught_by: &["unauthorized-names-the-scheme"],
},
Fault {
name: "a token response may be stored",
how: Break::Header(|path, _, headers| {
if path.ends_with("/auth/token") {
headers.remove(axum::http::header::CACHE_CONTROL);
}
}),
caught_by: &["token-response-is-not-stored"],
},
Fault {
name: "a read says nothing about caching",
how: Break::Header(|path, _, headers| {
if path.ends_with("/programs") {
headers.remove(axum::http::header::CACHE_CONTROL);
headers.remove(axum::http::header::VARY);
}
}),
caught_by: &["reads-declare-a-caching-policy"],
},
Fault {
name: "errors are plain JSON rather than problem+json",
how: Break::Header(|_, status, headers| {
if status.is_client_error() || status.is_server_error() {
headers.insert(
axum::http::header::CONTENT_TYPE,
axum::http::HeaderValue::from_static("application/json"),
);
}
}),
caught_by: &["problem-uses-the-rfc-9457-media-type"],
},
Fault {
name: "a missing programme answers 403",
how: Break::Status(|method, path, status| {
if status == StatusCode::NOT_FOUND
&& method == axum::http::Method::GET
&& path.contains("/programs/")
{
return StatusCode::FORBIDDEN;
}
status
}),
caught_by: &[
"missing-object-is-404-problem",
"problem-uses-the-rfc-9457-media-type",
"programme-delete-leaves-no-orphan",
],
},
Fault {
name: "an object hidden by targeting answers 403, confirming it exists",
how: Break::Status(|method, path, status| {
if status == StatusCode::NOT_FOUND
&& method == axum::http::Method::GET
&& path.contains("/events/")
{
return StatusCode::FORBIDDEN;
}
status
}),
caught_by: &[
"hidden-object-is-404-not-403",
"programme-delete-leaves-no-orphan",
],
},
Fault {
name: "a duplicate programName is accepted",
how: Break::Status(|method, path, status| {
if status == StatusCode::CONFLICT
&& method == axum::http::Method::POST
&& path.ends_with("/programs")
{
return StatusCode::CREATED;
}
status
}),
caught_by: &["program-name-is-unique"],
},
Fault {
name: "an event naming no programme is accepted",
how: Break::Status(|method, path, status| {
if status == StatusCode::BAD_REQUEST
&& method == axum::http::Method::POST
&& path.ends_with("/events")
{
return StatusCode::CREATED;
}
status
}),
caught_by: &["event-needs-a-programme"],
},
Fault {
name: "a VEN's topic request for another VEN is granted",
how: Break::Status(|method, path, status| {
if method == axum::http::Method::GET && path.contains("/notifiers/mqtt/topics/vens/") {
return StatusCode::OK;
}
status
}),
caught_by: &["mqtt-foreign-ven-topics-refused"],
},
Fault {
name: "GET /notifiers omits the WEBHOOK key",
how: Break::Body(|_, path, _, _, body| {
if path.ends_with("/notifiers")
&& let Some(object) = body.as_object_mut()
{
object.remove("WEBHOOK");
}
}),
caught_by: &["notifiers-webhook-key"],
},
Fault {
name: "the MQTT binding names no URIS",
how: Break::Body(|_, path, _, _, body| {
if path.ends_with("/notifiers")
&& let Some(mqtt) = body.get_mut("MQTT").and_then(|m| m.as_object_mut())
{
mqtt.remove("URIS");
}
}),
caught_by: &["mqtt-binding-shape"],
},
Fault {
name: "a created object carries no objectType",
how: Break::Body(|method, path, status, _, body| {
if method == axum::http::Method::POST
&& status == StatusCode::CREATED
&& path.ends_with("/programs")
&& let Some(object) = body.as_object_mut()
{
object.remove("objectType");
}
}),
caught_by: &["create-stamps-metadata"],
},
Fault {
name: "PUT leaves modificationDateTime where it was",
how: Break::Body(|method, path, _, _, body| {
if method == axum::http::Method::PUT
&& path.contains("/programs/")
&& let Some(created) = body.get("createdDateTime").cloned()
&& let Some(object) = body.as_object_mut()
{
object.insert("modificationDateTime".into(), created);
}
}),
caught_by: &["update-moves-modification-time"],
},
Fault {
name: "P9999Y is normalised into an ordinary 9999-year span",
how: Break::Body(|_, _, _, _, body| {
rewrite_strings(body, |s| {
if s == "P9999Y" {
Some("P9999Y0M0DT0H0M0S".to_string())
} else {
None
}
})
}),
caught_by: &["forever-duration-round-trips"],
},
Fault {
name: "the 0001-01-01 sentinel is resolved to an ordinary date on the way out",
how: Break::Body(|_, _, _, _, body| {
rewrite_strings(body, |s| {
s.starts_with("0001-01-01")
.then(|| "2026-01-01T00:00:00Z".to_string())
})
}),
caught_by: &["now-sentinel-round-trips"],
},
Fault {
name: "a problem body carries no instance",
how: Break::Body(|_, _, status, _, body| {
if (status.is_client_error() || status.is_server_error())
&& let Some(object) = body.as_object_mut()
{
object.remove("instance");
}
}),
caught_by: &["problem-carries-a-traceable-instance"],
},
Fault {
name: "an unauthenticated write is accepted",
how: Break::Status(|method, path, status| {
if status == StatusCode::UNAUTHORIZED
&& method == axum::http::Method::POST
&& path.ends_with("/programs")
{
return StatusCode::CREATED;
}
status
}),
caught_by: &["unauthenticated-write-refused"],
},
Fault {
name: "a body missing a required field is accepted",
how: Break::Status(|method, path, status| {
if status == StatusCode::BAD_REQUEST
&& method == axum::http::Method::POST
&& path.ends_with("/programs")
{
return StatusCode::CREATED;
}
status
}),
caught_by: &["malformed-body-is-400-problem"],
},
Fault {
name: "a report naming no event is accepted",
how: Break::Status(|method, path, status| {
if status == StatusCode::BAD_REQUEST
&& method == axum::http::Method::POST
&& path.ends_with("/reports")
{
return StatusCode::CREATED;
}
status
}),
caught_by: &["report-needs-an-event"],
},
Fault {
name: "a VEN obtains a collection-wide topic",
how: Break::Status(|method, path, status| {
if method == axum::http::Method::GET
&& path.contains("/notifiers/mqtt/topics/")
&& !path.contains("/topics/vens/")
{
return StatusCode::OK;
}
status
}),
caught_by: &["mqtt-collection-topics-are-business-logic-only"],
},
Fault {
name: "a client-chosen id and createdDateTime are honoured",
how: Break::Body(|method, path, status, sent, body| {
if method == axum::http::Method::POST
&& status == StatusCode::CREATED
&& path.ends_with("/programs")
&& let Some(object) = body.as_object_mut()
{
for field in ["id", "createdDateTime"] {
if let Some(claimed) = sent.get(field) {
object.insert(field.into(), claimed.clone());
}
}
}
}),
caught_by: &["create-ignores-client-metadata"],
},
Fault {
name: "a report is filed as whichever client the body claims",
how: Break::Body(|method, path, status, sent, body| {
if method == axum::http::Method::POST
&& status == StatusCode::CREATED
&& path.ends_with("/reports")
&& let Some(claimed) = sent.get("clientID")
&& let Some(object) = body.as_object_mut()
{
object.insert("clientID".into(), claimed.clone());
}
}),
caught_by: &["report-is-stamped-not-claimed"],
},
Fault {
name: "a VEN's own claim to a target is honoured",
how: Break::Body(|method, path, status, sent, body| {
if method == axum::http::Method::POST
&& status == StatusCode::CREATED
&& (path.ends_with("/vens") || path.ends_with("/resources"))
&& let Some(claimed) = sent.get("targets")
&& let Some(object) = body.as_object_mut()
{
object.insert("targets".into(), claimed.clone());
}
}),
caught_by: &[
"ven-cannot-grant-a-resource-targets",
"ven-cannot-grant-itself-targets",
],
},
Fault {
name: "prices are held as binary floats",
how: Break::Body(|_, _, _, _, body| {
rewrite_numbers(body, |n| (n == "0.1").then_some(0.100_000_000_000_000_03))
}),
caught_by: &["decimal-prices-are-exact"],
},
Fault {
name: "an event's interval ids are reassigned",
how: Break::Body(|method, path, status, _, body| {
if method == axum::http::Method::POST
&& status == StatusCode::CREATED
&& path.ends_with("/events")
&& let Some(intervals) = body.get_mut("intervals").and_then(|i| i.as_array_mut())
{
for (n, interval) in intervals.iter_mut().enumerate() {
if let Some(object) = interval.as_object_mut() {
object.insert("id".into(), serde_json::json!(100 + n));
}
}
}
}),
caught_by: &["interval-payloads-round-trip"],
},
Fault {
name: "a report comes back without its resources",
how: Break::Body(|method, path, _, _, body| {
if method == axum::http::Method::GET
&& path.contains("/reports/")
&& let Some(object) = body.as_object_mut()
{
object.remove("resources");
}
}),
caught_by: &["report-round-trips"],
},
Fault {
name: "a VEN's own topic set omits DELETE",
how: Break::Body(|_, path, _, _, body| {
if path.contains("/notifiers/mqtt/topics/vens/")
&& let Some(topics) = body.get_mut("topics").and_then(|t| t.as_object_mut())
{
topics.remove("DELETE");
}
}),
caught_by: &["mqtt-ven-scoped-topics"],
},
Fault {
name: "an object with no targets is hidden as if it had some",
how: Break::Body(|method, path, _, _, body| {
if method == axum::http::Method::GET
&& path.ends_with("/programs")
&& let Some(items) = body.as_array_mut()
{
items.retain(|p| {
p["targets"]
.as_array()
.is_some_and(|targets| !targets.is_empty())
});
}
}),
caught_by: &[
"pagination-is-complete-and-ordered",
"program-name-lookup",
"untargeted-objects-are-visible",
],
},
Fault {
name: "a resource reports a venID that is not its own",
how: Break::Body(|method, path, _, _, body| {
if method == axum::http::Method::GET
&& path.ends_with("/resources")
&& let Some(items) = body.as_array_mut()
{
for item in items {
if let Some(object) = item.as_object_mut() {
object.insert("venID".into(), serde_json::json!("not-this-vens"));
}
}
}
}),
caught_by: &["ven-reads-only-its-own-resources"],
},
Fault {
name: "every credential is read as business logic",
how: Break::EveryCredentialIsBusinessLogic,
caught_by: &[
"hidden-object-is-404-not-403",
"mqtt-collection-topics-are-business-logic-only",
"mqtt-foreign-ven-topics-refused",
"report-filters-by-event",
"report-is-stamped-not-claimed",
"report-needs-an-event",
"report-round-trips",
"target-hiding-on-reads",
"ungranted-targets-are-invisible",
"ven-cannot-write-programmes",
"ven-reads-only-its-own-reports",
"ven-reads-only-its-own-subscriptions",
"ven-reads-only-its-own-vens",
],
},
];
fn strip_param(target: &mut String, name: &str) {
let Some((path, query)) = target.split_once('?') else {
return;
};
let kept: Vec<&str> = query
.split('&')
.filter(|pair| {
let key = pair.split('=').next().unwrap_or(pair);
key != name
})
.collect();
*target = if kept.is_empty() {
path.to_string()
} else {
format!("{path}?{}", kept.join("&"))
};
}
fn rewrite_numbers(value: &mut serde_json::Value, f: fn(&str) -> Option<f64>) {
match value {
serde_json::Value::Number(n) => {
if let Some(replacement) = f(&n.to_string())
&& let Some(number) = serde_json::Number::from_f64(replacement)
{
*value = serde_json::Value::Number(number);
}
}
serde_json::Value::Array(items) => items.iter_mut().for_each(|i| rewrite_numbers(i, f)),
serde_json::Value::Object(fields) => {
fields.values_mut().for_each(|v| rewrite_numbers(v, f))
}
_ => {}
}
}
fn rewrite_strings(value: &mut serde_json::Value, f: fn(&str) -> Option<String>) {
match value {
serde_json::Value::String(s) => {
if let Some(replacement) = f(s) {
*s = replacement;
}
}
serde_json::Value::Array(items) => items.iter_mut().for_each(|i| rewrite_strings(i, f)),
serde_json::Value::Object(fields) => {
fields.values_mut().for_each(|v| rewrite_strings(v, f))
}
_ => {}
}
}
async fn failures_from(fault: &'static Fault) -> Vec<String> {
let base = match fault.how {
Break::Config(configure) => start(configure).await,
_ => {
let upstream = start(|c| c).await;
behind_a_fault(&upstream, fault).await
}
};
let report = Runner::new(full_target(&base)).unwrap().run().await;
let mut failed: Vec<String> = report.failures().map(|f| f.check.id.to_string()).collect();
failed.sort();
failed
}
async fn behind_a_fault(upstream: &str, fault: &'static Fault) -> String {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let root = upstream.trim_end_matches("/openadr3/3.1.0").to_string();
let business_logic = match fault.how {
Break::EveryCredentialIsBusinessLogic => Some(
business_logic_token(upstream)
.await
.expect("the fixture VTN issues its own tokens"),
),
_ => None,
};
let app = axum::Router::new().fallback(axum::routing::any(
move |request: axum::extract::Request| {
let root = root.clone();
let business_logic = business_logic.clone();
async move { relay(&root, request, fault, business_logic.as_deref()).await }
},
));
tokio::spawn(async move {
let _ = axum::serve(listener, app).await;
});
format!("http://{addr}/openadr3/3.1.0")
}
async fn business_logic_token(base: &str) -> Option<String> {
let response = reqwest::Client::new()
.post(format!("{base}/auth/token"))
.form(&[
("grant_type", "client_credentials"),
("client_id", BL_CLIENT),
("client_secret", BL),
])
.send()
.await
.ok()?;
let body: serde_json::Value = response.json().await.ok()?;
body["access_token"].as_str().map(str::to_string)
}
async fn relay(
upstream: &str,
request: axum::extract::Request,
fault: &'static Fault,
business_logic: Option<&str>,
) -> axum::response::Response {
use axum::response::IntoResponse as _;
let (parts, body) = request.into_parts();
let bytes = axum::body::to_bytes(body, usize::MAX)
.await
.unwrap_or_default();
let sent: serde_json::Value = serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null);
let mut target = parts
.uri
.path_and_query()
.map(|p| p.as_str().to_string())
.unwrap_or_else(|| "/".into());
let mut headers = parts.headers.clone();
if let Break::Request(apply) = fault.how {
apply(parts.uri.path(), &mut target, &mut headers);
}
if let (Break::EveryCredentialIsBusinessLogic, Some(token)) = (&fault.how, business_logic)
&& headers.contains_key(axum::http::header::AUTHORIZATION)
&& let Ok(value) = axum::http::HeaderValue::from_str(&format!("Bearer {token}"))
{
headers.insert(axum::http::header::AUTHORIZATION, value);
}
let client = reqwest::Client::new();
let mut outbound = client
.request(parts.method.clone(), format!("{upstream}{target}"))
.body(bytes);
for (name, value) in headers.iter() {
if name != axum::http::header::HOST {
outbound = outbound.header(name, value);
}
}
let Ok(response) = outbound.send().await else {
return StatusCode::BAD_GATEWAY.into_response();
};
let path = parts.uri.path();
let mut status = response.status();
let mut headers = response.headers().clone();
let mut body = response.bytes().await.unwrap_or_default().to_vec();
if let Break::Status(apply) = fault.how {
status = apply(&parts.method, path, status);
}
if let Break::Header(apply) = fault.how {
apply(path, status, &mut headers);
}
if let Break::Body(apply) = fault.how
&& let Ok(mut json) = serde_json::from_slice::<serde_json::Value>(&body)
{
apply(&parts.method, path, status, &sent, &mut json);
body = serde_json::to_vec(&json).unwrap_or(body);
}
headers.remove(axum::http::header::CONTENT_LENGTH);
headers.remove(axum::http::header::TRANSFER_ENCODING);
headers.remove(axum::http::header::CONTENT_ENCODING);
(status, headers, body).into_response()
}
const MIN_FALSIFIABLE: usize = 47;
const UNFALSIFIABLE_FROM_OUTSIDE: &[(&str, &str)] = &[(
"auth-server-unauthenticated",
"the suite discovers the token endpoint through GET /auth/server, so a fault that breaks it \
breaks the run rather than the check. Reaching it needs a VTN wrong in that one way, not a \
proxy in front of a right one",
)];
#[tokio::test]
async fn every_break_is_caught_by_exactly_the_checks_that_name_it() {
let runs: Vec<(&Fault, Vec<String>)> = futures_util::stream::iter(
FAULTS
.iter()
.map(|fault| async move { (fault, failures_from(fault).await) }),
)
.buffer_unordered(6)
.collect()
.await;
let mut observed: std::collections::BTreeSet<String> = Default::default();
let mut wrong: Vec<String> = Vec::new();
for (fault, failed) in runs {
let mut expected: Vec<String> = fault.caught_by.iter().map(|s| s.to_string()).collect();
expected.sort();
if failed != expected {
wrong.push(format!(
"\n break: {}\n caught by: {failed:?}\n expected: {expected:?}",
fault.name
));
}
observed.extend(failed);
}
assert!(
wrong.is_empty(),
"{} break(s) are not caught by exactly the checks that name them:{}",
wrong.len(),
wrong.join("")
);
let unproven: Vec<&str> = checks()
.iter()
.map(|c| c.id)
.filter(|id| !observed.contains(*id))
.collect();
println!(
"{} of {} checks have been observed failing against a deliberately broken VTN.\n\
Never seen failing: {unproven:?}",
observed.len(),
checks().len()
);
for (id, _) in UNFALSIFIABLE_FROM_OUTSIDE {
assert!(
!observed.contains(*id),
"{id} is listed as unfalsifiable from outside and a fault caught it; drop the entry"
);
assert!(
checks().iter().any(|c| c.id == *id),
"{id} is listed as unfalsifiable and is not a check"
);
}
assert!(
observed.len() >= MIN_FALSIFIABLE,
"{} checks are demonstrably able to fail, and the floor is {MIN_FALSIFIABLE}. A check \
that has never been seen failing is a check nobody has shown to be a check: add a fault \
to FAULTS rather than lowering the floor.",
observed.len()
);
}
#[tokio::test]
async fn every_check_is_reachable_and_ordered() {
let base = start(|c| c).await;
let report = Runner::new(full_target(&base)).unwrap().run().await;
assert_eq!(
report.findings.len(),
checks().len(),
"the report has a different number of findings than there are checks"
);
for (finding, check) in report.findings.iter().zip(checks()) {
assert_eq!(
finding.check.id, check.id,
"the report is not in the order the checks are listed"
);
if let Outcome::Skipped(reason) = &finding.outcome {
assert!(
!reason.contains("not implemented"),
"{} is listed but has no implementation",
check.id
);
}
}
}
#[tokio::test]
async fn a_conformance_run_is_reportable_as_json() {
let base = start(|c| c).await;
let report = Runner::new(full_target(&base)).unwrap().run().await;
let json = report.to_json();
assert_eq!(json["conformant"], true);
assert_eq!(
json["findings"].as_array().unwrap().len(),
checks().len(),
"the JSON dropped findings"
);
for finding in json["findings"].as_array().unwrap() {
assert!(finding["id"].is_string());
assert!(finding["clause"].is_string());
assert!(matches!(
finding["outcome"].as_str(),
Some("passed" | "failed" | "skipped")
));
}
}
#[tokio::test]
async fn a_vtn_with_no_broker_skips_the_mqtt_checks_rather_than_failing_them() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let base = format!("http://{addr}/openadr3/3.1.0");
let vtn = Vtn::builder()
.storage(MemoryStorage::shared())
.authenticator(Arc::new(
InternalAuth::builder(format!("{base}/auth/token"))
.client(BL_CLIENT, BL, Scopes::new(Scope::BUSINESS_LOGIC))
.unwrap()
.client(VEN_CLIENT, VEN, Scopes::new(Scope::VEN))
.unwrap()
.build(),
))
.notifier(Notifiers::new().with(RecordingNotifier::shared()).shared())
.config(VtnConfig {
base_path: "/openadr3/3.1.0".into(),
..Default::default()
})
.build();
let router = vtn.router();
tokio::spawn(async move {
let _ = axum::serve(listener, router).await;
});
let report = Runner::new(full_target(&base)).unwrap().run().await;
assert!(report.is_conformant(), "{report}");
let mqtt: Vec<&openadr::conformance::Finding> = report
.findings
.iter()
.filter(|f| f.check.id.starts_with("mqtt-"))
.collect();
assert!(!mqtt.is_empty());
for finding in mqtt {
assert!(
finding.outcome.is_skipped(),
"{} was not skipped on a VTN with no broker: {:?}",
finding.check.id,
finding.outcome
);
}
assert!(
report
.to_string()
.contains("offers no MQTT notifier binding")
);
}