Skip to content

Commit

Permalink
Re-enable old test
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Mar 19, 2024
1 parent 34a0d9a commit 9b06a90
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
36 changes: 14 additions & 22 deletions mullvad-daemon/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1435,26 +1435,18 @@ mod test {
);
}

// TODO(markus): `preferred_tunnel_constraints` is slated for removal - consider writing a new test which
// does not depend on relay selector internals.
// /// Test whether the relay selector selects wireguard often enough, given no special
// /// constraints, to verify that the device is valid
// #[test]
// fn test_validates_by_default() {
// for attempt in 0.. {
// let should_validate =
// TunnelStateChangeHandler::should_check_validity_on_attempt(attempt);
// let (_, _, tunnel_type) =
// RelaySelector::preferred_tunnel_constraints(attempt.try_into().unwrap());
// assert_eq!(
// tunnel_type,
// TunnelType::Wireguard,
// "failed on attempt {attempt}"
// );
// if should_validate {
// // Now that we've triggered a device check, we can give up
// break;
// }
// }
// }
/// Test whether the relay selector selects wireguard often enough, given no special constraints.
/// A Wireguard relay must be used to verify that the device is valid.
#[test]
fn test_validates_by_default() {
use mullvad_relay_selector::{RelaySelector, SelectorConfig};
use talpid_types::net::TunnelType;
// No special relay constraints / user settings are assumed
let config = SelectorConfig::default();
// It ought to be enough that the first 3 connection attempts will yield a Wireguard relay
for attempt in 0..3 {
let typ = RelaySelector::would_return(attempt, &config).unwrap();
assert_eq!(typ, TunnelType::Wireguard);
}
}
}
24 changes: 24 additions & 0 deletions mullvad-relay-selector/src/relay_selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,30 @@ impl RelaySelector {
self.get_relay_with_order(&RETRY_ORDER, retry_attempt)
}

/// Peek at which [`TunnelType`] that would be returned for a certain connection attempt for a given
/// [`SelectorConfig`]. Returns [`Option::None`] if the given config would return a custom
/// tunnel endpoint.
///
/// # Note
/// This function is only really useful for testing-purposes. It is exposed to ease testing of
/// other mullvad crates which depend on the retry behaviour of [`RelaySelector`].
pub fn would_return(connection_attempt: usize, config: &SelectorConfig) -> Option<TunnelType> {
let config = SpecializedSelectorConfig::from(config);
match config {
// This case is not really interesting
SpecializedSelectorConfig::Custom(_) => None,
SpecializedSelectorConfig::Normal(config) => Some(
Self::pick_and_merge_query(
&RETRY_ORDER,
RelayQuery::from(config),
connection_attempt,
)
.tunnel_protocol
.unwrap_or(TunnelType::Wireguard),
),
}
}

/// Returns a random relay and relay endpoint matching the current constraints defined by
/// `retry_order` corresponsing to `retry_attempt`.
pub fn get_relay_with_order(
Expand Down

0 comments on commit 9b06a90

Please sign in to comment.