From 295216a8eed7dbaf1670fd4aacfd3ee32dbee66c Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Tue, 20 Oct 2020 14:33:32 +1100 Subject: [PATCH] Add Tor feature flag and only run test if enabled --- .github/workflows/ci.yml | 3 + xmr-btc/Cargo.toml | 8 ++- xmr-btc/src/lib.rs | 1 + xmr-btc/tests/tor.rs | 129 ++++++++++++++++++++------------------- 4 files changed, 77 insertions(+), 64 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 739fe386..4c41a63a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,6 +46,9 @@ jobs: build_test: runs-on: ubuntu-latest steps: + - name: Install tor + run: sudo apt-get tor + - name: Checkout sources uses: actions/checkout@v2 diff --git a/xmr-btc/Cargo.toml b/xmr-btc/Cargo.toml index 767e8d49..57a9535d 100644 --- a/xmr-btc/Cargo.toml +++ b/xmr-btc/Cargo.toml @@ -22,7 +22,7 @@ serde_json = "1" sha2 = "0.9" thiserror = "1" tokio = { version = "0.2", default-features = false, features = ["blocking", "macros", "rt-core", "time", "rt-threaded"] } -torut = "0.1" +torut = { version = "0.1", optional = true } tracing = "0.1" [dev-dependencies] @@ -34,4 +34,8 @@ monero-harness = { path = "../monero-harness" } spectral = "0.6" testcontainers = "0.10" tracing = "0.1" -tracing-subscriber = "0.2" \ No newline at end of file +tracing-subscriber = "0.2" + +[features] +default = [] +tor = ["torut"] diff --git a/xmr-btc/src/lib.rs b/xmr-btc/src/lib.rs index f4638dc4..9526bad4 100644 --- a/xmr-btc/src/lib.rs +++ b/xmr-btc/src/lib.rs @@ -49,5 +49,6 @@ pub mod alice; pub mod bitcoin; pub mod bob; pub mod monero; +#[cfg(feature = "tor")] pub mod tor; pub mod transport; diff --git a/xmr-btc/tests/tor.rs b/xmr-btc/tests/tor.rs index ff667231..6deac6b3 100644 --- a/xmr-btc/tests/tor.rs +++ b/xmr-btc/tests/tor.rs @@ -1,64 +1,69 @@ -use anyhow::Result; -use hyper::service::{make_service_fn, service_fn}; -use reqwest::StatusCode; -use spectral::prelude::*; -use std::convert::Infallible; -use tokio::sync::oneshot::Receiver; -use torut::onion::TorSecretKeyV3; -use xmr_btc::tor::{AuthenticatedConnection, TOR_PROXY_ADDR}; +#[cfg(feature = "tor")] +mod tor_test { -async fn hello_world( - _req: hyper::Request, -) -> Result, Infallible> { - Ok(hyper::Response::new("Hello World".into())) -} - -fn start_test_service(port: u16, rx: Receiver<()>) { - let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(hello_world)) }); - let addr = ([127, 0, 0, 1], port).into(); - let server = hyper::Server::bind(&addr).serve(make_svc); - let graceful = server.with_graceful_shutdown(async { - rx.await.ok(); - }); - tokio::spawn(async { - // server.await.unwrap(); - if let Err(e) = graceful.await { - eprintln!("server error: {}", e); - } - }); -} - -#[tokio::test] -async fn test_tor_control_port() -> Result<()> { - // Setup test HTTP Server - let (tx, rx) = tokio::sync::oneshot::channel::<()>(); - let port = 8080; - start_test_service(port, rx); - - // Connect to local Tor service - let mut authenticated_connection = AuthenticatedConnection::new().await?; - - // Expose an onion service that re-directs to the echo server. - let tor_secret_key_v3 = TorSecretKeyV3::generate(); - authenticated_connection - .add_service(port, &tor_secret_key_v3) - .await?; - - // Test if Tor service forwards to HTTP Server - - let proxy = reqwest::Proxy::all(format!("socks5h://{}", *TOR_PROXY_ADDR).as_str()) - .expect("tor proxy should be there"); - let client = reqwest::Client::builder().proxy(proxy).build().unwrap(); - let onion_address = tor_secret_key_v3.public().get_onion_address().to_string(); - let onion_url = format!("http://{}:8080", onion_address); - - let res = client.get(&onion_url).send().await?; - assert_that(&res.status()).is_equal_to(StatusCode::OK); - - let text = res.text().await?; - assert_that!(text).contains("Hello World"); - - // gracefully shut down server - let _ = tx.send(()); - Ok(()) + use anyhow::Result; + use hyper::service::{make_service_fn, service_fn}; + use reqwest::StatusCode; + use spectral::prelude::*; + use std::convert::Infallible; + use tokio::sync::oneshot::Receiver; + use torut::onion::TorSecretKeyV3; + use xmr_btc::tor::{AuthenticatedConnection, TOR_PROXY_ADDR}; + + async fn hello_world( + _req: hyper::Request, + ) -> Result, Infallible> { + Ok(hyper::Response::new("Hello World".into())) + } + + fn start_test_service(port: u16, rx: Receiver<()>) { + let make_svc = + make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(hello_world)) }); + let addr = ([127, 0, 0, 1], port).into(); + let server = hyper::Server::bind(&addr).serve(make_svc); + let graceful = server.with_graceful_shutdown(async { + rx.await.ok(); + }); + tokio::spawn(async { + // server.await.unwrap(); + if let Err(e) = graceful.await { + eprintln!("server error: {}", e); + } + }); + } + + #[tokio::test] + async fn test_tor_control_port() -> Result<()> { + // Setup test HTTP Server + let (tx, rx) = tokio::sync::oneshot::channel::<()>(); + let port = 8080; + start_test_service(port, rx); + + // Connect to local Tor service + let mut authenticated_connection = AuthenticatedConnection::new().await?; + + // Expose an onion service that re-directs to the echo server. + let tor_secret_key_v3 = TorSecretKeyV3::generate(); + authenticated_connection + .add_service(port, &tor_secret_key_v3) + .await?; + + // Test if Tor service forwards to HTTP Server + + let proxy = reqwest::Proxy::all(format!("socks5h://{}", *TOR_PROXY_ADDR).as_str()) + .expect("tor proxy should be there"); + let client = reqwest::Client::builder().proxy(proxy).build().unwrap(); + let onion_address = tor_secret_key_v3.public().get_onion_address().to_string(); + let onion_url = format!("http://{}:8080", onion_address); + + let res = client.get(&onion_url).send().await?; + assert_that(&res.status()).is_equal_to(StatusCode::OK); + + let text = res.text().await?; + assert_that!(text).contains("Hello World"); + + // gracefully shut down server + let _ = tx.send(()); + Ok(()) + } }