Skip to content

Commit a163223

Browse files
dlonMarkusPettersson98
authored andcommitted
Clean up test_automatic_wireguard_rotation
1 parent 537233b commit a163223

File tree

1 file changed

+27
-41
lines changed

1 file changed

+27
-41
lines changed

test/test-manager/src/tests/account.rs

+27-41
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use mullvad_types::{
1010
};
1111
use std::time::Duration;
1212
use talpid_types::net::wireguard;
13+
use talpid_types::net::wireguard::PublicKey;
1314
use test_macro::test_function;
1415
use test_rpc::ServiceClient;
1516

@@ -214,14 +215,10 @@ pub async fn test_automatic_wireguard_rotation(
214215
rpc: ServiceClient,
215216
mut mullvad_client: MullvadProxyClient,
216217
) -> anyhow::Result<()> {
218+
const ROTATION_TIMEOUT: Duration = Duration::from_secs(120);
219+
217220
// Make note of current WG key
218-
let old_key = mullvad_client
219-
.get_device()
220-
.await?
221-
.logged_in()
222-
.context("Client is not logged in to a valid account")?
223-
.device
224-
.pubkey;
221+
let old_key = get_current_wireguard_key(&mut mullvad_client).await?;
225222

226223
log::info!("Old wireguard key: {old_key}");
227224

@@ -240,64 +237,53 @@ pub async fn test_automatic_wireguard_rotation(
240237
drop(mullvad_client);
241238
let mut mullvad_client = ctx.rpc_provider.new_client().await;
242239

243-
// Check if the key rotation has already occurred when connected to the daemon, otherwise
244-
// listen for device daemon events until we observe the change. We have to register the event
245-
// listener before polling the current key to be sure we don't miss the change.
246240
log::info!("Verifying that wireguard key has changed");
247241
let event_listener = mullvad_client
248242
.events_listen()
249243
.await
250244
.context("Failed to begin listening for state changes")?;
251-
let new_key = mullvad_client
252-
.get_device()
253-
.await?
254-
.logged_in()
255-
.context("Client is not logged in to a valid account")?
256-
.device
257-
.pubkey;
245+
let new_key = get_current_wireguard_key(&mut mullvad_client).await?;
258246

259247
// If key has not yet been updated, listen for changes to it
260248
if new_key == old_key {
261-
// Verify rotation has happened within 100 seconds - if the key hasn't been rotated after
249+
// Verify rotation has happened within `ROTATION_TIMEOUT` - if the key hasn't been rotated after
262250
// that, the rotation probably won't happen anytime soon.
263251
log::info!("Listening for device daemon event");
264252
let device_event = |daemon_event| match daemon_event {
265253
DaemonEvent::Device(device_event) => Some(device_event),
266254
_ => None,
267255
};
268256
let device_event_listener = tokio::time::timeout(
269-
Duration::from_secs(100),
257+
ROTATION_TIMEOUT,
270258
helpers::find_daemon_event(event_listener, device_event),
271259
);
272-
let new_key = match device_event_listener.await {
273-
Err(err) => {
274-
log::warn!("{err}");
275-
log::warn!(
276-
"Did not observe any daemon event indicating that a key rotation happened"
277-
);
278-
// Note: The key rotation could possible have happened without us noticing due to
279-
// some raceiness in the timeframe between starting the daemon and us starting to
280-
// listen for new daemon events. Thus, it is probably a good idea to check manually if the
281-
// device key was rotated.
282-
log::info!("Manually checking if device key was rotated");
283-
mullvad_client
284-
.get_device()
285-
.await
286-
.context("Failed to get device data")?
287-
}
288-
Ok(device_event) => device_event?.new_state,
289-
}
290-
.logged_in()
291-
.context("Client is not logged in to a valid account")?
292-
.device
293-
.pubkey;
260+
let _ = device_event_listener.await;
261+
262+
// Note: The key rotation could possible have happened without us noticing due to
263+
// some raceiness in the timeframe between starting the daemon and us starting to
264+
// listen for new daemon events. Thus, it is probably a good idea to check manually if the
265+
// device key was rotated.
266+
let new_key = get_current_wireguard_key(&mut mullvad_client).await?;
294267

295268
assert_ne!(old_key, new_key);
296269
}
297270

298271
Ok(())
299272
}
300273

274+
async fn get_current_wireguard_key(
275+
mullvad_client: &mut MullvadProxyClient,
276+
) -> anyhow::Result<PublicKey> {
277+
let pubkey = mullvad_client
278+
.get_device()
279+
.await?
280+
.logged_in()
281+
.context("Client is not logged in to a valid account")?
282+
.device
283+
.pubkey;
284+
Ok(pubkey)
285+
}
286+
301287
/// Remove all devices on the current account
302288
pub async fn clear_devices(device_client: &DevicesProxy) -> anyhow::Result<()> {
303289
log::info!("Removing all devices for account");

0 commit comments

Comments
 (0)