#![cfg(all(feature = "vtn", feature = "mqtt", feature = "client"))]
use std::time::Duration;
use openadr::mqtt::{BrokerAddress, Connection, ConnectionConfig, Incoming, QoS};
use openadr::{
client::{BusinessLogic, Client},
model::{BlVenRequest, EventRequest, Interval, IntervalPeriod, ProgramRequest, VenRequest},
};
use testcontainers::compose::DockerCompose;
const BL_TOKEN: &str = "bl-secret";
const VEN_TOKEN: &str = "ven-secret";
const VEN_CLIENT: &str = "ven";
const PREFIX: &str = "openadr3/3.1.0";
struct Stack {
_compose: Option<DockerCompose>,
vtn: String,
broker_host: String,
broker_port: u16,
}
impl Stack {
async fn start() -> Option<Self> {
let vtn = std::env::var("OPENADR_VTN_URL").ok();
let broker = std::env::var("OPENADR_BROKER").ok();
assert_eq!(
vtn.is_some(),
broker.is_some(),
"set both OPENADR_VTN_URL and OPENADR_BROKER to use an existing stack"
);
if let (Some(vtn), Some(broker)) = (vtn, broker) {
let (host, port) = broker.rsplit_once(':')?;
let stack = Self {
_compose: None,
vtn,
broker_host: host.to_string(),
broker_port: port.parse().ok()?,
};
return stack.wait_until_ready().await.then_some(stack);
}
const COMPOSE: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/deploy/compose.yaml");
let mut compose = DockerCompose::with_local_client(&[COMPOSE])
.with_build(true)
.with_wait(true);
compose
.up()
.await
.expect("deploy/compose.yaml must come up: it is the configuration under test");
let vtn = compose.service("vtn")?;
let broker = compose.service("broker")?;
let vtn_port = vtn.get_host_port_ipv4(3000).await.ok()?;
let broker_port = broker.get_host_port_ipv4(1883).await.ok()?;
let stack = Self {
_compose: Some(compose),
vtn: format!("http://127.0.0.1:{vtn_port}"),
broker_host: "127.0.0.1".to_string(),
broker_port,
};
stack.wait_until_ready().await.then_some(stack)
}
fn api(&self) -> String {
format!("{}/openadr3/3.1.0", self.vtn)
}
async fn wait_until_ready(&self) -> bool {
if !self.wait_until_serving().await {
return false;
}
for _ in 0..90 {
let good = connect_result(self, "readiness-good", VEN_CLIENT, VEN_TOKEN).await;
let bad = connect_result(self, "readiness-bad", VEN_CLIENT, "definitely-wrong").await;
if good.is_none() && bad.is_some() {
return true;
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
eprintln!(
"the broker never settled into answering with the VTN's decisions — a good \
credential and a bad one did not get different answers"
);
false
}
async fn wait_until_serving(&self) -> bool {
openadr::install_crypto_provider().unwrap();
let http = reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
.expect("an HTTP client");
for _ in 0..120 {
if http
.get(format!("{}/auth/server", self.api()))
.send()
.await
.is_ok_and(|r| r.status().is_success())
{
return true;
}
tokio::time::sleep(Duration::from_millis(500)).await;
}
eprintln!("the VTN never answered GET /auth/server");
false
}
fn business_logic(&self) -> Client<BusinessLogic> {
Client::<BusinessLogic>::builder(&self.api())
.expect("the base URL parses")
.bearer_token(BL_TOKEN)
.build()
.expect("a client")
}
fn mqtt(
&self,
id: &str,
username: &str,
password: &str,
subscriptions: Vec<(String, QoS)>,
) -> (
std::sync::Arc<Connection>,
tokio::sync::mpsc::Receiver<Incoming>,
) {
let address =
BrokerAddress::parse(&format!("mqtt://{}:{}", self.broker_host, self.broker_port))
.expect("the broker URL parses");
let (tx, rx) = tokio::sync::mpsc::channel(64);
let connection = Connection::open(
address,
ConnectionConfig {
client_id: id.to_string(),
username: Some(username.to_string()),
password: Some(password.to_string()),
keep_alive: Duration::from_secs(30),
..Default::default()
},
subscriptions,
tx,
);
(connection, rx)
}
}
async fn connect_result(stack: &Stack, id: &str, username: &str, password: &str) -> Option<String> {
let (_connection, mut events) = stack.mqtt(id, username, password, Vec::new());
match tokio::time::timeout(Duration::from_secs(20), events.recv()).await {
Ok(Some(Incoming::Connected(ack))) if ack.is_accepted() => None,
Ok(Some(Incoming::Connected(ack))) => Some(ack.reason().to_string()),
Ok(Some(Incoming::Disconnected(why))) => Some(why),
Ok(Some(Incoming::Message { .. })) => Some("a message before a CONNACK".to_string()),
Ok(None) => Some("the connection task stopped".to_string()),
Err(_) => Some("the broker did not answer CONNECT within 20s".to_string()),
}
}
async fn subscribe_result(
stack: &Stack,
id: &str,
username: &str,
password: &str,
filter: &str,
) -> Result<(), String> {
let (connection, mut events) = stack.mqtt(id, username, password, Vec::new());
match tokio::time::timeout(Duration::from_secs(20), events.recv()).await {
Ok(Some(Incoming::Connected(ack))) if ack.is_accepted() => {}
Ok(Some(Incoming::Connected(ack))) => return Err(ack.reason().to_string()),
Ok(Some(Incoming::Disconnected(why))) => return Err(why),
Ok(_) => return Err("no CONNACK".to_string()),
Err(_) => return Err("the broker did not answer CONNECT within 20s".to_string()),
}
let codes = tokio::time::timeout(
Duration::from_secs(20),
connection.subscribe(vec![(filter.to_string(), QoS::AtLeastOnce)]),
)
.await
.map_err(|_| "the broker neither granted nor refused within 20s".to_string())?
.map_err(|e| e.to_string())?;
match codes.contains(&0x80) {
true => Err(format!("SUBACK refused: {codes:?}")),
false => Ok(()),
}
}
struct Subscriber {
_connection: std::sync::Arc<Connection>,
events: tokio::sync::mpsc::Receiver<Incoming>,
}
impl Subscriber {
async fn connect(
stack: &Stack,
id: &str,
username: &str,
password: &str,
filter: &str,
) -> Self {
let (connection, mut events) = stack.mqtt(
id,
username,
password,
vec![(filter.to_string(), QoS::AtLeastOnce)],
);
let connected = tokio::time::timeout(Duration::from_secs(20), events.recv()).await;
assert!(
matches!(connected, Ok(Some(Incoming::Connected(ack))) if ack.is_accepted()),
"the broker never accepted a connection for {filter}: {connected:?}"
);
Self {
_connection: connection,
events,
}
}
async fn next(&mut self, patience: Duration) -> Option<(String, serde_json::Value)> {
let deadline = tokio::time::Instant::now() + patience;
loop {
match tokio::time::timeout_at(deadline, self.events.recv()).await {
Ok(Some(Incoming::Message { topic, payload })) => {
return Some((
topic,
serde_json::from_slice(&payload).unwrap_or(serde_json::Value::Null),
));
}
Ok(Some(_)) => continue,
Ok(None) | Err(_) => return None,
}
}
}
}
async fn ven_for(
bl: &Client<BusinessLogic>,
client_id: &str,
name: &str,
targets: &[&str],
) -> String {
let request = VenRequest::Bl(BlVenRequest {
client_id: client_id.parse().expect("a client id"),
ven_name: name.parse().expect("a ven name"),
targets: targets
.iter()
.map(|t| t.parse().expect("a target"))
.collect(),
attributes: None,
});
let existing = bl
.vens()
.list()
.await
.expect("the VEN list is readable")
.into_iter()
.find(|v| v.client_id.as_str() == client_id);
match existing {
Some(v) => bl.vens().update(&v.id, &request).await.expect("update").id,
None => bl.vens().create(&request).await.expect("create").id,
}
.to_string()
}
async fn targeted_event(bl: &Client<BusinessLogic>, program: &str, name: &str, target: &str) {
let mut event = EventRequest::new(program.parse().expect("a program id"));
event.event_name = Some(name.parse().expect("an event name"));
event.targets = vec![target.parse().expect("a target")];
event.interval_period = Some(IntervalPeriod::new(
"2026-02-11T12:00:00Z".parse().expect("a start"),
"PT15M".parse().expect("a duration"),
));
event.intervals = Some(vec![Interval::new(
0,
vec![openadr::model::ValuesMap::single(
"SIMPLE".parse().expect("a payload type"),
openadr::model::Value::Integer(1),
)],
)]);
bl.events()
.create(&event)
.await
.expect("the event is created");
}
#[tokio::test]
#[ignore = "brings up deploy/compose.yaml: run with --ignored --nocapture"]
async fn object_privacy_holds_across_a_real_broker() {
let stack = Stack::start()
.await
.expect("broker test requires a ready VTN and broker with reachable ports");
let bl = stack.business_logic();
let run = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let ven_a = ven_for(&bl, VEN_CLIENT, &format!("probe-a-{run}"), &["group1"]).await;
let ven_b = ven_for(&bl, "other", &format!("probe-b-{run}"), &["group2"]).await;
assert_ne!(ven_a, ven_b);
let program = bl
.programs()
.create(&ProgramRequest::new(
format!("broker-check-{run}")
.parse()
.expect("a program name"),
))
.await
.expect("the programme is created")
.id
.to_string();
assert!(
connect_result(&stack, "bad", VEN_CLIENT, "definitely-wrong")
.await
.is_some(),
"the broker accepted a credential the VTN does not know"
);
println!("ok a wrong password is refused");
let mut entitled = Subscriber::connect(
&stack,
"probe-a",
VEN_CLIENT,
VEN_TOKEN,
&format!("{PREFIX}/events/vens/{ven_a}/#"),
)
.await;
targeted_event(&bl, &program, "curtail", "group1").await;
let (topic, body) = entitled
.next(Duration::from_secs(30))
.await
.expect("the entitled VEN's private topic received nothing");
assert!(
topic.starts_with(&format!("{PREFIX}/events/vens/{ven_a}/")),
"{topic}"
);
assert_eq!(body["operation"], "CREATE", "{body}");
assert_eq!(
body["targets"],
serde_json::json!(["group1"]),
"the notification did not carry exactly this VEN's own target: {body}"
);
println!("ok a targeted event reached the entitled VEN, carrying its own target");
let refused = subscribe_result(
&stack,
"eavesdropper",
VEN_CLIENT,
VEN_TOKEN,
&format!("{PREFIX}/events/vens/{ven_b}/#"),
)
.await;
assert!(
refused.is_err(),
"one VEN was granted a subscription to another VEN's private topic"
);
println!("ok the broker kept one VEN out of another's private topic");
let (forger, _forger_events) = stack.mqtt("forger", VEN_CLIENT, VEN_TOKEN, Vec::new());
let _ = tokio::time::timeout(
Duration::from_secs(10),
forger.publish(
&format!("{PREFIX}/events/vens/{ven_a}/create"),
br#"{"forged":true}"#.to_vec(),
QoS::AtLeastOnce,
false,
),
)
.await;
let forged = entitled.next(Duration::from_secs(5)).await;
assert!(
forged.is_none_or(|(_, body)| body["forged"].is_null()),
"a VEN published a forged dispatch instruction and a subscriber received it"
);
println!("ok a VEN's publish reached no subscriber");
}