@@ -10,6 +10,7 @@ use mullvad_types::{
10
10
} ;
11
11
use std:: time:: Duration ;
12
12
use talpid_types:: net:: wireguard;
13
+ use talpid_types:: net:: wireguard:: PublicKey ;
13
14
use test_macro:: test_function;
14
15
use test_rpc:: ServiceClient ;
15
16
@@ -214,14 +215,10 @@ pub async fn test_automatic_wireguard_rotation(
214
215
rpc : ServiceClient ,
215
216
mut mullvad_client : MullvadProxyClient ,
216
217
) -> anyhow:: Result < ( ) > {
218
+ const ROTATION_TIMEOUT : Duration = Duration :: from_secs ( 120 ) ;
219
+
217
220
// 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 ?;
225
222
226
223
log:: info!( "Old wireguard key: {old_key}" ) ;
227
224
@@ -240,64 +237,53 @@ pub async fn test_automatic_wireguard_rotation(
240
237
drop ( mullvad_client) ;
241
238
let mut mullvad_client = ctx. rpc_provider . new_client ( ) . await ;
242
239
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.
246
240
log:: info!( "Verifying that wireguard key has changed" ) ;
247
241
let event_listener = mullvad_client
248
242
. events_listen ( )
249
243
. await
250
244
. 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 ?;
258
246
259
247
// If key has not yet been updated, listen for changes to it
260
248
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
262
250
// that, the rotation probably won't happen anytime soon.
263
251
log:: info!( "Listening for device daemon event" ) ;
264
252
let device_event = |daemon_event| match daemon_event {
265
253
DaemonEvent :: Device ( device_event) => Some ( device_event) ,
266
254
_ => None ,
267
255
} ;
268
256
let device_event_listener = tokio:: time:: timeout (
269
- Duration :: from_secs ( 100 ) ,
257
+ ROTATION_TIMEOUT ,
270
258
helpers:: find_daemon_event ( event_listener, device_event) ,
271
259
) ;
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 ?;
294
267
295
268
assert_ne ! ( old_key, new_key) ;
296
269
}
297
270
298
271
Ok ( ( ) )
299
272
}
300
273
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
+
301
287
/// Remove all devices on the current account
302
288
pub async fn clear_devices ( device_client : & DevicesProxy ) -> anyhow:: Result < ( ) > {
303
289
log:: info!( "Removing all devices for account" ) ;
0 commit comments