ptth/src/tests.rs

208 lines
5.8 KiB
Rust
Raw Normal View History

2020-11-26 23:33:10 +00:00
use std::{
convert::{TryFrom, TryInto},
path::PathBuf,
2020-11-26 23:33:10 +00:00
sync::{
Arc,
},
time::Duration,
};
use tokio::{
runtime::Runtime,
spawn,
sync::oneshot,
time::delay_for,
};
use reqwest::Client;
use tracing::{debug, info};
use ptth_relay::load_templates;
2020-11-26 23:33:10 +00:00
#[test]
fn end_to_end () {
use ptth_relay::key_validity::BlakeHashWrapper;
2020-11-26 23:33:10 +00:00
// Prefer this form for tests, since all tests share one process
// and we don't care if another test already installed a subscriber.
//tracing_subscriber::fmt ().try_init ().ok ();
let mut rt = Runtime::new ().expect ("Can't create runtime for testing");
2020-11-26 23:33:10 +00:00
// Spawn the root task
rt.block_on (async {
let server_name = "aliens_wildland";
let api_key = "AnacondaHardcoverGrannyUnlatchLankinessMutate";
let tripcode = BlakeHashWrapper::from_key (api_key.as_bytes ());
debug! ("Relay is expecting tripcode {}", tripcode.encode_base64 ());
2020-11-27 00:20:18 +00:00
let config_file = ptth_relay::config::file::Config {
iso: Default::default (),
2020-11-26 23:33:10 +00:00
port: None,
2020-11-30 15:55:14 +00:00
servers: vec! [
ptth_relay::config::file::Server {
name: server_name.to_string (),
2020-11-26 23:33:10 +00:00
tripcode,
display_name: None,
},
2020-11-30 15:55:14 +00:00
],
scraper_keys: vec! [],
2020-11-26 23:33:10 +00:00
};
let config = ptth_relay::config::Config::try_from (config_file).expect ("Can't load config");
2020-11-29 16:58:56 +00:00
let relay_state = Arc::new (ptth_relay::RelayState::try_from (config).expect ("Can't create relay state"));
2020-11-26 23:33:10 +00:00
let relay_state_2 = relay_state.clone ();
let (stop_relay_tx, stop_relay_rx) = oneshot::channel ();
let task_relay = spawn (async move {
ptth_relay::run_relay (
relay_state_2,
Arc::new (load_templates (&PathBuf::new ())?),
stop_relay_rx,
None
).await
2020-11-26 23:33:10 +00:00
});
assert! (relay_state.list_servers ().await.is_empty ());
let relay_url = "http://127.0.0.1:4000";
let config_file = ptth_server::ConfigFile {
2020-11-26 23:33:10 +00:00
name: server_name.into (),
api_key: api_key.into (),
relay_url: "http://127.0.0.1:4000/7ZSFUKGV".into (),
file_server_root: None,
};
let (stop_server_tx, stop_server_rx) = oneshot::channel ();
let task_server = {
spawn (async move {
ptth_server::run_server (config_file, stop_server_rx, None, None).await
2020-11-26 23:33:10 +00:00
})
};
2020-12-10 06:24:56 +00:00
delay_for (Duration::from_millis (1000)).await;
2020-11-26 23:33:10 +00:00
assert_eq! (relay_state.list_servers ().await, vec! [
server_name.to_string (),
]);
let client = Client::builder ()
.timeout (Duration::from_secs (2))
.build ().expect ("Couldn't build HTTP client");
2020-11-26 23:33:10 +00:00
let resp = client.get (&format! ("{}/frontend/relay_up_check", relay_url))
.send ().await.expect ("Couldn't check if relay is up").bytes ().await.expect ("Couldn't check if relay is up");
2020-11-26 23:33:10 +00:00
assert_eq! (resp, "Relay is up\n");
let resp = client.get (&format! ("{}/frontend/servers/{}/files/COPYING", relay_url, server_name))
.send ().await.expect ("Couldn't find license").bytes ().await.expect ("Couldn't find license");
2020-11-26 23:33:10 +00:00
if blake3::hash (&resp) != blake3::Hash::from ([
0xca, 0x02, 0x92, 0x78,
0x9c, 0x0a, 0x0e, 0xcb,
0xa7, 0x06, 0xf4, 0xb3,
0xf3, 0x49, 0x30, 0x07,
0xa9, 0x95, 0x17, 0x31,
0xc1, 0xd4, 0x32, 0xc5,
0x2c, 0x4a, 0xac, 0x1f,
0x1a, 0xbb, 0xa8, 0xef,
]) {
panic! ("{}", String::from_utf8 (resp.to_vec ()).expect ("???"));
2020-11-26 23:33:10 +00:00
}
// Requesting a file from a server that isn't registered
// will error out
let resp = client.get (&format! ("{}/frontend/servers/obviously_this_server_does_not_exist/files/COPYING", relay_url))
.send ().await.expect ("Couldn't send request to bogus server");
2020-11-26 23:33:10 +00:00
assert_eq! (resp.status (), reqwest::StatusCode::NOT_FOUND);
info! ("Shutting down end-to-end test");
stop_server_tx.send (()).expect ("Couldn't shut down server");
stop_relay_tx.send (()).expect ("Couldn't shut down relay");
2020-11-26 23:33:10 +00:00
info! ("Sent stop messages");
task_relay.await.expect ("Couldn't join relay").expect ("Relay error");
2020-11-26 23:33:10 +00:00
info! ("Relay stopped");
task_server.await.expect ("Couldn't join server").expect ("Server error");
2020-11-26 23:33:10 +00:00
info! ("Server stopped");
});
}
#[test]
fn scraper_endpoints () {
let mut rt = Runtime::new ().expect ("Can't create runtime for testing");
rt.block_on (async {
use ptth_relay::*;
let config_file = config::file::Config {
iso: config::file::Isomorphic {
enable_scraper_api: true,
dev_mode: Default::default (),
},
port: Some (4001),
servers: vec! [],
scraper_keys: vec! [
key_validity::ScraperKey::new_30_day ("automated testing", b"bogus")
],
};
let config = config::Config::try_from (config_file).expect ("Can't load config");
let relay_state = Arc::new (RelayState::try_from (config).expect ("Can't create relay state"));
let relay_state_2 = relay_state.clone ();
let (stop_relay_tx, stop_relay_rx) = oneshot::channel ();
let task_relay = spawn (async move {
run_relay (
relay_state_2,
Arc::new (load_templates (&PathBuf::new ())?),
stop_relay_rx,
None
).await
});
let relay_url = "http://127.0.0.1:4001";
let mut headers = reqwest::header::HeaderMap::new ();
headers.insert ("X-ApiKey", "bogus".try_into ().unwrap ());
let client = Client::builder ()
.default_headers (headers)
.timeout (Duration::from_secs (2))
.build ().expect ("Couldn't build HTTP client");
let mut resp = None;
for _ in 0usize..5 {
let x = client.get (&format! ("{}/scraper/api/test", relay_url))
.send ().await;
match x {
Err (_) => {
// Probably a reqwest error cause the port is in
// use or something. Try again.
},
Ok (x) => {
resp = Some (x);
break;
},
};
delay_for (Duration::from_millis (200)).await;
}
let resp = resp.expect ("Reqwest repeatedly failed to connect to the relay");
let resp = resp.bytes ().await.expect ("Couldn't check if relay is up");
assert_eq! (resp, "You're valid!\n");
stop_relay_tx.send (()).expect ("Couldn't shut down relay");
task_relay.await.expect ("Couldn't join relay").expect ("Relay error");
});
}