Skip to content

Commit

Permalink
test_proxy_connection: make test more robust
Browse files Browse the repository at this point in the history
 - Support running on macos with podman (this probably also fixes it to work with docker as well).
   This is done by using a dedicated network name and by dynamically finding the host ip.
   In macos, both podman & docker are running inside a Linux VM, so it is essential to find a real IP
   associated with the macos itself.

 - Run the mock TCP server on a dynamic port. This avoid flakiness in case the port is already in use.
  • Loading branch information
tsnoam committed Dec 23, 2024
1 parent ace8389 commit 7a6e8b0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
54 changes: 48 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ collection_macros = "0.2.0"
rstest = "0.23.0"
serial_test = "3.2.0"
derive_more = { version = "1.0.0", features = ["from"] }
cfg-if = "1.0.0"

[target.'cfg(target_os = "macos")'.dev-dependencies]
get_if_addrs = "0.5.3"


[profile.release]
lto = "fat"
Expand Down
30 changes: 24 additions & 6 deletions src/protocols/tcp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,33 @@ mod tests {

#[tokio::test]
async fn test_proxy_connection() {
let server_addr: SocketAddr = "[::1]:1236".parse().unwrap();
let server = TcpListener::bind(server_addr).await.unwrap();
cfg_if::cfg_if! {
if #[cfg(not(target_os = "macos"))] {
let network_name = "host";
let host = "127.0.0.1";
} else {
let network_name = "wstunnel_test_proxy_connection";
let host = get_if_addrs::get_if_addrs()
.unwrap()
.into_iter()
.map(|iface| iface.addr.ip())
.find(|ip| ip.is_ipv4() && !ip.is_loopback())
.unwrap();
}
}

let mitm_proxy: ContainerAsync<MitmProxy> = MitmProxy.with_network(network_name).start().await.unwrap();

let proxy_port = mitm_proxy.get_host_port_ipv4(8080).await.unwrap();

let _mitm_proxy: ContainerAsync<MitmProxy> = MitmProxy.with_network("host".to_string()).start().await.unwrap();
// bind to a dynamic port - avoid conflicts
let server = TcpListener::bind((host, 0)).await.unwrap();
let server_port = server.local_addr().unwrap().port();

let mut client = connect_with_http_proxy(
&"http://localhost:8080".parse().unwrap(),
&Host::Domain("[::1]".to_string()),
1236,
&Url::parse(&format!("http://127.0.0.1:{proxy_port}")).unwrap(),
&Host::Domain(host.to_string()),
server_port,
None,
Duration::from_secs(1),
&DnsResolver::System,
Expand Down

0 comments on commit 7a6e8b0

Please sign in to comment.