Cover the approval timeout and missing-UI denials

This commit is contained in:
raucao committed 2026-11-13 16:39:39 +04:00
1 parent 4617838d3c
commit 69f82249f4
2 files changed
+53 -19

No files matched your search

+53 -1
View File
@@ -94,6 +94,17 @@ impl Approver {
/// Ask, and treat a timeout or a missing UI as a denial.
pub async fn ask(&self, client: ClientInfo, request: Request) -> Decision {
self.ask_within(ASK_TIMEOUT, client, request).await
}
/// The same, with an explicit deadline, so tests can exercise the timeout
/// without waiting a minute.
async fn ask_within(
&self,
within: Duration,
client: ClientInfo,
request: Request,
) -> Decision {
let (reply, answer) = bounded(1);
let question = ApprovalRequest {
client,
@@ -103,9 +114,50 @@ impl Approver {
if self.inbox.send(question).await.is_err() {
return Decision::Deny;
}
match tokio::time::timeout(ASK_TIMEOUT, answer.recv()).await {
match tokio::time::timeout(within, answer.recv()).await {
Ok(Ok(decision)) => decision,
_ => Decision::Deny,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn client() -> ClientInfo {
ClientInfo {
public_key: Keys::generate().public_key(),
name: None,
url: None,
image: None,
}
}
/// A UI that receives the question but never answers must deny.
#[tokio::test]
async fn a_silent_ui_times_out_to_deny() {
let (approver, questions) = Approver::new();
let holding = tokio::spawn(async move {
// Keep the question (and its reply channel) alive, but silent.
while let Ok(question) = questions.recv().await {
tokio::time::sleep(Duration::from_secs(30)).await;
drop(question);
}
});
let decision = approver
.ask_within(Duration::from_millis(50), client(), Request::Pair)
.await;
assert_eq!(decision, Decision::Deny);
holding.abort();
}
/// With no UI at all the send fails or the answer never comes: deny.
#[tokio::test]
async fn a_missing_ui_is_a_denial() {
let (approver, questions) = Approver::new();
let drain = tokio::spawn(async move { while questions.recv().await.is_ok() {} });
assert_eq!(approver.ask(client(), Request::Pair).await, Decision::Deny);
drain.abort();
}
}
-18
View File
@@ -1130,22 +1130,4 @@ mod tests {
);
}
/// No UI, no answer: a dropped question must deny rather than hang.
#[tokio::test]
async fn a_dropped_question_is_a_denial() {
let (approver, questions) = Approver::new();
let drain = tokio::spawn(async move {
while let Ok(question) = questions.recv().await {
drop(question); // nobody ever answers
}
});
let client = ClientInfo {
public_key: Keys::generate().public_key(),
name: None,
url: None,
image: None,
};
assert_eq!(approver.ask(client, Request::Pair).await, Decision::Deny);
drain.abort();
}
}