Skip to content

Commit 1095234

Browse files
committed
Restore credentials service unit test
1 parent e0c063e commit 1095234

13 files changed

Lines changed: 692 additions & 663 deletions

File tree

creds-lib/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use futures_lite::Stream;
55
use crate::model::{BackgroundEvent, Device};
66

77
/// Used for communication from trusted UI to credential service
8-
pub trait CredentialServiceClient {
8+
pub trait FlowController {
99
fn get_available_public_key_devices(
1010
&self,
1111
) -> impl Future<Output = Result<Vec<Device>, ()>> + Send;

creds-lib/src/model.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ pub enum UsbState {
260260
Failed(Error),
261261
}
262262

263+
#[derive(Debug)]
263264
pub enum BackgroundEvent {
264265
UsbStateChanged(UsbState),
265266
HybridQrStateChanged(HybridState),

creds-ui/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use async_std::stream::Stream;
2-
use creds_lib::client::CredentialServiceClient;
2+
use creds_lib::client::FlowController;
33
use futures_lite::StreamExt;
44
use zbus::{Connection, zvariant};
55

@@ -20,7 +20,7 @@ impl DbusCredentialClient {
2020
}
2121
}
2222

23-
impl CredentialServiceClient for DbusCredentialClient {
23+
impl FlowController for DbusCredentialClient {
2424
async fn get_available_public_key_devices(
2525
&self,
2626
) -> std::result::Result<Vec<creds_lib::model::Device>, ()> {

creds-ui/src/gui/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@ use std::{sync::Arc, thread::JoinHandle};
66
use async_std::{channel::Receiver, sync::Mutex as AsyncMutex};
77

88
use creds_lib::server::ViewRequest;
9-
use creds_lib::{client::CredentialServiceClient, model::ViewUpdate};
9+
use creds_lib::{client::FlowController, model::ViewUpdate};
1010

1111
use view_model::ViewEvent;
1212

13-
pub(super) fn start_gui_thread<C: CredentialServiceClient + Send + Sync + 'static>(
13+
pub(super) fn start_gui_thread<F: FlowController + Send + Sync + 'static>(
1414
rx: Receiver<ViewRequest>,
15-
client: C,
15+
flow_controller: F,
1616
) -> Result<JoinHandle<()>, std::io::Error> {
1717
thread::Builder::new().name("gui".into()).spawn(move || {
18-
let client = Arc::new(AsyncMutex::new(client));
18+
let flow_controller = Arc::new(AsyncMutex::new(flow_controller));
1919
// D-Bus received a request and needs a window open
2020
while let Ok(view_request) = rx.recv_blocking() {
21-
run_gui(client.clone(), view_request);
21+
run_gui(flow_controller.clone(), view_request);
2222
}
2323
})
2424
}
2525

26-
fn run_gui<C: CredentialServiceClient + Send + Sync + 'static>(
27-
client: Arc<AsyncMutex<C>>,
26+
fn run_gui<F: FlowController + Send + Sync + 'static>(
27+
flow_controller: Arc<AsyncMutex<F>>,
2828
request: ViewRequest,
2929
) {
3030
let operation = request.operation;
3131
let (tx_update, rx_update) = async_std::channel::unbounded::<ViewUpdate>();
3232
let (tx_event, rx_event) = async_std::channel::unbounded::<ViewEvent>();
3333
let event_loop = async_std::task::spawn(async move {
34-
let mut vm = view_model::ViewModel::new(operation, client, rx_event, tx_update);
34+
let mut vm = view_model::ViewModel::new(operation, flow_controller, rx_event, tx_update);
3535
vm.start_event_loop().await;
3636
println!("event loop ended?");
3737
});

creds-ui/src/gui/view_model/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ use serde::{Deserialize, Serialize};
1111
use tracing::{error, info};
1212

1313
use creds_lib::{
14-
client::CredentialServiceClient,
14+
client::FlowController,
1515
model::{
1616
BackgroundEvent, Credential, Device, Error, HybridState, Operation, Transport, UsbState,
1717
ViewUpdate,
1818
},
1919
};
2020

2121
#[derive(Debug)]
22-
pub(crate) struct ViewModel<C>
22+
pub(crate) struct ViewModel<F>
2323
where
24-
C: CredentialServiceClient + Send,
24+
F: FlowController + Send,
2525
{
26-
credential_service: Arc<AsyncMutex<C>>,
26+
flow_controller: Arc<AsyncMutex<F>>,
2727
tx_update: Sender<ViewUpdate>,
2828
rx_event: Receiver<ViewEvent>,
2929
title: String,
@@ -39,15 +39,15 @@ where
3939
// hybrid_linked_state: HybridState,
4040
}
4141

42-
impl<C: CredentialServiceClient + Send> ViewModel<C> {
42+
impl<F: FlowController + Send> ViewModel<F> {
4343
pub(crate) fn new(
4444
operation: Operation,
45-
credential_service: Arc<AsyncMutex<C>>,
45+
flow_controller: Arc<AsyncMutex<F>>,
4646
rx_event: Receiver<ViewEvent>,
4747
tx_update: Sender<ViewUpdate>,
4848
) -> Self {
4949
Self {
50-
credential_service,
50+
flow_controller,
5151
rx_event,
5252
tx_update,
5353
operation,
@@ -73,7 +73,7 @@ impl<C: CredentialServiceClient + Send> ViewModel<C> {
7373

7474
async fn update_devices(&mut self) {
7575
let devices = self
76-
.credential_service
76+
.flow_controller
7777
.lock()
7878
.await
7979
.get_available_public_key_devices()
@@ -111,11 +111,11 @@ impl<C: CredentialServiceClient + Send> ViewModel<C> {
111111
// start discovery for newly selected device
112112
match device.transport {
113113
Transport::Usb => {
114-
let mut cred_service = self.credential_service.lock().await;
114+
let mut cred_service = self.flow_controller.lock().await;
115115
(*cred_service).get_usb_credential().await.unwrap();
116116
}
117117
Transport::HybridQr => {
118-
let mut cred_service = self.credential_service.lock().await;
118+
let mut cred_service = self.flow_controller.lock().await;
119119
cred_service.get_hybrid_credential().await.unwrap();
120120
}
121121
_ => {
@@ -132,7 +132,7 @@ impl<C: CredentialServiceClient + Send> ViewModel<C> {
132132
pub(crate) async fn start_event_loop(&mut self) {
133133
let view_events = self.rx_event.clone().map(Event::View);
134134
let bg_events = {
135-
let mut cred_service = self.credential_service.lock().await;
135+
let mut cred_service = self.flow_controller.lock().await;
136136
cred_service.initiate_event_stream().await.unwrap()
137137
};
138138
let mut all_events = view_events.merge(bg_events.map(Event::Background));
@@ -147,7 +147,7 @@ impl<C: CredentialServiceClient + Send> ViewModel<C> {
147147
println!("Selected device {id}");
148148
}
149149
Event::View(ViewEvent::UsbPinEntered(pin)) => {
150-
let mut cred_service = self.credential_service.lock().await;
150+
let mut cred_service = self.flow_controller.lock().await;
151151
if cred_service.enter_client_pin(pin).await.is_err() {
152152
error!("Failed to send pin to device");
153153
}
@@ -159,7 +159,7 @@ impl<C: CredentialServiceClient + Send> ViewModel<C> {
159159
);
160160

161161
if let Err(_) = self
162-
.credential_service
162+
.flow_controller
163163
.lock()
164164
.await
165165
.select_credential(cred_id)

credsd/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ zbus = { version = "5.9.0", default-features = false, features = ["tokio"] }
2727

2828
[dev-dependencies]
2929
gio = "0.21.0"
30+
zbus = { version = "5.9.0", default-features = false, features = ["blocking-api", "tokio"] }

credsd/src/credential_service/hybrid.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,15 @@ pub(super) mod test {
270270
stream: DummyHybridStateStream,
271271
}
272272

273+
impl DummyHybridHandler {
274+
#[cfg(test)]
275+
pub fn new(states: Vec<HybridStateInternal>) -> Self {
276+
Self {
277+
stream: DummyHybridStateStream { states },
278+
}
279+
}
280+
}
281+
273282
impl Default for DummyHybridHandler {
274283
fn default() -> Self {
275284
Self {

0 commit comments

Comments
 (0)