diff options
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | mullvad-daemon/Cargo.toml | 1 | ||||
| -rw-r--r-- | mullvad-daemon/tests/common/mod.rs | 55 |
3 files changed, 44 insertions, 13 deletions
diff --git a/Cargo.lock b/Cargo.lock index 46cd320f69..96396c2e4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -779,6 +779,7 @@ dependencies = [ "talpid-core 0.1.0", "talpid-ipc 0.1.0", "talpid-types 0.1.0", + "tempfile 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-timer 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml index 0bcc98b573..26961a6265 100644 --- a/mullvad-daemon/Cargo.toml +++ b/mullvad-daemon/Cargo.toml @@ -48,3 +48,4 @@ assert_matches = "1.0" duct = "0.10" notify = "4.0" os_pipe = "0.6" +tempfile = "3.0" diff --git a/mullvad-daemon/tests/common/mod.rs b/mullvad-daemon/tests/common/mod.rs index 31107955bb..a93d1b1528 100644 --- a/mullvad-daemon/tests/common/mod.rs +++ b/mullvad-daemon/tests/common/mod.rs @@ -6,10 +6,11 @@ extern crate mullvad_ipc_client; extern crate mullvad_paths; extern crate notify; extern crate os_pipe; +extern crate tempfile; -use std::fs::{self, File}; -use std::io::{BufRead, BufReader, Write}; -use std::path::Path; +use std::fs; +use std::io::{BufRead, BufReader}; +use std::path::{Path, PathBuf}; use std::sync::{mpsc, Arc, Mutex}; use std::thread; use std::time::{Duration, Instant}; @@ -19,6 +20,7 @@ use duct; use self::mullvad_ipc_client::DaemonRpcClient; use self::notify::{op, RawEvent, RecursiveMode, Watcher}; use self::os_pipe::{pipe, PipeReader}; +use self::tempfile::TempDir; #[cfg(unix)] pub static DAEMON_EXECUTABLE_PATH: &str = "../target/debug/mullvad-daemon"; @@ -67,31 +69,57 @@ pub fn wait_for_file_write_finish<P: AsRef<Path>>(file_path: P, timeout: Duratio } } -fn prepare_relay_list<T: AsRef<Path>>(path: T) { - let path = path.as_ref(); +fn prepare_test_dirs() -> (TempDir, PathBuf) { + let temp_dir = TempDir::new().expect("Failed to create temporary daemon directory"); + let resource_dir = temp_dir.path().join("resource-dir"); - if !path.exists() { - File::create(path) - .expect("Failed to create relay list file") - .write_all(b"{ \"countries\": [] }") - .expect("Failed to write relay list"); - } + fs::create_dir(&resource_dir).expect("Failed to create resource directory"); + + prepare_relay_list(resource_dir.join("relays.json")); + + (temp_dir, resource_dir) +} + +fn prepare_relay_list<T: AsRef<Path>>(path: T) { + fs::write( + path, + r#"{ + "countries": [{ + "name": "Mockland", + "code": "fake", + "latitude": -91, + "longitude": 0, + "relays": [{ + "hostname": "fake-mockland", + "ipv4_addr_in": "192.168.0.100", + "ipv4_addr_exit": "192.168.0.101", + "include_in_country": true, + "weight": 100, + "tunnels": { + "openvpn": [ { "port": 10000, "protocol": "udp" } ], + "wireguard": [], + }, + }], + }] + }"#, + ).expect("Failed to create mock relay list file"); } pub struct DaemonRunner { process: Option<duct::Handle>, output: Arc<Mutex<BufReader<PipeReader>>>, + _temp_dir: TempDir, } impl DaemonRunner { pub fn spawn() -> Self { - prepare_relay_list("../dist-assets/relays.json"); + let (temp_dir, resource_dir) = prepare_test_dirs(); let (reader, writer) = pipe().expect("Failed to open pipe to connect to daemon"); let process = cmd!(DAEMON_EXECUTABLE_PATH, "-v", "--disable-log-to-file") .dir("..") .env("MULLVAD_CACHE_DIR", "./") - .env("MULLVAD_RESOURCE_DIR", "./dist-assets") + .env("MULLVAD_RESOURCE_DIR", resource_dir) .stderr_to_stdout() .stdout_handle(writer) .start() @@ -100,6 +128,7 @@ impl DaemonRunner { DaemonRunner { process: Some(process), output: Arc::new(Mutex::new(BufReader::new(reader))), + _temp_dir: temp_dir, } } |
