Skip to content

Commit

Permalink
Add timeout to tester geoip rcp call
Browse files Browse the repository at this point in the history
  • Loading branch information
hulthe committed Apr 2, 2024
1 parent bd5ad33 commit 121af32
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
3 changes: 0 additions & 3 deletions test/test-manager/src/tests/relay_ip_overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub async fn test_wireguard_ip_override(
let _remove_nft_rule_on_drop = block_route(guest_ip, relay_ip).await?;

log::info!("checking that the connection does not work while blocked");
// FIXME: this fails because of rpc timeouts, which is sort of fine but not ideal
ensure!(
helpers::geoip_lookup_with_retries(&rpc).await.is_err(),
"Assert that relay is blocked by firewall rule"
Expand Down Expand Up @@ -129,7 +128,6 @@ pub async fn test_openvpn_ip_override(
let _remove_nft_rule_on_drop = block_route(guest_ip, relay_ip).await?;

log::info!("checking that the connection does not work while blocked");
// FIXME: this fails because of rpc timeouts, which is sort of fine but not ideal
ensure!(
helpers::geoip_lookup_with_retries(&rpc).await.is_err(),
"Assert that relay is blocked by firewall rule"
Expand Down Expand Up @@ -212,7 +210,6 @@ pub async fn test_bridge_ip_override(
let _remove_nft_rule_on_drop = block_route(guest_ip, relay_ip).await?;

log::info!("checking that the connection does not work while blocked");
// FIXME: this fails because of rpc timeouts, which is sort of fine but not ideal
ensure!(
helpers::geoip_lookup_with_retries(&rpc).await.is_err(),
"Assert that relay is blocked by firewall rule"
Expand Down
15 changes: 12 additions & 3 deletions test/test-rpc/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use futures::channel::oneshot;
use hyper::{Client, Uri};
use once_cell::sync::Lazy;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::net::SocketAddr;
use std::{net::SocketAddr, time::Duration};
use tokio_rustls::rustls::ClientConfig;

use crate::{AmIMullvad, Error};
Expand Down Expand Up @@ -72,10 +72,10 @@ impl Drop for SockHandle {
}
}

pub async fn geoip_lookup(mullvad_host: String) -> Result<AmIMullvad, Error> {
pub async fn geoip_lookup(mullvad_host: String, timeout: Duration) -> Result<AmIMullvad, Error> {
let uri = Uri::try_from(format!("https://ipv4.am.i.{mullvad_host}/json"))
.map_err(|_| Error::InvalidUrl)?;
http_get(uri).await
http_get_with_timeout(uri, timeout).await
}

pub async fn http_get<T: DeserializeOwned>(url: Uri) -> Result<T, Error> {
Expand Down Expand Up @@ -106,6 +106,15 @@ pub async fn http_get<T: DeserializeOwned>(url: Uri) -> Result<T, Error> {
})
}

pub async fn http_get_with_timeout<T: DeserializeOwned>(
url: Uri,
timeout: Duration,
) -> Result<T, Error> {
tokio::time::timeout(timeout, http_get(url))
.await
.map_err(|_| Error::HttpRequest("Request timed out".into()))?
}

fn read_cert_store() -> tokio_rustls::rustls::RootCertStore {
let mut cert_store = tokio_rustls::rustls::RootCertStore::empty();

Expand Down
14 changes: 11 additions & 3 deletions test/test-runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
path::{Path, PathBuf},
process::Stdio,
sync::Arc,
time::Duration,
time::{Duration, SystemTime},
};
use util::OnDrop;

Expand Down Expand Up @@ -171,10 +171,18 @@ impl Service for TestServer {

async fn geoip_lookup(
self,
_: context::Context,
ctx: context::Context,
mullvad_host: String,
) -> Result<test_rpc::AmIMullvad, test_rpc::Error> {
test_rpc::net::geoip_lookup(mullvad_host).await
let timeout = ctx
.deadline
.duration_since(SystemTime::now())
.ok()
// account for some time to send the RPC response
.and_then(|d| d.checked_sub(Duration::from_millis(500)))
.unwrap_or_default();

test_rpc::net::geoip_lookup(mullvad_host, timeout).await
}

async fn resolve_hostname(
Expand Down

0 comments on commit 121af32

Please sign in to comment.