Add Tor feature flag and only run test if enabled

This commit is contained in:
Philipp Hoenisch 2020-10-20 14:33:32 +11:00
parent 5e19949d71
commit 295216a8ee
No known key found for this signature in database
GPG Key ID: E5F8E74C672BC666
4 changed files with 77 additions and 64 deletions

View File

@ -46,6 +46,9 @@ jobs:
build_test: build_test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Install tor
run: sudo apt-get tor
- name: Checkout sources - name: Checkout sources
uses: actions/checkout@v2 uses: actions/checkout@v2

View File

@ -22,7 +22,7 @@ serde_json = "1"
sha2 = "0.9" sha2 = "0.9"
thiserror = "1" thiserror = "1"
tokio = { version = "0.2", default-features = false, features = ["blocking", "macros", "rt-core", "time", "rt-threaded"] } 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" tracing = "0.1"
[dev-dependencies] [dev-dependencies]
@ -35,3 +35,7 @@ spectral = "0.6"
testcontainers = "0.10" testcontainers = "0.10"
tracing = "0.1" tracing = "0.1"
tracing-subscriber = "0.2" tracing-subscriber = "0.2"
[features]
default = []
tor = ["torut"]

View File

@ -49,5 +49,6 @@ pub mod alice;
pub mod bitcoin; pub mod bitcoin;
pub mod bob; pub mod bob;
pub mod monero; pub mod monero;
#[cfg(feature = "tor")]
pub mod tor; pub mod tor;
pub mod transport; pub mod transport;

View File

@ -1,64 +1,69 @@
use anyhow::Result; #[cfg(feature = "tor")]
use hyper::service::{make_service_fn, service_fn}; mod tor_test {
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( use anyhow::Result;
_req: hyper::Request<hyper::Body>, use hyper::service::{make_service_fn, service_fn};
) -> Result<hyper::Response<hyper::Body>, Infallible> { use reqwest::StatusCode;
Ok(hyper::Response::new("Hello World".into())) use spectral::prelude::*;
} use std::convert::Infallible;
use tokio::sync::oneshot::Receiver;
fn start_test_service(port: u16, rx: Receiver<()>) { use torut::onion::TorSecretKeyV3;
let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(hello_world)) }); use xmr_btc::tor::{AuthenticatedConnection, TOR_PROXY_ADDR};
let addr = ([127, 0, 0, 1], port).into();
let server = hyper::Server::bind(&addr).serve(make_svc); async fn hello_world(
let graceful = server.with_graceful_shutdown(async { _req: hyper::Request<hyper::Body>,
rx.await.ok(); ) -> Result<hyper::Response<hyper::Body>, Infallible> {
}); Ok(hyper::Response::new("Hello World".into()))
tokio::spawn(async { }
// server.await.unwrap();
if let Err(e) = graceful.await { fn start_test_service(port: u16, rx: Receiver<()>) {
eprintln!("server error: {}", e); 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 {
#[tokio::test] rx.await.ok();
async fn test_tor_control_port() -> Result<()> { });
// Setup test HTTP Server tokio::spawn(async {
let (tx, rx) = tokio::sync::oneshot::channel::<()>(); // server.await.unwrap();
let port = 8080; if let Err(e) = graceful.await {
start_test_service(port, rx); eprintln!("server error: {}", e);
}
// Connect to local Tor service });
let mut authenticated_connection = AuthenticatedConnection::new().await?; }
// Expose an onion service that re-directs to the echo server. #[tokio::test]
let tor_secret_key_v3 = TorSecretKeyV3::generate(); async fn test_tor_control_port() -> Result<()> {
authenticated_connection // Setup test HTTP Server
.add_service(port, &tor_secret_key_v3) let (tx, rx) = tokio::sync::oneshot::channel::<()>();
.await?; let port = 8080;
start_test_service(port, rx);
// Test if Tor service forwards to HTTP Server
// Connect to local Tor service
let proxy = reqwest::Proxy::all(format!("socks5h://{}", *TOR_PROXY_ADDR).as_str()) let mut authenticated_connection = AuthenticatedConnection::new().await?;
.expect("tor proxy should be there");
let client = reqwest::Client::builder().proxy(proxy).build().unwrap(); // Expose an onion service that re-directs to the echo server.
let onion_address = tor_secret_key_v3.public().get_onion_address().to_string(); let tor_secret_key_v3 = TorSecretKeyV3::generate();
let onion_url = format!("http://{}:8080", onion_address); authenticated_connection
.add_service(port, &tor_secret_key_v3)
let res = client.get(&onion_url).send().await?; .await?;
assert_that(&res.status()).is_equal_to(StatusCode::OK);
// Test if Tor service forwards to HTTP Server
let text = res.text().await?;
assert_that!(text).contains("Hello World"); let proxy = reqwest::Proxy::all(format!("socks5h://{}", *TOR_PROXY_ADDR).as_str())
.expect("tor proxy should be there");
// gracefully shut down server let client = reqwest::Client::builder().proxy(proxy).build().unwrap();
let _ = tx.send(()); let onion_address = tor_secret_key_v3.public().get_onion_address().to_string();
Ok(()) 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(())
}
} }