Make it work on CI

This commit is contained in:
Philipp Hoenisch 2020-10-21 17:53:49 +11:00
parent e1d8a1c39b
commit ae1ea08bb2
No known key found for this signature in database
GPG Key ID: E5F8E74C672BC666
4 changed files with 54 additions and 24 deletions

View File

@ -46,12 +46,20 @@ jobs:
build_test:
runs-on: ubuntu-latest
steps:
- name: Install tor
run: sudo apt-get tor
- name: Checkout sources
uses: actions/checkout@v2
- name: Install and stop tor in case it was running
run: |
sudo apt install software-properties-common
sudo curl https://deb.torproject.org/torproject.org/A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89.asc | sudo gpg --import
sudo gpg --export A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89 | sudo apt-key add -
sudo add-apt-repository 'deb https://deb.torproject.org/torproject.org bionic main'
sudo apt update
sudo apt install tor deb.torproject.org-keyring
sudo /etc/init.d/tor stop
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:

View File

@ -33,6 +33,7 @@ hyper = "0.13"
monero-harness = { path = "../monero-harness" }
port_check = "0.1"
spectral = "0.6"
tempfile = "3"
testcontainers = "0.10"
tracing = "0.1"
tracing-subscriber = "0.2"

View File

@ -112,6 +112,6 @@ impl AuthenticatedConnection {
.iter(),
)
.await
.map_err(|_| anyhow!("Could not add onion service."))
.map_err(|e| anyhow!("Could not add onion service.: {:#?}", e))
}
}

View File

@ -5,12 +5,14 @@ mod tor_test {
use hyper::service::{make_service_fn, service_fn};
use reqwest::StatusCode;
use spectral::prelude::*;
use std::{convert::Infallible, process::Child};
use std::{convert::Infallible, fs};
use tempfile::{Builder, NamedTempFile};
use tokio::sync::oneshot::Receiver;
use torut::{
onion::TorSecretKeyV3,
utils::{run_tor, AutoKillChild},
};
use tracing_subscriber::util::SubscriberInitExt;
use xmr_btc::tor::UnauthenticatedConnection;
async fn hello_world(
@ -32,9 +34,16 @@ mod tor_test {
eprintln!("server error: {}", e);
}
});
tracing::info!("Test server started at port: {}", port);
}
fn run_tmp_tor() -> (Child, u16, u16) {
fn run_tmp_tor() -> Result<(AutoKillChild, u16, u16, NamedTempFile)> {
// we create an empty torrc file to not use the system one
let temp_torrc = Builder::new().tempfile()?;
let torrc_file = format!("{}", fs::canonicalize(temp_torrc.path())?.display());
tracing::info!("Temp torrc file created at: {}", torrc_file);
let control_port = if port_check::is_local_port_free(9051) {
9051
} else {
@ -46,30 +55,33 @@ mod tor_test {
port_check::free_local_port().unwrap()
};
(
run_tor(
"tor",
&mut [
"--CookieAuthentication",
"1",
"--ControlPort",
control_port.to_string().as_str(),
"--SocksPort",
proxy_port.to_string().as_str(),
]
.iter(),
)
.expect("Starting tor filed"),
control_port,
proxy_port,
)
let child = run_tor(
"tor",
&mut [
"--CookieAuthentication",
"1",
"--ControlPort",
control_port.to_string().as_str(),
"--SocksPort",
proxy_port.to_string().as_str(),
"-f",
&torrc_file,
]
.iter(),
)?;
tracing::info!("Tor running with pid: {}", child.id());
let child = AutoKillChild::new(child);
Ok((child, control_port, proxy_port, temp_torrc))
}
#[tokio::test]
async fn test_tor_control_port() -> Result<()> {
let _guard = tracing_subscriber::fmt()
.with_env_filter("info")
.set_default();
// start tmp tor
let (child, control_port, proxy_port) = run_tmp_tor();
let _child = AutoKillChild::new(child);
let (_child, control_port, proxy_port, _tmp_torrc) = run_tmp_tor()?;
// Setup test HTTP Server
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
@ -82,6 +94,8 @@ mod tor_test {
.init_authenticated_connection()
.await?;
tracing::info!("Tor authenticated.");
// Expose an onion service that re-directs to the echo server.
let tor_secret_key_v3 = TorSecretKeyV3::generate();
authenticated_connection
@ -96,11 +110,18 @@ mod tor_test {
let onion_address = tor_secret_key_v3.public().get_onion_address().to_string();
let onion_url = format!("http://{}:8080", onion_address);
tracing::info!("Tor service added: {}", onion_url);
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");
tracing::info!(
"Local server called via Tor proxy. Its response is: {}",
text
);
// gracefully shut down server
let _ = tx.send(());