diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-05-17 08:19:28 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-06-20 08:08:31 -0300 |
| commit | 0777129b027087ea8a5d1d47cfa3f5a7f14f3de8 (patch) | |
| tree | 2e1ebe6221235b16c6432fc4c1ff8e10034ddaec | |
| parent | c09b1cd6c16c2d5d73948a3885c180aba9622844 (diff) | |
| download | mullvadvpn-0777129b027087ea8a5d1d47cfa3f5a7f14f3de8.tar.xz mullvadvpn-0777129b027087ea8a5d1d47cfa3f5a7f14f3de8.zip | |
Create initial mock executable
Simply creates a special file in the current working directory, prints
its command line arguments into it and waits for it to be removed so it
finishes.
| -rw-r--r-- | Cargo.lock | 3 | ||||
| -rw-r--r-- | mullvad-tests/Cargo.toml | 1 | ||||
| -rw-r--r-- | mullvad-tests/src/bin/mock_openvpn.rs | 58 |
3 files changed, 62 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock index 6dc877d08f..10e0145904 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -834,6 +834,9 @@ dependencies = [ [[package]] name = "mullvad-tests" version = "0.1.0" +dependencies = [ + "notify 4.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "mullvad-types" diff --git a/mullvad-tests/Cargo.toml b/mullvad-tests/Cargo.toml index 0c9dec8780..5b3df950df 100644 --- a/mullvad-tests/Cargo.toml +++ b/mullvad-tests/Cargo.toml @@ -6,3 +6,4 @@ description = "Mullvad test specific modules and binaries" license = "GPL-3.0" [dependencies] +notify = "4.0" diff --git a/mullvad-tests/src/bin/mock_openvpn.rs b/mullvad-tests/src/bin/mock_openvpn.rs new file mode 100644 index 0000000000..b5c1ff8be4 --- /dev/null +++ b/mullvad-tests/src/bin/mock_openvpn.rs @@ -0,0 +1,58 @@ +extern crate notify; + +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::sync::mpsc; + +use notify::{raw_watcher, RawEvent, RecursiveMode, Watcher}; + +fn main() { + let (file, path) = create_args_file(); + + write_command_line(file); + wait_for_file_to_be_deleted(path); +} + +fn create_args_file() -> (File, PathBuf) { + let path = PathBuf::from( + env::var_os("MOCK_OPENVPN_ARGS_FILE").expect("Missing mock OpenVPN arguments file path"), + ); + let file = File::create(&path).expect("Failed to create mock OpenVPN arguments file"); + + (file, path) +} + +fn write_command_line(mut file: File) { + for argument in env::args() { + let escaped_argument = argument + .replace("\\", "\\\\") + .replace("\n", "\\n") + .replace("\r", "\\r"); + + writeln!(file, "{}", escaped_argument).expect("Failed to write argument to file"); + } +} + +fn wait_for_file_to_be_deleted<P: AsRef<Path>>(file: P) { + let file = file.as_ref(); + let (tx, rx) = mpsc::channel(); + + let mut watcher = raw_watcher(tx).expect(&format!( + "Failed to create file watcher for \"{}\"", + file.display() + )); + + watcher + .watch(&file, RecursiveMode::NonRecursive) + .expect(&format!("Failed to watch file: {}", file.display())); + + for event in rx { + if let RawEvent { op: Ok(op), .. } = event { + if op.contains(notify::op::REMOVE) { + break; + } + } + } +} |
